/*******************************************************************/      
// Original idea is from:      
// http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/farsitools/#dirlist and      
// http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/farsitools/php/date/scripts/
// CODES HAVE BEEN EXPANDED BY PersianBlog.com
      
var langFarsi = true;
var farsikey = [	// Farsi keyboard map based on Iran Popular Keyboard Layout      
	0x0020, 0x0021, 0x061B, 0x066B, 0x00A4, 0x066A, 0x060C, 0x06AF,      
	0x0029, 0x0028, 0x002A, 0x002B, 0x0648, 0x002D, 0x002E, 0x002F,      
	0x06F0, 0x06F1, 0x06F2, 0x06F3, 0x06F4, 0x06F5, 0x06F6, 0x06F7,      
	0x06F8, 0x06F9, 0x003A, 0x0643, 0x003E, 0x003D, 0x003C, 0x061F,      
	0x066C, 0x0624, 0x200C, 0x0698, 0x064A, 0x064D, 0x0625, 0x0623,      
	0x0622, 0x0651, 0x0629, 0x00BB, 0x00AB, 0x0621, 0x004E, 0x005D,      
	0x005B, 0x0652, 0x064B, 0x0626, 0x064F, 0x064E, 0x0056, 0x064C,            
	0x0058, 0x0650, 0x0643, 0x062C, 0x0698, 0x0686, 0x00D7, 0x0640,            
	0x067E, 0x0634, 0x0630, 0x0632, 0x064A, 0x062B, 0x0628, 0x0644,            
	0x0627, 0x0647, 0x062A, 0x0646, 0x0645, 0x0626, 0x062F, 0x062E,            
	0x062D, 0x0636, 0x0642, 0x0633, 0x0641, 0x0639, 0x0631, 0x0635,            
	0x0637, 0x063A, 0x0638, 0x007D, 0x007C, 0x007B, 0x007E            
];            
            
function changelang() {
 if (form1.text.style.direction == "rtl")  
  {  form1.text.style.direction = "ltr";  document.imglang.src="images/fa.gif";}
 else {  form1.text.style.direction = "rtl"; document.imglang.src="images/en.gif"; }
 form1.text.focus();
}

function cFKeyDown() {
         if (form1.text.style.direction == "rtl") {
	if (window.event.shiftKey && window.event.altKey) {
		langFarsi=!langFarsi;
		return false;
	}
          }
	return true;
}

function cFKeyPress() {
if (form1.text.style.direction == "rtl") {
   var key = window.event.keyCode;
   if (key < 0x0020 || key >= 0x00FF)
      return;
   if (langFarsi) {
      var el = event.srcElement;
      var objRegExp = new RegExp("[A-Za-z\x27\x2C\x3B\x5B\x5C\x5D\x7C]");
      var validate_key = objRegExp.test(String.fromCharCode(key));
      if ((validate_key || (key==92)) && (key != 0x200C) && (el.value.lastIndexOf(String.fromCharCode(1740)) == el.value.length - 1) && el.value.length > 0) {
         el.value = el.value.slice(0, -1);
         el.value += String.fromCharCode(1610);
      }
      if (key == 0x0020 && window.event.shiftKey)
         window.event.keyCode = 0x200C;
      else
         window.event.keyCode = farsikey[key - 0x0020];
   }
}
   return true;

}

function FKeyDown() {
	if (window.event.shiftKey && window.event.altKey) {
		langFarsi=!langFarsi;
		return false;
	}
	return true;
}


function FKeyPress() {
   var key = window.event.keyCode;
   if (key < 0x0020 || key >= 0x00FF)
      return;
   if (langFarsi) {
      var el = event.srcElement;
      var objRegExp = new RegExp("[A-Za-z\x27\x2C\x3B\x5B\x5C\x5D\x7C]");
      var validate_key = objRegExp.test(String.fromCharCode(key));
      if ((validate_key || (key==92)) && (key != 0x200C) && (el.value.lastIndexOf(String.fromCharCode(1740)) == el.value.length - 1) && el.value.length > 0) {
         el.value = el.value.slice(0, -1);
         el.value += String.fromCharCode(1610);
      }
      if (key == 0x0020 && window.event.shiftKey)
         window.event.keyCode = 0x200C;
      else
         window.event.keyCode = farsikey[key - 0x0020];
   }
   return true;

}

function FKeyPressWithoutNumbers() {
   var key = window.event.keyCode;
   if (((key <= 0x0039) || (key >= 0x00FF) || (key == 0x003D))&& (key != 32)&& (key != 39) && (key != 44))
   {
   //alert(window.event.keyCode);
   window.event.keyCode ='';
     return false;
     }
   if (langFarsi) {
      var el = event.srcElement;
      var objRegExp = new RegExp("[A-Za-z\x27\x2C\x3B\x5B\x5C\x5D\x7C]");
      var validate_key = objRegExp.test(String.fromCharCode(key));
      if ((validate_key || (key==92)) && (key != 0x200C) && (el.value.lastIndexOf(String.fromCharCode(1740)) == el.value.length - 1) && el.value.length > 0) {
         el.value = el.value.slice(0, -1);
         el.value += String.fromCharCode(1610);
      }
      if (key == 0x0020 && window.event.shiftKey)
         window.event.keyCode = 0x200C;
      else
         window.event.keyCode = farsikey[key - 0x0020];
   }
   return true;
   
   //alert(window.event.keyCode);

}


function onlyNumbersKeyPress(elem) 
{
	var key = window.event.keyCode;
	   // alert(key);
	if (key < 0x0030 || key > 0x0039)
	    window.event.keyCode ='';
	else 
	{
		window.event.keyCode =key;
		//elem.style.backgroundColor='ff0000';
		elem.className = 'txtGridEdited';
		elem.title='';
	}
}		



function over(){el.onmouseover=event.srcElement.style.backgroundColor = "#ff0000";}
function out(){el.onmouseout=event.srcElement.style.backgroundColor = "#f8f8f8";}

function focusIn()
{
	event.srcElement.style.backgroundColor = "#f8f8f8";
	//alert(event.srcElement.id+"  now focused");
}

function focusOut()
{
	event.srcElement.style.backgroundColor = "#FFFFFF";
	
}

function initFormControls()
{
	var tabCount = 0;
	var firstCtrl = null;
	for(f=0;f<document.forms.length;f++) {
			// iterate all the elements in the form
			for(e=0;e<document.forms[f].elements.length;e++){
				tabCount++;
				
				el = document.forms[f].elements[e];
				el.tabIndex = tabCount;
				
				//alert( el.id );
				//alert(el.tagName);
				switch (el.tagName.toLowerCase() )
				{
				case 'input':
					switch (el.type.toLowerCase())
					{
					case 'submit':
					case 'button':
						if( f==0 && e==0 ){ firstCtrl = el.id; }
						el.className = "ipt_txt";
						el.onmouseover=over;
						el.onmouseout=out;
						break;
					case 'reset':
						el.className = "ipt_btn";
						el.style.cursor = "hand";
						break;
					case 'password':
					case 'text':
						if( f==0 && e==0 ){ firstCtrl = el.id; }
						el.className = "ipt_txt";
						el.onfocus=focusIn;
						el.onblur=focusOut;
						break;
					default:
						break;
					}
					break;
				case 'textarea':
						el.className = "ipt_txt";
						el.onfocus=focusIn;
						el.onblur=focusOut;
						break;
				case 'select':
					if( f==0 && e==0 ){ firstCtrl = el.id;}
					el.className = "ipt_sel";
					el.onfocus=focusIn;
					el.onblur=focusOut;	
					break;
				default:
					break;
				}
			}
	}

	if ( firstCtrl != null )
	{
		//alert( firstCtrl );
		document.getElementById( firstCtrl ).focus();
	}
}



