jQuery.fn.fadeIn = function(speed, callback) {
    return this.animate({ opacity: 'show' }, speed, function() {
        if (jQuery.browser.msie)
            this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
            callback();
    });
};

jQuery.fn.fadeOut = function(speed, callback) {
    return this.animate({ opacity: 'hide' }, speed, function() {
        if (jQuery.browser.msie)
            this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
            callback();
    });
};

$(document).ready(function () {

    //***********************************************//
    // COMMON STUFF
    //***********************************************//

    //guthy renker nav hover
    var grNav = $(".guthyRenkerNav dl");
    $(".guthyRenkerNav").hover(
        function () { $(this).addClass("guthyRenkerNavHover"); },
        function () { $(this).removeClass("guthyRenkerNavHover"); }
    );
    $(".guthyRenkerNav").click(function () {
        clearTimeout(200);
        $(".guthyRenkerNav .gradient, .guthyRenkerNav h2").addClass("hover");
    });
    $(".guthyRenkerNav h2").bind("mouseenter", function () { $(this).addClass("hover"); });
    $(".guthyRenkerNav .gradient").bind("mouseleave", function () {
        setTimeout(function () {
            $(".guthyRenkerNav .gradient").removeClass("hover");
            $(".guthyRenkerNav h2").removeClass("hover");
        }, 200);
    });

    var hackIE = $.browser.msie && ($.browser.version < 7);
    var awmsHeaderHolderHeight = $(".guthyRenkerNav").height() + $('.cartSection').height();
    var containerCsStyle = "min-height";

    //cart nav hover
    $(".cartNav li.liCartItems").hover(
        function () {
            //to push down the footer if the cart listing is really long
            if ($(this).find('ul').height() > $(window).height()) {
                if (hackIE) { containerCsStyle = "height"; }
                $('.containerTemplate').css(containerCsStyle, $(this).find('ul').height() + awmsHeaderHolderHeight + "px");
            }
            if ($(this).parent("ul").hasClass("noMember")) {
                $(".cartNav").addClass("cartHoverNoMember");
                if ($(this).parent("ul").hasClass("noCart")) {
                    $(".cartNav").addClass("cartHoverNoCart"); //for grc
                }
            }
            else { $(".cartNav").addClass("cartHover"); }
        },
        function () {
            $('.containerTemplate').removeAttr('style');
            if ($(this).parent("ul").hasClass("noMember")) {
                $(".cartNav").removeClass("cartHoverNoMember");
                if ($(this).parent("ul").hasClass("noCart")) {
                    $(".cartNav").removeClass("cartHoverNoCart"); //for grc
                }
            }
            else { $(".cartNav").removeClass("cartHover"); }
        }
    );


    //$(".cartNav li.liClubMembers").hover(function(){$(".cartNav").addClass("memberHover");},function(){$(".cartNav").removeClass("memberHover");});
    //default navigation
    $(".defaultNav li").not(".first, .orderNow").hover(
        function () { $(this).addClass("hover"); },
        function () { $(this).removeClass("hover"); });
    $(".defaultNav li.first").not(".selected").hover(
        function () { $(this).addClass("firstHover"); },
        function () { $(this).removeClass("firstHover"); });
    $(".defaultNav li.last").not(".selected").hover(
        function () { $(".defaultNav").addClass("defaultNavLastHover"); $(this).addClass("hover"); },
        function () { $(".defaultNav").removeClass("defaultNavLastHover"); $(this).removeClass("hover"); });
    $(".defaultNav li.orderNow").not(".selected").hover(
        function () { $(".defaultNav").addClass("defaultNavOrderHover"); $(this).addClass("hover orderHover"); },
        function () { $(".defaultNav").removeClass("defaultNavOrderHover"); $(this).removeClass("hover orderHover"); });

    //remove border when u click on a link
    $(".callNav li a, .defaultNav li a, .componentSubNav li a").focus(function () { $(this).blur(); });

    //componentSubNav li hover
    $(".componentSubNav li").hover(
        function () {
            if ($(this).hasClass("section") || $(this).hasClass("selected") || $(this).hasClass("selectedTwoLines")) { }
            else { $(this).addClass("hover"); }
        },
        function () { $(this).removeClass("hover"); }
    );

    //help popup   
    $("a.viewExplanation").hover(function () {
        var thisParentLabel = $(this).parent("label").get(0);
        $(thisParentLabel).next(".help").addClass("show");
        return false;
    }, function () {
        var thisParentLabel = $(this).parent("label").get(0);
        $(thisParentLabel).next(".help").removeClass("show");
        return false;
    });
    $(".helpClose a").click(function () {
        $(this).parents("div.help").removeClass("show");
        return false;
    });

    //watermarks    
    var swapValues = []; $(".watermarks").each(function (i) { swapValues[i] = $(this).val(); $(this).focus(function () { if ($(this).val() == swapValues[i]) { $(this).val("") } }).blur(function () { if ($.trim($(this).val()) == "") { $(this).val(swapValues[i]) } }) });

    //***********************************************//
    // component FAQ
    //***********************************************//
    $(".componentFAQ dt").toggle(
        function () { $(this).addClass('selected'); $(this).next("dd").addClass("show"); },
        function () { $(this).removeClass('selected'); $(this).next("dd").removeClass("show"); });

    // submit question
    $(".formSubmitQuestion .name input").blur(function () { $(this).validateInput({ errMessage: '1', watermarks: 'Name*' }); });
    $(".formSubmitQuestion .email input").blur(function () { $(this).validateInput({ validationType: 'email', watermarks: 'Email*', errMessage: '2' }); });
    $(".formSubmitQuestion .question textarea").blur(function () { $(this).validateInput({ watermarks: 'Your Question *', errMessage: '3' }); });

    //***********************************************//
    // component PRODUCT DISPLAY
    //***********************************************//

    //pack includes
    $(".packIncludes li").each(function (index, domEle) {
        $(domEle).bind("click", function () {
            $(domEle).addClass("click");
            $(".packIncludesExtraInfo").css({
                left: ($(window).width() - 535) / 2 + $(window).scrollLeft()
            });
            $(".packIncludesExtraInfo").show();
            $(".packIncludesExtraInfo li").removeClass("show");
            $(".packIncludesExtraInfo li.extraInfo:eq(" + index + ")").addClass("show");
            return false;
        });
    });
    $(".packIncludesExtraInfo .closeExtraInfo").click(function () {
        $(".packIncludesExtraInfo").hide();
        $(".packIncludesExtraInfo li").removeClass("show");
        return false;
    });

    /// extra rte flash popup for Sheercover
    $(".aboutSC a.aboutPopup").each(function (index, domEle) {
        $(domEle).bind("click", function () {
            $(domEle).addClass("click");
            $(".packIncludesExtraInfo").css({
                left: ($(window).width() - 285) / 2 + $(window).scrollLeft()
            });
            $(".packIncludesExtraInfo").show();
            $(".packIncludesExtraInfo li").removeClass("show");
            $(".packIncludesExtraInfo li.extraInfo:eq(" + index + ")").addClass("show");
            return false;
        }).attr("href", "javascript:{};");
    });

    ///

    //zoom
    $(".zoomExtraInfo .scrollable").scrollable({ size: 1, clickable: false, hoverClass: 'hover' });
    $(".zoom").click(function () {
        $(".zoomExtraInfo").css({ top: $(this).y() - 275, left: $(this).x() - 475, display: 'block' });
        return false;
    });
    $(".zoomExtraInfo .closeExtraInfo").click(function () {
        $(".zoomExtraInfo").hide();
        return false;
    });

    //automatic membership hover
    $(".automaticMembership .hoverArea").hover(
        function () {
            $(".automaticMembership").css("position", "relative");
            $(".automaticMembership .automaticMembershipExtraInfo").show();
        },
        function () {
            $(".automaticMembership").css("position", "");
            $(".automaticMembership .automaticMembershipExtraInfo").hide();
        }
    );

    //choose GWP
    $(".btnGWPProactiv").click(function () {
        var distanceFromLeft = ($(window).width() - 648) / 2 + $(window).scrollLeft();
        $(".gwpExtraInfoProactiv").css({ left: distanceFromLeft });
        $(".gwpExtraInfoProactiv").show();
        return false;
    });
    $(".gwpExtraInfoProactiv .closeExtraInfo").click(function () {
        $(".gwpExtraInfoProactiv").hide();
        return false;
    });

    //choose your free gift
    $(".btnChooseFreeGift").click(function () {
        var freeGiftsWidth = ($(".chooseFreeGiftsExtraInfo li").length * 180) + 40;
        if ($(".chooseFreeGiftsExtraInfo .limitedOffer:visible").length) { freeGiftsWidth = freeGiftsWidth + 80; }
        $(".chooseFreeGiftsExtraInfo .size").css("width", freeGiftsWidth);
        var distanceFromLeft = ($(window).width() - freeGiftsWidth) / 2 + $(window).scrollLeft();
        $(".chooseFreeGiftsExtraInfo").css({
            width: freeGiftsWidth + 20,
            left: distanceFromLeft
        });
        $(".chooseFreeGiftsExtraInfo").show();
        return false;
    });
    $(".chooseFreeGiftsExtraInfo .closeExtraInfo").click(function () {
        $(".chooseFreeGiftsExtraInfo").hide();
        return false;
    });

    //free gifts -- DONT TOUCH ME! u break it - u fix it
    $(".freeGifts .scrollable").scrollable({ size: 3, clickable: false, hoverClass: 'hover' });
    /// change from Nerida - when there is only one entry like this it centres everything
    if ($(".freeGifts .scrollable li").length == 1) {
        $(".componentProductDisplay .freeGifts .scrollable").css('width', '80px');
        $(".componentProductDisplay .freeGifts .screen").css('left', '110px');
        $(".componentProductDisplay .freeGifts .screen").css('width', '220px');
    } else if ($(".freeGifts .scrollable li").length == 2) {
        $(".componentProductDisplay .freeGifts .scrollable").css('width', '160px');
        $(".componentProductDisplay .freeGifts .screen").css('left', '45px');
        $(".componentProductDisplay .freeGifts .screen").css('width', '285px');
    }

    $(".paymentOptionList label").each(function () {
        if ($(".spanPnH").html() != null) {
            var content = "<div class='rblPaymentOptions'>" + $(".spanPnH").html() + "</div>";
            $(this).after(content);
        }
    });
    ///
    $(".freeGifts .scrollable li").each(function (index, domEle) {
        $(domEle).bind("mouseenter", function () {
            $(".freeGifts .scrollable li").removeClass("selected");
            $(domEle).addClass("selected");
            $(".freeGiftsExtraInfo").show();
            $(".freeGiftsExtraInfo li").removeClass("show");
            $(".freeGiftsExtraInfo li.extraInfo:eq(" + index + ")").addClass("show");
            $(".freeGiftsPopUpArea").css("height", $(".freeGiftsExtraInfo").height() - 10).show();
            return false;
        });
    });
    $(".freeGiftsScreenArea, .productDetailsRightCol, .freeGifts .screen .instruction,.productShot").bind("mouseenter", function () {
        $(".freeGifts .scrollable li").removeClass("selected");
        $(".freeGiftsExtraInfo").hide();
        $(".freeGiftsExtraInfo li").removeClass("show");
        $(".freeGiftsPopUpArea").css("height", '0px').hide();
        return false;
    });
    $(".freeGiftsExtraInfo").bind("mouseleave", function () {
        $(".freeGifts .scrollable li").removeClass("selected");
        $(".freeGiftsExtraInfo").hide();
        $(".freeGiftsExtraInfo li").removeClass("show");
        $(".freeGiftsPopUpArea").css("height", '0px').hide();
        return false;
    });
    $(".freeGifts .closeExtraInfo").click(function () {
        $(".freeGifts .scrollable li").removeClass("click");
        $(".freeGiftsExtraInfo").hide();
        $(".freeGiftsExtraInfo li").removeClass("show");
        return false;
    });

    //product tabs
    if ($(".productTabsLink li:first").attr("class") != null) {
        $(".productTabs .tabContent:eq(" + $(".productTabsLink li:first").attr("class").substring(3) + ")").addClass("show");
        $(".productTabsLink li:first").addClass("selected");
    }
    $(".productTabsLink li").click(function () {
        $(".productTabsLink li").removeClass("selected");
        var tabPosition = $(this).attr("class").substring(3);
        $(".productTabs .tabContent").removeClass("show");
        $(".productTabs .tabContent:eq(" + tabPosition + ")").addClass("show");
        $(this).addClass("selected");
        return false;
    });
    $(".productTabsLink li a").focus(function () { $(this).blur(); });

    //user review
    $(".userReview h3 a").click(function () { $(".userReview fieldset").show(); $(".userReview h3").hide(); return false; });

    //formPostReview
    $(".formPostReview .cta input").bind("mousedown", function () { validateFormPostReview(); });
    $(".formPostReview .name input").blur(function () { $(this).validateInput({ errMessage: '1', watermarks: 'Name*' }); });
    $(".formPostReview .email input").blur(function () { $(this).validateInput({ validationType: 'email', watermarks: 'Email*', errMessage: '2' }); });
    $(".formPostReview .age select").blur(function () { $(this).validateInput({ validationType: 'age', watermarks: 'Age Group*', errMessage: '3' }); });
    //$(".formPostReview .comment textarea").blur(function() { $(this).validateInput({ watermarks: 'Your say...', errMessage: '4' }); });
    $(".formPostReview .comment textarea").blur(function () {
        //comment filled
        if ($(".formPostReview .comment textarea").val() != "" && $(".formPostReview .comment textarea").val() != "Your say...") {
            $(".formPostReview .comment .validateOK").addClass("show");
            $(".formPostReview .comment .validateFAIL").removeClass("show");
            $(".formPostReview .validationSummary ul").find("li:eq(4)").removeClass("show");
            //no need to check rating            
            $(".formPostReview .rating .validateFAIL").removeClass("show");
        }
        else {
            $(".formPostReview .comment .validateOK").removeClass("show");
            //rating not chosen
            if ($(".formPostReview .starRating input[type='hidden']").val() == "0") {
                $(".formPostReview .comment .validateOK").removeClass("show");
                $(".formPostReview .comment .validateFAIL").addClass("show");
                $(".formPostReview .rating .validateOK").removeClass("show");
                $(".formPostReview .rating .validateFAIL").addClass("show");
                $(".formPostReview .validationSummary ul").find("li:eq(4)").addClass("show");
            }
            //rating chosen
            else {
                $(".formPostReview .rating .validateOK").addClass("show");
                $(".formPostReview .rating .validateFAIL").removeClass("show");
                $(".formPostReview .validationSummary ul").find("li:eq(4)").removeClass("show");
            }
        }
    });
    $(".formPostReview .rating .stars-wrapper").click(function () {
        //rating chosen
        if ($(".formPostReview .starRating input[type='hidden']").val() != "0") {
            $(".formPostReview .rating .validateOK").addClass("show");
            $(".formPostReview .rating .validateFAIL").removeClass("show");
            $(".formPostReview .validationSummary ul").find("li:eq(4)").removeClass("show");
            //no need to check comment            
            $(".formPostReview .comment .validateFAIL").removeClass("show");
        }
        else {
            $(".formPostReview .rating .validateOK").removeClass("show");
            //comment not filled
            if ($(".formPostReview .comment textarea").val() == "" || $(".formPostReview .comment textarea").val() == "Your say...") {
                $(".formPostReview .comment .validateOK").removeClass("show");
                $(".formPostReview .comment .validateFAIL").addClass("show");
                $(".formPostReview .rating .validateOK").removeClass("show");
                $(".formPostReview .rating .validateFAIL").addClass("show");
                $(".formPostReview .validationSummary ul").find("li:eq(4)").addClass("show");
            }
            //comment filled
            else {
                $(".formPostReview .comment .validateOK").addClass("show");
                $(".formPostReview .comment .validateFAIL").removeClass("show");
                $(".formPostReview .validationSummary ul").find("li:eq(4)").removeClass("show");
            }
        }
    });

    //***********************************************//
    // component UPSELL
    //***********************************************//
    $(".componentProductUpsell .scrollable").scrollable({ size: 6, clickable: false, hoverClass: 'hover' });
    $(".componentProductUpsell .scrollable li").each(function (index, domEle) {
        $(domEle).bind("click", function () {
            $(".componentProductUpsell .scrollable li").removeClass("click");
            $(domEle).addClass("click");
            $(".upsellProductsExtraInfo").show();
            $(".upsellProductsExtraInfo li").removeClass("show");
            $(".upsellProductsExtraInfo li.extraInfo:eq(" + index + ")").addClass("show");
            return false;
        });
    });
    $(".componentProductUpsell .closeExtraInfo").click(function () {
        $(".componentProductUpsell .scrollable li").removeClass("click");
        $(".upsellProductsExtraInfo").hide();
        $(".upsellProductsExtraInfo li").removeClass("show");
        return false;
    });

    //***********************************************//
    // component MBG
    //***********************************************//

    $(".componentMoneyBack .instruction a").attr("href", "javascript:{};");
    $(".componentMoneyBack").hover(function ()
    { $(".componentMoneyBack .moneyBackExtraInfo").show(); },
	function () { $(".componentMoneyBack .moneyBackExtraInfo").hide(); });

    //***********************************************//
    // component SUBSCRIBE
    //***********************************************//    
    //formSubscribe
    $(".formSubscribe .email input").blur(function () { $(this).validateInput({ validationType: 'email', watermarks: 'Your email*', errMessage: '1' }); });

    //***********************************************//
    // component TELL A FRIEND
    //***********************************************//
    //formTellAFriend
    $(".formTellAFriend .cta input").bind("mousedown", function () { validateFormTellAFriend(); });
    $(".formTellAFriend input.inputFriendName1").blur(function () { $(this).validateInput({ errMessage: '1' }); });
    $(".formTellAFriend input.inputFriendEmail1").blur(function () { $(this).validateInput({ validationType: 'email', errMessage: '2' }); });
    $(".formTellAFriend input.inputYourName").blur(function () { $(this).validateInput({ errMessage: '3' }); });
    $(".formTellAFriend input.inputYourEmail").blur(function () { $(this).validateInput({ validationType: 'email', errMessage: '4' }); });

    //***********************************************//
    // component SEND STORY
    //***********************************************//    
    //replace input type=file
    $(".fieldsetYourPhotos input[type=file]").filestyle({ image: "images/btnBrowse.png", imageheight: 31, imagewidth: 96, width: 215 });

    //formSendStory
    $(".formSendStory .cta input").bind("mousedown", function () { validateFormSendStory(); });

    $(".formSendStory input.inputFirstName").blur(function () { $(this).validateInput({ errMessage: '1' }); });
    $(".formSendStory input.inputLastName").blur(function () { $(this).validateInput({ errMessage: '2' }); });
    $(".formSendStory input.inputAddress").blur(function () { $(this).validateInput({ errMessage: '3' }); });
    $(".formSendStory input.inputSuburb").blur(function () { $(this).validateInput({ errMessage: '4' }); });
    $(".formSendStory input.inputState").blur(function () { $(this).validateInput({ errMessage: '5' }); });
    $(".formSendStory input.inputPostcode").blur(function () { $(this).validateInput({ errMessage: '6' }); });
    $(".formSendStory input.inputEmail").blur(function () { $(this).validateInput({ validationType: 'email', errMessage: '7' }); });
    $(".formSendStory input.inputConfirmEmail").blur(function () { $(this).validateInput({ validationType: 'confirmEmail', errMessage: '8' }); });
    $(".formSendStory input.inputPhone").blur(function () { $(this).validateInput({ validationType: 'phone', errMessage: '9' }); });
    $(".formSendStory select.selectAge").blur(function () { $(this).validateInput({ validationType: 'age', errMessage: '10' }); });
    $(".formSendStory .infoGender input[type='radio']").blur(function () { $(".infoGender").validateInput({ inputType: 'radio', errMessage: '11' }); });
    $(".formSendStory textarea.txtQuestion1").blur(function () { $(this).validateInput({ errMessage: '12' }); });
    $(".formSendStory textarea.txtQuestion2").blur(function () { $(this).validateInput({ errMessage: '12' }); });
    $(".formSendStory textarea.txtQuestion3").blur(function () { $(this).validateInput({ errMessage: '12' }); });
    $(".formSendStory textarea.txtQuestion24").blur(function () { $(this).validateInput({ errMessage: '12' }); });
    $(".formSendStory input.inputDate").blur(function () { $(this).validateInput({ validationType: 'ddmmyyyy', errMessage: '13' }); });
    $(".formSendStory .beforePhoto1 input").each(function (index, domEle) {
        if ($(".formSendStory .beforePhoto1 .validate").length)
            $(domEle).bind("change", function () {
                $(".formSendStory .beforePhoto1").validateInput({ inputType: 'file', errMessage: '14' });
            });
    });

    //***********************************************//
    // component CLICK TO CALL
    //***********************************************//    
    $(".componentHelp a:first").not(".closeExtraInfo").click(function () {
        $(".callBackExtraInfo").show();
        return false;
    });
    $(".callBackExtraInfo .closeExtraInfo").click(function () {
        $(".callBackExtraInfo").hide();
        return false;
    });

    //***********************************************//
    // MICHE BAG ONLY
    //***********************************************//
    $(".shellListing li").hover(
        function () { $(this).addClass("hover"); },
        function () { $(this).removeClass("hover"); }
    );

    $(".shellExtraInfo .closeExtraInfo").click(function () {
        $(".shellExtraInfo").hide();
        $(".shellExtraInfo li").removeClass("show");
        return false;
    });

    //***********************************************//
    // component DISTRIBUTOR
    //***********************************************//
    //formDistributor
    $(".formDistributor .cta input").bind("mousedown", function () { validateFormDistributor(); });
    $(".formDistributor .doInterest input[type='radio']").blur(function () { $(".doInterest").validateInput({ inputType: 'radio', errMessage: '1' }); });
    $(".formDistributor .doFirstName input").blur(function () { $(this).validateInput({ errMessage: '2' }); });
    $(".formDistributor .doLastName input").blur(function () { $(this).validateInput({ errMessage: '3' }); });
    $(".formDistributor .doAddress .addr1").blur(function () { $(this).validateInput({ errMessage: '4' }); });
    $(".formDistributor .doSuburb input").blur(function () { $(this).validateInput({ errMessage: '5' }); });
    $(".formDistributor .doPostcode input").blur(function () { $(this).validateInput({ errMessage: '6' }); });
    $(".formDistributor .doState select").blur(function () { $(this).validateInput({ errMessage: '7' }); });
    $(".formDistributor .doPhone input").blur(function () { $(this).validateInput({ validationType: 'phone', errMessage: '8' }); });
    $(".formDistributor .doEmail input").blur(function () { $(this).validateInput({ validationType: 'email', errMessage: '9' }); });

    //***********************************************//
    // SHEER COVER and CORPORATE ONLY
    //***********************************************//

    //shade guarantee hover
    $(".shadeGuarantee h2").hover(
        function () { $(".shadeGuarantee .shadeGuaranteeExtraInfo").show(); },
        function () { $(".shadeGuarantee .shadeGuaranteeExtraInfo").hide(); });

    //select a shade hover
    $(".selectShade ul li").hover(
        function () { if (!$(this).children("input").hasClass("selected")) { $(this).addClass("hover"); } },
        function () { $(this).removeClass("hover"); });

    // MGM
    $(".MGMForm .cta input").bind("mousedown", function () { validateFormMGM(); });

    var valSummary = $(".MGMForm .validationSummary ul");
    valSummary.removeClass("show");
    $(".MGMForm .doEmail1 input").blur(function () { $(this).validateInput({ validationType: 'email', errMessage: '0' }); });
    $(".MGMForm .doEmail2 input").blur(function () { $(this).validateInput({ validationType: 'email', errMessage: '1' }); });
    $(".MGMForm .doEmail3 input").blur(function () { $(this).validateInput({ validationType: 'email', errMessage: '2' }); });

    //***********************************************//
    // CORPORATE ONLY
    //***********************************************//

    //***********************************************//
    // component HOME PAGE SUBSCRIBE
    //***********************************************//        
    $(".formHomeSubscribe .name input").blur(function () { $(this).validateInput({ errMessage: '1', watermarks: 'Your name*' }); });
    $(".formHomeSubscribe .email input").blur(function () { $(this).validateInput({ validationType: 'email', watermarks: 'Your email*', errMessage: '2' }); });

    //***********************************************//
    // component CONTACT US
    //***********************************************//
    $(".formContact .infoPhone .viewExplanation, .formContact .infoEmail .viewExplanation").hover(
        function () { $(this).parents("li").addClass("hover"); },
        function () { $(this).parents("li").removeClass("hover"); }
    );

    //formContact
    $(".formContact .cta input").bind("mousedown", function () { validateFormContact(); });
    $(".formContact .infoFirstName input").blur(function () { $(this).validateInput({ errMessage: '1' }); });
    $(".formContact .infoLastName input").blur(function () { $(this).validateInput({ errMessage: '2' }); });
    $(".formContact .infoPostcode input").blur(function () { $(this).validateInput({ errMessage: '3' }); });
    $(".formContact .infoEmail input").blur(function () { $(this).validateInput({ validationType: 'email', errMessage: '4' }); });
    $(".formContact .infoPhone input").blur(function () { $(this).validateInput({ validationType: 'phone', errMessage: '5' }); });

    $(".formContact .queryProduct input[type='radio']").blur(function () {
        $(".formContact .query .validate img").removeClass("show");
        $(".formContact .validationSummary ul").find("li:eq(7)").removeClass("show");
    }).click(function () {
        $(".formContact .query .validate img").removeClass("show");
        $(".formContact .validationSummary ul").find("li:eq(7)").removeClass("show");
    });
    $(".formContact .queryWebsite input[type='radio']").blur(function () {
        $(".formContact .queryWebsite .text").validateInput({ watermarks: 'Web address', errMessage: '7' });
    }).click(function () {
        $(".formContact .queryWebsite .text").validateInput({ watermarks: 'Web address', errMessage: '7' });
    });
    $(".formContact .queryWebsite .text").blur(function () {
        $(".formContact .queryWebsite input[type='radio']").attr("checked", "checked");
        $(".formContact .queryProduct input[type='radio']").removeAttr("checked");
        $(this).validateInput({ watermarks: 'Web address', errMessage: '7' })
    }).click(function () {
        $(".formContact .queryWebsite input[type='radio']").attr("checked", "checked");
        $(".formContact .queryProduct input[type='radio']").removeAttr("checked");
        $(this).validateInput({ watermarks: 'Web address', errMessage: '7' })
    });

    $(".formContact .comments textarea").blur(function () { $(this).validateInput({ errMessage: '8' }); });

    //hack fix for anchor tag    
    $("a[href^='#']").each(function () {
        $(this).attr("href", window.location.href + $(this).attr("href"));
    })

    $(".media a").flash(
      { autoplay: true, wmode: 'transparent' },
      {},
      function (htmlOptions) {
          var $this = $(this);
          htmlOptions.src = $this.attr('href');
          if ($this.attr("rel").length > 0) {
              var params = $this.attr('rel').split(':');
              htmlOptions.width = params[0];
              htmlOptions.height = params[1];

              if (params.length > 2)
                  htmlOptions.bgcolor = params[2];
          }
          $this.before($.fn.flash.transform(htmlOptions));
          $this.parent().addClass('flash-replaced');
      });


    if ($.jqURL != null && $.jqURL.get('scrollTo') != null && $.jqURL.get('scrollTo').length > 0) {
        var target = $("a[name='" + $.jqURL.get('scrollTo') + "']");

        $('html, body').animate({
            scrollTop: target.offset().top
        }, 1000);
    }

});

//***********************************************//
// FORM VALIDATIONS
//***********************************************//
function validateFormHomeSubscribe() {
    var valSummary = $(".formHomeSubscribe .validationSummary ul");
    $(".formHomeSubscribe .validate img").removeClass("show");
    $(".formHomeSubscribe .name input").validateInput({ errMessage: '1', watermarks: 'Your name*' });
    $(".formHomeSubscribe .email input").validateInput({ validationType: 'email', watermarks: 'Your email*', errMessage: '2' });
    if ($(valSummary).hasClass("show")) { return false; }
    else { return true; }
}

function validateFormContact() {
    var valSummary = $(".formContact .validationSummary ul");
    $(".formContact .validate img").removeClass("show");
    $(".formContact .infoFirstName input").validateInput({ errMessage: '1' });
    $(".formContact .infoLastName input").validateInput({ errMessage: '2' });
    $(".formContact .infoPostcode input").validateInput({ errMessage: '3' });
    $(".formContact .infoEmail input").validateInput({ validationType: 'email', errMessage: '4' });
    $(".formContact .infoPhone input").validateInput({ validationType: 'phone', errMessage: '5' });

    if ($(".formContact .query input[type='radio']:checked").val() == "website") {
        $(".formContact .queryWebsite .text").validateInput({ watermarks: 'Web address', errMessage: '7' });
    }
    $(".formContact .comments textarea").validateInput({ errMessage: '8' });
    if ($(valSummary).hasClass("show")) { return false; }
    else { return true; }
}

function validateFormPostReview() {
    var valSummary = $(".formPostReview .validationSummary ul");
    $(".formPostReview .validate img").removeClass("show");
    $(".formPostReview .name input").validateInput({ errMessage: '1', watermarks: 'Name*' });
    $(".formPostReview .email input").validateInput({ validationType: 'email', watermarks: 'Email*', errMessage: '2' });
    $(".formPostReview .age select").validateInput({ validationType: 'age', watermarks: 'Age*', errMessage: '3' });
    if ($(".formPostReview .starRating input[type='hidden']").val() == "0" &&
        ($(".formPostReview .comment textarea").val() == "" || $(".formPostReview .comment textarea").val() == "Your say...")) {
        $(".formPostReview .comment textarea").validateInput({ watermarks: 'Your say...', errMessage: '4' });
        $(".formPostReview .rating .validateFAIL").addClass("show");
    }
    else {
        //rating chosen
        if ($(".formPostReview .starRating input[type='hidden']").val() != "0") {
            $(".formPostReview .rating .validateOK").addClass("show");
            $(".formPostReview .rating .validateFAIL").removeClass("show");
            $(valSummary).find("li:eq(4)").removeClass("show");
        }
        //comment filled
        if ($(".formPostReview .comment textarea").val() != "" && $(".formPostReview .comment textarea").val() != "Your say...") {
            $(".formPostReview .comment .validateOK").addClass("show");
            $(".formPostReview .comment .validateFAIL").removeClass("show");
            $(valSummary).find("li:eq(4)").removeClass("show");
        }
    }
    if ($(valSummary).hasClass("show")) { return false; }
    else { return true; }
}

function validateFormSubscribe() {
    var valSummary = $(".formSubscribe .validationSummary ul");
    $(".formSubscribe .validate img").removeClass("show");
    $(".formSubscribe .email input").validateInput({ validationType: 'email', watermarks: 'Email*', errMessage: '1' });
    if ($(valSummary).hasClass("show")) { return false; }
    else { return true; }
}

function chooseFreeGiftClose() {
    $(".chooseFreeGiftsExtraInfo").hide();
    return true;
}

function validateFormTellAFriend() {
    var valSummary = $(".formTellAFriend .validationSummary ul");
    $(".formTellAFriend .validate img").removeClass("show");
    $(".formTellAFriend input.inputFriendName1").validateInput({ errMessage: '1' });
    $(".formTellAFriend input.inputFriendEmail1").validateInput({ validationType: 'email', errMessage: '2' });
    $(".formTellAFriend input.inputYourName").validateInput({ errMessage: '3' });
    $(".formTellAFriend input.inputYourEmail").validateInput({ validationType: 'email', errMessage: '4' });
    if ($(valSummary).hasClass("show")) { return false; }
    else { return true; }
}

function validateFormSendStory() {
    var valSummary = $(".formSendStory .validationSummary ul");
    $(".formSendStory .validate img").removeClass("show");

    $(".formSendStory input.inputFirstName").validateInput({ errMessage: '1' });
    $(".formSendStory input.inputLastName").validateInput({ errMessage: '2' });
    $(".formSendStory input.inputAddress").validateInput({ errMessage: '3' });
    $(".formSendStory input.inputSuburb").validateInput({ errMessage: '4' });
    $(".formSendStory input.inputState").validateInput({ errMessage: '5' });
    $(".formSendStory input.inputPostcode").validateInput({ errMessage: '6' });
    $(".formSendStory input.inputEmail").validateInput({ validationType: 'email', errMessage: '7' });
    $(".formSendStory input.inputConfirmEmail").validateInput({ validationType: 'confirmEmail', errMessage: '8' });
    $(".formSendStory input.inputPhone").validateInput({ validationType: 'phone', errMessage: '9' });
    $(".formSendStory select.selectAge").validateInput({ validationType: 'age', errMessage: '10' });
    $(".formSendStory .infoGender").validateInput({ inputType: 'radio', errMessage: '11' });
    $(".formSendStory textarea.txtQuestion1").validateInput({ errMessage: '12' });
    $(".formSendStory textarea.txtQuestion2").validateInput({ errMessage: '12' });
    $(".formSendStory textarea.txtQuestion3").validateInput({ errMessage: '12' });
    $(".formSendStory textarea.txtQuestion24").validateInput({ errMessage: '12' });
    $(".formSendStory input.inputDate").validateInput({ validationType: 'ddmmyyyy', errMessage: '13' });

    if ($(".formSendStory .beforePhoto1 .validate").length)
        $(".formSendStory .beforePhoto1").validateInput({ inputType: 'file', errMessage: '14' });

    if ($(valSummary).hasClass("show")) { return false; }
    else { return true; }
}

function validateFormSubmitQuestion() {
    var valSummary = $(".formSubmitQuestion .validationSummary ul");
    $(".formSubmitQuestion .validate img").removeClass("show");
    $(".formSubmitQuestion .name input").validateInput({ errMessage: '1', watermarks: 'Name*' });
    $(".formSubmitQuestion .email input").validateInput({ validationType: 'email', watermarks: 'Email*', errMessage: '2' });
    $(".formSubmitQuestion .question textarea").validateInput({ watermarks: 'Your Question*', errMessage: '3' });
    if ($(valSummary).hasClass("show")) { return false; }
    else { return true; }
}

function validateFormDistributor() {
    var valSummary = $(".formDistributor .validationSummary ul");
    $(".formDistributor .validate img").removeClass("show");
    $(".formDistributor .doInterest").validateInput({ inputType: 'radio', errMessage: '1' });
    $(".formDistributor .doFirstName input").validateInput({ errMessage: '2' });
    $(".formDistributor .doLastName input").validateInput({ errMessage: '3' });
    $(".formDistributor .doAddress .addr1").validateInput({ errMessage: '4' });
    $(".formDistributor .doSuburb input").validateInput({ errMessage: '5' });
    $(".formDistributor .doPostcode input").validateInput({ errMessage: '6' });
    $(".formDistributor .doState select").validateInput({ errMessage: '7' });
    $(".formDistributor .doPhone input").validateInput({ validationType: 'phone', errMessage: '8' });
    $(".formDistributor .doEmail input").validateInput({ validationType: 'email', errMessage: '9' });

    if ($(valSummary).hasClass("show")) { return false; }
    else { return true; }
}

function validateFormMGM() {
    var valSummary = $(".MGMForm .validationSummary ul");
    $(".MGMForm .validate img").removeClass("show");
    $(".MGMForm .doEmail1 input").validateInput({ validationType: 'email', errMessage: '0' });
    $(".MGMForm .doEmail2 input").validateInput({ validationType: 'email', errMessage: '1' });
    $(".MGMForm .doEmail3 input").validateInput({ validationType: 'email', errMessage: '2' });

    if ($(valSummary).hasClass("show")) { return false; }
    else { return true; }
}

function validateFormClickToCall() {
    var valSummary = $(".formClickToCall .validationSummary ul");
    $(".formClickToCall .validate img").removeClass("show");
    $(".formClickToCall li.name input.text").validateInput({ errMessage: '1' });
    $(".formClickToCall li.phone input.text").validateInput({ validationType: 'phoneAUNZ', errMessage: '2' });
    if ($(valSummary).hasClass("show")) { return false; }
    else { return true; }
}


// CLICK TO CALL
function submitClickToCall(obj) {

    //calculate GMT time.
    var timezoneOffset = new Date().getTimezoneOffset(); // Minutes
    var callbackTime = $("#ctcFieldWrapper li.time").find("select.ctcField").val().replace(":00", "");
    var callbackDate = $("div#callBackCalendar").datepicker('getDate');
    callbackDate.setHours(parseInt(callbackTime));

    var gmtCallbackDateTime = callbackDate;
    gmtCallbackDateTime.setMinutes(timezoneOffset);
    var gmtCallbackTimeString = gmtCallbackDateTime.getHours() + ":00";
    var gmtCallbackDateString = "";

    gmtCallbackDateString += ((gmtCallbackDateTime.getDate() < 10) ? "0" : "") + gmtCallbackDateTime.getDate();
    gmtCallbackDateString += "-" + (((gmtCallbackDateTime.getMonth() + 1) < 10) ? "0" : "") + (gmtCallbackDateTime.getMonth() + 1);
    gmtCallbackDateString += "-" + gmtCallbackDateTime.getFullYear();

    //set date store
    $("#hfGMTDate").val(gmtCallbackDateString);

    //set gmt time
    $("#hfGMTTime").val(gmtCallbackTimeString);

    //setup args for server-side site
    var args = '';
    $("#ctcFieldWrapper").find("input:text.ctcField,input:hidden.ctcField").each(function(index, ele) {
        args += String(ele.className.match(/id:[^\s]*/i)).replace("id:", "") + "~" + ele.value + "|"; ;
    });

    $("#ctcFieldWrapper").find("input:checkbox.ctcField").each(function(index, ele) {
        args += String(ele.className.match(/id:[^\s]*/i)).replace("id:", "") + "~" + ele.checked + "|"; ;
    });

    $("#ctcFieldWrapper").find("select.ctcField").each(function(index, ele) {
        args += String(ele.className.match(/id:[^\s]*/i)).replace("id:", "") + "~" + $(ele).val() + "|"; ;
    });

    args = args.substring(0, args.length - 1);

    CallServer(args, "");
    return false;
}

function ReceiveClickToCallResponse(retValue) {
    if (retValue == 'True') {
        $("#ctcFieldWrapper").hide();
        $("#ctcFormMsg div.errorMsg").hide();
        $("#ctcFormMsg div.successMsg").show();
    }
    else if (retValue == 'False') {
        $("#ctcFormMsg div.errorMsg").show();
    }
}

//MICHE BAG FUNCTIONS
function addShellsToCart() {
    var allValues = "";
    var api = $(".chosenShells .scrollable li");
    for (var i = 0; i < api.length; i++) {
        if (api.eq(i).hasClass("base")) {
            if ($(".chosenShells").hasClass("chosenShellsBYOTote")) { allValues += "tote="; }
            else if ($(".chosenShells").hasClass("chosenShellsBYOMini")) { allValues += "mini="; }
            else { allValues += "base="; }
            allValues += api.eq(i).find("input[type='hidden']").val();
        }
        else {
            allValues += api.eq(i).find("input[type='hidden']").val();
        }
        if (i != (api.length - 1)) { allValues += ","; }

    }

    //assign category product keycode id
    var category_product_keycode_id = $("#divItemPayment input[type='radio']:checked").val();
    allValues += "|cpKeycodeId=" + category_product_keycode_id;

    $(".chosenShells .cta input[type='hidden']").val(allValues);



    return false;
}
function enlargeShell(index, y) {
    $(".shellExtraInfo").show().css({
        top: y,
        left: $(".shellListing").x() + 60
    });
    $(".shellExtraInfo li").removeClass("show");
    $(".shellExtraInfo li.extraInfo:eq(" + index + ")").addClass("show");
}

function removeShell(index) {
    //removing selected shell - by index
    var api = $(".chosenShells .scrollable").scrollable();
    api.getItems().eq(index).remove();
    api.reload().prev();
    updateStatus();
    updateChosenItemIndex();
}
function updateChosenItemIndex() {
    //update chosen item index - so its properly removed                  
    var api = $(".chosenShells .scrollable li");
    for (var i = 0; i < api.length; i++) {
        if (!api.eq(i).hasClass("base")) {
            api.eq(i).children(".remove").remove();
            api.eq(i).append('<a onclick="removeShell(' + i + '); return false;" class="remove" title="remove">remove this shell</a>');
        }
    }
}

function chooseClassicColor() {
    $(".buildYourOwnBundle .step").fadeOut();
    $('html, body').animate({ scrollTop: 0 }, 'slow', function() {
        $(".buildYourOwnBundle .step1b").fadeIn('slow');
    });
}
function loadBaseItem(keycodeID, imageURL, name) {

    var api = $(".chosenShells .scrollable").scrollable();
    var ctaWarning = $(".chosenShells .cta .warning");

    setTimeout("loadShellButtons()", 2000);
    api.getItemWrap().append('<li class="base"><img src="' + imageURL + '" alt="' + name + '" /><input type="hidden" value="' + keycodeID + '" /><div class="title"><div class="titleBody">' + name + '</div><div class="titleBottom"></div></div></li>');
    api.reload().end();
    //update warning message for tote base bag
    if ($(".chosenShells").hasClass("chosenShellsBYOTote") || $(".chosenShells").hasClass("chosenShellsBYOMini")) {
        ctaWarning.html("Please select 2 shells to add to your bundle.");
    }
    ctaWarning.addClass("show");
    $(".chosenShells").slideDown(325);
}
function loadShellButtons() {
    $(".shellListing li").each(function(index, domEle) {
        var buttonContainer = $(domEle).find("span.clearer");
        var keycodeID = $(this).find("input[type='hidden']").eq(0).val();
        var imageURL = $(this).find("input[type='hidden']").eq(1).val();
        var name = $(this).find("strong").html();
        buttonContainer.append('<a onclick="addShell(' + keycodeID + ',\'' + imageURL + '\',\'' + name + '\'); return false;" title="Add"><img src="images/btnAddShell.png" alt="Add" /></a>');
        buttonContainer.append('<a onclick="enlargeShell(' + index + "," + $(domEle).y() + '); return false;" title="Enlarge"><img src="images/btnEnlargeShell.png" alt="Enlarge" /></a>');
    });
    $(".shellExtraInfo li.extraInfo").each(function(index, domEle) {
        var ddCta = $(domEle).find("dd.cta");
        var keycodeID = ddCta.find("input[type='hidden']").eq(0).val();
        var imageURL = ddCta.find("input[type='hidden']").eq(1).val();
        var name = $(domEle).find("dt.title").html();
        ddCta.append('<a onclick="addShell(' + keycodeID + ',\'' + imageURL + '\',\'' + name + '\'); return false;" title="Add"><img src="images/btnAddShellExtraInfo.png" alt="Add" /></a>');

    });
}
function addShell(keycodeID, imageURL, name) {
    //add selected shell based on the shell listing
    if ($(".chosenShells:hidden")) { $(".chosenShells").slideDown(325); }
    var chosenShells = $(".chosenShells");

    //chosenShellsBYOClassic - max 2 shells
    if (chosenShells.hasClass("chosenShellsBYOClassic") && chosenShells.find(".scrollable li").not(".base").length >= 2) { }
    //chosenShellsBYOTote - max 2 shells
    else if ((chosenShells.hasClass("chosenShellsBYOTote") || chosenShells.hasClass("chosenShellsBYOTote")) && chosenShells.find(".scrollable li").not(".base").length >= 2) { }
    else {
        var api = $(".chosenShells .scrollable").scrollable();
        api.getItemWrap().append('<li><img src="' + imageURL + '" alt="' + name + '" /><input type="hidden" value="' + keycodeID + '" /><div class="title"><div class="titleBody">' + name + '</div><div class="titleBottom"></div></div><a onclick="removeShell(' + api.getSize() + '); return false;" class="remove" title="remove">remove this shell</a></li>');
        api.reload().end();
        updateStatus();
    }
}

function updateStatus() {
    //update cta status - to show the button or show warning message
    var api = $(".chosenShells .scrollable li").not(".base");
    var totalShell = $(".chosenShells h3 .total");
    var shellUnit = $(".chosenShells h3 .shell");
    var chosenShells = $(".chosenShells");
    var ctaButton = $(".chosenShells .cta .btnAddToCart");
    var ctaWarning = $(".chosenShells .cta .warning");
    var ctaShellNum = $(".chosenShells .cta .warning .shellNum");
    var maxNumOfShell = 0;

    var spanNumOfShell = $("#divItemSummary input[type='radio']:checked").parent();
    if (spanNumOfShell != null) {
        try {
            maxNumOfShell = spanNumOfShell.attr("class").match(/shellNum\d*/)[0].replace("shellNum", "");
        }
        catch (err) { }
    }

    totalShell.html(api.length);
    //chosenShellsBYOClassic
    if (chosenShells.hasClass("chosenShellsBYOClassic")) {
        //only valid if the bundle has 2 or 4 or 6 shells
        if (api.length == maxNumOfShell) {
            ctaButton.addClass("show");
            ctaWarning.removeClass("show");
        }
        else {
            ctaButton.removeClass("show");
            ctaWarning.addClass("show");
        }
    }
    //chosenShellsBYOTote || chosenShellsBYOMini
    else if ((chosenShells.hasClass("chosenShellsBYOTote") || chosenShells.hasClass("chosenShellsBYOTote"))) {
        //only valid if the bundle has 2 shells
        if (api.length == maxNumOfShell) {
            ctaButton.addClass("show");
            ctaWarning.removeClass("show");
        }
        else {
            ctaButton.removeClass("show");
            ctaWarning.addClass("show");
        }
    }
    //chosenShellsIndividual
    else {
        if (
            api.length == maxNumOfShell
            ) {
            ctaButton.addClass("show");
            ctaWarning.removeClass("show");
        }
        else if (api.length == 0) {
            ctaButton.removeClass("show");
            ctaWarning.addClass("show");
            $(".chosenShells").slideUp(325);
        }
        else {
            ctaWarning.addClass("show");
            ctaButton.removeClass("show");
        }
    }
    //update total shell status - "you've chosen 1 shell"
    if (api.length < 1) { totalShell.html("0"); shellUnit.html("shells"); }
    else if (api.length == 1) { shellUnit.html("shell"); }
    else { shellUnit.html("shells"); }

    //update shell number in the warning message.
    ctaShellNum.text(maxNumOfShell);
}

// Star rating
$(function() {
    $("div.starRating").each(function(i, ele) {
        if ($(ele).hasClass('fixed')) {
            SetStarWrapper($(ele), true, 2, 0, false);
        }

        if ($(ele).hasClass('dynamic')) {
            SetStarWrapper($(ele), false, 1, 0, true);
        }
    });
});

function SetStarWrapper(parent, disabled, split, cancelValue, cancelShow) {
    //  Attribute example
    //	disabled: false,
    //	split: 2,
    //	inputType: "select",
    //	cancelValue: 0,
    //	cancelShow: true,
    //	oneVoteOnly: false,
    //	captionEl: $(".stars-cap")

    $(".stars-wrapper", parent)
				.stars({
				    disabled: disabled,
				    split: split,
				    inputType: "select",
				    cancelValue: cancelValue,
				    cancelShow: cancelShow,
				    oneVoteOnly: false,
				    captionEl: $(".stars-cap")
				})
}

// youtube video z-index layer fix
$(document).ready(function () {
    $('iframe').each(function () {
        var url = $(this).attr("src");

        if (url.indexOf('youtube.com') > 0) {
            if (url.indexOf('?') < 0) {
                $(this).attr("src", url + "?wmode=transparent");
            }
            else {
                $(this).attr("src", url + "&amp;wmode=transparent");
            }
        }
    });
});

