$(document).ready(function () {
	$("input").placeholder();
	// Add a regex function to jquery validate
	$.validator.addMethod("password",function(value,element){
		return this.optional(element) || /^.*(?=.{8,})(?=.*\d)(?=.*[a-zA-Z]).*$/i.test(value);
    },"Invalid password.  Passwords must be 8 characters long and consist of at least 1 number and 1 letter.");


    // Script Related to Logging in.
    // Login Dialog creation.
    $("#login-Dialog").dialog({ autoOpen: false, resizable: false, draggable: false, zIndex: 5000 }).parents('.ui-dialog:eq(0)');
    $("#Bulk-Buy-Div").dialog({ autoOpen: false, modal: true, zIndex: 6000, width: 900, position: 'center', buttons: { "Continue Shopping": function () { $(this).dialog("close"); }, "Go To Cart": function () { document.location.href = "Shopping-cart.asp"; } } }).parents('.ui-dialog:eq(0)').wrap('<div class="darkblue" style="font-size:60%;" ></div>');

    // Used by all cart-adding buttons for styling.
    $(".theCartbutton").button({ icons: { primary: 'ui-icon-cart'} });
    // Used by all "Next/Pricing & Detail" Buttons - Grabs a hidden input value to generate the href.
    $(".btnNext", ".darkblue").button({ icons: { primary: 'ui-icon-circle-triangle-e'} })
    .bind('click', function () {
        document.location.href = $(this).next().val();
    });

    $("#ccpImg").one('mouseenter', function () {
        //$("#ccpImg").append('<span><ul><li>Scissors easily cut without fray!</li><li>Study Construction!</li></span>');
        $("#ccpImg").wrap('<div id="infoFloat" class="shadow-bringer shadow"></div>');
        $("#infoFloat").append("<div id='infoBullets' style='float: right;'>1. Scissors easily cut without fray!<br/>2. Study Construction!</div>");
    });
    $("#ccpImg").one('mouseleave', function () {
        $("#infoBullets").remove();
        $("#ccpImg").unwrap();
    });

    $("#login")
    .bind('click', function () {
        if ($(this).children("#loginText").html() == "Log In") {
            loginDialogOpen(($(this).position().left-75), ($(this).position().top + 29));
        }
        else {
            $.ajax({ type: "GET", url: "header-login-validation.asp?v=Logout&randomAjax=" + Math.random(), success: function () {
                if ((window.location.pathname).toLowerCase() == "/heat-shrink-printing-service.asp" || (window.location.pathname).toLowerCase() == "/shippingaddress.asp" || (window.location.pathname).toLowerCase() == "/shipping.asp" || (window.location.pathname).toLowerCase() == "/payment.asp" || (window.location.pathname).toLowerCase() == "/profile.asp" || (window.location.pathname).toLowerCase() == "/changeEmail.asp" || (window.location.pathname).toLowerCase() == "/ordersummary.asp" || (window.location.pathname).toLowerCase() == "/orderdetails.asp") {
                    document.location.href = "index.asp";
                } else if ((window.location.pathname).toLowerCase() == "/login.asp") {
                    document.location.href = "shopping-cart.asp";
                } else {
                    $("#loginText").html("Log In");
                    $("#divCartDisplay").html(""); // Relevant if on cart page.
                    $("#checkoutMenu").hide(); // Relevant if on cart page.
                    $("#cartTotals").text("Your cart: 0 item(s) $0.00");
                    $(".topMenuProfile").hide();
                    //document.getElementById("loginDialogForm").reset();
                    $("#theEmail").val($("#theEmail")[0].defaultValue);
                    $("#Password").val($("#Password")[0].defaultValue);
                }
            }
            });
        }
		return false;
    });

    // Sign in validation script:
    $(".theLoginNewCustomerbutton").button().bind('click', function () { window.location.href = "login.asp"; });
    $(".theLoginbutton").button().bind('click', function () {
		
        var strValidator = $("#loginText").html();
		
        if (strValidator == "Log In") {
            // Iterate through the form, pulling all the fields and converting them to a JSON object
            theFormSerialized = $("#loginDialogForm").serialize();
            // **************************************************************************************
            $.ajax({ url: "header-login-validation.asp?v=Login&func1=checkHSPInProgress&randomAjax=" + Math.random() + "&" + theFormSerialized, dataType: 'jsonp', success: function (data) {
                theData = JSON.parse(data);
                if (theData.result == "True") {
                    if (theData.theNextPage != "") {
                        if (theData.HSPInProgress == "True") {
                            $("#login-Dialog").dialog("close");
                            $("#Div-Heat-Shrink-Printing-In-Progress").dialog("open");
                        } else { document.location.href = theData.theNextPage; }
                    }
                    else {
                        if ((window.location.pathname).toLowerCase() == "/login.asp") {
                            document.location.href = "shopping-cart.asp";
                        } else if ((window.location.pathname).toLowerCase() == "/heat-shrink-printing.asp") { // They're logging on, when on the HSP page.
                            $.ajax({ url: "Heat-Shrink-printing-service-ajax.asp?HSpageStep=getCounter&theAction=Write&randomAjax=" + Math.random(), success: function (data) {
                                if (data != -1) { document.location.href = "Heat-Shrink-printing-service.asp?HSpageStep=1"; }
                            }
                            });
                        } else if ((window.location.pathname).toLowerCase() == "/shopping-cart.asp") { // They're logging on, when on the HSP page. 
                            $.ajax({ type: "GET", url: "shopping-cart-ajax.asp?v=1&randomAjax=" + Math.random(), success: function (data) {
                                // shopping-cart-ajax.asp?v=2
                                $("#divCartDisplay").html(data);
                            }
                            });
                            $.ajax({ type: "GET", url: "shopping-cart-ajax.asp?v=2&randomAjax=" + Math.random(), success: function (data) {
                                $(".subTotalTitle").html(data);
                            }
                            });
                            $.ajax({ type: "GET", url: "shopping-cart-ajax.asp?v=3&randomAjax=" + Math.random(), success: function (data) { // Check to make sure the cart isn't empty.
                                if (data == "0") {
                                    $("#checkoutMenu").hide();
                                } else {
                                    $("#checkoutMenu").show();
                                }
                            }
                            });
                        }
                    }
                    $("#loginText").html("Log Out");
                    $(".topMenuProfile").show();
                    $("#login-Dialog").dialog("close");
                    $.ajax({ type: "GET", url: "header-ajax.asp?v=1&randomAjax=" + Math.random(), success: function (data) {
                        $("#cartTotals").html(data);
                    }
                    });
                }
                else {
                    $("#loginDialogError").text("Login Failed, Please try again.");
                    document.loginDialogForm.reset();
                    $('#theEmail').focus();
                }
            }, failure: function () { alert("Failure"); }
            });
        }
    });
	/*
    $(window).resize(function () {
        if ((window.location.pathname).toLowerCase() != "/heat-shrink-printing-service.asp" && (window.location.pathname).toLowerCase() != "/heat-shrink-printing.asp") {
            $("#login-Dialog").dialog('option', 'position', [$("#menuButtonLogin").offset().left, ($("#menuButtonLogin").offset().top + 29)]);
        }
    });*/
});

function loginDialogOpen(theLeft, theTop) {
    $("#login-Dialog").dialog("open");
    var myDialogX = theLeft;
    var myDialogY = theTop;
    $('#login-Dialog').dialog('option', 'position', [myDialogX, myDialogY]);
    $("#login-Dialog div").show();
    $('#theEmail').focus();
}


function loginDialogOpenCenter(nextPage) {
    $("#login-Dialog").dialog("open");
    $("#theNextPage").val(nextPage);
    $("#login-Dialog div").show();
    $('#theEmail').focus();
}

// Center an element on the screen, using centerIt
(function ($) { $.fn.CenterIt = function (B) { var C = { ignorechildren: true, showPopup: true }; var D = $.extend({}, C, B); var E = $(this); if (D.showPopup) { E.show() } $(document).ready(function () { CenterItem() }); $(window).resize(function () { CenterItem() }); function CenterItem() { var a = 0; var b = 0; if (D.ignorechildren) { a = E.height(); b = E.width() } else { var c = E.children(); for (var i = 0; i < c.length; i++) { if (c[i].style.display != 'none') { a = c[i].clientHeight; b = c[i].clientWidth } } } var d = E.css("margin"); var e = E.css("padding"); if (d != null) { d = d.replace(/auto/gi, '0'); d = d.replace(/px/gi, ''); d = d.replace(/pt/gi, '') } var f = ""; if (d != "" && d != null) { var g = e.split(' '); if (g.length == 1) { var h = parseInt(g[0]); f = new Array(h, h, h, h) } else if (g.length == 2) { var j = parseInt(g[0]); var k = parseInt(g[1]); f = new Array(j, k, j, k) } else if (g.length == 3) { var l = parseInt(g[0]); var m = parseInt(g[1]); var n = parseInt(g[2]); f = new Array(l, m, n, m) } else if (g.length == 4) { var l = parseInt(g[0]); var m = parseInt(g[1]); var o = parseInt(g[2]); var p = parseInt(g[3]); f = new Array(l, m, n, p) } } var k = 0; var j = 0; if (f != "NaN") { if (f.length > 0) { k = f[1] + f[3]; j = f[0] + f[2] } } if (e != null) { e = e.replace(/auto/gi, '0'); e = e.replace(/px/gi, ''); e = e.replace(/pt/gi, '') } var q = ""; if (e != "" && e != null) { var r = e.split(' '); if (r.length == 1) { var s = parseInt(r[0]); q = new Array(s, s, s, s) } else if (r.length == 2) { var t = parseInt(r[0]); var u = parseInt(r[1]); q = new Array(t, u, t, u) } else if (r.length == 3) { var v = parseInt(r[0]); var w = parseInt(r[1]); var x = parseInt(r[2]); q = new Array(v, w, x, w) } else if (r.length == 4) { var v = parseInt(r[0]); var w = parseInt(r[1]); var x = parseInt(r[2]); var y = parseInt(r[3]); q = new Array(v, w, x, y) } } var u = 0; var t = 0; if (q != "NaN") { if (q.length > 0) { u = q[1] + q[3]; t = q[0] + q[2] } } if (j == "NaN" || isNaN(j)) { j = 0 } if (t == "NaN" || isNaN(t)) { t = 0 } var z = $(window).height(); var A = $(window).width(); if ($.browser.msie && document.documentMode < 7) { E.css("position", "absolute") } else { E.css("position", "fixed") } if (!D.ignorechildren) { E.css("height", a + "px"); E.css("width", b + "px") } E.css("top", ((z - (a + j + t)) / 2) + "px"); E.css("left", ((A - (b + k + u)) / 2) + "px"); return this } return this } })(jQuery);



