﻿// JScript File

function pageLoad() {
    /** Make the tabs work **/
    $("#tabs1").tabs();
    $("#tabs2").tabs();
    $("#productTabs").tabs();

    $('.error').hide();

    var $btn = $('.form_submit');
    var $form = $btn.parents('.form');
    $form.keydown(function (e) {
        if (e.which == 13 && e.target.type != 'textarea') {
            if ($btn[0].type == 'submit')
                $btn[0].click();
            else
                eval($btn[0].href);
            return false;
        }
    });

    var $qoButton = $('.qoButton');
    var textboxes = $("input.PartNumber, input.Quantity, input.BulkPartNumber, input.BulkQuantity");
    if ($.browser.mozilla) {
        $(textboxes).keypress(checkForEnter);
    } else {
        $(textboxes).keydown(checkForEnter);
    }

    function checkForEnter(event) {
        if (event.keyCode == 13) {
            currentBoxNumber = textboxes.index(this);

            if (textboxes[currentBoxNumber + 1] != null) {
                nextBox = textboxes[currentBoxNumber + 1];
                nextBox.focus();
                nextBox.select();

                event.preventDefault();
                return false;
            }
            else {
                eval($qoButton[0].href);
                event.preventDefault();
                return false;
            }
        }
    }

    $('#ctl00_inclLeftMargin_btnSearch').click(function () {
        var keywords = $("input#ctl00_inclLeftMargin_txtKeywords").val();
        var partNum = $("input#ctl00_inclLeftMargin_txtPartNumber").val();

        if (keywords == '' || keywords == 'Enter keyword(s)') {
            if (partNum == '' || partNum == 'Enter manufacturer part number') {
                alert('Please enter a part number or keyword to search');
                return false;
            }
        }
    });

    $('#btnContact').click(function () {
        $('.error').hide();

        //Validate and process form here
        var isValid = true;
        isValid = IsValid('question', 'textarea', isValid);
        isValid = IsValid('phone', 'input', isValid);
        isValid = IsValid('email', 'input', isValid);
        isValid = IsValid('company', 'input', isValid);
        isValid = IsValid('name', 'input', isValid);

        $('input[title!=""]').hint();
        $('textarea[title!=""]').hint();

        if (!isValid) { return false }
        else {
            var name = $("input#name").val();
            var company = $("input#company").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var question = $("textarea#question").val();

            $("#btnContact").removeClass("right button");
            $("#btnContact").empty().html('<img src="/media/ajax-loader.gif" />');

            $.ajax({
                type: "POST",
                url: "/UtilityService.asmx/SendContactEmail",
                data: "{'name':'" + name + "','company':'" + company + "','email':'" + email + "','phone':'" + phone + "','question':'" + question + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#ContactResult").addClass("ContactSuccess");
                    $('#ContactResult').show('normal', function () {
                        $('#ContactResult').fadeOut(7500)
                    });

                    $("#btnContact").addClass("right button");
                    $("#btnContact").empty().html('<span>Send Message</span>');
                }
            });


            return false;
        };
    });

    //** Make the text box hits work **//
    $('input[title!=""]').hint();
    $('textarea[title!=""]').hint();

    //** make the popupwork  **//
    $(".infoLink").fancybox();
    $(".group").fancybox();

//    $("#tree").treeview({
//        collapsed: true,
//        animated: "medium",
//        persist: "location"
//    });

    $("input.PartNumber").blur(function () {
        var txtPartNumber = $(this);
        var partNumber = txtPartNumber.val();

        if (partNumber != '' & partNumber != 'Part Number') {
            //alert(txtPartNumber.attr('id'));
            //alert(txtPartNumber.attr('id').replace('ctl00_inclLeftMargin_txtPart', ''));
            //alert("#ctl00_inclLeftMargin_txtResult" + txtPartNumber.attr('id').replace('ctl00_inclLeftMargin_txtPart', ''));
            var lblResult = $("#result" + txtPartNumber.attr('id').replace('ctl00_inclLeftMargin_txtPart', ''));
            var results = '<span style="color:green; font-size:11px;">Found It</span>'

            $.ajax({
                type: "POST",
                url: "/UtilityService.asmx/CheckPartNumber",
                data: "{'partNumber':'" + partNumber + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    //alert(txtPartNumber.attr('id'));
                    //alert(lblResult.attr('id'));
                    //alert(msg.d);
                    if (msg.d == 'false') {
                        results = '<span style="color:red; font-size:11px;">Not Found</span>';
                    }

                    lblResult.html(results);
                    lblResult.show();

                }
            });
        }
    });

    $("input.Quantity").blur(function () {
        var txtQty = $(this);
        var qty = txtQty.val();

        if (qty != '' & qty != 'Qty') {
            if (qty.match(/[^\d]/) != null) {
                alert('Quantity must be a number');
            }
        }
    });

    $("input.BulkPartNumber").blur(function () {
        var txtPartNumber = $(this);
        var partNumber = txtPartNumber.val();

        if (partNumber != '' & partNumber != 'Part Number') {
            //alert(txtPartNumber.attr('id'));
            //alert(txtPartNumber.attr('id').replace('ctl00_ContentPlaceHolder1_txtPart', ''));
            //alert("#ctl00_ContentPlaceHolder1_lblResult" + txtPartNumber.attr('id').replace('ctl00_ContentPlaceHolder1_txtPart', ''));
            var lblResult = $("#ctl00_ContentPlaceHolder1_lblResult" + txtPartNumber.attr('id').replace('ctl00_ContentPlaceHolder1_txtPart', ''));
            var results = '<span style="color:green; font-size:12px;">Found It</span>'

            $.ajax({
                type: "POST",
                url: "/UtilityService.asmx/GetPartInfo",
                data: "{'partNumber':'" + partNumber + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    //alert(txtPartNumber.attr('id'));
                    //alert(lblResult.attr('id'));
                    //alert(msg.d);
                    if (msg.d == 'false') {
                        results = '<span style="color:red; font-size:12px;">Not Found</span>';
                    }
                    else {
                        results = '<span style="color:green; font-size:12px;">' + msg.d + '</span>';
                    }

                    lblResult.html(results);
                    lblResult.show();

                }
            });
        }
    });

    $("input.BulkQuantity").blur(function () {
        var txtQty = $(this);
        var qty = txtQty.val();

        if (qty != '' & qty != 'Qty') {
            if (qty.match(/[^\d]/) != null) {
                alert('Quantity must be a number');
            }
        }
    });

    $('#ctl00_ContentPlaceHolder1_btnPlaceOrder').click(function () {
        var shipPayment = $('#ctl00_ContentPlaceHolder1_ddlShipPayment').val();
        var shipAccountId = $('#ctl00_ContentPlaceHolder1_txtShipAcctNumber').val();
        var poNumber = $('#ctl00_ContentPlaceHolder1_txtPoNumber').val();
        var packingBasis = $('#ctl00_ContentPlaceHolder1_ddlPackingBasis').val();
        var paymentMethod = $('#ctl00_ContentPlaceHolder1_ddlPaymentMethod').val();
        var terms = $('#ctl00_ContentPlaceHolder1_chkUserAgreement').is(':checked');
        var sId = $('#ctl00_ContentPlaceHolder1_hdnSid').val();
        var popup = $('#ctl00_ContentPlaceHolder1_hdnPopupRedirect').val();

//        alert(shipPayment);
//        alert(shipAccountId);
//        alert(poNumber);
//        alert(packingBasis);
//        alert(paymentMethod);
//        alert(terms);
//        alert(sId);

        if (paymentMethod == 'credit card' && popup == 'popup') {
            $.ajax({
                type: "POST",
                url: "/UtilityService.asmx/ValidateCheckout",
                data: "{'sId':'" + sId + "','shipPayment':'" + shipPayment + "','shipAccountId':'" + shipAccountId + "','poNumber':'" + poNumber + "','packingBasis':'" + packingBasis + "','paymentMethod':'" + paymentMethod + "','terms':'" + terms + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    if (msg.d == 'true') {
                        DoPaymentPopup();
                        return false;
                    }
                    else {
                        return false;
                    }
                },
                error: function (req, status, ex) {
                    //alert(status);
                    //alert(ex);
                    return false;
                }
            });
        }
        else {
            return true;
        }
    });

    $('#btnCompare').click(function () {
        var numChecked = $("input:checkbox:checked").length;
        if (numChecked < 2) {
            alert('Please check at least 2 items to compare.');
        }
        else {
            if (numChecked > 3) {
                alert('You can compare a maximum of 3 items.');
            }
            else {
                var url = '/compare.aspx?id=';
                var params = '';
                var checked = $("input:checkbox:checked");
                checked.each(function (i) {
                    params = params + $(this).attr("class") + ',';
                });
                window.location = url + params;
            }
        }
        return false;
    });

    $('#btnCompareRelated').click(function () {
        var url = '/compareRelated.aspx?id=';
        var originalItemId = $("#ctl00_ContentPlaceHolder1_hdnItemId").val();
        var compareItemId = $("input:radio:checked").attr("class");
        var params = originalItemId + ',' + compareItemId;
        window.location = url + params;
        return false;
    });

    $(":checkbox").click(function () {
        var numChecked = $("input:checkbox:checked").length;
        var unchecked = $("input:checkbox:not(:checked)");

        if (numChecked >= 3) {
            unchecked.each(function (i) {
                $(this).attr("disabled", "disabled");
            });
        }
        else {
            unchecked.each(function (i) {
                $(this).removeAttr("disabled");
            });
        }
    });
}

function DisplayObject(name)
{

    if (typeof name != "undefined") {
        var obj = document.getElementById(name);
        obj.style.visibility="visible";
        obj.style.display="block";
    }
}

function DisplayObjectByType(name, displayType)
{
    try {
        if (typeof name != "undefined") {
            var obj = document.getElementById(name);
            obj.style.visibility="visible";
            obj.style.display=displayType;
        }
    }
    catch(err) {
    }
}

function HideObject(name)
{
    try {
        var obj = document.getElementById(name);
        //alert(name);
        //alert(typeof obj);
        if (typeof obj != "undefined") {
            //alert('not undefined');
            //var obj = document.getElementById(name);

            if (obj.style.display=="block" || obj.style.display=="") {
                //alert('hiding via display');
                obj.style.display="none";
            }
            else {
                //alert('hiding via visibility');
                obj.style.visibility="hidden";
            }
        }
    }
    catch(err) {
    }
}

function ToggleDisplay(name)
{

    if (typeof name != "undefined") {
        var obj = document.getElementById(name);

        if (obj.style.display=="block" || obj.style.display=="") {
            obj.style.display="none";
        }
        else {
            if (obj.style.display=="inline") {
                obj.style.visibility="hidden";
            }
            else {
                obj.style.display="block";
                obj.style.visibility="visible";
            }
        }
    }
}

function ChangeLinkText(name, newText) {
    if (typeof name != "undefined") {
        var obj = document.getElementById(name);
        obj.innerHTML = newText;
    }
}

function ToggleShowPassword(me) {
    var objShow = document.getElementById('ctl00_ContentPlaceHolder1_chkCreateAccount');
    var tRow = document.getElementById('tPassword');
    var tRow2 = document.getElementById('tCheckExistingID');
    var tRow3 = document.getElementById('tExistingID');
    
    if (objShow.checked == true) {
        tRow.style.display = '';
        tRow2.style.display = '';
    }
    else {
        tRow.style.display = 'none';
        tRow2.style.display = 'none';
        tRow3.style.display = 'none';
    }
}

function ToggleShowExistingCustomerId(me) {
    var objShow = document.getElementById('ctl00_ContentPlaceHolder1_chkExistingAccount');
    var tRow = document.getElementById('tExistingID');
    
    if (objShow.checked == true) {
        tRow.style.display = '';
    }
    else {
        tRow.style.display = 'none';
    }
}

function PopulateShippingAddress() {
    var objCopy = document.getElementById('ctl00_ContentPlaceHolder1_chkSameAsBilling');
    var billCompany = document.getElementById('ctl00_ContentPlaceHolder1_txtBillCompanyName');
    var billAddress1 = document.getElementById('ctl00_ContentPlaceHolder1_txtBillAddress1');
    var billAddress2 = document.getElementById('ctl00_ContentPlaceHolder1_txtBillAddress2');
    var billCity = document.getElementById('ctl00_ContentPlaceHolder1_txtBillCity');
    var billState = document.getElementById('ctl00_ContentPlaceHolder1_ddlBillState');
    var billZip = document.getElementById('ctl00_ContentPlaceHolder1_txtBillZip');
    var billCountry = document.getElementById('ctl00_ContentPlaceHolder1_ddlBillCountry');
    var shipCompany = document.getElementById('ctl00_ContentPlaceHolder1_txtShipCompanyName');
    var shipAddress1 = document.getElementById('ctl00_ContentPlaceHolder1_txtShipAddress1');
    var shipAddress2 = document.getElementById('ctl00_ContentPlaceHolder1_txtShipAddress2');
    var shipCity = document.getElementById('ctl00_ContentPlaceHolder1_txtShipCity');
    var shipState = document.getElementById('ctl00_ContentPlaceHolder1_ddlShipState');
    var shipZip = document.getElementById('ctl00_ContentPlaceHolder1_txtShipZip');
    var shipCountry = document.getElementById('ctl00_ContentPlaceHolder1_ddlShipCountry');

    if (objCopy.checked == true) {
        //shipCompany.value = billCompany.value;
        shipAddress1.value = billAddress1.value;
        shipAddress2.value = billAddress2.value;
        shipCity.value = billCity.value;
        shipState.value = billState.value;
        shipZip.value = billZip.value;
        shipCountry.value = billCountry.value;
    }
}

function CopyFirstName() {
    fname = document.getElementById('ctl00_ContentPlaceHolder1_txtFirstName');
    lname = document.getElementById('ctl00_ContentPlaceHolder1_txtLastName');
    contact = document.getElementById('ctl00_ContentPlaceHolder1_txtShipContactName');

    if (fname.value == contact.value) {}
    else {
        contact.value = fname.value;
    }
}

function CopyName() {
    var fname = $('#ctl00_ContentPlaceHolder1_txtFirstName');
    var lname = $('#ctl00_ContentPlaceHolder1_txtLastName');
    var contact = $('#ctl00_ContentPlaceHolder1_txtShipContactName');
    //fname = document.getElementById('ctl00_ContentPlaceHolder1_txtFirstName');
    //lname = document.getElementById('ctl00_ContentPlaceHolder1_txtLastName');
    //contact = document.getElementById('ctl00_ContentPlaceHolder1_txtShipContactName');

    if (contact.val() == fname.val() + ' ' + lname.val()) {}
    else {
        contact.val(fname.val() + ' ' + lname.val());
    }
}

function ToggleShowCreditCard(me) {
    var objDropDown = document.getElementById('ctl00_ContentPlaceHolder1_ddlPaymentMethod');
    var objPopupRedirect = document.getElementById('ctl00_ContentPlaceHolder1_hdnPopupRedirect');
    var objShow = document.getElementById('ctl00_ContentPlaceHolder1_ddlPaymentMethod');
    var tRow = document.getElementById('ctl00_ContentPlaceHolder1_tCreditCard');
    var tPCI = document.getElementById('ctl00_ContentPlaceHolder1_tPCIExplanation');
    var objPO = document.getElementById('ctl00_ContentPlaceHolder1_txtPoNumber');
    var objTerms = document.getElementById('ctl00_ContentPlaceHolder1_chkUserAgreement');

    if (objShow.value == 'credit card') {
        if (objPopupRedirect.value == 'popup') {
            tPCI.style.display = '';
            //DoPaymentPopup();
        }
        else {
            tRow.style.display = '';
            $('#ctl00_ContentPlaceHolder1_poRequired').hide();
            return false;
        }
    }
    else {
        tPCI.style.display = 'none';
        //tRow.style.display = 'none';
        //$('#ctl00_ContentPlaceHolder1_poRequired').show();
        //return false;
    }
}

function ToggleShowShipAccount(me) {
    var objShow = document.getElementById('ctl00_ContentPlaceHolder1_ddlShipPayment');
    var objAcctNum = document.getElementById('dShipAcctNumber');

    if (objShow.value == '1') {
        objAcctNum.style.display = '';
    }
    else {
        objAcctNum.style.display = 'none';
    }
}

function IsValid(fieldname, ftype, isvalid) {
    var field = $(ftype + "#" + fieldname)
    var val = field.val();
    var valDefault = field.attr("title");
    if (val == "" || val == valDefault) {
        var errLbl = $("label#" + fieldname + "_error");
        errLbl.show();
        field.focus();
        return false
    }
    else {
        if (isvalid == false) { return false } else { return true };
    };
}

function scrollTop() {
    window.document.body.scrollTop = 0;
    window.document.documentElement.scrollTop = 0;
}    

