function onSubmit(){
	var frm=document.getElementById("loginForm");
	if(checkValidParamertes()){
		frm.submit();
	}
}

function checkValidParamertes()
{
	var result=checkValidParamertesInternal();
	if(result==false)
	{
		var count = document.getElementById("countError").value;
		if(count=="01111")
			window.location ="followToUpgrade.jsp";
	}
	return result;
}

function checkValidParamertesInternal()
{
	var MAIL_COUNTER=document.getElementById("countEMail");
	if(MAIL_COUNTER.value*1>2){
		window.location.href="/CivilServant/jsp/followToUpgrade.jsp";
	}
	var count=0;
	document.getElementById("message").innerHTML="המערכת מחפשת , אנא המתן...";
	document.getElementById("message").style.display = "block";
	var id = document.getElementById("identityNumber").value;
 	 if(id.length==0 || document.getElementById("vehicalNumber").value.length==0)
	 {
		document.getElementById("message").innerHTML="חובה למלא את כל הערכים";
		document.getElementById("showError").style.display = "block";
		count = document.getElementById("countError").value;
		count=count+1;
		document.getElementById("countError").value=count;
		return false;
		
	 }  
	 
	 if(document.getElementById("emailAddress").value.length==0
  	 ||
  	 document.getElementById("emailAddressR").value.length==0  	 
  	 ||
  	 document.getElementById("cellPhoneSuffix").value.length==0 )
	{  	 
		document.getElementById("message").innerHTML="חובה למלא את כל הערכים";
		document.getElementById("showError").style.display = "block";
		return false;
	}
	
	/**/
	if(checkId(id)==false)
	{
		document.getElementById("message").innerHTML="ת. זהות לא תקינה";
		document.getElementById("showError").style.display = "block";
		document.getElementById("identityNumber").value="";
		count = document.getElementById("countError").value;
		count=count+1;
		document.getElementById("countError").value=count;
	
		return false;
	}
	/**/
	var len = document.getElementById("vehicalNumber").value.length;	

	
	if(document.getElementById("vehicalNumber").value=="0000000" || len <5 || len>7)
	{
		document.getElementById("message").innerHTML="מספר רישוי לא תקין";
		document.getElementById("showError").style.display = "block";
		document.getElementById("vehicalNumber").value="";
		count = document.getElementById("countError").value;
		count=count+1;
		document.getElementById("countError").value=count;
	
		return false;
	}
	
	
	
	if (checkValidMail()==false){
		return  false;
	}
	
	if (checkEmail("emailAddress")==false){
		document.getElementById("message").innerHTML="כתובת שהוזנה אינה תקינה, יש להזין שוב";
		document.getElementById("showError").style.display = "block";
		document.getElementById("emailAddress").value="";
		MAIL_COUNTER.value=MAIL_COUNTER.value*1+1;
		return  false;
	}
	if (checkEmail("emailAddressR")==false){
		document.getElementById("message").innerHTML="כתובת לאימות שהוזנה אינה תקינה, יש להזין שוב";
		document.getElementById("showError").style.display = "block";
		document.getElementById("emailAddressR").value="";
		MAIL_COUNTER.value=MAIL_COUNTER.value*1+1;
		return  false;
	}
	if (document.getElementById("emailAddress").value != document.getElementById("emailAddressR").value)
	{
		document.getElementById("message").innerHTML="כתובת לאימות שהוזנה אינה זהה לכתובת, יש להזין שוב";
		document.getElementById("showError").style.display = "block";
		document.getElementById("emailAddressR").value="";
		MAIL_COUNTER.value=MAIL_COUNTER.value*1+1;
		return  false;
	
	}
	
	len = document.getElementById("cellPhoneSuffix").value.length;

	if(document.getElementById("cellPhoneSuffix").value=="0000000" || len <7)
	{
		document.getElementById("message").innerHTML="מספר טלפון לא תקין";
		document.getElementById("showError").style.display = "block";
		document.getElementById("cellPhoneSuffix").value="";
		return false;
	}
  	
  	var pagesObj = document.getElementById("cellPhonePreffix"); 
	if ( pagesObj.options[pagesObj.selectedIndex].value==1){
		document.getElementById("message").innerHTML="חובה לבחור קידומת";
		document.getElementById("showError").style.display = "block";
		return false;

	}
	return true;
			
	
}

function checkValidMail()
{
	var MAIL_COUNTER=document.getElementById("countEMail");
	var email = document.getElementById("emailAddress").value;
	var aPosition = email.indexOf("@");
	if(aPosition==-1)
	{
		document.getElementById("message").innerHTML="כתובת שהוזנה אינה תקינה, יש להזין שוב";
		document.getElementById("emailAddress").value="";
		document.getElementById("showError").style.display = "block";
		MAIL_COUNTER.value=MAIL_COUNTER.value*1+1;
		return false;
	}
	var emailTemp = email.substring(aPosition+1);
	aPosition = emailTemp.indexOf("@");
	if(aPosition!=-1)
	{
		document.getElementById("message").innerHTML="כתובת שהוזנה אינה תקינה, יש להזין שוב";
		document.getElementById("emailAddress").value="";
		document.getElementById("showError").style.display = "block";
		MAIL_COUNTER.value=MAIL_COUNTER.value*1+1;
		return false;
	}
	
	var aPosition = email.lastIndexOf(".");
	var emailTemp = email.substring(aPosition+1);
	if(emailTemp.length<2)
	{
		document.getElementById("message").innerHTML="כתובת שהוזנה אינה תקינה, יש להזין שוב";
		document.getElementById("emailAddress").value="";
		document.getElementById("showError").style.display = "block";
		MAIL_COUNTER.value=MAIL_COUNTER.value*1+1;
		return false;
	}

}


function checkStatus(id , vehicalNumber, email ,phone)
{
	
	try 
	{
 		 xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
 		 xmlhttp.open("POST","check.jsp?id="+id+"&vehicalNumber="+vehicalNumber+"&emailAddress="+email+"&phone="+phone,false);
	 } 
	 catch (e) 
	 {
		  try 
		  {
			   xmlhttp = new XMLHttpRequest();
		   	   xmlhttp.open("GET","check.jsp?id="+id+"&vehicalNumber="+vehicalNumber+"&emailAddress="+email+"&phone="+phone,false);
	  	  } 
   	  	  catch (E) 
	  	  {
		    xmlhttp=false;
          }
     }   
    
	xmlhttp.setRequestHeader("Content-Type", "text/xml");
	xmlhttp.onreadystatechange=getContenetCheckStatus;
	xmlhttp.send(null); 
}

function getContenetCheckStatus() 
{
	if (xmlhttp.readyState==4) 
	{
		if (xmlhttp.status==200) 
		{
			var content = xmlhttp.responseText;
			if(content.indexOf('ERROR')==0)
			{
				document.getElementById("message").innerHTML="ישנה בעיה במערכת , אנא נסה שנית במועד מאוחר יותר";
				document.getElementById("showError").style.display = "block";
				return;
			}
			else if(content.indexOf('N')==0)
			{
				window.location ="agree.jsp";
			}
			else if(content.indexOf('Y1')==0)
			{
				window.location ="ExistAndEligible.jsp";
			}
			else if(content.indexOf('Y2')==0)
			{
				window.location ="ExistAndNotEligible.jsp";
			}
			else if(content.indexOf('Y3')==0)
			{
				window.location ="notExistAndEligible.jsp";
			}
			else if(content.indexOf('Y4')==0)
			{
				window.location ="notExistAndNotEligible.jsp";
			}
			else if(content.indexOf('Y5')==0)
			{
				window.location ="renewedPolicyDetails.jsp";
			}
			else if(content.indexOf('BAD_PARAM')==0)
			{
				document.getElementById("message").innerHTML="המערכת אינה מזהה את אחד או יותר מהנתונים שהקשת - נא הקש את הנתונים מחדש";
				document.getElementById("showError").style.display = "block";
				return;
			}
					
		}
	}	
}

function emailCheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true
}

function checkId(idnum){
 
    while (idnum.length<9){
        idnum="0"+idnum;
    }
    if (idnum=="000000000")
    	return false;
    idnum1=idnum.substr(0,1)*1;
    idnum2=idnum.substr(1,1)*2;
    idnum3=idnum.substr(2,1)*1;
    idnum4=idnum.substr(3,1)*2;
    idnum5=idnum.substr(4,1)*1;
    idnum6=idnum.substr(5,1)*2;
    idnum7=idnum.substr(6,1)*1;
    idnum8=idnum.substr(7,1)*2;
    idnum9=idnum.substr(8,1)*1;
 
    if (idnum1>9) idnum1=(idnum1%10)+1
    if (idnum2>9) idnum2=(idnum2%10)+1
    if (idnum3>9) idnum3=(idnum3%10)+1
    if (idnum4>9) idnum4=(idnum4%10)+1
    if (idnum5>9) idnum5=(idnum5%10)+1
    if (idnum6>9) idnum6=(idnum6%10)+1
    if (idnum7>9) idnum7=(idnum7%10)+1
    if (idnum8>9) idnum8=(idnum8%10)+1
    if (idnum9>9) idnum9=(idnum9%10)+1
 
    var sumval=idnum1+idnum2+idnum3+idnum4+idnum5+idnum6+idnum7+idnum8+idnum9;
    sumval=sumval%10
    if (sumval>0){
//        alert("תעודת הזהות שגוייה");
        return false;
    }
 
  return true;
}

function checkEmail(parm) {
	var mailAdress = document.getElementById(parm).value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mailAdress)){
		return (true)
	}
	return false;
}

