//###############################
//# Declarations                #
//###############################
var mblnRefreshedShortlists = false;
var mintPropertyShortlistsId  = -1;

//###############################
//# Functions                   #
//###############################
function _icb_RefreshShortlistsSummaryBlocks()
{
  var strHTML     = document.body.innerHTML;
  var intStartPos = 0;
  var intEndPos   = 0;
  var intBreaker  = 0;
  
  while (intStartPos > -1 && intBreaker < 1000)
  {
    intBreaker++;
    intStartPos = strHTML.indexOf('_icb_LoadShortlistsSummary(', intEndPos);
    if (intStartPos > -1)
    {
      intEndPos = strHTML.indexOf(')', intStartPos);
      if (intEndPos > -1)
      {      
        strShortlistsSummaryName = strHTML.substring(intStartPos + 24, intEndPos - 1);
        mblnRefreshedShortlists = false;
        _icb_LoadShortlistsSummary(strShortlistsSummaryName);
      }
      else
        intStartPos = -1;
    }
  }   
}
function _icb_RefreshAllShortlistSummaryComponents()
{

  var arrLayers      = document.getElementsByTagName('div');
  var intLoop        = 0;
  var strId          = '';

  for (intLoop = 0; intLoop < arrLayers.length; intLoop ++)
  {
    if (arrLayers[intLoop].id.toLowerCase() == 'divshortlistsummarydetails')
    {                      

      strId = arrLayers[intLoop].parentNode.id;
      if (arrLayers[intLoop].parentNode.id == '')
        strId = arrLayers[intLoop].parentNode.parentNode.id;
  
      if (strId != '' && strId != undefined && strId != null)
      {        
        mblnRefreshedShortlists = false;
        _icb_LoadShortlistsSummary(strId);
      }
    }
  }
}


function _icb_DelayLoadShortlistSummary(pstrComponentName)
{
  if (navigator.appName.toLowerCase() == 'microsoft internet explorer')
  {
    if (document.readyState=='complete')
    {

      _icb_LoadShortlistsSummary(pstrComponentName)
    } 
    else
      setTimeout('_icb_DelayLoadShortlistSummary("'+pstrComponentName+'")', 100) 
  }
  else
    _icb_LoadShortlistsSummary(pstrComponentName)
}

function _icb_LoadShortlistsSummary(pstrComponentName)
{
  var strSummaryData  = '';
  var strParams       = '';
  var objHTTPRequest  = ICB_CreateHTTPRequestObject();  //Create an ajax request object
  var strCookie       = document.cookie + ';';
  var strShortlistsId     = '';
  var Today     = new Date();

  //if we do not have a productShortlists it then call another function which will get it from the server before we continue and then call this function with it
  if (!objHTTPRequest)
  {
    //The Ajax control could not be created so show an error in the Shortlists component
    _icb_LoadShortlistsSummaryResponse('<errorhtml>Ajax Disabled!<br /><br />Your browser settings currently have a feature called Ajax disabled.  Please enable this feature to allow this shopping Shortlists to operate correctly.<br /><br />- <a href="">Tell me how</a> -</errorhtml>')
  }
  else
  {
    if (mintPropertyShortlistsId == -1)
      _icb_GetProductShortlistsId(pstrComponentName)
    else
    {  

      //If we have not yet gathered the Shortlists summary do it now
      if (! mblnRefreshedShortlists)
      {
        mblnRefreshedShortlists = true;
                       
        //Send an ajax request to get the Shortlists summary data from the server
        strParams = '_icb_ShortlistsId=' + mintPropertyShortlistsId + '&' +
                    '_icb_ComponentName=' + pstrComponentName;                                    

        objHTTPRequest.open("GET", '/Modules/ICB/Property/Scripts/ShortlistSummary.aspx?CS=' + Today.getSeconds() * Today.getMilliseconds() + '&' + strParams, true);
        objHTTPRequest.onreadystatechange = function(){if (objHTTPRequest.readyState==4) _icb_LoadShortlistsSummaryResponse (objHTTPRequest.responseText);};
        objHTTPRequest.send(null)  	
      }
    }
  }
}
function _icb_LoadShortlistsSummaryResponse(pstrResponse)
{

  //Declarations
  var arrLayers      = document.getElementsByTagName('div');
  var intLoop        = 0;
  var strItemsHTML   = pstrResponse;
  var strTotalHTML   = pstrResponse;
  var strErrorHTML   = pstrResponse;
  var intStartPos    = 0;
  var intEndPos      = 0;
  
  //Extract the Shortlists Items HTML from the Ajax Response
  if (strItemsHTML.toLowerCase().indexOf('<itemhtml>') != -1 && strItemsHTML.toLowerCase().indexOf('</itemhtml>') != -1)
  {    
    intStartPos  = strItemsHTML.toLowerCase().indexOf('<itemhtml>') + 10;
    intEndPos    = strItemsHTML.toLowerCase().indexOf('</itemhtml>');    
    strItemsHTML = strItemsHTML.substring(intStartPos, intEndPos);
  }
  else
    strItemsHTML = "";
  
  //Extract the Shortlists Total HTML from Ajax response
  if (strTotalHTML.toLowerCase().indexOf('<totalhtml>') != -1 && strTotalHTML.toLowerCase().indexOf('</totalhtml>') != -1)
  {
    intStartPos  = strTotalHTML.toLowerCase().indexOf('<totalhtml>') + 11;
    intEndPos    = strTotalHTML.toLowerCase().indexOf('</totalhtml>');    
    strTotalHTML = strTotalHTML.substring(intStartPos, intEndPos);
  }
  else
    strTotalHTML = "";

  //Extract any errors from Ajax response
  if (strErrorHTML.toLowerCase().indexOf('<errorhtml>') != -1 && strErrorHTML.toLowerCase().indexOf('</errorhtml>') != -1)
  {
    intStartPos  = strErrorHTML.toLowerCase().indexOf('<errorhtml>') + 11;
    intEndPos    = strErrorHTML.toLowerCase().indexOf('</errorhtml>');    
    strErrorHTML = strErrorHTML.substring(intStartPos, intEndPos);
  }
  else
    strErrorHTML = '';
    
  if (strItemsHTML == '' && strErrorHTML == '')
  {
    strItemsHTML = '<center>Your property shortlist is currently empty!</center>';
    strTotalHTML = '';
    
  }
  else if (strErrorHTML != '')
  {
    strItemsHTML = '<center>' + strErrorHTML + '</center>';
  }
 
  //Loop through all of the layers on this page and populate any that have an id of 
  //divShortlistssummarydetails or divShortlistssummarytotal
  for (intLoop = 0; intLoop < arrLayers.length; intLoop ++)
  {
    if (arrLayers[intLoop].id.toLowerCase() == 'divshortlistsummarydetails')
    {
      //if this layer is a details layer for a Shortlists summary component populate its content
      arrLayers[intLoop].innerHTML = strItemsHTML;
    }
    else if (arrLayers[intLoop].id.toLowerCase() == 'divshortlistsummarytotal')
    {
      //if this layer is a totals layer for a Shortlists summary component populate its content            
      arrLayers[intLoop].innerHTML = strTotalHTML;
      arrLayers[intLoop].style.display = '';
    }
  }
}
function _icb_GetProductShortlistsId(pstrComponentName)
{
  var strParams       = '';
  var objHTTPRequest  = ICB_CreateHTTPRequestObject();  //Create an ajax request object
  var strCookie       = document.cookie + ';';
  var strShortlistsId     = '';
  var Today     = new Date();
  
  //Send an ajax request to get the Shortlists summary data from the server
  strParams = '_icb_ComponentName=' + pstrComponentName;

  objHTTPRequest.open("GET", '/Modules/ICB/Property/Scripts/GetPropertyEnquiryId.aspx?CS=' + Today.getSeconds() * Today.getMilliseconds() + '&' + strParams, true);
  objHTTPRequest.onreadystatechange = function(){if (objHTTPRequest.readyState==4) _icb_GetProductShortlistsIdResponse (objHTTPRequest.responseText);};
  objHTTPRequest.send(null)  	

}
function _icb_GetProductShortlistsIdResponse(pstrResponse)
{
  //Declarations
  var strComponentName   = '';
  var intProductShortlistsId = 0;
  var intLoop            = 0;
  var arrLayers          = document.getElementsByTagName('div');

  if (pstrResponse.indexOf('|') != -1)
  {
    intProductShortlistsId = pstrResponse.substring(0, pstrResponse.indexOf('|'));
    strComponentName   = pstrResponse.substring(pstrResponse.indexOf('|') + 1);
    if ((! isNaN(intProductShortlistsId)))
    {    
      mintPropertyShortlistsId = intProductShortlistsId;
      _icb_LoadShortlistsSummary(strComponentName);
    }
    else if (intProductShortlistsId == 0)
    {
      for (intLoop = 0; intLoop < arrLayers.length; intLoop ++)
      {
        if (arrLayers[intLoop].id.toLowerCase() == 'divShortlistssummarydetails')
        {
          //if this layer is a details layer for a Shortlists summary component populate its content
          arrLayers[intLoop].innerHTML = "<center>Your Shortlists is currently empty</center>";
        }        
      }
    }
  }  

}

function _icb_RemoveEnquiryItems(pintShortlistId, pintPropertyId, pintPropertyEnquiryItemId, pstrPropertyName)
{
  var strParams       = '';
  var objHTTPRequest  = ICB_CreateHTTPRequestObject();  //Create an ajax request object    
  var Today           = new Date();

  //If we have not yet gathered the Shortlists summary do it now
  if (confirm('You are about to remove the following products from your shopping Shortlists:\n\n' + pstrPropertyName.replace("'", "\'") + '\n\nAre you sure?'))
  {        
    //Send an ajax request to remove the selected items from the Shortlists 
    strParams = '_icb_EnquiryId=' + pintShortlistId + '&' +
                '_icb_PropertyId=' + pintPropertyId + '&' +
                '_icb_PropertyEnquiryItemId=' + pintPropertyEnquiryItemId;                                    

    objHTTPRequest.open("GET", '/Modules/ICB/Property/Scripts/RemoveFromShortlist.aspx?CS=' + Today.getSeconds() * Today.getMilliseconds() + '&' + strParams, true);
    objHTTPRequest.onreadystatechange = function(){if (objHTTPRequest.readyState==4) _icb_RemoveEnquiryItemsResponse(objHTTPRequest.responseText);};
    objHTTPRequest.send(null)  	

  }
}
function _icb_RemoveEnquiryItemsResponse(pstrResponse)
{

  //Check to make sure that the items have been removed ok
  if (! isNaN(pstrResponse))
  {

    if (parseInt(pstrResponse) > 0)
    {
      //Reset the Refreshed flag and call a function to refresh all Shortlists components on this page
      mblnRefreshedShortlists = false;
      _icb_RefreshAllShortlistSummaryComponents();
    }
    else
      alert('We encountered a problem while trying to remove the selected items from your Shortlists.\n\nPlease try again.')
  }
  else
    alert('We encountered a problem while trying to remove the selected items from your Shortlists.\n\nPlease try again.')
}

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;
}   
