﻿//this function is being called when clicking the sort button
function goSort(parentID)
{
    var value = document.getElementById(parentID).value;

//    var valueParent=document.getElementById('ctl00_ctl00_ctl00_SiteContentPlaceHolder_MainContentPlaceHolder__mainContent_CategoryListing1_MercadoResults1_cmbSortOptions').value;
    
    // 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 = baseURL + (baseURL.indexOf('?')>0 ? '&' : '?') + '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 = "/compare.aspx?selected_prod_ids=" + selected_ids;
    }
}
