function isValidEmailAddress(e) {
    var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    for(var i=0; i < e.length; i++) {
        if(ok.indexOf(e.charAt(i))<0){
            return (false);
        }
    }
    var re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    var re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!e.match(re) && e.match(re_two)) {
        return (true);
    }

    return false;
}

function isNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
           IsNumber = false;
        }
    }
    return IsNumber;
}

function isLength(sText, sLength) {
    var IsLength = true;
    if (sText.length < sLength) {
        return IsLength = false;
    }
    return IsLength;
}


function checkPhone(strng){
	var isPhone = true;
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	   isPhone = false;
	}

	if (!(stripped.length == 7)) {
		isPhone = false;
	}

	return isPhone;
}


function isEmpty(field) {
    return typeof(field) == undefined || field == null || field.value.length == 0 || field.value == '';
}

function xor(a,b) {
  return ( a || b ) && !( a && b );
}

function sort(name) {
    $('#newSearch').val('false');
    if (name == $('#sort').val()) {
        if ('true' == $('#ascending').val()) {
            $('#ascending').val('false');
        } else {
            $('#ascending').val('true');
        }
    } else {
        $('#sort').val(name);
        $('#ascending').val('true');
    }
    $('#searchForm').trigger('submit');
}

function page(pos) {
    $('#position').val(pos);
    $('#newSearch').val('false');
    $('#searchForm').trigger('submit');
}

function gotoPage(nextPage) {

    document.getElementById('goto').value = nextPage;
    var form = document.getElementById('mainForm');
    if (form.onsubmit) {
        if (!form.onsubmit()) {
            return;
        }
    }
    if (nextPage == 5) {
        if (document.getElementById("btn_buy_now")) {
            document.getElementById("btn_buy_now").innerHTML='Processing...';
             document.getElementById('btn_buy_now').className = 'btn_wide_white_disabled';
        }
    }
    form.submit();
}

function navigateAuto() {
    var make = document.getElementById('makeSelection').value;
    var model = document.getElementById('modelSelection').value;

    var url = '/warranty';

    if (make) {
        url += '/' + encodeURI(make);
        if (model) {
            url += '/' + encodeURI(model);
        }
    }

    window.location = url;
}

function submitCallMeForm() {
	var form = document.getElementById('callMeForm');
	if(validateCallMeForm(form)) {
		form.submit();
	}
}

function validateCallMeForm(form) {
    try {
		var returnVal = true;
        var email = false;
        var phone = false;

        form.firstName.style.border = '';
        if (isEmpty(form.firstName)) {
            document.getElementById('firstName').style.border = 'red solid 2px';
			returnVal = false;
        }

        form.lastName.style.border = '';
        if (isEmpty(form.lastName)) {
            document.getElementById('lastName').style.border = 'red solid 2px';
			returnVal = false;
        }

        form.email.style.border = '';
        if (isEmpty(form.email)) {
            document.getElementById('email').style.border = 'red solid 2px';
            email = false;
        } else if (!isValidEmailAddress(form.email.value)) {
            document.getElementById('email').style.border = 'red solid 2px';
			returnVal = false;
        } else {
            email = true;
        }

        document.getElementById('phone1').style.border = '';
        document.getElementById('phone2').style.border = '';
        document.getElementById('phone3').style.border = '';
        if (isEmpty(form.phone1) || isEmpty(form.phone2) || isEmpty(form.phone3)) {
             document.getElementById('phone1').style.border = 'red solid 2px';
             document.getElementById('phone2').style.border = 'red solid 2px';
             document.getElementById('phone3').style.border = 'red solid 2px';
            phone = false;
        } else if (!isNumeric(form.phone1.value) || !isNumeric(form.phone2.value) || !isNumeric(form.phone3.value)) {
             document.getElementById('phone1').style.border = 'red solid 2px';
             document.getElementById('phone2').style.border = 'red solid 2px';
             document.getElementById('phone3').style.border = 'red solid 2px';
			returnVal = false;
        } else {
            phone = true;
        }

        if (returnVal && !email && !phone) {
			returnVal = false;
            document.getElementById('errorMsg2').style.display = 'block';
            document.getElementById('errorMsg').style.display = 'none';
        }
        else if (!returnVal) {
            document.getElementById('errorMsg').style.display = 'block';
            document.getElementById('errorMsg2').style.display = 'none';            
        }
    } catch(e) {
        alert(e);
    }
    return returnVal;
}

function submitQuoteForm() {
    var form = document.getElementById('quoteForm');
    if(validateAndSubmitQuote(form)) {
        document.getElementById('quoteForm').submit();
    }
}

function validateAndSubmitQuote(form) {
    //var error = false;
    var returnVal = true;
    try {
        if (isEmpty(form.year)) {
            document.getElementById('yearError').style.display = '';
            document.getElementById('yearError').innerHTML = 'Please provide a year';
            //error = true;
            returnVal = false;
        } else {
            document.getElementById('yearError').style.display = 'none';
        }

        if (isEmpty(form.make)) {
            document.getElementById('makeError').style.display = '';
            document.getElementById('makeError').innerHTML = 'Please provide a make';
            //error = true;
            returnVal = false;
        } else {
            document.getElementById('makeError').style.display = 'none';
        }

        if (isEmpty(form.model)) {
            document.getElementById('modelError').style.display = '';
            document.getElementById('modelError').innerHTML = 'Please provide a model';
            //error = true;
            returnVal = false;
        } else {
            document.getElementById('modelError').style.display = 'none';
        }

        if (isEmpty(form.cylinders)) {
            document.getElementById('cylindersError').style.display = '';
            document.getElementById('cylindersError').innerHTML = 'Please provide cylinders';
            //error = true
            returnVal = false;
        } else {
            document.getElementById('cylindersError').style.display = 'none';
        }

        if (isEmpty(document.getElementById('mileage'))) {
            document.getElementById('mileageError').style.display = '';
            document.getElementById('mileageError').innerHTML = 'Please provide your mileage';
            //error = true;
            returnVal = false;
        } else if (form.mileage.value == '') {
            document.getElementById('mileageError').style.display = '';
            document.getElementById('mileageError').innerHTML = 'Please provide your mileage';
            //error = true;
            returnVal = false;
        } else if (!isNumeric(form.mileage.value)) {
            document.getElementById('mileageError').style.display = '';
            document.getElementById('mileageError').innerHTML = 'Mileage must be numeric';
            //error = true;
            returnVal = false;
        } else if (	form.mileage.value > 100000) {
            document.getElementById('mileageError').style.display = '';
            document.getElementById('mileageError').innerHTML = 'Mileage must be less than 100k';
            //error = true;
            returnVal = false;
        } else {
            document.getElementById('mileageError').style.display = 'none';
        }

        if (typeof(form.turbo) == undefined || form.turbo == null) {
            var turbo = document.createElement('INPUT');
            turbo.name = 'turbo';
            turbo.type = 'checbox';
            turbo.value = 'false';
            turbo.appendChild(form);
        }

        if (typeof(form.diesel) == undefined || form.diesel == null) {
            var diesel = document.createElement('INPUT');
            diesel.name = 'diesel';
            diesel.type = 'checbox';
            diesel.value = 'false';
            diesel.appendChild(form);
        }

        if (typeof(form.fwd) == undefined || form.fwd == null) {
            var fwd = document.createElement('INPUT');
            fwd.name = 'fwd';
            fwd.type = 'checbox';
            fwd.value = 'false';
            fwd.appendChild(form);
        }

        if (typeof(form.awd) == undefined || form.awd == null) {
            var awd = document.createElement('INPUT');
            awd.name = 'awd';
            awd.type = 'checbox';
            awd.value = 'false';
            awd.appendChild(form);
        }

        if (form.stateProvince.value == '') {
            document.getElementById('stateError').style.display = '';
            //error = true;
            returnVal = false;
        } else {
            document.getElementById('stateError').style.display = 'none';
        }

    } catch(e) {
        alert(e);
    }

    if(returnVal) {
        document.cookie = ('showEntryForm=false');
        document.cookie = ('showCallMeForm=true');
        document.cookie = ('currentQuoteId=;');
    }

    return returnVal;

}

function initializeMakesSelect(defMake, defModel) {
    var makeSel = 'makeSelect';
    var modelSel = 'modelSelect';
    var makeSelect = document.getElementById(makeSel);
    var modelSelect = document.getElementById(modelSel);
    makeSelect.options.length = 0;
    var selectedIndex = 0;
    for (var i = 0; i < warrantyVehicleTypeMakes.length; i++) {
        if (warrantyVehicleTypeMakes[i] == defMake) {
            selectedIndex = makeSelect.options.length;
        }
        var option = new Option();
        option.value = warrantyVehicleTypeMakes[i];
        option.text = warrantyVehicleTypeMakes[i];
        makeSelect.options[makeSelect.options.length] = option;
    }
    makeSelect.selectedIndex = selectedIndex;

    var make = document.getElementById(makeSel).value;
    modelSelect.options.length = 0;
    selectedIndex = 0;
    for (i = 0; i < warrantyVehicleTypes.length; i++) {
        if (warrantyVehicleTypes[i].make == make) {
            if (warrantyVehicleTypes[i].model.toLowerCase() == defModel.toLowerCase()) {
                selectedIndex = modelSelect.options.length;
            }
            option = new Option();
            option.value = warrantyVehicleTypes[i].model;
            option.text = warrantyVehicleTypes[i].model;
            modelSelect.options[modelSelect.options.length] = option;
        }
    }
    modelSelect.selectedIndex = selectedIndex;
}

function updateMake(makeSel,modelSel) {
    if(makeSel == undefined) {
        makeSel = 'makeSelect';
    }
    if(modelSel == undefined) {
        modelSel = 'modelSelect';
    }
    var make = document.getElementById(makeSel).value;
    var modelSelect = document.getElementById(modelSel);
    modelSelect.options.length = 0;
    for (var i = 0; i < warrantyVehicleTypes.length; i++) {
        if (warrantyVehicleTypes[i].make == make) {
            var option = new Option();
            option.value = warrantyVehicleTypes[i].model;
            option.text = warrantyVehicleTypes[i].model;
            modelSelect.options[modelSelect.options.length] = option;
        }
    }
}

function initializeDisplayMakesSelect(defMake, defModel) {
    var makeSel = 'makeSelection';
    var modelSel = 'modelSelection';
    var makeSelect = document.getElementById(makeSel);
    var modelSelect = document.getElementById(modelSel);
    makeSelect.options.length = 0;
    var selectedIndex = 0;
    for (var i = 0; i < displayTypeMakes.length; i++) {
        if (displayTypeMakes[i] == defMake) {
            selectedIndex = makeSelect.options.length;
        }
        var option = new Option();
        option.value = displayTypeMakes[i];
        option.text = displayTypeMakes[i];
        makeSelect.options[makeSelect.options.length] = option;
    }
    makeSelect.selectedIndex = selectedIndex;

    var make = document.getElementById(makeSel).value;
    modelSelect.options.length = 0;
    selectedIndex = 0;
    for (i = 0; i < displayTypes.length; i++) {
        if (displayTypes[i].make == make) {
            if (displayTypes[i].model.toLowerCase() == defModel.toLowerCase()) {
                selectedIndex = modelSelect.options.length;
            }
            option = new Option();
            option.value = displayTypes[i].model;
            option.text = displayTypes[i].model;
            modelSelect.options[modelSelect.options.length] = option;
        }
    }
    modelSelect.selectedIndex = selectedIndex;
}

function updateDisplayMake(makeSel,modelSel) {
    if(makeSel == undefined) {
        makeSel = 'makeSelect';
    }
    if(modelSel == undefined) {
        modelSel = 'modelSelect';
    }
    var make = document.getElementById(makeSel).value;
    var modelSelect = document.getElementById(modelSel);
    modelSelect.options.length = 0;
    for (var i = 0; i < displayTypes.length; i++) {
        if (displayTypes[i].make == make) {
            var option = new Option();
            option.value = displayTypes[i].model;
            option.text = displayTypes[i].model;
            modelSelect.options[modelSelect.options.length] = option;
        }
    }
}

function validateStep1Form() {
    if (document.getElementById('goto').value != 2) {
        return true;
    }

    var vMessage = '';
    var vSuffix = '<br />\n';
    if (document.getElementById('radioBundle') && document.getElementById('radioBundle').checked) {
        var vPrefix = 'Bundle Vehicle: ';
        var year = document.getElementById('bundleYear');
        if (isEmpty(year) || !isNumeric(year)) {
            vMessage += vPrefix + 'Year must be a numeric value.' + vSuffix;
        } else if (!isLength(year.value, 4)) {
            vMessage += vPrefix + 'Year must be 4 digits long.' + vSuffix;
        }
        var make = document.getElementById('bundleMake');
        if (isEmpty(make)) {
            vMessage += vPrefix + 'Enter a value for Make.' + vSuffix;
        }
        var model = document.getElementById('bundleModel');
        if (isEmpty(model)) {
            vMessage += vPrefix + 'Enter a value for Model.' + vSuffix;
        }
        var color = document.getElementById('bundleColor');
        if (isEmpty(color)) {
            vMessage += vPrefix + 'Enter a value for Color.' + vSuffix;
        }
        var odometer = document.getElementById('bundleOdometer');
        if (isEmpty(odometer) || !isNumeric(odometer)) {
            vMessage += vPrefix + 'Odometer must be a numeric value.' + vSuffix;
        }
    }

    if (vMessage.length > 0) {
        document.getElementById('errorMessage').style.display = '';
        document.getElementById('errorMessage').style.color = 'red';
        document.getElementById('errorMessage').innerHTML = vMessage;
        return false;
    } else {
        document.getElementById('errorMessage').style.display = 'none';
        return true;
    }
}

function validateStep2Form() {
    if (document.getElementById('goto').value != 3) {
        return true;
    }

    var error = false;
    var theForm = document.getElementById('mainForm');
    with (theForm) {

        document.getElementById('firstNameLabel').innerHTML = '*First Name: ';
        document.getElementById('firstNameLabel').style.color = 'black';
        if (isEmpty(theForm.firstName)) {
            document.getElementById('firstNameLabel').innerHTML = 'First Name is required > ';
            document.getElementById('firstNameLabel').style.color = 'red';
            error = true;
        }

        document.getElementById('lastNameLabel').innerHTML = '*Last Name: ';
        document.getElementById('lastNameLabel').style.color = 'black';
        if (isEmpty(theForm.lastName)) {
            document.getElementById('lastNameLabel').innerHTML = 'Last Name is required > ';
            document.getElementById('lastNameLabel').style.color = 'red';
            error = true;
        }

        document.getElementById('daytimePhoneLabel').innerHTML = '*Daytime Phone #: ';
        document.getElementById('daytimePhoneLabel').style.color = 'black';
        if (isEmpty(theForm.workPhone1) || isEmpty(theForm.workPhone2) || isEmpty(theForm.workPhone3)) {
            document.getElementById('daytimePhoneLabel').innerHTML = 'Daytime Phone is required > ';
            document.getElementById('daytimePhoneLabel').style.color = 'red';
            error = true;
        } else if (!isNumeric(theForm.workPhone1.value) || !isNumeric(theForm.workPhone2.value) || !isNumeric(theForm.workPhone3.value)) {
            document.getElementById('daytimePhoneLabel').innerHTML = 'Daytime Phone must be numeric > ';
            document.getElementById('daytimePhoneLabel').style.color = 'red';
            error = true;
        } else if (!isLength(theForm.workPhone1.value, 3)) {
            document.getElementById('daytimePhoneLabel').innerHTML = 'Phone field 1 must be 3 digits > ';
            document.getElementById('daytimePhoneLabel').style.color = 'red';
            error = true;
        } else if (!isLength(theForm.workPhone2.value, 3)) {
            document.getElementById('daytimePhoneLabel').innerHTML = 'Phone field 2 must be 3 digits > ';
            document.getElementById('daytimePhoneLabel').style.color = 'red';
            error = true;
        } else if (!isLength(theForm.workPhone3.value, 4)) {
            document.getElementById('daytimePhoneLabel').innerHTML = 'Phone field 3 must be 4 digits > ';
            document.getElementById('daytimePhoneLabel').style.color = 'red';
            error = true;
        }


        if (!isEmpty(theForm.workPhoneExt)) {
            if (!isNumeric(theForm.workPhoneExt.value)) {
                document.getElementById('daytimePhoneLabel').innerHTML = 'Daytime Phone must be numeric > ';
                document.getElementById('daytimePhoneLabel').style.color = 'red';
                error = true;
            }
        }

        // if they give us some optional info, make sure it is properly formatted
        document.getElementById('eveningPhoneLabel').innerHTML = 'Evening Phone #: ';
        document.getElementById('eveningPhoneLabel').style.color = 'black';
        if (!isEmpty(theForm.homePhone1)) {
            if (!isNumeric(theForm.homePhone1.value)) {
                document.getElementById('eveningPhoneLabel').innerHTML = 'Evening Phone must be numeric > ';
                document.getElementById('eveningPhoneLabel').style.color = 'red';
                error = true;
            }
        }
        if (!isEmpty(theForm.homePhone2)) {
            if (!isNumeric(theForm.homePhone2.value)) {
                document.getElementById('eveningPhoneLabel').innerHTML = 'Evening Phone must be numeric > ';
                document.getElementById('eveningPhoneLabel').style.color = 'red';
                error = true;
            }
        }
        if (!isEmpty(theForm.homePhone3)) {
            if (!isNumeric(theForm.homePhone3.value)) {
                document.getElementById('eveningPhoneLabel').innerHTML = 'Evening Phone must be numeric > ';
                document.getElementById('eveningPhoneLabel').style.color = 'red';
                error = true;
            }
        }
        // if one of these is not empty...
        if (!isEmpty(theForm.homePhone1) || !isEmpty(theForm.homePhone2) || !isEmpty(theForm.homePhone3)) {
            // then if any are empty....
            if (isEmpty(theForm.homePhone1) || isEmpty(theForm.homePhone2) || isEmpty(theForm.homePhone3)) {
                document.getElementById('eveningPhoneLabel').innerHTML = 'Evening Phone incomplete > ';
                document.getElementById('eveningPhoneLabel').style.color = 'red';
                error = true;
            } else if (!isLength(theForm.homePhone1.value, 3)) {
                document.getElementById('eveningPhoneLabel').innerHTML = 'Phone field 1 must be 3 digits > ';
                document.getElementById('eveningPhoneLabel').style.color = 'red';
                error = true;
            } else if (!isLength(theForm.homePhone2.value, 3)) {
                document.getElementById('eveningPhoneLabel').innerHTML = 'Phone field 2 must be 3 digits > ';
                document.getElementById('eveningPhoneLabel').style.color = 'red';
                error = true;
            } else if (!isLength(theForm.homePhone3.value, 4)) {
                document.getElementById('eveningPhoneLabel').innerHTML = 'Phone field 3 must be 4 digits > ';
                document.getElementById('eveningPhoneLabel').style.color = 'red';
                error = true;
            }
        }

        // if they give us some optional info, make sure it is properly formatted
        document.getElementById('faxLabel').innerHTML = 'Fax #: ';
        document.getElementById('faxLabel').style.color = 'black';
        if (!isEmpty(theForm.fax1)) {
            if (!isNumeric(theForm.fax1.value)) {
                document.getElementById('faxLabel').innerHTML = 'Fax must be numeric > ';
                document.getElementById('faxLabel').style.color = 'red';
                error = true;
            }
        }
        if (!isEmpty(theForm.fax2)) {
            if (!isNumeric(theForm.fax2.value)) {
                document.getElementById('faxLabel').innerHTML = 'Fax must be numeric > ';
                document.getElementById('faxLabel').style.color = 'red';
                error = true;
            }
        }
        if (!isEmpty(theForm.fax3)) {
            if (!isNumeric(theForm.fax3.value)) {
                document.getElementById('faxLabel').innerHTML = 'Fax must be numeric > ';
                document.getElementById('faxLabel').style.color = 'red';
                error = true;
            }
        }
        // if one of these is not empty...
        if (!isEmpty(theForm.fax1) || !isEmpty(theForm.fax2) || !isEmpty(theForm.fax3)) {
            // then if any are empty....
            if (isEmpty(theForm.fax1) || isEmpty(theForm.fax2) || isEmpty(theForm.fax3)) {
                document.getElementById('faxLabel').innerHTML = 'Fax incomplete > ';
                document.getElementById('faxLabel').style.color = 'red';
                error = true;
            } else if (!isLength(theForm.fax1.value, 3)) {
                document.getElementById('faxLabel').innerHTML = 'Phone field 1 must be 3 digits > ';
                document.getElementById('faxLabel').style.color = 'red';
                error = true;
            } else if (!isLength(theForm.fax2.value, 3)) {
                document.getElementById('faxLabel').innerHTML = 'Phone field 2 must be 3 digits > ';
                document.getElementById('faxLabel').style.color = 'red';
                error = true;
            } else if (!isLength(theForm.fax3.value, 4)) {
                document.getElementById('faxLabel').innerHTML = 'Phone field 3 must be 4 digits > ';
                document.getElementById('faxLabel').style.color = 'red';
                error = true;
            }

        }

        document.getElementById('emailLabel').innerHTML = ' *Email: ';
        document.getElementById('confirmEmailLabel').innerHTML = ' *Confirm Email: ';
        document.getElementById('emailLabel').style.color = 'black';
        document.getElementById('confirmEmailLabel').style.color = 'black';
        if (isEmpty(theForm.email)) {
            document.getElementById('emailLabel').innerHTML = 'Email address is required >';
            document.getElementById('emailLabel').style.color = 'red';
            error = true;
        }
        if (isEmpty(theForm.confirmEmail)) {
            document.getElementById('confirmEmailLabel').innerHTML = 'Confirm address is required >';
            document.getElementById('confirmEmailLabel').style.color = 'red';
            error = true;
        }
        if (!isEmpty(theForm.email) && !isEmpty(theForm.confirmEmail)) {
            if (theForm.email.value != theForm.confirmEmail.value ) {
                document.getElementById('emailLabel').innerHTML = 'Email addresses do not match >';
                document.getElementById('emailLabel').style.color = 'red';
                error = true;
            } else if (!isValidEmailAddress(theForm.email.value)) {
                document.getElementById('emailLabel').innerHTML = 'Invalid email address > ';
                document.getElementById('emailLabel').style.color = 'red';
                error = true;
            }
        }
    }

    document.getElementById('address1Label').innerHTML = '*Address 1: ';
    document.getElementById('address1Label').style.color = 'black';
    if (isEmpty(theForm.address1)) {
        document.getElementById('address1Label').innerHTML = 'Address 1 is required > ';
        document.getElementById('address1Label').style.color = 'red';
        error = true;
    }

    document.getElementById('cityLabel').innerHTML = '*City: ';
    document.getElementById('cityLabel').style.color = 'black';
    if (isEmpty(theForm.city)) {
        document.getElementById('cityLabel').innerHTML = 'City is required > ';
        document.getElementById('cityLabel').style.color = 'red';
        error = true;
    }

    document.getElementById('stateCodeLabel').innerHTML = '*State: ';
    document.getElementById('stateCodeLabel').style.color = 'black';
    if (isEmpty(theForm.stateCode)) {
        document.getElementById('stateCodeLabel').innerHTML = 'State is required > ';
        document.getElementById('stateCodeLabel').style.color = 'red';
        error = true;
    }

    document.getElementById('postalCodeLabel').innerHTML = '*Zip: ';
    document.getElementById('postalCodeLabel').style.color = 'black';
    if (isEmpty(theForm.postalCode)) {
        document.getElementById('postalCodeLabel').innerHTML = 'Zip is required > ';
        document.getElementById('postalCodeLabel').style.color = 'red';
        error = true;
    }

    if (document.getElementById('passwordLabel') != null)  {
    document.getElementById('passwordLabel').innerHTML = '*Password: ';
    document.getElementById('passwordLabel').style.color = 'black';
    document.getElementById('confirmPasswordLabel').innerHTML = '*Confirm Password: ';
    document.getElementById('confirmPasswordLabel').style.color = 'black';
    if (isEmpty(theForm.password)) {
        document.getElementById('passwordLabel').innerHTML = 'Password is required > ';
        document.getElementById('passwordLabel').style.color = 'red';
        error = true;
    } else if (isEmpty(theForm.confirmPassword)) {
        document.getElementById('confirmPasswordLabel').innerHTML = 'Confirm Password is required > ';
        document.getElementById('confirmPasswordLabel').style.color = 'red';
        error = true;
    } else if (theForm.password.value != theForm.confirmPassword.value) {
        document.getElementById('passwordLabel').innerHTML = 'Passwords do not match > ';
        document.getElementById('passwordLabel').style.color = 'red';
        document.getElementById('confirmPasswordLabel').innerHTML = 'Passwords do not match > ';
        document.getElementById('confirmPasswordLabel').style.color = 'red';
        document.getElementById('password').value = '';
        document.getElementById('confirmPassword').value = '';
        error = true;
    }
    }

    var vMessage = '';
    var vTable = document.getElementById('vehiclesTable');
    var vSuffix = '<br />\n';
    for (var i = 1; i < Math.abs(vIndex); i++) {
        var hazard = document.getElementById('vSel-' + i);
        if (isEmpty(hazard)) {
            continue;
        }
        var vPrefix = 'Vehicle ' + (i) + ': ';
        var year = document.getElementById('vYear-' + i);
        if (isEmpty(year) || !isNumeric(year)) {
            vMessage += vPrefix + 'Year must be a numeric value.' + vSuffix;
        } else if (!isLength(year.value, 4)) {
            vMessage += vPrefix + 'Year must be 4 digits long.' + vSuffix;
        }
        var make = document.getElementById('vMake-' + i);
        if (make && isEmpty(make) && (make.style.display != 'none')) {
            vMessage += vPrefix + 'Enter a value for Make.' + vSuffix;
        }
        var model = document.getElementById('vModel-' + i);
        if (isEmpty(model)) {
            vMessage += vPrefix + 'Enter a value for Model.' + vSuffix;
        }
        var color = document.getElementById('vColor-' + i);
        if (isEmpty(color)) {
            vMessage += vPrefix + 'Enter a value for Color.' + vSuffix;
        }
        var odometer = document.getElementById('vOdometer-' + i);
        if (isEmpty(odometer) || !isNumeric(odometer)) {
            vMessage += vPrefix + 'Odometer must be a numeric value.' + vSuffix;
        }
    }

    if (vMessage.length > 0) {
        error = true;
    }

    if (error) {
        document.getElementById('vehicleErrors').style.display = '';
        document.getElementById('vehicleErrors').innerHTML = vMessage;
    } else {
        document.getElementById('vehicleErrors').style.display = 'none';
    }

    if (error) {
        document.getElementById('errorMessage').style.display = '';
        document.getElementById('errorMessage').style.color = 'red';
    } else {
        document.getElementById('errorMessage').style.display = 'none';
    }

    return !error;
}

function validateStep3Form() {
    if (document.getElementById('goto').value != 4) {
        return true;
    }

    var error = false;
    var theForm = document.getElementById('mainForm');
    with (theForm) {
        document.getElementById('billingAddress1Label').innerHTML = '*Address 1: ';
        document.getElementById('billingAddress1Label').style.color = 'black';
        if (isEmpty(theForm.billingAddress1)) {
            document.getElementById('billingAddress1Label').innerHTML = 'Address 1 is required > ';
            document.getElementById('billingAddress1Label').style.color = 'red';
            error = true;
        }

        document.getElementById('billingCityLabel').innerHTML = '*City: ';
        document.getElementById('billingCityLabel').style.color = 'black';
        if (isEmpty(theForm.billingCity)) {
            document.getElementById('billingCityLabel').innerHTML = 'City is required > ';
            document.getElementById('billingCityLabel').style.color = 'red';
            error = true;
        }

        document.getElementById('billingStateCodeLabel').innerHTML = '*State: ';
        document.getElementById('billingStateCodeLabel').style.color = 'black';
        if (isEmpty(theForm.billingStateCode)) {
            document.getElementById('billingStateCodeLabel').innerHTML = 'State is required > ';
            document.getElementById('billingStateCodeLabel').style.color = 'red';
            error = true;
        }

        document.getElementById('billingPostalCodeLabel').innerHTML = '*Zip: ';
        document.getElementById('billingPostalCodeLabel').style.color = 'black';
        if (isEmpty(theForm.billingPostalCode)) {
            document.getElementById('billingPostalCodeLabel').innerHTML = 'Zip is required > ';
            document.getElementById('billingPostalCodeLabel').style.color = 'red';
            error = true;
        }

        document.getElementById('cardHoldersNameLabel').innerHTML = '*Name on card: ';
        document.getElementById('cardHoldersNameLabel').style.color = 'black';
        if (isEmpty(theForm.cardHoldersName)) {
            document.getElementById('cardHoldersNameLabel').innerHTML = 'Name is required > ';
            document.getElementById('cardHoldersNameLabel').style.color = 'red';
            error = true;
        }

        document.getElementById('cardNumberLabel').innerHTML = '*Card Number: ';
        document.getElementById('cardNumberLabel').style.color = 'black';
        if (isEmpty(theForm.cardNumber)) {
            document.getElementById('cardNumberLabel').innerHTML = 'Card Number is required > ';
            document.getElementById('cardNumberLabel').style.color = 'red';
            error = true;
        } else if (!isNumeric(theForm.cardNumber.value)) {
            document.getElementById('cardNumberLabel').innerHTML = 'Card Number must be numeric > ';
            document.getElementById('cardNumberLabel').style.color = 'red';
            error = true;
        }

        document.getElementById('expDateLabel').innerHTML = '*Exp Date: ';
        document.getElementById('expDateLabel').style.color = 'black';
        if (isEmpty(theForm.cardExpMonth) || isEmpty(theForm.cardExpYear)) {
            document.getElementById('expDateLabel').innerHTML = 'Exp. Date is required > ';
            document.getElementById('expDateLabel').style.color = 'red';
            error = true;
        }

        document.getElementById('cardCVV2Label').innerHTML = '*Verification Code: ';
        document.getElementById('cardCVV2Label').style.color = 'black';
        if (isEmpty(theForm.cardCVV2)) {
            document.getElementById('cardCVV2Label').innerHTML = 'Verification Code is required > ';
            document.getElementById('cardCVV2Label').style.color = 'red';
            error = true;
        } else if (!isNumeric(theForm.cardCVV2.value)) {
            document.getElementById('cardCVV2Label').innerHTML = 'Verification Code must be numeric > ';
            document.getElementById('cardCVV2Label').style.color = 'red';
            error = true;
        } else {
            if (theForm.cardType.value == 2) {  // Amex
                if (!isLength(theForm.cardCVV2.value, 4)) {
                    document.getElementById('cardCVV2Label').innerHTML = 'Code must be 4 digits for this card type > ';
                    document.getElementById('cardCVV2Label').style.color = 'red';
                    error = true;
                }
            } else { // Visa, MC, Disc
                if (!isLength(theForm.cardCVV2.value, 3)) {
                    document.getElementById('cardCVV2Label').innerHTML = 'Code must be 3 digits for this card type > ';
                    document.getElementById('cardCVV2Label').style.color = 'red';
                    error = true;
                }
            }
        }
    }

    return !error;
}

function validateRoadsideFleetForm() {
	var error = false;
	var theForm = document.getElementById('roadside_contactform');
	with (theForm) {

		document.getElementById('stateLabel').innerHTML = '*State';
		document.getElementById('stateLabel').style.color = 'black';
		if (theForm.stateProvince.selectedIndex == 0) {
			document.getElementById('stateLabel').innerHTML = 'State is required';
			document.getElementById('stateLabel').style.color = 'red';
			error = true;
		}

		document.getElementById('firstNameLabel').innerHTML = '*First Name';
		document.getElementById('firstNameLabel').style.color = 'black';
		if (isEmpty(theForm.firstName)) {
			document.getElementById('firstNameLabel').innerHTML = 'First Name is required';
			document.getElementById('firstNameLabel').style.color = 'red';
			error = true;
		}

		document.getElementById('lastNameLabel').innerHTML = '*Last Name';
		document.getElementById('lastNameLabel').style.color = 'black';
		if (isEmpty(theForm.lastName)) {
			document.getElementById('lastNameLabel').innerHTML = 'Last Name is required';
			document.getElementById('lastNameLabel').style.color = 'red';
			error = true;
		}

		document.getElementById('emailLabel').innerHTML = ' *Email';
		document.getElementById('emailLabel').style.color = 'black';
		if (!isEmpty(theForm.email)) {
			if (!isValidEmailAddress(theForm.email.value)) {
				document.getElementById('emailLabel').innerHTML = 'Invalid email address';
				document.getElementById('emailLabel').style.color = 'red';
				error = true;
			}
		}

		document.getElementById('phoneLabel').innerHTML = '*Phone #';
		document.getElementById('phoneLabel').style.color = 'black';
		if (isEmpty(theForm.phone1) || isEmpty(theForm.phone2) || isEmpty(theForm.phone3)) {
			document.getElementById('phoneLabel').innerHTML = 'Phone is required';
			document.getElementById('phoneLabel').style.color = 'red';
			error = true;
		} else if (!isNumeric(theForm.phone1.value) || !isNumeric(theForm.phone2.value) || !isNumeric(theForm.phone3.value)) {
			document.getElementById('phoneLabel').innerHTML = 'Enter a valid phone';
			document.getElementById('phoneLabel').style.color = 'red';
			error = true;
		}
		if (!isEmpty(theForm.phone_ext)) {
			if (!isNumeric(theForm.phone_ext.value)) {
				document.getElementById('phoneLabel_ext').innerHTML = 'Ext must be numeric';
				document.getElementById('phoneLabel_ext').style.color = 'red';
				error = true;
			}
		}

		if (error) {
            document.getElementById('errorMessage').style.display = '';
            document.getElementById('errorMessage').style.color = 'red';
        } else {
            document.getElementById('errorMessage').style.display = 'none';
            document.getElementById('roadside_contactform').submit();
        }
		return error;
	}
}

function validateTireWheelFleetForm() {
	var error = false;
	var theForm = document.getElementById('tirewheel_contactform');
	with (theForm) {

		document.getElementById('stateLabel').innerHTML = '*State';
		document.getElementById('stateLabel').style.color = 'black';
		if (theForm.stateProvince.selectedIndex == 0) {
			document.getElementById('stateLabel').innerHTML = 'State is required';
			document.getElementById('stateLabel').style.color = 'red';
			error = true;
		}

		document.getElementById('firstNameLabel').innerHTML = '*First Name';
		document.getElementById('firstNameLabel').style.color = 'black';
		if (isEmpty(theForm.firstName)) {
			document.getElementById('firstNameLabel').innerHTML = 'First Name is required';
			document.getElementById('firstNameLabel').style.color = 'red';
			error = true;
		}

		document.getElementById('lastNameLabel').innerHTML = '*Last Name';
		document.getElementById('lastNameLabel').style.color = 'black';
		if (isEmpty(theForm.lastName)) {
			document.getElementById('lastNameLabel').innerHTML = 'Last Name is required';
			document.getElementById('lastNameLabel').style.color = 'red';
			error = true;
		}

		document.getElementById('emailLabel').innerHTML = ' *Email';
		document.getElementById('emailLabel').style.color = 'black';
		if (!isEmpty(theForm.email)) {
			if (!isValidEmailAddress(theForm.email.value)) {
				document.getElementById('emailLabel').innerHTML = 'Invalid email address';
				document.getElementById('emailLabel').style.color = 'red';
				error = true;
			}
		}

		document.getElementById('phoneLabel').innerHTML = '*Phone #';
		document.getElementById('phoneLabel').style.color = 'black';
		if (isEmpty(theForm.phone1.value) || isEmpty(theForm.phone2.value) || isEmpty(theForm.phone3.value)) {
			document.getElementById('phoneLabel').innerHTML = 'Phone is required';
			document.getElementById('phoneLabel').style.color = 'red';
			error = true;
		} else if (!isNumeric(theForm.phone1.value) || !isNumeric(theForm.phone2.value) || !isNumeric(theForm.phone3.value)) {
			document.getElementById('phoneLabel').innerHTML = 'Enter a valid phone';
			document.getElementById('phoneLabel').style.color = 'red';
			error = true;
		}
		if (!isEmpty(theForm.phone_ext)) {
			if (!isNumeric(theForm.phone_ext.value)) {
				document.getElementById('phoneLabel_ext').innerHTML = 'Ext must be numeric';
				document.getElementById('phoneLabel_ext').style.color = 'red';
				error = true;
			}
		}

		if (error) {
            document.getElementById('errorMessage').style.display = '';
            document.getElementById('errorMessage').style.color = 'red';
        } else {
            document.getElementById('errorMessage').style.display = 'none';
            document.getElementById('tirewheel_contactform').submit();
        }
		return error;

	}
}

var vIndex = -1;
function addStep1Vehicle(type, sel) {
    if(document.getElementById('vehiclesTable')) {
        var vehiclesTable = document.getElementById('vehiclesTable');
        var row = vehiclesTable.insertRow(vehiclesTable.rows.length - 1);
        var td = row.insertCell(-1);
        vIndex--;
        td.innerHTML =
            '<input type="hidden" name="vID' + vIndex + '" value="' + Math.abs(vIndex) + '">' +
            '<input type="hidden" name="vBundle' + vIndex + '" value="false">' +
            'Add Vehicle ' + (Math.abs(vIndex)-1) + ': ' +
            '<select name="vType' + vIndex + '"><option value="Auto" ' + (type == 'Auto' ? 'selected' : '') + '>Auto</option><option value="Motorcycle" ' + (type == 'Motorcycle' ? 'selected' : '') + '>Motorcycle</option><option value="RV" ' + (type == 'RV' ? 'selected' : '') + '>RV</option></select>' +
            '&nbsp;<input name="vSel' + vIndex + '" type="radio" value="advantage" ' + (sel == 'advantage' ? 'checked' : '') + '/>&nbsp;Advantage Tire &amp; Wheel Protection' +
            '&nbsp;<input name="vSel' + vIndex + '" type="radio" value="" ' + (!sel ? 'checked' : '') + '/>&nbsp;No Thanks';
    }
    document.getElementById('vIndex').value = vIndex;
}

function enableBundleVehicle() {
    if (document.getElementById('bundleVehicleRow')) {
        document.getElementById('bundleVehicleRow').style.display = '';
    }
}

function disableBundleVehicle() {
    if (document.getElementById('bundleVehicleRow')) {
        document.getElementById('bundleVehicleRow').style.display = 'none';
    }
}

function updateVehicleType(index) {
    var vType = document.getElementById('vType' + index).value;
    document.getElementById('vMake' + index).style.display = ('Auto' == vType) ? 'none' : '';
    document.getElementById('vMakeA' + index).style.display = ('Auto' == vType) ? '' : 'none';
}

function addStep2Vehicle() {
    var vTable = $('vehiclesTable');
    var row = vTable.insertRow(vTable.rows.length - 1);
    var cell = row.insertCell(-1);
    cell.colSpan = 10;
    cell.innerHTML = '<span class="bold"><select onchange="updateVehicleType(' + vIndex + ');" name="vType' + vIndex + '" id="vType' + vIndex + '"><option value="Auto">Auto</option><option value="Motorcycle">Motorcycle</option><option value="RV">RV</option></select> enrolled in <select name="vSel' + vIndex + '" id="vSel' + vIndex + '"><option value="">None</option><option value="classic">Classic</option><option value="advantage">Advantage</option></select>:</span>';
    row = vTable.insertRow(vTable.rows.length - 1);
    row.insertCell(-1).innerHTML =
    '<input type="hidden" name="vID' + vIndex + '" value="' + vIndex + '">' +
    'Vehicle ' + Math.abs(vIndex);
    row.insertCell(-1).innerHTML = 'Year:';
    row.insertCell(-1).innerHTML = '<input name="vYear' + vIndex + '" type="text" id="vYear' + vIndex + '" size="4" value=""/>';
    row.insertCell(-1).innerHTML = 'Make:';
    row.insertCell(-1).innerHTML = '<input name="vMake' + vIndex + '" id="vMake' + vIndex + '" type="text" id="vMake' + vIndex + '" size="12" value=""/ style="display: none;">' +
            '<select name="vMakeA' + vIndex + '" id="vMakeA' + vIndex + '"><option>Acura</option><option>Audi</option><option>BMW</option><option>Buick</option><option>Cadillac</option><option>Chevrolet</option><option>Chrysler</option><option>Cooper</option><option>Daewoo</option><option>Dodge</option><option>Eagle</option><option>Ford</option><option>GMC</option><option>Honda</option><option>Hummer</option><option>Hyundai</option><option>Infiniti</option><option>Isuzu</option><option>Jaguar</option><option>Jeep</option><option>Kia</option><option>Land Rover</option><option>Lexus</option><option>Lincoln</option><option>Mazda</option><option>Mercedes Benz</option><option>Mercury</option><option>Mitsubishi</option><option>Nissan</option><option>Oldsmobile</option><option>Plymouth</option><option>Pontiac</option><option>Porsche</option><option>Saab</option><option>Saturn</option><option>Scion</option><option>Subaru</option><option>Suzuki</option><option>Toyota</option><option>Volkswagen</option><option>Volvo</option></select>';
    row.insertCell(-1).innerHTML = 'Model:';
    row.insertCell(-1).innerHTML = '<input name="vModel' + vIndex + '" type="text" id="vModel' + vIndex + '" size="12" value=""/>';
    row.insertCell(-1).innerHTML = 'Color:';
    row.insertCell(-1).innerHTML = '<input name="vColor' + vIndex + '" type="text" id="vColor' + vIndex + '" size="10" value=""/>';
    row.insertCell(-1).innerHTML = 'Approximate<br />Mileage:';
    row.insertCell(-1).innerHTML = '<input name="vOdometer' + vIndex + '" type="text" id="vOdometer' + vIndex + '" size="9" value=""/>';
    vIndex--;
    document.getElementById('vIndex').value = vIndex;
}

function submitAffiliateForm() {
	var form = document.getElementById('affiliateForm');
	if(validateAffiliateForm(form)) {
		form.submit();
	}
}
function validateAffiliateForm(form) {
	var returnVal = true;
	var email = false;
    var phone = false;

	document.getElementById('label_firstName').className = '';
	if (isEmpty(form.firstName)) {
		document.getElementById('label_firstName').className = 'req';
		returnVal = false;
	}

	document.getElementById('label_lastName').className = '';
	if (isEmpty(form.lastName)) {
		document.getElementById('label_lastName').className = 'req';
		returnVal = false;
	}

	document.getElementById('label_email').innerHTML = 'Email';
	document.getElementById('label_email').className = '';
	if (isEmpty(form.email)) {
		email = false;
		//returnVal = false;
	} else if (!isValidEmailAddress(form.email.value)) {
		document.getElementById('label_email').innerHTML = 'Invalid email address format';
		document.getElementById('label_email').className = 'req';
		email = false;
		returnVal = false;
	} else {
		email = true;
	}

	document.getElementById('label_phone').innerHTML = 'Phone Number';
	document.getElementById('label_phone').className = '';
	phone = !(isEmpty(form.phone1) || isEmpty(form.phone2) || isEmpty(form.phone3));

	if (returnVal && !email && !phone) {
		returnVal = false;
		alert("Please provide either your phone number or email address\n      so that we may contact you about your quoute.");
	}

	return returnVal;
}

function validateStateForm() {
    if (document.getElementById('stateForm').stateProvince.value == '') {
        document.getElementById('pickState').style.display = '';
    } else {
        document.getElementById('stateForm').submit();
    }
}

function openpopup(popurl){
    window.open(popurl,"","width=400,height=500,left=15,top=15,scrollbars,resizable").focus();
}

function newQuote() {
        document.cookie = ('showEntryForm=true');
        document.cookie = ('showCallMeForm=false');
        document.cookie = ('currentQuoteId=;');
        document.getElementById('newQuoteForm').submit();
}

function startOver() {
	try {
        document.getElementById('quote_top').className = '';
        document.getElementById('quote_box').className = '';
        document.getElementById('quote').className = '';
        document.getElementById('sidebarQuote').style.display = 'none';
        document.getElementById('sidebarThanks').style.display = 'none';
		document.getElementById('notabs').innerHTML = 'Instant Quote';
        document.cookie = ('currentQuoteId=');
        showEntryForm();
    } catch(e) {
        alert('startOver() error: ' + e);
    }
}

function showEntryForm() {
    try {
        document.getElementById('sidebarEntryForm').style.display = '';
        document.getElementById('notabs').style.display = '';
        document.getElementById('notabs').innerHTML = 'Instant Quote';
        document.cookie = ('showEntryForm=true');
        document.cookie = ('showCallMeForm=false');
    } catch(e) {
        alert('showEntryForm() error: ' + e);
    }
}

function showCallMeForm() {
    document.cookie = ('showEntryForm=false');
    document.cookie = ('callMeForm=true');
}


function showArchiveReports() {
    if ($("#archiveReportsList").css('display') == 'none')
        $("#archiveReportsList").css('display', 'block');
    else
        $("#archiveReportsList").css('display', 'none');

}
