/**
 * UtilClient
 *
 * static client util functions
 *
 */

function UtilClient(){}

/**
 * get_params
 * @param qs => query string
 * @return a hash indexed with the parameter names
 */
UtilClient.get_params =
function /* get_params */(qs)
{
  var s = qs;
  if (qs.indexOf("?")==0) {
    s = qs.substring(1);
  }
  var p = s.split(/&/);
  var pht={};
  if (typeof p!="undefined") {
    for(var i=0;i<p.length;i++) {
      var av=p[i].split(/=/);
      if (typeof av!="undefined") {
        pht[av[0]]=av[1]?unescape(av[1]):av[1];
      }
    }
  }
  return pht;
}

/**
 * get_domain
 * @param o => { location, fulldomain }
 * @return the domain
 */
UtilClient.get_domain =
function /* get_domain */(o)
{
   var i = 1;
   var m,l,f;
   if (Util.isOk(o)) {
      l = Util.get_if_ok(o.location,top.location);
      f = Util.get_if_ok(o.fulldomain,false);
   }
   else {
      l = top.location;
      f = false;
   }
   if (Util.isOk(l.hostname))
   {
      if (Util.isOk(f) && f) {
        m  = l.hostname.match(/\w+\.(.*)$/);
      }
      else {
        m  = l.hostname.match(/.*\.(\w+\.\w+)$/);
      }
      if ( Util.isOk(m) && Util.isOk(m.length) ) {
        return m[i];
      }
   }
   return null;
}

/**
 * get_sid
 * @param s => a SID with or without 'sid:' prefix
 * @return the sid without 'sid:' prefix
 */
UtilClient.get_sid =
function /* get_sid */(s){
   if (typeof s!="undefined") {
      if (s.indexOf("sid:")==0) {
        return s.substring(4);
      }
      else {
        return s;
      }
   }
   return "";
}

/**
 * trim
 * @param s => any string
 * @return the string received without surrounding blanks
 */
UtilClient.trim =
function /* trim */(s)
{
    for (i=0; i < s.length; i++) {
        var ch = s.charAt(i);
        if ( ch != " " ) break;
    }
    for (j = s.length-1; j>0; j--) {
        var ch = s.charAt(j);
        if ( ch != " " ) break;
    }
    return s.substring(i, j+1);
}

/**
 * get_delimiter
 * @param u => any url
 */
UtilClient.get_delimiter =
function /* get_delimiter */(u)
{
   if (u.indexOf('?')!=-1)  {
      return '&';
   }
   else {
      return '?';
   }
}
