//////////////////////////////////////////////////////////////////////////////////////////
//Javascript Form validation 
//Author: Hao Zhuang
//Date:   2002/03
//Copyright: Great Lakes Fishery Commission
//
//////////////////////////////////////////////////////////////////////////////////////////


function trimString(s)
{
 return s.replace(/^\s*|\s*$/g,""); 
}


//////////////////////////////////////////////////////////////////////////////////////////


function checkFirstname(theform)
{

var result = true;

var n = trimString(theform.fname.value);
if(n=="") {alert("Please fill your first name!");result=false;}
else{
	badchars = /[^a-zA-Z0-9_\.\- ']/;  // want to match all character but not letters,underscore,single quote,digits 
        if(badchars.test(n)){alert("First name field can only contain letters,digits, and single quote!");result=false;}
     }
     
     
return result;

}

function checkLastname(theform)
{

var result = true;

var n = trimString(theform.lname.value);
if(n=="") {alert("Please fill your last name!");result=false;}
else{
	badchars = /[^a-zA-Z0-9_\.\- ']/;  // want to match all character but not letters,underscore,single quote,digits 
        if(badchars.test(n)){alert("Last name field can only contain letters,digits, and single quote!");result=false;}
     }
     
     
return result;

}

function checkAgency(theform)
{

var result = true;


var n = trimString(theform.agency.value);

if(n=="") {alert("Please fill the agency field!");result=false;}
else {
	badchars = /[^a-zA-Z0-9_ \-\.\(\)']/;  // want to match all character but not letters,underscore,quote,digits 
	if(badchars.test(n)){alert("Agency field can only contain letters,digits and single quote!");result=false;}
     }
return result;  //if you use return true, it returns ture all the way to the first level, such that you can not check next field

}



function checkAddress(theform)
{

var result = true;

var n = trimString(theform.address.value);
if(n=="") {alert("Please fill the address field!");result=false;}
else{
	badchars = /["]/;  // can't use double quote
        if(badchars.test(n)){alert("Doulbe quotes must be replaced with single quotes!");result=false;}
     }
     
return result;
}

function checkCity(theform)
{

var result = true;

var n = trimString(theform.city.value);
if(n=="") {alert("Please fill the city field!");result=false;}
else{
	badchars = /[^a-zA-Z0-9_\.\- ']/;  // want to match all character but not letters,underscore,single quote,digits 
        if(badchars.test(n)){alert("City field can only contain letters,digits, and single quote!");result=false;}
     }
     
     
return result;

}

function checkState(theform)
{

var result = true;

var n = trimString(theform.state.value);
if(n=="") {alert("Please fill the state field!");result=false;}
else{
	badchars = /[^a-zA-Z0-9_\. \-']/;  // want to match all character but not letters,underscore,single quote,digits 
        if(badchars.test(n)){alert("State field can only contain letters,digits, and single quote!");result=false;}
     }
     
     
return result;

}

//////////////////////////////////////////////////////////////////////////////////////////


function checkZip(theform)
{

var result = true;

var n = trimString(theform.zip.value);
if(n=="") {alert("Please fill the postal code field!");result=false;}
else{
	badchars = /[^a-zA-Z0-9_\- ]/;  // want to match all character but not letters,underscore,single quote,digits 
        if(badchars.test(n)){alert("Postal code field can only contain letters,digits!");result=false;}
     }
     
     
return result;

}


function checkPhone(theform)
{

var result = true;

var n = trimString(theform.phone.value);
if(n=="") {alert("Please provide your phone number!");result=false;}
else{
	var yourphone = n.replace(/[\(\)\.\-\ ]/g, '');
        if( (isNaN(parseInt(yourphone)))||!((yourphone.length==10)||(yourphone.length==11))||(/[^0-9]/.test(yourphone))){alert("Phone number is not correctly entered!");result=false;}
        //need to modify, becaue it ignores something like 11111111111www
     }
    
     
return result;

}
 

function checkEmail(theform)
{

var result = true;

var n = trimString(theform.email.value);
if(n=="") {alert("Please fill the email field!");result=false;}
else{
	rightformat = /^.+@.+\..{2,3}$/;  // email pattern 
        if(!rightformat.test(n)){alert("Email format is not correct!");result=false;}
        // can do further filter here, to filter out illegal chars such as : (,),>...
     }
     
     
return result;

}

function checkCommittee(theform)
{

var result = true;

if(theform.lec.checked==false&&theform.loc.checked==false&&theform.lhc.checked==false&&theform.lmc.checked==false&&theform.lsc.checked==false){alert("Please select at least one committee meeting field!");result=false;}
     
return result;

}






