var xhr1 = null;
var xhr2 = null;
var usedObject = null;

function getXhr(i){

    var usedObject = eval('xhr'+i);

	// Firefox and others
    if(window.XMLHttpRequest){

    	usedObject = new XMLHttpRequest();

    }else if (window.ActiveXObject){  // Internet Explorer

		try{

			usedObject = new ActiveXObject("Msxml2.XMLHTTP");

        }catch(e){

            usedObject = new ActiveXObject("Microsoft.XMLHTTP");

        }

    }else{

    	usedObject = false;

    }

    return usedObject;

}


function GetAjaxRequest(sMethod, sUrl, sTargetElementID, calledFunction){

    xhr_ojbect = getXhr(1);

    // When there is an answer
    xhr_ojbect.open(sMethod, sUrl, true);
    if(sMethod == 'POST'){

    	xhr_ojbect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
       	xhr_ojbect.send(sUrl);

    }else{

    	xhr_ojbect.send(null);

    }   
    
    xhr_ojbect.onreadystatechange = function(){

        // If everything is ok
        if (xhr_ojbect.readyState == 4 && xhr_ojbect.status == 200){

            document.getElementById(sTargetElementID).innerHTML = xhr_ojbect.responseText;

            if(calledFunction != null){

            	eval(calledFunction);

            }
            
            //self.location.href= "#test";

        }

    }

    //xhr_ojbect.send(null);

}

// Function to call 2 ajax script
function Get2AjaxRequest(sMethod_1, sUrl_1, sTargetElementID_1, calledFunction, sMethod_2, sUrl_2, sTargetElementID_2){

    xhr_ojbect = getXhr(1);

    // When there is an answer
    xhr_ojbect.open(sMethod_1, sUrl_1, true);
    xhr_ojbect.onreadystatechange = function(){

        // If everything is ok
        if (xhr_ojbect.readyState == 4 && xhr_ojbect.status == 200){

            document.getElementById(sTargetElementID_1).innerHTML = xhr_ojbect.responseText;

            if(calledFunction != null){

            	eval(calledFunction);

            }
            
            xhr_ojbect_2 = getXhr(2);

            xhr_ojbect_2.open(sMethod_2, sUrl_2, true);
            xhr_ojbect_2.onreadystatechange = function(){

		        // If everything is ok
		        if (xhr_ojbect_2.readyState == 4 && xhr_ojbect_2.status == 200){

		        	document.getElementById(sTargetElementID_2).innerHTML = xhr_ojbect_2.responseText;

		        }

            }
            xhr_ojbect_2.send(null);

		}

    }

    xhr_ojbect.send(null);

}

// Function to use Ajax to control via PHP
function GetAjaxControlRequest(sMethod, sUrl, param){

    xhr_ojbect = getXhr(1);

    // When there is an answer
    xhr_ojbect.open(sMethod, sUrl, false);
   	if(sMethod == 'POST'){

    	xhr_ojbect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
       	xhr_ojbect.send(param);

    }else{

    	xhr_ojbect.send(null);

    }

    // If everything is ok
    if (xhr_ojbect.readyState == 4){

        return xhr_ojbect.responseText;

    }else{

    	return false;

    }

}
