
/* JavaScript functions for validating form */

function validData(formName){
	
	var error_string = "";
	var frm = document.getElementById(formName);
		
	// check the name fields
	if(isEmpty(frm.first_name.value)|| (alpha(frm.first_name.value))){
		error_string += "First Name\n";
		document.getElementById("fname").style.color="#990000";
			
	}else{	
		document.getElementById("fname").style.color="#000";
	}
	
	if(isEmpty(frm.last_name.value)|| (alpha(frm.last_name.value))){
		error_string += "Last Name\n";
		document.getElementById("lname").style.color="#990000";
			
	}else{	
		document.getElementById("lname").style.color="#000";
	}
	
	if(isEmpty(frm.address1.value)){
		error_string += "Address\n";
		document.getElementById("addr").style.color="#990000";
	}else{	
		document.getElementById("addr").style.color="#000";
	}

	//check city field
	if(isEmpty(frm.city.value)|| (alpha(frm.city.value))){
		error_string += "City\n";
		document.getElementById("cty").style.color="#990000";
	}else{	
		document.getElementById("cty").style.color="#000";
	}
	
	// check state/province
		
		if (!isChosen(frm.state)){
			error_string += "State/Province\n";
			document.getElementById("ste").style.color="#990000";
		}else{
			document.getElementById("ste").style.color="#000";
		}	
	
	
	
		//*check phone
	if (checkPhone(frm.phone.value) == true){
		error_string += "Phone Number\n";
		document.getElementById("phoneNum").style.color="#990000";	
	}else{	
		document.getElementById("phoneNum").style.color="#000";
	} 
*/
	//check e-mail
		
		if (checkEmail(frm.email.value) == true){
			error_string += "Your e-mail address\n";	
			document.getElementById("eml").style.color="#990000";
		}else{	
		document.getElementById("eml").style.color="#000";
	}
	
		//check radio buttons for agreeing to terms
		if(frm.agree.checked == true)
		{
			document.getElementById("agreeLbl").style.color="#000";
	
		}else{
			
		error_string += "You must read and agree to the Terms and Disclosure Statement.";
		document.getElementById("agreeLbl").style.color="#990000";
	}
	
		
		
	if(error_string ==""){

		return true;
	}
	
	else{
		error_string = "The following items were not filled in completely: \n\n" +error_string +"\nPlease complete the form.\n Thank you\n\n";
		alert(error_string);
		return false;
	}
}	



// function to test whether text input is empty
function isEmpty(str)
{
    str = trimSp(str);	
    if(str == null || str.length == 0)
    {
	return true;
    }
    else
    {
	return false;
    }
}
  

function alpha(str)
{
    var regEx = /^[A-Za-z]+\s*[\-A-Za-z]*?\s*[\-A-Za-z]*?$/;
    
    str = trimSp(str);
    
    if(!(str.match(regEx)))
    {
		return true;
	
	}else{
		return false;
	}
   
}

  
	
	
function checkPhone(numString){
	
	
        numString = trimSp(numString);	

	var regex=/^\(?\d{3}\)?-?\s*\d{3}\s*?-?\d{4}$/;

	
	if(!(regex.test(numString))) {
		return true;
	}
	
}


	
function checkEmail(addy){

	
	//var emailFilter = /^[a-z][\w\.]*@[\w\.]+\.[a-z]{2,3}/i;
	var emailFilter = /^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,4}$/i;
	var illegalChars = /[\(\)\<\>\,\;\:\\\"[\]]/;
	
	addy=trimSp(addy);
	
	if((addy == "") || (!(emailFilter.test(addy))) || (addy.match(illegalChars))){
		return true;
	}
}


//validate select element
function isChosen(select){
	if(select.selectedIndex==0){
		return false;
	}else{
		return true;
	}
}


function whichBox(str){
	
	var i;
	for (i=0;i<str.length;i++)
	{
	    if (str[i].checked)
	{
		return true;
	}
	}
		return false;
}


function trimSp(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}