var festivi = null;

function inviarichiesta()
	{
	var errori = false;
	// Verifica il form
	RichiestaIscrizione = document.getElementById('richiestaIscrizione');
	var elementi = RichiestaIscrizione.elements;
//	alert(elementi.length);

	for (c = 0; c <= elementi.length - 1; c++)
		{
		altTag = elementi[c].title;
	//		document.getElementById('test').innerHTML +=  ('<br/> ' + elementi[c].id + ' l. = ' + elementi[c].value.length + ' alt l = ' + altTag.length) ;
		if (typeof altTag != 'undefined')
		if (altTag.length > 0)
			{
	//			document.getElementById('test').innerHTML +=  ('<br/> ' + elementi[c].id + ' l. = ' + elementi[c].value.length + ' alt l = ' + altTag.length) ;
			switch (altTag)
				{
				case 'blank':
					if (elementi[c].value.length == 0)
						{
						document.getElementById(elementi[c].id).className = 'errHilite';
						errori = true;
						}
					else if(elementi[c].name == 'corsoBreve[]')
						{
						conta = 0;
						for (i = 0; i < document.getElementsByName('corsoBreve[]').length; i++)
							{
							if(document.getElementsByName('corsoBreve[]')[i].checked)
								conta++;
							}
						if(conta == 0)
							{
							errori = true;
							document.getElementById('corsiBrevi').className = 'errHilite';
							}
						else
							{
							document.getElementById('corsiBrevi').className = '';
							}
						}
					else
						elementi[c].className = '';
				break;
				}
			}
		}

	if(document.getElementById('EMail').value != document.getElementById('ConfermaEmail').value)
		{
		document.getElementById('EMail').className = 'errHilite';
		document.getElementById('ConfermaEmail').className = 'errHilite';
		errori = true;
		}
	else
		{
		if(!document.getElementById('EMail').value.match(/[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/))
			{
			document.getElementById('EMail').className = 'errHilite';
			document.getElementById('ConfermaEmail').className = 'errHilite';
			Invia = false;
			}
		else
			{
			document.getElementById('EMail').className = '';
			document.getElementById('ConfermaEmail').className = '';
			}
		}

	// chiama il formtomail

	if (!(errori))
		{
		RichiestaIscrizione.action = 'index.php';
		RichiestaIscrizione.submit();
		}
	}

/*function aggiornaLingua(valoreLingua)
	{
	new Ajax.Updater('CategoriaCorso', './form_ajax.php',
		{
		method: 'post',
		parameters: {azione: 'aggiornaLingua', lingua: valoreLingua},
		}
		);
	}*/

function sceltaCategoriaCorso(valore, lingua)
	{
	new Ajax.Updater('dettagliCorso', '/form/form_ajax.php',
//	new Ajax.Request('./form_ajax.php',
		{
			method: 'post',
			parameters: {azione: 'dettagliCorso', corsoScelto: valore, lingua: lingua}
//			onSuccess: function (transport) { document.getElementById('dettagliCorso').innerHTML = transport.responseText; }
		}
		);
	}

function sceltaDurata()
	{
	var durata = document.getElementById('durata_estivo').value;
	var frequenza = document.getElementById('frequenza').value;

	if (durata == 100)
		{
		frequenza = document.getElementById('frequenza').value = 8;
		document.getElementById('frequenza').disabled = 'disabled';
		}
	else
		{
		document.getElementById('frequenza').disabled = '';
		}
	aggiornaElencoCorsiEstivi();
	}

function aggiornaElencoCorsiEstivi(cambioData)
	{
	var livello = document.getElementById('livello_estivo').value;
	var durata = document.getElementById('durata_estivo').value;
 	var frequenza = document.getElementById('frequenza').value;
	var inizioCorso = document.getElementById('inizioCorsoMese').value;

/*	if(livello != '' && durata != '' && frequenza != '' && inizioCorso != '')
		{
		alert('ora si');
		}
*/
//	if(durata != '' && livello != '')

	new Ajax.Updater('corso', '/form/form_ajax.php',
		{
		method: 'post',
		parameters: {azione: 'prelevaCorsiEstivi', durata: durata, livello: livello}
		}
		);

	// Ora, in base alla durata, aggiorna frequenza e date inizio

	if (typeof(cambioData) == 'undefined')
		{
		new Ajax.Updater('inizioCorsoMese', '/form/form_ajax.php',
			{
			method: 'post',
			parameters: {azione: 'estiviPrelevaIniziCorso', durata: durata, livello: livello}
			}
			);
		}

	if (durata == 100)
		{
		document.getElementById('frequenza').value = 8;
		document.getElementById('frequenza').disabled = 'disabled';
		document.getElementById('inizioCorsoMese').selectedIndex = 0;
		document.getElementById('inizioCorsoMese').disabled='disabled';
		}
	else
		{
		document.getElementById('frequenza').disabled = '';
		document.getElementById('inizioCorsoMese').disabled='';
		}

	livello = document.getElementById('livello_estivo').value;
	durata = document.getElementById('durata_estivo').value;
 	frequenza = document.getElementById('frequenza').value;
	inizioCorso = document.getElementById('inizioCorsoMese').value;

	if (document.getElementById('frequenza').value == 4)
		{
		document.getElementById('inizioCorsoMese').selectedIndex = 0;
		document.getElementById('inizioCorsoMese').disabled='disabled';
		}

	new Ajax.Request('/form/form_ajax.php',
		{
		method: 'post',
		parameters: {azione: 'calcolaFineCorsoBreve', frequenza: frequenza, inizio: inizioCorso, durata: durata, um: 'h', estivo:true},
		onSuccess: function(transport) {
		var dataFine = transport.responseText;
		document.getElementById('dataFineCorso').innerHTML = dataFine;
		document.getElementById('hiddenFineCorso').value = dataFine;
			}
		}
		);

/*
	new Ajax.Updater('dataFineCorso', './form_ajax.php',
		{
		method: 'post',
		parameters: {azione: 'calcolaFineCorsoBreve', frequenza: frequenza, inizio: inizioCorso, durata: durata, um: 'h', estivo:true}
		}
		);
*/
	}

function mostraCorsiPerGruppoScelto( valore )
	{
	if (valore == '')
		{
		document.getElementById('elencoCorsi').innerHTML = '';
		return;
		}

	if (valore == 'CORSO COMPUTER')
		{
		// Carica i festivi
		}

	new Ajax.Updater('elencoCorsi', '/form/form_ajax.php',
		{
		method: 'post',
		parameters: {azione: 'corsiSpecialiNelGruppoScelto', corsoScelto: valore},
		onComplete: function()
			{
			if(valore == 'CORSI ESTIVI')
			aggiornaElencoCorsiEstivi();

			/*	if (valore == 'CORSO COMPUTER')
				{
				Calendar.setup(
					{
					flatCallback	: selezionaData,
					dateStatusFunc : ourDateStatusFunc,
					firstDay		: 1,
				 // inputField  	: "inizioCorsoMese",
					ifFormat		: "%d/%m/%Y",
					flat       	 	: "calendario" // ID of the parent element
					}
					);
				}*/
			}
		}
		);
	}
	
function mostraDatePerPaginaScelta(valore, lingua)
	{
	new Ajax.Updater('elencoCorsi', '/form/form_ajax.php',
		{
		method: 'post',
		parameters: {azione: 'corsiSpecialiNellaPagina', corsoScelto: valore, lingua: lingua},
		onComplete: function()
			{
			if(valore == 'CORSI ESTIVI')
			aggiornaElencoCorsiEstivi();
			
			if(valore == 'CORSO COMPUTER, 53' | valore == 'CORSO COMPUTER, 52')
				{
				//alert(document.getElementById('corso').value);
				caricaCalendario(document.getElementById('corso'));
				}
/*			if(valore == 'CORSO COMPUTER, 52')
				{
				//alert(document.getElementById('corso').value);
				caricaCalendario(document.getElementById('corso'));
				}*/
			}
		}
		);
	}
	
function caricaCalendario(sceltaCorso)
	{
	document.getElementById('caricaCalendario').innerHTML = '';
	if(sceltaCorso.value != '')
		{
		dataInizio = cercaDataInizio();
		new Ajax.Updater('caricaCalendario', '/form/form_ajax.php',
			{
			method: 'post',
			parameters: {azione: 'caricaCorsoComputer'},
			onComplete: function()
				{
				 Calendar.setup(
					{
					date				: dataInizio,
					flatCallback		: selezionaData,
					dateStatusFunc 		: ourDateStatusFunc,
					firstDay			: 1,
								 // inputField  : "inizioCorsoMese",
					ifFormat			: "%d/%m/%Y",
					flat        	 	: "calendario" // ID of the parent element
					}
					);
				}
			}
			);
		/*************/
			//var input_field = document.getElementById("inizioCorsoMese");
			//document.write('');
			inizio = PadDigits(dataInizio.getDate(), 2) + '/' + PadDigits((dataInizio.getMonth() + 1), 2) + '/' + dataInizio.getFullYear();
			//alert(inizio);
			//alert(document.getElementById('dataFineCorso'));
			var fineCorso = new Date();
			var durata = document.getElementById('corso').options[document.getElementById('corso').selectedIndex].value.match(/\d+/g);
	
			new Ajax.Request('/form/form_ajax.php',
				{
				method: 'post',
				parameters: {azione: 'calcolaFineCorsoBreve', frequenza: 1, inizio: inizio, durata: durata[1], um: 'h', estivo: false, computer: true},
				onSuccess: function(transport)
					{
					var dataFine = transport.responseText;
					document.getElementById('dataFineCorso').innerHTML = dataFine;
					document.getElementById('hiddenFineCorso').value = dataFine;
					}
				}
				);
		/*****************/
		}
	}

function cercaDataInizio()
	{
	dataInizio = new Date();
	while(ourDateStatusFunc(dataInizio, dataInizio.getFullYear(), dataInizio.getMonth(), dataInizio.getDate()))
		{
		dataInizio = new Date(dataInizio.valueOf()+(24*60*60* 1000));
		}
	return dataInizio;
	}

function PadDigits(n, totalDigits)
	{
    n = n.toString();
    var pd = '';

	if (totalDigits > n.length)
	    {
        for (i=0; i < (totalDigits-n.length); i++)
    	    {
			pd += '0';
	        }
	    }
    return pd + n.toString();
	}

function selezionaData(calendar, date)
	{
	 if (calendar.dateClicked)
	 	{
		var input_field = document.getElementById("inizioCorsoMese");
		input_field.value = PadDigits(calendar.date.getDate(), 2) + '/' + PadDigits((calendar.date.getMonth() + 1), 2) + '/' + calendar.date.getFullYear();
		var fineCorso = new Date();
		var durata = document.getElementById('corso').options[document.getElementById('corso').selectedIndex].value.match(/\d+/g);

		new Ajax.Request('/form/form_ajax.php',
			{
			method: 'post',
			parameters: {azione: 'calcolaFineCorsoBreve', frequenza: 1, inizio: input_field.value, durata: durata[1], um: 'h', estivo: false, computer: true},
			onSuccess: function(transport)
				{
				var dataFine = transport.responseText;
				document.getElementById('dataFineCorso').innerHTML = dataFine;
				document.getElementById('hiddenFineCorso').value = dataFine;
				}
			}
			);

//		fineCorso.setDate(calendar.date.getDate()+7);
//		document.getElementById('inizioCorsoMese').value = calendar.date.getDate() + '/' + calendar.date.getMonth() + '/' + calendar.date.getFullYear();
//		document.getElementById('dataFineCorso').innerHTML = fineCorso.getDate() + '/' + (fineCorso.getMonth() + 1) + '/' + fineCorso.getFullYear();
//		document.getElementById('hiddenFineCorso').value = fineCorso.getDate() + '/' + (fineCorso.getMonth() + 1) + '/' + fineCorso.getFullYear();
		}
	}

function dateIsSpecial(year, month, day)
	{
	if (festivi.indexOf(year + '-' + month + '-' + day) != -1)
		return true;
	}

function ourDateStatusFunc(date, y, m, d)
	{
	if ( (date.getDay() == 6) | (date.getDay() == 0) )
	   	return true;
	oggi = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());

	if (date < oggi)
		return true;

	if(document.getElementById('corso').value == '52|42' && document.getElementById('linguaSessione').value == 'eng')
		{
		document.getElementById('inizioCorsoMese').value = 'qui';
		if(date.getDay() != 5)
			return true;
		}

	festivi = document.getElementById('giorniFestivi').innerHTML;

	if (d <= 9)
	  	{
  		dd = new String(d);
  		dd = "0" + dd;
	  	}
	else
		dd = d;

	if (m <= 9)
	  	{
  		mm = new String(m + 1);
  		mm = "0" + mm;
  		}
	else
		mm = m + 1;

   if (festivi.indexOf(y + '-' + mm + '-' + dd) != -1)
		return true;
	}

function selezionaCorsoBreve(oggettoCliccato, linguaCorso)
	{
	var contenitoreCorsiBrevi = document.getElementById('corsiBrevi');
	var totaleOre = 0;
	var totaleCorsi = document.getElementById('totaleCorsiBrevi').value * 1;
	for (c = 1; c <= totaleCorsi; c++)
		{
		elementoCorrente = document.getElementById('corsoBreve_' + c);
		if (elementoCorrente.checked)
			{
			totaleOre += (elementoCorrente.alt * 1)
			}
		}

	// Si possono selezionare anche pi� corsi brevi, basta non superare le 500 ore

	if (totaleOre > 500)
		{
		if(linguaCorso == 'ita')
			alert('I corsi brevi non possono superare le 450 ore totali. Se vuoi selezionare un nuovo corso devi rimuoverne un altro');
		else
			alert('The short course are not available for more of 450 hours totals.');
		oggettoCliccato.checked = false;
		totaleOre -= (oggettoCliccato.alt);
		}

	document.getElementById('totaleOre').innerHTML=totaleOre;
	calcolaFineCorsoBreve();
	}

function aggiornaTotaleOre ( ore )
	{
	if(ore.selectedIndex != 0)
		{
		// Preleviamo le ore
		var totOre = ore.options[ore.selectedIndex].value.match(/\d+/g);
	//	alert(totOre[1]);
		document.getElementById('totaleOre').innerHTML = totOre[1];
		}
	}

function calcolaFineCorsoBreve()
	{
	inizioCorso = document.getElementById('inizioCorsoMese').value;
	durataCorso = document.getElementById('totaleOre').innerHTML;
	frequenza = document.getElementById('frequenza').value;

	new Ajax.Request('/form/form_ajax.php',
		{
		method: 'post',
		parameters: {azione: 'calcolaFineCorsoBreve', frequenza: frequenza, inizio: inizioCorso, durata: durataCorso, um: 'h'},
		onSuccess: function(transport)
			{
			var dataFine = transport.responseText;
			document.getElementById('dataFineCorso').innerHTML = dataFine;
			document.getElementById('hiddenFineCorso').value = dataFine;
			}
		}
		);

/*	new Ajax.Updater('dataFineCorso', './form_ajax.php',
		{
		method: 'post',
		parameters: {azione: 'calcolaFineCorsoBreve', frequenza: frequenza, inizio: inizioCorso, durata: durataCorso, um: 'h'}
		}
		);
*/

	}

/*****************************************************************************************
Funzione per il calcolo della fine dei corsi standard
*/

function calcolaFineCorsoA()
	{
	durataCorso = document.getElementById('durataCorso').value;
	inizioCorso = document.getElementById('inizioCorso').value;

	new Ajax.Request('/form/form_ajax.php',
		{
		method: 'post',
		parameters: {azione: 'calcolaFineCorso', inizio: inizioCorso, durata: durataCorso, um: 'm'},
		onSuccess: function(transport){
		var data = transport.responseText;
		document.getElementById('fineCorso').innerHTML = data;
		document.getElementById('hiddenFineCorso').value = data;
			}
		}
		);

/*	new Ajax.Updater('fineCorso', './form_ajax.php',
		{
		method: 'post',
		parameters: {azione: 'calcolaFineCorso', inizio: inizioCorso, durata: durataCorso, um: 'm'}
		}
		);*/
	}

function verificaPrivacy(valore)
	{
	if (valore)
		{
		document.getElementById('invia').disabled = "";
		}
	else
		{
		document.getElementById('invia').disabled = "disabled";			
		}
	}



function popupRimborsi(lingua)
	{
	var w = 650;
	var h = 400;
	var l = Math.floor((screen.width-w)/2);
	var t = Math.floor((screen.height-h)/2);
	if(lingua == 'ita')
		file = 'rimborsi.htm';
	else
		file = 'rimborsi_eng.htm';

	window.open(file,"","scrollbars=yes, width=" + w + ",height=" + h + ",top=" + t + ",left=" + l);
	}

function popupPrivacy(lingua)
	{
	var w = 650;
	var h = 550;
	var l = Math.floor((screen.width-w)/2);
	var t = Math.floor((screen.height-h)/2);
	if(lingua == 'ita')
		file = 'privacy.htm';
	else
		file = 'privacy_eng.htm';
	window.open(file,"","scrollbars=yes, width=" + w + ",height=" + h + ",top=" + t + ",left=" + l);
	}
	
function cambiaProvincia(valore, targetId)
	{
	if(valore == 'Italy (EU)' | valore == '')
		document.getElementById(targetId).selectedIndex=0;
	else
		document.getElementById(targetId).selectedIndex=1;
	}
	
function cambiaStato(valore, targetId)
	{
	if(valore != '' && valore != 'EE')
		{
		target = document.getElementById(targetId);
		lunghezza = target.length;
		for(i = 0; i < lunghezza; i++)
			{
			if(target[i].value == 'Italy (EU)')
				target.selectedIndex = i;
			}
		}
	else
		target.selectedIndex = 0;
	}
