function getHTTPObject() {
    try{
        if (window.XMLHttpRequest)
            return new XMLHttpRequest();
        else if (window.ActiveXObject)
            return new ActiveXObject("Msxml2.XMLHTTP");
        else
            throw ("XMLHttpRequest not supported");
    }catch(e)
    {
        alert("Ajax.getHTTPObject(): "+e);
        return false;
    }
}
function is_ajax_error(xml_result,sender)
{
    if( xml_result.getElementsByTagName("error").length > 0 )
    {
        if(xml_result.getElementsByTagName("validation").length > 0 )
            alert(xml_result.getElementsByTagName("validation")[0].firstChild.nodeValue);
        else
            window.location.href=_ERROR_PAGE_URL+
            "?message="+xml_result.getElementsByTagName("error")[0].firstChild.nodeValue;
		
        return true;
    }
	
    return false;
	
}
function process_ajax_call(params,url,handler)
{
    try{
        var http_req=getHTTPObject();
        http_req.open("POST",url,true );
        http_req.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded; charset=UTF-8");
        http_req.onreadystatechange =function()
        {
            if(http_req.readyState == 4 )
            {
                if(http_req.status == 200)
                {
                    if( !is_ajax_error(http_req.responseXML,this) )
                        handler.call(http_req,false);
                    else
                        handler.call(http_req,true);
                }
                else
                    alert("Errore ajax connesione: "+http_req.responseText);
            }
			
        }
        http_req.send(params);
    }catch(e)
    {
        alert("Ajax.process_ajax_call(): "+e);
        return false;
    }
}

function process_ajax_sync_call(params,url,handler)
{
    try{
        var http_req=getHTTPObject();
        http_req.open("POST",url,false );
        http_req.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded; charset=UTF-8");
        http_req.send(params);
        if(http_req.readyState == 4 )
        {
            if(http_req.status == 200)
            {
                //alert(http_req.responseText);
                if( !is_ajax_error(http_req.responseXML,this) )
                    handler.call(http_req,false);
                else
                    handler.call(http_req,true);
            }
            else
                alert(http_req.responseText);
        }
        else
            alert(http_req.responseText);
		
    }catch(e)
    {
        alert("Ajax.process_ajax_call(): "+e);
        return false;
    }
}

/**
 * Comment
 */
function execut_map_ajax_call(params,cntr_url) {
    loading_message(true);
    process_ajax_call( params,cntr_url,
        function(error){
            if(!error)
            {
                reset_extra_graphics();
                reload_map("type=map&action=load");
                loading_message(false);
                return true;
            }
            else
            {
                loading_message(false);
                return false;
            }
        } );
}
