/**
 * Variable to store XMLHTTP object.
 */
var objXmlHttp 	= null;
var lastCall 	= new Date();

/**
 * Function to check whether the XMLHTTP object is created or not.
 * 
 * @return	boolean	true if object is created.
 * 					false if object is not created.
 */
function isCreated() {
	
	if ( !objXmlHttp ) {
		if ( (objXmlHttp == false) || (objXmlHttp == null)) {
			return false;
		}
	}
	return true;
}

/**
 * Function to create the XMLHTTP object to handle the AJAX functionality.
 *
 * @return 	XMLHTTP 	object on success.
 * 			boolean 	false on failure.
 */
function createObject() { 

	if ( isCreated() ) {
		objXmlHttp = null;
	}

	if ( !isCreated() ) {
		try {
			// For Opera 8.0+, Mozilla, Firefox, Safari
			objXmlHttp = new XMLHttpRequest();
		}
		catch(e) {
			// For Internet Explorers
			try {
				// Internet Explorer 5.5+
				objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try {
					// Internet Explorer upto 5.5
					objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e) {
					// property is not supported.
					objXmlHttp = false;
				}
			}
		}
	}
}

/**
 * Function to call the server using the GET method.
 *
 * @param	string	url	to which the call will be made.
 * @param	string	name of the function that will handle the response from the server.
 *
 * @return	boolean	true if the call is successful to the server.
 					false if the call is un-successful to the server.
 */
function requestGET(url, handler) {
	createObject();
	if ( !isCreated() ) {
		return false;
	}
	
	// Hack to make a new call everytime.
	if ( url.indexOf("?") >= 0 ) {
		url += '&rnd='+ Math.random();
	}
	else {
		url += '?&rnd='+ Math.random();
	}
	
	objXmlHttp.onreadystatechange = handler; 
	objXmlHttp.open( "GET", url, true );
	objXmlHttp.send( null );
	return true;
}

/**
 * Function to call the server using the POST method.
 * It is to be noted that AJAX cannot handle file uploads
 * by iteself, to upload a file to the server a server 
 * script has to be used.
 *
 * @param	string	url	to which the call will be made.
 * @param	string	name of the function that will handle the response from the server.
 *
 * @return	boolean	true if the call is successful to the server.
 					false if the call is un-successful to the server.
 */
function requestPOST(url, parameters, handler) {
	createObject();
	if ( !isCreated() ) {
		return false;
	}

	// Hack to make a new call everytime.
	if ( url.indexOf("?") >= 0 ) {
		url += '&rnd='+ Math.random();
	}
	else {
		url += '?&rnd='+ Math.random();
	}

	objXmlHttp.onreadystatechange = handler; 
	objXmlHttp.open( "POST", url, true );
	objXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	objXmlHttp.setRequestHeader("Content-length", parameters.length);
	objXmlHttp.setRequestHeader("Connection", "close");
	objXmlHttp.send(parameters);
	return true;
}

function registerScript(html) {//alert(document.getElementById('lastLoadedScript'));
	var head = document.getElementsByTagName('head').item(0);
	var old  = document.getElementById('lastLoadedScript');
	if (old) head.removeChild(old);
//alert(document.getElementById('lastLoadedScript'));

	// Extract the script 
	var reg = new RegExp("<script.*?>(.+?)</script>");
	var m = reg.exec(html);
	
	if ( m && m[1]!='' ) {
		script = document.createElement('script');
		script.text = m[1];
		script.type = 'text/javascript';
		script.defer = true;
		script.id = 'lastLoadedScript';
		void(head.appendChild(script));
	}
}

function getLoadingHTML() {
	var html = 	'<table id="window_blue" align="center"><tr><td>'
				+'	<div class="header">'
				+'		<div class="headerL"></div>'
				+'		<div class="floatL">'
				+'			<div class="pTop7">Please wait..</div>'
				+'		</div>'
				+'		<div class="headerR"></div>'
				+'	</div>'
				+'	<div>'
				+'	<table align="center" cellpadding="5" cellspacing="5" border="0" class="pBottom5" style="background-color:#FFFFFF;border:2px solid #45B6FA;">'
				+'	<tr>'
				+'		<td align="center" valign="middle">'
				+'			<table align="center" cellpadding="0" cellspacing="5" class="loading">'
				+'			<tr>'
				+'				<td valign="middle">'
				+'					<img src="./../media/images/equotation/loading.gif" alt="Loading" border="0"/>'
				+'				</td>'
				+'				<td class="text_style">'
				+'					Please Wait<br/>'
				+'					Processing...'
				+'				</td>'
				+'			</tr>'
				+'			</table>'
				+'		</td>'
				+'	</tr>'
				+'	</table>'
				+'</td></tr></table>'
				+'<br/>';

return html;

}


function FunctionCallingSequence() {
	this.findex 	= -1;
	this.functions 	= new Array;
	this.args		= new Array;
	
	this.getFCount	= function() { return (this.findex+1); }
	
	/**
	 * Function to register a new function that has to be executed in sequence.
	 *
	 * @param	reference	reference to the function that is to be called.
	 *
	 * @retrun	boolean		true on success.
	 */
	this.addFunction	= 
		function(function_name) {
			this.findex += 1;
			this.functions[this.findex] = function_name;
			return true;
	}
	
	/**
	 * Function to add the arguments that have to be passed to the function 
	 * call. The arguments will be added to the last added function. These 
	 * arguments should be added in sequence so that there is no data mismatch.
	 *
	 * @param	var			any data that is accepeted as an argument in javascript.
	 *
	 * @retrun	boolean		true on success.
	 */
	this.addArgs	= 
		function(args) {

			if ( this.findex > -1 ) {
				this.args[this.findex] = new Array;
				if ( args && args != '' ) {
					if (args.constructor.toString().indexOf('Array') != -1) {
						count = args.length;
						for ( i=0; i<count; i++ ) {
							this.args[this.findex][i] = args[i];
						}
					}
					return true;
				}
				else {
					//alert('Please specify the arguments that are to be assocciated with the function');
					return false;
				}
			}
			else {
				//alert('Please first add a Fucntion, and then add the arguments.');
				return false;
			}
	}
	
	this.callFunctions = 
		function() {
			if ( this.findex > -1 ) {

				if ( this.args[this.findex] && this.args[this.findex].length > 0 ) {
					count = this.args[this.findex].length;
					
					// Call the function by sending the arguments in the function call.
					// Make the call.
					switch (count) {
						case (1): 
							
							this.functions[this.findex]( this.args[this.findex][0] );
							break;
						case (2): 
							this.functions[0](this.args[this.findex][0],this.args[this.findex][1]);
							/*this.functions[this.findex]( this.args[this.findex][0]
														,this.args[this.findex][1]);*/
							break;
						case (3): 
							this.functions[this.findex]( this.args[this.findex][0]
														,this.args[this.findex][1]
														,this.args[this.findex][2] );
							break;
						case (4): 
							this.functions[this.findex]( this.args[this.findex][0]
														,this.args[this.findex][1]
														,this.args[this.findex][2]
														,this.args[this.findex][3] );
							break;
						case (5): 
							this.functions[this.findex]( this.args[this.findex][0]
														,this.args[this.findex][1]
														,this.args[this.findex][2]
														,this.args[this.findex][3]
														,this.args[this.findex][4] );
							break;
						case (6): 
							this.functions[this.findex]( this.args[this.findex][0]
														,this.args[this.findex][1]
														,this.args[this.findex][2]
														,this.args[this.findex][3]
														,this.args[this.findex][4]
														,this.args[this.findex][5] );
							break;
						case (7): 
							this.functions[this.findex]( this.args[this.findex][0]
														,this.args[this.findex][1]
														,this.args[this.findex][2]
														,this.args[this.findex][3]
														,this.args[this.findex][4]
														,this.args[this.findex][5]
														,this.args[this.findex][6] );
							break;
						case (8): 
							this.functions[this.findex]( this.args[this.findex][0]
														,this.args[this.findex][1]
														,this.args[this.findex][2]
														,this.args[this.findex][3]
														,this.args[this.findex][4]
														,this.args[this.findex][5]
														,this.args[this.findex][6]
														,this.args[this.findex][7] );
							break;
						case (9): 
							this.functions[this.findex]( this.args[this.findex][0]
														,this.args[this.findex][1]
														,this.args[this.findex][2]
														,this.args[this.findex][3]
														,this.args[this.findex][4]
														,this.args[this.findex][5]
														,this.args[this.findex][6]
														,this.args[this.findex][7]
														,this.args[this.findex][8] );
							break;
						case (10): 
							this.functions[this.findex]( this.args[this.findex][0]
														,this.args[this.findex][1]
														,this.args[this.findex][2]
														,this.args[this.findex][3]
														,this.args[this.findex][4]
														,this.args[this.findex][5]
														,this.args[this.findex][6]
														,this.args[this.findex][7]
														,this.args[this.findex][8]
														,this.args[this.findex][9] );
							break;
					}
					
					// Remove the function that has been called.
					this.args.splice([this.findex], 1);
					this.functions.splice([this.findex], 1);
					this.findex -= 1;
					return true;
				}
				else {
					// Make the call.
					this.functions[this.findex]();

					// Remove the function that has been called.
					this.functions.splice([this.findex], 1);
					this.findex -= 1;
					return true;
				}
			}
			else {
				//alert('There is no function present to be called.\nPlease add a Fucntion, and then make a call.');
				return false;
			}
		}
	
}

var fcs = new FunctionCallingSequence();
