﻿
var ShopperList_CurrentProductId
var ShopperList_CurrentCatalogName
var ShopperList_CurrentVariantId
var ShopperList_CurrentQuantity
var ShopperList_ListName
var controlId

function LaunchAddToShopperListPopup(divId, productId, catalogName, variantId, qty)
{  
    var div = document.getElementById("AddToShopperListPopup");
    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 divPos = document.getElementById(divId);
    if( divPos == null)
    {	
	    divPos = GetElementByIdMatch(divId);
    }
    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_CurrentProductId = productId;
    ShopperList_CurrentCatalogName = catalogName;
    ShopperList_CurrentVariantId = variantId;
    ShopperList_CurrentQuantity = qty;           
}

function HideAddToShopperListPopup()
{
    var div = document.getElementById("AddToShopperListPopup");
    div.style.display='none';
}

function RemoveAddToShopperListPopup()
{
    var div = document.getElementById("AddToShopperListPopup");             
    div.parentNode.removeChild(div);  
}

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(SubmitAddToShopperListRequestToServer(isNew, isDefault))
    {    
        if(isNew == true)
        {
            RemoveAddToShopperListPopup();
        }
        else
        {
            HideAddToShopperListPopup();
        } 
    }          
}  

function SubmitAddToShopperListRequestToServer(isNew,isDefault)
{
    var xmlhttp= GetXmlHttp();

    var request = "/ShopperList.axd?ln=" + ShopperList_ListName + "&pid=" + ShopperList_CurrentProductId + "&cid=" + ShopperList_CurrentCatalogName + "&vid=" + ShopperList_CurrentVariantId + "&qty=" + ShopperList_CurrentQuantity + "&default=" + isDefault;
    if(isNew)
    {
	    request += "&new=true";
    }
    xmlhttp.open("GET", request, false);    
    xmlhttp.send(null);
    alert(xmlhttp.responseText) 
    return (xmlhttp.status == 200);  
}

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 GetDivContext()
{
    var xmlhttp= GetXmlHttp();
    var request = "/ShopperList.axd";
    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;              
      }	
   }
}                    

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 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;
        }
    }
}