//###############################
//# Declarations                #
//###############################


//###############################
//# Functions                   #
//###############################
function _icb_AddItemsToBasket(pstrProductRef, pintPPCId)
{

  //Declarations  
  var strParams       = '';
  var intQty          = 1;
  var intProductId    = 0;
  var objHTTPRequest  = _icb_CreateHTTPRequestObject();  //Create an ajax request object
  
  //Check to make sure the Qty field exists for this product
  if (document.getElementById('txt' + pstrProductRef))
  {    
    //Call a function to disable the Qty field and Buy button for this product
    _icb_EnableDisableProductControls(pstrProductRef, false);      
        
    //Show that we are adding this product the shopping basket
    if (document.getElementById('div' + pstrProductRef))
    {    
      document.getElementById('div' + pstrProductRef).innerHTML = 'Adding to basket, please wait...';
      document.getElementById('div' + pstrProductRef).style.display = '';
    }
      
    //Get the Quantity and Product Id
    intQty = document.getElementById('txt' + pstrProductRef).value;
    intProductId = pstrProductRef.substring(pstrProductRef.lastIndexOf('_') + 1);
    
    //Build are form post parameters
    strParams = '_icb_ProductId='  + intProductId + '&' +
                '_icb_ProductRef=' + pstrProductRef + '&' +
                '_icb_ProductQty=' + intQty + '&' +
                '_icb_PPCId=' + pintPPCId;
                            
    //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) 
                                            _icb_AddItemsToBasketResponse(objHTTPRequest.responseText);
                                        };
    //Send the ajax request
    objHTTPRequest.send(strParams)                      
  }
  else
    alert('Unable to locate the Qty field for this product!')
}

function _icb_AddItemsToBasketResponse(pstrResponse)
{
  //Declarations
  var strResult     = '';
  var strProductQty = '';
  var strProductRef = '';
  
  //If we have received the expected result and productref tags continue
  if (pstrResponse.indexOf('<result>') != -1 && pstrResponse.indexOf('<productref>') != -1)
  {
    //Extract the result
    strResult = pstrResponse.substring(pstrResponse.indexOf('<result>') + 8);
    strResult = strResult.substring(0, strResult.indexOf('<'));
  
    //Extract the productref
    strProductRef = pstrResponse.substring(pstrResponse.indexOf('<productref>') + 12);
    strProductRef = strProductRef.substring(0, strProductRef.indexOf('<'));

    //Extract the Qty
    strProductQty = pstrResponse.substring(pstrResponse.indexOf('<productqty>') + 12);
    strProductQty = strProductQty.substring(0, strProductQty.indexOf('<'));

    //If we received a result of OK tell the user that ? number of items have been added to their basket
    if (strResult.toUpperCase() == "OK")
    {
      if (document.getElementById('div' + strProductRef))
      {
        if (strProductQty == '1')
          document.getElementById('div' + strProductRef).innerHTML = strProductQty + ' item has been added to your bsket';    
        else
          document.getElementById('div' + strProductRef).innerHTML = strProductQty + ' items have been added to your bsket';    
      }
      
      //If this page contains a basket summary call a function to refresh the basket summary
      try
      {      
        _icb_RefreshBasketSummaryBlocks();
      }
      catch(e){}

    }
    else
    {
      //AddToBasket called failed to tell the user
      if (document.getElementById('div' + strProductRef))
        document.getElementById('div' + strProductRef).innerHTML = 'Add to basket failed!<br />Please refresh and try again';
    }

    //Call a function to disable the Qty field and Buy button for this product
    _icb_EnableDisableProductControls(strProductRef, true);
    
  }
}

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;
}   

