// - - - - - - - - - - - -
// BROWSER SNIFFING
// - - - - - - - - - - - -

var agt=navigator.userAgent.toLowerCase();

// *** BROWSER VERSION ***
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

function turnOff (id) {setProp(id,"display","none");}

function turnOn (id) {
	if (typeof(id)=="string"){
		el = document.getElementById(id);
	}else{
		el = id;
	}

	if(el.tagName=="TR"){
		el.style.display = is_ie ? "block" : "table-row";
	}else if(el.tagName=="TH"){
		el.style.display = is_ie ? "block" : "table-caption";
	}else if(el.tagName=="TD"){
		el.style.display = is_ie ? "block" : "table-cell";
	}else{
		setProp(id,"display","block");
	}
}

function turnOnInline(tId){
	el = document.getElementById(tId);
	el.style.display="inline";
}

function clickClear(field, emptytext) {
	if (field.value == emptytext) { field.value = ''; return;}
	else if (field.value == '' ) { field.value = emptytext; return; }
}

function setProp (id,prop,val) {
	if (typeof(id)=="string")
		el = document.getElementById(id);
	else
		el = id;
	eval("el.style."+prop+" = '"+val+"';");
}
function toggleRows (lnk, state) {
	//var catrow = getGrandParent(lnk);
	var catrow = document.getElementById(lnk);
	var catrowClass = catrow.className;
	var sectionID = catrow.id;
	var allRows = getElementsByClassName(getParent(catrow),"tr",sectionID);

	if (state=="closed") {
		catrow.className = "catrow closed groupNormal";
		for (i=0;i<allRows.length;i++) {
			turnOff(allRows[i]);
		}
	} else if (state=="open") {
		catrow.className = "catrow open groupNormal";
		for (i=0;i<allRows.length;i++) {
			allRows[i].style.display = is_ie ? "block" : "table-row";
		}
	}
	return false;
}

// - - - - - - - - - - - -
// Pop-Up Windows
// - - - - - - - - - - - -

function popup(pageName) {
mywindow = window.open (pageName, "","location=0,status=1,scrollbars=1,toolbar=0,width=600,height=400");
}

function getGrandParent(j) {
	p = j.parentNode;
	gp = p.parentNode;
	return gp;
}

function getParent(j) {
	var n = null;
	if (j.parentNode) {
		n = j.parentNode;
	}
	else if (j.parentElement) {
		n = j.parentElement;
	}
	if (n)
		return n;
	else
		return null;
}

function setProp (id,prop,val) {
	if (typeof(id)=="string")
		el = $get(id);
	else
		el = id;
	eval("el.style."+prop+" = '"+val+"';");
}

function addToFavorites() {
	var url = document.URL;
	var title = document.title;
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}

// - - - - - - - - - - - -
// ESSENTIAL JAVASCRIPT
// Copyright Robert Nyman, http://www.robertnyman.com
// Free to use if this text is included
// - - - - - - - - - - - -

function $get(strId){
	return document.getElementById(strId);
}

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function addClassName(oElm, strClassName){
	var strCurrentClass = oElm.className;
	if(!new RegExp(strClassName, "i").test(strCurrentClass)){
		oElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
	}
}

function removeClassName(oElm, strClassName){
	var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
	oElm.className = oElm.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
}

function expandAll($table){
	var tRows = getElementsByClassName($table, "tr", "off");
	for (i=0;i<tRows.length;i++) {
		tRows[i].style.display = is_ie ? "block" : "table-row";
	}
	var tRows = getElementsByClassName($table, "tr", "closed");
	for (i=0;i<tRows.length;i++) {
		tRows[i].className = tRows[i].className.replace('closed','open');
	}
}
function collapseAll($table){
	var tRows = getElementsByClassName($table, "tr", "subTask");
	for (i=0;i<tRows.length;i++) {
		turnOff(tRows[i]);
	}
	var tRows = getElementsByClassName($table, "tr", "open");
	for (i=0;i<tRows.length;i++) {
		tRows[i].className = tRows[i].className.replace('open','closed');
	}
}

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}

// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

// process the url
function process(tURL)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate the asynchronous HTTP request
      xmlHttp.open("GET", tURL, true);
      xmlHttp.onreadystatechange = handleRequestStateChange;
	  //xmlHttp.overrideMimeType("text/xml");
      xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChange()
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        // do something with the response from the server
		handleServerResponse();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" +
            xmlHttp.statusText);
    }
  }
}

// handles the response received from the server
function handleServerResponse()
{

  var xmlResponse = xmlHttp.responseXML;


  // catching potential errors with IE and Opera
  if (!xmlResponse || !xmlResponse.documentElement){
    throw("Invalid XML structure A:\n" + xmlHttp.responseText);
  }

  // catching potential errors with Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror"){
    throw("Invalid XML structure B:\n" + xmlHttp.responseText);
  }

  // getting the root element (the document element)
  xmlRoot = xmlResponse.documentElement;
  // testing that we received the XML document we expect
  if (rootNodeName != "response"){
	throw ("Not response root");
  }
  if (rootNodeName != "response" || !xmlRoot.firstChild){
    throw("Invalid XML structure C:\n" + xmlHttp.responseText);
  }

  processResult(xmlResponse);

}

