﻿// ----------------------------------------
// Wrapper function for constructing a request object.
// Parameters:
// reqType: The HTTP request type, such as GET or POST.
// url: The URL of the server program.
// asynch: Whether to send the request asynchronously or not.
// ----------------------------------------

function httpRequest(reqType, url, asynch, postdata, callback,errHandler)
{   
    var request = null;
    if (window.XMLHttpRequest)
    {
        // If IE7, Mozilla, Safari, and so on: Use native object.
        request = new XMLHttpRequest();   
    }
    else if (window.ActiveXObject)
    {
        // ...otherwise, use the ActiveX control for IE5.x and IE6.
        request = new ActiveXObject('MSXML2.XMLHTTP.3.0');
    }
     if (!request)
     {
        alert("Your browser does not permit the use of all of this application's features!");
     }

    try
    {
        request.open(reqType, url, asynch);
       
        request.onreadystatechange = function()
                {        
                    if (request.readyState == 4)
                    {
                        if (request.responseText.indexOf("SERVISE_ERROR") != -1)
                        {
                            alert("oops.. Services operation failure.");
                            return;
                        }
                        
                        callback(request);                        
                    }
                };

       
        if (reqType.toLowerCase() == "post")
        {
            request.setRequestHeader("Content-Type", "application/x-ww-form-urlencoded; charset=UTF-8");
            request.setRequestHeader("Content-length", postdata.length);
            request.setRequestHeader("Connection", "close");
            request.send(postdata); //lorem=ipsum&name=binny"
        }
        else
        {
            request.send(null);
            
            if(BrowserDetect.browser=='Firefox' && !asynch){
                callback(request); 
            }
         }
    }
    catch (ex)
    {       
        alert("The application cannot contact the server at the moment. Please try again in a few seconds.\n" +
			"Error detail: " + ex.message);
		errHandler();
    }
}//httpRequest.function

// ===========================================================
// XML Parse =================================================
// ===========================================================

function parseXML(XmlString)
{
    var xmlDoc = null;

    try
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(XmlString);
        return xmlDoc;
    }
    catch (e)
    {
        try
        {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(XmlString, "text/xml");
            return xmlDoc;
        }
        catch (e)
        {
            alert("parseXML:"+e.message);
        }
    }
}
