startMenu = function() {
if (document.all&&document.getElementById) {
cssmenu = document.getElementById("csstopmenu");
for (i=0; i<cssmenu.childNodes.length; i++) {
node = cssmenu.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
}
node.onmouseout=function(){                  
this.className=this.className.replace(" over", "")
}
}
}
}
}

if (window.attachEvent)
window.attachEvent("onload", startMenu)
else
window.onload=startMenu;

/** FUNZIONI */


    //AJAX V 0.2
/*
La funzione accetta come argomenti:
	- nomeFile: nome del file da chiamare (in genere .php, .asp), dovrà restituire delle informazioni (testo, html).
	- fcallback: nome della funzione che processerà il valore restituito dal file nomeFile
	- asincrono: indica se la chiamata al server deve essere di tipo sincrono (false) o asincrono (true) 
	- metodo: get o post
	- poststring: parametri
	- returnAjax : ajax può ritornare un testo o un xml (bisogna passargli una stringa 'xml' par fargli ritornare un xml
*/


function callServer(nomeFile, fcallback, asincrono, metodo, poststring,returnAjax)
{
	// assegnazione oggetto XMLHttpRequest
	var ajax = assegnaXMLHttpRequest();

    var ret = null;
	// assegnazione elemento del documento
	//elemento = prendiElementoDaId("grid"),
	// risultato booleano di funzione
	var usaLink = true;



	// se l'oggetto XMLHttpRequest non è nullo
	if(ajax)
	{
    	// il link al file non deve essere usato
    	usaLink = false;

		// impostazione richiesta asincrona in GET
		// del file specificato

        if(metodo == undefined)
            metodo = 'get';
		
		//aggiunta per gestire ritorno in xml
		if(returnAjax == undefined)
			returnAjax='text';
		
		ajax.open(metodo, nomeFile, asincrono);

		// rimozione dell'header "connection" come "keep alive"
		// "close" su windows sembra introdurre tempi di latenza biblici
		ajax.setRequestHeader("connection", "keep alive");


        if(asincrono===true)
		// impostazione controllo e stato della richiesta
		{
		    ajax.onreadystatechange = function()
		    {

			    // verifica dello stato
			    if(ajax.readyState === readyState.COMPLETATO)
			    {
				    // operazione avvenuta con successo
				      //elemento.innerHTML = ajax.responseText;
				    if(fcallback != null)
				    {
				  	    //aggiunta per gestire ritorno in xml
				  	    if(returnAjax=='text')
				  		    //fcallback( ajax.responseText );
				  	        ret = ajax.responseText;
    				  	
				  	    else
				  		    //fcallback(ajax.responseXML);
				  		    ret = ajax.responseXML;
				    }
				    else
				    {
				      // errore di caricamento
				      alert('Errore Ajax: '+statusText[ajax.status] );
				      //elemento.innerHTML = "Impossibile effettuare l'operazione richiesta.<br />";
				      //elemento.innerHTML += "Errore riscontrato: " + statusText[ajax.status];
				    }
			    }
		    }
        }
		
		var ret = null;
		
		if(metodo == 'get')
		{
			// invio richiesta
			ret = ajax.send(null);
			
			    
		}
		else
		{
			ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
            ajax.send(poststring);
		}
        
        if(ajax.readyState === readyState.COMPLETATO)
			    {
				    // operazione avvenuta con successo
				      //elemento.innerHTML = ajax.responseText;
				    if(fcallback != null)
				    {
				  	    //aggiunta per gestire ritorno in xml
				  	    if(returnAjax=='text')
				  		    
				  	        ret = ajax.responseText;
    				  	
				  	    else
				  		    
				  		    ret = ajax.responseXML;
				    }
				    else
				    {
				      // errore di caricamento
				      alert('Errore Ajax: '+statusText[ajax.status] );
				      //elemento.innerHTML = "Impossibile effettuare l'operazione richiesta.<br />";
				      //elemento.innerHTML += "Errore riscontrato: " + statusText[ajax.status];
				    }
        
	            }

	    return ret;
	}
}



	// funzione per prendere un elemento con id univoco
		function prendiElementoDaId(id_elemento) {
			var elemento;
			if(document.getElementById)
				elemento = document.getElementById(id_elemento);
			else
				elemento = document.all[id_elemento];
			return elemento;
		};
	
	// funzione per assegnare un oggetto XMLHttpRequest
		function assegnaXMLHttpRequest() {
			var
				XHR = null,
				browserUtente = navigator.userAgent.toUpperCase();
			if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
				XHR = new XMLHttpRequest();
			else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
				if(browserUtente.indexOf("MSIE 5") < 0)
					XHR = new ActiveXObject("Msxml2.XMLHTTP");
				else
					XHR = new ActiveXObject("Microsoft.XMLHTTP");
			}
			return XHR;
		};





	// oggetto di verifica stato
		var readyState = {
			INATTIVO:	0,
			INIZIALIZZATO:	1,
			RICHIESTA:	2,
			RISPOSTA:	3,
			COMPLETATO:	4
		};

	// array descrittivo dei codici restituiti dal server
	// [la scelta dell' array è per evitare problemi con vecchi browsers]
		var statusText = new Array();
		statusText[100] = "Continue";
		statusText[101] = "Switching Protocols";
		statusText[200] = "OK";
		statusText[201] = "Created";
		statusText[202] = "Accepted";
		statusText[203] = "Non-Authoritative Information";
		statusText[204] = "No Content";
		statusText[205] = "Reset Content";
		statusText[206] = "Partial Content";
		statusText[300] = "Multiple Choices";
		statusText[301] = "Moved Permanently";
		statusText[302] = "Found";
		statusText[303] = "See Other";
		statusText[304] = "Not Modified";
		statusText[305] = "Use Proxy";
		statusText[306] = "(unused, but reserved)";
		statusText[307] = "Temporary Redirect";
		statusText[400] = "Bad Request";
		statusText[401] = "Unauthorized";
		statusText[402] = "Payment Required";
		statusText[403] = "Forbidden";
		statusText[404] = "Not Found";
		statusText[405] = "Method Not Allowed";
		statusText[406] = "Not Acceptable";
		statusText[407] = "Proxy Authentication Required";
		statusText[408] = "Request Timeout";
		statusText[409] = "Conflict";
		statusText[410] = "Gone";
		statusText[411] = "Length Required";
		statusText[412] = "Precondition Failed";
		statusText[413] = "Request Entity Too Large";
		statusText[414] = "Request-URI Too Long";
		statusText[415] = "Unsupported Media Type";
		statusText[416] = "Requested Range Not Satisfiable";
		statusText[417] = "Expectation Failed";
		statusText[500] = "Internal Server Error";
		statusText[501] = "Not Implemented";
		statusText[502] = "Bad Gateway";
		statusText[503] = "Service Unavailable";
		statusText[504] = "Gateway Timeout";
		statusText[505] = "HTTP Version Not Supported";
		statusText[509] = "Bandwidth Limit Exceeded";
	
		
/*
    CONTROLLO VALIDITA CAMPI
*/

/**
* isCampoValido(value,tipo,required,zero)
* Restituisce true se il campo e' valido, false altrimenti
* value: valore da analizzare
* tipo: tipo del valore (text, float)
* required: true se obbligatorio
* zero: true se puo' essere uguale a 0
*/
function isCampoValido(value,tipo,required,zero) {
	// Campo obbligatorio?
	if (required) if (value=="") return false;

	// Valore corretto?
	switch (tipo) {
		case "text":
		return true;
		break;
		
		case "float":
		if (value=="") value=0;
		else {
			var v=parseFloat(value);
			if (isNaN(v)) return false;
		}
		if (!zero) if (v==0) return false;
		return true;
		break;
		
		case "partitaiva":
		if (value.length!=11) return false;
		var v=parseFloat(value);
		if (isNaN(v)) return false;
		return true;
		break;
		
		case "data":
		_pattern=/^(((((0[1-9])|(1\d)|(2[0-8]))\/((0[1-9])|(1[0-2])))|((31\/((0[13578])|(1[02])))|((29|30)\/((0[1,3-9])|(1[0-2])))))\/(([0-9][0-9][0-9][0-9]))|(29\/02\/[0-9][0-9](([02468][048])|([13579][26]))))$/;
		return _pattern.test(value);
		break;
		
		case "perc":
		if (value=="") value=0;
		else {
			var v=parseFloat(value);
			if (isNaN(v)) return false;
			if (v<0) return false;
			if (v>100) return false;
		}
		if (!zero) if (v==0) return false;
		return true;
		break;
		
		case "email":
		var re = new RegExp(regex_email);
		if(value=="") return true;
		else return value.match(re);
		break;
	}
}

/**
* checkCampo(label,field,tipo,required,zero)
* Restituisce il messaggio d'errore relativo al valore del campo (vuoto se il campo e' valido)
* label: label del campo
* (per gli altri parametri vedere isCampoValido)
*/
function checkCampo(label,field,tipo,required,zero) {
	// Sistemazione parametri
	if (zero==undefined) zero=true;

	// Controllo
	if (!isCampoValido(field.value,tipo,required,zero)) {
		// Costruzione dell'eventuale messaggio d'errore
		errorMsgNew="\n";
		switch(tipo) {
			case "text":
			errorMsgNew+=" - "+label+"";
			break;
			
			case "float":
			errorMsgNew+="un valore numerico ";
			if (zero==false) errorMsgNew+="maggiore di zero ";
			errorMsgNew+="in <"+label+">";
			break;
			
			case "partitaiva":
			errorMsgNew+="un valore valido in <"+label+"> (11 cifre)";
			break;

			case "data":
			errorMsgNew+="una data valida (gg/mm/aaaa) in <"+label+">";
			break;
		
			case "perc":
			errorMsgNew+="un numero compreso tra 0 e 100";
			break;
		
			case "email":
			errorMsgNew+="un valore corretto in <"+label+">";
			break;	
		}
		return errorMsgNew;
	} else
	return "";
}

function generalCheckForm(errorMsg, errorPre) {
  	if (errorMsg!="") {
		alert(errorPre + errorMsg);
		return false;
	} else
	return true;
}



function download()
{
    //var ret = callServer("downloadcontroller.asp", ajaxcallback_download, false);
    //alert();
    var ret = callServer("downloadcontroller.asp", ajaxcallback_download, false);
    
    //ret = 0;
    
    if(ret=='1')
        return true;
    else
    {  
      
        var userAgent = navigator.userAgent.toLowerCase();
        var is_ie = ((userAgent.indexOf('msie') != -1));
        if(is_ie)
        {
            var a = document.getElementById("downloadform");
            a.click();
        }
        else
            self.location = "downloadform.asp";

      
       return false;
    }
    
}

function ajaxcallback_download(msg)
{
    alert(msg);
        
}


function test()
{
    //document.location.href;
  //alert("dd"); 
  var ret = callServer("downloadcontroller.asp", ajaxcallback_download, false); 
  alert(ret);
  
}


function fireEvent(obj,evt)
{

    var fireOnThis = obj;
    if(document.createEvent) 
    {
        var evObj = document.createEvent('MouseEvents');
        evObj.initEvent( evt, true, false );
        fireOnThis.dispatchEvent(evObj);
    } 
    else if( document.createEventObject ) 
    {
        fireOnThis.fireEvent('on'+evt);
        //fireOnThis.fireEvent('onclick');
    }

}


function recuperaContatto(idLingua, combo, idDivisione)
{
    var idNazione = combo.options[combo.selectedIndex].value;    
    
    //alert(idNazione);
    var ret = callServer("ajax_recuperaContatto.asp?idL="+ idLingua+"&idN="+idNazione+"&idF="+idDivisione, ajaxcallback_download, false); 

    //alert(ret);
    var area = document.getElementById("contact");
    area.innerHTML = ret;
    
    

}

/**
 * Script utilizzato per cambiare la lingua attiva
 */ 
function cVersion(vers)
{
   var sVersion = vers;
   document.VersionSetting._SetCurrentVersion.value=sVersion;
   document.VersionSetting.submit();
}

