﻿function makeLinkAbsolute(hyperLink, baseUrl, controlId)
{
    if(hyperLink.href.match(/^http(s)?\:\/\//))
        hyperLink.href = hyperLink.href.replace(/^http(s)?\:\/\/.*?\//, baseUrl + "/");
    else
        hyperLink.href = baseUrl + hyperLink.href;
}

function fixWelcomeLinks(welcomeLabel, baseUrl, controlId)
{
    // Get the first link under the label
    // and fix it
    var links = welcomeLabel.getElementsByTagName("a");
    if(links && links[0])
    {
        makeLinkAbsolute(links[0], baseUrl);
    }
}

function replaceNotUsernameLink(hyperLink, baseUrl, controlId)
{
    hyperLink.href = baseUrl + "/Microsite/Command.aspx?commandName=logout";
}

function fixPromoSlider(promoSliderDiv, baseUrl, controlId)
{
    // Get to the first hyperlink under the div tag
    var links = promoSliderDiv.getElementsByTagName("a");
    if(links && links[0])
    {
        // Remove the onclick attribute and set href to a fixed location
        links[0].onclick = function() { };
        links[0].href = baseUrl + "/Account/Acct_MyPromotions.aspx";
    }
}

function replaceSearchButton(searchButton, baseUrl, controlId)
{
    // Get the ID of the search text box
    var regEx = /(.*?)ibtnSearchSubmit/;
    var results = regEx.exec(searchButton.id);
    var prefix = results[1];
    var txtSearchID = prefix + "txtSearch";
    
    searchButton.onclick = function() {return navigateToSearchPage(txtSearchID, baseUrl);};
}

function replaceQuickOrderButton(qoButton, baseUrl, controlId)
{
    // Get the ID of the quick order text box
    var regEx = /(.*?)ibtnQuickOrderSubmit/;
    var results = regEx.exec(qoButton.id);
    var prefix = results[1];
    var txtQuickOrderID = prefix + "txtQuickOrder";
    
    qoButton.onclick = function() {return navigateToQOPage(txtQuickOrderID, baseUrl);};
}

function navigateToSearchPage(txtSearchID, baseUrl, controlId)
{
    // Get the contents of the search box
    // and prepare the url for search page
    var txtSearch = document.getElementById(txtSearchID);
    var searchUrl = baseUrl + "/Search/SearchResults.aspx?SearchHeader=" + escape(txtSearch.value);
    
    // Navigate to the search page
    window.location.href = searchUrl;
    return false;
}

function navigateToQOPage(txtQuickOrderID, baseUrl, controlId)
{
    // Get the contents of the QO box
    // and prepare the url for QO page
    var txtQuickOrder = document.getElementById(txtQuickOrderID);
    var qoUrl = baseUrl + "/Catalog/CatalogQuickOrder.aspx?Quick=" + escape(txtQuickOrder.value);
    
    // Navigate to the search page
    window.location.href = qoUrl;
    return false;
}

function fixFashionGlossary(ulShopping, baseUrl, controlId)
{
    var liArray = ulShopping.getElementsByTagName("li");
    var liFashionGlossary = liArray[1];
    var aFashionGlossary = liFashionGlossary.childNodes[0];
    aFashionGlossary.onclick = function() { 
        window.open(baseUrl + "/Product/ProductGlossaryDefault.aspx", "",
        "width=630,height=550,menubar=no,toolbar=no,scrollbars=yes,resizable=no,location=no,status=yes"); };
}

function replaceEmailSignupButton(emailButton, baseUrl, controlId)
{
    var emailButtonName = emailButton.id.replace(/_/g, "$");
    emailButton.href = "javascript:Microsite_SendMail('" + emailButton.id + "','" + emailButtonName + "','"
        + baseUrl  + "','" + controlId + "');";
}

var emailError;

function Microsite_SendMail(emailButtonID, emailButtonUniqueID, baseUrl, footerId)
{
    // Get the prefix for all the controls in the SignUpEmail div
    var regEx = /(.*?)lbtnEmailSignUp/;
    var results = regEx.exec(emailButtonID);
    var prefix = results[1];
    
    // Get the controls required
    var txtEmail = document.getElementById(prefix + "txtEmailAddress");
    var divSignUpEmail = document.getElementById("SignUpEmail");
    if(!emailError)
    {
        emailError = document.getElementById(prefix + "reqValLogin");
        if(!emailError)
        {
            emailError = document.createElement("span");
            divSignUpEmail.parentNode.appendChild(emailError);
            emailError.style.color = "red";
            emailError.className = "validator";
            emailError.style.fontSize = "12px";
        }
    }
    
    var url;
    emailError.innerHTML = "";
    
    // Validate email and take action if valid
    // else show error message
    if (txtEmail.value.length > 0)
    {
        //alert (txtEmail.value)
        if (echeck(txtEmail.value)==true)
        {
            //var url= baseUrl + "/Checkout/ChameleonEmail.aspx?eml=" + txtEmail.value; 
            //ExecuteCall (url);
            var footerControl = document.getElementById(footerId);
            theForm = footerControl.getElementsByTagName("form").item(0);
            theForm.action = baseUrl + "/";
            if(theForm.__VIEWSTATE)
            {
                theForm.__VIEWSTATE.parentNode.removeChild(theForm.__VIEWSTATE);
            }
            __doPostBack(emailButtonUniqueID,'');
        }
        else
        {
            emailError.innerHTML = "Your email address is invalid.";
        }
    }
    else
    {
        emailError.innerHTML = "Please enter your email address.";
    }
}
function performClientSideContentReplacements(clientId, replacementEntries, baseUrl)
{
    // Get to the container div
    var containerDiv = document.getElementById(clientId);

    // Iterate through the replacement entries
    for(var i=0; i<replacementEntries.length; i++)
    {
        // Get all the elements targeted by the replacement entry
        var entry = replacementEntries[i];
        var targetElements = containerDiv.getElementsByTagName(entry.tagName);

        // Go through all the target elements
        for(var j=0; j<targetElements.length; j++)
        {
            // Filter the target elements using the match criteria
            if(entry.matchCriteria)
            {
                // Get the property value from the element
                var attrValue = eval("targetElements[j]." + entry.matchCriteria.attribute);
                
                // Check if we got a regex from the server
                var regEx = new RegExp(entry.matchCriteria.value);
                var foundMatch = false;
                if(regEx)
                {
                    foundMatch = attrValue.match(regEx);
                }
                else
                {
                    foundMatch = (attrValue == entry.matchCriteria.value);
                }

                // Execute the replacement function
                if(foundMatch)
                {
                    var functionCallString = entry.replacementFunction + "(targetElements[j],baseUrl,clientId)";
                    eval(functionCallString);
                }
            }
        }
    }
}

function repopulateHeaderUIFromCookies(vendorName, headerClientId)
{
    this.BasketCountInterface(vendorName + ".Basket.BasketCount", headerClientId + "_ShoppingBasket1_lblBasketItemCount" );
	this.WelcomeMessageInterface(vendorName + ".User",vendorName + ".FirstName",headerClientId + "_UserIdentification1_lblFirstNameWelcome",
		headerClientId + "_UserIdentification1_lblWelcomeText",headerClientId + "_UserIdentification1_linkWelcomeToMyAcct",
		headerClientId + "_UserIdentification1_spanWishListLink");
	this.ProductMessageInterface(vendorName + ".LastViewedProducts",headerClientId + "_UserIdentification1_lkPreViewedProducts",
		headerClientId + "_UserIdentification1_spanDivider");

	if(typeof(CardInfoLink) != "undefined")
	{
		this.CardInfoLink(vendorName + ".User",vendorName + ".CreditInfoString",vendorName + ".HasAdsCard",vendorName + ".PreApprovedOfferDeclined",headerClientId + "_haApplyCreditCard");
	}

	if(typeof(YourCreditCardLink) != "undefined")
	{
		this.YourCreditCardLink(vendorName + ".User",vendorName + ".CreditInfoString",vendorName + ".HasAdsCard",vendorName + ".PreApprovedOfferDeclined",headerClientId + "_haApplyCreditCardOTB");
	}

	this.CardInfoMessageEx(vendorName + ".User",vendorName + ".CreditInfoString",vendorName + ".PreApprovedOfferDeclined",headerClientId + "_hlCreditCardInfo",
		headerClientId + "_imgCreditCardInfo");
}

function repopulateFooterUIFromCookies(vendorName, footerClientId)
{
    this.CardInfoMessageNew(vendorName + ".User",vendorName + ".CreditInfoString",vendorName + ".PreApprovedCardType",
        footerClientId + "_hlCreditCardInfo",footerClientId + "_imgCreditCardInfo"); 
}
