﻿/*
	AJAX based Prototype.
	
	CREATED BY:	WAHAB QAMAR
	CREATED ON: 28th March 2007	
*/
<!--
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
	
	// Class MessageBar
	function MessageBar () {}
	// Constructor
	function MessageBar (divId, des_address) {this.setData(divId, des_address);}
	
	//Class Attributes
	MessageBar.prototype.sendMessage 		= void(0);
	MessageBar.prototype.methodType 		= "get";
	MessageBar.prototype.address 			= false;
	MessageBar.prototype.XmlHttpObj 		= false;
	MessageBar.prototype.resultDivId 		= false;
	MessageBar.prototype.resultDiv 			= false;
	MessageBar.prototype.loadingImage 		= "./images/wait.gif";
	MessageBar.prototype.successMessage 	= "Done";
	MessageBar.prototype.errorMessage 		= "Error";
	MessageBar.prototype.onError = function (r) {};
	MessageBar.prototype.onSuccess = function (r) {};
	MessageBar.prototype.onDone = function (r) {document.getElementById(this.resultDivId).innerHTML = r;};
	MessageBar.prototype.errorCharacter 	= '0';
	MessageBar.prototype.successCharacter 	= '1';
	MessageBar.prototype.responseStatus 	= false;
		
	// Class Methods
	
	MessageBar.prototype.setData = function (divId, des_address)
	{
		this.resultDivId = divId;
		this.address = des_address;
	}
	
	MessageBar.prototype.getXmlHttpObject = function ()
	{
		if (typeof XMLHttpRequest != "undefined") {
			return new XMLHttpRequest();
		} else if (window.ActiveXObject) {
		var aVersions = [ "MSXML2.XMLHttp.5.0",
			"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
			"MSXML2.XMLHttp","Microsoft.XMLHttp"
			];
		for (var i = 0; i < aVersions.length; i++) {
			try {
					var oXmlHttp = new ActiveXObject(aVersions[i]);
					return oXmlHttp;
				} catch (oError) {
					//Do nothing
				}
			}
		}
		
		throw new Error("XMLHttp object could be created.");
	}
	
	MessageBar.prototype.showLoading = function ()
	{
		this.resultDiv.innerHTML = '<img src="' + this.loadingImage + '" />';
	}
	
	MessageBar.prototype.randomizeAddress = function()
	{
		if (this.address.indexOf('?') >= 0)
		{
			this.address += "&r="+Math.random();
		}
		else
		{
			this.address += "?r="+Math.random();
		}
	}
		
	MessageBar.prototype.send = function ()
	{

		var XmlHttpObj = this.getXmlHttpObject();
		if (!this.resultDivId || !this.address || !this.methodType)
		{
			throw new Error("Not all data provided. Please provide the complete data.");
		}
		
		this.randomizeAddress();
		
		XmlHttpObj.open(this.methodType, this.address, true);
		var onSent = this.onSent;

		this.resultDiv = document.getElementById(this.resultDivId);
		resultDiv = this.resultDiv;
		
		var parent = this;
		
		this.showLoading();

		XmlHttpObj.onreadystatechange = function () {
			if (XmlHttpObj.readyState == 4) {
				if (XmlHttpObj.status == 200) {
					var responseData = XmlHttpObj.responseText;					
					onSent (parent, XmlHttpObj.responseText);
				}
			}
		}

		XmlHttpObj.send(null);	
	}
	
	MessageBar.prototype.onSent = function (parent,response)
	{
		response = response.trim();
		parent.resultDiv.innerHTML = "";
		if (response.charAt(0) == parent.errorCharacter) // ERROR
		{
			parent.resultDiv.innerHTML = '<span class = "errorReturnMSG">' + parent.errorMessage + '</span>';
			parent.responseStatus = false;
			parent.onError(response);
		}
		else if (response.charAt(0) == parent.successCharacter)
		{
			parent.resultDiv.innerHTML = '<span class = "successReturnMSG">' + parent.successMessage + '</span>';
			parent.responseStatus = true;
			parent.onSuccess(response);
		}
		parent.onDone(response);
	}
	
	MessageBar.prototype.getRequestBody = function (oForm) 
	{
		var aParams = new Array();
		for (var i=0 ; i < oForm.elements.length; i++) {
		var sParam = encodeURIComponent(oForm.elements[i].name);
		sParam += "=";
		sParam += encodeURIComponent(oForm.elements[i].value);
		aParams.push(sParam);
		}
		return aParams.join("&");
	}

	MessageBar.prototype.send_by_POST = function (oform)
	{
		var XmlHttpObj = this.getXmlHttpObject();
		
		if (!oform)
		{
			throw new Error("Not all data provided. Please provide the complete data.");
		}
		
		var sBody = this.getRequestBody(oform);
		
		XmlHttpObj.open('post', oform.action, true);
		XmlHttpObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		var onSent = this.onSent;

		this.resultDiv = document.getElementById(this.resultDivId);
		resultDiv = this.resultDiv;
		
		var parent = this;
		
		this.showLoading();
					
		XmlHttpObj.onreadystatechange = function () {
			if (XmlHttpObj.readyState == 4) {
				if (XmlHttpObj.status == 200) {
					onSent (parent, XmlHttpObj.responseText);
				}
			}
		}
		XmlHttpObj.send(sBody);	
	}
	// -->
