


// COPYRIGHT DENNY WONG PUI CHUNG 2009

// Global Variables
// To troubleshoot IE forgetting object properties
var xmlconnect_layoutObj_ie_responseText = "";



// ajax_XML_Connect object
// Usage:
// var varname = new ajax_XML_Connect(String xml_path, function success_fnc, function failure_fnc);
//
// What it does:
// When object is created, automatically sends XMLHTTPREQUEST to obtain XML file at xml_path.
// Does not wait for server to respond, but instead finish function.
// Upon server feedback, check result if success,
// if YES, run the function at success_fnc with context to the ajax_XML_Connect object,
// if NO, run the function at failure_fnc with context to the ajax_XML_Connect object.
// 
// PROPERTIES:
// DOM				Contains the XMLHttpRequest object.
// 		Note that, these functions are called when "this" refers to the ajax_XML_Connect object.
// success_fnc		Function to call upon success.
// failure_fnc		Function to call upon failure.
// responseXML		Parsed XML object, only exists if DOM returns successful results
function ajax_XML_Connect(xml_path, success_fnc, failure_fnc)
{
	// Create XMLHTTP object. Legacy support for IE5 and 6 using Microsoft.XMLHTTP object.					
	
	this.getDOM = function ()
						{
							this.is_legacy_browser = (typeof window.XMLHttpRequest == "undefined");
							//return !this.is_legacy_browser ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLDOM");
							return !this.is_legacy_browser ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
						}
						
	var objDOM = this.getDOM();
	var objThis = this;
	
	this.responseText = "";
	this.responseXML = "";
	
	//TO ENABLE LEGACY BROWSER SUPPORT THE FOLLOWING ORIGINAL DESIGN WAS DISABLED
	//if (typeof objDOM.constructor == "function")
	//{	
		//Create prototype values ONLY if its a non-legacy browser
		//IE6 do not recongize objDOM.constructor.
	//	objDOM.constructor.prototype.success_fnc = function () {};
	//	objDOM.constructor.prototype.failure_fnc = function () {};
		
	//	objDOM.success_fnc = success_fnc;
	//	objDOM.failure_fnc = failure_fnc;
	//} else {
		this.success_fnc = success_fnc;
		this.failure_fnc = failure_fnc;
	//}
	
	objDOM.onreadystatechange = function ()
										{
											switch(objDOM.readyState)
											{
												case 0: //UNINITIALIZED
												case 1: //LOADING
												case 2: //LOADED
												case 3: //INTERACTIVE
													return null;
													break;
												case 4: //SUCCESS
													if (!objThis.is_legacy_browser)
													{
														if (objDOM.status == 200)
														{
															objThis.responseXML = objDOM.responseXML;
															objThis.responseText = objDOM.responseText;

															return objThis.success_fnc.call(objThis);
														} else {
															return objThis.failure_fnc.call(objThis);
														}
													} else {
														objThis.responseXML = objDOM;
														objThis.responseText = objDOM.responseText;
														xmlconnect_layoutObj_ie_responseText = objDOM.responseText;
														return objThis.success_fnc.call(objThis);
													}
													
													//TO ENABLE LEGACY BROWSER SUPPORT THE FOLLOWING ORIGINAL DESIGN WAS DISABLED
													//return (objDOM.status == 200) ? objDOM.success_fnc.call(objDOM) : objDOM.failure_fnc.call(objDOM);
											}
										}
	
	//if (!this.is_legacy_browser)
	//{
		objDOM.open("GET", xml_path, true);
		objDOM.send("");
	//} else {
	//	objDOM.async=true;
	//	objDOM.load(xml_path);
	//}
		
	
	this.DOM = objDOM;
}


// ajax_PageManager object
var ajax_PageManager = function () {
					this.PageValue_path = "";
					this.PageFrame_path = "";
					this.PageValue = new function () {};
					this.PageFrame = new function () {};
					this.PageValueXMLHnd = new function () {};
					this.PageFrameXMLHnd = new function () {};
					
					this.Connect = function (PageType, xml_path, on_load, on_fail)
									{
										xml_path += (xml_path.search(/\?(.*?)=/) != -1) ? "&" : "?";
										
										xml_path += "forcerefresh="+Math.floor(Math.random() * 32767);
											
										this["Page"+PageType+"XMLHnd"] = new ajax_XML_Connect (xml_path, on_load, on_fail);
									}
					}
