//###############################
//# 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
  var Today           = new Date();

  //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;

    objHTTPRequest.open("GET", '/Modules/ICB/eCommerce/Scripts/AddToBasket.aspx?CS=' + Today.getSeconds() * Today.getMilliseconds() + '&' + strParams, true);
    objHTTPRequest.onreadystatechange = function()
                                        {
                                          if (objHTTPRequest.readyState==4) 
                                            _icb_AddItemsToBasketResponse(objHTTPRequest.responseText);
                                        };
    objHTTPRequest.send(null);  	

    //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.toLowerCase().indexOf('<result>') != -1 && pstrResponse.toLowerCase().indexOf('<productref>') != -1)
  {
    //Extract the result
    strResult = pstrResponse.substring(pstrResponse.toLowerCase().indexOf('<result>') + 8);
    strResult = strResult.substring(0, strResult.toLowerCase().indexOf('<'));
  
    //Extract the productref
    strProductRef = pstrResponse.substring(pstrResponse.toLowerCase().indexOf('<productref>') + 12);
    strProductRef = strProductRef.substring(0, strProductRef.toLowerCase().indexOf('<'));

    //Extract the Qty
    strProductQty = pstrResponse.substring(pstrResponse.toLowerCase().indexOf('<productqty>') + 12);
    strProductQty = strProductQty.substring(0, strProductQty.toLowerCase().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 basket<br />To remove it click on the trash can in the My Basket area<br /><br />';    
        else
          document.getElementById('div' + strProductRef).innerHTML = strProductQty + ' items have been added to your basket<br />To remove it click on the trash can in the My Basket area<br /><br />';
      }
      
      //If this page contains a basket summary call a function to refresh the basket summary
      try
      { 
        _icb_RefreshAllBasketSummaryComponents();
        //_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;
}   

