var anno;
var mese;
var data;
var cont = false;
		
function validate(){
	return true;
}

function cat_replace(cat){
	anno = (querySt('anno')!=undefined) ? querySt('anno') : "";
	mese = (querySt('mese') !=undefined) ? querySt('mese') : "";
	data = (querySt('data') !=undefined) ? querySt('data') : "";
	var url = "index.php?module=Cms";
	//url += (anno!="") ? "&anno="+anno : "";
	//url += (mese!="") ? "&mese="+mese : "";
	//url += (data!="") ? "&data="+data : "";
	
	if ((cat!='T') && (cat!='N')){	
		cat = cat.replace("c","");
		url += "&cat="+cat;
	}else if (cat=='T'){
		url += "&show=T";
	}else if (cat=='N'){
		url += "&show=N";
	}
	window.location.replace(url);
}

function txt_replace(txt){
	anno = (querySt('anno')!=undefined) ? querySt('anno') : "";
	mese = (querySt('mese') !=undefined) ? querySt('mese') : "";
	data = (querySt('data') !=undefined) ? querySt('data') : "";
	var url = "index.php?module=Cms";
	//url += (anno!="") ? "&anno="+anno : "";
	//url += (mese!="") ? "&mese="+mese : "";
	//url += (data!="") ? "&data="+data : "";
	
	txt = txt.replace("t","");
	window.location.replace(url+"&txt="+txt);
}

function ag_replace(gg,mm,yy){
	mm = (mm<10) ? '0'+mm : mm;
	gg = (gg<10) ? '0'+gg : gg;
	var date=yy+'-'+mm+'-'+gg;
	window.location.replace("index.php?module=Cms&show=A&data="+date);
}

function arch_replace(val){
	//Recupero anno e mese precedente
	anno = (querySt('anno')!=undefined) ? querySt('anno') : "";
	mese = (querySt('mese') !=undefined) ? querySt('mese') : "";
	if (val<=12)
		mese = val;
	else
		anno = val;
	var url = "index.php?module=Cms&show=N";
	url += (anno!="") ? "&anno="+anno : "";
	url += (mese!="") ? "&mese="+mese : "";
	window.location.replace(url);
}

function showAppuntamenti(anno,mese,giorno){
	giorno = (giorno<10) ? '0'+giorno : giorno;
	openTooltip('index.php?module=Cms&act=appuntamento&anno='+anno+'&mese='+mese+'&giorno='+giorno, 'appuntamento');
}

function showBando(id){
	openTooltip('index.php?module=Cms&act=bando&id='+id, 'bando');
}

function showFaq(id){
	if ($('domanda_'+id).style.display=='none'){
		Effect.BlindDown('domanda_'+id);
		new Effect.Highlight('domanda_'+id);
	}else{
		Effect.BlindUp('domanda_'+id);
	}
	return false;
}

function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

function check_captcha(action){
	cod = document.getElementById('captcha').value
	if (action=='1'){	
		ajaxGetRand('Server/general.php?op=captcha&cod='+cod, submit_nlsms);
	}else if (action=='0'){
		ajaxGetRand('Server/general.php?op=captcha&cod='+cod, remove_nlsms);
	}
}

function submit_nlsms(answer){
	if (answer=='VALID'){
		document.getElementById('action').value='1';
		form = document.getElementById('request_nlsms');
		
		email 		= document.getElementById('email');
		sms 		= document.getElementById('cellulare');
		fax 		= document.getElementById('fax');
		
		if (email!=undefined){
			if (email.value!=""){
				if (email.value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1){
					cont = true
				}else{
					cont = false;
					alert("La tua email non e\' valida");
				}
			}
		}
		if (sms!=undefined){
			if (sms.value!=""){
				if (sms.value.search(/^\+39(\d{9,10})$/) != -1){
					cont = true;
				}else{
					cont = false;
					alert("Il tuo numero di cellulare non e\' valido\nAssicurati che inizi con +39\nche abbia un prefisso di tre cifre\nche sia di 6 o 7 cifre\nNon inserire spazi\n+39xxxyyyyyyy");
				}
			}
		}
		if (fax!=undefined){	
			if (fax.value!=""){
				if (fax.value.search(/^\+39(\d{9,10})$/) != -1){
					cont = true;
				}else{
					cont = false;
					alert("Il tuo numero di fax non e\' valido\nAssicurati che inizi con +39\nche abbia un prefisso di tre cifre\nche sia di 6 o 7 cifre\nNon inserire spazi\n+39xxxyyyyyyy");
				}
			}
		}
		if (cont){
			form.submit();
		} 
	}else{
		alert('Il codice di conferma non corrisponde');
	}
}

function remove_nlsms(answer){
	if (answer=='VALID'){
		document.getElementById('action').value='0';
		email 		= document.getElementById('email').value;
		sms 		= document.getElementById('sms').value;
		
		var query = "select IF(count(id_anagrafica)>0,'OK','NO') as ans from ang_anagrafica where email='"+email+"' or cellulare='"+sms+"'";
		ajaxGetRand('Server/general.php?op=query&query='+query, 
			function(query_res){
				form = document.getElementById('request_nlsms');
				eval(query_res);
				if (result['1']['ans']=='NO'){
					msg = "I tuoi dati non corrispondono a quelli inseriti nel nostra database";
					alert(msg);
				}else{
					form.submit();
				}
				alert(cont);
			}
		);
	}else{
		alert('Il codice di conferma non corrisponde');
	}
}

function remove_subscription(id){
	var conf = confirm('Sei sicuro di voler rimuovere la tua sottoscrizione?');
	if (conf)
		window.location.href = 'index.php?module=Cms&act=removeSMS_NL&id='+id
}

function close_subscription_box(){
	window.location.href = 'index.php?module=Cms';
}

function show_commlist(tipo,check){
	if (check.checked){
		document.getElementById(tipo+'_list').style.display = 'block';
	}else{
		document.getElementById(tipo+'_list').style.display = 'none';
	}
}

function partecipa_evento(form,anno,mese,giorno){
	if ((partecipa_evento_valid($('adulti'))) && (partecipa_evento_valid($('bambini'))) && (partecipa_evento_valid($('ospiti')))){
		new Ajax.Updater('appuntamento', 'index.php?module=Cms&act=appuntamento&anno='+anno+'&mese='+mese+'&giorno='+giorno,{
			parameters: $('partecipazione').serialize()
		});
	}else{
		alert('Hai inserito valori non validi')
	}
}

function partecipa_evento_valid(el){
	return (el.value>0) || (el.value=="") || ( IsNumeric(el.value) && (el.value==0))
}

function rimuovi_partecipazione(prenotazione,anno,mese,giorno){
	new Ajax.Updater('appuntamento', 'index.php?module=Cms&act=appuntamento&anno='+anno+'&mese='+mese+'&giorno='+giorno+'&opt=remove&prenotazione='+prenotazione);
}

function modifica_partecipazione(){
	Effect.toggle('mod_part','slide');
}

/* Curriculum Vitae */

function fill_formazione(i, tipo){
	/* Visualizza sempre */
	$('tr_formazione_diploma_'+i).style.display = 'none';
	$('tr_formazione_laurea_'+i).style.display = 'none';
	$('tr_formazione_indirizzo_'+i).style.display = 'none';
	$('tr_formazione_votazione_'+i).style.display = 'none';
	$('tr_formazione_titolo_tesi_'+i).style.display = 'none';
		
	$('formazione_diploma_id_'+i).value = 0;
	$('formazione_corso_laurea_id_'+i).value = 0;
	$('formazione_indirizzo_'+i).value = '';
	$('formazione_votazione_voto_'+i).value = '';
	$('formazione_votazione_su_'+i).value = '';
	$('formazione_lode_'+i).checked = false;
	$('formazione_titolo_tesi_'+i).value = '';
	
	$('tr_formazione_nome_istituto_'+i).style.display = 'table-row';
	$('tr_formazione_citta_'+i).style.display = 'table-row';
	$('tr_formazione_inizio_'+i).style.display = 'table-row';
	$('tr_formazione_conseguimento_'+i).style.display = 'table-row';
	$('tr_formazione_note_'+i).style.display = 'table-row';
	$('tr_formazione_new_'+i).style.display = 'table-row';
	
	if (tipo<2){
		/* Media inferiore */
		
	}else if (tipo==2){
		/* Diploma */
		$('tr_formazione_diploma_'+i).style.display = 'table-row';
		$('tr_formazione_votazione_'+i).style.display = 'table-row';
	}else{
		/* Laurea */
		$('tr_formazione_laurea_'+i).style.display = 'table-row';
		$('tr_formazione_indirizzo_'+i).style.display = 'table-row';
		$('tr_formazione_votazione_'+i).style.display = 'table-row';
		$('tr_formazione_titolo_tesi_'+i).style.display = 'table-row';
	}
}

function addFormazione(i){
	new Ajax.Updater('formazione_int', 'index.php?module=Cms&act=addFormazione&i='+i, {
		insertion: Insertion.Bottom
	})
}

function addOccupazione(i){
	new Ajax.Updater('occupazione_int', 'index.php?module=Cms&act=addOccupazione&i='+i, {
		insertion: Insertion.Bottom
	})
}

function vai_da(id, element){
	try{
		from = element.parentNode.parentNode;
	}catch(e){
		from = element.parentElement.parentElement;
	}
	to = $(id);
	from.style.display = 'none';
	to.style.display = 'block';
}

function vai_a(id, element){
	if (id=='formazione'){
		eval($('validator_Cms_cv').innerHTML);
		var test = validate(validator);
	}else{
		var test = true;
	}
	if (test){
		try{
			from = element.parentNode.parentNode;
		}catch(e){
			from = element.parentElement.parentElement;
		}
		to = $(id);
		from.style.display = 'none';
		to.style.display = 'block';
	}
}