﻿/// <reference path="jquery-1.3.2-vsdoc.js" />

var isClicked = false;

$(function () {

    $("#txtdiscount").defaultvalue($("#txtdiscount").attr("defaultvalue"));
    $("#txtgift").defaultvalue($("#txtgift").attr("defaultvalue"));
    $("#savecartname").defaultvalue($("#savecartname").attr("defaultvalue"));

    if ($("#existing_cart_name").val() != "") $("#savecartname").val($("#existing_cart_name").val());

    $("#applycodes").click(function () {

        if ($("#txtgift").val() == $("#txtgift").attr("defaultvalue")) {
            $("#txtgift").val("");
        }
        if ($("#txtdiscount").val() == $("#txtdiscount").attr("defaultvalue")) {
            $("#txtdiscount").val("");
        }
        if ($("#savecartname").val() == $("#savecartname").attr("defaultvalue")) {
            $("#savecartname").val("");
        }

        var giftCode = $("#txtgift").val();
        if (giftCode.length < 9 && giftCode.length > 0) {
            var numZeros = 9 - giftCode.length;

            for (i = 0; i < numZeros; i++) {
                giftCode = "0" + giftCode;
            }
            $("#txtgift").val(giftCode);
        }
    });

    $(".linkremove").click(function (event) {
        if ($(this).attr("clid") != null && $(this).attr("clid").length > 0) {
            event.preventDefault();
            $("#delete_cart_line_id").val($(this).attr("clid"));
            $("#frmcart").submit();
        }
    });

    $("#updateqty").bind('click', function (event) {
        event.preventDefault();
        submitForm("frmcart", "/shop.axd/UpdateCart");
    });

    $("#savecart").click(function (event) {
        event.preventDefault();
        submitForm("frmnewcart", "/shop.axd/SaveNamedCart");
    });

    $("#savecartfrompopup").click(function (event) {
        isClicked = true;
        $("#formsavecartprompt").submit();
    });    

    //Events to track as internal links that shouldn't trigger the pop up below

    $("#frmsearch").submit(function() {
        killSaveCartPrompt();
    });

    $("#frmapplycodes").submit(function() {
        killSaveCartPrompt();
    });

    $("#frmmaillist").submit(function() {
        killSaveCartPrompt();
    });
    
    $('a').click(function () {
        killSaveCartPrompt();
    });

    $('#dontGoDialog').dialog({
        modal: true,
        autoOpen: false,
        title: 'There are items in your cart',
        closeText: '',
        closeOnEscape: true,
        draggable: true,
        resizable: false,
        width: 300
    });

    $(window).bind("beforeunload", function (evt) {
        if (!isClicked && $("#existing_cart_name").val() == "") {
            $('#dontGoDialog').dialog({
                position: [$(window).width / 2 - $(this).width, $(window).height / 2 - $(this).height / 2]
            });

            $('#dontGoDialog').dialog('open');

            var returnValue = "Would you like to save this cart so you can return to it later? Click 'Cancel' or 'Stay on this page' below to save your cart.";

            return returnValue;
        }
    });
});

function killSaveCartPrompt()
{
    $('#dontGoDialog').dialog('close');
    isClicked = true;
}

function submitForm(formId, formAction) {
    
    if ($("#" + formId).length > 0) {
        $("#" + formId).attr("action", formAction);
        $("#" + formId).submit();
    }    
}



