/* Sets the focus on the Virtua Forms
 * that is if there is a point of Focus needed
 */
function inputFocus() {
if(this.document.forms[0].t1 != null &&
   this.document.forms[0].t1.type == "text") {
  this.document.forms[0].t1.focus();
  }
else if(this.document.forms[0].sessionid != null &&
        this.document.forms[0].sessionid == "text") {
  this.document.forms[0].sessionid.focus();
  }
else if(this.document.forms[0].patronid != null) {
  this.document.forms[0].patronid.focus();
  }

}

function Querystring() {
  this.params = {};
  qs = location.search.substring(1, location.search.length);
  if (qs.length == 0) return;

  var args = qs.split('&'); // split the name/value pairs of the url
  /* load them into an associative array*/
  for (var i = 0; i < args.length; i++) {
    var pair = args[i].split('=');
    var name = decodeURIComponent(pair[0]);
    var value = (pair.length==2)
      ? decodeURIComponent(pair[1])
      : name;
      this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
  var value = this.params[key];
  return (value != null) ? value : default_;
}

function lang_flip ()
{
  var x=document.getElementsByName("lng");
  var a=0;
  for(var z=0; z < x.length; z++) {
    if(x[z].name == 'lng') {
      a = 1;
      if(x[z].value == 'en') {
        x[z].value = 'fr-ch';
        }
      else {
        x[z].value = 'en';
        }
      }
    }

  //if(!a) document.write("<input type=hidden name='lng' value='fr-ch'/>");
  lang();
}

function lang () {
  var q     = new Querystring();
  var field = q.get("lng");
  var newq   = "";
  var newurl = "http://" + location.host.toString();
  var path   = location.pathname.toString();
  var i      = "";

  if(field == null) field = "en"; /* Default is always english*/

  if(field == "en") q.params["lng"] = "fr-ch";
  else q.params["lng"] = "en";

  if(path.substring(path.length - 1, 1) == "/") {
    if(path.length > 1)
      path = path.substring(0, path.length - 1);
    else
      path = "";
    }

  //document.write("PATH="+path+"<br>");

  for(i in q.params) { if(i.length != 0) newq = newq + i + "=" + q.params[i] + "&"; }
  //document.write("QUERY="+newq+"<br>");

  //document.write("newq.LENGTH="+newq.length+"<br>");
  if(newq.length) {
    newq = newq.substring(0, newq.length - 1);
    /*if(newurl.substring(newurl.length - 1, 1) != "/") newurl = newurl + "/";*/
    if(path != "") newurl = newurl + path;
    newurl = newurl + "?" + newq;
    }

  //document.write("URL="+newq+"<br>");
  window.open(encodeURI(newurl), '_top');
}
