
$(document).ready(function () {
    $("form").submit(function () {
        return false;
    });
    $("#searchjobs").click(function () {
        $(this).val("");
    });
    $("#searchjobs").blur(function () {
        if ($(this).val() == "")
            $(this).val("Type city here");
    });
    $("#zipcode").click(function () {
        $(this).val("");
    });
    $("#zipcode").blur(function () {
        if ($(this).val() == "")
            $(this).val("enter zip code here");
    });
    $("#zipsearch").click(function () {
        var v = $("#zipcode").val();
        if (v != null && v.toString().length > 0 && v != "enter zip code here")
            window.location.href = "/jobs/job-results.aspx?z=" + v;
    });
    $("#zipcode-btn").click(function () {
        var v = $("#zipcode").val();
        if (v != null && v.toString().length > 0 && v != "enter zip code here")
            window.location.href = "/jobs/job-results.aspx?z=" + v;
    });
    $("#searchjobs").focus(function () {
        $(this).val("");
    });
    $("#searchjobs").keyup(function () {
        window.location.href = "/jobs/job-results.aspx?c=" + $("#searchjobs").val();
    });
    $("#offices_txtCityOfficeSearch").focus(function () {
        $(this).val("");
        SuggestCityOffices(); // this will cause any current list to disappear
    });
    $("#offices_txtCityOfficeSearch").blur(function () {
        if ($(this).val() == "")
            $(this).val("Type location here");
    });


    $("#jobs_txtCityJobSearch").focus(function () {
        $(this).val("");
        $("#cityIsSpecific").val("0"); // reset citsIsSpecific to default value
        $("#cityGeo").val(""); // clear geography value
        SuggestCityJobs(); // this will cause any current list to disappear
    });
    $("#jobs_txtCityJobSearch").blur(function () {
        if ($(this).val() == "")
            $(this).val("City, State");
    });
    $("#jobs_cmdCityJobSearch").click(function () {
        var c, s, cs; var cis = $("#cityIsSpecific").val(); var geo = $("#cityGeo").val();
        c = s = cs = "";
        if ($.trim($("#jobs_txtCityJobSearch").val()) != "" && $("#jobs_txtCityJobSearch").val() != "City, State") {
            if ($("#jobs_txtCityJobSearch").val().indexOf(",") >= 0) {
                c = $("#jobs_txtCityJobSearch").val().split(", ")[0].toString();
                s = $("#jobs_txtCityJobSearch").val().split(", ")[1].toString().replace(/\x5B.*\x5D/, ""); // replace[#] with "" = remove [#]
            }
            cs = $("#jobs_txtCityJobSearch").val();
            window.location.href = "/jobs/job-results.aspx?c=" + htmlEncode(c) + "&s=" + htmlEncode(s) + "&cs=" + htmlEncode(cs) + "&cis=" + cis + "&geo=" + geo;
        }
    });
    


    // #callnumber and #callbtn are located at the top of every page "Questions? Call Us 1.877.404.8458 or we'll call you: 
    $("#callnumber").blur(function () {
        if ($(this).val() == "")
            $(this).val("Enter Number");
    });
    $("#callnumber").click(function () {
        $(this).val("");
    });
    $("#callnumber").focus(function () {
        $(this).val("");
    });
    $("#callbtn").click(function (event) {
        event.preventDefault();
        if ($("#callnumber").val().replace("(", "").replace(")", "").replace(" ", "").replace("-", "").replace(".", "").trim().length === 10) {
            CallEmBack();
        }
    });




    /*
    * -- SEE CityJobSearch.ashx - GenCityList() --
    * $(".suggest_link").click(){};
    * $(".suggest_link").mouseenter(){};
    * $(".suggest_link").mouseleave(){};
    */

    SuggestCityJobs();
});

function cityJobSearchClicked() {
    var cs = $("#jobs_txtCityJobSearch").val();
    var geo = $("#cityGeo").val()
    if(geo.indexOf('|') != -1) {
        window.location.href = "/jobs/job-results.aspx?geo=" + geo + "&cs=" + cs;
    }
}

var doThing = true; 
function SuggestCityJobs(v) {
    if (!doThing) { return; }
    if ((v != null) && (v.toString().length >= 2) && (v.toString() != "City, State" && v != "")) {
        doThing = false; 
        $.ajax({
            type: "POST",
            url: "CityJobSearch.ashx",
            data: { suggest: v },
            dataType: "html",
            success: function (html) {
                setSuggestedCityJobs(html);
                $("#jobs_SuggestedCityJobs").show();
            },
            complete: function () {
                doThing = true;
            }
        });
    }
    else {
        setSuggestedCityJobs("");
        $("#jobs_SuggestedCityJobs").hide();
    }
}

function setSuggestedCityJobs(html) {
    $("#jobs_SuggestedCityJobs").html(html);
}

function htmlEncode(value) {
    if ((value != typeof(undefined)) && (value != null) && (value != "")) {
        return htmlEscape($('<div/>').text(value).html());
    }
    else {
        return "";
    }
}

function htmlDecode(value) {
    return $('<div/>').html(value).text();
}

function htmlEscape(str) {
    return String(str)
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
} 

function scrollToCity() {
    var urlParams = getUrlParams();
    if ((urlParams["c"] != null) && (urlParams["c"].toString().trim().length > 0)) {
        if ((urlParams["t"] != null) && (urlParams["t"].toString().trim().length > 0)) {
            var target = "#" + urlParams["t"] + "_" + urlParams["c"];
            $('html,body').animate({ scrollTop: $(target).offset().top - 60 }, 2000);
        }
    }
}

function getUrlParams() {
    var urlParams = {};
    (function () {
        var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space 
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

        while (e = r.exec(q)) {
            urlParams[d(e[1])] = d(e[2]);
        }
        if (!urlParams["redir"]) {
            urlParams["redir"] = window.location.href;
        }
    })();
    return urlParams;
}

function SuggestCityOffices() {
    var v = $("#offices_txtCityOfficeSearch").val();
    $(".officeListItem").each(function() {
        if (v != "" && v.length > 0 && v != "Type city here") {
            if ($(this).find("a").html().toLowerCase().indexOf(v.toLowerCase()) != -1) {
                $(this).show();
            } else { $(this).hide(); } 
        } else { $(this).hide(); } 
    }); 
}

function CallEmBack() {
    $.ajax({
        type: "POST",
        url: "CallBack.ashx",
        data: { cbn: encodeURI($("#callnumber").val()) },
        dataType: "html",
        complete: function () {
            window.location.href = "/thank-you.aspx?cn=" + encodeURI($("#callnumber").val());
        }
    });
}

function queryString(p) {
    var match = "noMatch";
    var ss = window.location.search.substring(1);
    var gy = ss.split("&");
    for (var i = 0; i < gy.length; i++) {
        var ft = gy[i].split("=");
        if (ft[0] == p) {
            return ft[1];
        }
    }
    return match;
}


