﻿// extra jQuery functions
; (function($) {
    $.fn.toggleBasic = function() {
        $('.swapimg').mouseover(function() { $(this).attr('src', $(this).attr('src').replace('0.', '1.')); })
            .mouseout(function() { $(this).attr('src', $(this).attr('src').replace('1.', '0.')); });

        $('#SearchForm').submit(function() {
            if ($.trim($('#SearchText').val()) == "") {
                alert("Please input search text.");
                return false;
            }
        });

        $('li', '.topNav')
            .mouseover(function() { $(this).children('.dropdown').show(); })
            .mouseout(function() { $(this).children('.dropdown').hide(); });

        //$('a[href*=/Delete/],a[href*=/DeleteItem/]').bind('click', function() { return confirm('Are you sure?'); });
    };

    $.fn.toggleList = function() {
        $('.productPopup').hide();
        $('.quickviewBtn').hide();

        $('.productImage')
            .mouseover(function() { $(this).children('.quickviewBtn').show(); })
			.mouseout(function() { $(this).children('.quickviewBtn').hide(); });

        $('.top', '.productPopup').click(function() { $('.productPopup').slideUp(); });

        $('.quickviewBtn').click(function() {
            $('.productPopup').slideUp();

            $('#popupDetail').load('/common/sub_ProductPopup.php', { key: (new Date()).getTime(), id: $(this).closest('form').children('#ProductID').val() }, function(data) {
                $('img', '.additionalCut').click(function() {
                    $('#mainPopupImage').attr('src', $(this).attr('src').replace('min', 'med'));
                });
            });

            $('.productPopup').slideDown();
        });

        $('input:button', '.qtyBtn').click(function() {
            var qty = $(this).siblings('.productListQty').val();
            //var id = $(this).closest('form').children('#ProductID').val();
            var data = $(this).closest('form').serialize();

            if (qty != '' && qty > 0) {
                $.post('/common/sub_Cart.php', data, function() {
                    $('document').toggleCart('list');
                });
            } else {
                alert('Please input valid quantity.');
                $(this).siblings('.productListQty').focus();
            }
        });

        $('select', '.sort').change(function() { $(this).closest('form').submit(); });
        $('.pageno').click(function() {
            if ($(this).text() == "Next >") {
                $(this).closest('form').children('#page').val(Math.ceil(parseInt($('#page').val()) / 10) * 10 + 1);
            }
            else if ($(this).text() == "< Prev") {
                $(this).closest('form').children('#page').val((Math.ceil(parseInt($('#page').val()) / 10) - 2) * 10 + 1);
            }
            else if ($(this).text() == "View All") {
                $(this).closest('form').children('#page').val(1);
                $(this).closest('form').children('#pagesize').val(1000);
            }
            else {
                $(this).closest('form').children('#page').val($(this).text());
            }

            $(this).closest('form').submit();
        });


        $('.productListQty').inteager();
    }

    $.fn.string2xml = function(strXml) {
        if (window.ActiveXObject) {
            var doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.async = "false";
            doc.loadXML(strXml);

            return doc;
        }
        else { // code for Mozilla, Firefox, Opera, etc.
            var parser = new DOMParser();
            var doc = parser.parseFromString(strXml, "text/xml");

            return doc;
        }
    };

    $.fn.toggleCart = function(cartMode) {
        $('#refreshCart').show();
        $('#cartDetail').load('/common/sub_Cart.php', { key: (new Date()).getTime(), mode: cartMode }, function(data) {
            if ($.trim(data).length > 0) {
                $('#cartDetail').parents('.cart').show();

                var cnt = $('.eachProduct', '#cartDetail').length;
                var displayCnt = cnt % 10;
                if (cnt > 10) {
                    if (displayCnt == 0)
                        displayCnt = 10;

                    $('.eachProduct:lt(' + (cnt - displayCnt) + ')', '#cartDetail').hide();
                }
            }
            else
                $('#cartDetail').parents('.cart').hide();

            $('#refreshCart').hide();
        });
    }

    $.fn.toggleChainList = function() {
        $('#chainListPopup').hide();

        $('#chainListClose').click(function() { $('#chainListPopup').slideUp(); });

        $('.chainListBtn').click(function() {
            $('#chainListPopup').slideUp();
            $('#chainListDetail').children().remove();
            $('#chainListDetail').append("<div class=\"iconLoad\"><img src=\"/images/common/icon_loading.gif\" width=\"64\" height=\"64\" /></div>");

            var y = (window.pageYOffset) ? window.pageYOffset : (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
            $('#chainListPopup').css('top', y + 20);

            $('#chainListDetail').load('/common/sub_ChainList.php', { key: (new Date()).getTime(), ChainType: $(this).closest('form').children('#ChainType').val() }, function(data) { });
            $('#chainListPopup').slideDown();
        });
    }

    $.fn.setLoginValidation = function() {
        $('#EmailAddress,#Passwd').addClass('required');
        $('#EmailAddress,#Passwd').css("width", "200px");

        if ($('#EmailAddress').val() == "")
            $('#EmailAddress').focus();
        else
            $('#Passwd').focus();

        $("#MyForm").validate({
            rules: {
                EmailAddress: {
                    email: true,
                    maxlength: 50
                }
            }
        });
        jQuery.validator.messages.required = " *";
    };

    $.fn.setPaymentValidation = function() {
        $('#ShippingFirstName,#ShippingLastName,#ShippingAddress,#ShippingCity,#ShippingCountryID,#ShippingState,#ShippingZipCode,#ShippingPhone').addClass('required');
        $('#ShippingFirstName,#ShippingLastName,#ShippingCompany,#ShippingAddress,#ShippingCity,#ShippingCountryID,#ShippingState').css("width", "200px");
        $('#ShippingZipCode,#ShippingPhone').css("width", "100px");

        $('#BillingFirstName,#BillingLastName,#BillingAddress,#BillingCity,#BillingCountryID,#BillingState,#BillingZipCode,#BillingPhone').addClass('required');
        $('#BillingFirstName,#BillingLastName,#BillingAddress,#BillingCity,#BillingCountryID,#BillingState').css("width", "200px");
        $('#BillingZipCode,#BillingPhone').css("width", "100px");

        $('#CreditCardType,#CreditCardNo,#CreditCardMonth,#CreditCardYear,#CreditCardCVV').addClass('required');
        $('#CreditCardType,#CreditCardNo').css("width", "200px");
        $('#CreditCardCVV,#CreditCardMonth,#CreditCardYear,#CreditCardPhone').css("width", "100px");

        //$('#ShippingFirstName').focus();

        $("#MyPaymentForm").validate({
            rules: {
                ShippingFirstName: {
                    maxlength: 50
                },
                ShippingLastName: {
                    maxlength: 50
                },
                ShippingCompany: {
                    maxlength: 50
                },
                ShippingAddress: {
                    maxlength: 150
                },
                ShippingCity: {
                    maxlength: 50
                },
                ShippingZipCode: {
                    maxlength: 15
                },
                ShippingFirstName: {
                    maxlength: 50
                },
                BillingLastName: {
                    maxlength: 50
                },
                BillingCompany: {
                    maxlength: 50
                },
                BillingAddress: {
                    maxlength: 150
                },
                BillingCity: {
                    maxlength: 50
                },
                BillingZipCode: {
                    maxlength: 15
                },
                //CreditCardNo: {
                //    creditcard: true
                //},
                CreditCardCVV: {
                    maxlength: 4
                },
                ShippingMethod: {
                    digits: true,
                    min: 1
                }
            }
        });
        jQuery.validator.messages.required = " *";
        jQuery.validator.messages.min = " *";

        $('#whatCVV').click(function() { $('#popCVV').show(); });
        $('#closeCVV').click(function() { $('#popCVV').hide(); });
        $('#whatCCPhone').click(function() { $('#popCCPhone').show(); });
        $('#closeCCPhone').click(function() { $('#popCCPhone').hide(); });

        $('#PaymentMethod,#PaymentMethod2').click(function() {
            var pm = $(this).val();
            if (pm == '1') {
                $('#CreditCardType,#CreditCardNo,#CreditCardMonth,#CreditCardYear,#CreditCardCVV').attr('disabled', false).addClass('required');
                $('#CreditCardPhone').attr('disabled', false);
            } else {
                $('#CreditCardType,#CreditCardNo,#CreditCardMonth,#CreditCardYear,#CreditCardCVV').attr('disabled', true).removeClass('required');
                $('#CreditCardPhone').attr('disabled', true);
            }
        }).change();

        $('#CreditCardNo,#CreditCardCVV').numeric();
    };


    $.fn.setContactUSValidation = function() {
        $('#Email,#Name,#Comments').addClass('required');
        $('#Email,#Name,#Comments').css("width", "300px");

        $('#Email').focus();

        $("#MyForm").validate({
            rules: {
                Email: {
                    email: true
                }
            }
        });
    };


    $.fn.setMemberValidation = function() {
        $('#EmailAddress,#FirstName,#LastName,#Passwd,#RePasswd,#Address,#City,#CountryID,#State,#ZipCode,#Phone').addClass('required');
        $('#EmailAddress,#CompanyName,#Passwd,#RePasswd,#Address,#CountryID,#State').css("width", "300px");
        $('#TaxID,#FirstName,#LastName,#City,#ZipCode,#Phone').css("width", "130px");

        if ($('#EmailAddress').length)
            $('#EmailAddress').focus();
        else
            $('#CompanyName').focus();

        $("#MyForm").validate({
            rules: {
                EmailAddress: {
                    email: true,
                    maxlength: 50
                },
                TaxID: {
                    maxlength: 20
                },
                FirstName: {
                    maxlength: 30
                },
                LastName: {
                    maxlength: 30
                },
                Passwd: {
                    minlength: 4,
                    maxlength: 20
                },
                RePasswd: {
                    minlength: 4,
                    maxlength: 20,
                    equalTo: "#Passwd"
                },
                Address: {
                    maxlength: 100
                },
                City: {
                    maxlength: 50
                },
                ZipCode: {
                    maxlength: 20
                }
            }
        });
        jQuery.validator.messages.required = " *";
    };



})(jQuery);


