/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------REQUEST MOOTOOLS     */



function Call(url,elementOut){    
    var urlencoded=encodeURL(url);
    $j.ajax  
    ({
                url: urlencoded["url"]+"?"+urlencoded["params"],
                cache: false,
                dataType: "html",
                type: "GET", 
                beforeSend:function()
                { 
                        mini_comparsa();           
                },
                success:function(html,textStatus)
                {
                        var response = parseScript(html);
                    
                        if ( elementOut == 'PopNotify') { Notify('open',response[0]);  }
                        else if ( elementOut == 'NoNotify') {  }
                        else if ( elementOut ) 
                        { 
                            var e = document.getElementById(elementOut); 
                            e.innerHTML = response[0];                                                 
                        };
                        
                        close_mini_comparsa();
                        
                        if (response[1]){
                            
                            if (window.execScript){
                                window.execScript(response[1]);
                            } else {    
                                var script = document.createElement('script');
                                script.setAttribute('type', 'text/javascript');
                                script[(Browser.Engine.webkit && Browser.Engine.version < 420) ? 'innerText' : 'text'] = response[1];
                                document.head.appendChild(script);
                                document.head.removeChild(script);
                            }
                        }
                         
                }                 
    });
}

function CallForm(url,form,elementOut){
    var urlencoded = encodeURL(url);
    $j.ajax  
    ({
                url: urlencoded["url"]+"?"+urlencoded["params"]+"&"+encodeFORM(form),
                cache: false,
                dataType: "html",
                type: "GET",
                beforeSend:function()
                { 
                        mini_comparsa();           
                },
                success:function(html,textStatus)
                {
                        var response = parseScript(html);
                    
                        if ( elementOut == 'PopNotify') { Notify('open',response[0]);  }
                        else if ( elementOut == 'NoNotify') {  }
                        else if ( elementOut ) 
                        { 
                            var e = document.getElementById(elementOut);
                            e.innerHTML = response[0];                                               
                        };
                        
                        close_mini_comparsa();
                        
                        if (response[1]){
                            
                            if (window.execScript){
                                window.execScript(response[1]);
                            } else {
                                var script = document.createElement('script');
                                script.setAttribute('type', 'text/javascript');
                                script[(Browser.Engine.webkit && Browser.Engine.version < 420) ? 'innerText' : 'text'] = response[1];
                                document.head.appendChild(script);
                                document.head.removeChild(script);
                            }
                        }
                }                 
    });
}

function CallFormPost(url,idform,elementOut){
    var urlencoded = encodeURL(url);
    $j.ajax  
    ({
                url: urlencoded["url"]+"?"+urlencoded["params"],
                cache: false,
                dataType: "html",
                type: "POST",
                data: $j('#'+idform).serialize(),
                beforeSend:function()
                { 
                        mini_comparsa();           
                },
                success:function(html,textStatus)
                {
                        var response = parseScript(html);
                    
                        if ( elementOut == 'PopNotify') { Notify('open',response[0]);  }
                        else if ( elementOut == 'NoNotify') {  }
                        else if ( elementOut ) 
                        { 
                            var e = document.getElementById(elementOut); 
                            e.innerHTML = response[0];                                                 
                        };
                        
                        close_mini_comparsa();
                        
                        if (response[1]){
                            
                            if (window.execScript){
                                window.execScript(response[1]);
                            } else {
                                var script = document.createElement('script');
                                script.setAttribute('type', 'text/javascript');
                                script[(Browser.Engine.webkit && Browser.Engine.version < 420) ? 'innerText' : 'text'] = response[1];
                                document.head.appendChild(script);
                                document.head.removeChild(script);
                            }
                        }
                         
                }                 
    });

}


function encodeURL(url)
	{
		var ArrayParam = new Array();
		var ArrayParams = new Array();
		var string = null;
		
		if (url.indexOf("?") == '-1') url = url+'?';
		
		var ArrayAllParams = url.split("?");
		var ArrayCV = ArrayAllParams[1].split("&");
		
		for (var i=0; i < ArrayCV.length; i++)
		{
			if (ArrayCV[i])
			{
				ArrayParam = ArrayCV[i].split("=");
				ArrayParams[i] = ArrayParam[0]+"="+encodeURIComponent(ArrayParam[1]);
			};
		};
		string = ArrayParams.join("&");
		
		var ReturnArray = new Array();
		ReturnArray["url"] = ArrayAllParams[0];
		ReturnArray["params"] = string;
		
		
		return ReturnArray;
	};

function encodeFORM(form)
	{
			var firstArray = new Array();
				
				for (var i=0; i < form.length; i++)
					{
						if (form[i].name)
						{
							firstArray[i] = "FORM"+form[i].name+"="+encodeURIComponent(form[i].value);
						};
					};
				
				var string = firstArray.join("&");
				
				return string;
	};
    
function parseScript(_source)
{       
        var source = _source;
        var scripts = new Array();
        
 
        // Strip out tags
        while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
            var s = source.indexOf("<script");
            var s_e = source.indexOf(">", s);
            var e = source.indexOf("</script", s);
            var e_e = source.indexOf(">", e);
             
            // Add to scripts array
            scripts.push(source.substring(s_e+1, e));
            // Strip from source
            source = source.substring(0, s) + source.substring(e_e+1);
        }
        
        // Loop through every script collected and eval it
        var javascript = '';
        for(var i=0; i<scripts.length; i++) {
            try {
                //alert('eseguo '+ scripts[i]);
                //eval(scripts[i]);
                //$j('script').insertAfter('head > script:first');
                //$j('head > script:last').attr({type : "text/javascript"});
                //$j('head > script:last').text(scripts[i]);
                javascript += scripts[i]+";"; 
            }
            catch(ex) {
                // do what you want here when a script fails
                alert('ERROR ON EXECUTION SCRIPT :: '+scripts[i]);
            }
        }
        // Return the cleaned source
        var response = new Array();
        response.push(source);
        response.push(javascript);
        return response;
}


/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------GESTIONE DELLE RISPOSTE     */

 function Notify(Action,responseHTML){
    
    if (Action == 'open')
    {
        document.getElementById('notify').innerHTML = responseHTML;
        document.getElementById('notify').style.display = 'block';
        document.getElementById('notify').style.top = 30 + document.body.scrollTop + document.documentElement.scrollTop + 'px'; 
        Overlay('open');
    }                              
    else if ( Action == 'close' )
    {

        document.getElementById('notify').style.display='none';
        Overlay('close'); 
        
    };
}



function Notify2(Action,responseHTML)
{    
    if (Action == 'open')
    {
         document.getElementById('notify2').style.top = -100 + document.body.scrollTop + document.documentElement.scrollTop;
         document.getElementById('notify2').innerHTML = responseHTML;
         Overlay('open');
    } 
    else if ( Action == 'close' )
    {
         document.getElementById('notify').style.display='none';
         Overlay('close');
        
    }
    else if ( Action == 'closeNOFX' )
    {
                document.getElementById('notify').style.top = -100 + document.body.scrollTop + document.documentElement.scrollTop;
                document.getElementById('notify').style.display='none';
                Overlay('close'); //alert('closeNOFX');
    }
    else if ( Action == '' )
    {
                document.getElementById('notify').style.top = -100 + document.body.scrollTop + document.documentElement.scrollTop;
                document.getElementById('notify').style.display='none';
                Overlay('close'); //alert('close action=0');
    };
    
}


function Overlay(Action){
        if (Action=='open')
        {
            document.getElementById('overlay').style.display='block';
            document.getElementById('opacity').style.display='block';
        }
        else if (Action=='close')
        {
            document.getElementById('overlay').style.display='none';
            document.getElementById('opacity').style.display='none';
        };
}

/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------GESTIONE DELLE ATTESE     */

function Test(e) {
	var posx;
	var posy;
	
	//if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	
	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	/*x = document.getElementById('x_cursor');
	y = document.getElementById('y_cursor');
	x.innerHTML = posx;
	y.innerHTML = posy;*/
	move_mini_comparsato(posy+15,posx+20);
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
}

function move_mini_comparsato(y,x) {
	e=document.getElementById('mini_comparsa');
	e.style.top=y+"px";
	e.style.left=x+"px";
}

function mini_comparsa(){
	e=document.getElementById('mini_comparsa');
	e.style.display='block';
};

function close_mini_comparsa()
{
	e=document.getElementById('mini_comparsa');
	e.style.display='none';
};

function nonfareniente()
{
};
