﻿
var ShopperList_CurrentProductId
var ShopperList_CurrentCatalogName
var ShopperList_CurrentVariantId
var ShopperList_CurrentQuantity
var ShopperList_CurrentSoldByType
var ShopperList_ListName
var ShopperList_CurrentItems
var controlId
var isMultipleSelection
var sourceListId
var sListTop
var sListLeft
var ShopperList_CurrentWeightKg
var ShopperList_CurrentWeightG
var ShopperList_CurrentComments
var ShopperList_CurrentImageUrl
var ShopperList_CurrentProductUrl

function LaunchDeleteCartAddSavetoWishListPopup(isAuthenticated)
{
    var div = document.getElementById("savetowishlistpopupId");
    if (div == null) {
        div = document.createElement('div');
        div.setAttribute("id", "savetowishlistpopupId");
        ////div.setAttribute("style","display: none; background-color: white;");
        document.body.appendChild(div);
    }
   
    SetDivContextDeleteCart(isAuthenticated);
    div = document.getElementById("savetowishlistpopupId");
    div.style.display = "block"; 
    var pageSize = getPageSize(document.getElementById("savetowishlistpopupId"));
    var scroll = getScrollXY();

    var left = pageSize.width / 2 + scroll.x - 200; // 200 is a guestimate
    var top = pageSize.height / 2 + scroll.y - 50;

    div.className = "pop-up-sm cart-alert";
    
    div.style.top = top;
    div.style.left = left;
    sListTop = top;
    sListLeft = left;

    ////  Need this for FireFox
    div.setAttribute("style", "top:" + top + "px; left:" + left + "px;");

}

function LaunchAddToShopperListPopup(divId, productId, catalogName, weightKg, weightG, comments, qty, soldByType, imageUrl, productUrl) {
    var pageSize = getPageSize(document.getElementById(divId));
    var div = document.getElementById("AddToShopperListPopup");
    SetupDarkBackground();
    if (div == null) {
        div = document.createElement('div');
        div.setAttribute("id", "AddToShopperListPopup");
        ////div.setAttribute("style","display: none; background-color: white;");
        document.body.appendChild(div);
        GetDivContext();
        div = document.getElementById("AddToShopperListPopup");
    }
    var scroll = getScrollXY();

    var left = pageSize.width / 2 + scroll.x - 200; // 200 is a guestimate
    var top = pageSize.height / 2 + scroll.y - 200;

    div.style.top = top;
    div.style.left = left;
    sListTop = top;
    sListLeft = left;
    div.style.zIndex = 9998;
    div.style.display = 'inline';
    div.style.position = 'absolute';
    div.style.backgroundColor = "white";

    ////  Need this for FireFox
    div.setAttribute("style", "top:" + top + "px; left:" + left + "px; display:inline; position:absolute; background-color:white; z-index:9998;");

    ShopperList_CurrentProductId = productId;
    ShopperList_CurrentCatalogName = catalogName;
    ShopperList_CurrentQuantity = qty;
    ShopperList_CurrentSoldByType = soldByType;
    ShopperList_CurrentWeightKg = weightKg;
    ShopperList_CurrentWeightG = weightG;
    ShopperList_CurrentComments = comments;
    ShopperList_CurrentImageUrl = imageUrl;
    ShopperList_CurrentProductUrl = productUrl;
}

function hideEmptyCartPopup() {

    var div = document.getElementById("EmptyCartPopupDiv");
    div.style.display = "none"; //to hide it
}

function LaunchEmptyCartConfirmationPopup(clickedClubId, currClubId, returnUrl) {
    ////currClubId is the userSelectedStore from the sitecontext
    if (clickedClubId == currClubId) {
        if (returnUrl != "0") {
            window.location.href = returnUrl;
        }
        else {

        	window.location.href = "/pages/defaultIndividual.aspx?cat=SamsClub_MX&category=Individual&ig=Individual-Home";
        }
        return false;
    }

    //set the hidden filed
    var hiddenField = document.getElementById("HiddenClubId");
    hiddenField.value = clickedClubId;

    var div = document.getElementById("EmptyCartPopupDiv");
    div.style.display = "block";
    div.style.visibility = "visible";

}
function getPageSize() {
	var myWidth = 0, myHeight = 0;
	if (typeof (window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return { width: myWidth, height: myHeight };
}
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if (typeof (window.pageYOffset) == 'number') {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return { x: scrOfX, y: scrOfY };
}

function LaunchMultipleActionToShopperListPopup(listId, items) {
        if (items != null && items != '') {
        var div = document.getElementById("AddToShopperListPopup");
        var divName = 'wishlistDetailNameDiv';
        if (div == null) {
            div = document.createElement('div');
            div.setAttribute("id", "AddToShopperListPopup");
            ////div.setAttribute("style","display: none; background-color: white;");
            document.body.appendChild(div);

            GetListtoListDivContext(listId);
            div = document.getElementById("AddToShopperListPopup");
        }


        var divPos;

        if (divPos == null) {
            divPos = GetElementByIdMatch('AddToWishList');
        }
        var pos = FindPosition(divPos);

        var left = pos[0] + 50;
        var top = pos[1] + 50;

        ///alert('Left:' + left + ' Top:' + top);

        div.style.left = left;
        div.style.top = top;
        div.style.display = 'inline';
        div.style.position = 'absolute';
        div.style.backgroundColor = "white";

        ////  Need this for FireFox
        div.setAttribute("style", "position: absolute; top:" + top + "px; left:" + left + "px; background-color: white;");
        ShopperList_CurrentProducts = items;
        isMultipleSelection = true;
        sourceListId = listId;
    }
}


function HideAddToShopperListPopup()
{
    var div = document.getElementById("AddToShopperListPopup");
    div.style.display='none';
}

function RemoveAddToShopperListPopup()
{
    var div = document.getElementById("AddToShopperListPopup");
    div.parentNode.removeChild(div);
    return true;
}
//
function RemovePopupAlert() {
    var div = $get("confirmationPopup");
    document.removeChild(div);
    return true;
}
function SetupDarkBackground() {
    var backDivHeight = document.body.offsetHeight.toString();
    var backDiv = document.getElementById("backDiv");
    if (backDiv == null) {
        backDiv = document.createElement('div');
        backDiv.setAttribute("id", "backDiv");
        backDiv.setAttribute("class", "wl-popback");
        backDiv.setAttribute("className", "wl-popback");
        backDiv.style.height = backDivHeight + "px";
        backDiv.setAttribute("style", "height:" + backDivHeight + "px");
        document.body.appendChild(backDiv);
    }
}
function RemoveDarkBackground() {
    var div = $get("backDiv");

    if (div != null) {
        div.parentNode.removeChild(div);
    }
    
    return true;
}
function AddItemToListClick()
{
    var isNew = false;
    var ListOption = document.getElementById("ListOption1");
    if ( ListOption != null && ListOption.checked )
    {
        var ExistingListSelector = document.getElementById("ExistingListSelector");
        ShopperList_ListName = ExistingListSelector.value;
        
    }
    else
    {
        var NewListName = document.getElementById("NewListName");                
        ShopperList_ListName = NewListName.value;  
	    isNew = true;              
    } 
    var isDefault = false;
    var DefaultOption = document.getElementById("DefaultCheck"); 
    if ( DefaultOption != null && DefaultOption.checked )
    {
       isDefault = true;
   }

    if (isMultipleSelection == true) {
        if (SubmitPostMultipleToShopperListRequestToServer(isNew, isDefault, 'AddToList')) {
            if (isNew == true) {
                HideAddToShopperListPopup();
            }
            else {
                RemoveAddToShopperListPopup();
            }
        } 
    }
    else {
        if (SubmitAddToShopperListRequestToServer(isNew, isDefault)) {
            if (isNew == true) {
                HideAddToShopperListPopup();
            }
            else {
                RemoveAddToShopperListPopup();
            }
        }
    }
    return false;
}  

function SubmitAddToShopperListRequestToServer(isNew,isDefault)
{
    var xmlhttp= GetXmlHttp();

    var request = "/ShopperList.axd?ln=" + ShopperList_ListName + "&pid=" + ShopperList_CurrentProductId + "&cid=" + ShopperList_CurrentCatalogName + "&vid=" + "&weightKg=" + ShopperList_CurrentWeightKg + "&weightG=" + ShopperList_CurrentWeightG + "&comments=" + ShopperList_CurrentComments + "&qty=" + ShopperList_CurrentQuantity + "&soldByType=" + ShopperList_CurrentSoldByType + "&default=" + isDefault + "&imageUrl=" + ShopperList_CurrentImageUrl + "&productUrl=" + ShopperList_CurrentProductUrl; 
    
    if (isNew)
    {
	    request += "&new=true";
    }
    xmlhttp.open("GET", request, false);    
    xmlhttp.send(null);
    SetupDarkBackground();     
    var div = document.getElementById("confirmationPopup");
    if (div == null) {
        div = document.createElement('div');
        div.setAttribute("id", "confirmationPopup");
        //div.setAttribute("class", "pop-up-sm");
        div.className = "pop-up-sm";
        div.style.position = 'absolute';
        var sListTopNew = sListTop + 140;
        var sListLeftNew = sListLeft;
        div.style.top = sListTopNew + 'px';
        div.style.left = sListLeftNew + 'px';
        div.style.display = 'inline';
        div.style.textAlign = 'center';
        div.style.backgroundColor = '#fff';
        div.style.zIndex = 9999;
        
        ////  Need this for FireFox
        div.setAttribute("style", "top:" + sListTopNew + "px; left:" + sListLeftNew + "px; display:inline;text-align:center; position:absolute;text-align:center; background-color:#fff; z-index:9999;");
        
        var innerDiv = document.createElement('div');
        var innerButton = document.createElement("a");
        var innerSpan = document.createElement("span");
        innerSpan.innerHTML = "Cerrar";
        innerButton.className = "std-button";  
              
        innerDiv.innerHTML = xmlhttp.responseText + "<br />";
        if (xmlhttp.responseText == "El artículo ha sido agregado a tu lista.") {
            RemoveAddToShopperListPopup();
            innerButton.onclick = function() {
                var d = $get("confirmationPopup");
                d.parentNode.removeChild(d);
                RemoveDarkBackground();
                return (xmlhttp.status == 200);
            }
        }
        else if (xmlhttp.responseText == "Especifica el nombre de una nueva lista de compra. El nombre debe ser único.") {
            sListLeftNew = sListLeft;
            div.style.left = sListLeftNew + 'px';
            innerButton.onclick = function() {
                var d = $get("confirmationPopup");
                d.parentNode.removeChild(d);
                return (xmlhttp.status == 200);
            }
        }
        else {
            sListLeftNew = sListLeft;
            div.style.left = sListLeftNew + 'px';
            innerButton.onclick = function() {
                var d = $get("confirmationPopup");
                d.parentNode.removeChild(d);
                return (xmlhttp.status == 200);
            }
        }

        div.appendChild(innerDiv);
        innerDiv.appendChild(innerButton);
        innerButton.appendChild(innerSpan);
        document.body.appendChild(div);
    }
}

function SubmitPostMultipleToShopperListRequestToServer(isNew, isDefault, ActionToExecute) {
    var xmlhttp = GetXmlHttp();
    var request = "/ShopperList.axd";
    var poststring = ShopperList_CurrentProducts + "&ln=" + ShopperList_ListName + "&isPost=True&ExecAction=" + ActionToExecute + "&sourceListId=" + sourceListId + "&default=" + isDefault;
    if (isNew) {
        poststring += "&new=true";
    }
  
    xmlhttp.open("POST", request, false);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlhttp.send(poststring);
    alert(xmlhttp.responseText)
    return (xmlhttp.status == 200);
}

function SubmitPostMultipleToCartRequestToServer(listId, items) {
    var result = true;
    if (items != null && items != '') {
        var xmlhttp = GetXmlHttp();
        var request = "/ShopperList.axd";
        ShopperList_CurrentProducts = items;
        sourceListId = listId;
        var poststring = ShopperList_CurrentProducts + "&isPost=True&ExecAction=AddToCart&sourceListId=" + sourceListId;

        xmlhttp.open("POST", request, false);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        xmlhttp.send(poststring);
        alert(xmlhttp.responseText)
        result = xmlhttp.status == 200
    }
    return result;
}

function SubmitPostUpdateQuantityRequestToServer(listId, items) {
    if (items != null && items != '') {
        var xmlhttp = GetXmlHttp();
        var request = "/ShopperList.axd";
        ShopperList_CurrentProducts = items;
        sourceListId = listId;
        var poststring = ShopperList_CurrentProducts + "&isPost=True&ExecAction=Update&sourceListId=" + sourceListId;

        xmlhttp.open("POST", request, false);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        xmlhttp.send(poststring);
        alert(xmlhttp.responseText)
        result = xmlhttp.status == 200
    }
    return result
}

function SubmitAddToDefaultShopperListRequest(productId, catalogName, variantId, qty) {

    var xmlhttp= GetXmlHttp(); 

    var request = "/ShopperList.axd?pid=" + productId + "&cid=" + catalogName + "&vid=" + variantId + "&qty=" + qty;
    xmlhttp.open("GET", request, false);   
    xmlhttp.send(null);
    alert(xmlhttp.responseText);
    return (xmlhttp.status == 200); 
}

function SaveToWishListandDeleteCart(SaveToDefaultList) {

    var xmlhttp = GetXmlHttp();

    var request = "/ShopperList.axd?DeleteCart=True&SaveToWishList=" + SaveToDefaultList;
    xmlhttp.open("GET", request, false);
    xmlhttp.send(null);
    
    if (xmlhttp.status != 200) {
        alert(xmlhttp.responseText); 
     }
     location.href = "cart.aspx";
     return (xmlhttp.status == 200);
}

function GetDivContext()
{
    var xmlhttp= GetXmlHttp();
    var request = "/ShopperList.axd?GetPopUpList=True";
    xmlhttp.open("GET", request, false);               
    xmlhttp.send(null);                      
    var div = document.getElementById("AddToShopperListPopup");            
    div.innerHTML = xmlhttp.responseText;
}

function SetDivContextDeleteCart(isAuthenticated) {
    var div = document.getElementById("savetowishlistpopupId");
   
    if (isAuthenticated == true) {
        div.innerHTML = "<p>¿Desea guardar el contenido de esta cesta de la compra como una lista de deseos (Sí / No)?</p><div class='btn-holder'><a id='btnNo' onclick='return SaveToWishListandDeleteCart(false);' class='std-button floatright'><span>No</span></a><a id='btnYes' onclick='return SaveToWishListandDeleteCart(true);' class='std-button floatright right-spacem'><span>Si</span></a><a id='btnCancelar' onclick='HideDeleteCartPoppup();' class='std-button cancel floatleft'><span>Cancelar</span></a></div>";
    }
    else {
        div.innerHTML = "<p>¿Está seguro que desea borrar el carrito (Sí / No)?</p><div class='btn-holder'><a id='btnNo' onclick='HideDeleteCartPoppup();' class='std-button floatright'><span>No</span></a><a id='btnYes' onclick='return SaveToWishListandDeleteCart(false);' class='std-button floatright right-spacem'><span>Si</span></a>";
    }
    
}

function RedirectToCartPage() {
    location.href = "cart.aspx";
}

function GetListtoListDivContext(listId) {
    var xmlhttp = GetXmlHttp();
    var request = "/ShopperList.axd?GetPopUpList=True&sourceListId=" + listId;
    xmlhttp.open("GET", request, false);
    xmlhttp.send(null);
    var div = document.getElementById("AddToShopperListPopup");
    div.innerHTML = xmlhttp.responseText;
}

function FindPosition( oElement ) 
{
    if( typeof( oElement.offsetParent ) != 'undefined' ) 
    {
        for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) 
        {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
        }
        return [ posX, posY ];
    } 
    else 
    {
        return [ oElement.x, oElement.y ];
    }
} 

function GetXmlHttp()
{        
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try 
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
        try 
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (E) 
        {
            xmlhttp = false;
        }
    }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp=false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp=false;
        }
    }
    return xmlhttp;
}

function GetVariantId(parent,dropdownControlId)
{
   var node = parent.parentNode;
   node = node.parentNode;
   var elements = node.getElementsByTagName("*");
   for( var i = 0; i< elements.length; i++)
   { 
      var id = elements[i].getAttribute("id");
      if (id!=null && (id.match(dropdownControlId)!= null))
      {
	  controlId = id.substring(0,id.indexOf(dropdownControlId)-1);	
          controlId = controlId.substring(0,controlId.lastIndexOf("_"));
	  var dropdownControl = document.getElementById(id);
	  return dropdownControl.value;              
      }
  }
   return "";
}

function GetWeightValue(dropdownControlId) {

    if (dropdownControlId == null || dropdownControlId == "")
        return "";
    var dropdownControl = document.getElementById(dropdownControlId);
    if (dropdownControl == null) {
        return "";
    }
    return dropdownControl.value;
} 

function GetElementByIdMatch(idMatch)
{
   var node;
   if(controlId == null) 
   {
      node = document;
   }
   else
   {
      node = document.getElementById(controlId);
   }
   var elements = node.getElementsByTagName("*");	   
   for( var i = 0; i< elements.length; i++)
   { 
      var id = elements[i].getAttribute("id");
      if (id!=null && (id.match(idMatch)!= null))
      {
	  return document.getElementById(id); 
      }	
   }
}

function GetElementByNameMatch(nameMatch) 
{
    var node = document;
   
    return elements = node.getElementsByName(nameMatch);
}

function GetSelectedItems(clientId, nameMatch) {
    var selectedItems = GetElementByNameMatch(clientId+nameMatch);
    var result = "";
    if (selectedItems != null)  
    {
        for (i = 0; i < selectedItems.length; i++) 
        {
            if (selectedItems[i].checked == true) 
            {
                if (result == "") 
                {
                    result = selectedItems[i].value ;
                }
                else 
                {
                    result +=  ";" + selectedItems[i].value ;
                }
            }
        }
    }
    if (result != null && result != '') {
        result = nameMatch + "=" + result;
    }
    return result;
}

function GetItemsQuantity(clientId, nameMatch) {
    var selectedItems = GetElementByNameMatch(clientId + nameMatch);
    var result = "";
    if (selectedItems != null) {
        for (i = 0; i < selectedItems.length; i++) {
            var strId = selectedItems[i].id;
                       
                if (result == "") {
                    result = strId.replace(clientId + nameMatch, "") + "=" + selectedItems[i].value;
                }
                else {
                    result += "&" + strId.replace(clientId + nameMatch, "") + "=" + selectedItems[i].value;
                }
           }
       }
  return result;
} 

function GetElementValueByIdMatch(idMatch) 
{
    var node = GetElementByIdMatch(idMatch);
    if (node != null) 
    {
        return node.value;
    }
    else
        return "";
}

function SetFormStatus()
{
    var ListOption1 = document.getElementById("ListOption1"); 
    var ListSelector = document.getElementById("ExistingListSelector");
    var DefaultCheck = document.getElementById("DefaultCheck");
    var NewListName = document.getElementById("NewListName");

    if ( ListOption1 != null && ListSelector != null && DefaultCheck != null && NewListName != null) 
    {
        if(ListOption1.checked )
        {
	        ListSelector.disabled = false;
	        DefaultCheck.disabled = true;	
            NewListName.disabled = true;
        }
        else
        {
            ListSelector.disabled = true;
	        DefaultCheck.disabled = false;	
            NewListName.disabled = false;
        }
    }
}

function ShowRequireMembershipMessage( ) {
    document.getElementById("MembershipRequired").style.display = 'block';
}

function ShowSaveToWishListPopup() { 
}
function GetCartUrlContinue(radioName) {
    var radioValue = get_radio_value(radioName)        
    location.href = 'Cart.aspx?continue=yes&DeliveryMethod=' + radioValue; 
}

function get_radio_value(radioName) {
    var radiolist = document.getElementsByName(radioName);
    var result = "";
    
    if (radiolist != null){
        for (var i = 0; i < radiolist.length; i++) {
            if (radiolist[i].checked) {
                 result = radiolist[i].value;
            } 
        }
    }
    return result;
}
function HideDeleteCartPoppup() {

    var div = document.getElementById("savetowishlistpopupId");
    div.style.display = "none"; //to hide it
}

