﻿var oHTTPRequest = false;

///////////HTTP Request For GET /////////////////////////
function MakeGETRequest(url, callback_function) 
{
    //resolving for internet explorer ajax client side caching bug
    url += "&urq=" + Math.random().toString();
    
    oHTTPRequest = false;
    if (window.XMLHttpRequest) // Mozilla, Safari,...
    {
	    oHTTPRequest = new XMLHttpRequest();
	    if (oHTTPRequest.overrideMimeType)
		    oHTTPRequest.overrideMimeType("text/xml");
    } 
    else if (window.ActiveXObject) // IE
    {
	    try 
	    { 
	        oHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
	    } 
	    catch (e) 
	    {
		    try 
		    {
		        oHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
		    } 
		    catch (e) {}
        }
    }
    if (!oHTTPRequest) 
    {
	    alert("Cannot create XMLHTTP instance");
	    return false;
    }
    
    oHTTPRequest.onreadystatechange = function() 
    {
	    if (oHTTPRequest.readyState == 4)
	    {
		    if (oHTTPRequest.status == 200)
		        eval(callback_function + "();");
		    else
			    alert("There was a problem with the request.(Code: " + oHTTPRequest.status + ")");
	    }
    }
    oHTTPRequest.open("GET", url, true);
    oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oHTTPRequest.send(null);
}

function MakeGETTextRequest(url, callback_function)
{
    //resolving for internet explorer ajax client side caching bug
    url += "&urq=" + Math.random().toString();
    
    oHTTPRequest = false;
    if (window.XMLHttpRequest) // Mozilla, Safari,...
    {
	    oHTTPRequest = new XMLHttpRequest();
	    if (oHTTPRequest.overrideMimeType)
		    oHTTPRequest.overrideMimeType("text/xml");
    } 
    else if (window.ActiveXObject) // IE
    {
	    try 
	    { 
	        oHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
	    } 
	    catch (e) 
	    {
		    try 
		    {
		        oHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
		    } 
		    catch (e) {}
        }
    }
    if (!oHTTPRequest) 
    {
	    alert("Cannot create XMLHTTP instance");
	    return false;
    }
    oHTTPRequest.onreadystatechange = function() 
    {
	    if (oHTTPRequest.readyState == 4)
	    {
		    if (oHTTPRequest.status == 200)
		        eval(callback_function + "(oHTTPRequest.responseText)");
		    else
			    alert("There was a problem with the request.(Code: " + oHTTPRequest.status + ")");
	    }
    }
    oHTTPRequest.open("GET", url, true);
    oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oHTTPRequest.send(null);
}

function MakeGETTextRequestWithParams(url, callback_function, sParameters) 
{
    //resolving for internet explorer ajax client side caching bug
    url += "&urq=" + Math.random().toString();
    
    oHTTPRequest = false;
    if (window.XMLHttpRequest) // Mozilla, Safari,...
    {
	    oHTTPRequest = new XMLHttpRequest();
	    if (oHTTPRequest.overrideMimeType)
		    oHTTPRequest.overrideMimeType("text/xml");
    } 
    else if (window.ActiveXObject) // IE
    {
	    try 
	    { 
	        oHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
	    } 
	    catch (e) 
	    {
		    try 
		    {
		        oHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
		    } 
		    catch (e) {}
        }
    }
    if (!oHTTPRequest) 
    {
	    alert("Cannot create XMLHTTP instance");
	    return false;
    }
    oHTTPRequest.onreadystatechange = function() 
    {
       
	    if (oHTTPRequest.readyState == 4)
	    {
		    if (oHTTPRequest.status == 200)
		        eval(callback_function + "(oHTTPRequest.responseText, '" + sParameters + "')");
		    else
			    alert("There was a problem with the request.(Code: " + oHTTPRequest.status + ")");
	    }
    }
    oHTTPRequest.open("GET", url, true);
    oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oHTTPRequest.send(null);
}

function MakeGETTextRequestSynchronizedWithParams(url, callback_function, sParameters) 
{
    //resolving for internet explorer ajax client side caching bug
    url += "&urq=" + Math.random().toString();
    
    oHTTPRequest = false;
    if (window.XMLHttpRequest) // Mozilla, Safari,...
    {
	    oHTTPRequest = new XMLHttpRequest();
	    if (oHTTPRequest.overrideMimeType)
		    oHTTPRequest.overrideMimeType("text/xml");
    } 
    else if (window.ActiveXObject) // IE
    {
	    try 
	    { 
	        oHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
	    } 
	    catch (e) 
	    {
		    try 
		    {
		        oHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
		    } 
		    catch (e) {}
        }
    }
    if (!oHTTPRequest) 
    {
	    alert("Cannot create XMLHTTP instance");
	    return false;
    }
    oHTTPRequest.onreadystatechange = function() 
    {
	    if (oHTTPRequest.readyState == 4)
	    {
		    if (oHTTPRequest.status == 200)
		        eval(callback_function + "(oHTTPRequest.responseText, '" + sParameters + "')");
		    else
			    alert("There was a problem with the request.(Code: " + oHTTPRequest.status + ")");
	    }
    }
    oHTTPRequest.open("GET", url, false);
    oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oHTTPRequest.send(null);
}

function MakeGETTextRequestSynchronized(url, callback_function) 
{
    //resolving for internet explorer ajax client side caching bug
    url += "&urq=" + Math.random().toString();
    
    oHTTPRequest = false;
    if (window.XMLHttpRequest) // Mozilla, Safari,...
    {
	    oHTTPRequest = new XMLHttpRequest();
	    if (oHTTPRequest.overrideMimeType)
		    oHTTPRequest.overrideMimeType("text/xml");
    } 
    else if (window.ActiveXObject) // IE
    {
	    try 
	    { 
	        oHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
	    } 
	    catch (e) 
	    {
		    try 
		    {
		        oHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
		    } 
		    catch (e) {}
        }
    }
    if (!oHTTPRequest) 
    {
	    alert("Cannot create XMLHTTP instance");
	    return false;
    }
    oHTTPRequest.onreadystatechange = function() 
    {
	    if (oHTTPRequest.readyState == 4)
	    {
		    if (oHTTPRequest.status == 200)
		        eval(callback_function + "(oHTTPRequest.responseText)");
		    else
			    alert("There was a problem with the request.(Code: " + oHTTPRequest.status + ")");
	    }
    }
    oHTTPRequest.open("GET", url, false);
    oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oHTTPRequest.send(null);
}	
	
///////////HTTP Request For Post /////////////////////////
function MakePOSTRequest(url, parameters, callback_function) 
{
    //resolving for internet explorer ajax client side caching bug
    url += "&urq=" + Math.random().toString();
    
    oHTTPRequest = false;
    if (window.XMLHttpRequest) // Mozilla, Safari,...
    {
        oHTTPRequest = new XMLHttpRequest();
        if (oHTTPRequest.overrideMimeType) 
            oHTTPRequest.overrideMimeType("text/html");
    } 
    else if (window.ActiveXObject) // IE
    {
        try 
        {
            oHTTPRequest= new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
            try
            {
                oHTTPRequest= new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
    }
  
    if (!oHTTPRequest) 
    {
         alert("Cannot create XMLHTTP instance");
         return false;
    }
  
    oHTTPRequest.onreadystatechange = function() 
    {
        if (oHTTPRequest.readyState == 4)
        {
            if (oHTTPRequest.status == 200)
                eval(callback_function + "();");  //  callback_function(oHTTPRequest.responseText);
            else
	            alert("There was a problem with the request.(Code: " + oHTTPRequest.status + ")");
        }
    }
    oHTTPRequest.open("POST", url, false);
    oHTTPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    oHTTPRequest.setRequestHeader("Content-length", parameters.length);
    //  oHTTPRequest.setRequestHeader("Connection", "close");   /* when create Synchronized http request cannot close http connection.*/
    oHTTPRequest.send(parameters);
}

///////////HTTP Request For Post /////////////////////////
function MakePOSTTextRequest(url, parameters, callback_function) 
{
    //resolving for internet explorer ajax client side caching bug
    url += "&urq=" + Math.random().toString();
    
    oHTTPRequest = false;
    if (window.XMLHttpRequest) // Mozilla, Safari,...
    {
        oHTTPRequest = new XMLHttpRequest();
        if (oHTTPRequest.overrideMimeType) 
            oHTTPRequest.overrideMimeType("text/html");
    } 
    else if (window.ActiveXObject) // IE
    {
        try 
        {
            oHTTPRequest= new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
            try
            {
                oHTTPRequest= new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
    }
  
    if (!oHTTPRequest) 
    {
         alert("Cannot create XMLHTTP instance");
         return false;
    }
  
    oHTTPRequest.onreadystatechange = function() 
    {
        if (oHTTPRequest.readyState == 4)
        {
            if (oHTTPRequest.status == 200)
                eval(callback_function + "(oHTTPRequest.responseText)");
            else
	            alert("There was a problem with the request.(Code: " + oHTTPRequest.status + ")");
        }
    }
    oHTTPRequest.open("POST", url, false);
    oHTTPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    oHTTPRequest.setRequestHeader("Content-length", parameters.length);
    oHTTPRequest.setRequestHeader("Connection", "close");
    oHTTPRequest.send(parameters);
}
