$(document).ready(function() {

    $(".popclose").click(function(event) {
        $(".overlay").css("display", "none");
        if ($("#limypantenetab1").length > 0) {
            $("#limypantenetab1").removeAttr("style");
        }
        $("#cii-locations").attr("src", "");
        $(".cii-locations").css("display", "none");
        $("#binTopIFrame").attr("src", "");
        $("#zip").val("Enter Zip Code");
        return false;
    });


    $(".padright").click(function(event) {
        $("#internalContdiv").css("display", "block");
        $("#disclaimtxt").css("display", "none");
    });

    //when clicking on "Find Stores" button in overlay
    $(".BuyItNowLink").click(function(event) {

        var overlay = $(".overlay");
        if ($("#limypantenetab1").length > 0) {
            $("#limypantenetab1").css("cssText", "z-index:1 !important");
        }
        overlay.css("margin-left", "-300px").css("display", "block");
        $("#disclaimtxt").css("display", "block");
        var upc = $(this).attr("id");
        $("#sku").val(upc);
        var clientname_ruleGroup = overlay.attr("id");
        var ciiConfig = clientname_ruleGroup.split("_");
        if (ciiConfig.length == 3) {
            var productLinks = "/pages/cii-product-links.aspx?upc=" + upc + "&client=" + ciiConfig[0] + "&ruleGroup=" + ciiConfig[1];
            $("#binTopIFrame").attr("src", productLinks);
            $(document).ready(function() {
                $('#binTopIFrame').load(function() {

                    var pagehr = window.location.href;

                    var lchref = pagehr.toLowerCase();

                    var posi = lchref.indexOf("en-ca");

                    if (posi > -1) {
                        var anchorArray = $('#binTopIFrame').contents().find('a');


                        if (anchorArray.length > 0) {
                            for (i = 1; i < anchorArray.length; i++) {
                                var aHref = anchorArray[i].href;
                                var genpos = aHref.indexOf("BuyProduct");
                                var confirmpos = aHref.indexOf("confirm");
                                if (genpos > -1 && confirmpos < 0) {
                                    var arr = aHref.split(':');

                                    var newhref = "javascript: if (confirm('You are now leaving a P&G site. We recommend you review their privacy policy, as their privacy practices may differ from P&G. Any personal information you provide on their Web site will be managed in accordance with their privacy policy.'))" + arr[1] + ";";
                                    anchorArray[i].href = newhref;
                                }
                            }
                        }
                    }
                });
            });
        }
        if (ciiConfig[2] == "en-CA") {
            $(".cii-middle").css("display", "none");
        }

    });


    $("input.zip").focus(function() {
        if ($(this).val() == "Enter Zip Code") {
            $(this).val("");
        }
    });

    $("input.zip").blur(function() {
        if ($(this).val() == "") {
            $(this).val("Enter Zip Code");
        }
    });

    $("a.zipsubmit").click(function(event) {
        $("p.ziperror").remove();
        var validZip = /^(\d{5})$/;
        if ($(this).prev().val().search(validZip) == -1) {
            $(this).after("<p class='ziperror'>Please enter a 5-digit zip code only.</p>");
            return false;
        }
        else {
            $("#cii-locations").css("display", "block");
            var sku = $("#sku").val();
            var client = $("#client").val();
            var country = $("#country").val();
            var ruleGroup = $("#ruleGroup").val();
            var zip = $("#zip").val();
            var storeLinks = "/pages/store-locator.aspx?sku=" + sku + "&client=" + client + "&country=" + country + "&zip=" + zip + "&ruleGroup=" + ruleGroup;
            $("#cii-locations").attr("src", storeLinks);
            return false;
        }
    });





});


function findStores(e) {
    var key = (e.keyCode) ? e.keyCode : e.which;
    //alert(key);
    if (key == 13) {
        
        $("p.ziperror").remove();
        
        var validZip = /^(\d{5})$/;
        if ($("#zip").val().search(validZip) == -1) {
            $("#zipsubmit").after("<p class='ziperror'>Please enter a 5-digit zip code only.</p>");
            
            return false;
        }
        else {
            $("#cii-locations").css("display", "block");
            var sku = $("#sku").val();
            var client = $("#client").val();
            
            var country = $("#country").val();
            var ruleGroup = $("#ruleGroup").val();
            var zip = $("#zip").val();
            var storeLinks = "/pages/store-locator.aspx?sku=" + sku + "&client=" + client + "&country=" + country + "&zip=" + zip + "&ruleGroup=" + ruleGroup;
            $("#cii-locations").attr("src", storeLinks);
            
            return false;
        }
      
        return false;
    }
    return true;
}    
    
