﻿/// <reference path="Plugins/jquery-1.3.2-vsdoc.js" />
$(document).ready(function() {

    var heightOnGoodsMark = $(".checkout-spec .box.last .wrap").height();
    if ($(".checkout-spec .box.last .wrap ul").height() > 100) {
        $(".checkout-spec .box .wrap").height(heightOnGoodsMark);
        heightOnGoodsMark = $(".checkout-spec .box.last .wrap ul").height();
        $(".checkout-spec .box .wrap ul").height(heightOnGoodsMark);
    }
    $(".checkout .order").click(function() {
        if ($("#HaveReadOrderTerms").attr("checked") == false) {
            $(".termsOfAgreement").addClass("error");
            $(".termsOfAgreement").removeClass("no-error");
            return false;
        } else {
        $(".termsOfAgreement").removeClass("error");
        $(".termsOfAgreement").addClass("no-error");
        }

    });
    var frame = GetQueryStringValue("frame");
    if (frame == "edit") {
        $("body").addClass("inner-thickbox");
    }
    $("#HaveReadOrderTerms").click(function() {
        if ($("#HaveReadOrderTerms:checked").size() == 1) {
            $(".termsOfAgreement").removeClass("error");
            $(".termsOfAgreement .field-validation-error").hide();
        } else {
            $(".termsOfAgreement").addClass("error");
            $(".termsOfAgreement .field-validation-error").show();
        }
    });

    $(".address").each(function() {
        $(this).removeClass("selected");
        if ($(this).attr("id") == $("#selectedShippingAddressID").val()) {
            $(this).addClass("selected");
        }
    });
    $(".address .address-select").click(function() {
        var addressID = $(this).attr("title");
        $(".address").each(function() {
            $(this).removeClass("selected");
        });
        $(this).parent().addClass("selected");

        $.post("/Checkout/SetShippingAddress", { addressId: addressID }, function(data) {
            parent.location.reload(1);
        });

        $("#selectedShippingAddressID").val($(this).children(".shippingAddressID").val());
    });
    $(".link-add-icon").click(function() {
        $(".addresses").animate({ scrollTop: 0 }, 'slow');
        $(".address-add").slideDown();
        $(".disabled").show();
        Initialize();
    });
    $(".address-edit").click(function() {
        $(".disabled").hide();
        $(".address-add").slideUp();
        $(".address-edit").each(function() {
            $(this).siblings(".editAddress").hide();
        });
        $(this).parent().siblings(".editAddress").show();
        Initialize();
    });

    $(".address-search input").keyup(function() {
        $(".addresses").children(".address").each(function() {
            var searchValue = $(".address-search input").val();
            var thisValue = $(this).children("ul").html();

            // Search the charecters.
            if (thisValue.search(new RegExp(searchValue, "i")) === -1) {
                $(this).hide();
            } else {
                $(this).show();
            }
        });
        if ($(".address:visible").size() == 0) {
            $(".no-addresses").show();
        } else {
            $(".no-addresses").hide();
        }
    });

    var idToEdit = $("#lastAddressEdited").val();
    if ($("#" + idToEdit).size() > 0) {
        $(".addresses").animate({ scrollTop: $("#" + idToEdit).offset().top - 58 }, 'slow');
    }
    var printPage = GetQueryStringValue("print");
    if (printPage == 'True') {
        window.print();
    }














    //    $(".klarnaInvoiceAddress").children("div").hide();
    //    // Ny kod.....
    //    $(".shippingAddresses").children("div").each(function() {
    //        if ($(this).children("input").attr("checked") === true) {
    //            $(this).addClass("active");
    //            CopyAddress(this);
    //            // Sctoll down to item.
    //            var scrollTo = $(this).offset().top - $(".shippingAddresses").offset().top;
    //            $(".shippingAddresses").scrollTop(scrollTo);

    //        } else {
    //            $(this).removeClass("active");
    //        }
    //    });
    //    if (IsAnyAddressSelected() != true) {
    //        var first = ".shippingAddresses div:first";
    //        $(first).children("input").attr("checked", true);
    //        $(first).addClass("active");
    //        CopyAddress($(first));
    //    }

    //    // We hide the shippingAddress div if there is only one.
    //    var intAddresses = $(".shippingAddresses").children("div").size();
    //    if (intAddresses == 1) {
    //        $(".shippingAddresses").hide();
    //    }

    //    $(".addressSearch").keyup(function() {
    //        $(".shippingAddresses").children("div").each(function() {
    //            var searchValue = $(".addressSearch").val();
    //            var thisValue = $(this).children("input").attr("title");

    //            // Search 
    //            if (thisValue.search(new RegExp(searchValue, "i")) === -1) {
    //                $(this).hide();
    //                if ($(this).children("input").attr("checked") === true) {
    //                    $("#ShippingAddress_Street").val("");
    //                    $("#ShippingAddress_PostalCode").val("");
    //                    $("#ShippingAddress_City").val("");
    //                    $("input[name=TemporaryDeliveryCity]").val("");
    //                    Clean();
    //                }
    //            } else {
    //                $(this).show();
    //            }
    //        });
    //    });
    //    $(".shippingAddresses > div").click(function() {
    //        $(this).children("input").attr("checked", true);
    //        $(this).addClass("active");
    //        $(this).siblings("div").each(function() {
    //            $(this).removeClass("active");
    //        });
    //        CopyAddress(this);
    //    });

    //    $("#ShippingAddress_Street").keydown(function() {
    //        Clean();
    //    });
    //    $("#ShippingAddress_City").keydown(function() {
    //        Clean();
    //    });
    //    $("#ShippingAddress_PostalCode").keydown(function() {
    //        Clean();
    //    });
    //    $("#ShippingAddress_PostalCode").keydown(function(event) {
    //        var countryIsoCode = $("#CountryIsoCode").val();
    //        SetSpace($(this), countryIsoCode, event);
    //    });
    //    $("#ShippingAddress_PostalCode").keyup(function() {
    //        var countryIsoCode = $("#CountryIsoCode").val();
    //        if ($(this).val().length > 3) {
    //            $.post("/SelfRegister/GetValidCity", { zipCode: $(this).val(), country: countryIsoCode }, function(data) {
    //                SetValue(data.city, "#ShippingAddress_City", "input[name=TemporaryDeliveryCity]");
    //                SetValue(data.state, "#ShippingAddress_State", null);
    //                SetValue(data.county, "#ShippingAddress_County", null);
    //                SetValue(data.countryisocode, "#ShippingAddress_CountryIsoCode", null);
    //            });
    //        }
    //        else {
    //            SetValue(null, "input[name=ShippingAddress.City]", "input[name=TemporaryDeliveryCity]");
    //        }
    //    });
});
function Initialize() {
    $('.editAddress .generalError').hide();
    $('.validationrule\\.required:visible,.validationrule\\.optional:visible').each(function() {
        //initilize serverErrorfields
        if ($(this).is('.validationrule\\.required.serverError:visible')) {
            serverErrorView($(this));
        }
        //initilize required fields
        else if ($(this).is('.validationrule\\.required:visible')) {
            requiredView($(this));
        }

        //initilize optional fields
        else if ($(this).is('.validationrule\\.optional:visible')) {
            optionalView($(this));
        }
    });
    //Initial validation
    $('.validationrule\\.required').each(function() {
        if ($(this).is('.validationtype\\.text')) {
            if (validateText($(this)))
                okView($(this));
            else
                errorView($(this));
        }
    });
}














//function Clean() {
//    $(".shippingAddresses").children("div").each(function() {
//        $(this).children("input").attr("checked", false);
//        $(this).removeClass("active");
//    });
// }
// function CopyAddress(elementToSet) {
//     $("#ShippingAddress_Street").val($(elementToSet).children(".street").children("input").val());
//    $("#ShippingAddress_PostalCode").val($(elementToSet).children(".postalcode").html());
//    $("#ShippingAddress_City").val($(elementToSet).children(".city").html());
//    $("#ShippingAddress_County").val($(elementToSet).children(".county").html());
//    $("#ShippingAddress_State").val($(elementToSet).children(".state").html());
//    $("#ShippingAddress_CountryIsoCode").val($(elementToSet).children(".countryisocode").html());
//    $("input[name=TemporaryDeliveryCity]").val($("input[name=ShippingAddress.City]").val());
//}
//function SetValue(value, elementToSet, tempElementToSet) {
//    if (value != null) {
//            $(elementToSet).val(value);
//            $(tempElementToSet).val(value);
//    } 
//    else 
//    {
//        $(elementToSet).val("");
//        $(tempElementToSet).val("");
//    }
// }
// function SetSpace(element, countryIsoCode, event) 
// {
//     if (event.keyCode != '8' && event.keyCode != '32') 
//     {
//         // Diffrent space rools for diffrent countrys.
//         if (countryIsoCode == "SE") 
//         {
//             if (element.val().length === 3) 
//             {
//                 element.val(element.val() + " ");
//             }
//         }
//     }
// }

// function IsAnyAddressSelected() {
//     var boolIsSelected = false;
//     $(".shippingAddresses").children("div").each(function() {
//         if ($(this).children("input").attr("checked") === true) {
//             boolIsSelected = true;
//         }
//     });
//     if (boolIsSelected === true) {
//         return true;
//     }
//     return false;
// }


// function shoppingCartAreaLoad() {
//     alert(2);
// }
