$(document).ready(function() {
    // Set up tabs
    // Using fx causes the page to jump to the top on tab click
    $("#tabs").tabs().show(); // { fx: { opacity: "toggle", duration: "fast"} });

    //track tabs
    $(".ui-tabs-nav a").click(function(){
      pageTracker._trackEvent("Contact Us", "Tab View", $(this).find("span").html());
    });
    
    // Wire up box 1 radio buttons
    $("#purchaseEquipment_1 input:radio").click(function() {
        pageTracker._trackEvent("Contact Us", "Purchase Equipment 1 Button Click", this.id);
        setBox1();
    });

    // Wire up box 2 radio buttons
    $("#purchaseEquipment_2 input:radio").click(function() {
      pageTracker._trackEvent("Contact Us", "Purchase Equipment 2 Button Click", this.id);
        setBox2();
    });

    // Wire up box 1 change button
    $("#btnChangePurchaseEquipment_1").click(function() {
      pageTracker._trackEvent("Contact Us", "Purchase Equipment 1 Change Click");
        clearBox1();
        return false;
    });

    // Wire up box 2 change button
    $("#btnChangePurchaseEquipment_2").click(function() {
      pageTracker._trackEvent("Contact Us", "Purchase Equipment 2 Change Click");
        clearBox2();
        return false;
    });

    // Wire up go button
    $(".btnGo:first").click(btnGo_Click);

    // Wire up service directory button
    $(".btnServiceDirGo:first").click(btnServiceDirGo_Click);

    // Wire up contact us form event handlers
    wireUpContactUsForm();

    // For testing purposes
    //    $("#iWorkInGrocery").click();
    //    $("#findDealerDistributor").click();
    //    $("#txtZipCode").val("45150");
});

// ------------------------------------------
// PURCHASE EQUIPMENT FORM
// ------------------------------------------

function btnGo_Click() {
    $(".btnGo:first").unbind("click", btnGo_Click).bind("click", function() { return false });
    doLookup();
    return false;
}

function doLookup() {
    if (validate()) {
        $("#purchaseEquipmentResultsInner").fadeOut("fast", function() {
            $("#purchaseEquipmentResultsInner").html("");
            $("#purchaseEquipmentResultsOverlay").fadeIn("fast", function() {
                setResultHeading();
                if ($("#iWorkInFoodService:checked").length > 0) {
                    if ($("#findDealerDistributor:checked").length > 0) {
                      pageTracker._trackEvent("Contact Us", "Get Foodservice Dealers", $("#txtZipCode").val() + "|" + $("#ddlCountry option:selected").val());
                        getFoodserviceDealers();
                    }
                    else if ($("#findSalesRep:checked").length > 0) {
                      pageTracker._trackEvent("Contact Us", "Get Foodservice Sale Reps", $("#txtZipCode").val() + "|" + $("#ddlCountry option:selected").val());
                        getFoodserviceSalesRep();
                    }
                    else if ($("#buyOnline:checked").length > 0) {
                        getFoodserviceBuyOnline();
                    }
                }
                else if ($("#iWorkInGrocery:checked").length > 0) {
                  pageTracker._trackEvent("Contact Us", "Get Food Retail Distributor", $("#txtZipCode").val() + "|" + $("#ddlCountry option:selected").val());
                    getFoodRetailDistributor();
                }
                $("#purchaseEquipmentResultsOverlay").fadeOut("fast", function() {
                    $("#purchaseEquipmentResultsInner").fadeIn("fast");
                });
            });
        });
    }
    $(".btnGo:first").unbind("click", null).bind("click", btnGo_Click);
}

function getFoodserviceDealers() {
    var TraulsenBaxterStr = "";
    if ($("#ddlCountry").val() != "-1") {
        $.getJSON("/Food-Service-Dealer/?r=" + encodeURIComponent($("#ddlCountry").val()), function(data) {
            if (!data) {
                showNoResults();
            }
            else {
                var records = data.records;
                if (records.length > 0) {
                    $("<div id='purchaseEquipmentInternationalResults'>").appendTo("#purchaseEquipmentResultsInner");
                    for (var i = 0; i < records.length; i++) {
                        var record = records[i];
                        $("<a>").text(record.name).attr("href", "javascript:showInternationalFoodserviceDealer(" + record.id + ")").appendTo("#purchaseEquipmentInternationalResults");
                        $("<p>").text(record.areas_served).appendTo("#purchaseEquipmentInternationalResults");
                    }
                }
                else {
                    showNoResults();
                }
            }
        });
    }
    else if ($("#txtZipCode").val().trim() != "") {
        $.getJSON("/Food-Service-Dealer/?z=" + $("#txtZipCode").val().trim(), function(data) {
            if (!data) {
                showNoResults();
            }
            else {
                var records = data.records;
                if (records.length > 0) {
                    $("<div id='purchaseEquipmentResultsLeft'>").appendTo("#purchaseEquipmentResultsInner");
                    $("<div id='purchaseEquipmentResultsRight'>").appendTo("#purchaseEquipmentResultsInner");
                    for (var i = 0; i < records.length; i++) {
                        var divid = "#purchaseEquipmentResultsLeft";
                        if (i >= (records.length / 2)) {
                            divid = "#purchaseEquipmentResultsRight";
                        }
                        var record = records[i];
                        $("<h5>").text(record.city).appendTo(divid);
                        for (var j = 0; j < record.dealers.length; j++) {
                            var dealer = record.dealers[j];
                             namesplit = dealer.name.split("|");
			                                
				TraulsenBaxterStr = "";
				if(namesplit[1] == "Disable" && namesplit[2] == "Enable" && namesplit[3] == "Disable"){
					TraulsenBaxterStr = " (Traulsen only)";
				}
				               
                            var tempdiv = $("<div>").appendTo(divid);
                            $("<a>").text(namesplit[0]).attr("href", "javascript:showFoodserviceDealer(" + dealer.id + ")").appendTo(tempdiv);
                            $("<span>").text(TraulsenBaxterStr).appendTo(tempdiv);
                        }
                    }
                    $("#purchaseEquipmentResultsInner").html($("#purchaseEquipmentResultsInner").html() + "<div class='clear'><!-- --></div>");
                }
                else {
                    showNoResults();
                }
            }
        });
    }
}

function showFoodserviceDealer(id) {
    showLightboxLoading(function() {
        // Load dealer
        $.getJSON("/Food-Service-Dealer/?id=" + id.toString(), function(data) {
            $("#purchaseEquipmentLightboxHead h4:first").text(data.record.name);
            $("#purchaseEquipmentAddress").html("<p>" + data.record.street1 + "<br />" + data.record.city + ", " + data.record.state + " " + data.record.postal_code + "</p>");
            if (data.record.phone.toString().trim() != "") {
                $("#purchaseEquipmentPhone").html("<p>Phone: " + data.record.phone + "</p>");
            }
            else {
                $("#purchaseEquipmentPhone").html("");
            }
            if (data.record.fax.toString().trim() != "") {
                $("#purchaseEquipmentFax").html("<p>Fax: " + data.record.fax + "</p>");
            }
            else {
                $("#purchaseEquipmentFax").html("");
            }
            if (data.record.email.toString().trim() != "") {
                $("#purchaseEquipmentEmail").html("<p>E-mail: <a href='mailto:" + data.record.email + "'>" + data.record.email + "</a></p>");
            }
            else {
                $("#purchaseEquipmentEmail").html("");
            }
            if (data.record.web_site.toString().trim() != "") {
                $("#purchaseEquipmentURL").html("<p>Web: <a href='" + data.record.web_site + "' target='_blank'>" + data.record.web_site + "</a></p>");
            }
            else {
                $("#purchaseEquipmentURL").html("");
            }
            showLightbox(function() {
                // Set up Google Map
                if (data.record.lat.toString().trim() != "" && data.record.lon.toString().trim() != "") {
                    loadGoogleMap(data.record.lat, data.record.lon);
                }
            });
        });
    }, id);
}

function showInternationalFoodserviceDealer(id) {
    pageTracker._trackEvent("Contact Us", "Show foodservice Location", id.toString());
    showLightboxLoading(function() {
        // Load dealer
        $.getJSON("/Food-Service-Dealer/?i=1&id=" + id.toString(), function(data) {
            $("#purchaseEquipmentLightboxHead h4:first").text(data.record.name);
            $("#purchaseEquipmentAddress").html("<p>" + data.record.address + "</p>");
            if (data.record.phone.toString().trim() != "") {
                $("#purchaseEquipmentPhone").html("<p>Phone: " + data.record.phone + "</p>");
            }
            else {
                $("#purchaseEquipmentPhone").html("");
            }
            if (data.record.fax.toString().trim() != "") {
                $("#purchaseEquipmentFax").html("<p>Fax: " + data.record.fax + "</p>");
            }
            else {
                $("#purchaseEquipmentFax").html("");
            }
            if (data.record.email.toString().trim() != "") {
                $("#purchaseEquipmentEmail").html("<p>E-mail: <a href='mailto:" + data.record.email + "'>" + data.record.email + "</a></p>");
            }
            else {
                $("#purchaseEquipmentEmail").html("");
            }
            if (data.record.web_site.toString().trim() != "") {
                var tempWebAddress = data.record.WebAddress;
                if(tempWebAddress.indexOf("http://") == -1){
                  tempWebAddress = "http://" + data.record.WebAddress;
                }
                $("#purchaseEquipmentURL").html("<p>Web: <a href='" + tempWebAddress + "' target='_blank'>" + data.record.web_site + "</a></p>");
            }
            else {
                $("#purchaseEquipmentURL").html("");
            }
            showLightbox(function() {
                // Set up Google Map
                if (data.record.lat.toString().trim() != "" && data.record.lon.toString().trim() != "") {
                    loadGoogleMap(data.record.lat, data.record.lon);
                }
            });
        });
    }, id);
}

function getFoodserviceSalesRep() {
    if ($("#ddlCountry").val() != "-1") {
        showNoResults();
    }
    else if ($("#txtZipCode").val().trim() != "") {
        $.getJSON("/Food-Service-Sales-Rep/?z=" + $("#txtZipCode").val().trim(), function(data) {
            if (!data) {
                showNoResults();
            }
            else {
                $("<div id='purchaseEquipmentSalesRepResults'>").appendTo("#purchaseEquipmentResultsInner");
                $("<h5>").text(data.record.name).appendTo("#purchaseEquipmentSalesRepResults");
                if (data.record.phone.toString().trim() != "") {
                    $("<p>").text("Phone: " + data.record.phone).appendTo("#purchaseEquipmentSalesRepResults");
                }
                if (data.record.email.toString().trim() != "") {
                    $("<a>").attr("href", "mailto:" + data.record.email).text(data.record.email).appendTo("#purchaseEquipmentSalesRepResults");
                }
            }
        });
    }
}

function getFoodserviceBuyOnline() {
    $("#purchaseEquipmentResultsInner").load("/ajaxcontent/FoodserviceBuyOnline.aspx #foodserviceBuyOnlineContent");
}

function getFoodRetailDistributor() {
    if ($("#ddlCountry").val() != "-1") {
        showNoResults();
    }
    else if ($("#txtZipCode").val().trim() != "") {
        $.getJSON("/Food-Retail-Dealer/?z=" + $("#txtZipCode").val().trim(), function(data) {
            if (!data) {
                showNoResults();
            }
            else {
                $("<h5>").text(data.root.RegionName).appendTo("#purchaseEquipmentResultsInner");
                $("<a>").text(data.root.DealerName).attr("href", "javascript:showFoodRetailDistributor(" + data.root.ID + ")").appendTo("#purchaseEquipmentResultsInner");
            }
        });
    }
}

function showFoodRetailDistributor(id) {
    pageTracker._trackEvent("Contact Us", "Show Food Retail Location", id.toString());
    showLightboxLoading(function() {
        // Load distributor
        $.getJSON("/Food-Retail-Dealer/?id=" + id.toString(), function(data) {
            $("#purchaseEquipmentLightboxHead h4:first").text(data.root.DealerName);
            $("#purchaseEquipmentAddress").html("<p>" + data.root.PhysicalAddress + "<br />" + data.root.CSZ + "</p>");
            if (data.root.PhoneNumber.toString().trim() != "") {
                $("#purchaseEquipmentPhone").html("<p>Phone: " + data.root.PhoneNumber + "</p>");
            }
            else {
                $("#purchaseEquipmentPhone").html("");
            }
            if (data.root.FaxNumber.toString().trim() != "") {
                $("#purchaseEquipmentFax").html("<p>Fax: " + data.root.FaxNumber + "</p>");
            }
            else {
                $("#purchaseEquipmentFax").html("");
            }
            if (data.root.EmailAddress.toString().trim() != "") {
                $("#purchaseEquipmentEmail").html("<p>E-mail: <a href='mailto:" + data.root.EmailAddress + "'>" + data.root.EmailAddress + "</a></p>");
            }
            else {
                $("#purchaseEquipmentEmail").html("");
            }
            if (data.root.WebAddress.toString().trim() != "") {
                var tempWebAddress = data.root.WebAddress;
                if(tempWebAddress.indexOf("http://") == -1){
                  tempWebAddress = "http://" + data.root.WebAddress;
                }
                $("#purchaseEquipmentURL").html("<p>Web: <a href='" + tempWebAddress + "' target='_blank'>" + data.root.WebAddress + "</a></p>");
            }
            else {
                $("#purchaseEquipmentURL").html("");
            }
            showLightbox(function() {
                // Set up Google Map
                if (data.root.Latitude.toString().trim() != "" && data.root.Longitude.toString().trim() != "") {
                    loadGoogleMap(data.root.Latitude, data.root.Longitude);
                }
            });
        });
    }, id);
}

function showLightboxLoading(cb, id) {
    $("#purchaseEquipmentLightboxBG").width($("body:first").width()).height($("body:first").height());
    $("#purchaseEquipmentLightboxBG").fadeIn("fast", function() {
        $("#ddlCountry").hide();
        $("#purchaseEquipmentLightboxBorder").css("margin-top", -198 + $(window).scrollTop() + "px").css("margin-left", -358 + $(window).scrollLeft() + "px").fadeIn("fast");
        $("#purchaseEquipmentLightboxOverlay").css("margin-top", -190 + $(window).scrollTop() + "px").css("margin-left", -350 + $(window).scrollLeft() + "px").fadeIn("fast", function() {
            $("#googleMapCanvas").hide();
            if (cb) {
                cb(id);
            }
        });
    });
}

function showLightbox(cb) {
    $("#purchaseEquipmentLightbox").css("margin-top", -190 + $(window).scrollTop() + "px").css("margin-left", -350 + $(window).scrollLeft() + "px").fadeIn("fast", function() {
        $("#purchaseEquipmentLightboxOverlay").fadeOut("fast", function() {
            if (cb) {
                cb();
            }
        });
    });
}

function loadGoogleMap(lat, lon) {
    if (GBrowserIsCompatible()) {
        $("#googleMapCanvas").show();
        var map = new GMap2(document.getElementById("googleMapCanvas"));
        var point = new GLatLng(lat, lon);
        map.setCenter(point, 15);
        map.setUIToDefault();
        map.addOverlay(new GMarker(point));
    }
}

function closeLightbox() {
    $("#purchaseEquipmentLightboxBorder").fadeOut("fast");
    $("#purchaseEquipmentLightbox").fadeOut("fast", function() {
        $("#purchaseEquipmentLightboxBG").fadeOut("fast");
        $("#purchaseEquipmentLightbox").hide();
        GUnload();
        $("#ddlCountry").show();
    });
}

function showNoResults() {
    $("<p>").text("No results.").appendTo("#purchaseEquipmentResultsInner");
}

function setBox1() {
    var box = $("#purchaseEquipment_1 .purchaseEquipmentBox:first");
    $(box).slideUp("fast", function() {
        $("#purchaseEquipmentChoices_1").hide();
        $("#purchaseEquipmentChoice_1").text($("#purchaseEquipment_1 input:radio:checked").attr("title")).show();
        $(box).slideDown("fast", function() {
            $("#btnChangePurchaseEquipment_1").fadeIn("fast");
            if ($("#iWorkInFoodService:checked").length > 0) {
                showBox(2);
            }
            else {
                showBox(3);
            }
        });
    });
}

function setBox2() {
    var box = $("#purchaseEquipment_2 .purchaseEquipmentBox:first");
    $(box).slideUp("fast", function() {
        $("#purchaseEquipmentChoices_2").hide();
        $("#purchaseEquipmentChoice_2").text($("#purchaseEquipment_2 input:radio:checked").attr("title")).show();
        $(box).slideDown("fast", function() {
            $("#btnChangePurchaseEquipment_2").fadeIn("fast");
            if ($("#iWorkInFoodService:checked").length > 0 && $("#buyOnline:checked").length > 0) {
                doLookup(); // No need to ask for zip code if buying online
            }
            else {
                showBox(3);
            }
        });
    });
}

function validate() {
    // BOX 1 - Make sure a radio button is checked
    if ($("#purchaseEquipment_1 input:radio:checked").length == 0) {
        markError(1);
        alert("Please choose your industry.");
        return false;
    }
    else {
        clearError();
    }

    // BOX 2 - Make sure a radio button is checked if the user indicates foodservice in step 1
    if ($("#iWorkInFoodService:checked").length > 0 && $("#purchaseEquipment_2 input:radio:checked").length == 0) {
        markError(2);
        alert("Please indicate what you are trying to locate.");
        return false;
    }
    else {
        clearError();
    }

    // BOX 3 - Make sure a location is entered if not buying online
    if ($("#buyOnline:checked").length == 0) {

        // If a country was selected, validate.
        if ($("#ddlCountry").val() != "-1") {
            clearError();
            return true;
        }

        // Make sure a five-digit US zip code was entered.
        var re = /^\d{5}([\-]\d{4})?$/;
        var entry = $("#txtZipCode").val();
        if (re.test(entry)) {
            clearError();
            return true;
        }
    }
    else {
        clearError();
        return true;
    }

    // Mark the ZIP code section and return false
    markError(3);
    alert("Please choose a ZIP code or country.");
    return false;
}

function markError(boxid) {
    $("#purchaseEquipment_" + boxid + " .purchaseEquipmentHeading:first").addClass("error");
}

function clearError() {
    $(".purchaseEquipmentHeading").each(function() { $(this).removeClass("error"); });
}

function clearResults() {
    $("#purchaseEquipmentResultsInner").fadeOut("fast").html("");
}

function showBox(boxid) {
    $("#purchaseEquipment_" + boxid).slideDown("fast");
}

function hideBox(boxid) {
    $("#purchaseEquipment_" + boxid).slideUp("fast");
}

function clearBox3() {
    $("#txtZipCode").val("");
    $("#ddlCountry").val("-1");
    clearError();
    clearResults();
}

function clearBox2() {
    var box = $("#purchaseEquipment_2 .purchaseEquipmentBox:first");
    $("#purchaseEquipment_2 input:radio").each(function() { $(this).removeAttr("checked"); });
    $("#btnChangePurchaseEquipment_2").fadeOut("fast", function() {
        $(box).slideUp("fast", function() {
            $("#purchaseEquipmentChoice_2").hide().text("");
            $("#purchaseEquipmentChoices_2").show();
            $(box).slideDown("fast");
        });
    });
    clearBox3();
    hideBox(3);
}

function clearBox1() {
    var box = $("#purchaseEquipment_1 .purchaseEquipmentBox:first");
    $("#purchaseEquipment_1 input:radio").each(function() { $(this).removeAttr("checked"); });
    $("#btnChangePurchaseEquipment_1").fadeOut("fast", function() {
        $(box).slideUp("fast", function() {
            $("#purchaseEquipmentChoice_1").hide().text("");
            $("#purchaseEquipmentChoices_1").show();
            $(box).slideDown("fast");
        });
    });
    clearBox2();
    hideBox(2);
}

function setResultHeading() {
    if ($("#ddlCountry").val() != "-1") {
        $("<h3>").text($("#ddlCountry option:selected").text()).appendTo("#purchaseEquipmentResultsInner");
    }
    else if ($("#txtZipCode").val().trim() != "") {
        $("<h3>").text($("#txtZipCode").val()).appendTo("#purchaseEquipmentResultsInner");
    }
}

// ------------------------------------------
// SERVICE DIRECTORY
// ------------------------------------------
function btnServiceDirGo_Click() {
    $(".btnServiceDirGo:first").unbind("click", btnServiceDirGo_Click).bind("click", function() { return false });
    doServiceDirLookup();
    return false;
}

function doServiceDirLookup() {

    if (validateServiceDir()) {
      pageTracker._trackEvent("Contact Us", "Lookup Service Directory", $("#ddlServiceState option:selected").val());
        $("#serviceDirectoryResultsInner").fadeOut("fast", function() {
            $("#serviceDirectoryResultsInner").html("");
            $("#serviceDirectoryResultsOverlay").fadeIn("fast", function() {
                setServiceDirectoryResultHeading();
                getServiceLocations();
                $("#serviceDirectoryResultsOverlay").fadeOut("fast", function() {
                    $("#serviceDirectoryResultsInner").fadeIn("fast");
                });
            });
        });
    }
    $(".btnServiceDirGo:first").unbind("click", null).bind("click", btnServiceDirGo_Click);
}

function getServiceLocations() {

    if ($("#chkServiceOutsideTheUS:checked")[0]) {
        $.getJSON("/Service-Directory/?i=1", function(data) {
            if (!data) {
                showNoServiceDirResults();
            }
            else {
                var records = data.records;
                if (records.length > 0) {
                    $("<div id='serviceDirectoryResultsLeft'>").appendTo("#serviceDirectoryResultsInner");
                    $("<div id='serviceDirectoryResultsRight'>").appendTo("#serviceDirectoryResultsInner");
                    for (var i = 0; i < records.length; i++) {
                        var divid = "#serviceDirectoryResultsLeft";
                        if (i >= (records.length / 2)) {
                            divid = "#serviceDirectoryResultsRight";
                        }
                        var record = records[i];
                        $("<h5>").text(record.country).appendTo(divid);
                        for (var j = 0; j < record.locs.length; j++) {
                            var loc = record.locs[j];
                            $("<a>").text(loc.name).attr("href", "javascript:showServiceLocation(" + loc.id + ")").appendTo(divid);
                        }
                    }
                    $("#serviceDirectoryResultsInner").html($("#serviceDirectoryResultsInner").html() + "<div class='clear'><!-- --></div>");
                }
                else {
                    showNoServiceDirResults();
                }
            }
        });
    }
    else {
        $.getJSON("/Service-Directory/?z=" + $("#ddlServiceState option:selected").val().trim(), function(data) {
            if (!data) {
                showNoServiceDirResults();
            }
            else {
                var records = data.records;
                if (records.length > 0) {
                    $("<div id='serviceDirectoryResultsLeft'>").appendTo("#serviceDirectoryResultsInner");
                    $("<div id='serviceDirectoryResultsRight'>").appendTo("#serviceDirectoryResultsInner");
                    for (var i = 0; i < records.length; i++) {
                        var divid = "#serviceDirectoryResultsLeft";
                        if (i >= (records.length / 2)) {
                            divid = "#serviceDirectoryResultsRight";
                        }
                        var record = records[i];
                        $("<h5>").text(record.city).appendTo(divid);
                        for (var j = 0; j < record.locs.length; j++) {
                            var loc = record.locs[j];
                            $("<a>").text(loc.name).attr("href", "javascript:showServiceLocation(" + loc.id + ")").appendTo(divid);
                        }
                    }
                    $("#serviceDirectoryResultsInner").html($("#serviceDirectoryResultsInner").html() + "<div class='clear'><!-- --></div>");
                }
                else {
                    showNoServiceDirResults();
                }
            }
        });
    }
}

function showServiceLocation(id) {
$("#ddlServiceState").hide();
    pageTracker._trackEvent("Contact Us", "Show Service Location", id.toString());
    showLightboxLoading(function() {
        // Load service location
        $.getJSON("/Service-Directory/?id=" + id.toString(), function(data) {
            $("#purchaseEquipmentLightboxHead h4:first").text(data.record.name);
            if (data.record.street1.toString().trim() != "" || data.record.city.toString().trim() != "" || data.record.state.toString().trim() != "" || data.record.postal_code.toString().trim() != "") {
                $("#purchaseEquipmentAddress").html("<p>" + data.record.street1 + "<br />" + data.record.city + ", " + data.record.state + " " + data.record.postal_code + "</p>");
            }
            else {
                $("#purchaseEquipmentAddress").html("");
            }
            if (data.record.phone.toString().trim() != "") {
                $("#purchaseEquipmentPhone").html("<p>Phone: " + data.record.phone + "</p>");
            }
            else {
                $("#purchaseEquipmentPhone").html("");
            }
            if (data.record.fax.toString().trim() != "") {
                $("#purchaseEquipmentFax").html("<p>Fax: " + data.record.fax + "</p>");
            }
            else {
                $("#purchaseEquipmentFax").html("");
            }
            if (data.record.email.toString().trim() != "") {
                $("#purchaseEquipmentEmail").html("<p>E-mail: <a href='mailto:" + data.record.email + "'>" + data.record.email + "</a></p>");
            }
            else {
                $("#purchaseEquipmentEmail").html("");
            }
            if (data.record.web_site.toString().trim() != "") {
                var tempWebAddress = data.record.web_site;
                if(tempWebAddress.indexOf("http://") == -1){
                  tempWebAddress = "http://" + data.record.web_site;
                }
                $("#purchaseEquipmentURL").html("<p>Web: <a href='" + tempWebAddress + "' target='_blank'>" + data.record.web_site + "</a></p>");
            }
            else {
                $("#purchaseEquipmentURL").html("");
            }
            showLightbox(function() {
            
              
                // Set up Google Map
                if (data.record.bg_lat.toString().trim() != "" && data.record.bg_long.toString().trim() != "" && data.record.bg_lat.toString().trim() != "0" && data.record.bg_long.toString().trim() != "0") {
                    loadGoogleMap(data.record.bg_lat, data.record.bg_long);
                    
                    $("#closeButton").click(function(){$("#ddlServiceState").show();});
                }
            });
        });
    }, id);
}

function showNoServiceDirResults() {
    $("<p>").text("No results.").appendTo("#serviceDirectoryResultsInner");
}

function validateServiceDir() {
    // Make sure a location is entered
    if ($("#chkServiceOutsideTheUS:checked")[0]) {
        clearError();
        return true;
    }

    // Make sure a five-digit US zip code was entered.
    var re = /^\d{5}([\-]\d{4})?$/;
    var entry = $("#ddlServiceState option:selected").val();
    if (entry != "") {
        clearError();
        return true;
    }

    // Mark the ZIP code section and return false
    $("#serviceDirectory .purchaseEquipmentHeading:first").addClass("error");
    alert("Please choose a State or mark 'Outside the U.S.'");
    return false;
}

function setServiceDirectoryResultHeading() {
    if ($("#chkServiceOutsideTheUS:checked")[0]) {
        $("<h3>International Service Centers</h3>").appendTo("#serviceDirectoryResultsInner");
    }
    else if ($("#ddlServiceState option:selected").val() != "") {
        $("<h3>").text($("#ddlServiceState option:selected").html()).appendTo("#serviceDirectoryResultsInner");
    }
}

// ------------------------------------------
// CONTACT US FORM
// ------------------------------------------

function wireUpContactUsForm() {
    // 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");
        }
        else {
            $("#State").attr("disabled", "disabled").val("-1");
            $("#ZipCode").attr("disabled", "disabled").val("");
            $("#RequestType").val("International Support");
        }
    });

    // Validate on submit
    $("#btnSubmit").click(function() { return validateContactUs(); });
}

function highlightCell(ctrlid) {
    $("#" + ctrlid).parent("td").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 {
        // Request Type is required
        if ($("#RequestType").val() == "-1") {
            highlightCell("RequestType");
            reqfields = false;
        }

        // First Name is required
        if ($("#FirstName").val().trim() == "") {
            highlightCell("FirstName");
            reqfields = false;
        }

        // Last Name is required
        if ($("#LastName").val().trim() == "") {
            highlightCell("LastName");
            reqfields = false;
        }

        // Company is required
        if ($("#Company").val().trim() == "") {
            highlightCell("Company");
            reqfields = false;
        }

        // Company Address is required
        if ($("#CompanyAddress").val().trim() == "") {
            highlightCell("CompanyAddress");
            reqfields = false;
        }

        // City is required
        if ($("#City").val().trim() == "") {
            highlightCell("City");
            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() == "-1") {
            highlightCell("Country");
            reqfields = false;
        }

        // Telephone Number is required
        if ($("#Phone1").val().trim() == "" || $("#Phone2").val().trim() == "" || $("#Phone3").val().trim() == "") {
            highlightCell("Phone1");
            reqfields = false;
        }

        // E-mail Address is required
        if ($("#EmailAddress").val().trim() == "") {
            highlightCell("EmailAddress");
            reqfields = false;
        }

        if (!reqfields)
            alert("One or more required fields were not filled out.");

        return reqfields;
    }
    catch (ex) {
        return false;
    }
}