﻿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;
			}
			
		}
	}
	mypost3 = mypost;
	return true;
}

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;
}


