/*
----------- FundSys.net(TM) Copyright SySys(R) Corp 2002 ------------
====================================================================
  Created By: BH
  Last Edited By: BH
  Inception Date: 1/1/2002
  Last Edited Date: 1/1/2002
  Description:  None
  File Dependencies: None
===================================================================''*/

/*'''---------------------------------
'' getQueryStringValue(key)
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: returns a value from a query string key
   Returns: the value of the query string parameter, null if it does not exist
----------------------------------'''*/ 
function getQueryStringValue(key){
  //parse the query string
  var qStr = location.search.substring(1);
  var QNameValuePairs = new Array();
  var curQPair = new Array();
  QNameValuePairs = qStr.split('&');
  //Look at each query string name/value pair
  for(i=0; i< QNameValuePairs.length; i++){
    curQPair = QNameValuePairs[i].split("=")
    if(curQPair[0].toLowerCase() == key.toLowerCase()){
      return curQPair[1];
    }  
  }
  return null;
}

/*'''---------------------------------
'' addQueryStringValue(formName, key, value)
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: Adds a query string key to the given form submit.  updates if the key exists
   Parameters:
     formName: the form submit that you wish to add the querystring value to
     key: key identifier for the query string variable
     value: initial value of the query string key  
   Returns: None
----------------------------------'''*/ 
function addQueryStringValue(formName, key, value){
  var frm = eval('document.' + formName);
  qStr = frm.action;
  if(qStr.indexOf('?')==-1){
    qStr += location.search;
  }
  //query string exists
  if(qStr.indexOf('?')!=-1){
    qStr = qStr.substring(qStr.indexOf('?'),qStr.length);
    //key exists
    if(qStr.indexOf(key)>-1){
      var valPos = qStr.indexOf(key) + key.length + 1;
      var endPos = qStr.indexOf('&',valPos);
      //last query string value
      if(endPos == -1){
        qStr = qStr.substring(0,valPos) + value
      //middle query string value
      }else{
        qStr = qStr.substring(0,valPos) + value + qStr.substring(endPos, qStr.length);
      }
    //key does not exist
    }else{
      qStr += '&' + key + '=' + value;
    }
  //query string does not exist
  }else{
    qStr = '?' + key + '=' + value;
  }
  frm.action = qStr;
}



/*'''---------------------------------
'' buildSortString(columnName,sortList){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: manages a sort string.  Adds columnName to front of sortList, moves to front if exists
   Parameters:
     columnName: name of column to add to sort list
     sortList: existing sort list to be added to
     directionDefault: the default direction for sort to go ASC=Ascending, DESC=Descending
   Returns: str string with whitespace removed from front
----------------------------------'''*/ 
function buildSortString(columnName,sortList,directionDefault){
  var newLst = ''
  if(sortList==null){sortList='';}
  var sortPos = sortList.indexOf(columnName);
  var dirPosS = sortPos + columnName.length + 1;
  var dirPosLen;
  //Last DESC
  if(sortList.substring(dirPosS,dirPosS+1)=='D'){
    directionDefault = 'DESC';
    dirPosLen = 4; //len of DESC
  //Last ASC
  }else if(sortList.substring(dirPosS,dirPosS+1)=='A'){
    directionDefault = 'ASC';
    var dirPosLen = 3; //len of ASC
  //Last Unspecified - Use Default
  }else{
    //flip defaults because they will be fliped later
    if(directionDefault.substring(0,1)=='D'){
      directionDefault = 'ASC';  
      dirPosLen = 3;
    }else{
      directionDefault = 'DESC';
      dirPosLen = 4; 
    }
  }
  //column in sort list (remove & place first)
  if(sortPos!=-1){
    if(sortPos==0){ //first col
      newLst = sortList;
    }else{ // not first col
      newLst = columnName + ' ' + directionDefault + ',' + sortList.substring(0,sortPos) + sortList.substring(sortPos+columnName.length+dirPosLen+2,sortList.length);
      //remove last comma 
      if(newLst.substring(newLst.length-1,newLst.length)==','){
        newLst = newLst.substring(0,newLst.length-1)
      }
    }
  //column not in sort list (place first)
  }else{
    if(sortList.length > 0){
      newLst = columnName + ' ' + directionDefault + ',' + sortList;
    }else{
      newLst = columnName + ' ' + directionDefault;
    }
  } 
  //flip ASC / DESC
  if(newLst.substring(columnName.length+1,columnName.length+1+dirPosLen)=='DESC'){
    newLst = newLst.substring(0,columnName.length+1) + 'ASC' + newLst.substring(columnName.length+dirPosLen+1,newLst.length);
  }else{
    newLst = newLst.substring(0,columnName.length+1) + 'DESC' + newLst.substring(columnName.length+dirPosLen+1,newLst.length);
  }
 
  return newLst;
}