
$(function(){


    $('.current-page').click(function() {
        return false;
    });
    $('.non-link').click(function() {
        return false;
    });
    /* Review/Payment Page */
     $("select[@name=ccType]").change( function() {
            if ( $(this).val() == "PP" || $(this).val() == "PH" ) {
                $("#gift-certificate").css( "display", "none" );
            } else {
                $("#gift-certificate").css( "display", "block" );
            }
            addToCart.cartLoader();
            $.get(
                "/checkout/payment.cmd",
                { act : "payment_type_swap", ccType: $(this).val(), timeStamp: new Date().getTime() },
                function( d ) {
                    $("#paymentTypeLayer").html( d );
                    $("#ajax-loading").remove();
                }
            );
        });

    /* Billing Shipping Page */

    function billingStateShipping() {
        $("select[@name=billingState]").change( function() {
            var stateValue = $("select[@name=billingState]").val();
            if ( stateValue == "PR" || stateValue == "AS" || stateValue == "GU" || stateValue == "VI" || $("select[@name=billingCountry]").val() != "US" ) {
                $("#billingIsUS").css( "display", "none" );
                $("#billingIsOther").css( "display", "inline" );
                $("select[@name=shipsTo] option").removeAttr( "selected" );
                $("#shippingAddress").css( "display", "block" );
            } else {
                $("select[@name=shipsTo] option:eq(1)").attr( "selected", "selected" );
                $("#billingIsUS").css( "display", "inline" );
                $("#billingIsOther").css( "display", "none" );
                $("#shippingAddress").css( "display", "none" );
            }
        });
    }
    billingStateShipping();

    $("select[@name=billingCountry]").change( function() {
            var stateProvinceValue = ( $("select[@name=billingState]").length > 0 ) ? $("select[@name=billingState]").val() : $("input[@name=billingState]").val();
            var countryValue = $("select[@name=billingCountry]").val();
            if ( countryValue != "US" && countryValue != "" ) {
                $("#billingIsUS").css( "display", "none" );
                $("#billingIsOther").css( "display", "inline" );
                $("select[@name=shipsTo] option").removeAttr( "selected" );
                $("#shippingAddress").css( "display", "block" );
            } else {
                $("#billingIsUS").css( "display", "inline" );
                $("#billingIsOther").css( "display", "none" );
                $("#shippingAddress").css( "display", "none" );
                $("select[@name=shipsTo] option").filter(":eq(1)").attr( "selected", "selected" );
            }
            if ( countryValue != "" ) {
                $(this).attr( "disabled", "disabled" );
                addToCart.cartLoader();
                $.get(
                    "/checkout/update_state_province.cmd",
                    { stateProvince : true, billingCountry : countryValue, billingState : stateProvinceValue, timeStamp: new Date().getTime() },
                    function( d ) {
                        $(".state_province").html( d );
                        $("select[@name=billingCountry]").removeAttr( "disabled" );
                        $("#ajax-loading").remove();
                        billingStateShipping();
                    }
                );

            }
        });

        $("select[@name=shipsTo]").change( function() {
            if ( $("select[@name=shipsTo]").val() == "1" ) {
                $("#shippingAddress").css( "display", "block" );
            } else {
                $("#shippingAddress").css( "display", "none" );
            }
        });

        $("form[@name=billingForm]").submit( function() {
            if ( ($("select[@name=shipsTo]").val() == "2" || $("select[@name=shipsTo]").val() == "3") && $("#shippingAddress").css( "display" ) == "none" ) {
                sameAsMap = { billingFirstName : "shippingFirstName", billingLastName : "shippingLastName", billingAddress1 : "shippingAddress1", billingAddress2 : "shippingAddress2", billingCity : "shippingCity", billingState : "shippingState", billingZipCode : "shippingZipCode", billingPhone : "shippingPhone", billingEmail : "shippingEmail" };
                for ( k in sameAsMap ) {
                    if ( k != "billingState" ) {
                        $("input[@name=" + sameAsMap[ k ] + "]").val( $("input[@name=" + k + "]" ).val() );
                    } else {
                        $("select[@name=" + sameAsMap[ k ] + "] option").removeAttr( "selected" ).parent().find( "option[@value=" + $("select[@name=" + k + "]" ).val() + "]" ).attr( "selected", "selected" );
                    }
                }
            }
        });

    /* Cart Page Functionality */
    $(".item-errors").css("display","none");
    $(".item-errors").each(function(){
        var $this = $(this);
        if ($this.children("td").children().is("strong")){
            $this.css("display","");
            if ($this.children("td").attr("type") == "embroidery") {
              $this.next().find(".embroidery-check").attr({checked: 'checked'});
            }
        }
        if($this.attr("id") == "gift-card-error"){
            if ($("#gift-card a").attr("class") == "remove" ) {
                $(this).attr( "class", "add" ).html( "add hand written gift card" )
                .next("eq:(0)").attr({checked: ''});
            } else {
                $("#gift-card a").attr( "class", "remove" ).html( "remove hand written gift card" )
                .next("eq:(0)").attr({checked: 'checked'});
            }
        }
    });

    $(".giftwrap-check").each(function(){
        var $this = $(this);
        if ($this.is('input:checked')){
            $this.siblings("a.add").css("display","none");
            $this.siblings("a.remove").css("display","inline").click(function(){
               $this.attr({checked: ''});
               $this.siblings("a").css("display","inline");
               $this.siblings("a.remove").css("display","none");
              return false;
            });
        }
    });

    $(".cart-embroidery").css('display','none');
    $(".embroidery-check").each(function(){
        var $this = $(this);
        var p = $this;
        while(!$(p).is("tr")){
                p = $(p).parent();
        }
        if ($this.is('input:checked')){
            $(p).next().css("display","");
            $this.siblings("a.add").css("display","none");
            $this.siblings("a.remove").css("display","inline").click(function(){
               $this.attr({checked: ''});
               $this.siblings("a").css("display","inline");
               $this.siblings("a.remove").css("display","none");
               $(p).next().css("display","none");
               return false;
            });
        }else{
            $(p).next().css("display","none");
        }
    });


    $('.add','td.item-options').click(function() {
        var $this = $(this);
        var p = $this;
        while(!$(p).is("tr")){
                p = $(p).parent();
        }
        $this.siblings("input").attr({checked: 'checked'});
        if ($this.next().next().is(".embroidery-check")){
            $(p).next().css("display","");
        }
        $this.css('display','none');
        $this.next().css('display','inline');
        return false;
    });

    $('.remove','.item-options').click(function() {
        var $this = $(this);
        var p = $this;
        while(!$(p).is("tr")){
                p = $(p).parent();
        }
        if ($this.siblings(".embroidery-check").is('input:checked')){
            $(p).next().css("display","none");
        }
        $this.siblings("input").attr({checked: ''});
        $this.css('display','none');
        $this.prev().css('display','inline');
        return false;
    });



    $("a", "#shipping-insurance").click( function(){
        if ( $(this).attr( "class" ) == "remove" ) {
            toggleShippingInsurance("N");
            $(this).attr( "class", "add" ).html( "add shipping insurance" )
            .next("eq:(0)").attr({checked: ''});
        } else {
            toggleShippingInsurance("Y");
            $(this).attr( "class", "remove" ).html( "remove shipping insurance" )
            .next("eq:(0)").attr({checked: 'checked'});
        }
        return false;
    });


    $("a", "#fedex-shipping-signature").click( function(){    
        if ( $(this).attr( "class" ) == "remove" ) {
            toggleFedexSignature("N");
            $(this).attr( "class", "add" ).html( "add fedex residential delivery signature" )
            .next("eq:(0)").attr({checked: ''});
        } else {
            toggleFedexSignature("Y");
            $(this).attr( "class", "remove" ).html( "remove fedex residential delivery signature" )
            .next("eq:(0)").attr({checked: 'checked'});
        }
        return false;
    });

    $("a", "#gift-card").click( function(){
        if ( $(this).attr( "class" ) == "remove" ) {
            $(this).attr( "class", "add" ).html( "add hand written gift card" )
            .next("eq:(0)").attr({checked: ''});
        } else {
            $(this).attr( "class", "remove" ).html( "remove hand written gift card" )
            .next("eq:(0)").attr({checked: 'checked'});
        }
        return false;
    });

    if ($("input[@name=specialPromotion]").is('input:checked')){

         $("a", "#special-offer").attr( "class", "add" ).html( "add special offer" );
    }

    $("a", "#special-offer").click( function(){
        if ( $(this).attr( "class" ) == "remove" ) {
            $(this).attr( "class", "add" ).html( "add special offer" )

           $("input[@name=specialPromotion]").attr({checked: 'checked'});
        } else {
            $(this).attr( "class", "remove" ).html( "remove special offer" );
           $("input[@name=specialPromotion]").attr({checked: ''});
        }
        return false;
    });
     $(".update-items").click( function(){
            document.itemsForm.act.value = "update";
            document.itemsForm.submit();
            return false;
        });
    $('.update-total a').click(function() {
        submitForm('update');
        return false;
    });
    $('.update').click(function() {
        submitForm('update');
        return false;
    });
    //edited for sizing tad
    $('a.learn').click(function() {
        openWindow(this.href, 520, 505);
        return false;
    });
	//edited for sizing tad
     $('a.embroidery').click(function() {
        openWindow(this.href, 520, 440);

        return false;
    });
	//edited for sizing tad
    $('a#cvv-code').click(function() {
        openWindow(this.href, 400, 250);

        return false;
    });
	//edited for sizing tad
    $('a.info, a.question').click(function() {
        openWindow(this.href, 784, 600);
        return false;
    });
    
    $('.checkout').click(function() {
        submitForm('checkout');
        return false;
    });
    $('.checkout-login').click(function() {
        submitForm('logincheckout');
        return false;
    });
    $('.wish').click(function() {
        convertToWishList();
        return false;
    });
    $('.registry').click(function() {
        convertToRegistry();
        return false;
    });
    $('.continue').click(function() {
        submitForm('continueShopping');
        return false;
    });
    
    $('.check-balance').click(function() {
        checkBalance();
        return false;
    });

   function changePaymentType(){
        var value = document.itemsForm.ccType.value;
        document.itemsForm.act.value = "payment_type_swap";
        document.itemsForm.submit();
    }

	//edited for sizing tad
	function checkBalance(){
		var url = "/checkout/gift_balance.jsp?redemptionCode=" + document.itemsForm.gcRedemptionCode.value;
		url += "&firstTime=true";
		openWindow(url, 450, 260);
	}

    function convertToRegistry() {
        document.itemsForm.action = "/checkout/convert_to_gift_registry.cmd";
        document.itemsForm.submit();
    }


    function convertToWishList() {
        document.itemsForm.action = "/checkout/convert_to_wish_list.cmd";
        document.itemsForm.submit();
    }



    function toggleShippingInsurance(input) {
        document.itemsForm.shippingInsurance.value = input;
    }

    function toggleFedexSignature(input) {
        document.itemsForm.shippingSignature.value = input;
    }

    /* Textarea for gift card limit */
    var textLimit = {
        init: function() {
            textLimit.remaining = textLimit.max - $(textLimit.obj).val().length;
            if (textLimit.remaining < 0) {
                $(textLimit.obj).val($(textLimit.obj).val().substring(0,textLimit.max));
            }
            $(textLimit.obj).siblings(".remaining").html(textLimit.remaining + " characters remaining.");
        },
        max: null,
        remaining: null,
        obj: null
    };
    $(".giftcard-limit").each(function() {
        $(this).focus(function() {
            var c = $(this).attr("class");
            textLimit.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
            textLimit.obj = this;
            iCount = setInterval(textLimit.init,1000);
        }).blur(function() {
            textLimit.init();
            clearInterval(iCount);
        });
    });
    /* Registration Succes */
    if($("#registered").length > 0){
        setTimeout("registered()",2000);
    }

});

function registered(){
    $('#registered').fadeOut("slow");
}