//Form Validation. TrafficSynergy 2004-2007
//Designed by : D.Kuhn
//8 AUG 2007



function validateForm(thisform)
{
with (thisform)
{
//the validate variable - if any condition is not met it changes to false
var validate = true;

//the last validated form element
var last_validated = ""


//firstname
if (!validate_required(firstname))
{


validate = false;
alert ("Firstname is Required");

		if (last_validated=="" || last_validated == null)
    {
    last_validated = "firstname";

    }
}
else
{

}//END firstname

//lastname
/*if (!validate_required(lastname))
{

validate = false;
alert ("Lastname is Required");
		if (last_validated=="" || last_validated == null)
    {
    last_validated = "lastname";
		
    }
}
else
{

}*///END lastname




//email
if (!echeck(email.value) || !validate_required(email))
{


if (!echeck(email.value))
{
alert ("Enter a valid email address");
}
else
{
alert ("Email is required");
}



validate = false;

		if (last_validated=="" || last_validated == null)
    {
    last_validated = "email";
		
    }
}
else
{

}//END email



//comments
if (!validate_required(comments))
{


validate = false;
alert ("You must enter a Question");

		if (last_validated=="" || last_validated == null)
    {
    last_validated = "comments";

    }
}
else
{

}//END comments




}//END with

if (!last_validated  == "" || !last_validated == null )
{
document.getElementById(last_validated).focus(); 
}

return validate;



}//END function





//checks if value is null
function validate_required(field)
{


with (field)
    {
			
          if (value==null || value=="" || value=="none")
            {
          	return false;
          	}
         
    		 return true;
			
    }
}


//Check for numeric number
	function validate_numeric(field)
{


validChar='0123456789()+-';// characters allowed in um
var invalidChars;
strlen=field.length;       // how long is test string

if(strlen < 1)
{
return false;
}

// Now scan string for illegal characters
	 for (i=0;i<strlen;i++)
	 {
    if(validChar.indexOf(field.charAt(i))<0)
		{
      alert("The following character is invalid in the Contact Number field: "+field.charAt(i));
			 return false;
    } // end scanning loop

	}

return true;


}

//Check for numeric number
	function validate_numeric_curr(field)
{


validChar='0123456789';// characters allowed in um
var invalidChars;
strlen=field.length;       // how long is test string

if(strlen < 1)
{
return false;
}

// Now scan string for illegal characters
	 for (i=0;i<strlen;i++)
	 {
    if(validChar.indexOf(field.charAt(i))<0)
		{
      alert("The following character is invalid : "+field.charAt(i));
			 return false;
    } // end scanning loop

	}

return true;


}

//Check id number
function isValidIdNumber(idObj)  
{  
    obj_value = idObj;  
    var valid = true;  
    strID = obj_value;  
    runningTotal = 0;  
    if(strID.length == 13)  
    {  
          for(i = 0; i < 13; i += 2)   
                 runningTotal += parseInt(strID.substr(i, 1));  
          for(i = 1; i < 13; i += 2)   
               if(strID.substr(i,1) < 5) runningTotal += parseInt((2*strID.substr(i,1)));   
               else runningTotal += parseInt((2*strID.substr(i,1) + 1));  
          if(runningTotal % 10 == 0)   
               return true;  
          else   
               valid = false;   
    }  
    else   
          valid = false;  
       
     if (!valid)  
     {  
          return false;  
     }  
     return true;  
}




//Check email address  
function echeck(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					
	}
	
		

