
/*  USAGE:
	var xreq = new xmlhttp();
	xreq.open("POST", "http://vine.bombur/old/rate.asp?checkip=" + IP, false);
	xreq.send("");
	if (xreq.responseText == "new") {
		printstr = "this is new";
	}

	document.getElementById("ratingbar").innerHTML = printstr;

*/

var STATE_UNINITIALISED = 0;
var STATE_LOADING       = 1;
var STATE_LOADED        = 2;
var STATE_INTERACTIVE   = 3;
var STATE_COMPLETED     = 4;

function xmlhttp() {

	try {
		// Mozilla / Safari
		this.xhttp = new XMLHttpRequest();
		this.myobject = "XMLHttpRequest";
		this.success = true;
	} 
	catch (e) {
		// Explorer
		var ie_versions = new Array(
			'Microsoft.XMLHTTP',
			'MSXML2.XMLHTTP.5.0',
			'MSXML2.XMLHTTP.4.0',
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP'
			);
		this.success = false;
		for (var i=0;i < ie_versions.length && !this.success; i++) {
			try {
				this.xhttp = new ActiveXObject(ie_versions[i]);
				this.myobject = ie_versions[i];
				this.success = true;
			} catch (e) {

			}
		}
	}

	if (this.success) {
		this.waiting_command = '';
		this.complete_command = '';
		return this.xhttp;
	}
	else {
		return false;
	}

}
	
/*  ===========================================================================
ajax_call();
-------------------------------------------------------------------------------
Makes an ajax call with a given url and optional complete and waiting 
commands that are eval'd.  Deprecated for ajax_exec().
===========================================================================  */

function ajax_call(url /*, complete_command, waiting_command, method, async, uname, pswd, send_vals, dbg */) {

	var xreq = new xmlhttp();
	var retval = '';

	// get the optional commands
	with (ajax_call) {
		var complete_command = (arguments.length > 1) ? arguments[1] : '';
		var waiting_command = (arguments.length > 2) ? arguments[2] : '';
		var method = (arguments.length > 3) ? arguments[3] : 'GET';
		var async = (arguments.length > 4) ? arguments[4] : true;
		var uname = (arguments.length > 5) ? arguments[5] : '';
		var pswd = (arguments.length > 6) ? arguments[6] : '';
		var send_vals = (arguments.length > 7) ? arguments[7] : '';
		if (send_vals == '' && method == 'GET') send_vals = null;
		var dbg = (arguments.length > 8) ? arguments[8] : false;
	}

	// run it
	xreq.onreadystatechange = function() {
		if (xreq.readyState != STATE_COMPLETED) {
			eval(waiting_command);
		}
		if (xreq.readyState == STATE_COMPLETED) {
			if (xreq.status == '200') {
				var response_text = xreq.responseText;
				(typeof complete_command == 'function') ? complete_command(response_text) : eval(complete_command);
			}
			else {
				if (dbg) alert('There was a problem with the page requested:\r\n\r\n' + url + ': ' + xreq.status + ': ' + xreq.statusText);
			}
		}
	};
	xreq.open(method, url, async, uname, pswd);
	xreq.send(send_vals);
	retval = xreq.responseText;
	return(retval);
}

/*  ===========================================================================
ajax_exec();
-------------------------------------------------------------------------------
Executes a url with various options and callback functions.
===========================================================================  */

function ajax_exec(url /*, send_vals, completed_callback, loading_callback, method, uname, pswd, async, dbg */)
{
	var xreq = new xmlhttp();
	var retval = '';

	// get the optional parameters
	with (ajax_exec) {
		var send_vals = (arguments.length > 1) ? arguments[1] : '';
		var completed_callback = (arguments.length > 2) ? arguments[2] : '';
		var loading_callback = (arguments.length > 3) ? arguments[3] : '';
		var method = (arguments.length > 4) ? arguments[4] : 'POST';
		var uname = (arguments.length > 5) ? arguments[5] : '';
		var pswd = (arguments.length > 6) ? arguments[6] : '';
		var async = (arguments.length > 7) ? arguments[7] : true;
		var dbg = (arguments.length > 8) ? arguments[8] : false;

		// GET passes null not '' at .send
		if (send_vals == '' && method == 'GET') send_vals = null;
	}

	// state-change functions
	xreq.onreadystatechange = function() {
		switch (xreq.readyState) {
			case STATE_UNINITIALISED:
				break;
			case STATE_LOADING:
				if (typeof loading_callback == 'function') loading_callback();
				break;
			case STATE_LOADED:
				break;
			case STATE_INTERACTIVE:
				break;
			case STATE_COMPLETED:
				if (xreq.status == '200')
				{
					if (typeof completed_callback == 'function') completed_callback(xreq.responseText);
				}
				else
				{
					if (dbg) alert('There was a problem with the page requested:\r\n\r\n' + url + ': ' + xreq.status + ': ' + xreq.statusText);
				}
				break;
		}
	};

	// open it
	xreq.open(method, url, async, uname, pswd);

	// set your headers
	if (method == 'POST')
	{
		xreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	}

	// send it
	xreq.send(send_vals);

	// return something if it's not async
	if (async == false)
	{
		retval = xreq.responseText;
		return(retval);
	}
}

/*  ===========================================================================
Ajax();
-------------------------------------------------------------------------------

	Notes:
	- HTTP querying wrapper object

	Usage:

	var reload = new Ajax();
	reload.url = "http://portraits.olanmills.com/ws/import_sitting.asp";
	reload.post = "data=0&poses=1&images=1&orderno=" + orderno;
	reload.send();
	delete reload;

===========================================================================  */


function Ajax()
{ 
	// constants
	var STATE_UNINITIALISED = 0;
	var STATE_LOADING       = 1;
	var STATE_LOADED        = 2;
	var STATE_INTERACTIVE   = 3;
	var STATE_COMPLETED     = 4;
	var parent_obj = this;

	// properties
	this.url = "";
	this.method = "POST";
	this.post = "";
	this.username = "";
	this.password = "";
	this.async = true;
	this.response = "";
	this.status = "";
	this.waiting = "";
	this.complete = "";
	this.change = "";
	this.query = "";
	this.dbg = false;

	// methods
	this.send = function()
	{
		/*	Post the url and send the values  */
		parent_obj.query = new xmlhttp();
		this.method = new String(this.method).toUpperCase(); // make sure it's capitalized

		parent_obj.query.onreadystatechange = function()
		{
			// on change function
			(typeof parent_obj.change == 'function') ? parent_obj.change() : eval(parent_obj.change);

			if (parent_obj.query.readyState != STATE_COMPLETED)
			{
				(typeof parent_obj.waiting == 'function') ? parent_obj.waiting() : eval(parent_obj.waiting);
			}

			if (parent_obj.query.readyState == STATE_COMPLETED)
			{
				if (parent_obj.dbg) alert("headers: \r\n" + parent_obj.query.getAllResponseHeaders());
				parent_obj.status = parent_obj.query.status; if (parent_obj.dbg) alert("status: " + parent_obj.status);
				parent_obj.response = parent_obj.query.responseText; if (parent_obj.dbg) alert("response: " + parent_obj.response);

				if (parent_obj.status == '200')
				{
					(typeof parent_obj.complete == 'function') ? parent_obj.complete(parent_obj.response) : eval(parent_obj.complete);
				}
				else
				{
					(typeof parent_obj.failure == 'function') ? parent_obj.failure() : eval(parent_obj.failure);
				}
			}
		}

		// spit out stuff for debug
		if (this.dbg)
		{
			alert("method: " + this.method + "\r\n" + 
				"url: " + this.url + "\r\n" + 
				"post: " + this.post + "\r\n" + 
				"async: " + this.async + "\r\n" + 
				"username: " + this.username + "\r\n" + 
				"password: " + this.password);
		}

		parent_obj.query.open(this.method, this.url, this.async, this.username, this.password);

		if (this.method == "POST")
		{
			parent_obj.query.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}

		parent_obj.query.send(this.post);

		// if it's async, give back the status and content
		if (!this.async)
		{
			parent_obj.status = parent_obj.query.status; if (parent_obj.dbg) alert("status: " + parent_obj.status);
			parent_obj.response = parent_obj.query.responseText; if (parent_obj.dbg) alert("response: " + parent_obj.response);
		}

		/*	Kill the object  */
		//parent_obj.query = "";
	}	

}


/*  ===========================================================================
load_to_div(div, filename)
-------------------------------------------------------------------------------
Loads htm selections to div
===========================================================================  */


function load_to_div(div, filename)
{

	var completed_callback = function(response_text)
	{
		// clear it first, then load the div with the new html
		document.getElementById(div).innerHTML = response_text;
		document.getElementById(div).scrollTop = 0;
	}

	// force the browser to pull the most recent version
	var refresh_number = new String((Math.round((Math.random()*99)+1)));

	ajax_exec(filename + '?' + refresh_number, '', completed_callback, '', 'GET');
}
