// 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, may get moved to ajax lib.
 */
function packForm(formid){
  var 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 Edward Kmett
 * @return a valid 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") { 
    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 responsetext as the argument
 */
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){
	utf8(post);
	encodeURI(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-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    xml_http.setRequestHeader("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
    xml_http.setRequestHeader("Accept-Encoding","gzip,deflate");

    xml_http.onreadystatechange = function( ) {
      if (xml_http.readyState==4) {
	   if(callback)
		callback(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){
	utf8(post);
      xml_http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
      xml_http.setRequestHeader("Content-length", post.length);
      xml_http.setRequestHeader("Connection", "close");
    }
    xml_http.setRequestHeader("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
    xml_http.setRequestHeader("Accept-Charset","utf-8");
    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
 */
function ajaj(url,post,context, override) {
  ajax(url,post,function(t) { 
	if (t.charAt(0) != "<") { eval(t);}
	else{	// may not work in IE
		var win = window.open("","ajax-failure","width=300,height=400");
		win.document.open();
		win.document.write(t);
		win.document.close();
		win.focus();
	}},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; });
}


function utf8(post) {
	if (post=="" || post == null){ return ""; }
	post = post.toString();
	var buf = "";
	for (var i=0;i<post.length;i++) {
		var char=post.charCodeAt(i);
		var bs = [];				
		if (char>0x10000) {
			bs[0] = 0xF0 | ((char & 0x1C0000) >>> 18);
			bs[1] = 0x80 | ((char & 0x3F000) >>> 12);
			bs[2] = 0x80 | ((char & 0xFC0) >>> 6);
			bs[3] = 0x80 | (char & 0x3F);
		} else if (char>0x800) {
			bs[0] = 0xE0 | ((char & 0xF000) >>> 12);
			bs[1] = 0x80 | ((char & 0xFC0) >>> 6);
			bs[2] = 0x80 | (char & 0x3F);
		} else  if (char>0x80) {
			bs[0] = 0xC0 | ((char & 0x7C0) >>> 6);
			bs[1] = 0x80 | (char & 0x3F);
		}
		else{
			bs[0] = char;
		}
		
		if (char == 10 || char == 13){ buf += '%0'+char.toString(16); }//added to correct problem with hard returns
		else if (bs.length == 1 && char>=48 && char<127 && char!=92){buf += post.charAt(i);}
		else{ for(var j=0;j<bs.length;j++){ buf+='%'+bs[j].toString(16);} }
	}/**/
	return buf;
}

