﻿var count = 0;

$(document).ready(function() {
    SetupWatermark($('#txtHeaderSearch'), "Enter Search");
    SignUpSetup();
    SetupMozillaClicks();
});

//Allows all elements to have click events
function SetupMozillaClicks() {
    if (typeof HTMLElement != 'undefined' && !HTMLElement.prototype.click)
        HTMLElement.prototype.click = function() {
            var evt = this.ownerDocument.createEvent('MouseEvents');
            evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
            this.dispatchEvent(evt);
        }
}

function SignUpSetup() {
    var txt = $('#txtNewsletterSignup');
    SetupWatermark(txt, 'enter email address');
}

function SignUp() {
    var email = $('#txtNewsletterSignup').val();

    var isEmail = true;
    re = new RegExp("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    isEmail = re.test(email);
    if (email.length > 0 && isEmail) {

        var newsletterUrl = $('input[id$="iNewsletterJoin"]').attr('newsletterUrl');
        $.post(newsletterUrl, { action: "signup", email: email },
   function(data) {
       if (data == "success") {

           window.location.href = newsletterUrl;
       }
       else {
           alert('An error occurred during sign up. Please try again at another time.');
       }
   });
    }
    else {
        alert('Please enter a valid e-mail address.');
    }

    return false;
}

function Search() {

    var keyword = document.getElementById('txtHeaderSearch').value;
    if (keyword == 'Enter Search') {
        document.getElementById('txtHeaderSearch').focus();
        return;
    }
    var pageUrl = document.getElementById('txtHeaderSearch').getAttribute('pageUrl');
    window.location = pageUrl + "?keyword=" + encodeURIComponent(keyword);
}

function SizedPopupOnLink(linkElement) {
    var href = linkElement.getAttribute('href');
    var width = linkElement.getAttribute('popWidth');
    var height = linkElement.getAttribute('popHeight');
    var optionsTemplate = 'width=WIDTHVAL, height=HEIGHTVAL';
    window.open(href, 'ShippingChart', optionsTemplate.replace('WIDTHVAL', width).replace('HEIGHTVAL', height));
    return false;
}

function SetupWatermark(jqueryElementObject, waterMarkText) {
    // Define what happens when the textbox comes under focus
    // Remove the watermark class and clear the box
    jqueryElementObject.focus(function() {

        $(this).filter(function() {

            // We only want this to apply if there's not 
            // something actually entered
            return $(this).val() == "" || $(this).val() == waterMarkText

        }).removeClass("watermarkOn").val("");

    });

    // Define what happens when the textbox loses focus
    // Add the watermark class and default text
    jqueryElementObject.blur(function() {

        $(this).filter(function() {

            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == ""

        }).addClass("watermarkOn").val(waterMarkText);

    });
}

/*--- BV Script - This is for the product display variant related callbacks ---*/

function Anthem_PreCallBack() {
    count++;
    setTimeout("CallBackStarted()", 500);
}

function Anthem_PostCallBack() { CallBackFinished(); }

function CallBackStarted() {
    if (count > 0) {
        var item = document.getElementById('wait');
        if ((!window.XMLHttpRequest) && (document.documentElement)) {
            item.style.position = 'absolute';
            //code to make ie6 behave
            if (document.documentElement.scrollTop) {
                item.style.top = document.documentElement.scrollTop + 15;
            }
            else { item.style.top = 15; }
            item.style.right = 15;
        }
        item.style.display = 'block';
    }
}

function CallBackFinished() {
    count--;
    if (count == 0) {
        var item = document.getElementById('wait');
        if (item != null) {
            item.style.display = 'none';
        }
    }
}




//Script to Disable Right Clicking START----------------------------
var message = "";
///////////////////////////////////
function clickIE() {
    if (document.all) {
        (message);
        return false;
    }
}

function clickNS(e) {
    if (document.layers || (document.getElementById && !document.all)) {
        if (e.which == 2 || e.which == 3) {
            (message);
            return false;
        }
    }
}

//Runs upon loading..with some specific page exceptions
if (document.location.toString().toLowerCase().indexOf('affiliateprogram.aspx') == -1) {
    if (document.layers) {
        document.captureEvents(Event.MOUSEDOWN); document.onmousedown = clickNS;
    }
    else {
        document.onmouseup = clickNS; document.oncontextmenu = clickIE;
    }
    document.oncontextmenu = new Function("return false")
}
//Script to Disable Right Clicking END----------------------------

