// JavaScript Document

var strAjaxElementID,strAjaxLoadingText,strAjaxLoadingTextDots,intAjaxIntervalID,intAjaxInProgress=0;
var strAjaxChkSum,intAjaxRetries,strAjaxUrl,strAjaxPostData;

var bodyKeyDownCode;
var bodyKeyDownCodeRes=',';
function onBodyKeyDown(e)
{
	if(window.event) // IE
	{
		bodyKeyDownCode = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		bodyKeyDownCode = e.which;
	}
	//append down-keys
	if (bodyKeyDownCodeRes.indexOf(','+bodyKeyDownCode+',')==-1) bodyKeyDownCodeRes+=bodyKeyDownCode+',';
	
	if (bodyKeyDownCode>=16 && bodyKeyDownCode<=18)				//16=shift    17=ctrl      18=alt
	{
		//check if bodyFlags-ctrlKeys elemet found
		if(document.getElementById('bodyFlags-ctrlKeys')!=null)
		{
			objTemp=document.getElementById('bodyFlags-ctrlKeys');
			if (objTemp.value.indexOf(','+bodyKeyDownCode+',')==-1)
			{
				if (objTemp.value=='') objTemp.value=',';
				objTemp.value+=bodyKeyDownCode+',';
			}
		}
	}
	return true;
}
function onBodyKeyUp(e)
{
	if(window.event) // IE
	{
		bodyKeyDownCode = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		bodyKeyDownCode = e.which;
	}
	//exclude up-keys from 
	if (bodyKeyDownCodeRes.indexOf(','+bodyKeyDownCode+',')!=-1) bodyKeyDownCodeRes=bodyKeyDownCodeRes.replace(','+bodyKeyDownCode+',',',')
	
	if (bodyKeyDownCode>=16 && bodyKeyDownCode<=18)				//16=shift    17=ctrl      18=alt
	{
		if(document.getElementById('bodyFlags-ctrlKeys')!=null)
		{
			objTemp=document.getElementById('bodyFlags-ctrlKeys');
			if (objTemp.value.indexOf(','+bodyKeyDownCode+',')!=-1)
			{
				objTemp.value=objTemp.value.replace(','+bodyKeyDownCode+',',',')
				if (objTemp.value==',') objTemp.value='';				
			}
		}
	}
	bodyKeyDownCode=0;
	return true;
}

function onBodyFocus()
{
	bodyKeyDownCode=0;
}
function runOnBodyLoad()
{
	//try to focus on first TEXT or COMBO element in first FORM
	//setFocusToFirstControl();
	if (typeof window.onBodyLoad=='function') onBodyLoad();
}

function runOnBodyUnload()
{
	try
	{
		onBodyUnload();
	}
	catch(err)
	{
	}
}

function setFocusToFirstControl()
{
  var bFound = false; 
 //for each form
  for (f=0; f < document.forms.length; f++) 
  {
    //for each element in each form
    for(i=0; i < document.forms[f].length; i++)
    {
      //if it's not a hidden element
			strObjType=document.forms[f][i].type;
      if (strObjType=="text")
      { 
        //and it's not disabled
        if (document.forms[f][i].disabled != true) 
        {
          try {
             //set the focus to it
             document.forms[f][i].focus();
             var bFound = true;
          }
          catch(err){}
        }
      }
      //if found in this element, stop looking
      if (bFound == true)
        break;
    }
    //if found in this form, stop looking
    if (bFound == true)
      break;
  }
}
///////////////////////////////////////////////

function openUrl(strUrl,strPostData)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp!=null)
	{
		if (typeof strPostData=='undefined')
		{
			xmlHttp.open("GET",strUrl,true);
			xmlHttp.send(null);
		}
		else
		{
			xmlHttp.open("POST",strUrl,true);
			xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xmlHttp.setRequestHeader('Referer', 'secretCode')
			xmlHttp.send(strPostData);
		}
	}	
}

function briefFetchData(strUrl,strPostData,strReplyFunctionName,strElement)
{
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	xmlHttp.onreadystatechange=function ()
	{
		if (xmlHttp.readyState==4)
		{
			var responseText=xmlHttp.responseText;
			//alert(responseText);
			responseText=responseText.replace(/'/g,'');
			responseText=responseText.replace(/\\/g,'');
			responseText=responseText.replace(/"/g,'');
			pos1=responseText.indexOf('[');
			pos2=responseText.indexOf(']');
			responseText=responseText.substr(pos1+1,pos2-pos1-1);
			if (typeof strElement!='undefined') document.getElementById(strElement).innerHTML=responseText;
			eval(strReplyFunctionName+'(\''+responseText+'\')');;
		}
	}
	xmlHttp.open("POST",strUrl,true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.send(strPostData);
}

function fetchData(strUrl,strElementID,strLoadingText,strChkSum,strPostData)
{
	strAjaxElementID=strElementID;
	strAjaxPostData=strPostData;
	strAjaxUrl=strUrl;
 	if (typeof strChkSum=='undefined')
 	{
		strChkSum='';
	}
	strAjaxChkSum=strChkSum;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	if (strAjaxUrl.indexOf('?')>0)
	{
		strAjaxUrl=strAjaxUrl+'&';
	}
	else
	{
		strAjaxUrl=strAjaxUrl+'?';
	}
	strAjaxUrl=strAjaxUrl+'rid='+Math.random();	
	xmlHttp.onreadystatechange=stateChanged;
	if (strAjaxPostData=='')
	{
		xmlHttp.open("GET",strAjaxUrl,true);
		xmlHttp.send(null);
	}
	else
	{
		xmlHttp.open("POST",strAjaxUrl,true);
    xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xmlHttp.setRequestHeader('Referer', 'secretCode')
		xmlHttp.send(strAjaxPostData);
	}
	intAjaxRetries=5;
	//show loading...
	if (intAjaxInProgress==0)
	{
		strAjaxLoadingText=strLoadingText;
		if (strAjaxLoadingText.indexOf('...')>0)
		{
			strAjaxLoadingText=strAjaxLoadingText.slice(0,strAjaxLoadingText.indexOf('...'));
			strAjaxLoadingTextDots='';
			intAjaxIntervalID=setInterval("refreshLoadingText()",500)
		}
		else
		{
			clearInterval(intAjaxIntervalID);
		}
	}
	document.getElementById(strAjaxElementID).innerHTML=strAjaxLoadingText;
	intAjaxInProgress=1;
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}


function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{
		strTemp=xmlHttp.responseText;
		if (strTemp.slice(0,strAjaxChkSum.length)==strAjaxChkSum)
		{
			//fetch URL Done
			document.getElementById(strAjaxElementID).innerHTML=xmlHttp.responseText;
			intAjaxInProgress=0;
			clearInterval(intAjaxIntervalID);
			initialBody();
		}
		else
		{
			//Error fetch URL
			intAjaxRetries=intAjaxRetries-1;
			if (intAjaxRetries>0)
			{
				//Retry
				xmlHttp=GetXmlHttpObject();
				if (strAjaxUrl.indexOf('?')>0)
				{
					strAjaxUrl=strAjaxUrl+'&';
				}
				else
				{
					strAjaxUrl=strAjaxUrl+'?';
				}
				strAjaxUrl=strAjaxUrl+'rid='+Math.random();	
				xmlHttp.onreadystatechange=stateChanged;
				if (strAjaxPostData=='')
				{
					xmlHttp.open("GET",strAjaxUrl,true);
					xmlHttp.send(null);
				}
				else
				{
					xmlHttp.open("POST",strAjaxUrl,true);
					xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					xmlHttp.setRequestHeader('Referer', 'secretCode')
					xmlHttp.send(strAjaxPostData);
				}
			}
			else
			{
				//Raise error
				document.getElementById(strAjaxElementID).innerHTML='Connection Error!';
				intAjaxInProgress=0;
				clearInterval(intAjaxIntervalID);
			}
		}
	}
}

function refreshLoadingText()
{
	strAjaxLoadingTextDots=strAjaxLoadingTextDots+'.';
	if (strAjaxLoadingTextDots.length>3) strAjaxLoadingTextDots='';
	document.getElementById(strAjaxElementID).innerHTML=strAjaxLoadingText+strAjaxLoadingTextDots;
}

function charCount(strString,strChar)
{
	intCount=0;
	for (intC=0;intC<strString.length;intC++)
	{
		if (strString.slice(intC,intC+1)==strChar)
		{
			intCount++;
		}
	}
	return intCount;
}

function openpopup(popurl,w,h,s)
{
  if (s==1)  //s: 1=w scrollbars   0=w/o
	{
		var winpops=window.open(popurl,"","scrollbars=2,width=" + w + ",height=" + h + ",left=" + (screen.width-w)/2 + ",top=" + (screen.height-h)/2)
	}
	else
	{
		var winpops=window.open(popurl,"","width=" + w + ",height=" + h + ",left=" + (screen.width-w)/2 + ",top=" + (screen.height-h)/2)
	}
}

//Colorpicker
var newwindow='';function pickerPopup202(ifn,sam){var bl=screen.width/2-102;var bt=screen.height/2-104;page="fcp202.html"+"?ifn="+escape(ifn)+"&sam="+escape(sam);if(!newwindow.closed&&newwindow.location){newwindow.location.href=page;}else{newwindow=window.open(page,"CTRLWINDOW","help=no,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,dependent=yes,width=250,height=250,left="+bl+",top="+bt+",");if(!newwindow.opener)newwindow.opener=self;};if(window.focus){newwindow.focus()}}

function getBrowser()
{
	if (navigator.userAgent.indexOf('MSIE')>0)
	{
		return 'ie';
	}
	else
	{
		return 'ff';
	}
}
function getBrowserVersion()
{
	
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{ //test for MSIE x.x;
		var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		var strV='';
		if (ieversion>=8)
		strV=8;
		else if (ieversion>=7)
		strV=7;
		else if (ieversion>=6)
		strV=6;
		else if (ieversion>=5)
		strV=5;
	}
	return strV;
}
/*
function findPosXReal(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
			alert(obj.tagName+' x='+obj.offsetLeft+' rx='+(obj.offsetLeft-obj.scrollLeft+obj.clientLeft));
			curleft += obj.offsetLeft-obj.scrollLeft+obj.clientLeft;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		{
		curleft += obj.x;
		}
	return curleft;
}
*/

function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		{
		curleft += obj.x;
		}
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function queryString(parameter)
{ 
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;
  var params = loc.split("&");
  for (i=0; i<params.length;i++)
	{
		param_name = params[i].substring(0,params[i].indexOf('='));
		if (param_name == parameter)
		{
			param_value = params[i].substring(params[i].indexOf('=')+1)
		}
  }
  if (param_value)
	{
		return param_value;
  }
  else
	{
		return false; //Here determine return if no parameter is found
  }
}

function switchObjects(strObjSource,strObjTarget)
{
	document.getElementById(strObjSource).style.display='none';
	document.getElementById(strObjTarget).style.display='';
}


////////////////////////////////Date and time boxes
function chkSlashFocusNext(e,objObjectNull)
{
  var intC,strC,strO,key;
	if (window.event)
	{
		e = window.event;
		obj = e.srcElement;
		key = e.keyCode;
	}
	else
	{
		obj = e.target;
		key = e.charCode;
	}

	intC=obj.name.indexOf('_')+1;
	strO=obj.name.slice(0,intC+1);
	strC=obj.name.slice(intC+1,intC+2);
	if (key==47 && strC!='y')
  {
    if (strC=='d'){strC='m';}else{strC='y';}
    document.getElementById(strO+strC).focus();
    document.getElementById(strO+strC).select();
    return false;
  }
	else if((key>=48 && key<=57) || (key>=127 && key<=255) || (key<32))
	{
		return true;
	}
	else
	{
	  return false;
	}
}

function getMainParent(objObject)
{
	objParent=objObject;
	do
	{
		objParent=objParent.parentNode;
	}while (objParent.length==undefined);
	return objParent;
}

function textFilter(strFilters,e)
{
	strFilters+=',functionKeys';
	if (window.event)
	{
		e = window.event;
		obj = e.srcElement;
		key = e.keyCode;
	}
	else
	{
		obj = e.target;
		key = e.charCode;
	}
	strFiltersS=strFilters.split(',');
	intC=0;
	strKey=String.fromCharCode(key);
	resKey=null;
	resReturn=true;
	do
	{
		switch(strFiltersS[intC])
		{
			case 'signs':
				strValidChars='`~!@#$%^&*()_+-=\\|{}[]<>,.;\'?/';
				if (strValidChars.indexOf(strKey)!=-1)
				{
					resKey=key;
				}
				break;
			case 'email':
				strValidChars='@.-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
				if (strValidChars.indexOf(strKey)!=-1)
				{
					resKey=key;
				}
				break;
			case 'latin':
				strValidChars=' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
				if (strValidChars.indexOf(strKey)!=-1)
				{
					resKey=key;
				}
				break;
			case 'numbers':
				strValidChars='0123456789';
				if (strValidChars.indexOf(strKey)!=-1)
				{
					resKey=key;
				}
				break;
			case 'farsiNumbers':
				strValidChars='0123456789';
				if (strValidChars.indexOf(strKey)!=-1)
				{
					resKey=key+1728;
				}
				break;
			case 'functionKeys':											//include unicode
				if ((key < 32) || (key > 128))
				{
					return true;
				}
				break;
			case 'farsi':
				if ((key >= 32) && (key < 128))
				{
					strValidChars=' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`\;\'[]';
					if (strValidChars.indexOf(strKey)!=-1)
					{
						resKey=' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُىلآ÷ـ،/’د×؛َءٍف‘{ًْإ~جژچ^_پشذزيثبلاهتنمئدخحضقسفعرصطغظ<|>ّ'.charCodeAt(key-32);
					}
				}
				break;
		}
		intC++;
	}while(resKey==null && intC<strFiltersS.length);
	
	if (resKey==null)
	{
		resReturn=false;
	}
	else
	{
		try
		{
			// Windows
			e.keyCode = resKey;
		}
		catch(err)
		{
			try
			{
				// Try inserting at cursor position, Gecko after banning fake key emission
				pnhMozStringInsert(obj, String.fromCharCode(resKey));
				e.preventDefault();
			}
			catch(err)
			{
				// Everything else, simply add to the end of buffer
				obj.value += String.fromCharCode(resKey);
				e.preventDefault();
			}
		}
	}
	return resReturn;
}

function getStrParam(strText,strParam,strSeparator,strEqualSign)
{
	if (strSeparator==undefined) strSeparator=';';
	if (strEqualSign==undefined) strEqualSign=':';
	strParamsS=strText.split(strSeparator);
	for (intSC=0;intSC<strParamsS.length;intSC++)
	{
		strParamNameVal=strParamsS[intSC].split(strEqualSign);
		if (strParamNameVal.length==0)
		{
			strParamName=strParamNameVal[0];
			strParamVal=strParamNameVal[0];
		}
		else
		{
			strParamName=strParamNameVal[0];
			strParamVal=strParamNameVal[1];
		}
		if (strParamName.toLowerCase()==strParam.toLowerCase())
		{
			return strParamVal;
		}
	}				 
	return '';
}

function doTrim(strText)
{
	strText=strText.replace('&nbsp;',' ');
	strText=strText.replace(String.fromCharCode(160),' ');
	var strTemp=strText.replace(/^\s*/, "").replace(/\s*$/, "");
	return strTemp;
}

/*
function doTrim(str)			//fastest trim!
{
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}
*/

function fireEvent(element,event){
	if (document.createEventObject)
	{
		var evt = document.createEventObject();
		return element.fireEvent('on'+event,evt)
	}
	else
	{
		var evt = document.createEvent("HTMLEvents");
		evt.initEvent(event, true, true ); // event type,bubbling,cancelable
		return !element.dispatchEvent(evt);
	}
}

function separateNums(strNum)
{
	var strRes='';
	var intT=0;
	strNum=strNum+'';
  for (var intC=strNum.length-1;intC>=0;intC--)
	{
		if (strNum.charCodeAt(intC)>=48 && strNum.charCodeAt(intC)<=57)
		{
			strRes=strNum.slice(intC,intC+1)+strRes;
			intT++;
			if ((intT % 3)==0 && intC!=0) strRes=','+strRes;
		}
	}
	return strRes;
}

function confirmDel()
{
  if (document.getElementById('gridSelect').value!=0 && document.getElementById('gridSelect').value!='')
	{
		return confirm('Are you sure?')==true;
	}
	else
	{
		return false;
	}
}

function setIFrameHeight(iframeWindow)
{
  if (iframeWindow.document.height) {
    var iframeElement = parent.document.getElementById(iframeWindow.name);
    iframeElement.style.height = iframeWindow.document.height + 'px';
    //iframeElement.style.width = iframeWindow.document.width + 'px';
  }
  else if (document.all) {
    var iframeElement = parent.document.all[iframeWindow.name];
    if (iframeWindow.document.compatMode && iframeWindow.document.compatMode != 'BackCompat') 
    {
      iframeElement.style.height =  iframeWindow.document.documentElement.scrollHeight + 5 + 'px';
      //iframeElement.style.width = iframeWindow.document.documentElement.scrollWidth + 5 + 'px';
    }
    else {
      iframeElement.style.height = iframeWindow.document.body.scrollHeight + 5 + 'px';
      //iframeElement.style.width = iframeWindow.document.body.scrollWidth + 5 + 'px';
    }
  }
}

function truncateString(strText,intCharNo)
{
	//like strText.slice(0,intCharNo)+'...' but only add '...' when characters are more than intCharNo and UNICODE compatible
	var strRes=strText;
	if (strText.length>intCharNo)
	{
		strRes=strText.slice(0,intCharNo)+'...';
	}	
	return strRes;
}

function val(strText)
{
	//convert string to numeric value	
if(strText=='00-1')
{
	return -1;
}
	var strRes='';
	var strSign=1;
	if (strText.length>0 && strText!=null && strText!=undefined && strText!='')
	{
		var intC=0;
		var strDot='.';		//one dot only can be used
		if (strText.slice(0,1)=='-')
		{
			strSign=-1;
			strText=strText.slice(1,strText.length);
		}
		if (strText.slice(0,1)=='+') strText=strText.slice(1,strText.length);
		do
		{
			var strCh=strText.slice(intC,intC+1);
			if ((strCh==strDot) || (strCh.charCodeAt(0)>=48 && strCh.charCodeAt(0)<=57)) strRes=strRes+''+strCh; else intC=strText.length;
			if (strCh==strDot) strDot='';
			intC++;
		}while (intC<strText.length);
	}
	else
	{
		strRes=0;
	}
	strRes=strSign*strRes;
	return strRes;
}

function getElementByIdArray(strTagName,strId,intElementIndex)
{
	objElements=document.getElementsByTagName(strTagName);
	if (objElements!=null)
	{
		if (objElements.length>0)
		{
			var intElementCounter=-1;
			var intC=-1;
			do
			{
				intC++;
				if (objElements[intC].id==strId) intElementCounter++;
			}while (!((intC>=objElements.length || (objElements[intC].id==strId && intElementCounter==intElementIndex))));
			if (objElements[intC].id==strId)
			{
				return objElements[intC];
			}
			else
			{
				return false;
			}
		}
		else
		{
			if (objElements.id==strId)
			{
				return objElements;
			}
			else
			{
				return false;
			}			
		}
	}
	else
	{
		return false;
	}
}

function filterSeparateNums(objCaller,e)
{
	if (e==null)
	{
		////////////onBlur
		objCaller.value;
		objCaller.value=objCaller.value.replace(',','');
		objCaller.value=separateNums(objCaller.value);
	}
	else
	{
		////////////onKeyPress
		
	}
}

function getAllChildNodes(objMain)
{		
	var objChildNodesAr=new Array();
	objChildNodesAr=getAllChildNodesAppend(objChildNodesAr,objMain);
	return objChildNodesAr;
}

function getAllChildNodesAppend(objArray,objMain)
{
	var intCO=0;
	var objChildNodes;
	objChildNodes=objMain.childNodes;
	for (intCO=0;intCO<=objChildNodes.length-1;intCO++)
	{
		objArray[objArray.length]=objChildNodes[intCO];
		objArray=getAllChildNodesAppend(objArray,objChildNodes[intCO]);		
	}
	
	return objArray;
}

function getListboxText(objListbox,intValue)
{
	//get a listbox and value as input, and return Options[].TEXT of the selectedIndex item
	var intLc=-1;
	do
	{
		intLc++;
	}while (intLc<objListbox.options.length && objListbox.options[intLc].value!=intValue)
	if (objListbox.options[intLc].value==intValue)
	{
		return objListbox.options[intLc].text;
	}
	else
	{
		return false;
	}
}

function isNumeric(input)
{
	var RE = /^-{0,1}\d*\.{0,1}\d+$/;
	return (RE.test(input));
}


/*
function calcChkSum(strText)
{
	var intChkSum=0;
	var intC;
  for (intC=0;intC<strText.length;intC++)
	{		
		intChkSum+=(intC & 127) * strText.charCodeAt(intC);
		intChkSum=(intChkSum & 65535);
	}
	return intChkSum;
}
*/
