function decode(string) 
{
  return unescape(string.replace(/\+/g, " "));
}

/*
** This function parses comma-separated name=value argument pairs from
** the query string of the URL. It stores the name=value pairs in 
** properties of an object and returns that object. 
*/
function getargs() {
  var args = new Object();
  var query = location.search.substring(1);   // Get query string.
  var pairs = query.split("&");               // Break at ampersand.
  for(var i = 0; i < pairs.length; i++) {
    var pos = pairs[i].indexOf('=');          // Look for "name=value".
    if (pos == -1) continue;                  // If not found, skip.
    var argname = pairs[i].substring(0,pos);  // Extract the name.
    var value = pairs[i].substring(pos+1);    // Extract the value.
    args[argname] = decode(value);            // Store as a property.
  }
  return args;                                // Return the object.
}

/*
** Various functions to set form elements once they've been displayed.
*/
function set_checkbox(element, value)
{
  if(element.value == value) {
    element.checked = true;
    return true;
  }

  return false;
}

function set_checkbox_multiple(element, values)
{
  element.checked = false;
  for(var j=0; j<values.length; j++) {
    if(element.value == values[j])
      element.checked = true;
  }
}

function set_radio(element, value) 
{ 
  for(var i=0; i<element.length; i++) {
    if(element[i].value == value) {
      element[i].checked = true;
      return true;
    }
  }

  return false;
}

function set_radio_multiple(element, values)
{
  for(var i=0; i<element.length; i++) {    
    for(var j=0; j<values.length; j++) {
      if(element[i].value == values[j])
	element[i].checked = true;
    }
  }
}

function set_select(element, value) 
{
  for(var i=0;i<element.length;i++) {
    if((element.options[i].value == value) || (element.options[i].text == value)) {
      element.options[i].selected = true;
      return true;
    }
  }

  return false;
}

function set_select_multiple(element, values)
{
  for(var k = 0; k < values.length; k++) {
    for(var j = ((element.length)-1); j >=0 ; j--) {	
      if(element.options[j].value == values[k])
	element.options[j].selected = true;
    }
  }
}

/**
 * You are asserting that boolCondition is true. Use this to ensure that your
 * functions have the required parameters set.
 *
 * @param  boolCondition True or False based on the condition your asserting
 * @param  strErrorMessage  Error to throw if condition is False
 * @throws  strErrorMessage if boolCondition is false
 * @author DPRA Cuesta, Josh Hevenor
 * @version 1.0.0
 */
function assert(boolCondition, strErrorMessage){
  if(!boolCondition)
    throw strErrorMessage;
}

/**
 * Check that all objects required for GeoPortal to function are available.
 * This approach should avoid problems with browser version updates.
 *
 * @return  TRUE iff all required objects are available
 * @author  DPRA Cuesta
 * @author  Josh Hevenor
 * @version 1.0.0
 */
function validBrowser(){
  var valid = true;
  if(!window.ActiveXObject && !document.implementation && document.implementation.createDocument)
  { // Check for XML Support
    alert("Your browser does not support the required objects for XML parsing. Please see the documentation for details.");
    valid=false;
  }
  if(!window.ActiveXObject && !window.XMLHttpRequest)
  { // Check for remote xml request support
    alert("Your browser does not support the required objects for remote xml requests. Please see the documentation for details.");
    valid = false;
  }
  if(!document.getElementById){
    alert("Your browser does not support the required objects for dynamic html. Please see the documentation for details.");
    valid = false;
  }
  return valid;
}

/**
 * Loads fileLoad into an xmldoc object
 *
 * @param   fileLoad  Path to XML file
 * @return  xmlDoc object loaded with fileLoad or null
 * @author  Josh Hevenor
 * @version 1.0.0
 * @copyright Cuesta Systems, Ontario Canada, 2007
 */
function loadXMLDoc(fileLoad)
{
  var localXMLDoc;
 	var cc = fileLoad.indexOf("\\")
	if (cc != -1)
	{
		var str = fileLoad.substr(0,cc);
		fileLoad = fileLoad.split("\\").join("\/");
		fileLoad = "file:///"+fileLoad;
	}

  // IE code
  if (window.ActiveXObject)
  {
    localXMLDoc=new ActiveXObject("Microsoft.XMLDOM");
    localXMLDoc.async=false;
    var loaded = xmlDoc.load(fileLoad);
    if(!loaded){
      alert("Failed to load: " + fileLoad);
    }
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation && document.implementation.createDocument)
  {
    localXMLDoc=document.implementation.createDocument("","",null);
    localXMLDoc.load(fileLoad);
    localXMLDoc.onload=false;
  }
  else
  {
    alert('Your browser does not meet the XML requirements of GeoPortal. Please use Internet Explorer version 5 or higher, or FireFox version 1.5 or higher.');
  }
  return localXMLDoc
}

/*
**  COMING SOON
**
** A list of functions that fill needs for code sharing.
**
*/

/*
** take all projections from xmlFile and add them as options in selBox
*/
function loadProj(xmlFile, selBox){


}

/*
var oXMLHTTP = null;
if(window.ActiveXObject) oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
if(window.XMLHttpRequest) oXMLHTTP = new XMLHttpRequest();
*/
