// JavaScript Document
var errorcount =0;
var okforemail = /(@\w[-._\w]*\w\.\w{2,3})$/;
var okforphone = new RegExp("^((1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9)|(0)|(\\()|(\\))|(-)|(\\.)|(\\s))+$");
var okforurl = new RegExp("^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+");
var okfordouble = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
var isnumber =  /^\d+$/;


function CheckMailingForm(FormName){
	errorcount = 0;
	checkemail('email');
	submitform(FormName);
}
function SearchError(FormName){
	errorcount = 0;
	checktext('search');
	submitform(FormName);
}
function CheckCareersForm(FomID){
	errorcount = 0;
	checktext('contact_location');
	checktext('contact_fname');
	checktext('contact_lname');
	checktext('contact_role');
	checktext('contact_email');
	checkextension('contact_resume','.doc.rtf.htm.html.pdf');
	submitform(FomID);
}

function getfileextension(inputId) 
	{ 
	 var fileinput = document.getElementById(inputId);
	 if(!fileinput ) return ""; 
	 var filename = fileinput.value; 
	 if( filename.length == 0 ) return ""; 
	 var dot = filename.lastIndexOf("."); 
	 if( dot == -1 ) return ""; 
	 var extension = filename.substr(dot,filename.length); 
	 return extension; 
	} 

function checktext(form_field){
		if (document.getElementById(form_field).value == ''){
			errorcount += 1;
			document.getElementById(form_field).style.backgroundColor='#A22020';
		  }else{
			document.getElementById(form_field).style.backgroundColor='';
		}
	}
	
	
function checkextension(form_field,okext){
		if(document.getElementById(form_field).value != ""){
			var ext	 = getfileextension(form_field);
				if (ext == "") return;
				if (okext.toLowerCase().indexOf(ext) == -1){
					errorcount += 1;
				document.getElementById(form_field).style.backgroundColor='#A22020';
			  }else{
				document.getElementById(form_field).style.backgroundColor='';
			}	
		}else{
			document.getElementById(form_field).style.backgroundColor='';;
		}
		
	}

function checkemail(form_field) {
	if (!okforemail.test(document.getElementById(form_field).value)){
		errorcount += 1;
        document.getElementById(form_field).style.backgroundColor='#A22020';
	  }else{
	    document.getElementById(form_field).style.backgroundColor='';
	}
}

function CheckEstimateForm(Formname){
	errorcount = 0
	checktext('ContactName');
	checkemail('ContactEmail');
	submitform(Formname);
}

function checkphone(form_field){
	if ((document.getElementById(form_field).value == "") || (!okforphone.test(document.getElementById(form_field).value))){
		errorcount += 1;
        document.getElementById(form_field).style.backgroundColor='#A22020';
	  }else{
	    document.getElementById(form_field).style.backgroundColor='';
	}	
}


function CheckAskAQuestionForm(TheForm){
	errorcount = 0;
	checktext('aaq_name');
	checkemail('aaq_email');
	checktext('aaq_question');
	submitform(TheForm);
}

function CheckContactForm(FormName){
	errorcount = 0
	checktext('ContactName');
	checkemail('ContactEmail');
	submitform(FormName);
}
function CheckReportForm(Formid){
	errorcount = 0;
	checktext('Problem');
	submitform(Formid);
}


function submitform(form_name){
		if (errorcount > 0){
			alert('You have ' + errorcount + ' errors in your form. Please correct.');
			//set the count back to 0
			errorcount = 0;
		}else{
			document.getElementById(form_name).submit();
		}
	}
