﻿jQuery.noConflict();

function showTab(id) {
    hideAllTabs();
    document.getElementById(id).style.display = "block";
    resetAllTabImages();
    document.images["img" + id].src = document.images["img" + id].src.replace("-out", "-on");
}
function hideAllTabs() {
    if (document.getElementById("NewProductsTabContent")) {

        document.getElementById("NewProductsTabContent").style.display = "none";
    }

    if (document.getElementById("SpecialsTabContent")) {

        document.getElementById("SpecialsTabContent").style.display = "none";
    }

    if (document.getElementById("CloseoutsTabContent")) {

        document.getElementById("CloseoutsTabContent").style.display = "none";
    }

    if (document.getElementById("AccessoriesTabContent")) {

        document.getElementById("AccessoriesTabContent").style.display = "none";
    }

    if (document.getElementById("SpecificationsTabContent")) {

        document.getElementById("SpecificationsTabContent").style.display = "none";

    }
    if (document.getElementById("DiagramsTabContent")) {

        document.getElementById("DiagramsTabContent").style.display = "none";
    }

}
function resetAllTabImages() {
    if (document.getElementById("NewProductsTabContent")) {
        document.images["imgNewProductsTabContent"].src = "/images/tab-whats-new-out.png";
        document.images["imgSpecialsTabContent"].src = "/images/tab-specials-out.png";
        document.images["imgCloseoutsTabContent"].src = "/images/tab-closeouts-out.png";
    }
    if (document.getElementById("AccessoriesTabContent")) {
        document.images["imgAccessoriesTabContent"].src = "/images/tab-accessories-out.png";
        document.images["imgSpecificationsTabContent"].src = "/images/tab-specifications-out.png";
        document.images["imgDiagramsTabContent"].src = "/images/tab-diagrams-out.png";
    }
}
function search() {
    var query = document.forms["productSearch"].elements["query"].value;
    location.href = "/search/default.aspx?q=" + escape(query);
    return false;
}
function advancedSearch() {
    var query = document.forms["advancedSearch"].elements["query"].value;
    //location.href = "/search/default.aspx?q=" + escape(query);
    return false;
}

jQuery.ajaxSetup({ cache: false }); // this is needed to avoid cache in IE 

function addToCart(customerid, productid, orderid, qtyboxid, verifyInventory) {
    var cid = customerid;
    var pid = productid;
    var oid = orderid;
    var qty = jQuery("#" + qtyboxid).val();
    if (qty == "" || qty == null || qty == "0") {
        qty = "1";
    }

    if (verifyInventory == 'true') {
        var realTimeQtyOnHand = jQuery.ajax({
            url: "/AjaxHandlers/getRealTimeQtyOnHand.ashx?ProductId=" + pid,
            async: false
        }).responseText;

        if (realTimeQtyOnHand == 0) {
            var applicationMessage = jQuery.ajax({
                url: "/AjaxHandlers/getApplicationMessage.ashx?ApplicationMessage=ProductQuantityNotOnHand",
                async: false
            }).responseText;
            alert(applicationMessage);
        }
    }    

    var data = jQuery.ajax({
        url: "/AjaxHandlers/SaveItem.ashx?Where=Cart&CustomerID=" + cid + "&ProductID=" + pid + "&OrderID=" + oid + "&Quantity=" + qty,
        async: false
    }).responseText;

    jQuery("#spanCart").html(getCount('Cart', orderid));
    jQuery('#cartItems').html(getItems(customerid, orderid));
    showStatusMessage("cart", data);
}

function addToFavorites(wishlistid, productid, qtyboxid) {
    var wid = wishlistid;
    var pid = productid;
    var qty = jQuery("#" + qtyboxid).val();
    if (qty == "" || qty == null || qty == "0") {
        qty = "1";
    }

    var data = jQuery.ajax({
        url: "/AjaxHandlers/SaveItem.ashx?Where=Favorites&ProductID=" + pid + "&WishListID=" + wid,
        async: false
    }).responseText;
    jQuery('#spanFavorites').html(getCount('Favorites', wishlistid));
    showStatusMessage("favorites", data);
}

function getCount(from, id) {
    var html = '';

    html += jQuery.ajax({
        url: "/AjaxHandlers/getCount.ashx?From=" + from + "&ID=" + id,
        async: false
    }).responseText;

    return html;
}

function getItems(customerid, orderid) {
    var html = '';
    
    if (orderid != null) {        
        html += jQuery.ajax({
            url: "/AjaxHandlers/getItems.ashx?CustomerID=" + customerid + "&OrderID=" + orderid,
            async: false
        }).responseText;

        if (html.toString().search('Your cart is currently empty.') != -1) {
            jQuery('#cartlinks').css("display", "none");
        }
        else {
            jQuery('#cartlinks').css("display", "block");
        }        
    }

    return html;
}

function confirmRemove(customerid, orderid, orderlineid, desc) {

    var confirmMessage = "Are you sure you want to remove " + desc + " from your cart?";
    if (confirm(confirmMessage.toString())) {
        removeItem(customerid, orderid, orderlineid);
    }
}

function removeItem(customerid, orderid, orderlineid) {

    var message = jQuery.ajax({
        url: "/AjaxHandlers/removeItem.ashx?OrderID=" + orderid + "&OrderLineID=" + orderlineid,
        async: false
    }).responseText;

    if (message.toString() == 'done') {

        jQuery("#spanCart").html(getCount('Cart', orderid));
        jQuery('#cartItems').html(getItems(customerid, orderid));
    }
}

function showStatusMessage(msgType, msg) {

    switch (msgType) {
        case "cart":
            document.getElementById("statusMessage").style.width = "284px";
            document.getElementById("statusMessage").style.backgroundImage = "url(/images/bg-added-to-cart.png)";
            break;
        case "favorites":
            document.getElementById("statusMessage").style.width = "343px";
            document.getElementById("statusMessage").style.backgroundImage = "url(/images/bg-added-to-favorites.png)";
            break;
    }

    var html = msg;
    document.getElementById("statusMessage").innerHTML = html;
    var t = setTimeout("hideStatusMessage()", 3000);
    document.getElementById("statusMessage").style.display = "inline";

}

function hideStatusMessage() {
    document.getElementById("statusMessage").style.display = "none";
}



function openExpandedCart() {
    changeProductThumbsClass("productThumb", "dummy");
    document.getElementById("expandedCartContainer").style.visibility = "visible";
}
function closeExpandedCart() {
    changeProductThumbsClass("dummy", "productThumb");
    document.getElementById("expandedCartContainer").style.visibility = "hidden";
}

function openExpandedSearch() {
    changeProductThumbsClass("productThumb", "dummy");
    document.getElementById("expandedSearch").style.visibility = "visible";
}
function closeExpandedSearch() {
    changeProductThumbsClass("dummy", "productThumb");
    document.getElementById("expandedSearch").style.visibility = "hidden";
}

function openBrowseProducts() {
    changeProductThumbsClass("productThumb", "dummy");
    document.getElementById("browseProductsContainer").style.visibility = "visible";
}
function closeBrowseProducts() {
    changeProductThumbsClass("dummy", "productThumb");
    document.getElementById("browseProductsContainer").style.visibility = "hidden";
}
function changeProductThumbsClass(currentClass, newClass) {
    var items = document.getElementsByTagName("A");
    for (i = 0; i < items.length; i++) {
        if (items[i].className == currentClass) {
            items[i].className = newClass;
        }
    }
}
function bookMarkPage(title) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, location.href, "");
    } else if (window.external) {
        window.external.AddFavorite(location.href, title);
    }
}

var phone_field_length = 0;
function TabNext(obj, event, len, next_field) { if (event == "down") { phone_field_length = obj.value.length; } else if (event == "up") { if (obj.value.length != phone_field_length) { phone_field_length = obj.value.length; if (phone_field_length == len) { next_field.focus(); } } } }

function showSubMenu(id) {
    hideAllSubMenus();
    changeProductThumbsClass("productThumb", "dummy");
    document.getElementById(id).style.visibility = "visible";
}

function hideAllSubMenus() {
    if (document.getElementById("AboutUs")) {
        document.getElementById("AboutUs").style.visibility = "hidden";
//        if (document.getElementById("NewsAndEvents")) {
//            document.getElementById("NewsAndEvents").style.visibility = "hidden";
//        }
    }
    if (document.getElementById("Account")) {
        document.getElementById("Account").style.visibility = "hidden";

//        if (document.getElementById("ToolsAndResources")) {
//            document.getElementById("ToolsAndResources").style.visibility = "hidden";
//        }
//        if (document.getElementById("NewsAndEvents")) {
//            document.getElementById("NewsAndEvents").style.visibility = "hidden";
//        }
    }
    if (document.getElementById("ToolsAndResources")) {
        document.getElementById("ToolsAndResources").style.visibility = "hidden";
    }
    if (document.getElementById("NewsAndEvents")) {
        document.getElementById("NewsAndEvents").style.visibility = "hidden";
    }
}