// JavaScript Document
var XMLHTTP;
var conta=0;

function manina() {	
	document.body.style.cursor="hand";
	} 
	
function freccia() {
	document.body.style.cursor="default";
	}

function finestra(url,opt)
{
	  if (opt == 0)
		{// current window
			nomefile = getCheckedValue(document.all.id_totem)
			if(nomefile=='')
			{
				return
			}		
    	window.location = url+"?dati="+nomefile;
		}
  else if (opt == 1) // new window
	{
			nomefile = ""
    	window.location = url+"?dati="+nomefile;
	}
  else if (opt == 2) // background window
    {
			window.open(url); 
			self.focus();
		}
}

function ajaxFunction(QualeDiv,QualePagina,Attesa,Report,Parametro)
	{
		var dati = '';
		if (window.XMLHttpRequest)
			{
			// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
			}
		else
			{
			// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		//document.getElementById('testo').value=xmlhttp.readyState;
		if(Report=='vc')
		{
			dati = VerificaConfigurazione()
			
		}		
		if(Report=='login')
		{
			dati = document.getElementById('txtNome').value + "^" + document.getElementById('txtPassword').value
		}
		
		if(Report=='ver_iscrizione')
		{
			conta = 0
			dati = VerificaIscrizione()
			if(dati=="")
			{
				return
			}

			dati = document.getElementById('evento').value+"^"+dati
		}
		if(Report=='iscrizione')
		{
			conta = 0
			dati = document.getElementById('evento').value
		}
		if(Report=='mod_iscrizione')
		{
			conta = 0
			dati = getCheckedValue(id_gara)
		}
		if(Report=='paga_iscrizione')
		{
			conta = 0
			dati = getCheckedValue(id_gara)
		}

		if(Report=='canc_iscrizione')
		{
			if(confirm('Vuoi eliminare questa prenotazione ?'))
			{
				conta = 0
				dati = getCheckedValue(id_gara)
			}
			else
			{
				return false
			}
		}

		if(Report=='ver_macchina')
		{
			dati = VerificaMacchina()
			if(dati=="")
			{
				return
			}
		}
		
		if(Attesa=='a')
		{
			var elem=document.getElementById("attesa");
			if(elem.style.display="none")
			{
				elem.style.display="block";			
			}
			document.getElementById(QualeDiv).innerHTML = ""
			//document.getElementById('risultato').innerHTML = ""
		}

		xmlhttp.onreadystatechange=function()
		{

			if(xmlhttp.readyState==4)
				{
					//document.getElementById('testo').value=xmlhttp.responseText
					document.getElementById(QualeDiv).innerHTML = xmlhttp.responseText;
					if(Attesa=='a')
					{
						if(elem.style.display="block")
						{
							elem.style.display="none";
						}
					}
					if(Report=='mod_iscrizione')
					{
						
						if(document.all.cate.length == undefined)
						{
								if(document.all.cate.checked == true)
								{
									conta = conta + 1
								}
						}
						else
						{
							for (var i=0; i<document.all.cate.length; i++) 
							{
								if(document.all.cate[i].checked == true)
								{
									conta = conta + 1								
								}
							}
						}
					}					
				}
		}
		xmlhttp.open("GET",noCache(QualePagina+"?dati="+dati+Parametro),true);
		xmlhttp.send(null);
	}

function VerificaConfigurazione()
{
	utente = document.getElementById('utente').value
	password = document.getElementById('password').value
	nome = document.getElementById('nome').value
	cognome = document.getElementById('cognome').value
	email = document.getElementById('email').value
	paese = document.getElementById('paese').value
	email1 = document.getElementById('email').value
	id_utente = document.getElementById('id_utente').value
	
	parametro=utente+"^"+password+"^"+nome+"^"+cognome+"^"
	parametro=parametro+email+"^"+paese+"^"+email1+"^"+id_utente

	return parametro
}

function VerificaMacchina()
{
	var errore = 0
	motore = document.getElementById('motore').value
	categoria = document.getElementById('categoria').value
	marca_auto = document.getElementById('marca_auto').value
	marca_motore = document.getElementById('marca_motore').value
	marca_gomme = document.getElementById('marca_gomme').value
	radiocomando = document.getElementById('radiocomando').value
	frequenza = document.getElementById('frequenza').value
	trasponder = document.getElementById('trasponder').value
	if(motore=="")
	{
		errore = 1
		alert("Devi selezionare un tipo Motore")
	}
	if(categoria=="" && errore ==0)
	{
		errore = 1
		alert("Devi selezionare una Categoria")
	}
	if(radiocomando=="frequenza" && frequenza=="" && errore ==0)
	{
		errore = 1
		alert("Devi inserire la frequenza")
	}
	if(trasponder=="" && errore ==0)
	{
		errore = 1
		alert("Devi inserire un trasponder")
	}

	if(errore==0)
	{
		parametro=motore+"^"+categoria+"^"+marca_auto+"^"+marca_motore+"^"
		parametro=parametro+marca_gomme+"^"+radiocomando+"^"+frequenza+"^"+trasponder
	}
	else
	{
		parametro = ""
	}
	return parametro
}

function VerificaIscrizione()
{
	var errore = 0
	var categoria = new Array()
	categoria[0]=""
	categoria[1]=""
	categoria[2]=""
	var c_categoria = 0
	
	tipo_motore1 = document.getElementById('tipo_motore').value
	if(document.all.cate.length == undefined)
	{
			if(document.all.cate.checked == true)
			{
				categoria[0] = Mid(document.all.cate.value,1,InStr(document.all.cate.value,",")-1)
			}
	}
	else
	{
		for (var i=0; i<document.all.cate.length; i++) 
		{
			if(document.all.cate[i].checked == true)
			{
				categoria[c_categoria] = Mid(document.all.cate[i].value,1,InStr(document.all.cate[i].value,",")-1)
				c_categoria=c_categoria+1
			}
		}
	}
	marca_auto1 = document.getElementById('marca_auto').value
	modello_auto1 = document.all.modello_auto.value
	scala1 = document.all.scala.value
	trazione1 = document.all.trazione.value
	marca_motore1 = document.all.marca_motore.value
	modello_motore1 = document.all.modello_motore.value
	marca_pneumatici1 = document.all.marca_pneumatici.value
	modello_pneumatici1 = document.all.modello_pneumatici.value
	radiocomando1 = document.all.radiocomando.value
	frequenza1 = document.all.frequenza.value
	trasponder1 = document.all.trasponder.value
	valore_trasponder1 = document.all.valore_trasponder.value
	//lbl_posto = document.all.lbl_posto.value
	lbl_posto = "0"
	spesa_iscrizione1 = document.all.spesa_iscrizione.value	

	if(tipo_motore1=="-1")
	{
		errore = 1
		alert("Devi selezionare un tipo Motore")
	}
	if((categoria[0]=="" && categoria[1]=="" && categoria[2]=="") && errore ==0)
	{
		errore = 1
		alert("Devi selezionare almeno una Categoria")
	}
	if(scala1=="-1")
	{
		errore = 1
		alert("Devi selezionare una scala macchina")
	}
	if(trazione1=="-1")
	{
		errore = 1
		alert("Devi selezionare un tipo di trazione")
	}

	if(radiocomando1=="-1" && errore ==0)
	{
		errore = 1
		alert("Devi selezionare un tipo radiocomando")
	}
	
	if(radiocomando1=="frequenza" && frequenza1=="" && errore ==0)
	{
		errore = 1
		alert("Devi inserire la frequenza")
	}
	if(trasponder1=="-1" && errore ==0)
	{
		errore = 1
		alert("Devi selezionare un tipo trasponder")
	}

	if(trasponder1=="si" && valore_trasponder1=="" && errore ==0)
	{
		errore = 1
		alert("Devi inserire il valore del trasponder")
	}

/*
	if(lbl_posto=="" && errore ==0)
	{
		errore = 1
		alert("Devi selezionare un BOX")
	}
*/
	if(errore==0)
	{
		
		parametro = tipo_motore1+"^"+categoria[0]+"^"+categoria[1]+"^"+categoria[2]+"^"+marca_auto1+"^"
		parametro = parametro + modello_auto1+"^"+marca_motore1+"^"+modello_motore1+"^"+marca_pneumatici1+"^"
		parametro = parametro + modello_pneumatici1+"^"+radiocomando1+"^"+frequenza1+"^"+trasponder1+"^"
		parametro = parametro + valore_trasponder1+"^"+lbl_posto+"^"+spesa_iscrizione1+"^"+scala1+"^"+trazione1
	}
	else
	{
		parametro = ""
	}
	return parametro
}


function noCache(uri){return uri.concat(/\?/.test(uri)?"&":"?","noCache=",(new Date).getTime(),".",Math.random()*1234567)};

function caratteriok(e, goods)
{
	var key, keychar;
	key = getkey(e);
	if (key == null) return true;
	
	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();
	
	// check goodkeys
	if (goods.indexOf(keychar) != -1)
		return true;
	
	// control keys
	if ( key==null || key==0 || key==8 || key==9 
			|| key==13 || key==27 )
		 return true;
	
	// else return false
	return false;
}

function getkey(e)
{
	if (window.event)
		 return window.event.keyCode;
	else if (e)
		 return e.which;
	else
		 return null;
}

// Funzione che :
// ritorna il valore del check selezionato
// Ritorna un valore vuoto se non ci sono check selezionati , oppure
// non ci sono check visibili
function getCheckedValue(radioObj) 
{
	if(!radioObj)
	{
		return "";
	}
	var radioLength = radioObj.length;
	if(radioLength == undefined)
	{
		if(radioObj.checked)
		{
			return radioObj.value;
		}
		else
		{
			return "";
		}
	}
	else
	{
		for(var i = 0; i < radioLength; i++) 
		{
			if(radioObj[i].checked) {
				return radioObj[i].value;
			}
		}
	}
	return "";
}


function ctr(quale)
	{
		if (quale.checked)
		{
			conta++;
			if (conta > 3)
			{
				alert("Puoi scegliere al massimo 3 categorie !");
				quale.checked=false;
				conta--;
			}
		}
		else
		{
			conta>0?conta--:null; 
		}
		if (conta == 2)
		{
			document.all.spesa_iscrizione.innerText = document.all.costo_per2.value
		}
		if (conta == 3)
		{
			document.all.spesa_iscrizione.innerText = document.all.costo_per3.value
		}
		if (conta == 1)
		{
			var costo_categoria = Mid(quale.value,InStr(quale.value,",")+1,Len(quale.value))
			document.all.spesa_iscrizione.innerText = costo_categoria
		}		
		if (conta == 0)
		{
			document.all.spesa_iscrizione.innerText = 0
		}		
		
}

var dbcategoria = new Object()
dbcategoria["Nitro"] = [{value:"Categoria A", text:"Categoria A"},{value:"Categoria B", text:"Categoria B"},
										{value:"Categoria C", text:"Categoria C"}];
dbcategoria["Elettrico"] = [{value:"Categoria D", text:"Categoria D"},{value:"Categoria E", text:"Categoria E"},
													{value:"Categoria F", text:"Categoria F"}];
function setCategoria(chooser) {
    var newElem;
    var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
    var cityChooser = chooser.form.elements["categoria"];
    while (cityChooser.options.length) {
        cityChooser.remove(0);
    }
    var choice = chooser.options[chooser.selectedIndex].value;
    var db = dbcategoria[choice];
    newElem = document.createElement("option");
    newElem.text = "Seleziona una categoria:";
    newElem.value = "";
    cityChooser.add(newElem, where);
    if (choice != "") {
        for (var i = 0; i < db.length; i++) {
            newElem = document.createElement("option");
            newElem.text = db[i].text;
            newElem.value = db[i].value;
            cityChooser.add(newElem, where);
        }
    }
}

function selComando(oggetto)
{
	if(oggetto.value != "-1")
	{
		if(oggetto.value == "digitale")
		{
			document.all.frequenza.value = "DSM"
			document.all.frequenza.readOnly = true
			document.all.txtFreq.innerText = ""
		}
		else
		{
			document.all.frequenza.value = ""
			document.all.frequenza.readOnly = false
			document.all.txtFreq.innerText = "Frequenza "
		}
	}
	else
	{
		document.all.frequenza.value = ""
		document.all.frequenza.readOnly = true
		document.all.txtFreq.innerText = ""		
	}
}

function selTrasponder(oggetto)
{
	if(oggetto.value != "-1")
	{
		if(oggetto.value == "no")
		{
			document.all.valore_trasponder.value = "none"
			document.all.valore_trasponder.readOnly = true
			document.all.txtTrasponder.innerText = ""
		}
		else
		{
			document.all.valore_trasponder.value = ""
			document.all.valore_trasponder.readOnly = false
			document.all.txtTrasponder.innerText = "Valore "
		}
	}
	else	
	{
		document.all.valore_trasponder.value = ""
		document.all.valore_trasponder.readOnly = true
		document.all.txtTrasponder.innerText = ""				
	}
}

function selPosto(cella)
{
	//alert(cella.innerText)
	
	var cells = document.getElementsByTagName("td");
	for (var i=0; i<cells.length; i++) 
	{
		if(cells[i].bgColor == "#ffff00")
		{
			cells[i].bgColor= '#00CC00'
		}
	}
	
	cella.bgColor = "#FFFF00"
	document.all.lbl_posto.innerText = cella.innerText
	//alert(cella.bgColor )
}

function Mid(String, Start, Length)
{
    if (String == null)
        return (false);
    
    if (Start > String.length)
        return '';
    
    if (Length == null || Length.length == 0)
        return (false);
    
    return String.substr((Start - 1), Length);
}


function InStr(String1, String2)
{
    var a = 0;
    
    if (String1 == null || String2 == null)
        return (false);
    
    String1 = String1.toLowerCase();
    String2 = String2.toLowerCase();
    
    a = String1.indexOf(String2);
    if (a == -1)
        return 0;
    else
        return a + 1;
}

function Len(string)
{
    if (string == null)
        return (false);
    
    return String(string).length;
}
function DoSubmit(obj,lingua) {
	var nome		 = obj.nome.value;
	var cognome		 = obj.cognome.value;
	var username     = obj.username.value;
	var password     = obj.pw1.value;
	var confirmation = obj.pw2.value;
	var email        = obj.email.value;
	var citta		 = obj.citta.value;
	var cap		     = obj.cap.value;
	var strMailFilter   = /^.+@.+\..{2,3}$/;
	var strIllegalChars = /[\(\)\<\>\,\;\:\\\/\*\+\=\"\[\]]/;
				
	var submitform = true;
	if(lingua=='it'){var error      = '************************* REGISTRAZIONE *************************\n\n';}
	else{var error      = '************************* REGISTRATION **************************\n\n';}

	if(lingua=='it'){error += ' Attenzione :   \n\n';}else{error += ' Attention :   \n\n';}
	
	if (nome.length < 3 ) {
		if(lingua=='it'){error += '    + Nome è un campo richiesto  \n';}else{error += '    + Name is required  \n';}
		submitform = false;
	}
	if (nome.length > 20 && isNaN(nome) ) {
		if(lingua=='it'){error += '    + Nome può essere massimo 20 caratteri  \n';}else{error += '    + The name can not exceed 20 characters  \n';}
		submitform = false;
	}
	if (nome.length > 1 && nome.match(strIllegalChars) ) {
		if(lingua=='it'){error += '    + Nome contiene caratteri non consentiti  \n';}else{error += '    + The name contains characters not allowed\n';}
		submitform = false;	
	}
	
	if (cognome.length < 1) {
		if(lingua=='it'){error += '    + Cognome è un campo obbligatorio  \n';}else{error += '    + Surname is required  \n';}
		submitform = false;
	}
	
	if (cognome.length > 20 ) {
		if(lingua=='it'){error += '    + Cognome può essere massimo 20 caratteri  \n';}else{error += '    + The surname can not exceed 20 characters  \n';}
		submitform = false;
	}
	if (cognome.length > 1 && cognome.match(strIllegalChars)) {
		if(lingua=='it'){error += '    + Cognome contiene caratteri non consentiti  \n';}else{error += '    + The surname contains characters not allowed\n';}
		submitform = false;
	}
		
	if (username.length < 1) {
		if(lingua=='it'){error += '    + Username è un campo obbligatorio  \n';}else{error += '    + Username is required  \n';}
		submitform = false;
	}
	if (username.length > 20) {
		if(lingua=='it'){error += '    + Username può essere massimo 20 caratteri  \n';}else{error += '    + The username can not exceed 20 characters  \n';}
		submitform = false;
	}
	if (username.length > 1 && username.match(strIllegalChars)) {
		if(lingua=='it'){error += '    + Username contiene caratteri non consentiti  \n';}else{error += '    + The username contains characters not allowed\n';}
		submitform = false;
	}
	
	if (password.length < 5) {
		if(lingua=='it'){error += '    + Password deve essere minimo 5 caratteri  \n';}else{error += '    + The Password must be at least 5 characters  \n';}
		submitform = false;
	}
	if (password.length > 20) {
		if(lingua=='it'){error += '    + Password può essere massimo 20 caratteri  \n';}else{error += '    + The password can not exceed 20 characters  \n';}
		submitform = false;
	}
	if (confirmation.length < 1) {
		if(lingua=='it'){error += '    + Conferma password è un campo obbligatorio  \n';}else{error += '    + Confirm password is required  \n';}
		submitform = false;
	}
	if (password != confirmation) {
		if(lingua=='it'){error += '    + La password non è uguale  \n';}else{error += '    + The password is not equal \n';}
		submitform = false;
	}
	if (email.length < 1) {
		if(lingua=='it'){error += '    + Email è un campo obbligatorio  \n';}else{error += '    + The email is required  \n';}
		submitform = false;
	}	
	if (email.length > 100) {
		if(lingua=='it'){error += "    + Email puo' essere massimo 100 caratteri  \n";}else{error += '    + The email can not exceed 1000 characters  \n';}
		submitform = false;
	}
	if (email.length > 1 && email.match(strIllegalChars)) {
		if(lingua=='it'){error += '    + Email contiene caratteri non consentiti\n';}else{error += '    + The Email contains illegal characters \n';}
		submitform = false;
	}
	if (email.length > 1 && !email.match(strIllegalChars) && !(strMailFilter.test(email))) {
		if(lingua=='it'){error += '    + Email non è valida \n';}else{error += '    + The Email is not valid \n';}
		submitform = false;
	}	
	if (citta.length < 3 ) {
		if(lingua=='it'){error += '    + Città è un campo obbligatorio  \n';}else{error += '    + The city is required  \n';}
		submitform = false;
	}
	if (citta.length > 50 ) {
		if(lingua=='it'){error += '    + Città può essere massimo 50 caratteri  \n';}else{error += '    + The city can not exceed 15 characters  \n';}
		submitform = false;
	}
	if (citta.length > 1 && citta.match(strIllegalChars)) {
		if(lingua=='it'){error += '    + Città contiene caratteri non consentiti\n';}else{error += '    + The city contains illegal characters \n';}
		submitform = false;
	}
	
	/*if (cap.length < 4 ) {
	error     += '    + Cap è un campo richiesto  \n';
	submitform = false;
	}
	
	if (cap.length > 4 && isNaN(cap) ) {
	error     += '    + Cap contiene caratteri non consentiti  \n';
	submitform = false;
	}
	*/
	if (submitform) {
		obj.submit();
	}
	else {
		if(lingua=='it'){error += '\n************************* REGISTRAZIONE *************************\n\n';}else{error += '\n************************* REGISTRATION *************************\n\n';}
		alert (error);
		return false;
	}
}

function DoAggiorna(obj,lingua) {
	var nome		 = obj.nome.value;
	var cognome		 = obj.cognome.value;
	var password     = obj.pw1.value;
	var confirmation = obj.pw2.value;
	var email        = obj.email.value;
	var citta		 = obj.citta.value;
	var cap		     = obj.cap.value;
	var strMailFilter   = /^.+@.+\..{2,3}$/;
	var strIllegalChars = /[\(\)\<\>\,\;\:\\\/\*\+\=\"\[\]]/;
				
	var submitform = true;
	if(lingua=='it'){var error      = '************************* Aggiornamento profilo *************************\n\n';}
	else{var error      = '************************* Update profile **************************\n\n';}

	if(lingua=='it'){error += ' Attenzione :   \n\n';}else{error += ' Attention :   \n\n';}
	
	if (nome.length < 3 ) {
		if(lingua=='it'){error += '    + Nome : campo richiesto  \n';}else{error += '    + Name is required  \n';}
		submitform = false;
	}
	if (nome.length > 20 && isNaN(nome) ) {
		if(lingua=='it'){error += '    + Nome : deve essere massimo 20 caratteri  \n';}else{error += '    + The name can not exceed 20 characters  \n';}
		submitform = false;
	}
	if (nome.length > 1 && nome.match(strIllegalChars) ) {
		if(lingua=='it'){error += '    + Nome : contiene caratteri non consentiti  \n';}else{error += '    + The name contains characters not allowed\n';}
		submitform = false;	
	}
	
	if (cognome.length < 1) {
		if(lingua=='it'){error += '    + Cognome : un campo obbligatorio  \n';}else{error += '    + Surname is required  \n';}
		submitform = false;
	}
	
	if (cognome.length > 20 ) {
		if(lingua=='it'){error += '    + Cognome : deve essere massimo 20 caratteri  \n';}else{error += '    + The surname can not exceed 20 characters  \n';}
		submitform = false;
	}
	if (cognome.length > 1 && cognome.match(strIllegalChars)) {
		if(lingua=='it'){error += '    + Cognome : contiene caratteri non consentiti  \n';}else{error += '    + The surname contains characters not allowed\n';}
		submitform = false;
	}
		
	if (password.length < 5) {
		if(lingua=='it'){error += '    + Password : deve essere minimo 5 caratteri  \n';}else{error += '    + The Password must be at least 5 characters  \n';}
		submitform = false;
	}
	if (password.length > 20) {
		if(lingua=='it'){error += '    + Password : deve essere massimo 20 caratteri  \n';}else{error += '    + The password can not exceed 20 characters  \n';}
		submitform = false;
	}
	if (confirmation.length < 1) {
		if(lingua=='it'){error += '    + Conferma password : un campo obbligatorio  \n';}else{error += '    + Confirm password is required  \n';}
		submitform = false;
	}
	if (password != confirmation) {
		if(lingua=='it'){error += '    + La password non é uguale  \n';}else{error += '    + The password is not equal \n';}
		submitform = false;
	}
	if (email.length < 1) {
		if(lingua=='it'){error += '    + Email : campo obbligatorio  \n';}else{error += '    + The email is required  \n';}
		submitform = false;
	}	
	if (email.length > 30) {
		if(lingua=='it'){error += '    + Email : deve essere massimo 30 caratteri  \n';}else{error += '    + The email can not exceed 30 characters  \n';}
		submitform = false;
	}
	if (email.length > 1 && email.match(strIllegalChars)) {
		if(lingua=='it'){error += '    + Email : contiene caratteri non consentiti\n';}else{error += '    + The Email contains illegal characters \n';}
		submitform = false;
	}
	if (email.length > 1 && !email.match(strIllegalChars) && !(strMailFilter.test(email))) {
		if(lingua=='it'){error += '    + Email : non valida \n';}else{error += '    + The Email is not valid \n';}
		submitform = false;
	}	
	if (citta.length < 3 ) {
		if(lingua=='it'){error += '    + Città : un campo obbligatorio  \n';}else{error += '    + The city is required  \n';}
		submitform = false;
	}
	if (citta.length > 50 ) {
		if(lingua=='it'){error += '    + Città : deve essere massimo 50 caratteri  \n';}else{error += '    + The city can not exceed 15 characters  \n';}
		submitform = false;
	}
	if (citta.length > 1 && citta.match(strIllegalChars)) {
		if(lingua=='it'){error += '    + Citta : contiene caratteri non consentiti\n';}else{error += '    + The city contains illegal characters \n';}
		submitform = false;
	}
	
	if (submitform) {
		obj.submit();
	}
	else {
		if(lingua=='it'){error += '\n************************* Aggiornamento profilo *************************\n\n';}else{error += '\n************************* Update profile *************************\n\n';}
		alert (error);
		return false;
	}
}

