//Menu Functions
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->

var el;

function showMenu() {
   ContextElement=event.srcElement;
   menu1.style.leftPos+=10;
   menu1.style.posLeft=event.clientX;
   menu1.style.posTop=event.clientY;
   menu1.style.display="";
   menu1.setCapture();
   document.getElementById('iframe1').style.leftPos+=10;
   document.getElementById('iframe1').style.posLeft=event.clientX;
   document.getElementById('iframe1').style.posTop=event.clientY;
   document.getElementById('iframe1').style.display="";
}
function toggleMenu(c) {   
   el=event.srcElement;
   if (el.className=="menuItem" + c) {
      el.className="highlightItem" + c;
   } else if (el.className=="highlightItem" + c) {
      el.className="menuItem" + c;
   }
}
function clickMenu() {
   menu1.releaseCapture();
   menu1.style.display="none";
   document.getElementById('iframe1').style.display="none"; 
   el=event.srcElement;
   if (el.doFunction != null) {
     eval(el.doFunction);
   }
}
//End Menu Functions

//Move or Copy Documents to New Group
function SndToGrp(s) {
  docs = slist.cid.value
  if (docs != '')
		  window.open('grouplist.aspx?S='+s+'&DocCodes='+docs,'SndToGrp','left=200,top=200,width=600,height=200,scrollbars=yes')
	else
	  alert('لطفا سند مورد نظر را انتخاب کنيد')	  
}
//End function

//Show ShortKey Menu
function ShortKeyMenu(pFld){
  if (window.event.ctrlKey && (window.event.keyCode==35)) { //Ctrl + End 
    ShortKeyValue = window.showModalDialog('../management/shortkeys.aspx','','dialogWidth:400px;dialogHeight:400px;scroll:yes')
    if ((ShortKeyValue != '')&&(ShortKeyValue != undefined)){
			ObjField = eval('sform.'+pFld)
			ObjField.value = ObjField.value + ShortKeyValue
			ObjField.focus()
		}	
    window.event.returnValue = false
  }
  else{
		window.event.returnValue=true
	}	
}
//End function

//function Make post Request for Ajax
var http_request = false;
function makeRequest(url, parameters, postbackfunc) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      // set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
      }
  } else if (window.ActiveXObject) { // IE
      try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
      }
  }
  if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
  }
  
  http_request.onreadystatechange = eval(postbackfunc);  //To Show Result
  
  http_request.open('POST', url, true);
  //http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}
//End function 

//Trim Function 
function Trim(pStr){
  return pStr.replace(/^\s+|\s+$/g, ''); 
}

//Check date time not earlier than current date time
function CheckValidDate(pDate, pTime, pCurDateTime){ 
  if (pDate.indexOf('/')>-1){
    arrRDateVals = pDate.split("/");
    var rYear = arrRDateVals[0];
    var rMonth = arrRDateVals[1] * 1;
    if (rMonth < 10) rMonth = '0' + rMonth ;
    var rDay = arrRDateVals[2] * 1;
    if (rDay < 10) rDay = '0' + rDay ;
  }
  else
    return false;
  
  if ((pTime != '') && (pTime.indexOf(':')>-1)){
    arrRTimeVals = pTime.split(":");
    var rHour = arrRTimeVals[0] * 1; 
    if (rHour < 10) rHour = '0' + rHour ;
    var rMin = arrRTimeVals[1] * 1; 
    if (rMin < 10) rMin = '0' + rMin ;
  }
  else{
    rHour = '00'
    rMin = '00'
  }
  				  
  var myDate = rYear + rMonth + rDay + rHour + rMin;
  
  if (pCurDateTime == '')
    return true;
  else{
    var curDate = new Date(pCurDateTime)
    
    var cYear  =  curDate.getYear() ;
    var cMonth =  curDate.getMonth() + 1; 
    var cDay   =  curDate.getDate() ;
    var cHour  =  curDate.getHours() ;
    var cMin   =  curDate.getMinutes() ;
    
    if (cMonth < 10) cMonth = '0' + cMonth;
    if (cDay < 10)   cDay   = '0' + cDay;
    if (cHour < 10)  cHour  = '0' + cHour;
    if (cMin < 10)   cMin   = '0' + cMin;
    
    if ((pTime != '') && (pTime.indexOf(':')>-1)){
      CurrentDate = String(cYear) + String(cMonth) + String(cDay) + String(cHour) + String(cMin) ;
    }
    else{
      CurrentDate = String(cYear) + String(cMonth) + String(cDay) + '0000'
    }
  }  
  
  if (myDate>=CurrentDate)
    return true;
  else
    return false;
}

//Copy Searched Documents to Cartable
function SndToCartable(){
	docs = slist.cid.value
  if (docs == '')
	  alert('هيچ سندي انتخاب نشده است');
  else
    document.getElementById('CartableMoveTree').style.display = 'block';
}

function CloseCartableTree(){
  document.getElementById('CartableMoveTree').style.display = 'none';
}

function CloseCartableDateTime(){
	document.getElementById('RemindDesc').value = '';
	document.getElementById('IsSms').checked = false;
	document.getElementById('TelNo').value = '';
	document.getElementById('MsgSmS').value = '';
	document.getElementById('SelCarCode').value = '';
  document.getElementById('DateTimeReminder').style.display = 'none';
}


// Helper function that gets an input element and applies the KafYeh settings on text of it
// The function assumes there is a hidden field by ID=KafYehSettings where conatains 1 or 0
function replaceChars(obj)
{
    try
    {
        var Kaf_Farsi = 1705;
        var Kaf_Arabic = 1603;
        var Yeh_Farsi = 1740;
        var Yeh_Arabic = 1610;
        var out = obj.value;
        var farsiOrArabicLetter = "1"

        if (farsiOrArabicLetter == "0") // 0 means system must use Farsi Yeh and Kaf
        {
            if (out.indexOf(String.fromCharCode(Kaf_Arabic)) != -1 || out.indexOf(String.fromCharCode(Yeh_Arabic)) != -1)
            {
                out = replaceAllOccurances(out, String.fromCharCode(Kaf_Arabic), String.fromCharCode(Kaf_Farsi));
                out = replaceAllOccurances(out, String.fromCharCode(Yeh_Arabic), String.fromCharCode(Yeh_Farsi));
                obj.value = out;
            }
        }
        else if (farsiOrArabicLetter == "1") // 1 means system must use Arabic Yeh and Kaf
        {
            if (out.indexOf(String.fromCharCode(Kaf_Farsi)) != -1 || out.indexOf(String.fromCharCode(Yeh_Farsi)) != -1)
            {
                out = replaceAllOccurances(out, String.fromCharCode(Kaf_Farsi), String.fromCharCode(Kaf_Arabic));
                out = replaceAllOccurances(out, String.fromCharCode(Yeh_Farsi), String.fromCharCode(Yeh_Arabic));
                obj.value = out;
            }
        }
    }
    catch (e) {

    }
}

// uility function for replacing all occurances of a string within another one even repeatetive occurances 
function replaceAllOccurances(str, find, replace)
{
    var out = str;
    while (out.indexOf(find) >= 0) {
        out = out.replace(find, replace);
    }
    return out;
}

$(document).ready(function(){
  $("input[@type=text],textarea,select").bind("change", function(e) {
  replaceChars(this);
  });
  
  $("input[@type=text],textarea").bind("keydown", function(e) {
    ShortKeyMenu(this.name);
  });
}); 
