///////////////////////
// General utilities //
///////////////////////

//------------------------------------------------------------- pageTitleSet
function pageTitleSet(topic){
  var div = document.getElementById("PageTitle");
  div.innerHTML = topic["name"];
}

///////////////////////////////////////////////////////////////
// Utility to get arguments from window.location querystring //
///////////////////////////////////////////////////////////////
var WINDOW_ARGS = {};  // map of name:value in the query string
var q = window.location.search;
if(q && q.length > 0){
  q = q.substring(1);
  var vars = q.split("&");
  for(var i=0, len=vars.length ; i<len; i++){
    var index = vars[i].indexOf("=");
    if(index != -1){
      var name = vars[i].substring(0, index);
      var value = vars[i].substring(index+1);
      if (! (name in WINDOW_ARGS))
          WINDOW_ARGS[name] = [];
      // decodeURIComponent doesn't convert "+" to space
      value_esc = value.replace(/\+/g," ");
      WINDOW_ARGS[name].push(decodeURIComponent(value_esc));
    }
  }
}

//------------------------------------------------------------- arg
// @param multiple:Boolean - whether or not to return an array of all
//                           arguments, or return a single argument
function arg(name, multiple) {
  if (name in WINDOW_ARGS)
    if (multiple)
      return WINDOW_ARGS[name];
    else
      return WINDOW_ARGS[name][0];
  else
    if (multiple)
      return [];
    else
      return null;
};

//------------------------------------------------------------- readCookie
// returns the value of a cookie given the cookie name.
function readCookie(name){
  var cookieValue = "";
  name += "=";
  if(document.cookie.length > 0){ 
    var offset = document.cookie.indexOf(name);
    if(offset != -1){ 
      offset += name.length;
      var end = document.cookie.indexOf(";", offset);
      if(end == -1) end = document.cookie.length;
      cookieValue = document.cookie.substring(offset, end);
    }
  }
  return cookieValue;
}

//------------------------------------------------------------- showMembers
// this is a debug helper - mark
function showMembers(o){
  var output = "Object:";
  for(var item in o){
    if(o!=Object.prototype && typeof Object.prototype[item] != "undefined") continue;
    var value = (typeof o[item] != "function") ? o[item]:"[function]";
    output += "\n  "+item+": "+value;
  }
  return output;
}

//------------------------------------------------------------- sprintf
// sprintf("%s foo %s", "hello", "world") == "hello foo world"
// @return String
function sprintf(str) {  

  
  // this is state used in the replacement closure - starts with the
  // first argument after 'str' and advances through all arguments
  var argIndex = 1;
  var localArgs = arguments;
  
  // this gets called once for each match
  function replace() {
      return localArgs[argIndex++];
  }
  
  var res = str.replace(/%s/g, replace)
  
  return (res);
}

//----------------------------------------------------- _idGenerator
// generates an ID useful for checkbox/radiobutton labelling to enable click on labels to work.
function _idGenerator(){
  var rand1 = Math.round(Math.random() * 1000);
  var rand2 = Math.round(Math.random() * 100);
  return rand1*rand2;
}

//------------------------------------------------------------- createElementWithName
// This fixes IE bug with ÒSetting the ÒnameÓ attribute in Internet Explorer
// See original blog and particular comments at end where this solution was derived from
//   http://www.thunderguy.com/semicolon/2005/05/23/setting-the-name-attribute-in-internet-explorer/
// Since we add DOM events, innerHTML's problems with using innerHTML necessitated this approach
// See also problem with read/write-once with type for input elements - hence createTypedElement
//   http://alt-tag.com/blog/archives/2006/02/ie-dom-bugs/
function createNamedTypedElement(){};

try {
  var el=document.createElement( '<div name="foo">' );
  if( 'div'!=el.tagName.toLowerCase() ||
      'foo'!=el.name ){
    throw 'create element error';
  }
  createNamedTypedElement = function( tag, nameValue, typeValue ){
    var nameString = "";
    if (typeof nameValue != "undefined" && nameValue)
      nameString = 'name="' + nameValue + '"';
    var typeString = "";
    if (typeof typeValue != "undefined" && typeValue)
      typeString = 'type="' + typeValue + '"';
    return document.createElement( '<'+tag + " " +nameString + " " + typeString+'></'+tag+'>' );
  }
}catch( e ){
  createNamedTypedElement = function( tag, nameValue, typeValue ){
    var el = document.createElement( tag );
    if (typeof nameValue != "undefined" && nameValue)
      el.setAttribute('name', nameValue);
    if (typeof typeValue != "undefined" && typeValue)
      el.setAttribute('type', typeValue);
    return el;
  }
}
var isFirefox = false;
var isIE = false;
var isIE6 = false;
var isIE7 = false;
var isSafari = false;
var userAgent = navigator.userAgent.toLowerCase();
if (/firefox\/(\d+(?:\.\d+))/.test(userAgent)) { 
  isFirefox = true;
  bv = Number(RegExp.$1);
}
else if (/safari\/(\d+(?:\.\d+))/.test(userAgent)) {  
  isSafari = true;
  bv = Number(RegExp.$1);
}
else if (/msie (\d+(?:\.\d+))/.test(userAgent)) { 
  isIE = true;
  bv = Number(RegExp.$1);
  isIE6 = bv >= 6 && bv < 7;
  isIE7 = bv >= 7;
}
