/***************************** check the contact form ***************************/
var whitespace = " \t\n\r";
var digits = "0123456789";
var dom1=(document.getElementById)?1:0;
var ie4plus=(document.all)?1:0;
var ns4=(document.layers)?1:0;
var safari=(navigator.appVersion.indexOf("Safari") != -1)?1:0;
///////////////////////////////////////////////////////
// remove IE square border around check/radio buttons
function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    }
    else if (elm.attachEvent) {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    }
    else {
        elm['on' + evType] = fn;
    }
}
function removeCheckBoxBorders() {
    var el = document.getElementsByTagName("input");
    for (i = 0; i < el.length; i++) {
        var type = el[i].getAttribute("type");
        if ((type == "checkbox") || (type == "radio")) {
            el[i].style.border = "none";
        }
    }
}
addEvent(window, 'load', removeCheckBoxBorders, false);
///////////////////////////////////////////////////
//function to control active/inactive fields
function activeField(f) {
    var x = document.getElementById(f).style;
    x.background = '#ffffcc'
    x.border = 'solid 1px #000';
}

function inactiveField(f) {
    var x = document.getElementById(f).style;
    x.background = '#fff'
    x.border = 'solid 1px #999';
}
//////////////////////////////////////////////////
//Misc Form Functions
function getElementRef(id){
	if (dom1) return document.getElementById(id);
	else if (ie4plus) return document.all[id];
	else if (ns4) return document.layers[id];
	else return null;
}
function switchUI(typeui){
	if (dom1 || ie4plus) {
		switch (typeui) {
			case 'pb_it' : 
				getElementRef('pb_it').style.display='block';
				getElementRef('pb_ss').style.display='none';
				getElementRef('pb_psa').style.display='none';
				getElementRef('pb_other').style.display='none';
				// clear software, other, psa
				for (i=1; i <=6; i++) {
					eval ('document.form1.elements.whatBusiness_ss_' + i + '.checked=false;');
				}
				document.form1.whatBusiness_psa.value='';
				document.form1.whatBusiness_other.value='';	
				break;
			case 'pb_psa' :  
				getElementRef('pb_it').style.display='none';
				getElementRef('pb_ss').style.display='none';
				getElementRef('pb_psa').style.display='block';
				getElementRef('pb_other').style.display='none';
				// clear it, software, other
				for (i=1; i <=8; i++) {
					eval ('document.form1.elements.whatBusiness_it_' + i + '.checked=false;');
				}
				for (i=1; i <=6; i++) {
					eval ('document.form1.elements.whatBusiness_ss_' + i + '.checked=false;');
				}
				document.form1.whatBusiness_other.value='';
				break;
			case 'pb_ss' :  
				getElementRef('pb_it').style.display='none';
				getElementRef('pb_ss').style.display='block';
				getElementRef('pb_psa').style.display='none';
				getElementRef('pb_other').style.display='none';
				// clear it, other, psa	
				for (i=1; i <=8; i++) {
					eval ('document.form1.elements.whatBusiness_it_' + i + '.checked=false;');
				}
				document.form1.whatBusiness_psa.value='';
				document.form1.whatBusiness_other.value='';
				break;
			case 'pb_other' :  
				getElementRef('pb_it').style.display='none';
				getElementRef('pb_ss').style.display='none';
				getElementRef('pb_psa').style.display='none';
				getElementRef('pb_other').style.display='block';
				// clear it, software, psa
				for (i=1; i <=8; i++) {
					eval ('document.form1.elements.whatBusiness_it_' + i + '.checked=false;');
				}
				for (i=1; i <=6; i++) {
					eval ('document.form1.elements.whatBusiness_ss_' + i + '.checked=false;');
				}
				document.form1.whatBusiness_psa.value='';
				break;
		}
	}
}
function isEmpty(s) {   
	return ((s == null) || (s.length == 0))
}
function isEmptyOrWhitespace (s) {   
	var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function isRadioSelected(radiofield) {   
	for (var i = 0; i < radiofield.length; i++) {   
		if (radiofield[i].checked) return true
    }
    return false
}
function isValidPhoneNumber(s) {
	var temp = s.replace(/\D/g, "")
	return temp.length > 9 && temp.length < 26
}

function checkHeardOther(eValue, v) {
    if (eValue == "Other") {
        getElementRef('heardOther').style.display = 'block';
        document.form1.hOther.focus();
    } else {
        document.form1.hOther.value = '';
        getElementRef('heardOther').style.display = 'none';
    }
}
function checkPhone(f, element) {
    //alert (element.name);
    if ( isEmptyOrWhitespace(f.txtPhone.value) && f.chkContactMe.checked && element.name == "chkContactMe" ) {
        alert("Thanks for your interest. We'll have a sales rep contact you right way. Be sure you have provided the best phone number to reach you. Please complete the phone field so we can contact you.");
        f.txtPhone.focus();   
    }
    if ( isEmptyOrWhitespace(f.txtPhone.value) && f.chkDemoMe.checked && element.name == "chkDemoMe" ) {
        alert("We'd love to schedule your live product demo, but we need the best phone number to reach you. Please complete the phone field so we can schedule your demo.");
        f.txtPhone.focus();   
    }
    if ( isEmptyOrWhitespace(f.txtPhone.value) && f.chkReady.checked && element.name == "chkReady" ) {
        alert("We're thrilled that you are ready to buy right now. The best way to move forward to the next step is to have one of our product specialists to call you, but we need the best phone number to reach you. Please complete the phone field so we can provide you with the best service.");
        f.txtPhone.focus();   
    }
}
//////////////////////////////////////////////////
// form validation
function checkForm(f) {
    
	// primary business
	if ( getCheckedValue(f.primaryBusiness) == '') {
		alert("Please select your primary business area.");
		f.pb1.focus();
		return false;
	}
	// Check Type of Business by Primary business
	// check primary business  - IT (select at least one)
	if ( getCheckedValue(f.primaryBusiness) == 'IT Services') {
		var checked; checked=false;
		var radio;
		for (i=1; i <=8; i++) {
			radio = eval('f.whatBusiness_it_' + i);
			if ( radio.checked ) {
				checked = true;      
			}
		}
		if ( !checked ) {
			alert("Please select at least one Business Type.");
			f.pt1.focus();
			return false;
		}
	}
	// check primary business  - Software (select at least one)
	if ( getCheckedValue(f.primaryBusiness) == 'Software Services') {
		var checked; checked=false;
		var radio;
		for (i=1; i <=6; i++) {
			radio = eval('f.whatBusiness_ss_' + i);
			if ( radio.checked ) {
				checked = true;      
			}
		}
		if ( !checked ) {
			alert("Please select at least one Business Type.");
			f.pt9.focus();
			return false;
		}
	}
	// check primary business  - Consulting
	if ( getCheckedValue(f.primaryBusiness) == 'Consulting or Professional Services' && isEmptyOrWhitespace(f.whatBusiness_psa.value) ) {
		alert("Please provide your Type of Business.");
		f.whatBusiness_psa.focus();
		return false;
	}
	// check primary business  - Other
	if ( getCheckedValue(f.primaryBusiness) == 'Other Specialized Services' && isEmptyOrWhitespace(f.whatBusiness_other.value) ) {
		alert("Please provide your Type of Business.");
		f.whatBusiness_other.focus();
		return false;
	}
	// check "how heard about" sub-category
	// check if heard about value is in subCats list (defined in lead_form_heard_about.js)
	var buildHeardAbout='';
	if (!subCats.match(f.heard.value)) {
	    //no subcat
	    if (f.heard.value == "Other" && isEmptyOrWhitespace(f.hOther.value)) {
	        alert("Please enter how you heard about us.");
	        f.hOther.focus();
	        return false;
	    } else {
	        if (f.heard.value == "Other" && !isEmptyOrWhitespace(f.hOther.value)) {
	            buildHeardAbout = f.heard.value + ' - ' + f.hOther.value
	        } else {
	            buildHeardAbout = f.heard.value
	        }
	    }
	} else {
	// subcat
	    // is subcat empty?
	    if (f.subcats.value == "") {
	        alert("Please select a sub-category.");
	        f.subcats.focus();
	        return false;
	    } else {
	        // subcat
	        buildHeardAbout = f.heard.value + ' - ' + f.subcats.value;
	    }
	    // is subcat other and other-text empty?
	    if ( (f.heard.value =="Other" || f.subcats.value == "Other") && isEmptyOrWhitespace(f.hOther.value)) {
	        alert("Please enter how you heard about us.");
	        f.hOther.focus();
	        return false;
	    } else {
	        buildHeardAbout = f.heard.value + ' - ' + f.subcats.value;
	    }
	    if (f.subcats.value == "Other" && !isEmptyOrWhitespace(f.hOther.value)) {
	        buildHeardAbout = f.heard.value + ' - ' + f.subcats.value + ' - ' + f.hOther.value;
	    }
	}
	f.txtHowDidYouHearAboutAutotask.value = buildHeardAbout;
	//testing
	//alert(buildHeardAbout);
	//return false;  

	//check the first name
	if ( isEmptyOrWhitespace(f.txtFirstName.value) ) {
		alert("Please provide your First Name in the space provided.");
		f.txtFirstName.focus();
		return false;
	}
	//check the last name
	if ( isEmptyOrWhitespace(f.txtLastName.value) ) {
		alert("Please provide your Last Name in the space provided.");
		f.txtLastName.focus();
		return false;
	}
	//check the email address
	if (f.txtEmail.value == "" || f.txtEmail.value == "null" || f.txtEmail.value == "NULL") {
		alert("Please provide your E-Mail Address in the space provided.");
		f.txtEmail.value = ""; 
		f.txtEmail.focus();
		return false;
	} else if (f.txtEmail.value.indexOf("@") == -1 || f.txtEmail.value.indexOf(".") == -1){
		alert("Please provide a valid E-Mail Address in the space provided.");
		f.txtEmail.value = "";
		f.txtEmail.focus();
		return false;
	}
	//check the company name
	if ( isEmptyOrWhitespace(f.txtCompany.value) ) {
		alert("Please provide your Company Name in the space provided.");
		f.txtCompany.focus();
		return false;
	}
	//check the country
	if (f.Country.value == "Select One") {
		alert("Please select your Country.");
		f.Country.focus();
		return false;
	}
	//check number of employees
	if (f.NumberOfEmployees.value == "Unknown") {
		alert("Please select the Size of Your Company.");
		f.NumberOfEmployees.focus();
		return false;
	}
	//check number of users
//	if (f.NumberOfUsers.value == "") {
//		alert("Please select the Potential Number of Users.");
//		f.NumberOfUsers.focus();
//		return false;
//	}
	// check at least 1 requested action
	if ( !f.chkContactMe.checked && !f.chkDemoMe.checked && !f.chkInfoMe.checked && !f.chkReady.checked) {
		alert("Please select at least one Requested Action.");
		f.chkContactMe.focus();
		return false;
	}
	//check purchase timeframe
	if (f.txtPurchaseTimeframe.value == "") {
		alert("Please select a Timeframe for Purchase.");
		f.txtPurchaseTimeframe.focus();
		return false;
	}
	//return false; // for debugging only
	//return true;
}
