function getAndDo(axurl, fnWhenDone) 
{
  new XHConn().connect(axurl, 'GET', 'a=a', fnWhenDone);
} 

function XHConn()
{
  var isIE = false;

  var xmlhttp;
  
  try 
  { 
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
    isIE = true;
  }
  catch (e) 
  { 
    try 
    { 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      isIE = true;
    }
    catch (e) 
    { 
      try 
      { 
        xmlhttp = new XMLHttpRequest(); 
      }
      catch (e) 
      { 
        xmlhttp = false; 
      }
    }
  }
  if (!xmlhttp) return null;

  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (sURL == null) { sURL = window.location; }

    if (!xmlhttp) return false;

    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4) {
        fnDone(xmlhttp); }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function ajax(sURL, sMethod, sVars, fnDone)
{
//	document.getElementById('loading').innerHTML = 'LOADING';
	document.getElementById('loading').style.display = 'block';

	if (!fnDone)
		fnDone = fnWhenDone;

	var myConn = new XHConn();
	if (!myConn) 
		alert('XMLHTTP not available. Try a newer/better browser.');

	myConn.connect(sURL, sMethod, sVars, fnDone);
}


function fnWhenDone(oXML) 
{
	document.getElementById('loading').style.display = 'none';
//	document.getElementById('loading').innerHTML = '';

//	alert(oXML.responseText);

	if (document.getElementById('xmlresult'))
		document.getElementById('xmlresult').value = oXML.responseText;
		
//	alert(oXML.responseText);

	var source = oXML.responseXML.getElementsByTagName("ajaxcmdset")[0]; //root element

	for (i = 0; i < source.childNodes.length; i++)
	{
        	if (source.childNodes[i].nodeType == 1) 
		{
			var aCmd = source.childNodes[i];

			var actionType   = aCmd.getAttribute("actionType");
			var targetID     = aCmd.getAttribute("targetID");
			var actionMethod = aCmd.getAttribute("actionMethod");

			var targetElement = document.getElementById(targetID);
			
//			alert(targetID + '(' + targetElement + ') / ' + actionType + ' / ' + actionMethod);

			if (actionType == 'dd')
			{
				if (targetElement == null)
					continue;
				
				if (actionMethod == 'replace')
				{
					for (j = targetElement.length; j >= 0; j--) 
						targetElement.options[j] = null;
				}
				for (j = 0; j < aCmd.childNodes.length; j++) 
				{
	        			if (aCmd.childNodes[j].nodeType == 1) 
					{
						var aRecord = aCmd.childNodes[j];
						targetElement.options[targetElement.length] = new Option(aRecord.getElementsByTagName("text")[0].firstChild.nodeValue, aRecord.getElementsByTagName("value")[0].firstChild.nodeValue);
					}
				}
			}

			if (actionType == 'javascript')
			{
				//alert(aCmd.getElementsByTagName("code")[0].firstChild.nodeValue);
				var myfunc=createMyFunction(aCmd.getElementsByTagName("content")[0].firstChild.nodeValue);
				myfunc();
			}

			if (actionType == 'style')
			{
				if (targetElement == null)
					continue;
				
				if (actionMethod == 'disable')
					targetElement.disabled = true;

				if (actionMethod == 'enable')
					targetElement.disabled = false;

				if (actionMethod == 'style')
				{
					for (j = 0; j < aCmd.childNodes.length; j++) 
					{
	        				if (aCmd.childNodes[j].nodeType == 1) 
							targetElement.style.cssText = aCmd.childNodes[j].getAttribute('name') + ':' + aCmd.childNodes[j].getAttribute('value');
					}
				}
			}

			if (actionType == 'table')
			{
				if (targetElement == null)
					continue;
				
				targetElement = targetElement.getElementsByTagName("tbody")[0];

				var tr, td, str, std;

				for (j = 0; j < aCmd.childNodes.length; j++) 
				{
					if (aCmd.childNodes[j].nodeType == 1) 
					{
						tr = targetElement.insertRow(targetElement.rows.length);
						str = aCmd.childNodes[j];

						for (k = 0; k < str.childNodes.length; k++) 
						{
							if (str.childNodes[k].nodeType == 1) 
							{
								td = tr.insertCell(tr.cells.length);
								std = str.childNodes[k];

								var srcAtts = std.attributes;
								for (l = 0; l < srcAtts.length; l++) 
								{
									//alert(srcAtts[l].nodeName + '=' + std.getAttribute(srcAtts[l].nodeName));
									td.setAttribute(srcAtts[l].nodeName, std.getAttribute(srcAtts[l].nodeName));
								}
								

								//alert(serialize(str.childNodes[k]));
								// technically nest a cell within a cell, but ok for now
								td.innerHTML = serialize(str.childNodes[k]);

								//tr.appendChild(str.childNodes[k]);
							}
						}
					}
				}

				stripenew(aCmd.getAttribute("targetID"), '#ffffff', '#dddddd');
			}

			if (actionType == 'alert')
				alert(aCmd.getElementsByTagName("content")[0].firstChild.nodeValue);

			if (actionType == 'close')
				window.close();

			if (actionType == 'redirect')
				window.location = aCmd.getElementsByTagName("content")[0].firstChild.nodeValue;

			if (actionType == 'html')
			{
				if (targetElement == null)
					continue;
				
				var content = aCmd.getElementsByTagName("content")[0].firstChild.nodeValue;
				var existing = '';
				if (actionMethod == 'append')
				{
					existing = targetElement.innerHTML;
					targetElement.innerHTML = existing + content;
				}
				if (actionMethod == 'replace')
				{
					targetElement.innerHTML = content;

					var scripts = content.split('<script type="text/javascript">');

					for(var p=1; p < scripts.length; p++)
					{
						var myfunc=createMyFunction(scripts[p].split('</script>')[0]);
						myfunc();
					}
				}
				if (actionMethod == 'delete')
				{
					for (j = 0; j < targetElement.childNodes.length; j++) 
					{
						if (targetElement.childNodes[j].nodeType == 1) 
							targetElement.removeChild(targetElement.childNodes[j]);
					}
	
					////var targetParent = targetElement.parentNode;
					////targetParent.removeChild(targetElement);

					//targetElement.innerHTML = '';
				}
			}

			
//			document.forms[0].elements[0].type
//			hidden, text, password, select-one, select-multiple, radio, checkbox, submit, button, image, textarea

			if (actionType == 'object')
			{
//				alert(targetElement);
				
				if (targetElement == null)
					continue;
				
				for (j = 0; j < aCmd.childNodes.length; j++) 
				{
					if (aCmd.childNodes[j].nodeType == 1) 
					{
						targetElement = document.getElementById(aCmd.childNodes[j].nodeName);

						if (targetElement)
						{
							var aNodeValue = '';
							if (aCmd.childNodes[j].firstChild != null)
								aNodeValue = aCmd.childNodes[j].firstChild.nodeValue;
						
//							alert(targetElement.type);
							
							if (targetElement.type == 'checkbox')
							{
								targetElement.checked = aNodeValue == 'true';
							}
							else
								targetElement.value = aNodeValue;
						}
					}
				}
			}
		}
	}
}



function traverseDOMTree(targetDocument, currentElement, depth)
{
  if (currentElement)
  {
    var j;
    var tagName=currentElement.tagName;
    // Prints the node tagName, such as <A>, <IMG>, etc
    if (tagName)
      targetDocument.writeln("&lt;"+currentElement.tagName+"&gt;");
    else
      targetDocument.writeln(currentElement.nodeValue);
      //targetDocument.writeln("[unknown tag]");

    // Traverse the tree
    var i=0;
    var currentElementChild=currentElement.childNodes[i];
    while (currentElementChild)
    {
      // Formatting code (indent the tree so it looks nice on the screen)
      targetDocument.write("<BR>\n");
      for (j=0; j<depth; j++)
      {
        // &#166 is just a vertical line
        targetDocument.write("&nbsp;&nbsp;&#166");
      }								
      targetDocument.writeln("<BR>");
      for (j=0; j<depth; j++)
      {
        targetDocument.write("&nbsp;&nbsp;&#166");
      }					
      if (tagName)
        targetDocument.write("--");

      // Recursively traverse the tree structure of the child node
      traverseDOMTree(targetDocument, currentElementChild, depth+1);
      i++;
      currentElementChild=currentElement.childNodes[i];
    }
    // The remaining code is mostly for formatting the tree
    targetDocument.writeln("<BR>");
    for (j=0; j<depth-1; j++)
    {
      targetDocument.write("&nbsp;&nbsp;&#166");
    }			
    targetDocument.writeln("&nbsp;&nbsp;");
    if (tagName)
      targetDocument.writeln("&lt;/"+tagName+"&gt;");
  }
}

function printDOMTree(domElement, destinationWindow)
{
  // Use destination window to print the tree.  If destinationWIndow is
  //   not specified, create a new window and print the tree into that window
  var outputWindow=destinationWindow;
  if (!outputWindow)
    outputWindow=window.open();

  // make a valid html page
  outputWindow.document.open("text/html", "replace");
  outputWindow.document.write("<HTML><HEAD><TITLE>DOM</TITLE></HEAD><BODY>\n");
  outputWindow.document.write("<CODE>\n");
  traverseDOMTree(outputWindow.document, domElement, 1);
  outputWindow.document.write("</CODE>\n");
  outputWindow.document.write("</BODY></HTML>\n");
  
  // Here we must close the document object, otherwise Mozilla browsers 
  //   might keep showing "loading in progress" state.
  outputWindow.document.close();
}


function serialize(dom) {
    var xml = dom.xml;
    if (xml == undefined) {
        try {
            var serializer = new XMLSerializer();
            xml = serializer.serializeToString(dom);
            delete serializer;
        } catch (error) {
            if (debug)
                alert("DOM serialization is not supported.");
        }
    }
    return xml;
}

function stripenew(id) 
{
	var even = false;

	var evenColor = arguments[1] ? arguments[1] : "#ffffff";
	var oddColor = arguments[2] ? arguments[2] : "#dddddd";
  
	var table = document.getElementById(id);

	if (! table) { return; }

	var tbodies = table.getElementsByTagName("tbody");

	for (var h = 0; h < tbodies.length; h++) 
	{
		var trs = tbodies[h].getElementsByTagName("tr");

		for (var i = 0; i < trs.length; i++) 
		{
			trs[i].style.backgroundColor = even ? evenColor : oddColor;

			//var tds = trs[i].getElementsByTagName("td");

			//for (var j = 0; j < tds.length; j++) 
				//tds[j].style.backgroundColor = even ? evenColor : oddColor;

			even = ! even;
		}
	}
}

function createMyFunction(myOperator)
{
  return new Function(myOperator);
}

function getWindowWidth() 
{
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	  
	  return myWidth;
	}
