$(document).ready(function() {

	//prevent the enter key from submitting the form
	$("input").keypress(function(e) {
		if(e.which == 13) {
			validateContactUs()
			e.preventDefault();
			return true;
		}
	});
    // When the country changes to a non-US country, hide State and Zip fields and change "Where should we direct your request" to "International Support"
    $("#Country").change(function() {
        var country = $("#Country").val();
        if (country == "-1" || country == "US") {
            $("#State").removeAttr("disabled");
            $("#ZipCode").removeAttr("disabled");
            $("#County").removeAttr("disabled");
        }
        else {
            $("#State").attr("disabled", "disabled").val("-1");
            $("#ZipCode").attr("disabled", "disabled").val("");
            $("#County").attr("disabled", "disabled").val("");
        }
    });

    // Validate on submit
    $("#btnSubmit").click(function() { return validateContactUs(); });


function highlightCell(ctrlid) {
    $("#" + ctrlid).parent().addClass("error");
}

function unhighlightCell(ctrlid) {
    $("#" + ctrlid).parent("td").removeClass("error");
}

function unhighlightAllCells() {
    $("td").removeClass("error");
}

function validateContactUs() {
    var reqfields = true;
    var formatsok = true;
    unhighlightAllCells();

    try {
        // First Name is required
        if ($("#YourName").val().trim() == "") {
            highlightCell("YourName");
            reqfields = false;
        }


        // Company is required
        if ($("#Company").val().trim() == "") {
            highlightCell("Company");
            reqfields = false;
        }

        // City is required
        if ($("#City").val().trim() == "") {
            highlightCell("City");
            reqfields = false;
        }
        
	if ($("#StreetAddress").val().trim() == "") {
		highlightCell("StreetAddress");
		reqfields = false;
        }
        
        if ($("#ModelNumber").val().trim() == "") {
			highlightCell("ModelNumber");
			reqfields = false;
        }
        if ($("#SerialNumber").val().trim() == "") {
			highlightCell("SerialNumber");
			reqfields = false;
        }
        if ($("#DateofInstallation").val().trim() == "") {
			highlightCell("DateofInstallation");
			reqfields = false;
        }

        // State is required if enabled
        if (!$("#State").attr("disabled") && $("#State").val() == "") {
            highlightCell("State");
            reqfields = false;
        }

        // ZIP Code is required if enabled
        if (!$("#ZipCode").attr("disabled") && $("#ZipCode").val().trim() == "") {
            highlightCell("ZipCode");
            reqfields = false;
        }

        // Country is required
        if ($("#Country").val() == "") {
            highlightCell("Country");
            reqfields = false;
        }
	        if ($("#SerialNumber").val() == "-1") {
	            highlightCell("SerialNumber");
	            reqfields = false;
        }
	        if ($("#ModelNumber").val() == "-1") {
	            highlightCell("ModelNumber");
	            reqfields = false;
        }

        if (!reqfields) {
            alert("One or more required fields were not filled out.");
        }

        return reqfields;
    }
    catch (ex) {
        return false;
    }
}
});