// 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 CheckContactSmallForm(FormName){
	errorcount = 0
	checktext('ContactName');
	checkemail('ContactEmail');
    checktext('Phone');
	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();
	}
}

function ValidateContactForm(form_name) {
    var msg = "";
    document.forms[form_name]["ContactName"].style.backgroundColor = '';
    document.forms[form_name]["ContactEmail"].style.backgroundColor = '';

    if (document.forms[form_name]["ContactName"].value == "") {
        msg = "Please Enter Name \n";
        document.forms[form_name]["ContactName"].style.backgroundColor = '#A22020';
    }

    if (document.forms[form_name]["ContactEmail"].value == "") {
        msg = msg + "Please Enter Email Address \n";
        document.forms[form_name]["ContactEmail"].style.backgroundColor = '#A22020';
    }
    else {
        var reg = /(@\w[-._\w]*\w\.\w{2,3})$/;
        if (reg.test(document.forms[form_name]["ContactEmail"].value) == false) {
            msg = msg + "Please Enter Valid Email Address \n";
            document.forms[form_name]["ContactEmail"].style.backgroundColor = '#A22020';
        }
    }

    if (msg != "") {
        alert(msg);
        return false;
    }
    else {
        document.getElementById(form_name).submit();
    }
    return true;
}

function checkEstimateForm(form_name) {
    var msg = "";

    document.forms[form_name]["ContactName"].style.backgroundColor = '';
    document.forms[form_name]["ContactEmail"].style.backgroundColor = '';
    document.forms[form_name]["Phone"].style.backgroundColor = '';

    if (document.forms[form_name]["ContactName"].value == "") {
        msg = "Please Enter Name \n";
        document.forms[form_name]["ContactName"].style.backgroundColor = '#A22020';
    }

    if (document.forms[form_name]["ContactEmail"].value == "") {
        msg = msg + "Please Enter Email Address \n";
        document.forms[form_name]["ContactEmail"].style.backgroundColor = '#A22020';
    }
    else {        
        if (!okforemail.test(document.forms[form_name]["ContactEmail"].value)) {
            msg = msg + "Please Enter Valid Email Address \n";
            document.forms[form_name]["ContactEmail"].style.backgroundColor = '#A22020';
        }
    }

    if (document.forms[form_name]["Phone"].value == "") {
        msg = msg + "Please Enter Phone Number \n";
        document.forms[form_name]["Phone"].style.backgroundColor = '#A22020';
    }
    else {
        if ((!okforphone.test(document.forms[form_name]["Phone"].value))) {
            msg = msg + "Please Enter Valid Phone Number \n";
            document.forms[form_name]["Phone"].style.backgroundColor = '#A22020';
        }
    }

    if (msg != "") {
        alert(msg);
        return false;
    }
    else {
        document.getElementById(form_name).submit();
    }
    return true;
}

function validateCareerForm(form_name) {
    var msg = "";
    var validExtension = ".doc.rtf.htm.html.pdf";
    document.forms[form_name]["contact_location"].style.backgroundColor = '';
    document.forms[form_name]["contact_fname"].style.backgroundColor = '';
    document.forms[form_name]["contact_lname"].style.backgroundColor = '';
    document.forms[form_name]["contact_role"].style.backgroundColor = '';
    document.forms[form_name]["contact_email"].style.backgroundColor = '';
    document.forms[form_name]["contact_resume"].style.backgroundColor = '';

    if (document.forms[form_name]["contact_location"].value == "") {
        msg = "Please Enter Location \n";
        document.forms[form_name]["contact_location"].style.backgroundColor = '#A22020';
    }

    if (document.forms[form_name]["contact_fname"].value == "") {
        msg = msg + "Please Enter First Name \n";
        document.forms[form_name]["contact_fname"].style.backgroundColor = '#A22020';
    }

    if (document.forms[form_name]["contact_lname"].value == "") {
        msg = msg + "Please Enter Last Name \n";
        document.forms[form_name]["contact_lname"].style.backgroundColor = '#A22020';
    }

    if (document.forms[form_name]["contact_role"].value == "") {
        msg = msg + "Please Enter Role \n";
        document.forms[form_name]["contact_role"].style.backgroundColor = '#A22020';
    }

    if (document.forms[form_name]["contact_email"].value == "") {
        msg = msg + "Please Enter Email Address \n";
        document.forms[form_name]["contact_email"].style.backgroundColor = '#A22020';
    }
    else {
        if (!okforemail.test(document.forms[form_name]["contact_email"].value)) {
            msg = msg + "Please Enter Valid Email Address \n";
            document.forms[form_name]["contact_email"].style.backgroundColor = '#A22020';
        }
    }

    if (document.forms[form_name]["contact_resume"].value != "") {
        var ext = getfileextension("contact_resume");
        if (ext == "") return;
        if (validExtension.toLowerCase().indexOf(ext) == -1) {
            msg = msg + "We accept .doc, .rtf, .htm, .html, .pdf extenstions only \n";
            document.forms[form_name]["contact_resume"].style.backgroundColor = '#A22020';
        } else {
            document.forms[form_name]["contact_resume"].style.backgroundColor = '';
        }
    } else {
        document.forms[form_name]["contact_resume"].style.backgroundColor = ''; ;
    }

    if (msg != "") {
        alert(msg);
        return false;
    }
    else {
        document.getElementById(form_name).submit();
    }
    return true;
}

function validateNewsLetter(form_name) {
    var msg = "";
    document.forms[form_name]["email"].style.backgroundColor = '';
    if (document.forms[form_name]["email"].value == "") {
        msg = msg + "Please Enter Email Address \n";
        document.forms[form_name]["email"].style.backgroundColor = '#A22020';
    }
    else {
        if (!okforemail.test(document.forms[form_name]["email"].value)) {
            msg = msg + "Please Enter Valid Email Address \n";
            document.forms[form_name]["email"].style.backgroundColor = '#A22020';
        }
    }

    if (msg != "") {
        alert(msg);
        return false;
    }
    else {
        document.getElementById(form_name).submit();
    }
    return true;
}

function ValidateReportForm(form_name) {
    var msg = "";
    document.forms[form_name]["Problem"].style.backgroundColor = '';

    if (document.forms[form_name]["Problem"].value == "") {
        msg = "Please Enter Problem Details \n";
        document.forms[form_name]["Problem"].style.backgroundColor = '#A22020';
    }

    if (msg != "") {
        alert(msg);
        return false;
    }
    else {
        document.getElementById(form_name).submit();
    }
    return true;
}

function ValidateSurveyForm(form_name) {
    var msg = "";

    document.forms[form_name]["company"].style.backgroundColor = '';
    document.forms[form_name]["contact"].style.backgroundColor = '';
    document.forms[form_name]["phone"].style.backgroundColor = '';

    if (document.forms[form_name]["company"].value == "") {
        msg = "Please Enter Company Name \n";
        document.forms[form_name]["company"].style.backgroundColor = '#A22020';
    }

    if (document.forms[form_name]["contact"].value == "") {
        msg = msg + "Please Enter Contact \n";
        document.forms[form_name]["contact"].style.backgroundColor = '#A22020';
    }    

    if (document.forms[form_name]["phone"].value == "") {
        msg = msg + "Please Enter Phone Number \n";
        document.forms[form_name]["phone"].style.backgroundColor = '#A22020';
    }
    else {
        if ((!okforphone.test(document.forms[form_name]["phone"].value))) {
            msg = msg + "Please Enter Valid Phone Number \n";
            document.forms[form_name]["phone"].style.backgroundColor = '#A22020';
        }
    }

    if (msg != "") {
        alert(msg);
        document.forms[form_name]["company"].focus();
        return false;
    }
    else {
        document.getElementById(form_name).submit();
    }
    return true;
}
