/*
	"BAJAH" - Basic Ajax JSON AHAH (Asynchronous HTML and HTTP)

	Copyright 2009 Cloud Mill
*/

///////////////////////////////////////////////////////////////////////////////////////
///// Global variables

var BajahVersion = "002b";				// do not edit
var BajahReq = getXmlHttpRequestObject();		// Ajax request object; no need to deal with this

///// Outputs from Ajax Request

var BajahResponse;					// Text response from the Ajax request
var BajahObj;						// Object created by evaluating the JSON; must be turned on in the settings or turned on in the function call

///// Settings / Constants

var BajahSetting_UseSplitter = false;		// put the splitter text at the bottom of your page to remove any system generated footers (like from CF debugging)
var BajahSetting_SplitterText = "@@@@@@@@@@";
var BajahSetting_GetBajahObjByDefault = false;
var BajahErrorMessage = "Error";


///////////////////////////////////////////////////////////////////////////////////////
/////  Bajah
///////////////////////	pageurl	= webpage URL of content to retrieve via ajax (required)
//////////////////////	params 	= (optional) params of the call
/////////////////////	outputid 	= DOM id of the box that you would like to output the ajax response to (optional)
////////////////////		fn		= text of the function to run after ajax retrieval is done (e.g. "alert('got the page!')") (optional)
///////////////////		getBajahobj 	= true/false, do you want to evaluate the ajax data into an object? useful for getting JSON data

function BajahGet (pageurl, outputid, fn, getBajahobj) {
	Bajah (pageurl, "GET", "", outputid, fn, getBajahobj);
}

function BajahPost (pageurl, params, outputid, fn, getBajahobj) {
	Bajah (pageurl, "POST", params, outputid, fn, getBajahobj);
}

function Bajah (pageurl, method, params, outputid, fn, getBajahobj)
{
	if (BajahReq.readyState == 4 || BajahReq.readyState == 0) {
		BajahReq.open(method, (method == "GET" && params) ? pageurl + "?" + params : pageurl, true);
		BajahReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		if (method == "POST") {
			BajahReq.setRequestHeader("Content-length", params.length);
			BajahReq.setRequestHeader("Connection", "close");
		}
		BajahReq.onreadystatechange = function () {
			if (BajahReq.readyState == 4) {
				if (BajahReq.status == 200) { 
					BajahResponse = BajahReq.responseText;
					if (BajahSetting_UseSplitter)
					{					
						var splitter = BajahSetting_SplitterText;			
						var r = BajahResponse + "" + splitter;
						BajahResponse = r.substring(0, r.indexOf(splitter));
					}
					if (outputid) if (document.getElementById(outputid)) document.getElementById(outputid).innerHTML = BajahResponse;
					if (BajahSetting_GetBajahObjByDefault || getBajahobj) BajahObj = eval("(" + BajahResponse + ")");			
					if (fn) eval(fn);
				} else { // status not 200 means fetched the wrong page or network error
					if (outputid) if (document.getElementById(outputid)) document.getElementById(outputid).innerHTML = BajahErrorMessage;
					if (fn) eval(fn);
				}
			}
		}
		BajahReq.send((method == "POST") ? params : null);
	}

}


/////////////////////////// Standard Ajax Object ////////////////////////////////////
//Gets the browser-specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest(); //Not IE
		} else if(window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP"); //IE
		} else {
			alert("Error: Your browser doesn't support the XmlHttpRequest object.\nYou should upgrade to the latest version of Firefox or Chrome.");
		}
}