﻿var xmlHttp;
var obj;
var loadIMGDiv = "waitbox";
var inimg="<img src='image/ajax-loader.gif' />";

function jPost(a,b,source,divID,loadID,loadIMG){
	inimg = loadIMG;
	loadIMGDiv = loadID;
	Post(a,b,source,divID);
}
function iPost(a,b,source,divID,loadID){
	loadIMGDiv = loadID;
	Post(a,b,source,divID);
}
function Post(a,b,source,divID){
  var pos , chars , operand , mypost;
  if (source.length > 0){
   if(checkForm(a,b)){
    obj = document.getElementById(divID);
    document.getElementById(loadIMGDiv).style.display="block";
  	document.getElementById(loadIMGDiv).innerHTML=inimg;
	//This is the url that links to the php page with POST Elemenets
	var url= source;
    xmlHttp=GetXmlHttpObject3(checkReadyState3)
    xmlHttp.open("POST", url , true)
    xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xmlHttp.setRequestHeader('Content-length', mypost.length);
    xmlHttp.setRequestHeader('Connection', 'close');
	xmlHttp.send(mypost);
   }
  } //  end of the souce.length
  else{
	obj.innerHTML=""
  }
}// ENd of the Get data function

function jGet(source,divID,loadID,loadIMG){
	inimg = loadIMG;
	loadIMGDiv = loadID;
	Get(source,divID);
}
function iGet(source,divID,loadID){
	loadIMGDiv = loadID;
	Get(source,divID);
}
function Get(source,divID){
	obj = document.getElementById(divID);
	if (source.length > 0){
  	  document.getElementById(loadIMGDiv).style.display="block";
  	  document.getElementById(loadIMGDiv).innerHTML=inimg;
	  //This is the url that links to the php page with GET Elemenets
	  var url= source;
      xmlHttp=GetXmlHttpObject3(checkReadyState3)
      xmlHttp.open("GET", url , true)
	  xmlHttp.send(null)
	}else
	  obj.innerHTML="";
}

function checkForm(a,b){
	if((a==0) || (b==0))
		return true;
	var pos , chars , operand , mypost;
	for(i=0 ; i<a.length ; i++){
		if(a[i]=='###')
			continue;
		if(b[i].indexOf('#@' , 0)!=-1){
			mypost = mypost + '&'+a[i]+'='+document.getElementById(a[i]).value;
			continue;
		}
		pos = a[i].indexOf('#' , 0);
		if(pos==-1){
			if(!checkEmp(a[i] , b[i]))
				return false;
			mypost = mypost + '&'+a[i]+'='+document.getElementById(a[i]).value;
		}else{
			operand = a[i].slice(pos+1);
			a[i] = a[i].slice(0 , pos);
			if(!checkEmp(a[i] , b[i]))
				return false;
			chars = operand.slice(3);
			operand = operand.slice(0 , 3);
			switch (operand) {
				case 'mai' :
					if(!email(a[i]))
						return false;
					mypost = mypost + '&'+a[i]+'='+document.getElementById(a[i]).value; break;
				case 'pas' : 
					if(!checkEmp(a[i+1] , b[i+1]))
						return false;
					if(!checkEqual(a[i] , a[i+1] , b[i]+' و '+b[i+1]))
						return false;
						mypost = mypost + '&'+a[i]+'='+document.getElementById(a[i]).value;
						mypost = mypost + '&'+a[i+1]+'='+document.getElementById(a[i+1]).value;
						a[i+1] ='###'; break;
				case 'min' :
					if(!checkMin(a[i] , chars , b[i]))
						return false;
					mypost = mypost + '&'+a[i]+'='+document.getElementById(a[i]).value; break;
				case 'max' :
					if(!checkMax(a[i] , chars , b[i]))
						return false;
					mypost = mypost + '&'+a[i]+'='+document.getElementById(a[i]).value; break;
				default : break;
			}
			
		}
	}
	return true;
}


function GetXmlHttpObject3(handler){
  var objXmlHttp=null
  if (navigator.userAgent.indexOf("Opera")>=0)
  {
    alert("This example doesn't work in Opera") 
	  return 
  }
  if (navigator.userAgent.indexOf("MSIE")>=0){ 
    var strName="Msxml2.XMLHTTP"
    if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
      strName="Microsoft.XMLHTTP"
    } 
    try{ 
      objXmlHttp=new ActiveXObject(strName)
      objXmlHttp.onreadystatechange=handler 
      return objXmlHttp
    } 
    catch(e){ 
      alert("Error. Scripting for ActiveX might be disabled") 
      return 
    } 
  } 
  if (navigator.userAgent.indexOf("Mozilla")>=0){
    objXmlHttp=new XMLHttpRequest()
	objXmlHttp.onload=handler
	objXmlHttp.onerror=handler 
	return objXmlHttp
  }
} 

function checkReadyState3(){
	if(xmlHttp.readyState < 4) { document.getElementById(loadIMGDiv).innerHTML =inimg; }
	else
	{
		if(xmlHttp.status == 200)
		{
			obj.innerHTML=xmlHttp.responseText;
			document.getElementById(loadIMGDiv).style.display="none";
		    document.getElementById(loadIMGDiv).innerHTML = "";
		}
		else if(xmlHttp.status == 404)
		{
			// Add a custom message or redirect the user to another page
			document.getElementById(loadIMGDiv).style.display="block";
			document.getElementById(loadIMGDiv).innerHTML = "File Not Found";
		}
		else
		{
			document.getElementById(loadIMGDiv).style.display="block";
			document.getElementById(loadIMGDiv).innerHTML = "Error";
		}
	}
}


function email(elem) {
    var val=".لطفا پست الکترونيکي معتبر وارد نماييد";
	var str = document.getElementById(elem).value;
	var at="@";
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		alert(val)
		return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		alert(val)
		return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert(val)
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
		alert(val)
		return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert(val)
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		alert(val)
		return false
	}
		
	if (str.indexOf(" ")!=-1){
		alert(val)
		return false
	}
 	return true;
}
	
function checkEmp(elem , msg) {
	if (document.getElementById(elem).value == "") {
		alert (".لطفا "+msg+" را وارد كنيد")
		return false;
	}
	return true;
}

	
function checkEqual(elem1 , elem2 , msg ) {
	if (document.getElementById(elem1).value != document.getElementById(elem2).value) {
		alert ("."+msg+" ، با هم يكسان نيستند")
		return false;
	}
	return true;
}


function checkMin(elem , minLength , msg) {
	if (document.getElementById(elem).value.length < minLength) {
		alert (".حداقل كاراكتر مجاز براي "+msg+" ، "+minLength+ " مي باشد")
		return false;
	}
	return true;
}
function checkMax(elem , maxLength , msg) {
	if (document.getElementById(elem).value.length > maxLength) {
		alert (".حداكثر كاراكتر مجاز براي "+msg+" ، "+maxLength+ " کاراکتر مي باشد \n عبارتي كه شما وارد كرديد ، "
		+document.getElementById(elem).value.length+" كاراكتر است" )
		return false;
	}
	return true;
}

