﻿function popSurvey(survey) {
    window.open("http://survey.dansko.com/" + survey, "SURVEY", "width=600,height=500,top=50,left=50,resizable=yes,scrollbars=yes,status=no,toolbar=no,location=no,menu=no");
}
function hollaBack() {
    clearInterval(timer);
    $("#enterme").load("/ajax/newsletter.aspx #answer");
}
var timer;
var junk = Number(new Date()); //timestamp for ie non-sense - not clearing cache...
$(document).ready(function () {
    $.ajaxSetup({
        cache: false
    });
//    $("#topsearch").autocomplete("/ajax/autocomplete.aspx", { max: 8, selectFirst: false, selectCurrent: false });
//    var doonce = 0;
//    if (doonce == 0) {
//        doonce = 1;
//        $("#topsearch").focus(function () {
//            $(this).val("");
//        });
//    }
    $("#search_button").click(function () {
        if ($("#topsearch").val() != "" && $("#topsearch").val() != "Search") {
            var url = "/Search/?searcher=" + $("#topsearch").val();
            document.location.href = url;
        }
    });
//    $("#topsearch").keyup(function (e) {
//        if (!e) {
//            e = window.event;
//        }
//        if (e.which == 13) {
//            if ($("#topsearch").val() != "" && $("#topsearch").val() != "Search") {
//                var url = "/Search/?searcher=" + $("#topsearch").val();
//                document.location.href = url;
//            }
//        }
//    });
    $("#quickgender").change(function () {
        $("#quicksizes").load("/ajax/quicksearch.aspx?gender=" + $("#quickgender").val() + " #response", function (response, status, xhr) {
            //alert(response);
        });
    });
    $("#quicksize").live("change", function () {
        var url = "/" + $("#quickgender").val() + "/Footwear/View All/Filter/Size/" + $("#quicksize").val() + "/";
        document.location.href = url;
    });
    $(".navbox").mouseenter(function () {
        $(this).children(".subnav").show();
    });
    $(".navbox").mouseleave(function () {
        $(this).children(".subnav").hide();
    });
    $(".navbox a").click(function () {
        $(".navbox").children(".subnav").hide();
    });
    $(".navcart").click(function () {
        $("#maincart").fadeIn();
    });
    $("#maincart").live("mouseleave", function () {
        //$("#maincart").fadeOut();
    });
    $(".cartclick").mouseover(function () {
        $("#maincart").show();
    });
    $(".cartclick").click(function () {
        $("#maincart").fadeOut();
    });
    $("#continue").click(function () {
        $("#maincart").fadeOut();
    });
    $("#checkout").click(function () {
        document.location.href = "/Checkout/";
    });
    $("#cartstuff").load("/ajax/cart.aspx?junk=" + junk + " #cartinfo", function (response, status, xhr) {
        var r = $(response).find("#cartinfo").html();
        $("#cartwait").hide();
        if (r == "<p>There are currently no items in your cart.</p>") {
            $("#cartstuff").find("#checkout").hide();
        }
    });
    $("#clearcart").click(function () {
        junk = Number(new Date()); 
        $(".cartitems").html("0");
        $("#cartstuff").load("/ajax/cart.aspx?junk=" + junk + "&clear=yes #cartinfo", function (response, status, xhr) {
            var r = $(response).find("#cartinfo").html();
            if (r == "<p>There are currently no items in your cart.</p>") {
                $("#cartstuff").find("#checkout").hide();
            }
        });
    });
    $("#getship").live("click", function () {
        junk = Number(new Date()); 
        var zippy = $("#shipzip").val();
        if (checkZip(zippy)) {
            $("#shipwait").fadeIn();
            var servicey = $("#shippingcode").val();
            var carttotal = $("#subtotal").html().replace("$", "");
            $.ajax({
                type: "POST",
                url: "/ajax/cart_fedex.aspx?junk=" + junk,
                data: "zip=" + zippy + "&service=" + servicey,
                success: function (msg) {
                    $("#shipwait").fadeOut();
                    var $dialog = $('<div></div>');
                    if (msg == "NA") {
                        $dialog.html("We were unable to get an estimate at this time.");
                        $dialog.dialog({ modal: true, title: "Estimate Not Available", position: ['center', 100] });
                        $("#shippingcode").val(shipindex);
                    } else if (msg == "SNA") {
                        $dialog.html("The service you have selected is not available for your location.");
                        $dialog.dialog({ modal: true, title: "Service Not Available", position: ['center', 100] });
                        $("#shippingcode").val(shipindex);
                    } else if (msg == "IPC") {
                        $dialog.html("Please check your zip code.");
                        $dialog.dialog({ modal: true, title: "Zip Code Error", position: ['center', 100] });
                        $("#shippingcode").val(shipindex);
                    } else {
                        shipindex = servicey;
                        $("#shipcost").html("$" + msg);
                        carttotal = (carttotal - 0) + (msg - 0);
                        carttotal = Math.round(carttotal * 100) / 100;
                        $("#carttotal").html("$" + carttotal);
                    }
                }
            });
        } else {
            var $dialog = $('<div></div>');
            $dialog.html("Please check your zip code.");
            $dialog.dialog({ modal: true, title: "Zip Code Error", position: ['center', 100] });
        }
    });
    $("#newsemail").focus(function () {
        $(this).val("");
        $(this).css("color", "#0000ff");
    });
    $("#eoccupation").change(function () {
        if ($(this).val() == "Other") {
            $("#eother").fadeIn();
            $("#eother").focus();
        } else {
            $("#eother").fadeOut();
        }
    });
    $("#epopsub").click(function () {
        var email = $("#eemail").val();
        var name = $("#ename").val();
        var city = $("#ecity").val();
        var state = $("#estates").val();
        var country = $("#ecountry").val();
        var occupation = $("#eoccupation").val();
        var other = $(".eother").val();
        var gender = $(".egender:checked").val();
        var age = $("#eage").val();
        var source = $("#esource").val();
        if (checkEmail(email)) {
            if ((city != "" && state != "") || (country != "")) {
                $("#eresponse").load("/ajax/newsletter.aspx", { email: email, name: name, city: city, state: state, country: country, occupation: occupation, other: other, gender: gender, age: age, source: source }, function (response, status, xhr) {
                    if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        $("#eresponse").html(msg + xhr.status + " " + xhr.statusText);
                    } else {
                        $("#eresponse").html(response);
                        $("#signuppop").delay(5000).fadeOut();
                    }
                });
            } else {
                alert("City and State or Country are required.");
            }
        } else {
            alert("Please check to see that your email is valid.");
        }
    });
//    $("#popsub").click(function () {
//        $("#signuppop").fadeIn();
    //    });
    // Click handler for email signup in footer
    $("#popsub").live("click", function () {
        showSignUp(-250, -100);
    });
    // FadeIn the email signup form
    function showSignUp(marginTop, marginLeft) {
        var popup = $("#signuppop");
        popup.css("margin-top", marginTop + "px");
        popup.css("margin-left", marginLeft + "px");
        popup.stop();
        popup.fadeIn();
    }
    $(".eclose").live("click", function () {
        $("#signuppop").stop();
        $("#signuppop").fadeOut();
    });
    $(".ofaq").click(function () {
        var ans = $(this).attr("ans");
        if ($("#ans" + ans).css("display") == "none") {
            $("#ans" + ans).show();
        } else {
            $("#ans" + ans).hide();
        }

    });
    $(".fbigclick").live("click", function () {
        $("#flighter").fadeOut();
        $("#finnerlight").hide();
        $("#finnerlight").html("");
    });
    $("#flighter").hide();
    $("#finnerlight").hide();
    var ttemp = $("#friendform").html();
    $("#sandalicon").click(function () {
        lightMe(ttemp, 600);
    });
    function lightMe(x, w) {
        $("#flighter").fadeIn();
        $("#flighter").width($(document).width());
        $("#flighter").height($(document).height());
        $("#finnerlight").width(w + "px");
        $("#finnerlight").html(x);
        var mtop = 150;
        $("#finnerlight").css("margin-top", mtop + "px");
        $("#finnerlight").fadeIn();
    }
    $("#tellafriend").live("click", function () {
        var femail = jQuery.trim($("#femail").val());
        var yemail = jQuery.trim($("#yemail").val());
        var tellme = "";
        var furl = "http://sandals.dansko.com";
        var fstyle = "";
        var fsku = "";
        var qstring = "item=" + tellme + "&style=" + fstyle + "&sku=" + fsku + "&url=" + furl + "&";
        if ((femail != "" && yemail != "") && (checkEmail(yemail))) {
            qstring += "yemail=" + yemail;
            if (femail.indexOf(";") > 0) {
                var fe = femail.split(";");
                for (var i = 0; i < fe.length - 1; i++) {
                    if (checkEmail(fe[i])) {
                        qstring += "&femail=" + fe[i];
                    } else {
                        alert("You must supply valid emails for yourself and your friends.");
                        return false;
                    }
                }
            } else if (checkEmail(femail)) {
                qstring += "&femail=" + femail;
            } else {
                alert("You must supply valid emails for yourself and your friend(s).");
                return false;
            }
            var url = "/ajax/tellafriend.aspx?" + qstring;
            url = url.replace(/\s/gi, "%20");
            $("#befriendly").load(url + " #results");
        } else {
            alert("You must supply valid emails for yourself and your friend.");
            return false;
        }
    });
    function checkEmail(eMailer) {
        var check = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        if (check.test(eMailer)) {
            return true;
        } else {
            return false;
        }
    }
    function checkZip(zipper) {
        var check = /^\d{5}$/;
        if (check.test(zipper)) {
            return true;
        } else {
            return false;
        }
    }
    // JavaScript Document
    // Credit Card Validation Javascript
    // copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd

    // You have permission to copy and use this javascript provided that
    // the content of the script is not changed in any way.
    function validateCreditCard(s) {
        var v = "0123456789";
        var w = "";
        for (var i = 0; i < s.length; i++) {
            x = s.charAt(i);
            if (v.indexOf(x, 0) != -1)
                w += x;
        }
        var j = w.length / 2;
        if (j < 6.5 || j > 8 || j == 7) return false;
        var k = Math.floor(j);
        var m = Math.ceil(j) - k;
        var c = 0;
        for (var i = 0; i < k; i++) {
            a = w.charAt(i * 2 + m) * 2;
            c += a > 9 ? Math.floor(a / 10 + a % 10) : a;
        }
        for (var i = 0; i < k + m; i++) c += w.charAt(i * 2 + 1 - m) * 1;
        return (c % 10 == 0);
    }
});
