
// $Header: /WebSites/affiliate/promotion/lastminutecruises/lastMinutePage_Details.js 17    9/30/09 9:15a Mccusker $


//Rewrite some of the data points on the page with the new context. 
window.onload = function() { 
  var query = window.location.toString();
  var queryArgs = query.substring((query.indexOf('?')) + 1).split("&");
  var queryFinal = "";
  
  var groupSailingCount = document.getElementById('groupSailingCount')
  if(!groupSailingCount)
    return;
  groupSailingCount = groupSailingCount.value;
  
  var filteredDataRowCount = document.getElementById("filteredDataRowCount").value;
  var displayName = document.getElementById('displayName').value;
  
  try {
      if(filteredDataRowCount < 21) {
        document.getElementById('sailingRangeTop').innerHTML = "1-" + filteredDataRowCount;
        document.getElementById('next20Top').style.display   = 'none';
        document.getElementById('sailingRangeBottom').innerHTML = "1-" + filteredDataRowCount;
        document.getElementById('next20Bottom').style.display   = 'none';
      }


      document.getElementById('sailingCountTop').innerHTML = filteredDataRowCount;
      document.getElementById('sailingCountBottom').innerHTML = filteredDataRowCount;  
      document.getElementById('detailsLoading').style.display = "none";
      document.getElementById('detailsHeader').style.display = "block";
      document.getElementById('loadingFooter').style.display = "none";
      document.getElementById('dealsFooter').style.display = "";
  }
  catch (e) {
  }
  
  var showAll   = null;
  var sort      = "";
  var sortorder = "";
  for (i = 0; i < queryArgs.length; ++i) {
    var argument = queryArgs[i];
    if ( argument.match(/sortorder/) ) {
      sortorder = argument.split('=');
      sortorder = sortorder[sortorder.length-1];
      sortorder = sortorder.toLowerCase();
    }
    else if ( argument.match(/sort/) ) { 
      sort = argument.split('=');
      sort = sort[sort.length-1];
      sort = sort.toLowerCase();
    }
    else if ( argument.match(/showAll/)) {
      showAll = (argument).split('=');
      showAll = showAll[showAll.length-1];
      showAll = parseInt(showAll);
      if(isNaN(showAll))
              showAll = 0;
    }
    else if (argument.match(/pgNumber/)) {
      // do not copy over the page number argument to the query string
      // if the user performs a new sort on this list they will be directed
      // back to the first page
    }
    else {
        if ( queryFinal.length == 0 ) { 
            queryFinal = argument;
        }
        else {
            queryFinal = queryFinal + "&" + argument; 
        }
    } 
  }    

    var columnHeaders = { Departs  : "saildate",           Interior: "interiorbestprice",
                          Oceanview: "oceanviewbestprice", Balcony : "balconybestprice",
                          Suite    : "suitebestprice"};
    try {  
      for (var key in columnHeaders) {
        var queryFinalCopy = queryFinal;
        if (sort==columnHeaders[key].toLowerCase())
          queryFinalCopy += "&sortorder=" + (sortorder=='asc' ? 'desc' : 'asc');
        else if (sort=="" && key=="Departs")
          queryFinalCopy += "&sortorder=asc";
        else
          queryFinalCopy += "&sortorder=asc";
        lmpDetailsColumn(key, columnHeaders[key], queryFinalCopy, showAll);
      }// end looping through column headers
    }// end try to update the column header links 
    catch (e) {
        alert("exception: " + e);
    }
}

//StringBuffer class
function StringBuffer() { 
    this.buffer = []; 
    
    this.append = function(string) { 
        this.buffer.push(string); 
        return this; 
    } 
    
    this.toString = function() { 
        return this.buffer.join(""); 
    } 
} 

//Rewrite the column headers with the appropriate links
function lmpDetailsColumn(name, sort, queryFinal, showAll) { 
      var thisColumn = document.getElementById(name);
      var anchor = new StringBuffer();
      anchor.append("<a href='/promotion/lastminutecruises/moresailings.asp?");
      anchor.append(queryFinal)
      anchor.append("&sort=");
      anchor.append(sort);
      if(showAll)
              anchor.append("&showAll=" + showAll);
      anchor.append("'>");
      anchor.append(name);
      anchor.append("</a>");
      thisColumn.innerHTML = anchor.toString();
}

