﻿//====================================================
// AJAX.js
//start code
//====================================================
// AJAX main  funtion. it will work as class.
function XP_ajax_class()
{
    this.req = null; 
    this.url = null; 
    this.method = 'GET'; 
    this.async = true; 
    this.status = null; 
    this.statusText = ''; 
    this.postData = null; 
    this.readyState = null; 
    this.responseText = null; 
    this.responseXML = null; 
    this.handleResp = null; 
    this.responseFormat = 'text', // 'text', 'xml', or 'object' 
    this.mimeType = null;  
    this.retutnOBJ=null;
    this.functionName='';   
    this.variable=null;   
}

XP_ajax_class.prototype.main=function(strUrl,strMethod,strPostData,strFormat,obj,fnName) 
{
    this.url=strUrl;
    this.method=strMethod || "GET";
    this.responseFormat = strFormat || 'text';
    this.postData=strPostData || null;
    this.retutnOBJ=obj || null;
    this.functionName=fnName || "";
    this.SendQuery();
}

//init function for request object
XP_ajax_class.prototype.init = function() 
{ 
    if (!this.req) { 
    try { 
        // Try to create object for Firefox, Safari, IE7 [30], etc. 
        this.req = new XMLHttpRequest(); 
    } 
    catch (e) { 
        try { 
            // Try to create object for later versions of IE. 
            this.req = new ActiveXObject('MSXML2.XMLHTTP'); 
        } 
        catch (e) { 
            try { 
                // Try to create object for early versions of IE. 
                this.req = new ActiveXObject('Microsoft.XMLHTTP'); 
            } 
            catch (e) { 
                // Could not create an XMLHttpRequest object. 
                return false; 
                } 
            } 
        } 
    } 
    return this.req; 
}

XP_ajax_class.prototype.SendQuery=function() //get data 
{     
   if (!this.init()) 
    { 
        alert('Could not create XMLHttpRequest object.'); 
        return; 
    } 
   if(this.req!=null) 
   {         
        var self = this;	
        var t;
        t=new Date();
        this.postData=this.postData+"&t="+t;	
        if(this.method=="GET"){            
            this.url=this.url+"?" + this.postData ;
        }                
        self.req.open(this.method, this.url, true);       
        this.req.onreadystatechange = function() 
        {         
            var resp = null; 
            if (self.req.readyState == 4) 
            { 
                switch (self.responseFormat) 
                { 
                    case 'text': 
                        resp = self.req.responseText; 
                        break; 
                    case 'xml': 
                        resp = self.req.responseXML; 
                        break; 
                    case 'object': 
                        resp = req; 
                        break; 
                } 
                if (self.req.status >= 200 && self.req.status <= 299) {                      
                    if(self.retutnOBJ){
                    self.retutnOBJ.innerHTML= resp; } 
                    if (self.functionName!=""){
                    eval(self.functionName);}
                }                 
            } 
        };
        if(this.method=="GET"){
            self.req.send(null);}
        else{            
            self.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            self.req.setRequestHeader("Content-length", this.postData.length);
            self.req.setRequestHeader("Connection", "close");
            self.req.send(this.postData);
        }
   } 
}

XP_ajax_class.prototype.handleResp=function(resp1)
{
    alert(resp1);
    if(self.retutnOBJ==null && self.functionName==""){
        alert(resp1);
        return resp1;
    }
    
    if(self.retutnOBJ!=null){
    self.retutnOBJ.innerHTML= resp1; }
    if (self.functionName!=""){
    eval(self.functionName);}   
    
};

XP_ajax_class.prototype.abort = function() 
{ 
    if (this.req) 
    { 
        this.req.onreadystatechange = function() { }; 
        this.req.abort(); 
        this.req = null; 
    } 
};

//====================================================
// AJAX.js
//end code
//====================================================
