

//***************** Check the form elements and submit if valid****************//


function CheckAndSubmit()
{
// check the required fields
var flagAst,flag,lblMsg
lblMissingDetails.innerHTML ="";

flagAst=0;


if (document.frm.txtName.value=="")
{
	lblName.innerHTML="<span class=error>* </span>";
	if (flagAst==0)
		document.frm.txtName.focus();
	flagAst=1;
}
else
	lblName.innerHTML="";



if (document.frm.txtPhone.value=="")
{
	lblPhone.innerHTML="<span class=error>* </span>";
	if (flagAst==0)
		document.frm.txtPhone.focus();
	flagAst=1;
}
else
	lblPhone.innerHTML="";



if (document.frm.txtEmail.value=="")
{
	lblEmail.innerHTML="<span class=error>* </span>";
	if (flagAst==0)
		document.frm.txtEmail.focus();
	flagAst=1;
}
else
	lblEmail.innerHTML="";


if (document.frm.txtSubject.value=="")
{
	lblSubject.innerHTML="<span class=error>* </span>";
	if (flagAst==0)
		document.frm.txtSubject.focus();
	flagAst=1;
}
else
	lblSubject.innerHTML="";
	
if (document.frm.txtDesc.value=="")
{
	lblDesc.innerHTML="<span class=error>* </span>";
	if (flagAst==0)
		document.frm.txtDesc.focus();
	flagAst=1;
}
else
	lblDesc.innerHTML="";





lblMsg="";

if (flagAst==0)
{

	if  (checkLettters(document.frm.txtName.value)==true)
	{
		if (lblMsg!="")
			lblMsg+="<br>"
		else
		{
			document.frm.txtName.focus();
			document.frm.txtName.select();
		}
		lblMsg+="שם לא תקין";
	}

	if (checkNum(document.frm.txtPhone.value)==false)
	{
		if (lblMsg!="")
			lblMsg+="<br>"
		else
		{
			document.frm.txtPhone.focus();
			document.frm.txtPhone.select();
		}
		lblMsg+="טלפון לא תקין";
	}
	else
		if(document.frm.txtPhone.value.length!=7)
		{
			if (lblMsg!="")
					lblMsg+="<br>"
			else
			{
				document.frm.txtPhone.focus();
				document.frm.txtPhone.select();
			}
		lblMsg+="טלפון לא תקין";
		}



	if (ValidateMail(document.frm.txtEmail.value)==false)
	{
		if (lblMsg!="")
			lblMsg+="<br>"
		else
		{
			document.frm.txtEmail.focus();
			document.frm.txtEmail.select();
		}
		lblMsg+="דוא''ל לא תקין";
	}



	if (lblMsg=="")
	{
		document.frm.btn.disabled= true;
		frm.submit();
	}
	else
		lblMissingDetails.innerHTML= "<font color=red>"+lblMsg+"</font>";

}
else
{
	lblMissingDetails.innerHTML = "<font color=red>נא מלא את השדות המסומנים ב *" + "<br></font>";

}
}

function checkNum(num){
var i,c, wrong;
for (i=0 ; i < num.length ; i++)
{
	c = num.charAt(i)
	if ( ( (c<'0') || (c>'9') ))
		return false
}
return true
}


function checkLettters(check){
var i,c, wrong;
for (i=0 ; i < check.length ; i++)
{
c = check.charAt(i)
//        -
if ( ( (c<'a') || (c>'z') ) && ((c<'A') || (c>'Z')) && ((c<'א') || (c>'ת')) && c!=' ')
wrong=1;
}
if (wrong==1)
return true
return false
}

function ValidateMail(WhatToCheck) {
    var ValidCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.@ "
	var state;
    for (i=0; i < WhatToCheck.length; i++)
    {
        if (ValidCharacters.indexOf(WhatToCheck.substring(i,i+1).toUpperCase()) == -1)
        {
           state=1;
        }
    }
      if (state==1)
			return false
	  else
	  {
			return checkmail(WhatToCheck);
	}
}

function checkmail(txt){
var i,c, state,s,d;
s=0;

   for (i=0 ; i < txt.length ; i++) {
                c = txt.charAt(i)

                if ( ( (c<'a') || (c>'z') ) && ((c<'A') || (c>'Z') ) )
                {
			     	if((i==0) || (i==txt.length-1))
			     	state=1;
				}
				if (c=='@')
					s++;
		}


		if (s!=1)
			state=1;
		else
		if ( txt.indexOf('.') == -1 )
			state=1;

        else if (txt.indexOf('@.') > -1)
			state=1;
        else if ( txt.indexOf('.@') > -1)
         state=1;

	if (state==1)
			return false

	return true


}
