﻿function validateSearch(text) {
    if (text.length < 3) {
        alert("The search text must be at least 3 characters long.");
        return false;
    }
    return true;
}
function changeSearchType(type) {
    var form = document.getElementById('siteSearch');

    switch (type) {
        case "manufacturers":
            form.action = "../ManufacturerSearch.aspx";
            break;
        case "partnumber":
        case "description":
        case "products":
            form.action = "../ProductSearch.aspx";
            break;
        case "inventory":
        default:
            form.action = "../InventorySearch.aspx";
            break;
    }
    setCookie("searchtype", type);
    clearSelectedSearch();

    var image = document.createElement("IMG");
    image.src = "/content/images/arrowhead-yellow.png";
    var p = document.getElementById(type + 'Search');

    p.insertBefore(image, p.firstChild);
}
function clearSelectedSearch() {
    var searchwidgetoptions = document.getElementById('searchwidgetoptions');
    var arrows = searchwidgetoptions.getElementsByTagName('img');

    for (var i = 0; i < arrows.length; ++i) {
        arrows[0].parentNode.removeChild(arrows[0]);
    }
}
function initSearchType() {
    var type = getCookie("searchtype");
    if (type == "") {
        type = "inventory";
    }
    changeSearchType(type);



    $('#SiteSearchButton').click(function () {

        var st = getCookie("searchtype");

        if (st == "") {
            st = "inventory";
        }

        if (st == "inventory" || st == "partnumber")
            $("#SiteSearchText").val(stripNonAlphaChars($("#SiteSearchText").val()));

        return validateSearch($("#SiteSearchText").val());
    });
}

function initInventorySearchButton() {

    $("#InventorySearchButton").click(function () {
        $("#InventorySearchText").val(stripNonAlphaChars($("#InventorySearchText").val()));
    });
}

function stripNonAlphaChars(text) {
    text = text.replace(/[^a-zA-Z0-9]+/g, '');
    return text;
}
function initComparison() {
    var a = $('.alert');

    if (a.html() != null) {
        var html = trimEmptySpace(a.html());
        if (html.length > 0) {
            a.fadeOut(3000, function () {
                // Animation complete.
            });
        }
        else {
            a.fadeOut(0);
        }
    }

    $('#loginModal').jqm({
        modal: true
    });
}
function initManufacturerSearch() {
    createManufacturerSearchDropDown('', '');
    $('#manufacturerSearchText').autocomplete("../Lookup.asmx/SearchManufacturers",
        {
            extraParams: { startRecord: 1 },
            parse: function (data) {
                var parsed = [];

                $(data).find("Manufacturers").find("Manufacturer").each(function () {
                    parsed[parsed.length] = {
                        data: [$(this).find("Name").text()],
                        value: $(this).find("ManufacturerId").text(),
                        result: [$(this).find("Name").text()]
                    };
                });

                return parsed;
            },
            max: 50,
            dataType: "xml"
        }
    );

    $('#manufacturerSearchText').result(function (event, data, formatted) {
        createManufacturerSearchDropDown(data, formatted);
        $('#manufacturerSearchText').val('');
    });
}
function createManufacturerSearchDropDown(newOptionText, newOptionValue) {
    if (newOptionText != '' && newOptionValue != '') {
        $('.selectedManufacturers').dropdownchecklist("destroy");
        $('.selectedManufacturers').append('<option value="' + newOptionValue + '|'+ newOptionText +'" selected="selected">' + newOptionText + '</option>');
    }
    $('.selectedManufacturers').dropdownchecklist({ width: 390, icon: {}, emptyText: "Manufacturer: Unfiltered" });
}
function changePartToComparison(checkbox) {
    var numPartsToCompare = getNumberOfPartsToCompare();

    if (numPartsToCompare > 10) {
        alert("You may only compare up to ten parts at a time!");
        checkbox.checked = !checkbox.checked;
    }
}
function validateComparison(isLoggedIn) {
    var numPartsToCompare = getNumberOfPartsToCompare();

    if (numPartsToCompare < 2) {
        alert("Please choose at least two parts to be compared!");
        return false;
    }

    if (!isLoggedIn) {
        $('#loginModal').jqmShow();
        return false;
    }

    return true;
}
function getNumberOfPartsToCompare() {
    return $("input[id*=_ckbPart]:checked").length + $("input[id*=_ckbPart]:hidden").length;
}
function getCookie(name) {
    var search = name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        // if cookie exists
        if (offset != -1) {
            offset += search.length
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset);
            // set index of end of cookie value
            if (end == -1) end = document.cookie.length;
            returnvalue = unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}
function setCookie(name, value) {
    document.cookie = name + "=" + value;
}
function trimEmptySpace(value) {
    value = value.replace(/^\t*/, "").replace(/\t*$/, "");
    value = value.replace(/^\s*/, "").replace(/\s*$/, "");
    value = value.replace(/^\r*/, "").replace(/\r*$/, "");
    return value;
}
