﻿$(document).ready(function() {

    SetNavigationStyles();
    SetRegistrationCallout();
    setSpecialIE6Styles();
    SetWatermarkOnCallouts();

    $("img.btnContinue").click(function() {
        var parentContainer = $(this).parent("div.callout_txt_fields");
        var firstName = $(parentContainer).children("input#txtFname").val();
        var lastName = $(parentContainer).children("input#txtLname").val();
        var email = $(parentContainer).children("input#txtEmail").val();

        var calloutCookieName = "PediaSure Register User";
        var now = new Date();
        now.setUTCMinutes(now.getUTCMinutes() + 10);
        if (firstName != "First Name")
            document.cookie = "calloutCookieFirstName=" + firstName + "; expires=" + now.toUTCString() + "; path=/";
        if (lastName != "Last Name")
            document.cookie = "calloutCookieLastName=" + lastName + "; expires=" + now.toUTCString() + "; path=/";
        if (email != "Email Address")
            document.cookie = "calloutCookieEmail=" + email + "; expires=" + now.toUTCString() + "; path=/";
        window.location = "/coupons-registration";
    });
    SetExternalLinks();
});
function SetWatermarkOnCallouts() {
    var fname = "First Name";
    var lname = "Last Name";
    var email = "Email Address";

    $("input#txtFname").val(fname);
    $("input#txtLname").val(lname);
    $("input#txtEmail").val(email);

    $(".callout_txt_fields input").blur(function() {
        if ($("input#txtFname").val() == "")
            $("input#txtFname").val(fname);
        if ($("input#txtLname").val() == "")
            $("input#txtLname").val(lname);
        if ($("input#txtEmail").val() == "")
            $("input#txtEmail").val(email);

    });
    $(".callout_txt_fields input").click(function() {
        if ($(this).val() == fname || $(this).val() == lname || $(this).val() == email)
            $(this).val("");
    });
}
function setSpecialIE6Styles() {

    //IE6 special nav handling
    if ($.browser.msie && $.browser.version == "6.0") {
        $("div#navigation div#all_items").addClass("navIE6")
            .removeClass("bottom");
        $("div#secondary_nav ul#sub_nav li").css("background-image", "none");
        $("img").ifixpng();
        $("div").ifixpng();
    }

}
function SetNavigationStyles() {
    //remove last blue nav divider
    $("ul#ul_main_menu li:last").addClass("last_item");

    //remove last right border on misc menu
    $("ul#ul_misc_nav li:last a").addClass("last_item");

    //remove last right border on secondary menu
    $("ul#sub_nav li:last a").addClass("last_item");

    //don't display left nav if it is not populated - required for ie6&7
    if ($("div#ctrl_left_nav ul#left_callout_ul li").length == 0) {
        if ($("div#ctrl_left_nav ul#left_nav_ul li").length == 0) {
            $("div#ctrl_left_nav").css("display", "none");
            $("div#ctrl_content div#sub_pages").addClass("no_left_nav_content");
        }
    }

    //on hover of main/featured nav
    $("ul#ul_main_menu li a").hover(
            function() { $(this).addClass("active"); },
            function() { $(this).removeClass("active"); }
    )
    $("ul#sub_nav li a").hover(
            function() { $(this).addClass("active"); },
            function() { $(this).removeClass("active"); }
        )
    var has_class = $("ul#sub_nav li a").hasClass("active_node");
    if (has_class == true) {
        $("ul#ul_main_menu li a").css("background-image", "none");
    }

    $("ul#left_nav_ul li.apply_style").hover(
        function() { $(this).addClass("active"); },
        function() { $(this).removeClass("active"); }
    )
    $("ul#left_callout_ul li").hover
    (
        function() { $(this).addClass("active"); },
        function() { $(this).removeClass("active"); }
    )
    $("ul#left_nav_ul li.tv_comm").hover(
        function() { $(this).addClass("tv_comm_active"); },
        function() { $(this).removeClass("tv_comm_active"); }
    )
}
//WARN ON LEAVE
jQuery.extend(jQuery.expr[':'], {
    external: function(a, i, m) {

        if (!a.href || (a.href.indexOf('mailto') >= 0 && a.href.indexOf('@') >= 0)) {
            return false;
        }
        if (a.hostname && a.hostname !== window.location.hostname) {
            var fixedHostname = a.hostname.replace(/^\s+|\s+$/g, '').toLowerCase().replace('qa.', '').replace('stage.', '').replace('www.', '');

            if (jQuery.inArray(fixedHostname, safeList) >= 0) {
                return false;
            }
            if (fixedHostname.indexOf('javascript') > 0 || fixedHostname.indexOf('(') > 0) {
                return false;
            }
            else {
                return true;
            }
        }
        else {
            return false;
        }

    }
});
function warn_on_leave(site) {
    var msg = "\nLinks which take you out of Abbott Laboratories worldwide \n" +
             "web site are not under the control of Abbott Laboratories, \n" +
             "and Abbott Laboratories is not responsible for the contents \n" +
             "of any such site or any further links from such site. Abbott \n" +
             "Laboratories is providing these links to you only as a \n" +
             "convenience, and the inclusion of any link does not imply \n" +
             "endorsement of the linked site by Abbott Laboratories.\n\n" +
             "Do you wish to leave this site?";

    if (confirm(msg)) {
        window.open(site);
    }
    else {
        return;
    }
}
function SetExternalLinks() {
    $('a:external').addClass("external_link");
    $('a.external_link').removeAttr("target");
    $('a').click(function() {
        if ($(this).hasClass('external_link')) {
            if (!warn_on_leave($(this)[0].href)) {
                return false;
            }
        }
    });
}
// END OF WARN ON LEAVE

// REGISTRATION CALLOUT
function SetRegistrationCallout() {

    //get contents from callout control to inject to the placeholder
    var regCalloutHtml = $("div#txtBoxDiv").html();
    $("div#divTextBoxPlaceHolder").html(regCalloutHtml);

    MaskKeysOnCallout();
}
function MaskKeysOnCallout() {

    // Text Mask: Only allows letters, space, apostrophe, hyphen, period.
    // ASCII Values:
    //  65-90 = A-Z
    //  97-122 = a-z
    //  32 = space
    //  39 = apostrophe
    //  45 = hyphen
    //  46 = period
    //  8 = Backspace
    //  0 = Null
    //  9 = Tab
    //  127 = Delete
    $(".mask-text").each(function() {
        $(this).keypress(function(e) {
            if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || e.which == 32 || e.which == 39 || e.which == 45 ||
            e.which == 46 || (e.which > 64 && e.which < 91) || (e.which > 96 && e.which < 123)) {
                return true;
            }
            else {
                return false;
            }
        });
    });

    // Number-Mask: Allows only numbers to be entered into a text field.
    // ASCII Values:
    //  8 = Backspace
    //  0 = Null
    //  9 = Tab
    //  127 = Delete
    //  48-57 = 0-9
    $(".mask-email").each(function() {
        $(this).keypress(function(e) {
            if ((e.which != 47) && (e.which != 60) && (e.which != 61) && (e.which != 62) && (e.which != 92)) {
                return true;
            }
            else {
                return false;
            }
        });
    });

    // Zip-Mask: Allows only numbers and hyphens to be entered into a text field.
    // ASCII Values:
    //  8 = Backspace
    //  0 = Null
    //  9 = Tab
    //  127 = Delete
    //  48-57 = 0-9
    //  45 = "-"
    $(".mask-zip").each(function() {
        $(this).keypress(function(e) {
            if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || e.which == 45 || (e.which > 47 && e.which < 58)) {
                return true;
            }
            else {
                return false;
            }
        });
    });

    // Email-Mask: Does not allow "<" or ">" or "/" or "\" to be entered in the password field.
    // ASCII Values:
    //  8 = Backspace
    //  0 = Null
    //  9 = Tab
    //  127 = Delete
    //  48-57 = 0-9
    //  45 = "-"
    $(".mask-numbers").each(function() {
        $(this).keypress(function(e) {
            if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || (e.which > 47 && e.which < 58)) {
                return true;
            }
            else {
                return false;
            }
        });
    });


    $(".mask-alpha").each(function() {
        $(this).keypress(function(e) {
            if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || e.which == 32 || e.which == 39 || e.which == 44 || e.which == 45 ||
            e.which == 46 || (e.which > 64 && e.which < 91) || (e.which > 96 && e.which < 123) || (e.which > 47 && e.which < 58)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
    
    $(".mask-letters").each(function() {
        $(this).keypress(function(e) {
            if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || (e.which > 64 && e.which < 91) || (e.which > 96 && e.which < 123)) {
                return true;
            }
            else {
                return false;
            }
        });
    });

    $(".mask-no-tags").each(function() {
        $(this).keypress(function(e) {
            if ((e.which != 60) && (e.which != 62)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
}
