﻿// JScript File
var _mapPrefix;
var _ddl;

// sets quantity textbox to 1 or empty string on display group page
function setTxtQuantity(rbl,txt)
{
   var txtQuantity = document.getElementById(txt);
         
   // if rbl is null then it must be "None" choice
   if(rbl.checked && rbl.value=="0")
   {
        txtQuantity.disabled = true;
        txtQuantity.value = "";
        return true;
   }
   else
   {
        txtQuantity.disabled = false;
        txtQuantity.value = "1";
        txtQuantity.focus();
        return true;
   }
}

function evaluateTxtQuantity(txt,origValue)
{
   var txtQuantity = document.getElementById(txt);
   var value = txtQuantity.value;
   var len = txtQuantity.value.length;
   
   for(var i=0;i<len;i++)
   {
        value = value.replace(' ','');
   }

   if(value.length==0||isNaN(value))
   {
        txtQuantity.value = origValue;
   }
   
}

function setDropDown(parentID,value)
{
    var parent = document.getElementById(parentID);
    
    findDropDown(parent);
    
    if(!_ddl) return;
    
    _ddl.selectedIndex = -1;
    for(var i=0;i<_ddl.options.length;i++)
    {
        if(_ddl.options[i].text == value)
        {
            _ddl.options[i].selected = true;
        }
    }
}

function findDropDown(n)
{
    if(n.type!=undefined)
    {
        if(n.type=="select-one")
        {
            _ddl = n;
            return;
        }
    }
    
    for(var i=0;i<n.childNodes.length;i++)
    {
        if(n.childNodes[i].childNodes.length>0)
        {
            findDropDown(n.childNodes[i]);
        }  
    }
}

function getDropDownValue(ddl)
{
    for(var i=0;i<ddl.options.length;i++)
    {
        if(ddl.options[i].selected)
            return ddl.options[i].value;
    }
}

function confirmDropDownCookieWrite(value)
{
    if(value!='All')
    {
        writeCookie(value);
    }
}   

function showHideTab(a, tab1, tab2, tab3)
{
    var t1 = document.getElementById("rootTab1");
    var t2 = document.getElementById("rootTab2");
    var t3 = document.getElementById("rootTab3");

    t1.style.display = tab1;
    t2.style.display = tab2;
    t3.style.display = tab3;
    
    writeCookie(a);
    
    //setDropDown('headersub',a);
}

function writeCookie(value)
{
    document.cookie = _mapPrefix+"="+value+";path=/;"
}

/*
checks maximum length of characters for <textArea>
*/
function checkMaxLength(textbox,l)
{
    return (textbox.value.length < l);
}

function isPoBox(sender, args)
{
     var regex = /(^(p[\s|\.|,-]*| ^post[\s|\.]*)(o[\s|\.|,]*| office[\s|\.]*))| (^box[.|\s]*\d+)/;
     
     if(regex.test(args.Value)==true)
     {
        args.IsValid = false;
     }
     else
     {
        args.IsValid = true;
     }
     
     return;
}

/*
    Email preference unsubscribe checkbox action
*/

function setCheckboxes(chkBoxIds, chkBoxId)
{
    var ids = chkBoxIds.split(",");
    var chkBox = document.getElementById(chkBoxId);
    
    for(var i=0;i<ids.length;i++)
    {
        document.getElementById(ids[i]).checked = chkBox.checked;
    }
}

/* 
    Textbox and Button event pairings 
*/

function isCorrectPairing(e, buttonID)
{
    //the purpose of this function is to allow the enter key to 
    //point to the correct button to click.

    if(e.which || e.keyCode)
    {
        if ((e.which == 13) || (e.keyCode == 13))
        {
            //Get the button the user wants to have clicked

            var btn = document.getElementById(buttonID);
            if (btn != null)
            { //If we find the button click it
                
                btn.click();
            }
        }
    }
}
//	{
//		if ((e.which == 13) || (e.keyCode == 13)) 
//		{
//			document.getElementById(buttonID).click();
//			return false;
//		}
//		else
//		{
//			return true;
//		}
//	} 
//			
//    return true;
//}


function showHideDivWithLink(divId, linkId, linkShowText, linkHideText) {
	
	var div = window.document.getElementById(divId);
	var link = window.document.getElementById(linkId);
	var linkText = '';
	
	if(div.style.display == 'none') {
		div.style.display = 'block';
		linkText = linkHideText + ' (-)';			
	}
	else {
		div.style.display = 'none';
		linkText = linkShowText + ' (+)';	
	}
	
	link.innerHTML = linkText;
}

function setHiddenValue(hdnId, value)
{
    var hdn = document.getElementById(hdnId);
    
    hdn.value = value;
}

// AutoCompleteExtender

function acePopulated(sender, e)
{
    var behavior = sender;
    var target = behavior.get_completionList();
    var children = target.childNodes;
    var searchText = behavior.get_element().value;
    var searchTextU = searchText.toUpperCase();

    for (var i = 0; i < children.length; i++)
    {
        var liElement = children[i];
        var textNode = liElement.childNodes[0];
        var text = textNode.data;
        var textU = text.toUpperCase();
        var last = 0;
        var bElement;
        var j = 0;
        
        liElement.removeChild(textNode);
        
        while ((j = textU.indexOf(searchTextU, j)) != -1)
        {
            if (last != j)
            {
                liElement.appendChild(document.createTextNode(text.substring(last, j)))
            }
            
            bElement = document.createElement("b");
            bElement.appendChild(document.createTextNode(text.substring(j, j + searchText.length)));
            liElement.appendChild(bElement);
            
            j += searchTextU.length;
            last = j;
        }
        
        j = textU.length;
        if (last != j)
        {
            liElement.appendChild(document.createTextNode(text.substring(last, j)))
        }
    }
    
    $addHandler(target, "mouseover", function(e) { aceListMouseOver(behavior, e); });
    
    // Workaround for Chrome so list drops down at correct location.
    // The list is added under the body element. It should be a sibling
    // to the input element like other browsers.
    if (target.parentNode.tagName === "BODY")
    {
        target.parentNode.removeChild(target);
        behavior.get_element().parentNode.appendChild(target);
    }
}

function aceListMouseOver(behavior, e)
{
    var item = aceFindItem(e.target);
    var childNodes = behavior.get_completionList().childNodes;
    for (var i = 0; i < childNodes.length; i++)
    {
        if (childNodes[i] === item)
        {
            if (behavior._selectIndex !== i)
            {
                behavior._highlightItem(item);
                behavior._selectIndex = i;
            }
            break;
        }
    }
}

function aceSelected(sender, e)
{
    var behavior = sender;
    var value = null;
    var item = aceFindItem(e.get_item());
    if (item !== null)
    {
        value = aceItemValue(item);
    }
    behavior.get_element().value = value !== null ? value : "";
}

function aceFindItem(item)
{
    while (item !== null)
    {
        if (item.nodeType === 1 && item.tagName === "LI")
        {
            if (item.getAttribute("_value") !== null || item._value !== undefined)
            {
                return item;
            }
        }
        item = item.parentNode;
    }
    return null;
}

function aceItemValue(item)
{
    if (item.getAttribute("_value") !== null)
    {
        return item.getAttribute("_value");
    }
    else if (item._value !== undefined)
    {
        return item._value;
    }
}

/* Qualifying Promo Details For PromoCode */

function getQualifyingPromoDetailsForPromoCode(promoCode, displayID, shopperPriceLevel, pnlPromotionDetailsID, sku) {
    $.ajax({            
            type: 'POST',
            url: 'WebService/Promo.asmx/getQualifyingPromoDetailsForPromoCode',                
            data: "{'promoCode':'" + promoCode + "', 'displayID':'" + displayID + "', 'shopperPriceLevel':" + shopperPriceLevel + ", 'sku':'" + sku + "'}",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(response) {
                var promoDetails;
                                
                if(response.d != undefined) {
                    promoDetails = response.d;
                }
                else {
                    promoDetails = response;
                }
                
                if(promoDetails.length > 0) {                    
                    showPromoDetailsPopup(promoDetails, pnlPromotionDetailsID);
                }
            },
            error: function(request, status, error) {
                //alert(request.statusText);
            }
        });
}

function showPromoDetailsPopup(msg, pnlPromotionDetailsID) {
    $('#' + pnlPromotionDetailsID).show().find('.promodetails-message-div').append(msg);
    $('.promodetails-close-div').click(function() {
        $('#' + pnlPromotionDetailsID).find('.promodetails-message-div').html('');
        $('#' + pnlPromotionDetailsID).hide();
    });
}

/* End Qualifying Promo Details For PromoCode */
