﻿//this function is being called when clicking the sort button
function goSort(ddlSortId)
{
    var value=document.getElementById(ddlSortId).value;
    var qsDelim = '?';
    
    // DMH 20080630 - modified to work in the CMC
//    var baseURL = document.getElementById('baseURLWithoutSort').value;
      var baseURL = document.URL;
    // DMH 20080630: this trims existing sort if it exists
    var sort_location = baseURL.indexOf('sort_option')
    if (sort_location > -1) {
          // Split the url into an array where the sort option is
          url_array = baseURL.split('sort_option');
          // Get the length of the second array value
          url_array_1_length = url_array[1].length;
          // Check to see if there is a querystring variable after sort (we don't want to lose it if so)
          // This will also tell us how much to trim (we don't want the value of the sort option either)
          nextquery = url_array[1].indexOf('&') + 1;
          if (nextquery > 0){
                // New url is the first half and the second, less the sort value
                baseURL = url_array[0] + url_array[1].substring(nextquery, url_array_1_length);
                } else {
                // New url is the first half, but we need to trim the ? or & that held the sort query option
                baseURL = url_array[0].substring(0, url_array[0].length - 1);
                }
          }
          var url;
          
          /*
          !!!!!!!!!!!!!!!!!!!!!!!!!
          MODIFIED FROM ORIGINAL
          Append ? instead of & when required.
          '''''''''''''''''''''''''
          */
          
          if(baseURL.indexOf('?') > -1) {
              qsDelim = '&';
          }
          
          url = baseURL + qsDelim + 'sort_option=' + value;
      
      window.location.href = url;
}
//this function is being called when clicking the compare button
function goCompare()
{
    //get all INPUT tags
    var compareChecks=document.getElementsByTagName ("input");
    if (compareChecks != null)
    {
      var selected_ids ="";
      for (var i=0; i< compareChecks.length; i++)
      { 
      
        //if it's a Checkbox and the ID contains the chkCompare string 
        // meaning it's a compare checkbox
        if (compareChecks[i].type =="checkbox") 
        {
          if (compareChecks[i].id.indexOf("chkProductCompare") != -1)
          {
            //if it's chekced, get the ID, from an attribute set on the server side and add it to the selected IDs list
            if (compareChecks[i].checked) 
                selected_ids += ((selected_ids.length==0)? "" : "|") + compareChecks[i].attributes["prod_id"].value;
          }
        }
      }
      if (selected_ids.length == 0)
        alert ("Please select products to compare");
      else //redirect to the compare page with the relevent url params
          window.location.href = "ProductCompare.aspx?selected_prod_ids=" + selected_ids;
    }
}

/* 
!!!!!!!!!!!!!!!!!!!!!!!!!
MODIFIED FROM ORIGINAL JS in topstrip.js
This allows us to change configuration options based on user selections.
For instance the ItemsPerPage is driven by a drop down. It is IMPORTANT to send
ALL these configuration parameters because the Mercado code saves the configuration
options in session and needs to detect if any changes have been made per request.
'''''''''''''''''''''''''
*/

function sendConfigOptions(perPage)
{
    var URLString = window.location.href;   
    var qsDelim = "?";
    
    /*
    !!!!!!!!!!!!!!!!!!!!!!!!!
    MODIFIED FROM ORIGINAL
    Append ? instead of & when required.
    '''''''''''''''''''''''''
    */
    
    if(URLString.indexOf("?") > -1) {
	   qsDelim = "&"; 
	}
             
	//saving the values set by the user by sending them in the request for the next load		
	if (URLString.indexOf("config")!=-1)
		{URLString=URLString.substring(0,URLString.indexOf("config")-1)}
	
	{URLString = URLString + qsDelim;}
	
	URLString = URLString + 'config=true';
	
	// AllowComparison
	URLString = URLString + '&AllowCompare=false';
	//
	URLString = URLString + '&ShowHighlights=false';
	//
	URLString = URLString + '&ShowScore=false';
	//
	URLString = URLString + '&ShowCounters=false';
	//
	URLString = URLString + '&AttachedAttributes=false';
	//
	URLString = URLString + '&ShowCommonRefinements=false';
	//
    URLString = URLString + '&ItemsPerPage=' + perPage;		    
	//
	URLString = URLString + '&ItemsPerRow=3';
	//
	URLString = URLString + '&ViewBy=GRID';
	//
	URLString = URLString + '&BuyerSegments=NONE';
	       
	window.location.href = URLString;
	
/*
		value=document.getElementById('SearchTopStrip_chkCompare').checked;
		URLString = URLString + '&AllowCompare=' + value;
		value=document.getElementById('SearchTopStrip_chkShowHighlights').checked;
		URLString = URLString + '&ShowHighlights=' + value;
		value=document.getElementById('SearchTopStrip_chkShowScore').checked;
		URLString = URLString + '&ShowScore=' + value;
		value=document.getElementById('SearchTopStrip_chkShowCounters').checked;
		URLString = URLString + '&ShowCounters=' + value;
		value=document.getElementById('SearchTopStrip_chkAttachedAttributes').checked;
		URLString = URLString + '&AttachedAttributes=' + value;
		value=document.getElementById('SearchTopStrip_chkCommonRefinements').checked;
		URLString = URLString + '&ShowCommonRefinements=' + value;
		value=document.getElementById('SearchTopStrip_cmbItemsPerPage').value;
		URLString = URLString + '&ItemsPerPage=' + value;
		value=document.getElementById('SearchTopStrip_cmbItemsPerRow').value;
		URLString = URLString + '&ItemsPerRow=' + value;
		value=document.getElementById('SearchTopStrip_cmbViewBy').value;
		URLString = URLString + '&ViewBy=' + value;
		value=document.getElementById('SearchTopStrip_cmbbuyerSegments').value;
		URLString = URLString + '&BuyerSegments=' + value;
		  */
}