/*
 * AJAX handler
 *  
 * Based on: Ajax and PHP: Building Responsive Web Applications
 * Christian Darie, Bogdan Brinzarea, Filip Chereches, Mihai Bucica 
 */


var PM_ajax = {
	XmlHttp : false,
	handleServerResponseFunction : false,
	ajaxDivId : false,
	ajaxWaitText : "...",
	sendValues : null,

	createXmlHttpRequestObject : function () {
		var xmlHttp;
		try {
			xmlHttp = new XMLHttpRequest();
		} catch(e) {
			var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
																			"MSXML2.XMLHTTP.5.0",
																			"MSXML2.XMLHTTP.4.0",
																			"MSXML2.XMLHTTP.3.0",
																			"MSXML2.XMLHTTP",
																			"Microsoft.XMLHTTP");
			for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
				try { 
					xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
				} catch (e) {
				}
			}
		}
		if (!xmlHttp)
			alert("Error creating the XMLHttpRequest object.");
		else 
			return xmlHttp;
	} ,

	openHttpRequest : function ( aMethod, aScript, aStatus, handleServerResponse ) {
		if (this.XmlHttp) {
			// try to connect to the server
			// open the script to handle
			try {
				this.handleServerResponseFunction = handleServerResponse;
				this.XmlHttp.open( aMethod, aScript + "&" + "AjaxProcess", aStatus);
				if (aMethod == "POST") {
					this.XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					this.XmlHttp.setRequestHeader("Content-length", this.sendValues);
					this.XmlHttp.setRequestHeader("Connection", "close");
				}
				this.XmlHttp.onreadystatechange = this.handleRequestStateChange ;
				this.XmlHttp.send( this.sendValues );
			} catch (e) {
				alert ( "Can't connect to server: " + aScript + "\n" + e.toString() );
			}
			return true;
		}
	} ,

	handleRequestStateChange : function () {
		// this does not work here... 
		oThis = PM_ajax;
		if (oThis.XmlHttp.readyState == 1 ) {
			if ( document.getElementById ( oThis.ajaxDivId ) ) {
				document.getElementById ( oThis.ajaxDivId ).innerHTML = '<div class="AjaxWait">' + oThis.ajaxWaitText + '</div>';
			}
		}
		if (oThis.XmlHttp.readyState == 4) {
			if (oThis.XmlHttp.status == 200) {
				try {
					// open the function that handles successful server response
					eval ( oThis.handleServerResponseFunction );
				} catch(e) {
					alert("Error reading the response: " + e.toString());
				}
			} else {
				alert("There was a problem retrieving the data:\n" + XmlHttp.statusText);
			}
		} 
	} ,

	GETSubmit : function ( Script, DivId ) {
		this.ajaxDivId = DivId;
		this.sendValues = null; 
		return this.openHttpRequest ( "GET", Script, true, "oThis.HandleServerResponse()" );
	} ,
	
	POSTSubmit : function ( Script, DivId, FormId ) {
		this.ajaxDivId = DivId;
		this.sendValues = this.getFormValuesAsString ( FormId );
		return this.openHttpRequest ( "POST", Script, true, "oThis.HandleServerResponse()" );
	} ,

	
	HandleServerResponse : function () {
		var Div = document.getElementById ( this.ajaxDivId );
		var response = this.XmlHttp.responseText;
		Div.innerHTML = response;
	} ,
	
	getFormValuesAsString : function ( FormId ) {
		var f = getE ( FormId );
		var values = new Array;
		for( var i = 0; i < f.elements.length; i++ ) {
			if ( f.elements[i].name.length > 0 )
				values[i] = encodeURIComponent (f.elements[i].name) + '=' + encodeURIComponent(f.elements[i].value) ;
		}
		values = values.join ('&');
		return (values);
	} ,
	
	activateAjax : function () {
		this.XmlHttp = this.createXmlHttpRequestObject();
	}

}

PM_ajax.activateAjax ();
