//
// Copyright (C) 2009 Endeca Technologies, Inc.
//
// The use of the source code in this file is subject to the ENDECA TECHNOLOGIES, INC. SOFTWARE TOOLS
// LICENSE AGREEMENT. The full text of the license agreement
// can be found in the ENDECA INFORMATION ACCESS PLATFORM THIRD-PARTY SOFTWARE USAGE AND LICENSES 
// document included with this software distribution.
//
//
//---------------------------------------------------------------------------
// Utility routines used by various javascript functions to create new queries
// from existing urls.  If current browser url does not reflect current query,
// these functions are not useful (since they are not aware of the current
// application state.)  These functions are used sparingly in the reference
// application only in conjunction with modules that contain form element.
//---------------------------------------------------------------------------

function ConstructURL(newurl,url,removeterms,addterms) {
  var hasparams = 0;
  var isfirst = 1;
  var params = BuildURLArray(url);
  for (var i=0; i<params.length; i++) {
    val = CheckArray(removeterms,params[i][0]);
    if (val == -1) {
      if (isfirst == 1) {
        newurl = newurl + "?";
        isfirst = 0;
      }
      else {
        newurl = newurl + "&";
      }
      newurl = newurl + params[i][0] + "=" + params[i][1];
      hasparams = 1;
    }
  }
  for (i=0; i<addterms.length; i++) {
    if (isfirst == 1) {
      newurl = newurl + "?";
      isfirst = 0;
    }
    else {
      newurl = newurl + "&";
    }
    newurl = newurl + addterms[i];
    hasparams = 1;
  }
  if (hasparams == 0) {
    newurl = newurl + "?N=0";
  }
  return newurl;
}
function BuildURLArray(oldurl) {
  var returnArray = new Array();
  var url;
  if (oldurl == "CURRENTURL") {
    url = location.search;
  }
  else if (oldurl == "BLANKURL") {

  }
  else {
    var tokens = oldurl.split("?");
    url = "?"+tokens[1];
  }   
  if (url) {
    url = url.substring(1);
    var params = url.split("&");
    for (var i=0; i<params.length; i++) {
      var param = params[i].split("=");
      returnArray[i] = param;
    }
  }
  return returnArray;
}
function CheckArray(removeterms,checkterm) {
  for (var i=0; i<removeterms.length; i++) {
    if (removeterms[i] == checkterm)
      return 1;
  }
  return -1;
}
function GetValue(url, term) {
  var params = BuildURLArray(url);
  for (var i=0; i<params.length; i++) {
    if (params[i][0] == term) {
      return params[i][1];
    }
  }
  return -1;
}

function ModifyURL(url,removeterms,addterms) {
  var hasparams = 0;
  var isfirst = 1;
  var params = BuildURLArray(url);
  var newurl = location.pathname;
  for (var i=0; i<params.length; i++) {
    val = CheckArray(removeterms,params[i][0]);
    if (val == -1) {
      if (isfirst == 1) {
        newurl = newurl + "?";
        isfirst = 0;
      }
      else {
        newurl = newurl + "&";
      }
      newurl = newurl + params[i][0] + "=" + params[i][1];
      hasparams = 1;
    }
  }
  for (i=0; i<addterms.length; i++) {
    if (isfirst == 1) {
      newurl = newurl + "?";
      isfirst = 0;
    }
    else {
      newurl = newurl + "&";
    }
    newurl = newurl + addterms[i];
    hasparams = 1;
  }
  if (hasparams == 0) {
    newurl = newurl + "?N=0";
  }
  return newurl;
}

function toggleMe(a){
  var e=document.getElementById(a);
  if(!e)return true;
  if(e.style.display=="none"){
    e.style.display="block"
  } else {
    e.style.display="none"
  }
  return true;
}

