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


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


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


function checkName(theform)
{

var result = true;

var n = trimString(theform.name.value);
if(n=="") {alert("Please fill the name 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("Name field can only contain letters,digits, and single quote!");result=false;}
     }
     
     
return result;

}

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


function checkTitle(theform)
{

var result = true;

//Title field empty is fine, but cannot contain special chars
var n = trimString(theform.title.value);

badchars = /[^a-zA-Z0-9_\.\- ']/;  // want to match all character but not letters,underscore,single quote,digits 
if(badchars.test(n)){alert("Title 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

}


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


//org optional

function checkOrgOpt(theform)
{

var result = true;

//Organization field empty is fine, but cannot contain special chars
var n = trimString(theform.org.value);

badchars = /[^a-zA-Z0-9_\-\. ']/;  // want to match all character but not letters,underscore,quote,digits 
if(badchars.test(n)){alert("Organization 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

}


// org required

function checkOrg(theform)
{

var result = true;


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

if(n=="") {alert("Please fill the affiliation 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("Organization 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;

}


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

//if state is a text field
/*
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;

}*/

//state is in a select box

function checkState(theform)
{
var result = true;
var n = theform.state.selectedIndex;

if(n==0) {alert("Please select the state/province!");result=false;}
     
return result;

}


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


function checkZip(theform)
{

var result = true;

var n = trimString(theform.zip.value);
if(n=="") {alert("Please fill the zip 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("Zip code field can only contain letters,digits!");result=false;}
     }
     
     
return result;

}


//////////////////////////////////////////////////////////////////////////////////////////
/*
// empty is fine, but no illegal chars

function checkCountry(theform)
{

var result = true;

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

badchars = /["]/;  // can't use double quote
if(badchars.test(n)){alert("Doulbe quotes must be replaced with single quotes!");result=false;}

     
return result;
}
*/

// empty is fine, but if user select the state as not applicable, must specify a country


function checkCountry(theform)
{

var result = true;
var i = theform.state.selectedIndex;
var n = trimString(theform.country.value);

if(i==1&&n=="") {alert("Please fill the country field!");result=false;}

badchars = /["]/;  // can't use double quote
if(badchars.test(n)){alert("Doulbe quotes must be replaced with single quotes!");result=false;}

     
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 checkPhone(theform)
{

var result = true;

var n = trimString(theform.phone.value);
if(n!="") 
{      //phone number empty OK
	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;}
        
   }
     
   return result;
}  
*/

// phone number must be supplied
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;

}
 

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

//optional

function checkPhoneExt(theform)
{

var result = true;

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

}


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


function checkFax(theform)
{

var result = true;

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


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


function checkPaytype(theform)
{

var result = true;

if(theform.ctype[0].checked==false&&theform.ctype[1].checked==false) { alert("Please select your credit card type!");result=false;}

return result;

}

/* when the pay by check and by credit card provided together, use this one 
function checkPaytype(theform)
{

var result = true;
var n = theform.credit_card.selectedIndex;

if(n==0) {
      if(theform.check_2.checked) ;
      else {alert("Please provide your method of payment!");result=false;}
      }
      
else {
      if(theform.check_2.checked) {alert("Can not choose more than one pay method!");result=false;}

     }
     
return result;

}

*/


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


function checkNameOnCard(theform)
{

var result = true;
var n = trimString(theform.cname.value);
	
if(n=="") {alert("Please fill the name on your credit card 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("Name on your credit card field can only contain letters,digits, and single quote!");result=false;}
    }	
     
    
    	
return result;

}


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


function checkCardNumber(theform)
{

var result = true;
var n = trimString(theform.cnumber.value);
var cardnum = n.replace(/[\(\)\.\-\ ]/g, '');  //strip hyphen etc.
	
if(cardnum=="") {alert("Please fill the your credit card number field!");result=false;}
else{
	if(cardnum.length!=16) {alert("Your credit card number is of wrong length!");result=false;}
	else{
	    badchars = /\D/;  // want to match all character but not letters,underscore,single quote,digits 
	    if(badchars.test(cardnum)){alert("Your credit card number field can only contain digits!");result=false;}
	    //more codes can be added to check card number 
	    }
       	
    }	
     
    	
return result;

}


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


function checkCardDate(theform,tm,ty) 
{

var result = true;
var y = theform.year.value;
var m = theform.month.value;
     

if(y==""||m=="") {alert("Please provide the complete expiration date!"); result=false;}
else
  {
       if(y<ty) {alert("This card is expired!");result=false;}
       else {
       		     if(y==ty&&m<=tm){alert("This card is expired!");result=false;}
       	     }
  }   

    
 return result;
 }

/** if the credit card info and other infor at the same page, use this function
function checkCardDate(theform) 
{

var result = true;

if(!(theform.check_2.checked))
    {
       
       var now = new Date();
       var now_mon = now.getMonth()+1;
       var now_year = now.getYear();
       
       var y = theform.yy.value;
       var m = theform.mm.value;
       
       if(y<now_year) {alert("This card is expired!");result=false;}
       else {
            if(y==now_year&&m<=now_mon){alert("This card is expired!");result=false;}
            }
       
    }
    
 return result;
 }
**/

//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
// project specific validations


//RDX: topic field is in a select box

function checkTopic(theform)
{
var result = true;
var n = theform.topic.selectedIndex;

if(n==0) {alert("Please select the topic!");result=false;}
     
return result;

}


function checkKeywords(theform)
{

var result = true;

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

badchars = /["]/;  // can't use double quote
if(badchars.test(n)){alert("Doulbe quotes must be replaced with single quotes in keywords field!");result=false;}
     
return result;
}

function checkPtitle(theform)
{

var result = true;

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

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


function checkDescription(theform)
{

var result = true;

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

if(n=="") {alert("Please fill the description field!");result=false;}
else {
	badchars = /["]/;  // can't use double quote
	if(badchars.test(n)){alert("Doulbe quotes must be replaced with single quotes in description field!");result=false;}
	if(n.length>3000){alert("Description is too long!");result=false;}
     }
     
return result;
}