var webbins_history = new Array();
var webbins_history_counter = 0;
var webbins_history_pointer = 0;

function folder_upload2ftp_completed(output_div){
	document.getElementById(output_div).innerHTML = "<h1 class=\"avviso\">file caricato</h1>";;
}
/**
 * 
 * @param {Object} transport
 * @param {Object} output_div � il div in cui verr� stampato il form di upload
 * @param {Object} dest_upload � l'url a cui dovr� essere fatta l'operazione di post
 */
function folder_upload_completed(transport,output_div,iframe_src){
	
	risposta=transport.responseXML;
	// alert(transport.responseText);
	
	// verifico se la richiesta � andata a buon fine
	var operazione_eseguita = risposta.getElementsByTagName('operazione_eseguita');
	
	if(operazione_eseguita.item(0).firstChild.nodeValue=="OK"){
		var htmlresult = "<iframe frameborder=\"0\" src=\""+iframe_src+"\" height=\"400\" width=\"600\"></iframe>";
		document.getElementById(output_div).innerHTML = htmlresult;
		
	}else{

		var errore = risposta.getElementsByTagName('errore').item(0).firstChild.nodeValue;
		document.getElementById(output_div).innerHTML = "<h1 class=\"errore\">"+errore+"</h1>";
		
	}

}

function webbins_navigazione_aggiornaclassi(){
	/*
	* provo a cambiare lo stile del menu navigazione
	*/
	try{
		
		if(webbins_history_pointer > 0){
			document.getElementById("webbins_navigazione_indietro").className="navigazione_attiva_sx";
		}else{
			document.getElementById("webbins_navigazione_indietro").className="navigazione_disattiva_sx";
		}

		if(webbins_history_pointer < webbins_history_counter){
			document.getElementById("webbins_navigazione_avanti").className="navigazione_attiva_dx";
		}else{
			document.getElementById("webbins_navigazione_avanti").className="navigazione_disattiva_dx";
		}
		
	}catch(e){
		// non faccio nulla	
	}
}

function webbins_navigazione(direzione)
{

	// alert("PRIMA counter = "+webbins_history_counter+" pointer attuale = "+webbins_history_pointer);
	
	if(direzione=="indietro"){
		
		if(webbins_history_pointer > 0){
			
			if (webbins_history[webbins_history_pointer] == undefined){
				webbins_history_pointer--;
				webbins_navigazione(direzione);
			}else{
				document.getElementById("div_module").innerHTML = webbins_history[webbins_history_pointer];
				webbins_history_pointer--;
			}
		}		
	}

	if(direzione=="avanti"){

		if(webbins_history_pointer < webbins_history_counter){
			if (webbins_history[webbins_history_pointer] == undefined){
				webbins_history_pointer++;
				webbins_navigazione(direzione);
			}else{
				document.getElementById("div_module").innerHTML = webbins_history[webbins_history_pointer];
				webbins_history_pointer++;
			}
		}

	}
	
	// alert("DOPO counter = "+webbins_history_counter+" pointer attuale = "+webbins_history_pointer);
	webbins_navigazione_aggiornaclassi();
	
	//direzione deve essere un url=>usato nel caso di no ajax
	if(webbins_history_counter==0&&direzione!="")
	{
		window.location = direzione;
	}

}


function execJS(t)
{
    var p1 = 0, p2 = 0, p3 = 0, p4 = 0;
    p1 = t.indexOf("<" + "script", 0);
    if(p1 == -1) return t;
    
    p2 = t.indexOf(">", p1 + 7) + 1;
    p3 = t.indexOf("<" + "/script>", p2);
    p4 = p3 + 9;
    
    var c = t.substring(p2, p3);
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.text = c;
    document.getElementsByTagName("head")[0].appendChild(s);
    
    t = t.substring(0, p1) + t.substr(p4);
    return execJS(t);

}

	function salva_form_POST(mioform,output){

		if(mioform.id === undefined){
			mioform.id = "idrandom"+Math.random();			
		}
		if(mioform.id.length == 0){
			mioform.id = "idrandom"+Math.random();
		}

		var input =document.getElementById("webbins_disabilita_template");
		
		if(input==null){
			input = document.createElement("input");
			input.setAttribute('type',"hidden");
			input.setAttribute('name',"webbins_disabilita_template");
			input.value="true";
			mioform.appendChild(input);
		}
		
		try{
			
			for(var i=0;i<mioform.length;i++){
				
				if(mioform[i].id!==undefined){
					
					// se true significa che questo elemento è un tinumce
					if(tinyMCE.get(mioform[i].id)!==undefined){
						
						// assegno al campo del form il valore di tinymce 
						mioform[mioform[i].name].value=tinyMCE.get(mioform[i].id).getContent();
						//alert(tinyMCE.get(mioform[i].id).getContent());
						//alert("Ok "+mioform[i].name+" è fatto con tinymce");
					}
				}
				
			}
		}catch(ex){
		//	alert("Errore durante l'onsubmit:"+ex);
		}
		
		new Ajax.Request(
		mioform.action, 
		{
		  method: 'POST',
		  parameters: $(mioform.id).serialize(true),
		  onSuccess: function(transport) {
			  document.getElementById(output).innerHTML=transport.responseText;		   
			  input.value="false";
			  transport.responseText.evalScripts(); 
		  },
		  onLoading: function() {
			   document.getElementById(output).innerHTML='<div class=\"wait\"><img src=\"http://' + document.location.hostname + '/files/getbyname/wait.gif\" /></div>';		   
		  }
		});
		
	}

	/*
	 * nmosca
	 * in data 2009-02-24
	 * aggiorno la makeRequest mettendo una chiamata a ProtoType
	 * questo perch� ho visto che nell'utilizzo di EditArea usando la nostra makerequest funzionava solo alla prima richiesta Ajax
	 * probabilmente il metodo evalJS da qualche problema, mentre in prototype � implementato pi� stabilmente
	 * il tutto viene messo in un try catch, se la chiamata non va a buon fine viene richiamata la makerequest_old
	 * */
	function makeRequest_new(id_div_output,notify_waiting,url){
		
		try {
		
			if (id_div_output == 'div_ajax') {
				id_div_output = 'div_module';
			}
			
			// salvo nella cronologia il contenuto del div
			webbins_history_pointer = webbins_history_counter;
			
			webbins_navigazione_aggiornaclassi();
			
			// provo a salvare l'history, se div_module non esiste genero un'eccezione che comunque non gestisco
			try {
				webbins_history[webbins_history_counter++] = document.getElementById("div_module").innerHTML;
			} 
			catch (eccezione) {
			
			}
			
			new Ajax.Request(url, {
				method: 'get',
				evalJS: true,
				onLoading: function(){
					if (notify_waiting) {
						document.getElementById(id_div_output).innerHTML = '<div class=\"wait\"><img src=\"http://' + document.location.hostname + '/files/getbyname/wait.gif\" /></div>';
					}
				},
				onSuccess: function(transport){
					document.getElementById(id_div_output).innerHTML = transport.responseText;
					transport.responseText.evalScripts();
				}
			});
		} 
		catch (exception_makerequest) {
			makeRequest_old(id_div_output, notify_waiting, url);
		}
			
		try
		{
			if(pageTracker!=null)
			{
			
				var pos1  =url.indexOf('AJAX/');
				pos1=pos1+5;
				var url2= url.substr(pos1,url.length);
				//alert(url2);
				pageTracker._trackPageview(url2);
			}
		}
		catch (ex) 
		{
			//alert(ex);
		}
	}
	
	/**
	 * rinominata da makeRequest a makeRequest_old in data 2009-02-24
	 * @param {Object} id_div_output
	 * @param {Object} notify_waiting
	 * @param {Object} url
	 */
    function makeRequest(id_div_output,notify_waiting,url) {

		if(id_div_output=='div_ajax'){
			id_div_output='div_module';
		}

		//alert(url);
        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }

		// salvo nella cronologia il contenuto del div
		webbins_history_pointer = webbins_history_counter;
		
		webbins_navigazione_aggiornaclassi();

		// provo a salvare l'history, se div_module non esiste genero un'eccezione che comunque non gestisco
		try{
			webbins_history[webbins_history_counter++]=document.getElementById("div_module").innerHTML;
		}catch(eccezione){
		
		}

		if(notify_waiting){
			document.getElementById(id_div_output).innerHTML='<div class=\"wait\"><img src=\"http://'+document.location.hostname+'/files/getbyname/wait.gif\" /></div>';
		}
		//alert(url);
		try{
        http_request.onreadystatechange = function() { alertContents(http_request,id_div_output); };
        http_request.open('GET', url, true);
        http_request.send(null);
		}catch(eccezione)
		{
		 //
		}
		
		
		try
		{
			if(pageTracker!=null)
			{
			
				var pos1  =url.indexOf('AJAX/');
				pos1=pos1+5;
				var url2= url.substr(pos1,url.length);
				//alert(url2);
				pageTracker._trackPageview(url2);
			}
		}
		catch (ex) 
		{
			//alert(ex);
		}
    }

    function alertContents(http_request,output) {
	try
	{
		if (http_request != null) {
			if (http_request.readyState == 4) {
				if (http_request.status == 200) {
					risposta = http_request.responseText;
					//alert(risposta);
					
					document.getElementById(output).innerHTML = risposta;
					execJS(risposta);
				//standardistaTableSortingInit();
				}
				/*else {
					alert('There was a problem with the request.');
				}*/
			}
		}
	}
	catch(e)
	{
		//
	}
    }

    function makeRequest_forceoutput(id_div_output,url) {


		//alert(url);
        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }

		//alert(url);
        http_request.onreadystatechange = function() { alertContents_forceoutput(http_request,id_div_output); };
        http_request.open('GET', url, true);
        http_request.send(null);

    }

    function alertContents_forceoutput(http_request,output) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
			risposta=http_request.responseText;
			//alert(output);
			output.innerHTML+=risposta;
			//output.innerHTML+="prova nicola";
			execJS(risposta);
            } 
			/*else {
                alert('There was a problem with the request.');
            }*/
        }

    }
 

    function makeRequest_doafter(id_div_output,notify_waiting,url,funzione) {

		if(id_div_output=='div_ajax'){
			id_div_output='div_module';
		}

        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }

		if(notify_waiting){
			document.getElementById(id_div_output).innerHTML='<div class=\"wait\"><img src=\"http://'+document.location.hostname+'/files/getbyname/wait.gif\" /></div>';
		}
		//alert(url);
        http_request.onreadystatechange = function() { alertContents_doafter(http_request,id_div_output,funzione); };
        http_request.open('GET', url, true);
        http_request.send(null);

    }

    function alertContents_doafter(http_request,output,funzione) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
			risposta=http_request.responseText;
			//alert(output);
			document.getElementById(output).innerHTML=risposta;
			funzione();
            } else {
                alert('There was a problem with the request.');
            }
        }

    }
    
    
    // questa funzione ritorna la risposta
    function makeRequest_settiny(url,tiny) {


		// alert(url);
        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
		
        http_request.onreadystatechange = function() { risultato = alertContents_settiny(http_request,tiny); };
        http_request.open('GET', url, true);
        http_request.send(null);
        

    }


    function alertContents_settiny(http_request,tiny) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
				tiny.setContent(http_request.responseText);

            } else {
                alert('There was a problem with the request.');
            }
        }

    }       