﻿function checkOnlyNumber(field)
{
	var check = true;
	var value = field.value; //get characters
	for(var i=0;i < field.value.length; ++i)
	{
		var new_key = value.charAt(i); //cycle through characters
		if(((new_key < "0") || (new_key > "9")) && !(new_key == ""))
		{
			check = false;
			break;
		}
	}
	if(!check)
		field.style.backgroundColor = "red";
	else
		field.style.backgroundColor = "white";
	return check
} 


var dialogIsOpen = 0 ;
var DialogPrev_overflowX, DialogPrev_overflowY;
var delimiter = "$$$";

function shLayers()
{
// Disable
	document.write("<div id=\"disableLayer\" style=\"padding:0px; position: absolute; width: 10px; height: 230px; z-index: 1; background-color: #FFFFFF; visibility:hidden; left:0; top:0; filter:Alpha(Opacity=70, Style=0)\"></div>");
	
// Shadow
	document.write("<div id=\"shadowLayer\" style=\"padding:0px; position: absolute; width: 10px; height: 230px; z-index: 2; background-color: #000000; visibility:hidden; left:0; top:0; filter:Alpha(Opacity=30, Style=0)\"></div>");
	
// Loading Box
document.write("	<div id=\"loadingBox\" style=\"border:3px solid #969569; position: absolute; width: 192px; height: 64px; z-index: 2; padding-left: 0px; padding-right: 0px; padding-top: 1px; padding-bottom: 1px; background-color: #E7E7DC; visibility:hidden; left:1px; top:1px\" >");
document.write("<p align=\"center\" dir=\"rtl\"><img border=\"0\" src=\"images/icon_loading.gif\"><font face=\"tahoma\" size=\"2\"><br>					در حال ارتباط با بانک اطلاعات</font></div>");

// Error Box
document.write ("<div id=\"errorBox\" style=\"border:3px solid #800000; position: absolute; width: 250px; height: 64px; z-index: 2; padding-left: 0px; padding-right: 0px; padding-top: 1px; padding-bottom: 1px; visibility:hidden; left:1px; top:1px\" >");
document.write ("<table border=\"0\" cellpadding=\"2\" width=\"100%\" id=\"table1\" style=\"font-size: 10pt\" bgcolor=\"#FFFFFF\">");
document.write ("<tr><td>");
document.write ("	<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" id=\"table2\">");
document.write ("		<tr>");
document.write ("			<td>");
document.write ("			<p dir=\"rtl\"><span lang=\"fa\"><font face=\"Arial\" size=\"3\">&nbsp;<b>Error</b></font></span></td>");
document.write ("			<td width=\"35\"><img border=\"0\" src=\"images/Icon_error.gif\" width=\"32\" height=\"32\"></td>");
document.write ("		</tr>");
document.write ("	</table>");
document.write ("	</td>");
document.write ("</tr>");
document.write ("<tr>");
document.write ("	<td dir=\"rtl\"><font face=\"Tahoma\" size=\"2\"><span id=\"errorBoxErrorText\"></span></font></td>");
document.write ("</tr>");
document.write ("<tr>");
document.write ("	<td><input type=\"button\" value=\"ادامه\" onclick=\"closeDialog('errorBox')\" name=\"B3\" style=\"font-family: Tahoma; font-size: 9pt; border: 1px solid #000000; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px\"></td>");
document.write ("</tr>");
document.write ("</table>");
document.write ("</div>");

// OK  Box
	document.write("<div id=\"okBox\" style=\"border:3px solid #800000; position: absolute; width: 192px; height: 64px; z-index: 2; padding-left: 0px; padding-right: 0px; padding-top: 1px; padding-bottom: 1px; background-color: #FFF2F2; visibility:hidden; left:1px; top:1px\" >");
document.write("<p align=\"center\" dir=\"rtl\"><img border=\"0\" src=\"images/icon_OK.gif\" width=\"32\" height=\"32\"><font face=\"tahoma\" size=\"2\"><br>	");
document.write("عمليات با موفقيت انجام شد</font><input type=\"button\" value=\"ادامه\" onclick=\"closeDialog('okBox')\" name=\"B3\" style=\"font-family: Tahoma; font-size: 9pt; border: 1px solid #000000; padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px\"></div>");
}

// =------------------ AJAX
function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
		objXMLHttp=new XMLHttpRequest();
	else if (window.ActiveXObject)
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	return objXMLHttp;
}
function ajDecode(txt)
{
	if (txt == "") 
		return "";
	txt = replaceSubstring(txt, "^01", "آ");
	txt = replaceSubstring(txt, "^02", "ا");
	txt = replaceSubstring(txt, "^03", "ب");
	txt = replaceSubstring(txt, "^04","پ");
	txt = replaceSubstring(txt, "^05","ت");
	txt = replaceSubstring(txt, "^06","ث");
	txt = replaceSubstring(txt, "^07","ج");
	txt = replaceSubstring(txt, "^08","چ");
	txt = replaceSubstring(txt, "^09","ح");
	txt = replaceSubstring(txt, "^10","خ");
	txt = replaceSubstring(txt, "^11","د");
	txt = replaceSubstring(txt, "^12","ذ");
	txt = replaceSubstring(txt, "^13","ر");
	txt = replaceSubstring(txt, "^14","ز");
	txt = replaceSubstring(txt, "^15","ژ");
	txt = replaceSubstring(txt, "^16","س");
	txt = replaceSubstring(txt, "^17","ش");
	txt = replaceSubstring(txt, "^18","ص");
	txt = replaceSubstring(txt, "^19","ض");
	txt = replaceSubstring(txt, "^20","ط");
	txt = replaceSubstring(txt, "^21","ظ");
	txt = replaceSubstring(txt, "^22","ع");
	txt = replaceSubstring(txt, "^23","غ");
	txt = replaceSubstring(txt, "^24","ف");
	txt = replaceSubstring(txt, "^25","ق");
	txt = replaceSubstring(txt, "^26","ک");
	txt = replaceSubstring(txt, "^27","گ");
	txt = replaceSubstring(txt, "^28","ل");
	txt = replaceSubstring(txt, "^29","م");
	txt = replaceSubstring(txt, "^30","ن");
	txt = replaceSubstring(txt, "^31","و");
	txt = replaceSubstring(txt, "^32","ه");
	txt = replaceSubstring(txt, "^33","ئ");
	txt = replaceSubstring(txt, "^34","ی");
	txt = replaceSubstring(txt, "^35","ی");
	txt = replaceSubstring(txt, "^36","ي");
	txt = replaceSubstring(txt, "^40","،");
	txt = replaceSubstring(txt, "^41","؟");
	txt = replaceSubstring(txt, "&s;","^");
	return(txt);
}

function ajEncode(txt)
{
	if (txt=="")
		return "";
	txt = replaceSubstring(txt, "^","&s;");
	txt = replaceSubstring(txt, "آ","^01");
	txt = replaceSubstring(txt, "ا","^02");
	txt = replaceSubstring(txt, "ب","^03");
	txt = replaceSubstring(txt, "پ","^04");
	txt = replaceSubstring(txt, "ت","^05");
	txt = replaceSubstring(txt, "ث","^06");
	txt = replaceSubstring(txt, "ج","^07");
	txt = replaceSubstring(txt, "چ","^08");
	txt = replaceSubstring(txt, "ح","^09");
	txt = replaceSubstring(txt, "خ","^10");
	txt = replaceSubstring(txt, "د","^11");
	txt = replaceSubstring(txt, "ذ","^12");
	txt = replaceSubstring(txt, "ر","^13");
	txt = replaceSubstring(txt, "ز","^14");
	txt = replaceSubstring(txt, "ژ","^15");
	txt = replaceSubstring(txt, "س","^16");
	txt = replaceSubstring(txt, "ش","^17");
	txt = replaceSubstring(txt, "ص","^18");
	txt = replaceSubstring(txt, "ض","^19");
	txt = replaceSubstring(txt, "ط","^20");
	txt = replaceSubstring(txt, "ظ","^21");
	txt = replaceSubstring(txt, "ع","^22");
	txt = replaceSubstring(txt, "غ","^23");
	txt = replaceSubstring(txt, "ف","^24");
	txt = replaceSubstring(txt, "ق","^25");
	txt = replaceSubstring(txt, "ک","^26");
	txt = replaceSubstring(txt, "گ","^27");
	txt = replaceSubstring(txt, "ل","^28");
	txt = replaceSubstring(txt, "م","^29");
	txt = replaceSubstring(txt, "ن","^30");
	txt = replaceSubstring(txt, "و","^31");
	txt = replaceSubstring(txt, "ه","^32");
	txt = replaceSubstring(txt, "ئ","^33");
	
	txt = replaceSubstring(txt, "&#1740;","^34");
	txt = replaceSubstring(txt, "ی","^35");
	txt = replaceSubstring(txt, "ي","^36");
	
	txt = replaceSubstring(txt, "،","^40");
	txt = replaceSubstring(txt, "^؟","41");
	return(txt);
}


// for generating and pass a Random ID to Internal ASPs
function TC()
{
	return Math.floor(Math.random()*100000000);
}


function callAsp(pageName, parameter , targetFunction)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		return;
	var url = pageName;
	url += "?" + parameter + "&tc=" + TC();
 	eval("xmlHttp.onreadystatechange = "+targetFunction);
 	xmlHttp.open("POST",url,true);
 	xmlHttp.send(null);
}

// ------ SHOW WAIT message for Cell
function activeWait4Cell(cellName)
{
	document.getElementById(cellName).innerText = "لطفاً صبر کنيد...";
	document.getElementById(cellName).bgColor = "FFFF00";
}
function deActiveWait4Cell(cellName)
{
	document.getElementById(cellName).innerText = "";
	document.getElementById(cellName).bgColor = "ADD8E4";
}


// -------------------------------------------------------------------
// ------------------- FARSI functions
function chr(c) 
{ 
	h=String.fromCharCode(c);
	return h;
}

function Asci2Chr(tmp)
{
	if (tmp.indexOf("-")==0)
		tmp = tmp.substring(1);
	var tmp1="";
	while (tmp.length>0)
	{
		if (tmp.indexOf("-")>0)
		{
			tmp1=tmp1+chr(tmp.substring(0,tmp.indexOf("-"))*1);
			tmp=tmp.substring(tmp.indexOf("-")+1);
		}
	else
   		{
   			tmp1=tmp1+chr(tmp*1);
   			tmp="" ;
   		}
  	}
  	
 	return(tmp1) ;
} 


function UniC(ff)
{
	dd="";
	for (u=0;u<ff.length;u++)
		dd=dd+"-"+ff.charCodeAt(u);
	return dd;  
}

// -------------------------------------------------------------------
// ------------------------------- Events
function getOnlyNo()
{
	var key;
	if (window.event)
		key = window.event.keyCode;
	else if (event)
		key = event.which;
	if ((key >= 48) && (key <= 57))
		return(true);
	else
		return(false);
}


// --------------------- Accessories
function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function
function setFieldFocus(id)
{
	document.getElementById(id).focus();
}
function setFieldEmpty(id)
{
	document.getElementById(id).value = "";
}
function getFieldValue(id)
{
	return (document.getElementById(id).value);
}
function setFieldValue(id,txt,addFlag)
{
	if (addFlag==0 || addFlag == null)
		document.getElementById(id).value = txt;
	else
		document.getElementById(id).value += txt;
}
function setFieldDisable(id, situation)
{
	if (situation==null)
		situation = true;
	document.getElementById(id).disabled = situation;
}

function setFieldVisible(id, situation)
{
	document.getElementById(id).style.visibility = "visible";
}
function setFieldHidden(id, situation)
{
	document.getElementById(id).style.visibility = "hidden";
}

function getComboValue(id)
{
	return (document.getElementById(id).options[document.getElementById(id).selectedIndex].text);
}

function setComboIndexByText(id,txt)
{
	for (var i=0;i<document.getElementById(id).options.length;i++)
	{
		if (document.getElementById(id).options[i].text==txt)
			document.getElementById(id).selectedIndex = i;
	}
}
function setComboIndexByValue(id,value)
{
	for (var i=0;i<document.getElementById(id).options.length;i++)
	{
		if (document.getElementById(id).options[i].value==value)
			document.getElementById(id).selectedIndex = i;
	}
}


// --------------------- Get number formats
function getNumberFormat(n)	// 23000 -> 23,000
{
	var arr=new Array('0'), i=0; 
	while (n>0) 
    {
    	arr[i]='' + n%1000; n=Math.floor(n/1000); i++;
    }
  	arr=arr.reverse();
  	for (var i in arr) 
  		if (i>0) //padding zeros
    		while (arr[i].length<3) arr[i]='0'+arr[i];
  	return arr.join();
}
function getDigitFormat(n)	// 23,000 -> 23000
{
	var txt;
	var pos;
	while (n.indexOf(",")!=-1)
	{
		pos = n.indexOf(",");
		n = n.substring(0,pos) + n.substring(pos+1);
	}
	return n;
}

// 	digit
// ==========================
function getDigit(id)
{
	var key ;
	if (window.event) 
		key = window.event.keyCode;
	else if (event)
		key = event.which;

	if (key==13)
		if (window.event) 
			window.event.keyCode = 9;
		else if (event)
			event.which = 9 ;
		
		if ( ((key >= 48) && (key <= 57)) || ((key >= 96) && (key <= 105)) || (key == 8) || (key==46) || (key==39) || (key==37) || (key==8) || (key==13) || (key==9)) // Delete, BackSpace
		return(true);
	else
		return(false);
}

// --------------------- POP and NEXTCODED
function popCodedTxt(txt, myDelimiter)
{
	if (myDelimiter == null)
		myDelimiter = delimiter;
	var tempTxt;
	tempTxt = txt.substring(0,txt.indexOf(myDelimiter));
//	txt = nextCodedTxt(txt,myDelimiter);
	return tempTxt;
}
function nextCodedTxt(txt, myDelimiter)
{
	if (myDelimiter == null)
		myDelimiter = delimiter;
	var tempTxt;
	txt = txt.substring(txt.indexOf(myDelimiter)+myDelimiter.length);
	return txt;
}



function giveNChar(txt,n)
{
	while (txt.length < n)
	{	txt = "0" + txt; }
	return txt;
}

// --------------------- GET DATE
function getJustYear(txt)
{
	return txt.substr(0,txt.indexOf('/',0));
}

function getJustMonth(txt)
{
	var y1=txt.substr(0,txt.indexOf('/',0));
	txt=txt.substr(txt.indexOf('/',0)+1);
	return giveNChar(txt.substr(0,txt.indexOf('/',0)),2);
}

function getJustDay(txt)
{
	var y1=txt.substr(0,txt.indexOf('/',0));
	txt=txt.substr(txt.indexOf('/',0)+1);
	var m1=txt.substr(0,txt.indexOf('/',0));
	return giveNChar(txt.substr(txt.indexOf('/',0)+1),2);
}


// --------------------- Disable / Enable Screen
function activeDisable(layerName)
{
	if (layerName == null)
		layerName = "disableLayer";
	DialogPrev_overflowX = document.body.style.overflowX ;
	DialogPrev_overflowY = document.body.style.overflowY ;
	document.body.style.overflowX="hidden" ;
	document.body.style.overflowY="hidden" ;
	document.getElementById(layerName).style.width = document.body.clientWidth*1+100;
	document.getElementById(layerName ).style.height = document.body.clientHeight*1+100;
	// Disable all fields by disable Layer
	setFieldVisible(layerName );
	
}

function deactiveDisable(layerName)
{
	// Enable all fields
	if (layerName == null)
		layerName = "disableLayer";
	document.getElementById(layerName).style.width = 1;
	document.getElementById(layerName ).style.height = 1;
	document.body.style.overflowY = DialogPrev_overflowY ;
	document.body.style.overflowX = DialogPrev_overflowX ;
	setFieldHidden(layerName);
}

//	-----------------------------------------------------
//	----------------- Dialog functions
//	-----------------------------------------------------
function showDialog(dialogName,disableLayerFlag)
{
	if (dialogIsOpen == 0)
	{
		// go to top
		if (disableLayerFlag != 0) 
		{
			if (navigator.appName.indexOf("Explorer")!=-1)
				document.getElementById("disableLayer").style.top = document.body.scrollTop;
			else 
				document.getElementById("disableLayer").style.top = window.pageYOffset;
			activeDisable();
		}

		var width = document.getElementById(dialogName).clientWidth;
		var height = document.getElementById(dialogName).clientHeight;
		var layerTop, layerLeft;
		
		if (navigator.appName.indexOf("Explorer")!=-1)
			layerTop = document.body.scrollTop + document.body.clientHeight/2 - height/2;
		else 
			layerTop = window.pageYOffset + document.body.clientHeight/2 - height/2;
		layerLeft = document.body.clientWidth/2 - width/2;

		document.getElementById(dialogName).style.top = layerTop;
		document.getElementById(dialogName).style.left = layerLeft;

		document.getElementById("shadowLayer").style.top = layerTop + 10;
		document.getElementById("shadowLayer").style.left = layerLeft + 10;
		document.getElementById("shadowLayer").style.width = width;
		document.getElementById("shadowLayer").style.height = height ;
		

		document.getElementById(dialogName).style.visibility = "visible";
		if (dialogName != "disableLayer")
			document.getElementById("shadowLayer").style.visibility = "visible";

		dialogIsOpen = 1;
	}	
}
function closeDialog(dialogName,disableLayerFlag)
{
//	if (dialogIsOpen == 1)
	{
		if (disableLayerFlag != 0) 
			deactiveDisable();
		document.getElementById(dialogName).style.visibility = "hidden";
		document.getElementById("shadowLayer").style.visibility = "hidden";
		dialogIsOpen = 0;
	}	
}


function menu_selectMenu(id,lang)
{
	var varLoop;
	for (varLoop = 1;varLoop <=8;varLoop++)
	{
		window.parent.document.getElementById("menu_BKMain"+varLoop).background = "images/_topmenu_bk.gif";
		if (varLoop!=8)
			window.parent.document.getElementById("menu_ramp"+id).src = "images/_topmenu_ramp"+lang+".gif";
		if (varLoop>1) window.parent.document.getElementById("menu_ramp"+(varLoop-1)).src = "images/_topmenu_ramp"+lang+".gif";
	}
	window.parent.document.getElementById("menu_ramp8").src = "images/_topmenu_lastRamp"+lang+".gif";
	// -----------
	window.parent.document.getElementById("menu_BKMain"+id).background = "images/_topmenu_bkSelected.gif";
	if (id!=8)
		window.parent.document.getElementById("menu_ramp"+id).src = "images/_topmenu_rampSelected"+lang+".gif";
	else
		window.parent.document.getElementById("menu_ramp"+id).src = "images/_topmenu_lastRampSelected"+lang+".gif";
	if (id>1) window.parent.document.getElementById("menu_ramp"+(id-1)).src = "images/_topmenu_rampPreSelected"+lang+".gif";
}