﻿var browser = '';
var ua = navigator.userAgent;
if (ua.indexOf("MSIE")!=-1){ browser = "MSIE"; } else if (ua.indexOf("Firefox")!=-1){ browser = "FF"; }
var Ajax1 =  
{
        Error : null ,
        
        GetXMLHTTPRequest : function()
        {
            this.Error = null;
            if (browser == "MSIE")
            {
                var oXMLHTTPRequest = null;
                try //IE
                {
                    oXMLHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch(err) 
                {
                    try 
                    {
                        oXMLHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
                    } 
                    catch(err) 
                    {
                        oXMLHTTPRequest = null;
                    }
                }
            }
            else if (browser == "FF")
            {
                if(!oXMLHTTPRequest) //Mozilla
                {
                    oXMLHTTPRequest = new XMLHttpRequest();
                }
            }
            return oXMLHTTPRequest;
        },      
        HTTPServerRequest : function(strURL,strPostParams,bIsResponseXML,oCallBackFunction,callbackParams,objectCreatorId)
        {
            AjaxProgress.fCreateProgress(objectCreatorId);
            var oResponseData = null;
           // strURL = encodeURI(strURL);
		   strURL =strURL;
			strPostParams = strPostParams? encodeURI(strPostParams) : strPostParams;			
            var oXMLHTTPRequest = this.GetXMLHTTPRequest();
            
            if(oXMLHTTPRequest)
            {
                var strMethod = strPostParams ? 'POST' : 'GET';
                strPostParams =  strPostParams=="%20" ? "" : strPostParams;                
                oXMLHTTPRequest.open(strMethod, strURL, true);
                oXMLHTTPRequest.onreadystatechange = function()
                {
                    var bComplete = false;
                    try
                    {
                        if(oXMLHTTPRequest.readyState == 4 && oXMLHTTPRequest.status == 200)
                        {
                            bComplete = true;
                            if(bIsResponseXML)
                            {
                                oResponseData = oXMLHTTPRequest.responseXML;
                            }
                            else
                            {
                                oResponseData = oXMLHTTPRequest.responseText;
                            }
                        }
                        else
                            if(oXMLHTTPRequest.readyState == 4 && oXMLHTTPRequest.status != 200)//oXMLHTTPRequest.status == 500 || oXMLHTTPRequest.status == 400 || oXMLHTTPRequest.status == 404 || oXMLHTTPRequest.status == 12152 ))
                            {
                                Ajax.Error = oXMLHTTPRequest.status;
                                AjaxProgress.fCloseProgress();
                                
                                alert("ERROR");
//                                switch(oXMLHTTPRequest.status){
//                                    case 12029:
//                                    case 12030:
//                                    case 12031:
//                                    case 12152:
//                                    case 12159:
//                                      Ajax.HTTPServerRequest(strURL,strPostParams,bIsResponseXML,oCallBackFunction,callbackParams,objectCreatorId);
//                                }

                            }
                    }
                    catch(ex)
                    {       
                        oResponseData = null;
                        bComplete = true;
                    }

                    if(bComplete && oCallBackFunction)
                    {
                       //oCallBackFunction(oResponseData,callbackParams);
                       AjaxProgress.fCreateCallback(oCallBackFunction,oResponseData,callbackParams);
                    }
                }                       
                if(strPostParams)
                {
				    oXMLHTTPRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');					
					oXMLHTTPRequest.setRequestHeader("Content-length", strPostParams.length);
					oXMLHTTPRequest.setRequestHeader("Connection", "close");


                }
                oXMLHTTPRequest.send(strPostParams);                                            
            }
            
            //return the request object
            return oXMLHTTPRequest;
        }       
}

function readXmlAjaxPage(AjaxPage)
{
    if (window.ActiveXObject)
    {
        var doc=new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(AjaxPage);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
        var parser=new DOMParser();
        var doc=parser.parseFromString(AjaxPage,"text/xml");
    }
    if (browser == "MSIE")
    {     
        var custIdNode = doc.documentElement.childNodes[0];        
    }
    else if (browser == "FF")
    {        
        var custIdNode = doc.documentElement.childNodes[1];
    }  
    return custIdNode;      
}

function getXmlDoc(AjaxPage)
{
    if (window.ActiveXObject)
    {
        var doc=new ActiveXObject("Microsoft.XMLDOM");
        doc.async="false";
        doc.loadXML(AjaxPage);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
        var parser=new DOMParser();
        var doc=parser.parseFromString(AjaxPage,"text/xml");
    }
    if (browser == "MSIE")
    {     
        var custIdNode = doc.documentElement;        
    }
    else if (browser == "FF")
    {        
        var custIdNode = doc.documentElement;
    }  
    return custIdNode;      
}


/////////////////////////////////////////////////////
// AjaxProgress OBJECT
// class that create the loading display
// while the ajax is loaded
// called and disposed from the Ajax OBJECT
/////////////////////////////////////////////////////

var AjaxProgress =
{
    
    isInitinlize : false ,
    
    blocker :  null ,
    
    
    objectHolder : null ,
    
    ProgressImage : null ,
    
    Acallback : null ,
    
    Adata : null ,
     
    AcallbackParams : null ,
    
    fCreateProgress : function(objectHolderId)
    {
        if(objectHolderId == null)return; 
        this.objectHolder = document.getElementById(objectHolderId);
        if(this.objectHolder == null)return;        
        this.blocker = document.createElement("div");
        this.blocker.className="AjaxBlocker";
        var Pos = findPos(this.objectHolder);
        var leftPosFix = 0;
        this.blocker.style.top = (Pos[1]<0 ? 0 : Pos[1]) + "px";
        this.blocker.style.left = (Pos[0])  + "px";
        this.ProgressImage = document.createElement("div");
        this.ProgressImage.id = "AjaxProgress";        
        this.blocker.style.height = this.objectHolder.offsetHeight + "px";
        this.blocker.style.width = (this.objectHolder.offsetWidth + leftPosFix) + "px";
        this.ProgressImage.style.top = ((this.objectHolder.offsetHeight/4)+ Pos[1]) + "px";
        this.ProgressImage.style.left = ((this.objectHolder.offsetWidth/2) + Pos[0]) + "px";
        document.body.appendChild(this.blocker);
        document.body.appendChild(this.ProgressImage);
        this.isInitinlize = true;
    
       
       
    },
    
    fCreateCallback : function(oCallback,data, callbackAdditionalParams)
    {
        this.Acallback = oCallback;
        this.Adata = data;
        this.AcallbackParams = callbackAdditionalParams;
        window.setTimeout(function(){AjaxProgress.fCloseProgress();},500);
        
    } ,
    
    fCloseProgress : function()
    {
        if(this.Acallback == null)
        {
            if(Ajax.Error != null)
            {
                this.fDispose();
            }
            else
                window.setTimeout(function(){AjaxProgress.fCloseProgress();},500);
        }
        else
        {
          this.Acallback(this.Adata,this.AcallbackParams);
          this.fDispose();
        }
    } ,
    
    fDispose : function()
    {
        if(this.isInitinlize == false) return;
        this.Acallback = null;
        this.Adata = null;
        this.AcallbackParams = null;
        document.body.removeChild(this.blocker);
        document.body.removeChild(this.ProgressImage);
        //this.objectHolder.removeChild(this.blocker);
       // this.objectHolder.removeChild(this.ProgressImage);
        this.isInitinlize = false;
    
    }
}

function findPos(obj) 
{
		var curleft = curtop = 0;		
		if(browser == 'MSIE')
		{
			if (obj.offsetParent) 
			{
				curleft = obj.offsetLeft;
				curtop = obj.offsetTop;				
				while (obj = obj.offsetParent ) 
				{
				    if(obj.tagName!= "BODY")
					    curleft += obj.offsetLeft;
					    curtop += obj.offsetTop;
				}
			}
		}
		else
		{
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;			
		}
		window.status = document.documentElement.scrollLeft;
		return [curleft,curtop];
		
}
