﻿//freeSearch.js
//this function is called when clicking on the 
//Go button in the free search area
function goFreeTextSearch(searchWithinResults, searchTextID, baseUrlID, currentUrlID, cannedTriggerID)
{
    var baseURL = document.getElementById(baseUrlID).value;
	var currentURL = document.getElementById(currentUrlID).value;
	var freeTextValue = document.getElementById(searchTextID).value;
	
	if (freeTextValue == '')
		return false;
		
	freeTextValue = encodeURIComponent(freeTextValue);

	var dt = new Date();

	//building the next search parameters URL
	var cannedTrigger = document.getElementById(cannedTriggerID);
	
	if (searchWithinResults == 'True')
	{
	  //if withing results, take the current url and add the free text to it
		var url = currentURL + '&free_text|' + dt.getTime() + '=' + freeTextValue;
		if (cannedTrigger) 
		{
			if (cannedTrigger.value != "")
				url += '&canned_results_trigger=' + cannedTrigger.value;
		}
		window.location.href= url + '&deptId=0'; // need deptId to fool Meta Tag error on BasePage;

	}
	else
	{
	  //if new free search, take the basic url and the free text to it 
	  window.location.href = 'ProductSummary.aspx?free_text|' + dt.getTime() + '=' + freeTextValue + '&deptId=0'; // need deptId to fool Meta Tag error on BasePage
	}	
	
	return false;
}

function goSelectSite(multisiteDropDown)
{
  if (multisiteDropDown.value)    
  {
    var url;
    for  (var i=0; i< multisiteDropDown.options.length; i++)
    {
      if (multisiteDropDown.options[i].value == multisiteDropDown.value)
      {
        url = multisiteDropDown.options[i].attributes ["url"].value;
        break;
      }
    }
    window.location.href= url;
  }
}

//common.js
//This function is being used as the A tag URL, 
//to include the CMC regExp indentification - when redirection is not relevant
function ignore(cmc_param) {}
//This function is being used as the A tag URL, 
//to include the CMC regExp indentification - when redirection is relevent
function loadURL (url, cmc_param, newWindow)
{
  if (newWindow)
    window.open(url, "_blank"); 
  else
    window.location.href = url;
}

//filter.js
/*Setting the collapse\expand state of the filters.  
The function is called from each filter object but appears once in the search page*/
function SwitchStyle(imgObj)
{
	if (imgObj.title=='Expand') 
	{
		//img.td.tr.tbody.table.siblingTable = children table
		//imgObj.parentElement.parentElement.parentElement.parentElement.nextSibling.style.display='';
		imgObj.title = 'Collapse';
		imgObj.src = './Images/arrow_up.gif';
	}
	else
	{
		//img.td.tr.tbody.table.siblingTable = children table
		//imgObj.parentElement.parentElement.parentElement.parentElement.nextSibling.style.display='none';
		imgObj.title = 'Expand';
		imgObj.src = './Images/arrow_down.gif';		
	}
	// TAY, Changed IE4-style "parentElement" to W3C "parentNode" for modern browsers
	toggleDisplay(imgObj.parentNode.parentNode.parentNode.parentNode.nextSibling);
}

function toggleDisplay(domEle) {
// TAY, Moved the toggle into a function to add code to work with non-IE browsers
 if (domEle) {
 myEle = domEle

  while (!myEle.style) {
   myEle = myEle.nextSibling;
    if (!myEle) break;
  }
   
  if (myEle.style.display == 'block') {
   myEle.style.display='none';
  } else {
   myEle.style.display='block';
  }
  domEle = myEle;
 }
}

//search.js
//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;
          
          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 = "?";
    
    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;
}

