﻿/*======================================================================*\
|| #################################################################### ||
|| # Complete Check Form Script And Ajax Post Form And Get data ( cjx.js ) # ||
|| # Requirment Script Page: jQuery 1.2.1 # ||
|| # ------------------------------------------------------------------------- # ||
|| # Copyright ©2008 Mohammad Habibi. All Rights Reserved. # ||
|| # Contact Email : habibi.mh@gmail.com # ||
|| #################################################################### ||
\*======================================================================*/
var mainDiv= 'content';
var loadDiv= 'waitbox';
var chatScript= false; //only for latif NIA ;




function setCenter(div){
	var w= parseInt($(div).css('width'));
	var h= parseInt($(div).css('height'));
	
	$(div).css("top",Math.round(($(window).height()/2 - h/2 ))+'px');
	$(div).css("left",Math.round(($(window).width()/2 - w/2))+'px');
}
function showLayer(layerName) 
{
    if (document.getElementById(layerName).style.display == 'none' ) {
		$('#'+layerName).fadeIn();
		//document.getElementById(layerName).style.display = '';
	} else
	{
		$('#'+layerName).fadeOut();
		//document.getElementById(layerName).style.display = 'none';
	}
}
function showLayerSC(layerName) 
{
    if (document.getElementById(layerName).style.display == 'none' ) {
		$('#'+layerName).slideDown();
		//document.getElementById(layerName).style.display = '';
	} else
	{
		$('#'+layerName).slideUp();
		//document.getElementById(layerName).style.display = 'none';
	}
}
function onlyshowLayer(layerName) 
{
	if(document.getElementById(layerName).style.display == 'none' )
	$('#'+layerName).fadeIn();
}
function onlyhideLayer(layerName) 
{
	$('#'+layerName).fadeOut();
}

function hideLayer(layerName) 
{
	document.getElementById(layerName).style.display = 'none';
}
function showLayer2(layerName) 
{
	document.getElementById(layerName).style.display = '';
}
var request_in_progress = false;
var request_in_progress2 = false;
var request_in_progress_post = false;
var staticMainDiv= mainDiv;
var xmlHttp,obj,queryStr,Action;
function waiting(){
	if(!chatScript){
		$('#'+mainDiv).hide().empty();
		$('#'+loadDiv).fadeIn();
	}else{
		$('#'+loadDiv).fadeIn();
	}


}
function loading(){
	if(!chatScript){
		$('#'+loadDiv).hide();
		$('#'+mainDiv).fadeIn();
	}else{
		$('#'+loadDiv).hide();
	}

}
function Get(page,divID,Act){
	if(!document.getElementById(divID) && !document.getElementById(mainDiv)){
		/*alert('Error on Page Element');*/ return false; }
	var getDiv= (divID == null)? '#'+mainDiv:'#'+divID;
	if (request_in_progress) return;
	$(getDiv).hide().empty().load(page, function() {
		request_in_progress = false;
		$('#'+loadDiv).hide();
		$(this).fadeIn();
		if(Act) eval(Act);
	});
	$('#'+loadDiv).fadeIn();
	request_in_progress = true;
}
function Get2(page,divID,Act){
	if(!document.getElementById(divID) && !document.getElementById(mainDiv)){
		/*alert('Error on Page Element');*/ return false; }
	var getDiv= (divID == null)? '#'+mainDiv:'#'+divID;
	if (request_in_progress2) return;
	$(getDiv).hide().empty().load(page, function() {
		request_in_progress2 = false;
		$('#'+loadDiv).hide();
		$(this).fadeIn();
		if(Act) eval(Act);
	});
	$('#'+loadDiv).fadeIn();
	request_in_progress2 = true;
}
function Post(a,b,source,divID,Act,postData){

  if (source.length>0 && checkForm(a,b)){
	if (request_in_progress_post) return;
    mainDiv= (divID == null)? mainDiv:divID;
    var thisQueryStr= (postData == null)? queryStr:postData;
    Action= (Act == null)? '':Act;
	if(document.getElementById(mainDiv)){
		obj = document.getElementById(mainDiv);
	}else{ alert('Error on Page Element'); return false; }
	waiting();
	xmlHttp=GetXmlHttpObject(checkReadyState)
    xmlHttp.open("POST", source , true)
    xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xmlHttp.setRequestHeader('Content-length', thisQueryStr.length);
    xmlHttp.setRequestHeader('Connection', 'close');
	xmlHttp.send(thisQueryStr);
	request_in_progress_post = true;
  }
}
function GetXmlHttpObject(handler){
  var objXmlHttp=null
  if (navigator.userAgent.indexOf("Opera")>=0)
  {
    alert("This site 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 checkReadyState(){
	if(xmlHttp.readyState < 4) {
		//waiting();
	}else{
		if(xmlHttp.status == 200){
			if(!chatScript)
				obj.innerHTML=xmlHttp.responseText;
			else
				obj.innerHTML += xmlHttp.responseText;
			loading();
			request_in_progress_post = false;
			mainDiv = staticMainDiv;
			if(Action) eval(Action);
		}else if(xmlHttp.status == 404){
			obj.innerHTML = "File Not Found";
			loading();
		}else{
			var txt_msg = "<span class=text>اتصال به اينترنت قطع مي باشد</span>";
			if(!chatScript)
				obj.innerHTML = txt_msg;
			else
				obj.innerHTML += txt_msg;
			
			loading();
		}
	}
}
function checkForm(a,b){
  if((a==null) || (b==null)) return true;
  var pos ='';
  var chars ='';
  var operand ='';
  var mypost ='';
  for(i=0 ; i<a.length ; i++){
	if(a[i]=='###') continue;
	if(b[i].indexOf('#@' , 0)!=-1){
	  mypost = mypost + '&'+a[i]+'='+EL(a[i]).value;
	  continue;
	}
	pos = a[i].indexOf('#' , 0);
	if(pos==-1){
	  if(!checkEmp(a[i] , b[i])) return false;
	  mypost = mypost + '&'+a[i]+'='+EL(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]+'='+EL(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]+'='+EL(a[i]).value;
		  mypost = mypost + '&'+a[i+1]+'='+EL(a[i+1]).value;
		  a[i+1] ='###'; break;
		case 'min' :
		  if(!checkMin(a[i] , chars , b[i])) return false;
		  mypost = mypost + '&'+a[i]+'='+EL(a[i]).value; break;
		case 'max' :
		  if(!checkMax(a[i] , chars , b[i])) return false;
		  mypost = mypost + '&'+a[i]+'='+EL(a[i]).value; break;
		case 'int' :
		  if(!checkInt(a[i] , b[i])) return false;
		  mypost = mypost + '&'+a[i]+'='+EL(a[i]).value; break;
		default : break;
	  }
	}
  }
  queryStr = mypost;
  return true;
}
function focus(elem){EL(elem).focus()}
function select(elem){
	EL(elem).focus()
	EL(elem).select()
}
function email(elem) {
    var val=".لطفا پست الکترونيکي معتبر وارد نماييد";
	var str = EL(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)
		select(elem)
		return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		alert(val)
		select(elem)
		return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert(val)
		select(elem)
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
		alert(val)
		select(elem)
		return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert(val)
		select(elem)
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		alert(val)
		select(elem)
		return false
	}
		
	if (str.indexOf(" ")!=-1){
		alert(val)
		select(elem)
		return false
	}
 	return true;
}
function checkEmp(elem , msg) {
	if(!elem){
		return true;
	}
	if (EL(elem).value == "") {
		alert (".لطفا "+msg+" را وارد كنيد")
		focus(elem)
		return false;
	}
	return true;
}
function checkInt(elem , msg) {
	if (isNaN(EL(elem).value)) {
		alert (".لطفا در قسمت "+msg+" عدد وارد كنيد")
		select(elem)
		return false;
	}
	return true;
}
function checkEqual(elem1 , elem2 , msg ) {
	if (EL(elem1).value != EL(elem2).value) {
		alert ("."+msg+" ، با هم يكسان نيستند")
		select(elem1)
		return false;
	}
	return true;
}
function checkMin(elem , minLength , msg) {
	if (EL(elem).value.length < minLength) {
		alert (".حداقل كاراكتر مجاز براي "+msg+" ، "+minLength+ " مي باشد")
		select(elem)
		return false;
	}
	return true;
}
function checkMax(elem , maxLength , msg) {
	if (EL(elem).value.length > maxLength) {
		alert (".حداكثر كاراكتر مجاز براي "+msg+" ، "+maxLength+ " کاراکتر مي باشد \n عبارتي كه شما وارد كرديد ، "
		+EL(elem).value.length+" كاراكتر است" )
		select(elem)
		return false;
	}
	return true;
}
function EL(obj){
	var theObj;
	if (document.all){
		if (typeof obj=='string'){
			return document.all(obj);
		}else{
			return obj.style;
		}
	}
	if (document.getElementById){
		if (typeof obj=='string'){
			return document.getElementById(obj);
		}else{
			return obj.style;
		}
	}
	return null;
}

function getFormValues(form) {
	var str = "";
	$(':input', $(form) ).each(function(i) {
	  if ((this.type == 'radio') || (this.type == 'checkbox')) {
		   if (this.checked) {
				if(this.name)
					str += this.name + "=" + escape(this.value) + "&";
		   }
	  } else {
			if(this.name)
				str += this.name + "=" + escape(this.value) + "&";
	  }
	});
	str = str.substr(0, str.length-1);
	return str;
};

function setFormValues(form,user_data) {
	if(!user_data)
		return '';
	var val;
	$(':input', $(form) ).each(function(i) {
		if(this.name){
			val = isExistValues(user_data,this.name);
			
			if ((this.type == 'radio') || (this.type == 'checkbox')){
				this.checked = true;
			}else{
				$(this).attr('value',val);
			}
		
			
			
		}
	});
};

function isExistValues(str,name) {
	if(!str)
		return '';
	var ary = str.split('&');
	var fr;
	for(i=0 ; i<ary.length ; i++ ){
		fr = ary[i].split('=');
		if(unescape(fr[0])==name) return unescape(fr[1]);
	}
	return '';
};
