/***************************** 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 checkCountry(country) {
    if (country !== 'United States' && country !== 'Canada' && country != 'Select One') {
        window.open("/international.htm", "international", "height=300,width=600,resizable,scrollbars=yes")
    } else {
        document.getElementById('stateReq').style.display = 'inline';
    }
}
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 showMore(eValue, yValue, fField, fList, fListCount) {
    if (eValue == yValue) {
        document.getElementById(fField).style.display = 'block';
    } else {
        document.getElementById(fField).style.display = 'none';
        for (i = 1; i <= fListCount; i++) {
            eval('document.form1.elements.' + fList + i + '.checked=false;');
        }
        j = eval('document.form1.' + fList + fListCount + "_o");
        j.value = "";
    }
}
function checkOther(f, container, field) {
    //alert(document.getElementById(f).checked);
    if (document.getElementById(f).checked) {
        document.getElementById(container).style.display = "block";
        document.getElementById(field).focus();
    } else {
        document.getElementById(container).style.display = "none";
        document.getElementById(field).value = "";
    }
}
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) {
    var fGood="true"
	// primary business
    if (getCheckedValue(f.primaryBusiness) == '') {
        alert("Please select your primary business area.");
        f.pb1.focus();
        fGood = false;
        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();
			fGood = false;
			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();
			fGood = false;
			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();
		fGood = false;
		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();
		fGood = false;
		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();
	        fGood = false;
	        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();
	        fGood = false;
	        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;
	        fGood = 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();
		fGood = false;
		return false;
	}
	//check the last name
	if ( isEmptyOrWhitespace(f.txtLastName.value) ) {
		alert("Please provide your Last Name in the space provided.");
		f.txtLastName.focus();
		fGood = false;
		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();
		fGood = false;
		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();
		fGood = false;
		return false;
	}
	//check the company name
	if ( isEmptyOrWhitespace(f.txtCompany.value) ) {
		alert("Please provide your Company Name in the space provided.");
		f.txtCompany.focus();
		fGood = false;
		return false;
	}
	//check the country
	if (f.Country.value == "Select One") {
	    alert("Please select your Country.");
	    f.Country.focus();
	    fGood = false;
	    return false;
	}
	if (f.Country.value == "United States" && (f.txtState.value == "" || f.txtState.value == "NA") ) {
		alert("You selected your country as \"United States,\" please select a state.");
		f.txtState.focus();
		fGood = false;
		return false;
    }
    if (f.Country.value == "Canada" && (f.txtState.value == "" || f.txtState.value == "NA")) {
        alert("You selected your country as \"Canada,\" please select a province.");
        f.txtState.focus();
        fGood = false;
        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();
        fGood = false;
        return false;
    }
	//check number of employees
	if (f.NumberOfEmployees.value == "Unknown") {
		alert("Please select the Size of Your Company.");
		f.NumberOfEmployees.focus();
		fGood = false;
		return false;
	}
	//check purchase timeframe
	if (f.txtPurchaseTimeframe.value == "") {
		alert("Please select a Timeframe for Purchase.");
		f.txtPurchaseTimeframe.focus();
		fGood = false;
		return false;
	}
	// checked Quoting/eCommerce
	finalValue = "";
	checked = false;
	quoteDetails = "";
	if (getCheckedValue(f.useQuote) == "Yes") {
	    for (i = 1; i <= 5; i++) {
	        radio = eval('f.qu' + i);
	        if (radio.checked) {
	            checked = true;
	            if (quoteDetails.length > 0) { quoteDetails += "; " }
	            quoteDetails += radio.value;
	        }
	    }
	}
    if (getCheckedValue(f.useQuote) == "Yes" && !checked) {
        alert('Please select at least one Quoting or eCommerce Tool.');
        f.qu1.focus();
        fGood = false;
        return false;
    } else {
        if (f.qu5.checked && isEmptyOrWhitespace(f.qu5_o.value)) {
            alert('Please provide a value for \"Quoting/eCommerce Tool - Other\".');
            f.qu5_o.focus();
            fGood = false;
            return false;
        } else {
            if (f.qu5.checked && f.qu5_o.value.length>0) { quoteDetails += " - " + f.qu5_o.value; }
        }
    }
    f.quoteValue.value = quoteDetails;
    
    // checked RMM
    finalValue = "";
    checked = false;
    rmmDetails = "";
    if (getCheckedValue(f.useRMM) == "Yes") {
        for (i = 1; i <= 6; i++) {
        radio = eval('f.rmm' + i);
        if (radio.checked) {
            checked = true;
            if (rmmDetails.length > 0) { rmmDetails += "; " }
            rmmDetails += radio.value;
        }
    }
    }
    if (getCheckedValue(f.useRMM) == "Yes" && !checked) {
        alert('Please select at least one RMM Tool.');
        f.rmm1.focus();
        fGood = false;
        return false;
    } else {
        if (f.rmm6.checked && isEmptyOrWhitespace(f.rmm6_o.value)) {
            alert('Please provide a value for \"Remote Monitoring and Management - Other\".');
            f.rmm6_o.focus();
            fGood = false;
            return false;
        } else {
            if (f.rmm6.checked && f.rmm6_o.value.length > 0) { rmmDetails += " - " + f.rmm6_o.value; }
        }
    }
    f.rmmValue.value = rmmDetails;
    // checked accounting package
    finalValue = "";
    checked = false;
    accDetails = "";
    if (getCheckedValue(f.useAcc) =="Yes") {
        for (i = 1; i <= 5; i++) {
            radio = eval('f.acc' + i);
            if (radio.checked) {
                checked = true;
                if (accDetails.length > 0) { accDetails += "; " }
                accDetails += radio.value;
                if (radio.checked && i == 1 && isEmptyOrWhitespace(f.acc1_o.value)) {
                    alert('Please your enter your QuickBooks version.');
                    f.acc1_o.focus();
                    fGood = false;
                    return false;
                } else {
                    if (i == 1 && !isEmptyOrWhitespace(f.acc1_o.value)) { accDetails += ' ' + f.acc1_o.value; }
                }
            }
        }
    }
    if (getCheckedValue(f.useAcc) == "Yes" && !checked) {
        alert('Please select at least one Accounting Package.');
        f.acc1.focus();
        fGood = false;
        return false;
    } else {
        if (f.acc5.checked && isEmptyOrWhitespace(f.acc5_o.value)) {
            alert('Please provide a value for \"Accounting Package - Other\".');
            f.acc5_o.focus();
            fGood = false;
            return false;
        } else {
        if (f.acc5.checked && f.acc5_o.value.length > 0) { accDetails += " - " + f.acc5_o.value; }
        }
    }
    f.accountingValue.value = accDetails;
    // add misc data into comments
//    if (fGood) {
//        if (quoteDetails.length < 2) { quoteDetails = "none selected"; }
//        if (rmmDetails.length < 2) { rmmDetails = "none selected"; }
//        if (accDetails.length < 2) { accDetails = "none selected"; } 
//        f.Comments.value += "\n\nQuoting/eCommerce Packages used: " + quoteDetails;
//        f.Comments.value += "\n\nRMM tools used: " + rmmDetails;
//        f.Comments.value += "\n\nAccount Packages used: " + accDetails;
//        //alert(f.Comments.value);
//    }
	
	//return false; // for debugging only
	return true;
}