﻿//This function is basically a class which encapsulates all the methods needed to perform asynchronous callbacks
function JeeranRemoteScripting() {
	//--------------PRIVATE MEMBERS
	
	var _JeeranRemoteScripting = this;
	var _HTTPRequester = null;
	var _RequestMethod = null;
	var _RequestURL = null;
	var _RequestAsynch = null;
	var _Data = null;
	var _GetXML = "";
	var _GetText = "";
	
	//--------------PROPERTIES
	
	//Write only
	this.SetRequestMethod = function(Method) {		
		_RequestMethod = Method;
	}
	this.SetRequestURL = function(URL) {
		_RequestURL = URL;		
	}		
	this.SetData = function(Data) {
		_Data = Data;
	}	
	this.SetRequestAsynch = function(Asynch) {
		_RequestAsynch = Asynch;
	}
	//Read only
	this.GetXML = function() {
		return _GetXML;
	}
	this.GetText = function() {
		return _GetText;
	}
	
	//--------------PUBLIC METHODS
	
	//Initialize the requester
	this.InitializeRequester = function() {		
		var randomnumber = Math.floor(Math.random()*100000000); //this is to prevent caching
		if (_RequestURL.indexOf("?") > -1)
		{
			_RequestURL += "&r=" + randomnumber
		}
		else
		{
			_RequestURL += "?r=" + randomnumber
		}		
		
		try
		{			
			_HTTPRequester = new XMLHttpRequest();
			_HTTPRequester.open(_RequestMethod, _RequestURL, _RequestAsynch);
		}
		catch (error)
		{
			try
			{
				_HTTPRequester = new ActiveXObject("Microsoft.XMLHTTP");
				_HTTPRequester.open(_RequestMethod, _RequestURL, _RequestAsynch);
			}
				catch (error)
			{	
				try
				{
					_HTTPRequester = new ActiveXObject("Msxml2.XMLHTTP");
					_HTTPRequester.open(_RequestMethod, _RequestURL, _RequestAsynch);
				}
				catch (error)
				{
				}
			}
		}				
	}
	
	//Send the request
	this.SendRequest = function() {
			if (_RequestMethod == "POST")
			{
				_HTTPRequester.setRequestHeader("Method", "POST "+_RequestURL+" HTTP/1.1");
       			_HTTPRequester.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			_HTTPRequester.send(_Data);
			_HTTPRequester.onreadystatechange = stateHandler;
	}
	
	//Close aborts the XMLHttpRequest object
	this.Close = function() {
		_HTTPRequester.abort();
	}	

	//--------------PRIVATE METHODS
	
	//This monitors the request status and performs the nessesary actions
	function stateHandler()
	{
	    try
	    {
		    if (_HTTPRequester.readyState == 4)
		    {
			    if (_HTTPRequester.status == 200)
			    {				
					    _GetXML = _HTTPRequester.responseXML;
					    _GetText = _HTTPRequester.responseText;
					    //_GetText = _HTTPRequester.responseText;
					    _Loaded(); //Fire the loaded event
			    }
			    else
			    {
				    _Failure(); //Fire the failure event
			    }
		    }
    	    return true;
        }
        catch(error)
        {
            return false;
            _Failure(); //Fire the failure event
        }
	}
	
	//--------------EVENTS
	
	//Public Events
	this.OnLoaded = function() {};
	this.OnFailure = function() {};
	
	//Private Events
	function _Loaded() { 
		_JeeranRemoteScripting.OnLoaded(); 
		_JeeranRemoteScripting.Close();
	}
	
	function _Failure() 
	{ 
		_JeeranRemoteScripting.OnFailure();
		_JeeranRemoteScripting.Close();
	}
}

// Does creates and sends the ajax request and returns the requester object
function CreateAjaxRequest(RequestURL, RequestMethod, RequestData)
{
    var AjaxRequester = new JeeranRemoteScripting();
	AjaxRequester.SetRequestMethod(RequestMethod);
	AjaxRequester.SetRequestURL(RequestURL);
	AjaxRequester.SetRequestAsynch(true);
	AjaxRequester.SetData(RequestData);

	AjaxRequester.InitializeRequester();
	AjaxRequester.SendRequest();
    
    return AjaxRequester;

}