﻿$(function () {

    var amounts = [25, 50, 75, 100];
    var descriptions = ['Gift on Wheels', 'Winter Scene', 'Dog GC', 'Irving Canopy GC'];
    var cw;

    // Limit input in this text box to integers only
    // http://github.com/SamWM/jQuery-Plugins (MIT license)
    $("td.quant .number input").numeric();
    $("#cc-number").numeric();
    $("#cc-code").numeric();

    $(".price-list li a").click(function (evt) {
        evt.preventDefault();
    });

    // Add to cart button
    $("a.btn-add").click(function (event) {
        event.preventDefault();

        // Get the currently selected card type (0 = Gift on Wheels, etc.)
        var card_type = $(".frame ul li.active").index();

        // Get the selected amount. Because the default is $25, 
        // there will never be a case where the price is not selected
        var card_amount_index = $(".price-list li.active").index();
        var card_amount = amounts[card_amount_index];

        var card_description = descriptions[card_type];

        // Set the form values
        $("#cardtype").val(card_type);
        $("#amount").val(card_amount);
        $("#description").val(card_description);
        $("#price").val(card_amount);
        $("#product").val(card_description);
        $("#quantity").val(1); // Default to 1 - user can update quantity later
        $("#method").val("add");
        window.open('', 'cw');
        // Submit the form
        $("#giftcardform").submit();

    });

    // Remove the item by setting its quantity to 0.
    $(".product span").click(function () {
        var card_description = $(this).siblings("strong").text();
        var card_amount = $(this).parent().siblings(".price").text();

        // Remove leading '$'
        card_amount = card_amount.substring(1);

        $("#description").val(card_description);
        $("#product").val(card_description);
        $("#amount").val(card_amount);
        $("#price").val(card_amount);
        $("#quantity").val(0); // 0 means remove
        $("#method").val("remove");

        // Submit the form
        $("#giftcardform").submit();

    });

    $("td.quant .number input").focusout(function () {
        var card_description = $(this).parent().parent().siblings(".product").children("strong").text();
        var card_amount = $(this).parent().parent().siblings(".price").text();

        // Remove leading '$'
        card_amount = card_amount.substring(1);

        // Don't update if the field is blank
        if ($(this).val() == '') {
            return;
        }

        $("#description").val(card_description);
        $("#product").val(card_description);
        $("#amount").val(card_amount);
        $("#price").val(card_amount);
        $("#quantity").val($(this).val());
        $("#method").val("update");

        // Submit the form
        $("#giftcardform").submit();
    });

    $("#sameAddress").click(function () {
        if ($(this).is(':checked')) {

            // Only when checked, copy the values over from the billing fields
            // to the shipping fields

            $("#shipFirstName").val($("#billFirstName").val());
            $("#shipLastName").val($("#billLastName").val());
            $("#shipCompany").val($("#billCompany").val());
            $("#shipAddress1").val($("#billAddress1").val());
            $("#shipAddress2").val($("#billAddress2").val());
            $("#shipAddress3").val($("#billAddress3").val());
            $("#shipCity").val($("#billCity").val());
            $("#shipState").val($("#billState").val());
            $("#shipZip").val($("#billZip").val());
            $("#shipCountry").val($("#billCountry").val());
            $("#shipPhone").val($("#billPhone").val());
            $("#shipFax").val($("#billFax").val());
            $("#shipEmail").val($("#billEmail").val());
        }
    });

    $("#continue-address").click(function () {
        // Form validation
        // Required fields: email, name, address 1, city, zip, billing phone

        var hasShipEmail = ($("#shipEmail").val() !== '');
        var hasBillEmail = ($("#billEmail").val() !== '');
        var hasShipFirstName = ($("#shipFirstName").val() !== '');
        var hasBillFirstName = ($("#billFirstName").val() !== '');
        var hasShipLastName = ($("#shipLastName").val() !== '');
        var hasBillLastName = ($("#billLastName").val() !== '');
        var hasShipAddr = ($("#shipAddress1").val() !== '');
        var hasBillAddr = ($("#billAddress1").val() !== '');
        var hasShipCity = ($("#shipCity").val() !== '');
        var hasBillCity = ($("#billCity").val() !== '');
        var hasShipZip = ($("#shipZip").val() !== '');
        var hasBillZip = ($("#billZip").val() !== '');
        var hasBillPhone = ($("#billPhone").val() !== '');

        // Also check that email address is in a valid format
        var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
        var validShipEmail = pattern.test($("#shipEmail").val());
        var validBillEmail = pattern.test($("#billEmail").val());

        if (!(hasShipEmail && hasBillEmail && hasShipFirstName && hasBillFirstName
                && hasShipLastName && hasBillLastName
                && hasShipAddr && hasBillAddr && hasShipCity && hasBillCity
                && hasShipZip && hasBillZip && hasBillPhone && validShipEmail && validBillEmail)) {

            $("#errors").empty(); // Remove any previous errors

            $("#errors").append("The following errors occurred:<br /><ul></ul>");

            if (!hasBillFirstName) {
                $("#errors ul").append("<li>Billing First Name is required</li>");
            }

            if (!hasShipFirstName) {
                $("#errors ul").append("<li>Shipping First Name is required</li>");
            }

            if (!hasBillLastName) {
                $("#errors ul").append("<li>Billing Last Name is required</li>");
            }

            if (!hasShipLastName) {
                $("#errors ul").append("<li>Shipping Last Name is required</li>");
            }

            if (!hasBillAddr) {
                $("#errors ul").append("<li>Billing Address is required</li>");
            }

            if (!hasShipAddr) {
                $("#errors ul").append("<li>Shipping Address is required</li>");
            }

            if (!hasBillCity) {
                $("#errors ul").append("<li>Billing City is required</li>");
            }

            if (!hasShipCity) {
                $("#errors ul").append("<li>Shipping City is required</li>");
            }

            if (!hasBillZip) {
                $("#errors ul").append("<li>Billing Zip Code is required</li>");
            }

            if (!hasShipZip) {
                $("#errors ul").append("<li>Shipping Zip Code is required</li>");
            }

            if (!hasBillPhone) {
                $("#errors ul").append("<li>Billing Phone Number is required</li>");
            }

            if (!hasBillEmail) {
                $("#errors ul").append("<li>Billing Email Address is required</li>");
            }

            if (!hasShipEmail) {
                $("#errors ul").append("<li>Shipping Email Address is required</li>");
            }

            if (!validBillEmail) {
                $("#errors ul").append("<li>Billing Email Address is invalid</li>");
            }

            if (!validShipEmail) {
                $("#errors ul").append("<li>Shipping Email Address is invalid</li>");
            }

            $("#errors").css('display', 'block');
        }
        else {
            $("#method").val("address");
            $("#addressform").submit();
        }
    });

    $("#back-address").click(function () {
        $("#method").val(""); // An empty method sends the user back to the enter address form
        $("#ccform").submit();
    });

    $("#continue-cc").click(function () {
        var hasCCNum = ($("#cc-number").val() !== '');
        var hasCCCode = ($("#cc-code").val() !== '');

        if (!(hasCCNum && hasCCCode)) {

            $("#errors").empty(); // Remove any previous errors

            $("#errors").append("The following errors occurred:<br /><ul></ul>");

            if (!hasCCNum) {
                $("#errors ul").append("<li>Credit Card Number is required</li>");
            }

            if (!hasCCCode) {
                $("#errors ul").append("<li>Security Code is required</li>");
            }

            $("#errors").css('display', 'block');
        }
        else {
            $("#method").val("process-cc");
            $("#ccform").submit();
        }
    });

    $("#submit-order").click(function () {
        $("#method").val("submit");
        $("#submitform").submit();
    });

    $("#order-login").click(function () {
        $("#method").val("login");
        $("#admin-login").submit();
    });

    $("#order-logout").click(function () {
        $("#method").val("logout");

        if ($("#admin-login").length > 0) {
            $("#admin-login").submit();
        }
        else if ($("#admin-list").length > 0) {
            $("#admin-list").submit();
        }
    });

    if ($("#order-tabs").length > 0) {
        $("#order-tabs").tabs();
    }

    $("#pending-orders a.order-edit").click(function () {
        var index = $("#pending-orders tr td a").index($(this));
        var order_id = $($("#pending-orders tr")[index + 1]).children('td').first().text(); // Add 1 because the first <tr> contains <th>, not <td>

        $("#orderid").val(order_id);
        $("#method").val("detail");
        $("#admin-list").submit();
    });

    $("#completed-orders a.order-edit").click(function () {
        var index = $("#completed-orders tr td a").index($(this));
        var order_id = $($("#completed-orders tr")[index + 1]).children('td').first().text(); // Add 1 because the first <tr> contains <th>, not <td>

        $("#orderid").val(order_id);
        $("#method").val("detail");
        $("#admin-list").submit();
    });

    $("#update-order-status").click(function () {
        $("#method").val("update-status");
        $("#update-status").submit();
    });

    $("#order-detail-back").click(function () {
        $("#method").val(""); // Empty method goes to overview screen
        $("#update-status").submit();
    });

    $("#location-search").click(function () {
        $("#method").val("search");        
        $("#location-form").submit();
    });

    $("#new-search").click(function () {
        $("#method").val("");
        $("#re-search-form").submit();
    });

    $(".results-next").click(function () {
        $("#method").val("next");
        $("#re-search-form").submit();
    });

    $(".results-prev").click(function () {
        $("#method").val("prev");
        $("#re-search-form").submit();
    });

    $("#postal-code").val(""); // Reset the postal code



});
