function set_tofrom(obj)
{
	function _chmode(name, mode)
	{
		var ar = document.getElementsByName(name);
		for (var i=0; i<ar.length; i++){
			ar[i].style.display = mode;	
		}
	}	
	if(obj.options[obj.selectedIndex].text == "") {
		return;
	}
	if (obj.options[obj.selectedIndex].value <= 1) {
		_chmode('countrysec', 'none');
		_chmode('customsec', '');
	} else {	
		_chmode('countrysec', '');
		_chmode('customsec', 'none');
	}
}

function openNewWindow(url, w, h)
{
	// open a blank window
	w = (w==null) ? 500 : w;
	h = (h==null) ? 400 : h;
	var aWindow = window.open(url, '_blank',
	'scrollbars=yes,resizable=yes,toolbar=no,menubar=no,location=no,top=0,left=0,width='+w+',height='+h);
}

function swapView(id)
{
	obj = document.getElementById('sub'+id);
	img = document.getElementById('icon'+id);
	if(obj.style.display!='block'){
		obj.style.display = 'block';
		if(img){
			img.src = '../images/btn-less.png';
			img.style.background = 'url(../images/btn-less.png)';
		}
	} else {
		obj.style.display = 'none';
		if(img){
			img.src = '../images/btn-more.png';
			img.style.background = 'url(../images/btn-more.png)';
		}
	}		
}

function swapDisplay(id)
{
	obj = document.getElementById(id);
	if(obj.style.display=='none'){
		obj.style.display = 'block';
	} else {
		obj.style.display = 'none';
	}		
}

function setCookie(cName, cValue)
{
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = cName + "=" + escape (cValue) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");	
}

function getCookie(keyName)
{
	var nameEQ = keyName + "=";
	var ca = document.cookie.split (';');
	for (var i = 0; i < ca.length; i ++)
	{
		var c = ca [i];
		while (c.charAt (0) == ' ') c = c.substring (1, c.length);
		if (c.indexOf (nameEQ) == 0) return c.substring (nameEQ.length, c.length);
	}
	return null;
}

function deleteCookie(cName)
{
	setCookie(cName, "", -1);
}

function sortList(name, value)
{
	if(getCookie(name+'sortBy')!=value) {
		setCookie(name+'sortOrder', 'ASC');
	} else {
		setCookie(name+'sortOrder', getCookie(name+'sortOrder')=='DESC'?'ASC':'DESC');
	}
	setCookie(name+'sortBy', value);
	document.location.href = document.location.href.replace(/&page=[0-9]*/, '');
}

function filter(path, obj)
{
	var name = path + obj.name;
	var value = obj.options[obj.selectedIndex].value;
	setCookie(name, value);
	clearSearch();  // clear page & query and reload
}

function goto(page)
{
	// my paging is n=zzz this is the page number page are start from 1
	q = document.location.href;
	var patern = /^(.*)(\?|\&)page=(\d+)(\&?.*)$/;
	var reg = new RegExp(patern);
	if(!reg.test(q))
	{						// check if n=zzz dose not exists, add n=1 at the end of query
		var reg2 = new RegExp(/.*\?.*/);
		if(reg2.test (q))					// add ?n=zzz or &n=zzz
			q = q + "&page=1"; 
		else
			q = q + "?page=1";				
	}
	if(page == "prev")
	{						// goto previous page
		Page = Number(q.replace (patern, "$3")) - 1;
	}
	else
	{
		if(page=="next")
		{					// goto next page
			Page = Number(q.replace (patern, "$3")) + 1;
		}
		else
		{
			Page = Number(page);				// goto page n
		}
	}	
	q = q.replace(patern, "$1$2page=" + String(Page) + "$4");
	document.location.href = q;
}

function clearSearch()
{
	document.location.href = document.location.href.replace(/&page=[0-9]*/, '').replace(/&showall=1/, '')+'&showall=1';		
}

function ajaxDelete(action, objName, sender)
{
	SendServerRequest(action);
	while(sender = sender.parentNode){
		if(sender.className == objName || sender.id == objName || sender.name == objName){
			sender.style.display = 'none';
			if(sender.tagName=="TR") {
				sender.parentNode.deleteRow(sender.sectionRowIndex);
			}
			return true;
		}
	}
	return false;
}

var sdiv;
var active;
var trid;
var stime;
function showStatusBox(id, obj)
{
	active = obj;
	trid = id;
	clearInterval(stime);
	if(!sdiv){										// create status dropdown div
		sdiv = document.createElement('DIV');
		sdiv.className='status-box';
		document.body.appendChild(sdiv);
		var img = new Array();						// create images
		for(var i=0; i<3; i++){
			img[i] = new Image();
			img[i].src = '../images/icons/16x16/status-'+i+'.png';
			img[i].style.margin = '3px';
			img[i].style.cursor = 'pointer';
			img[i].style.background = 'url(../images/icons/16x16/status-'+i+'.png)';
			img[i].style.height = '16px';
			img[i].style.width = '73px';
			img[i].id = "status_"+i;
			sdiv.appendChild(img[i]);
			img[i].onclick =  function (){setStatus(this);};
		}
	}
	document.body.onmousedown = function()			// set mousedown event for body to hide the sdiv
		{
			stime = setTimeout("sdiv.style.display='none'", 200);
			document.body.onmousedown = function(){};
		};
	with(sdiv.style){
		position = 'absolute';
		display = "block";
		top = (findPosY(obj) + 20) + 'px';
		left = (findPosX(obj) - 60) + 'px';
	}
}

function setStatus(obj)
{
	var colors = new Array('#909090', '#00AA00', '#AA0000');		// color array of status
	var i = obj.id.substr(7,1);										// extract index from image id (status-X)
	sdiv.style.display='none';
	document.body.onmousedown = function(){};
	SendServerRequest('index.php?section=admin&module=transport-edit&transportid_frm='+trid+'&status='+i);
	active.style.background = 'url(../images/icons/16x16/status-'+i+'.png) right';		// change the active status image
	var par = active;
	while (par = par.parentNode) {				// find this row first td and change the color
		if (par.className == "transport_row") {
			var ch = par.childNodes[0];	
			if(ch.tagName != 'TD'){				// difrence between ie & mozilla
				ch = par.childNodes[1];
			}			
			ch.style.borderRightColor = colors[i];
			break;
		}
	}
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function showDetail(ObjId, module){
	nw = window.open("front.php?section=guest&module="+module+"&frm_id="+ObjId, "Book_Detail", "width=500,height=350,toolbar=no,scrollbars=yes,status=no,menubar=no");
	nw.focus();
}

function setSub(source, destination, opts, cnt)
{	
	if(opts)
	{
		var src = document.getElementById(source);
		if(!src) {
			src = document.getElementsByName(source)[0];	
		}
		if(src.selectedIndex >= 0){
			var pid = src.options[src.selectedIndex].value;
		} else {
			var pid = 0;
		}			
		var cnt2= 0;
		var dest = document.getElementById(destination);
		if(!dest) {
			dest = document.getElementsByName(destination)[0];	
		}
		while(dest.options[cnt])
		{
			dest.options[cnt] = null;	//remove all options
		}
		if(opts[pid])
		{
			while(opts[pid][cnt2])
			{
				ar = opts[pid][cnt2++].split("*:-)");
				dest.options[cnt++] = new Option(ar[1], ar[0]);		//add new options
			}
		}
	}
}

function CheckBoxes(fmobj)
{
	for(var i = 0; i < fmobj.elements.length; i ++)
	{

		var e = fmobj.elements[i];
		if((e.type == 'checkbox') && (e.type == 'checkbox') && (e.checked == true))
		{
			return true;
		}
	}	
}

function CheckAll(fmobj)
{
	for(var i = 0; i < fmobj.elements.length; i ++)
	{
		var e = fmobj.elements[i];
		if((e.name != 'allbox') && (e.type == 'checkbox') && (!e.disabled))
		{
			e.checked = fmobj.allbox.checked;
		}
	}
}

function CheckCheckedAll(fmobj)
{	
	var TotalBoxes = 0;
	var TotalOn = 0;
	for(var i = 0; i < fmobj.elements.length; i ++)
	{
		var e = fmobj.elements[i];
		if((e.name != 'allbox') && (e.type == 'checkbox'))
		{
			TotalBoxes ++;
			if(e.checked)
			{
				TotalOn ++;
			}
		}
	}
	
	if (TotalBoxes == TotalOn)
	{
		fmobj.allbox.checked = true;
		return -1;		// all of them checked
	}
	else
	{
		fmobj.allbox.checked = false;
		return TotalOn;	// none of them checked=0 else some of them checked
	}
}

function sortList(sortBy, sortMode, sortByVal, sortModeVal, url)
{
	setCookie(sortBy, sortByVal);
	setCookie(sortMode, sortModeVal);
	document.location.href = document.location.href.replace(/&page=([0-9]*)/g, '&page=1');
}

// Centeralized Pop-up Window
// PageURL  	: URL of pop-up page
// pageName		: Name of pop-up page
// w			: Width of pop-up page
// h			: Height of pop-up page
// pageScroll	: Scrollbars view or no : 'yes' or 'no'
// pageCenter	: Center of screen view or no : 'yes' or 'no'
function makePopup(pageURL, pageName, w, h, pageScroll, pageCenter)
{
	var popup = null;
	if(pageCenter == 'yes')
	{
		LeftPosition = (screen.width) ? (screen.width-w) / 2 : 0;
		TopPosition = (screen.height) ? (screen.height-h) / 2 : 0;
	}
	else
	{
		LeftPosition = 10;
		TopPosition = 10;
	}

	pageConfig = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=' + pageScroll + ', resizable=yes, width=' + (w + 18) + ', height=' + (h + 18) + ', left=' + LeftPosition + ', top=' + TopPosition;
	popup = window.open(pageURL, pageName, pageConfig);
	popup.focus();
}

function setTend(obj)
{
	var res = getAllChilds(obj.parentNode.parentNode);
	for(var i=0; i<res.length; i++){
		if(res[i].name == "frm_tendid[]"){
			res[i].checked = obj.checked;	
		}
	}
}

function checkTend(obj)
{
	var res = getAllChilds(obj.parentNode.parentNode);
	var flag_and = true;
	for(var i=0; i<res.length; i++){
		if(res[i].name == "frm_tendid[]"){
			flag_and &= res[i].checked;
		}
		if(res[i].tagName == "INPUT" && res[i].name==""){
			var chk = res[i];
		}
	}
	if(chk){
		chk.checked = flag_and;
	}
}

function getAllChilds(obj)
{
	var res = new Array();
	var cnt = 0;
	if(obj && obj.childNodes){
		for(var i=0; i<obj.childNodes.length; i++){
			res[res.length] = obj.childNodes[i];
			if(obj.childNodes[i].firstChild){
				var tmp = getAllChilds(obj.childNodes[i]);
				if(tmp){
					res = res.concat(tmp);
				}
			}
		}
		return res;
	}
}

//	parse URL
function createRequestObject() {
  FORM_DATA = new Object();
    // The Object ("Array") where our data will be stored.
  separator = ',';
    // The token used to separate data from multi-select inputs
  query = '' + this.location;
  qu = query
    // Get the current URL so we can parse out the data.
    // Adding a null-string '' forces an implicit type cast
    // from property to string, for NS2 compatibility.
  query = query.substring((query.indexOf('?')) + 1);
    // Keep everything after the question mark '?'.
  if (query.length < 1) { return false; }  // Perhaps we got some bad data?
  keypairs = new Object();
  numKP = 1;
    // Local vars used to store and keep track of name/value pairs
    // as we parse them back into a usable form.
  while (query.indexOf('&') > -1) {
    keypairs[numKP] = query.substring(0,query.indexOf('&'));
    query = query.substring((query.indexOf('&')) + 1);
    numKP++;
      // Split the query string at each '&', storing the left-hand side
      // of the split in a new keypairs[] holder, and chopping the query
      // so that it gets the value of the right-hand string.
  }
  keypairs[numKP] = query;
    // Store what's left in the query string as the final keypairs[] data.<
  for (i in keypairs) {
    keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
      // Left of '=' is name.
    keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
      // Right of '=' is value.
    while (keyValue.indexOf('+') > -1) {
      keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
        // Replace each '+' in data string with a space.
    }
    keyValue = unescape(keyValue);
      // Unescape non-alphanumerics
    if (FORM_DATA[keyName]) {
      FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
        // Object already exists, it is probably a multi-select input,
        // and we need to generate a separator-delimited string
        // by appending to what we already have stored.
    } else {
      FORM_DATA[keyName] = keyValue;
        // Normal case: name gets value.
    }
  }
  return FORM_DATA;
}

FORM_DATA = createRequestObject();
// This is the array/object containing the GET data.
// Retrieve information with 'FORM_DATA [ key ] = value'.

function doIt(optionValue) {
	if(optionValue == 'در دست ساخت') {
		document.getElementById('processUnit').style.display = '';
	} else {
		document.getElementById('processUnit').style.display = 'none';
	}
}
