// ajax request routines.

function getXMLHttpRequestObject()
{
  var A = null;
  try{
    A = new ActiveXObject("Msxml2.XMLHTTP");
    //alert('here1');
  } catch(e) {
    try {
      A = new ActiveXObject("Microsoft.XMLHTTP");
      //alert('here2')
    } catch(oc){
      A = null;
    }
  }
  if( !A && typeof XMLHttpRequest != "undefined"){
    isMozilla = 1;
    A = new XMLHttpRequest();
    //alert('here3');
  }
  return A;
} // end: getXMLHttpRequestObject

function getServerRequest(url,queryString, async, action,meta, method)
{
  var request = getXMLHttpRequestObject();
  var uri = url + ((queryString) ? "?"+queryString : "");
  method = (method && (method.toLowerCase() == 'get')) ? "GET" : "POST" ;
  request.open(method,uri,async);

  if (async && action){
    request.onreadystatechange =
      function() {
	//if ( (request.readyState == 4) && request.responseText) {
	if (request.readyState == 4){
	  //alert(request.getResponseHeader('Content-type'));
	  //alert("responseText = "+request.responseText.length);
	  if (request.getResponseHeader('Content-type') == 'text/xml'){
	    action(request.responseXML,meta);
	  } else {
	    action(request.responseText,meta);
	  } // end: if
	} // end: if
      }; // end: function
  } // end: if
  request.send(null);

  if (!async){
    if (request.readyState == 4){
      //alert(request.getResponseHeader('Date'));
      //alert("responseText = "+request.responseText.length);
      if (request.getResponseHeader('Content-type') == 'text/xml'){
	action(request.responseXML,meta);
      } else {
	action(request.responseText,meta);
      } // end: if
    } // end: if
  } // end: if
} // end: getServerRequest

function loadXMLDoc(dname)
{
  var xmlDoc;
  // code for IE
  if (window.ActiveXObject)
    {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation && document.implementation.createDocument)
    {
      xmlDoc=document.implementation.createDocument("","",null);
    }
  else
    {
      alert('Your browser cannot handle this script');
    }
  xmlDoc.async=false;
  xmlDoc.load(dname);
  return(xmlDoc);
}

function createXMLDoc()
{
  var xmlDoc;
  if (window.ActiveXObject) {
    // code for IE
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  } else if (document.implementation && document.implementation.createDocument) {
    // code for Mozilla, Firefox, Opera, etc.
    xmlDoc=document.implementation.createDocument("","",null);
  } else {
    alert('Your browser cannot handle this script');
  }
  
  xmlDoc.async=false;

  return(xmlDoc);
} // end: createXMLDoc;

// given an xml element node retrieve the firstchild from the first tag having name tagName.
function getFirstChildByTag(node, tagName)
{
  var tagNode = node.getElementsByTagName(tagName)[0];
  
  return (tagNode && tagNode.firstChild) ? unescape(tagNode.firstChild.nodeValue) : '';
  //return (tagNode.firstChild) ? decodeURIComponent(tagNode.firstChild.nodeValue) : '';
} // end: getFirstChildByTag

// removeNode:  removes all of DOM node's children, including node.
// if keepNode is set to true only node's children are deleted, not node itself.
function removeNode(node, keepNode)
{
  if (node){
    for(var i = (node.childNodes.length - 1); i >= 0; i--)
      removeNode(node.childNodes[i], false);
    if (!keepNode && node){
      if (node.parentNode){
	node.parentNode.removeChild(node);
      } // end: if
    } // end: if
  } // end: if
} // end: removeNode

// given a form object pack all the form element name=value pairs onto a string for submission via POST or GET.
function getPostData(form)
{
  var postData = "";
  for (i = 0; i < form.elements.length; i++){
    if (form.elements[i].name != ""){
      postData += (postData) ? "&" : "";
      postData += form.elements[i].name+"="+escape(form.elements[i].value);
    } // end: if
  } // end: for
  return postData;
} // end: getPostData


function loadToElement(element, url, queryString) 
{
  var async = false;
  getServerRequest(url,queryString, async, loadToElementAction, element, 'post');
} // end: loadToElement

function loadToElementAction (responseText, element)
{
  if (element){
    element.innerHTML = responseText;
    element.style.visibility = 'visible';
  } // end: if
} // end: loadToElementAction

