/*==========================================
		CONFERMA CANCELLAZIONE
==========================================*/
function conferma(ptype, pid) {
	if (confirm("ATTENZIONE! Procedere con la cancellazione?")) {
		document.location.href = "_delete.php?content="+ptype+"&id="+pid;
	}
}
function confermauser(pid) {
	if (confirm("ATTENZIONE! Procedere con la cancellazione?")) {
		document.location.href = "admin.php?delete="+pid;
	}
}
/*==========================================
		FINESTRE POP-UP
==========================================*/
function newwin(file, wid, hei) {
	popup = top.window.open(file, "popwin", "width= "+wid+", height="+hei+", toolbar=0, location=0, scrolling=no, directories=0, status=0, menubar=0, resizable=0");
	final_hei = (screen.availheight - hei)/2-30;
	if (final_hei<0) final_hei = 0;
	if (navigator.userAgent.indexOf("Mac")==-1) {
		popup.moveTo((screen.availwidth - wid)/2, final_hei);
	}
	popup.focus();
}
function newwinclose(where) {
	top.window.opener.document.location.href = where;
	top.window.close();
}
/***************************************************************
 *  Esegue un controllo con espressioni regolari e sulla lunghezza
 *  di una stringa    
 */
function XControL(nome, campo, string, minl, maxl, ins_type){  
   switch(ins_type) {
     case "user":
     reg = "^[a-zA-Z0-9-_.+]+$";
     break;
     case "numero":
     reg = "^[0-9]+$";
     break;
     case "testo":
     reg = "^(.*)+\$";
     break;
     case "email":
     reg = "^[a-zA-Z0-9-_.+]{2,100}@[a-zA-Z0-9-_.+]{2,100}.[a-zA-Z]{2,5}$";
     break;
     case "url":
     reg = "^http://+[^@ ]+\.+[^@ ]+$";
     break;
     case "numtel":
     reg = "^[0-9+-]+$";
     break;   
   }
   if(string.length<minl||string.length>maxl){
		 alert(nome+' deve avere lunghezza compresa tra '+minl+' e '+maxl+' caratteri');
 		 campo.focus();
 		 return false;    
   } 	 
   // restituisce true se il riconoscimento va a buon fine
   ereg = new RegExp(reg);
   if(string.match(ereg)){ return true; }   
   // restituisce false e visualizza un errore in caso di lunghezza non corretta
   if(ins_type=="testo") {
	   alert(nome+' non può contenere * o /'); 
	 }else{
		 alert(nome+' non è stato inserito correttamente');
	 } 	 
	 return false;
}
function checkSelect(nome, campo, valore) {
	if (valore==0) {
		alert("Selezionare "+nome);
		campo.focus();
		return false;
	} else {
		return true;
	}
}
function checkAlt(nome, campo, valore) {
	if (valore=="") {
		alert("Inserire "+nome);
		campo.focus();
		return false;
	} else {
		return true;
	}
}
function checkDates() {
	if (document.form1.t_d.selectedIndex==0) {
		document.form1.t_d.value = document.form1.f_d.value;
		document.form1.t_m.value = document.form1.f_m.value;
		document.form1.t_y.value = document.form1.f_y.value;
		return true;
	} else {
		startdate = new Date(parseInt(document.form1.f_y.value, 10), parseInt(document.form1.f_m.value, 10)-1, parseInt(document.form1.f_d.value, 10));
		enddate   = new Date(parseInt(document.form1.t_y.value, 10), parseInt(document.form1.t_m.value, 10)-1, parseInt(document.form1.t_d.value, 10));
		if (enddate<startdate) {
			alert("La data di fine è precedente alla data d'inizio");
			return false;
		} else {
			return true;
		}
	}
}
function checkPassword(campo, pass1, pass2) {
	if (pass1 != pass2) {
		alert("Le due password non coincidono");
		campo.focus();
		return false;
	} else {
		return true;
	}
}
function checkPrivacy(val) {
	if (!val) {
		alert("Deve essere data l'autorizzazione al trattamento dei dati personali");
		return false;
	}
	return true;
}
function checklevel(val){
  if(val=="Seleziona"){
    alert("Seleziona la Tipologia dell'Utente da inserire");
    return false;
  }
  return true;
}
/*==========================================
		CONTROLLO ARTICOLI
==========================================*/
function checkFormArt() {
	if (			XControL("Il titolo", document.form1.title, document.form1.title.value, 5, 100, 'testo')
				 && checkSelect("la categoria", document.form1.cat, document.form1.cat.selectedIndex)
				 && checkSelect("l'area", document.form1.where_area, document.form1.where_area.selectedIndex)
				 && checkSelect("la provincia", document.form1.where_prov, document.form1.where_prov.selectedIndex)
				 && XControL("La città", document.form1.where_city, document.form1.where_city.value, 2, 50, 'testo')
				 && checkSelect("il livello di Interesse", document.form1.famen, document.form1.famen.selectedIndex)
				 && !(document.form1.sign.value!="" && !XControL("La firma", document.form1.sign, document.form1.sign.value, 1, 80, 'testo'))
				 && !(document.form1.upfile1.value!="" && !checkAlt("La descrizione della prima immagine", document.form1.altext1, document.form1.altext1.value))
				 && !(document.form1.upfile2.value!="" && !checkAlt("La descrizione della seconda immagine", document.form1.altext2, document.form1.altext2.value))) {
		document.form1.submit();
	}
}
/*==========================================
		CONTROLLO EVENTI
==========================================*/
function checkFormEve() {
	if (			XControL("Il nome dell'evento", document.form1.event_name, document.form1.event_name.value, 5, 100, 'testo')
				 && checkSelect("la categoria", document.form1.cat, document.form1.cat.selectedIndex)
				 && checkSelect("il giorno d'inizio", document.form1.f_d, document.form1.f_d.selectedIndex)
				 && checkSelect("il mese d'inizio", document.form1.f_m, document.form1.f_m.selectedIndex)
				 && checkSelect("l'anno d'inizio", document.form1.f_y, document.form1.f_y.selectedIndex)
				 && !((document.form1.t_d.selectedIndex>0 || document.form1.t_m.selectedIndex>0 || document.form1.t_y.selectedIndex>0) && !(checkSelect("il giorno di fine", document.form1.t_d, document.form1.t_d.selectedIndex) && checkSelect("il mese di fine", document.form1.t_m, document.form1.t_m.selectedIndex) && checkSelect("l'anno di fine", document.form1.t_y, document.form1.t_y.selectedIndex)))
				 && checkDates()
				 && checkSelect("l'area", document.form1.where_area, document.form1.where_area.selectedIndex)
				 && checkSelect("la provincia", document.form1.where_prov, document.form1.where_prov.selectedIndex)
				 && XControL("La città", document.form1.where_city, document.form1.where_city.value, 2, 50, 'testo')
				 && !(document.form1.where_sede.value!="" && !XControL("La sede", document.form1.where_sede, document.form1.where_sede.value, 4, 100, 'testo'))
				 && !(document.form1.where_addr.value!="" && !XControL("L'indirizzo", document.form1.where_addr, document.form1.where_addr.value, 6, 100, 'testo'))
				 && !(document.form1.orari.value!="" && !XControL("L'orario", document.form1.orari, document.form1.orari.value, 4, 200, 'testo'))
				 && !(document.form1.info_tel.value!="" && !XControL("Il numero di telefono", document.form1.info_tel, document.form1.info_tel.value, 6, 20, 'numtel'))
				 && !(document.form1.info_mail.value!="" && !XControL("L'indirizzo email", document.form1.info_mail, document.form1.info_mail.value, 6, 50, 'email'))
				 && !((document.form1.info_www.value!="http://" && document.form1.info_www.value!="") && !XControL("L'indirizzo del sito web", document.form1.info_www, document.form1.info_www.value, 7, 80, 'url'))
				 && !(document.form1.upfile1.value!="" && !checkAlt("La descrizione della prima immagine", document.form1.altext1, document.form1.altext1.value))
				 && !(document.form1.upfile2.value!="" && !checkAlt("La descrizione della seconda immagine", document.form1.altext2, document.form1.altext2.value))) {
		if (document.form1.info_www.value=="http://") document.form1.info_www.value = "";
		document.form1.submit();
	}
}
/*==========================================
		CONTROLLO UTENTI
==========================================*/
function checkFormUser() {
	if (			XControL("Lo username", document.form1.e_usr, document.form1.e_usr.value, 4, 40, 'testo')
				 && XControL("Il nome completo", document.form1.realname, document.form1.realname.value, 4, 40, 'testo')
				 && XControL("L'indirizzo email", document.form1.email, document.form1.email.value, 6, 50, 'email')
         && checklevel(document.form1.level.value)) {
		document.form1.submit();
	}
}
/*==========================================
		CONTROLLO CINEMA
==========================================*/
function checkFormCn() {
	if (		XControL("Il nome del cinema", document.form1.cinema, document.form1.cinema.value, 4, 80, 'testo')
						 && checkSelect("La regione", document.form1.where_area, document.form1.where_area.selectedIndex)
		     && checkSelect("La provincia", document.form1.where_prov, document.form1.where_prov.selectedIndex)
			 && XControL("La città", document.form1.where_city, document.form1.where_city.value, 4, 60, 'testo')
			 && XControL("L'indirizzo del cinema", document.form1.where_addr, document.form1.where_addr.value, 4, 60, 'testo')
			 && XControL("Il numero di telefono", document.form1.info_tel, document.form1.info_tel.value, 6, 20, 'numtel')
			 && XControL("Il titolo del film", document.form1.film_name, document.form1.film_name.value, 4, 60, 'testo')
			 && checkSelect("il giorno d'inizio", document.form1.f_d, document.form1.f_d.selectedIndex)
			 && checkSelect("il mese d'inizio", document.form1.f_m, document.form1.f_m.selectedIndex)
			 && checkSelect("l'anno d'inizio", document.form1.f_y, document.form1.f_y.selectedIndex)
			 && !((document.form1.t_d.selectedIndex>0 || document.form1.t_m.selectedIndex>0 || document.form1.t_y.selectedIndex>0) && !(checkSelect("il giorno di fine", document.form1.t_d, document.form1.t_d.selectedIndex) && checkSelect("il mese di fine", document.form1.t_m, document.form1.t_m.selectedIndex) && checkSelect("l'anno di fine", document.form1.t_y, document.form1.t_y.selectedIndex)))
			 && !((document.form1.info_www.value!="http://" && document.form1.info_www.value!="") && !XControL("L'indirizzo del sito web", document.form1.info_www, document.form1.info_www.value, 7, 100, 'url'))			 
       && !(document.form1.ticket.value!="" && !XControL("Il prezzo del biglietto", document.form1.ticket, document.form1.ticket.value, 1, 100, 'testo'))
			 && !(document.form1.film_regia.value!="" && !XControL("Il regista del film", document.form1.film_regia, document.form1.film_regia.value, 4, 100, 'testo'))
			 && !(document.form1.actors.value!="" && !XControL("Il cast del film", document.form1.actors, document.form1.actors.value, 4, 150, 'testo'))
			 && !(document.form1.film_genre.value!="" && !XControL("Il genere del film", document.form1.film_genre, document.form1.film_genre.value, 4, 40, 'testo'))
			 && !(document.form1.film_anno.value!="" && !XControL("L'anno di produzione del film", document.form1.film_anno, document.form1.film_anno.value, 4, 4, 'numero'))
			 && !(document.form1.info_fax.value!="" && !XControL("Il numero di fax", document.form1.info_fax, document.form1.info_fax.value, 6, 20, 'numtel'))
			 && !(document.form1.info_mail.value!="" && !XControL("L'indirizzo email", document.form1.info_mail, document.form1.info_mail.value, 6, 207, 'email'))       			 
			 && checkDates()
			 && XControL("L'orario", document.form1.orari, document.form1.orari.value, 4, 100, 'testo')
         ){
		  if(document.form1.info_www.value=="http://") document.form1.info_www.value = "";
		  document.form1.submit();
	}
}
