//###############################
//# Declarations                #
//###############################


//###############################
//# Functions                   #
//###############################
function _eCom_mfAddItemsToBasket()
{
  //Declarations  
  var strProductId       = '';
  var strParentProductId = '';
  var strParams          = '';
  var intQty             = 1;
  var strAttributes      = '';
  var objHTTPRequest     = ICB_CreateHTTPRequestObject();  //Create an ajax request object
  var Today              = new Date();
  
  if (window.location.href.toLowerCase().indexOf('pid=') != -1)
  {
    strProductId = window.location.href.substring(window.location.href.toLowerCase().indexOf('pid=') + 4);
    if (strProductId.indexOf('&') !=-1) strProductId = strProductId.substring(0, strProductId.toLowerCase().indexOf('&'));
  }
  else if (document.getElementById('_ecom_txtPId') != null)
    strProductId = document.getElementById('_ecom_txtPId').value;    
  
  if (window.location.href.toLowerCase().indexOf('ppcid=') != -1)
  {
    strParentProductId = window.location.href.substring(window.location.href.toLowerCase().indexOf('ppcid=') + 6);
    if (strParentProductId.indexOf('&') !=-1) strParentProductId = strParentProductId.substring(0, strParentProductId.toLowerCase().indexOf('&'));
  }
  else if (document.getElementById('_ecom_txtPPCId') != null)
    strParentProductId = document.getElementById('_ecom_txtPPCId').value;      
  
  //Check to make sure the Qty field exists for this product
  if (document.getElementById('_eCom_txtQty'))
  {                      
    //Get the Quantity and Product Id
    intQty = document.getElementById('_eCom_txtQty').value;    
    
    //Call a function that will build a , sep list of the attribute options that have been selected by the customer
    strAttributes = _eCom_mfGetAttributeSelection();    
       
    //Build are form post parameters
    strParams = '_icb_ProductId='  + strProductId + '&' +                
                '_icb_ProductQty=' + intQty + '&' +
                '_icb_PPCId=' + strParentProductId + '&' +
                '_icb_Attr=' + strAttributes;
                            
    //Setup an ajax POST command to the AddToBasket.aspx script to add the requested number of the requested product
    //to the users shopping basket
    //objHTTPRequest.open("POST", '/Modules/ICB/eCommerce/Scripts/AddToBasket.aspx', true);
    //objHTTPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //objHTTPRequest.setRequestHeader("Content-length", strParams.length);
    //objHTTPRequest.setRequestHeader("Connection", "close");
    
    //Build the function that will be executed the HTTPRequest object's onreadystatechange event
    //objHTTPRequest.onreadystatechange = function()
    //                                    {
    //                                      if (objHTTPRequest.readyState==4) 
    //                                        _eCom_AddItemsToBasketResponse(objHTTPRequest.responseText);
    //                                    };
    //Send the ajax request
    //objHTTPRequest.send(strParams)                      

    objHTTPRequest.open("GET", '/Modules/ICB/eCommerce/Scripts/AddToBasket.aspx?CS=' + Today.getSeconds() * Today.getMilliseconds() + '&' + strParams, true);
    objHTTPRequest.onreadystatechange = function()
                                        {
                                          if (objHTTPRequest.readyState==4) 
                                            _eCom_AddItemsToBasketResponse(objHTTPRequest.responseText);
                                        };

    objHTTPRequest.send(null);


  }
  else
    alert('Unable to locate the Qty field for this product!')
}

function _eCom_AddItemsToBasketResponse(pstrResponse)
{

  try
  {      
    _icb_RefreshAllBasketSummaryComponents();
  }
  catch(e){}
}

function _eCom_mfGetAttributeSelection()
{
  var intLoop       = 1;
  var strSelection  = '';
  
  while (document.getElementById('txtattributeid_' + intLoop))
  {
    strSelection += ',' + document.getElementById('cboattributelist_' + intLoop).options[document.getElementById('cboattributelist_' + intLoop).selectedIndex].value;
    intLoop ++;
  }
  
  if (strSelection.length > 0 && strSelection.substring(0,1) == ',')
    strSelection = strSelection.substring(1);
    
  return strSelection;
}

function _icb_EnableDisableProductControls(pstrProductRef, pblnEnabled)
{
  if (document.getElementById('cmd' + pstrProductRef))
    document.getElementById('cmd' + pstrProductRef).disabled = ! pblnEnabled;
  if (document.getElementById('txt' + pstrProductRef))
    document.getElementById('txt' + pstrProductRef).disabled = ! pblnEnabled;
}


function _icb_CreateHTTPRequestObject()
{
  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();		 
		  //xmlhttp.overrideMimeType('text/xml');
	  } catch (e) {
		  xmlhttp=false;
	  }
  }
  if (!xmlhttp && window.createRequest) {
	  try {
		  xmlhttp = window.createRequest();
	  } catch (e) {
		  xmlhttp=false;
	  }
  }
  return xmlhttp;
}   

