// JavaScript Document



function setFocus(){
document.getElementById("contactname").focus();}

function validateFormOnSubmit(theForm) {
	
	
var Emptyfield = '';


for(i=0;i<theForm.length;i++){ // this checks all elements on the form. even invisible ones. plus ones added in the future.
	
	
	if(theForm[i].value == ''
			   
			&&  theForm[i].id != 'contactname'   // id you add fields ID in the same format as this lines those will be ignored and not mandatory. all others are required fields.
			&&  theForm[i].id != 'contactmessage' 			   
			   
			   ){
		
		theForm[i].style.backgroundColor = '#FFFF66';
		
		++Emptyfield;
		
		} else {
			
		theForm[i].style.backgroundColor = '';

			}

}

if(Emptyfield>0){alert('Please complete the yellow fields. Other fields are Optional.'); return false; }


	
				switch(trim(theForm.contactemail.value)){
				
						case '' : 
						
							theForm.contactemail.style.backgroundColor = '#FFFF66'; //light yellow for empty fields
							alert('e-mail field is empty.'); 
							return false; break;
					
					
					
						default:	
						
							if(!validateEmailContactUs(theForm.contactemail)) { 
							
							theForm.contactemail.style.backgroundColor = '#FF5E5E';	 //light red for invalid emails
							alert('Please check the e-mail address for typos or invalid characters, for example: \n\n'
								  + '                       \' ; , "  % ^ & * ! # $ < > ( ) ');
							
							return false; }; break;
				
				}

}



function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 



function validateEmailContactUs(fld) {
	
	
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;   // this is a very poor email checker :(  but good ones take a lot of work!!! ;)
    var illegalChars= /[\!\*\&\%\$\#\(\)\<\>\,\;\:\\\"\[\]]/ ;  //added some more illegal characters

	
    if (fld.value == "") {
		
					fld.style.backgroundColor = '#FFFF66'; //light yellow for empty fields
					//alert('case 1');
					return false;
							
							
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
				
							fld.style.backgroundColor = '#FFFF66'; //light yellow for empty fields
 
					//alert('case 2');
					return false;
		
    } else if (fld.value.match(illegalChars)) {
		
							fld.style.backgroundColor = '#FFFF66'; //light yellow for empty fields
					//alert('case 3');
					return false;
							
		
		
		
    } else {
				//alert('case 4');			
				fld.style.backgroundColor = ''; //light yellow for empty fields
				return true;
    }


}
