﻿var currentBoxNumber

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;
            }
        }
    }

    $('#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();

    /** this little chunk makes the tables light up when you hover  **/
    /*$('#compareTable tbody tr').hover(
    function() {
    $(this).addClass("tableHover");
    },
    function() {
    $(this).removeClass("tableHover");
    }
    ); */

    //** 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');
            }
        }
    });

}

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 };
    };
}
