// SetCurrentPage.js
// Rewritten to handle search strings (ie queries)
// Larry Ching  Oct. 29, 2008
//


function extractPageName(hrefString)
{
 var arr = hrefString.split('/');
 if(arr.length >= 2) {
	  if (arr[arr.length-1] == '') // example: domain.com/foldername/
	  {
		return arr[arr.length-2]+"index"; // returns any link that also have index
	  }
	  else
	  {
	  	arr2 = arr[arr.length-1].split('.');
		return arr[arr.length-2]+arr2[0].toLowerCase();
	  }	 
  } else {   // if the url is incomplete, prevents errors
  return "x";
 }
}

// search through all the links in array, if one points to
// the same file, apply the class .current to it and to its 
// parent

function setActiveMenu(arr, crtPage)
{
  for(var i=0; i < arr.length; i++)
  if(extractPageName(arr[i].href) == crtPage)
  {
	// DEBUG // alert (crtPage);  
    arr[i].className = 'current';
    arr[i].parentNode.className = 'current';
  }
}
// ============== Start of new code ===========================


function match_pathnames(theaobj){
  // Define temp variables, removing trailing whitespace.
  var temp_a_pathname = theaobj.pathname.replace(/\s+$/,"") ;
  var temp_wl_pathname = window.location.pathname.replace(/\s+$/,"") ;
  
  // Process theaobj.pathname first to handle
  // cross-browser inconsistency in using a leading "/"
  if(temp_a_pathname != "" && temp_a_pathname.charAt(0) != "/") {
    temp_a_pathname = "/" + temp_a_pathname ;
  }
  
  // Test for last character - handle cases where it is a "/"
  // De Anza default for "/" is index.html.
  if(temp_a_pathname.lastIndexOf("/") == temp_a_pathname.length - 1){
    // temp_a_pathname ends with a "/"
    if(temp_wl_pathname.indexOf("index.html") > -1){
      temp_a_pathname = temp_a_pathname + "index.html" ;
    }
  }else if(temp_wl_pathname.lastIndexOf("/") == temp_wl_pathname.length - 1){
    // temp_wl_pathname ends with a "/"
    if(temp_a_pathname.indexOf("index.html") > -1){
      temp_wl_pathname = temp_wl_pathname + "index.html" ;
    }
  }
  
  if(temp_a_pathname != temp_wl_pathname){
    // Pathnames do not match
    return false;
  }else{
    // Pathnames match
    return true;
  }
}

function match_searches(theaobj){
  if (window.location.search == "" && theaobj.search == ""){
    // Absence of both search strings is OK 
    return true ;
  }else if ((window.location.search != "" && theaobj.search == "")||(theaobj.search != "" && window.location.search == "")){
    // Only one search string found
    return false ;
  }else if (theaobj.search != "" && window.location.search != ""){
    var a_searchstr = theaobj.search ;
    var wl_searchstr = window.location.search;
    
    // remove any trailing whitespace
    a_searchstr = a_searchstr.replace(/\s+$/,"") ;
    wl_searchstr = wl_searchstr.replace(/\s+$/,"") ;
    
    // remove leading "?"
    a_searchstr = a_searchstr.substr(1);
    wl_searchstr = wl_searchstr.substr(1);
    
    var a_amploc = a_searchstr.indexOf("&");
    var wl_amploc = wl_searchstr.indexOf("&");
    
    var a_pairs = new Array();
    var wl_pairs = new Array();
    
    if ((a_amploc > -1 && wl_amploc == -1)||(a_amploc == -1 && wl_amploc > -1)){
    // Search strings don't match - different amounts of & in string
      return false ;
    }else if(a_amploc == -1 && wl_amploc == -1){
      // Neither have an ampersand, so only one label=value pair
      if (a_searchstr == wl_searchstr) {
        // Search strings match
        return true;
      }else{
        // Search strings do not match
        return false;
      }
    }else if(a_amploc > -1 && wl_amploc > -1){
      // Must parse search strings
      
      // Obvious check first
      if (a_searchstr == wl_searchstr) {
        // Searches match
        return true;
      }
      // At this point, the searches may not match as text strings, but may 
      // still contain the same label=value pairs, in different sequences.
      
      var a_lvpairs = a_searchstr.split("&") ;
      var wl_lvpairs = wl_searchstr.split("&") ;
      var a_labels = new Array() ;
      var wl_labels = new Array() ;
      var a_values = new Array() ;
      var wl_values = new Array() ;
      var temphold = new Array() ;
      for (var i = 0; i < a_lvpairs.length; i++) {
        temphold = a_lvpairs[i].split("=") ;
        a_labels[i] = temphold[0] ;
        a_values[i] = temphold[1] ;
      }
      for (var i = 0; i < wl_lvpairs.length; i++) {
        temphold = wl_lvpairs[i].split("=") ;
        wl_labels[i] = temphold[0] ;
        wl_values[i] = temphold[1] ;
      }
      if (a_labels.length != wl_labels.length) {
        // Amount of label/value pairs do not match
        return false;
      }
        
      // At this point, we have two pairs of arrays of equal length.
      // Continue development here - otherwise search parser is good enough to use.
      return true;
    }
  }else{
    // Default is no match
    return false;
  }
  // Default is no match
  return false ;
}

function matchWL2AProps(theaobj){
  // 
  if (theaobj.hostname != "") {
    if (theaobj.hostname == window.location.hostname) {
      // hostname properties match
      // now must test for matching pathnames
      if (match_pathnames(theaobj)) {
        // Pathnames match, now test search values
        return match_searches(theaobj) ;
      }else{
        // Pathnames didn't match
        return false;
      }
    }else{
      // hostname properties don't match, and they should for properly formed
      // left nav links in nav.inc. An absence of a mark will indicate
      // an error in the href of the link.
      return false ;
    }
  }else{
    // Might indicate a link href value that has been set with JavaScript.
    // Return "false" for now
    return false ;
  }
}

function setActiveMenu2(listnav_a_arr)
{
  for(var i=0; i < listnav_a_arr.length; i++)
  if(matchWL2AProps(listnav_a_arr[i]))
  {
    listnav_a_arr[i].className = 'current';
    listnav_a_arr[i].parentNode.className = 'current';
    i = listnav_a_arr.length; // stops search at first match
  }
}

// call this method from your page
function setPage()
{
  /* if(window.location.href) 
    hrefString = window.location.href;
      else
    hrefString = window.location; */

  if (document.getElementById('listnav')!=null)
    setActiveMenu2(document.getElementById('listnav').getElementsByTagName('a'));
}