// JavaScript Document


function checkFormValidation(CatalogRequestForm) {

	
	for(i=0; i<document.CatalogRequestForm.elements.length; i++)
{
document.CatalogRequestForm.elements[i].className='regular';
}


// build stings to check
var phone = document.getElementById("phone1").value + document.getElementById("phone2").value + document.getElementById("phone3").value;
var fax = document.getElementById("fax1").value + document.getElementById("fax2").value + document.getElementById("fax3").value;
	
    var why = "";
	
	why += isEmpty(document.getElementById("firstname").value,'firstname','a first name');
	why += isEmpty(document.getElementById("lastname").value,'lastname','a last name');
	why += isEmpty(document.getElementById("address").value,'address','an address');
	why += isEmpty(document.getElementById("city").value,'city','a city');
	why += checkDropdown(document.getElementById("state").value,'state','state/province');
	why += isEmpty(document.getElementById("zip").value,'zip','a zip/postal code');
	why += checkDropdown(document.getElementById("country").value,'country','country');    
	why += checkPhone(phone,'phone');
    why += checkEmail(document.getElementById("email").value,document.getElementById("cemail").value,'email','email');
	why += checkDropdown(document.getElementById("interest").value,'interest','interest');
	// why += checkDropdown(document.getElementById("primaryagegroup").value,'primaryagegroup','primary age group');
	why += checkDropdown(document.getElementById("howyouheard").value,'howyouheard','how did you hear about us');
	// why += checkDropdown(document.getElementById("timeline").value,'timeline','project timeline');
	// why += isEmpty(document.getElementById("comments").value,'comments','a comment');
	
    if (why != "") {
       //alert(why);
	   document.getElementById("errormessage").className='errormessage';
	   document.getElementById("errormessage").innerHTML=why;

       return false;
    }
	
	

return true;
}






// email

function checkEmail (strng, strng2, field) {
var error="";
if ((strng == "")||(strng != strng2)) {
   error = "Enter an email address and ensure it is the same as the confirm email.<br />";document.getElementById(field).className='error';
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.<br />";document.getElementById(field).className='error';
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.<br />";document.getElementById(field).className='error';
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.<br />";
	   	    document.getElementById('phone1').className='error';
   document.getElementById('phone2').className='error';
   document.getElementById('phone3').className='error';
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.<br />";
	   	    document.getElementById('phone1').className='error';
   document.getElementById('phone2').className='error';
   document.getElementById('phone3').className='error';
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.<br />";
	   	    document.getElementById('phone1').className='error';
   document.getElementById('phone2').className='error';
   document.getElementById('phone3').className='error';
    } 
		if(strng.length>0){
	//document.getElementById(field).value=parseInt(stripped);
	}
return error;
}

// fax number - strip out delimiters and check for 10 digits

function checkFax (strng,field) {
var error = "";
if (strng == "") {
   error = "You didn't enter a fax number.<br />";document.getElementById(field).className='error';
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The fax number contains illegal characters.<br />";
	   	 document.getElementById(field).className='error';
  
    }
    if (!(stripped.length == 10)) {
	error = "The fax number is the wrong length. Make sure you included an area code.<br />";document.getElementById(field).className='error';
    } 
		if(strng.length>0){
	document.getElementById(field).value=parseInt(stripped);
	}
return error;
}



// non-empty textbox

function isEmpty(strng,field,propername) {

var error = "";
  if (strng.length == 0) {
     error = "Please enter "+propername+".<br />"
	 document.getElementById(field).className='error';
	 
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.<br />";}
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.<br />";}
return error;
}

// valid selector from dropdown list

function checkDropdown(choice,field,propername) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the " + propername + " drop-down list.<br />";document.getElementById(field).className='error';
    }    
return error;
}    

// sin number - strip out delimiters and check for 10 digits



function isNumber (strng,field){
	var error = "";
	
if(isNaN(strng)){
	error = strng + " is not a numeric value that was expected.<br />";document.getElementById(field).className='error';
}
	return error;
}


function checkPostalCode(strng,field){
var error = "";	
var postalPattern = /^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$/i;
var postalRegExp = new RegExp(postalPattern);

var testResult = postalRegExp.test(strng);


if(!(testResult))
{
	
	error = "A properly formatted postal code was not entered.<br />";document.getElementById(field).className='error';
}	

if ((strng == "")||(strng == null))
{
	error = "A Postal Code was not entered.<br />";document.getElementById(field).className='error';

}

document.getElementById(field).value=strng.replace(/ /,"");
return error;
		
}




