jQuery.fn.validalphanumeric = function() {
    var reg = new RegExp(/^([a-z0-9\-\' \.]*)$/i)
    if (reg.test(this.val())) { return true; }
    else {return false;}
}

jQuery.fn.validemail = function() {
    var reg = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    if (reg.test(this.val())) { return true; }
    else { return false; }
} 


function ShowModal(popupId, width, height) {
    $("#popupBackground").css({
        "opacity": "0.7"
    });
    $("#popupBackground").show();

    var widthString = width.toString() + "px";
    var marginLeftString = "-" + (width / 2).toString() + "px";

    var heightString = height.toString() + "px";
    var topString = (posTop() + (pageHeight() - height) / 2).toString() + "px";

    var popup = $('#' + popupId);

    popup.css({
        "width": widthString,
        "margin-left" : marginLeftString,
        "height" : heightString,
        "top" : topString
    });
    
    popup.show();
   
}

function HideModal(popupId) {
    if (popupId !== "") {
        $('#popupBackground').hide();
        $('#' + popupId).hide();
        ClearModal(popupId);
    }
}

function ClearModal(popupId) {
    if (popupId !== "") {
        $('#' + popupId).find(":input[type=text]").attr("value", "");
        $('#' + popupId).find(":input[type=textarea]").attr("value", "Type your message here");
        $('#' + popupId).find(":input[type=textarea]").css({
            "color": "gray"
        });
        ClearErrors(popupId);
    }
}

function ClearErrors(popupId) {
    if (popupId !== "") {
        $('#' + popupId).find(".jqError").css({ "visibility": "hidden" });
    }
}

function DefaultMsgClear(popupId) {
    if (popupId !== "") {
        if ($('#' + popupId).find(":input[type=textarea]").attr("value") == 'Type your message here') {
            $('#' + popupId).find(":input[type=textarea]").attr("value", "");
        }
        $('#' + popupId).find(":input[type=textarea]").css({
            "color": "black"
        });
    }
}

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() { return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null; } function pageHeight() { return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null; } function posLeft() { return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0; } function posTop() { return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0; } function posRight() { return posLeft() + pageWidth(); } function posBottom() { return posTop() + pageHeight(); }

//Specific to Send To a Friend START
function ValidateSendToaFriendModal() {
    ClearErrors('sendToAFriend');
    DefaultMsgClear('sendToAFriend')

    var fName = $('#friendsName');
    var fEmail = $('#friendsEmail');
    var oName = $('#yourName');
    var oEmail = $('#yourEmail');
    var oMsg = $('#yourMsg');
    var oPFID = $('#hfpfID');
    var oBase = $('#hfstafBase');
    var oImage = $('#hfstafImage');
    var oProdName = $('#hfstafName');
    var oOffers = $('#hfstafOffers');
    var oSale = $('#hfstafSale');
    var oPrice = $('#hfstafPrice');
    var oDesc = $('#hfstafDesc');
    
    var aOK = true;
    var errCount = 0;
    
    //Check Friend Name
    if ($.trim(fName.val()).length == 0) {
        aOK = false;
        errCount = errCount + 1;
        $('#reqFldfriendsName').css({
            "visibility": "visible"
        });
    }
    else {
        if (!fName.validalphanumeric()) {
            aOK = false;
            errCount = errCount + 1;
            $('#reqFldfriendsName').css({
                "visibility": "visible"
            });
        }
    }

    //Check Friend Email
    if ($.trim(fEmail.val()).length == 0) {
        aOK = false;
        errCount = errCount + 1;
        $('#reqFldfriendsEmail').css({
            "visibility": "visible"
        });
    }
    else {
        if (!fEmail.validemail()) {
            aOK = false;
            errCount = errCount + 1;
            $('#reqFldfriendsEmail').css({
                "visibility": "visible"
            });
        }
    }

    //Check Name
    if ($.trim(oName.val()).length == 0) {
        aOK = false;
        errCount = errCount + 1;
        $('#reqFldyourName').css({
            "visibility": "visible"
        });
    }
    else {
        if (!oName.validalphanumeric()) {
            aOK = false;
            errCount = errCount + 1;
            $('#reqFldyourName').css({
                "visibility": "visible"
            });
        }
    }

    //Check Email
    if ($.trim(oEmail.val()).length == 0) {
        aOK = false;
        errCount = errCount + 1;
        $('#reqFldyourEmail').css({
            "visibility": "visible"
        });
    }
    else {
        if (!oEmail.validemail()) {
            aOK = false;
            errCount = errCount + 1;
            $('#reqFldyourEmail').css({
                "visibility": "visible"
            });
        }
    }

    if (errCount == 0) {
        $.post("/shop/xt_orderform_send_friend_email.asp", { fName: fName.val(), fEmail: fEmail.val(), oName: oName.val(), oEmail: oEmail.val(), oMsg: oMsg.val(), oPFID: oPFID.val(), oBase: oBase.val(), oImage: oImage.val(), oProdName: oProdName.val(), oOffers: oOffers.val(), oSale: oSale.val(), oPrice: oPrice.val(), oDesc: oDesc.val() },
            function(data) {
                HideModal("sendToAFriend");
                $('#sendToAFriendThankyouMsg').css({ "display": "none" })
                $('#sendToAFriendErrorMsg').css({ "display": "none" })
                if (data.save_ok == 'valid') {
                    $('#sendToAFriendThankyouMsg').css({ "display": "block" })
                } else {
                    $('#sendToAFriendErrorMsg').css({ "display": "block" })
                }
                ShowModal('sendToAFriendThankyou', 430, 70);
            }, "json");
    }
}
//Specific to Send To a Friend END

//Specific to More Promos START
function ShowMorePromos(promodiv) {
    var promodiv = $('#' + promodiv);

    promodiv.css({ "display": "block" });
    $('#hidemorePromos').css({ "display": "block" })
    $('#showmorePromos').css({ "display": "none" })

}

function HideMorePromos(promodiv) {
    var promodiv = $('#' + promodiv);

    promodiv.css({ "display": "none" });
    $('#hidemorePromos').css({ "display": "none" })
    $('#showmorePromos').css({ "display": "block" })

}
//Specific to More Promos END

//Specific to Notify Me START
function ValidateNotifyMeModal() {
    ClearErrors('notifyMe');

    var emailAddress = $('#emailAddress');
    var pfID = $('#hfNMpfID');
    var productName = $('#hfNMname');

    var aOK = true;
    var errCount = 0;

    //Check Email
    if ($.trim(emailAddress.val()).length == 0) {
        aOK = false;
        errCount = errCount + 1;
        $('#reqFldemailAddress').css({
            "visibility": "visible"
        });
    }
    else {
        if (!emailAddress.validemail()) {
            aOK = false;
            errCount = errCount + 1;
            $('#reqFldemailAddress').css({
                "visibility": "visible"
            });
        }
    }

    if (errCount == 0) {
        $.post("/shop/xt_notify_me.asp", { emailAddress: emailAddress.val(), pfID: pfID.val(), productName: productName.val() },
            function(data) {
                HideModal("notifyMe");
                $('#notifyMeThankyouMsg').css({ "display": "none" })
                $('#notifyMeErrorMsg').css({ "display": "none" })
                if (data.save_ok == 'valid') {
                    $('#notifyMeThankyouMsg').css({ "display": "block" })
                } else {
                    $('#notifyMeErrorMsg').css({ "display": "block" })
                }
                ShowModal('notifyMeThankYou', 410, 80);
            }, "json");
    }
}
//Specific to Notify Me END

//MINI BASKET
function ShowMiniBasket() {
    if ($("#minibasketghost").css("display") == 'none') {
        $("#minibasketghost").slideDown("slow");
    }
    else {
        $("#minibasketghost").slideUp("slow");
    }
}
function HideMiniBasket() {
    $("#minibasketghost").slideUp("slow");
}

function ShowMiniBasket_AfterAdd() {
    $("#minibasketghost").slideDown("slow");
    setTimeout("$('#minibasketghost').slideUp('slow')", 6000);
}
