﻿chkSignIn = false;
var tm; //timer
var puStatus = 0;

function newsletterPU(em, loc) {
    var value = em;
    if ((value.indexOf('@') < 0) || ((value.charAt(value.length - 4) != '.') && (value.charAt(value.length - 3) != '.')))
    {
        alert("You have entered an invalid email address. Please try again.");
        return false;
    }
    else {
        puIframe('', loc + escape(em) + '&mp=s', 10);
    }
}
function puIframe(hdr, url, h, secsToClose) {
    if (secsToClose == undefined) secsToClose = 10;
    createPopup(hdr);
    innerPopup('<iframe src="' + location.protocol + '//' + location.hostname + url + '" height="' + h + '" width="500" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>', false, secsToClose);
}
function createPopup(hdr) {
    r = [];
    r.push('<tr class="puR1"><td class="c1">&nbsp;</td><td class="c2">&nbsp;</td><td class="c3">&nbsp;</td></tr>');
    r.push('<tr class="puR2"><td class="c1">&nbsp;</td><td class="main">');
    r.push('<h1><a class="puClose" title="Click here to close this window and continue shopping" onclick="showPopup(false);"></a>' + hdr + '</h1>');
    r.push('<div id="puOuter"><div id="innr"></div></div><a onclick="showPopup(false);" class="clBs">Close Window</a></td><td class="c3">&nbsp;</td></tr>');
    r.push('<tr class="puR3"><td class="c1">&nbsp;</td><td class="c2">&nbsp;</td><td class="c3">&nbsp;</td></tr>');
    $("#puCnt").html('<table class="puTbl">' + r.join('') + '</table>');
}
function innerPopup(txt, animateMove, secsToClose) {
    $("#innr").html(txt);
    $('#PopupContent').shadow().modalDialog({ backgroundOpacity: .20 }).draggable();
}
function placePopup(animateMove) {
    windowWidth = $(window).width();
    windowHeight = $(window).height();
    scrollTop = $(window).scrollTop();
    pDiv = $("#puDiv");
    pHeight = pDiv.height();
    popupTop = Math.round((windowHeight - pHeight) / 2 + scrollTop) - 20;
    popupLft = Math.round(windowWidth - pDiv.width()) / 2 + $(window).scrollLeft();

    if (popupTop - 20 < scrollTop) window.location = '/cart/basket.aspx'; //if its too large go to the cart

    if (animateMove) {
        if ((popupTop + pHeight + 350) > (windowHeight + scrollTop)) pDiv.animate({ "top": popupTop, "left": popupLft }, 300);
    } else {
        pDiv.css({ "top": popupTop, "left": popupLft });
        $("#bgPu").css({ "width": windowWidth - 20, "height": windowHeight - 10 });
    }
}
function showPopup(openWin, secsToClose) {
    if (openWin && puStatus == 0) {
        puStatus = 1;
        $("#bgPu").show();
        $("#puDiv").fadeIn(250);
        clearTimeout(tm);
        tm = setTimeout('showPopup(false)', (secsToClose * 1000));
        if (isBrw('msie',6)) $("select").hide();
    } else if (!openWin && puStatus == 1) {
        puStatus = 0;
        $("#bgPu").hide();
        $("#puDiv").slideUp(200);
        clearTimeout(tm);
        if (chkSignIn) setSignedIn();
        if (isBrw('msie',6)) $("select").show();
    }
}
function isBrw(brw, ver) {
    var isBrwr = false;
    jQuery.each(jQuery.browser, function(i, val) { if (i == brw && val) isBrwr = true; });
    if (isBrwr && ver != undefined)
        return (parseInt(jQuery.browser.version) == ver);
    else if (isBrwr)
        return true;
    return false;
}
