
function MyAjax()
{

    this.onRequestBegin=null;
    this.onRequesting=null;
    this.onRequestComplete=null;
    this.onError=null;

    this.url="";
	this.state=0;
    this.requestMethod="GET";
    this.responseMethod="text";
    this.postData = null;
    this.asyncronize = true;
    this.postData=null;
    this.responseText="";
    this.responseXML="";
	
    this.destinationDivId=null;
	this.destinationAlign="left";
    
    this.clearDestination=false;

    /*this.divTextVertialAlign="middle";*/
    
    this.xmlHttpObject=null;

	/*-------------------------------------------------------*/
	this.loadingMessage=new Object();
	
	this.loadingMessage.source = "";
	this.loadingMessage.autoHide = false;
	this.loadingMessage.show = true;
	this.loadingMessage.displayErrorMessage = false;
	this.loadingMessage.style = null;
	this.loadingMessage.text = "";
	this.loadingMessage.html = "";
	this.loadingMessage.image = "";
	this.loadingMessage.align = "center";
	
	/*-----------------------------------------------------------------------------------------------------------------*/

    this.doRequest = function(rMethod, rUrl, rDiv, rHandle, rPData)
    {
        //alert("Request");
        /*
        if(this.state>0)
        {
            alert("Already a process is running with URL : " + this.url);
            break;
        }
        */

        this.state=1;
		var ldmsgdiv;
		
		if(this.loadingMessage.source)
			ldmsgdiv = this.loadingMessage.source;
		else if(rDiv)
			ldmsgdiv = rDiv;

        //alert("before loading message");

		if(this.clearDestination == true && rDiv)
        	document.getElementById(rDiv).innerHTML="";
        
        
        
		if(ldmsgdiv)
		{
		//alert("loading message" + ldmsgdiv);
		
			document.getElementById(ldmsgdiv).innerHTML="Loading...";
			
		//alert("loading message after ");	
			
			
             if(this.loadingMessage.show==true)
             {
                //alert("showing");
                
				document.getElementById(ldmsgdiv).style.textAlign=this.loadingMessage.align;
				
				if(this.loadingMessage.autoHide==true)
				{
					document.getElementById(ldmsgdiv).style.visibility="visible";
					document.getElementById(ldmsgdiv).style.display="block";
				}
				
                if(this.loadingMessage.html)
                {
                    document.getElementById(rldmsgdiv).innerHTML=this.loadingMessage.html;   
                }
                else
                {
                
                    var im,sty;
                    
                    if(this.loadingMessage.image)
                        im="<img src='"+this.loadingMessage.image+"'>"
                    else
                        im="";
                       
                    if (this.loadingMessage.style==null)
                    {
                       	sty="padding:0px; text-align:justify; background-color:'#eef1f5'; color:'#6B6B6B';";
                        sty+="font-family: Verdana,Arial,Tahoma;font-size:'8pt';";
                    }
                    else
                        sty=this.loadingMessage.style;


                    txt = "<span style=\"" +sty+"\">"+(this.loadingMessage.text?this.loadingMessage.text:"Loading...")+"&nbsp;&nbsp;"+im+"</span>&nbsp;&nbsp;";

                    document.getElementById(ldmsgdiv).innerHTML=txt;
                }  
             }
		}

        //alert("loading...");

        if(rMethod) this.requestMethod=rMethod;

		if(rUrl) this.url=rUrl;
		
        if(rDiv) this.destinationDivId=rDiv;
		
        if(rHandle) this.onRequestComplete=rHandle;

        if(rPData) this.postData=rPData;    
        
        this.xmlHttpObject=getobj();
        
        
        if(this.onRequestBegin)
		
            this.onRequestBegin("");
            
        var parentObj=this;

		/*---------------------------------------------------------------------------------------------------------------*/
        this.xmlHttpObject.onreadystatechange=function()
        { 
            
            //alert("onredystate");
            
            
            this.state=parentObj.xmlHttpObject.readyState;
            
            if(parentObj.onRequesting)
                parentObj.onRequesting(parentObj.xmlHttpObject.readyState);
			
            if(parentObj.xmlHttpObject.readyState==4)
            {
				//alert("innder readystate");
				
				if(parentObj.destinationDivId)
	                document.getElementById(parentObj.destinationDivId).style.textAlign = parentObj.destinationAlign;
				
                parentObj.responseText=parentObj.xmlHttpObject.responseText;
                parentObj.responseXML=parentObj.xmlHttpObject.responseXML;
                var t;
                
                if(parentObj.responseMethod=="text")
                {
					
                    t=parentObj.xmlHttpObject.responseText;
					
                    if(parentObj.destinationDivId)
                        document.getElementById(parentObj.destinationDivId).innerHTML=t;
                
                }
                
                else if(parentObj.responseMethod=="xml")
                {
                    t=parentObj.xmlHttpObject.responseXML;
                    
                    if(parentObj.destinationDivId)
                        document.getElementById(parentObj.destinationDivId).innerHTML=t;
                }
                
				
                
				if(ldmsgdiv)
				{
				    if(parentObj.loadingMessage.autoHide==true)
					    document.getElementById(ldmsgdiv).style.display="none";
					
				    if(parentObj.destinationDivId!=ldmsgdiv)
				        document.getElementById(ldmsgdiv).innerHTML="";
                }
                
                if(parentObj.onRequestComplete)
                {
                    parentObj.onRequestComplete(t);
                    parentObj.state=0;
                }
                
            }
            else
            {
                if(parentObj.onError!=null)
                    parentObj.onError(parentObj.xmlHttpObject.readyState);
                    
                if(parentObj.displayErrorMessage==true)
                {
                    window.alert('An error occurred, but the error message cannot be \n displayed. This is probably because of your browser\'s \n pop-up blocker');
                    /*
                    + ' Code: ' + parentObj.xmlHttpObject.status + '\n'
                    + 'Status Description: ' + parentObj.xmlHttpObject.statusText);*/
                }
            }
			

			
        };
		
        this.xmlHttpObject.open(this.requestMethod,this.url,this.asyncronize);
        this.xmlHttpObject.send(this.postData);
		
    };
    
    
	    			
					
	/*-----------------------------------------------------------------------------------------------------------------*/

    this.Abort = function() 
    {
        if (this.xmlHttpObject) 
        {
            this.xmlHttpObject.onreadystatechange = function() { };
            this.xmlHttpObject.abort();
            this.xmlHttpObject = null;
        }
    }

    
    /*-------------------------------------------------------------------------------------------------------------------------*/
	
    function getobj() 
    {
        var re;

        
        try 
        {   
            re = new XMLHttpRequest();
        }
        catch (e) 
        {
            try 
            {
                re = new ActiveXObject("MSXML2.XMLHTTP.3.0");
            }
            catch (e) 
            {
                try 
                {

                    re = new ActiveXObject('Microsoft.XMLHTTP');
                }
                catch (e) 
                {
                    re = false;
                }
            }
        }
        
        return re;
    }
    /*-------------------------------------------------------------------------------------------------------------------------*/
    
}
