// Adapted from:

/**
SAL - Simple Ajax Lib. 23-Sep-2005
by Nigel Liefrink
Email: leafrink@hotmail.com
*/

var debug = false;
/**
<summary>
Browser Compatability function.
Returns the correct XMLHttpRequest 
  depending on the current browser.
</summary>
*/
function GetXmlHttp() {
  var xmlhttp = false;
  if (window.XMLHttpRequest)
  {
    xmlhttp = new XMLHttpRequest()
  }
  else if (window.ActiveXObject)// code for IE
  {
    try
    {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
    } catch (e) {
      try
      {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
      } catch (E) {
        xmlhttp=false
      }
    }
  }
  return xmlhttp;
}


/**
<summary>
Gets the response stream from the passed url, and then calls
   the callbackFuntion passing the response and the div_ids.
</summary>
<param name="url">The url to make the request
            to get the response data.</param>
<param name="callbackFunction">The function to call after 
the response has been recieved.
The response <b>must</b> always 
be the first argument to the function.</param>
<param name="params"> (optional) Any other parameters 
you want to pass to the functions.
(Note: only constants/strings/globals can be passed
       as params, most variables will be out of scope.)
</param>
<example>
    <code>
PassAjaxResponseToFunction('?getsomehtml=1', 
              'FunctionToHandleTheResponse', 
              "\'div1\',\'div2\',\'div3\'');

function FunctionToHandleTheResponse(response, d1, d2, d3){
    var data = response.split(';');
    document.getElementById(d1).innerHTML = data[0];
    document.getElementById(d2).innerHTML = data[1];
    document.getElementById(d3).innerHTML = data[2];
}
    </code>
</example>
*/
function RunAjax(url, callbackFunction, callbackFailure)
{
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = 
            function ()
            {
              if (xmlhttp && xmlhttp.readyState==4) {
                if (xmlhttp.status==200) {
                  callbackFunction(xmlhttp.responseText);
                  return;
                }
                if (callbackFailure != null)
                  callbackFailure();
              }
            }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}


/**
<summary>
Sets the innerHTML property of obj_id with the response from the passed url.
</summary>
<param name="url">The url to make the request 
to get the response data.</param>
<param name="obj_id">The object or the id of 
the object to set the innerHTML for.</param>
*/
function SetInnerHTMLFromAjaxResponse(url, obj_id, onfinish)
{
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = 
            function ()
            {
              if (xmlhttp && xmlhttp.readyState==4)
              {//we got something back..
                if (xmlhttp.status==200)
                {
                                  if(debug)
                                  {
                    alert(xmlhttp.responseText);
                  }
                  SetInnerHtml(obj_id, xmlhttp.responseText);
						 if (onfinish != null) onfinish();
                } else if(debug){
                  alert(xmlhttp.responseText);
                }
              }
            }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}

function SetInnerHtml(obj_id, html) {
                  if(typeof obj_id == 'object')
                  {
                    obj_id.innerHTML = html;
                  } else {
                    document.getElementById(obj_id).innerHTML = html;
                  }
}

function AppendInnerHtml(obj_id, html) {
                  if(typeof obj_id == 'object')
                  {
                    obj_id.innerHTML += html;
                  } else {
                    document.getElementById(obj_id).innerHTML += html;
                  }
}

function AjaxElement(obj_id, immediate_html, ajax_url, onfinish) {
	SetInnerHtml(obj_id, immediate_html);
	SetInnerHTMLFromAjaxResponse(ajax_url + "&elemid=" + obj_id, obj_id, onfinish);
}

function GTMonitor(obj_id, addremove, monitor) {
	AjaxElement(obj_id,
		addremove ? 'Adding tracker...' : 'Canceling tracker...',
		'/users/ajax_addmonitor.xpd?monitor=' + monitor + (addremove ? '' : '&action=remove') );
}

function AjaxSelect(obj_id, immediate_text, ajax_url, first_option, onfinish) {
	var s = document.getElementById(obj_id);
	
	var cursel;
	if (s.selectedIndex != -1)
		cursel = s.options[s.selectedIndex].value;
	
	s.disabled = 1;
	s.options.length = 1;
	s.options[0].text = immediate_text;
	s.options[0].value = '';

  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = 
            function ()
            {
              if (xmlhttp && xmlhttp.readyState==4)
              {//we got something back..
                if (xmlhttp.status==200)
                {
						var fo = (first_option ? 1 : 0);
						s.options.length = xmlhttp.responseXML.documentElement.childNodes.length + fo;
						if (first_option) {
							s.options[0].text = first_option;
							s.options[0].value = '';
						}
						for (var i = 0; i < xmlhttp.responseXML.documentElement.childNodes.length; i++) {
							s.options[i+fo].text = xmlhttp.responseXML.documentElement.childNodes[i].textContent;
							s.options[i+fo].value = xmlhttp.responseXML.documentElement.childNodes[i].getAttribute('value');
							s.options[i+fo].selected = xmlhttp.responseXML.documentElement.childNodes[i].getAttribute('selected');
						}
						s.disabled = 0;
						onfinish();
                }
              }
            }
    xmlhttp.open("GET",ajax_url + "&selected=" + cursel,true);
    xmlhttp.send(null);
  }
}

function makeFormQueryString(formobj) {
  var getstr = "";
  var elems = formobj.getElementsByTagName("input");
  for (i=0; i<elems.length; i++) {
    if (elems[i].type == "text") {
      getstr += "&" + escape(elems[i].name) + "=" + escape(elems[i].value);
    } else if (elems[i].type == "hidden") {
      getstr += "&" + escape(elems[i].name) + "=" + escape(elems[i].value);
    } else if (elems[i].type == "checkbox") {
      if (elems[i].checked) {
        getstr += "&" + escape(elems[i].name) + "=" + escape(elems[i].value);
      } else {
			getstr += "&" + escape(elems[i].name) + "=";
		}
	 } else if (elems[i].type == "radio") {
		if (elems[i].checked) {
		  getstr += "&" + escape(elems[i].name) + "=" + escape(elems[i].value);
		}
	}
  }
  elems = formobj.getElementsByTagName("textarea");
   for (i=0; i<elems.length; i++) {
     getstr += "&" + escape(elems[i].name) + "=" + escape(elems[i].value);
   }
  elems = formobj.getElementsByTagName("select");
  for (i=0; i<elems.length; i++) {
     getstr += "&" + escape(elems[i].name) + "=" + escape(elems[i].options[elems[i].selectedIndex].value);
  }
  return getstr;
}

function makeArrayQueryString(assocarray) {
  var getstr = "";
  for (var name in assocarray) {
    getstr += "&" + escape(name) + "=" + escape(assocarray[name]);
  }
  return getstr;
}

