﻿// Copyright 2002-2003 Jennifer Madden
// www.jennifermadden.com

function Reset(){
return confirmIt = confirm("האם למחוק את כל הנתונים בטופס ?")
}

function foundSpaces(field){
	if ( field.indexOf(" ") == -1) {
		return false;
	}
	return true;
}

function Validate_Email_Address(email_address)
         {
         //Assumes that valid email addresses consist of user_name@domain.tld
         at = email_address.indexOf('@');
         dot = email_address.indexOf('.');
		 
		 if (dot <= at + 1)
			dot = email_address.substr(at + 1, email_address.length).indexOf('.') + at + 1;
         
         if(at == -1 || 
            dot == -1 || 
            dot <= at + 1 ||
            dot == 0 || 
            dot == email_address.length - 1)
            return(false);
            
         user_name = email_address.substr(0, at);
         domain_name = email_address.substr(at + 1, email_address.length);                  
         
        if(Validate_String(user_name) === false || 
            Validate_String(domain_name) === false)
            return(false);                     
         
         return(true);
         }
		 
function IsEmpty(aTextField) {
   if ((aTextField.length==0) || (aTextField==null)) {
      return true;
   }
   else { return false; }
}


function checkFields(){

var theform = document.forms[0]

for(i=0; i<theform.elements.length; i++){
		var field = theform.elements[i]
		//alert(theform.elements[i].type)
		var isReq = (field.name.charAt(0)=="*") ? true : false
		if(isReq){
		
			if((field.type=="text" || field.type=="textarea" || field.type=="password") && IsEmpty(field.value)){
			alert("יש למלא את השדה '"+field.name.substring(1,field.name.length)+ "'.")
			setTimeout("document.forms[0].elements["+i+"].focus()", 10)
			return false
			}
			
			if((field.type=="text" || field.type=="textarea" || field.type=="password") && (field.name=="*טלפון / נייד") && foundSpaces(field.value)){
			alert("השדה '"+field.name.substring(1,field.name.length)+ "' מכיל רווחים מיותרים.")
			setTimeout("document.forms[0].elements["+i+"].focus()", 10)
			return false
			}			
			
			if(field.type=="select-one" && field.selectedIndex == 0){	
			alert("Please select a value for the '"+field.name+ "' field.")
			setTimeout("document.forms[0].elements["+i+"].focus()", 10)
			return false
			}
			
			
			if(field.type=="select-multiple"){	
			Sel=0
				for(o=0; o<field.options.length; o++){
					if(field.options[o].selected){
					Sel++
					break
					}
				}
				if(Sel == 0){
				alert("Please select a value for the '"+field.name+ "' field.")
				setTimeout("document.forms[0].elements["+i+"].focus()", 10)
				return false
				}			
			}
			
			if(field.type=="checkbox" || field.type=="radio"){
			var startingIndex = i	
			var Checked = 0
			var rLength=1
				while(field.type == theform.elements[i+1].type){
				rLength++
				i++ 
				}
				
				for(g = startingIndex; g < rLength+startingIndex; g++){
					if(theform.elements[g].checked){
					Checked++
					break
					}
				}		
				if(Checked == 0){
				i=startingIndex
				alert("Please select a value for the '"+field.name+ "' "+field.type+" group")				
				setTimeout("document.forms[0].elements["+i+"].focus()", 10)
				return false
				}
			}
			
		}//isReq
		
		if(field.name=="דואר אלקטרוני" && !IsEmpty(field.value))
		{
		    //if (field.value.indexOf("@")==-1 || field.value.indexOf(".")==-1)
		    if (!Validate_Email_Address(field.value))
		    {
		        alert("כתובת הדואר האלקטרוני שהוקלדה אינה תקינה !")
		        setTimeout("document.forms[0].elements["+i+"].focus()", 10)
		        return false
		    }
		}			
	  
	}//for
	return true
}
