
// Ajax Interface by Edward Kmett and Chris Dibbs

// This is designed to retrieve executable javascript from the server 
// and run it in the current page context. This helps us bypass all of
// the XML parsing. 

// In order to use, call the following:
// ajax("http://www.example.com/my.cgi?field="+escape(field));

// which can return a snippet of executable javascript which is processed by the client
// If you would rather use a POST url:
// ajax("http://www.example.com/my.cgi","field="+escape(field));

// The mime-type returned by the server is ignored, but SHOULD be text/javascript.
// The exception is if you gzip the data before sending it to the browser, then you will
// need to set the mime-type to text/plain, due to the fact that Mozilla does not support
// gzipping javascript for some reason.

// In the event that the server returns an XML/HTML document rather than javascript
// it will be launched in a new window and shown to the end user.

/**
 * @author Chris Dibbs (cjdibbs@gmail.com)
 * @breif walks a from get the feilds marked for submition.
 * if a feild has a name defined it will be submitted if it is not blank. this just packs the args.
 * This gong here because I really don't have any where else to put this, my get moved to ajax lib.
 */
function packForm(formid, ret){
  var ret = ret || "";
  var form = document.getElementById(formid);
  for(var ii = 0; ii < form.length; ii++){
    var name = form.elements[ii].name;
    var value = form.elements[ii].value;
    if( name != '' && value != '' ){
      ret = ret + (ret.length ? '&' : '') + name + '=' + escape(value);
    }
  }
  return ret;
}

/**
 * @author Chris Dibbs
 */
function packObj( obj, ret){
  ret = ret || '';
  for( var ii in obj ){
    ret += (ret.length ? '&' : '') + escape(ii) + '=' + escape(obj[ii]);
  }
  return ret;
}
/**
 * @author Edward Kmett
 * @return a vaild http request object if the browser suports that.
 */
function get_http_request_object() { 
  var A = null;
  try { 
    A = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) { 
    try { 
      A = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) { 
      A = null;
    }
  }
  if (!A && typeof XMLHttpRequest != "undefined") { 
    try {
      netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    } catch (e) { }

    A = new XMLHttpRequest();
  }
  return A;
}

/**
 * @author Chris Dibbs
 * @author Edward Kmett
 * @param url the url to set the request to
 * @param post if you want to set post this is the post data, if it is null you send a get
 * @param callback a call back that takes the text as the argument
 * @param async [boolean] if false will wait for the request to compleate before it returns.
 */
function ajax(url,post,callback, context, override) {

  var xml_http = get_http_request_object();
  if (xml_http) { 
    xml_http.open(post ? "POST" : "GET", url, true);

    if(post){
      //Send the proper header information along with the request (grr ed!)
      xml_http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xml_http.setRequestHeader("Content-length", post.length);
      xml_http.setRequestHeader("Connection", "close");
    }
    xml_http.setRequestHeader("Accept","text/plain");
    xml_http.setRequestHeader("Accept-Encoding","gzip,deflate");

    xml_http.onreadystatechange = function( ) {
      if (xml_http.readyState==4) {
	if(callback)
	  callback.call( override ? context || window : window, xml_http.responseText);
      }
    };
    xml_http.send(post);
  }
}



/**
 * @author Chris Dibbs
 * @breif a syncous xml http request 
 */
function sjax(url,post) {

  var xml_http = get_http_request_object();
  if (xml_http) {
    xml_http.open(post ? "POST" : "GET", url, false);

    if(post){
      xml_http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xml_http.setRequestHeader("Content-length", post.length);
      xml_http.setRequestHeader("Connection", "close");
    }
    xml_http.setRequestHeader("Accept","text/plain");
    xml_http.setRequestHeader("Accept-Encoding","gzip,deflate");

    xml_http.send(post);
    return xml_http.responseText;
  }
}

/**
 * @author Chris Dibbs
 * @url url to pass to ajax
 * @post post data to send to ajax
 * @context in scope when the async javascript is evaled
 * @async async to pass to ajax
 */
function ajaj(url,post,context, override) {
  ajax(url,post,function(t) { eval(t);},context, override);
}

/**
 * @author Chris Dibbs
 * @url url to pass to ajax
 * @post post data to send to ajax
 * @context in scope when the async javascript is evaled
 * @async async to pass to ajax
 */
function ajax2id(url, post, id){
	ajax(url, post, function(t) { document.getElementById(id).innerHTML = t; });
}


/**
 * @author Benjamin Cool
 * @str string to turn into xml object
 */
function XMLObjFromText(str){

// code for IE
if (window.ActiveXObject)
  {
  var doc=new ActiveXObject("Microsoft.XMLDOM");
  doc.async="false";
  doc.loadXML(str);
  }
// code for Mozilla, Firefox, Opera, etc.
else
  {
  var parser=new DOMParser();
  var doc=parser.parseFromString(str,"text/xml");
  }

var x=doc.documentElement;

return x;

}

function sjaj(url,post){
	eval(sjax(url,post));	
}
