function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	//alert("Email address seems incorrect (check @ and .'s)")
	return strEMail
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    //alert("The username doesn't seem to be valid.")
    return strUser
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        //alert("Destination IP address is invalid!")
		return strIP
	    }
    }
    return strIP
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//alert("The domain name doesn't seem to be valid.")
    return strDomain
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
/*if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   //alert("The address must end in a three-letter domain, or two letter country.")
   return "שתי ספרות"
}*/

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return strDomain
}

var user=matchArray[1]

// See if "user" is valid
if (user == "anonumous") {
    // user is not valid
    //alert("The username doesn't seem to be valid.")
    return strUser
}

// If we've gotten this far, everything's valid!
return "";
}


function trimStr(str){
	while(''+str.charAt(0)==' '){
		str=str.substring(1,str.length);
	}
	while(''+str.charAt(str.length-1)==' '){
		str=str.substring(0,str.length-1);
	}
	return str	
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function isValidDate(dateStr) {
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables
	
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	var resMsg = "";
	var month;
	var day;
	var year;

	// To require a 4 digit year entry, use this line instead:

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		resMsg = strDate;
		return resMsg;
	}
	day = matchArray[1]; // parse date into variables
	month = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		resMsg = strDateMonth;
		return resMsg;
	}
	if (day < 1 || day > 31) {
		resMsg = strDateDay;
		return resMsg;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		resMsg = strDateMaxDay;
		return resMsg
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			resMsg = "!לחודש פברואר " + year + " אין " + day + " ימים"
			return resMsg;
		}
	}
	return resMsg;  // date is valid
}

function CheckIfTheFieldMustBeNumeric(FieldProp,FieldValueOther,index){
	if ((FieldProp.Numeric == true) && (isNaN(FieldValueOther.value))){
		alert(strFld + FieldProp.DisplayName + "\n" + strNumeric);
		FieldValueOther.focus();
		return false;
	}
	return true;
}

function CheckIfTheFieldMustBeDate(FieldProp,FieldValue,index){
	if (FieldProp.Date == true && FieldValue.value != "") {
		dateMsg = isValidDate(trimStr(FieldValue.value))
  		if (dateMsg != "") {  			
			alert(strFld + FieldProp.DisplayName + (parseInt(index) + 1) +  "," + "\n" + dateMsg)
			FieldValue.focus()
			return false	 
		}else{
			return true
		}
	}else{
		return true
	}
}

function CheckIfTheFieldIsMandatoric(FieldProp,FieldValue,FieldValueOther,Type){
	if (FieldProp.OpenList == true){
		if ((FieldProp.Mandatory == true) && (FieldValue.selectedIndex <= 0) && (trimStr(FieldValueOther.value) == "")){
			return false
		}else{
			return true
		}
	}else{
		if (Type == "select-one"){
			if ((FieldProp.Mandatory) && ((FieldValue.selectedIndex) <= 0) && (FieldValueOther== "0")){
				return false
			}else{
				return true
			}
		}else{
			if ((FieldProp.Mandatory) && (trimStr(FieldValue.value) == "") && (FieldValueOther== "0")){
				return false
			}else{
				return true
			}	
		}
	}
}

function CheckMaxLengthAndMaxNumber(FieldProp,FieldValueOther,index){
	if ((FieldProp.Numeric == true) && (FieldProp.MaxNumber < FieldValueOther.value)){
		alert(strFld + FieldProp.DisplayName + "\n" + strMaxNum + " : " + FieldProp.MaxNumber)
		FieldValueOther.focus()
		return false
	//Checks for the More OpenLists that have to be string if the value is more the the max length allowed		 
	}else if ((FieldProp.Numeric != true) && (FieldProp.MaxLength < (FieldValueOther.value).length)){
		alert(strFld + FieldProp.DisplayName + "\n" + strMaxLen + " : " + FieldProp.MaxLength )
		FieldValueOther.focus()
		return false
	}else{
		return true
	}
	
}

function CheckIfTheFieldMustBeEMail(FieldProp,FieldValue,index){
	if (FieldProp.EMail == true && FieldValue.value != "") {
		mailMsg = emailCheck(trimStr(FieldValue.value))
  		if (mailMsg != "") {  			
  			if (index != "" && !isNaN(index)){
				alert(strFld + FieldProp.DisplayName + (parseInt(index) + 1) +  "," + "\n" + mailMsg)
			}else{
				alert(strFld + FieldProp.DisplayName + "\n" + mailMsg)
			}
			FieldValue.focus()
			return false	 
		}else{
			return true
		}
	}else{
		return true
	}
}

function CheckIfTheFieldMustBePhone(FieldProp,FieldValue,index){
	if (FieldProp.Phone == true && FieldValue.value != "") {
		mailMsg = (trimStr(FieldValue.value)).search(/\(?\d{3}\)?[ \.-]?\d{3}[ \.-]\d{4}/);
  		if (mailMsg != -1) {  			
  			if (index != "" && !isNaN(index)){
				alert(strFld + FieldProp.DisplayName + (parseInt(index) + 1) +  "," + "\n" + strPhone)
			}else{
				alert(strFld + FieldProp.DisplayName + "\n" + strPhone)
			}
			FieldValue.focus()
			return false	 
		}else{
			return true
		}
	}else{
		return true
	}
}

function CheckIfTheFieldMustBeEnglish(FieldProp,FieldValue,index){
	if (FieldProp.English == true && FieldValue.value != "") {
		mailMsg = (trimStr(FieldValue.value)).search(/[^\w\s]/);
  		if (mailMsg != -1) {  			
  			if (index != "" && !isNaN(index)){
				alert(strFld + FieldProp.DisplayName + (parseInt(index) + 1) +  "," + "\n" + strEnglish)
			}else{
				alert(strFld + FieldProp.DisplayName + "\n" + strEnglish)
			}
			FieldValue.focus()
			return false	 
		}else{
			return true
		}
	}else{
		return true
	}
}



function PropertiesCheck(FieldProp,FieldValueOther,index){
	if (!CheckIfTheFieldMustBeNumeric(FieldProp,FieldValueOther,index))
		return false
						
	if (!CheckMaxLengthAndMaxNumber(FieldProp,FieldValueOther,index))
		return false
						
	if (!CheckIfTheFieldMustBeDate(FieldProp,FieldValueOther,index))
		return false
		
	if (!CheckIfTheFieldMustBeEMail(FieldProp,FieldValueOther,index))
		return false

	/*if (!CheckIfTheFieldMustBePhone(FieldProp,FieldValueOther,index))
		return false*/

	if (!CheckIfTheFieldMustBeEnglish(FieldProp,FieldValueOther,index))
		return false
	
	return true	
}

//=== Client Function === clientVerify =========
function clientVerify(f) {	
	var stri
	var lastValue
	var NameSelectIndex
	var nameStr
	var nameIndex
	var charStartPos
	var otherValue
	var tempValue
	var radioValues
	radioValues = false

	strFld = strFld + " "
	
	for(var i = 0; i < f.length; i++) {
		var e = f.elements[i]
		stri  = e.name

		/********************* Checks for the Regular Fields ********************/
		if ((e.type != "button") && (e.type != "submit")  && (e.type != "hidden") && (e.type != "")){

			for (var j=stri.length; j >= 0 ; j--){
					if (isNaN(stri.charAt(j))){
					charStartPos=j
					break
					}
			}
			lastValue	= stri.substring(j+1,stri.length)
			nameStr		= stri.substring(0,j+1)

			//Set Of Checks for regular fields
			Type = e.type
		
			// Check radio type fields
			if (e.type == "radio"){
				if (f.elements[e.name].Mandatory){
					if (e.checked)
						radioValues = true
					if ((f.elements[i+1].name != e.name) && (!radioValues)){
						radioValues = false;
						alert(strFld + e.DisplayName + "\n" + strMandatory)
						return false;
					}else if (f.elements[i+1].name != e.name){
						radioValues = false;
					}
				}
			}else if (!CheckIfTheFieldIsMandatoric(e,e,"0",Type)){
					radioValues = false;
					e.focus();
					alert(strFld + e.DisplayName + "\n" + strMandatory)
					return false;
			}
			if (e.type != "radio"){
				if (!PropertiesCheck(e,e,""))
					return false;	
			}
		}
	}
	return true;
}

function show_msg(msgText,MessageFromFile){
	if(MessageFromFile != undefined)
		{
		MessageFromFile=unescape(MessageFromFile)
		}
		else
			{
			MessageFromFile =''
			}
			
	window.showModalDialog('../message.asp?MessageFromFile=' + MessageFromFile + '&messageText=' + msgText,'','dialogHeight:310px;dialogWidth:420px;center:yes;edge:raised;status:no')
}


// FUNCTION TO CHECK INPUT MAIL IN CONTACY FORM

function new_checkEmail(str) {
///// function for validating email address
		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
		} else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		    return false
		} else 	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		} else  if (str.indexOf(at,(lat+1))!=-1){
		    return false
		} else 	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   return false
		} else  if (str.indexOf(dot,(lat+2))==-1){
		    return false
		} else if (str.indexOf(" ")!=-1){
		     return false
		} else {
 		 	return true
 		}
}


// THE NEW FUNCTION VERIFY CONTACT FORM
function new_clientVerify(f)
{
	var name = f.txtName.value;
	var email = f.txtEMail.value;
	var phone = f.txtPhone.value;
	
	if(name=="")
	{
		alert(_alert_name);
		document.frmDocument.txtName.focus();
		return false;
	}
	
	if(phone=="")
	{
		alert(_alert_phone);
		document.frmDocument.txtPhone.focus();
		return false;
	}
	
	if(email=="")
	{
		alert(_alert_email);
		document.frmDocument.txtEMail.focus();
		return false;
	}

	if(new_checkEmail(email)==false)
	{
		alert(_alert_valid);
		document.frmDocument.txtEMail.focus();
		return false;
	}

	return true;
/* end function */
}	

