//metodo utilizzato per passare i dati alla pagina segnala.php o correggi.php
function inviaRisultato(gara,serie,genere,girone,nextPage){
	document.formS.action=nextPage;
	document.formS.gara.value=gara;
	document.formS.serie.value=serie;
	document.formS.genere.value=genere;
	document.formS.girone.value=girone;
	createTarget(document.formS.target);
	document.formS.submit();
}
//funzione che smista l'azione verso risultati o calendari
function scegliPath(nextPage, serie, genere, girone){
	document.formS.action=nextPage;
	document.formS.serie.value=serie;
	document.formS.genere.value=genere;
	document.formS.girone.value=girone;
	document.formS.submit();
}

// valorizza i parametri da usare in fase di conferma correzione
function inviaCorrezione(gara,serie,genere,girone,casa_new,ospiti_new,user_old,user_new,casa_ris_old,ospiti_ris_old,idCasa,idOspiti){
	document.formS.gara.value=gara;
	document.formS.serie.value=serie;
	document.formS.genere.value=genere;
	document.formS.girone.value=girone;
	document.formS.casa_ris.value=casa_new;
	document.formS.ospiti_ris.value=ospiti_new;
	document.formS.userIns.value=user_old;
	document.formS.userCor.value=user_new;
	document.formS.casa_ris_old.value=casa_ris_old;
	document.formS.ospiti_ris_old.value=ospiti_ris_old;
	document.formS.idCasa.value=idCasa;
	document.formS.idOspiti.value=idOspiti;
	document.formS.step.value="correggi";
	document.formS.submit();

}
// valorizza i parametri da usare in fase di archiviazione segnalazione errata
function archiviaSegnalazione(gara,serie,genere,girone,casa_new,ospiti_new,user_old,user_new,casa_ris_old,ospiti_ris_old,idCasa,idOspiti){
	document.formS.gara.value=gara;
	document.formS.serie.value=serie;
	document.formS.genere.value=genere;
	document.formS.girone.value=girone;
	document.formS.casa_ris.value=casa_new;
	document.formS.ospiti_ris.value=ospiti_new;
	document.formS.userIns.value=user_old;
	document.formS.userCor.value=user_new;
	document.formS.casa_ris_old.value=casa_ris_old;
	document.formS.ospiti_ris_old.value=ospiti_ris_old;
	document.formS.idCasa.value=idCasa;
	document.formS.idOspiti.value=idOspiti;
	document.formS.step.value="archivia";
	document.formS.submit();

}

//da il campo obbligatorio a mailinglist
function campoobbligatorio(){ 
  if(trim(document.mailinglist.email.value) == "") {
    alert('Attenzione: Il campo deve essere riempito'); 
    return false;
  }
  document.mailinglist.submit();
} 

//da il campo obbligatorio alla creazione del guestbook e negli altri moduli
function campoobbligatorioguestbook(){ 
  if(trim(document.moduli.squadra.value) == "") {
    alert('Attenzione: riempi il campo squadra'); 
    return false;
  }
    if(trim(document.moduli.user.value) == "") {
    alert('Attenzione: riempi il campo user'); 
    return false;
  }
    if(trim(document.moduli.pass.value) == "") {
    alert('Attenzione: riempi il campo password'); 
    return false;
  }
}

function trim(str) {
  return lTrim(rTrim(str));
}

function rTrim(str) {
  while (str.substring(str.length-1) == ' ') {
   str = str.substring(0,str.length-1);
  }
  return str;
}

function lTrim(str) {
  while (str.substring(0,1) == ' ') {
    str = str.substring(1, str.length);
  }
  return str;
}

function validaScheda(){
  //alert("in");
	var nome = document.form1.nome.value;
	var cognome = document.form1.cognome.value;
	var foto = document.form1.elements['foto[]'];
	//var foto2 = document.form1.foto[1].value;
  var anno_nascita = document.form1.anno_nascita.options[document.form1.anno_nascita.selectedIndex].value;
  var segno = document.form1.segno.options[document.form1.segno.selectedIndex].value;
  var squadra = document.form1.squadra.options[document.form1.squadra.selectedIndex].value;
  var anni_pallavolo = document.form1.anni_pallavolo.options[document.form1.anni_pallavolo.selectedIndex].value;
  var professione = document.form1.professione.value;
  var hobby = document.form1.hobby.value;	
  var descrizione = document.form1.descrizione.value;
  //alert(foto.length);
  if ((foto[0].value == "") || (foto[0].value == "undefined")) {
  	alert("Selezionare la foto principale.");
  	return false;
	}
	if ((foto[1].value == foto[0].value)) {
  	alert("Non e' possibile inserire la stessa foto.");
  	return false;
	}
	if ((foto[1].value == "") || (foto[1].value == "undefined")) {
  	alert("Selezionare la foto in completino.");
  	return false;
	}
  if ((nome == "") || (nome == "undefined")) {
  	alert("Il campo Nome è obbligatorio.");
   	document.form1.nome.focus();
  	return false;
	}
	if ((cognome == "") || (cognome == "undefined")) {
  	alert("Il campo Cognome è obbligatorio.");
   	document.form1.cognome.focus();
  	return false;
	}
	if ((anno_nascita == "") || (anno_nascita == "undefined")) {
   	alert("Il campo Anno nascita è obbligatorio.");
   	document.form1.anno_nascita.focus();
   	return false;
	}
	if ((segno == "") || (segno == "undefined")) {
   	alert("Il campo 'Segno zodiacale' è obbligatorio.");
   	document.form1.segno.focus();
   	return false;
	}
	if ((squadra == "") || (squadra == "undefined")) {
   	alert("Il campo Squadra è obbligatorio.");
   	document.form1.squadra.focus();
   	return false;
	}
  if ((anni_pallavolo == "") || (anni_pallavolo == "undefined")) {
   	alert("Il campo 'Gioca da anni' è obbligatorio.");
   	document.form1.anni_pallavolo.focus();
   	return false;
	} 
	if ((professione == "") || (professione == "undefined")) {
  	alert("Il campo Professione è obbligatorio.");
   	document.form1.professione.focus();
  	return false;
	}
	if ((hobby == "") || (hobby == "undefined")) {
  	alert("Il campo Hobby è obbligatorio.");
   	document.form1.hobby.focus();
  	return false;
	}
	if ((descrizione == "") || (descrizione == "undefined")) {
  	alert("L'inserimento di una breve descrizione è obbligatorio.");
   	document.form1.descrizione.focus();
  	return false;
	}
  document.form1.submit();	    	
}

function mostraScheda(id) {
	document.form1.id.value = id;
	document.form1.submit();
}

function votaMiss(voto) {
	//alert("LE VOTAZIONI ANCORA NON SONO INIZIATE");
	//return false;
	
	document.form1.voto.value = voto;
	document.form1.submit();
}

function votaMister(voto) {
  //alert("LE VOTAZIONI ANCORA NON SONO INIZIATE!");
	//return false;
	
	document.form1.voto.value = voto;
	document.form1.submit();
}

//MESSANGER
var newwindow;
function poptastic(url){
	newwindow=window.open(url,'name','width=760,height=500,left=20,top=20,toolbar=no,menubar=no,directories=no,location=no,scrollbars=yes,status=no,resizable=yes,fullscreen=no');
	if (window.focus) {
		newwindow.focus()
	}
} 

function validashop(){
  var pezzi = document.shop.pezzi.options[document.shop.pezzi.selectedIndex].value;
  var taglia = document.shop.taglia.options[document.shop.taglia.selectedIndex].value;
  var nome = document.shop.nome.value;
  var cognome = document.shop.cognome.value;
  var indirizzo = document.shop.indirizzo.value;
  var numero_civico = document.shop.numero_civico.value;	
  var cap = document.shop.cap.value;
  var citta = document.shop.citta.value;
  var email = document.shop.email.value;
  
	if ((pezzi == "") || (pezzi == "undefined")) {
   	alert("Il campo Numero di maglie è obbligatorio.");
   	document.shop.pezzi.focus();
   	return false;
	}
	if ((taglia == "") || (taglia == "undefined")) {
   	alert("Il campo Taglia è obbligatorio.");
   	document.shop.taglia.focus();
   	return false;
	}
    if ((nome == "") || (nome == "undefined")) {
  	alert("Il campo Nome è obbligatorio.");
   	document.shop.nome.focus();
  	return false;
	}
	if ((cognome == "") || (cognome == "undefined")) {
  	alert("Il campo Cognome è obbligatorio.");
   	document.shop.cognome.focus();
  	return false;
	}
	if ((indirizzo == "") || (indirizzo == "undefined")) {
  	alert("Il campo Indirizzo è obbligatorio.");
   	document.shop.indirizzo.focus();
  	return false;
	}
	if ((numero_civico == "") || (numero_civico == "undefined")) {
  	alert("Il campo Numero civico è obbligatorio.");
   	document.shop.numero_civico.focus();
  	return false;
	}
	if ((cap == "") || (cap == "undefined")) {
  	alert("Il campo Cap è obbligatorio.");
   	document.shop.cap.focus();
  	return false;
	}
	if ((citta == "") || (citta == "undefined")) {
  	alert("Il campo Citta è obbligatorio.");
   	document.shop.citta.focus();
  	return false;
	}
	if ((email == "") || (email == "undefined")) {
   	alert("Il campo email è obbligatorio.");
   	document.shop.email.focus();
   	return false;
	}
  document.shop.submit();	    	
}

function showTable(theTable, focusOnElement) {
  if (document.getElementById(theTable).style.display == 'none') {
    document.getElementById(theTable).style.display = 'block';
    if ((focusOnElement!="") && (focusOnElement != null)){
      document.getElementById(focusOnElement).focus();
    }
  }
}

function validaCommento(){
	alert("Stiamo lavorando a questa sezione. Presto sara' possibile commentare i video");
	
	var testo = document.form1.text.value;
	if (testo == "") {
  	alert("Non hai inserito alcun testo per il commento.");
   	return false;
	}
  document.form1.submit();	    	
}

function PopupCentrata() {
   var w = 460;
   var h = 220;
   var l = Math.floor((screen.width-w)/2);
   var t = Math.floor((screen.height-h)/2);
   var popup = window.open("disclaimer.php","disclaimer","width=" + w + ",height=" + h + ",top=" + t + ",left=" + l);
      popup.focus();
}
function validaVideo() {
  var titolo = document.form1.titolo.value;
	var descrizione = document.form1.descrizione.value;
	var video = document.form1.file.value.toUpperCase();
  var categoria = document.form1.categoria.options[document.form1.categoria.selectedIndex].value;
  var disclaimer = document.form1.disclaimer;
  if ((titolo == "") || (titolo == "undefined")) {
  	alert("Il campo 'titolo' è obbligatorio.");
   	document.form1.titolo.focus();
  	return false;
	}
	if ((descrizione == "") || (descrizione == "undefined")) {
  	alert("Il campo 'descrizione' è obbligatorio.");
   	document.form1.descrizione.focus();
  	return false;
	}
	if ((categoria == "") || (categoria == "undefined")) {
  	alert("Il campo 'categoria' è obbligatorio.");
   	document.form1.categoria.focus();
  	return false;
	}
	if ((video == "") || (video == "undefined")) {
  	alert("La selezione del video è obbligatoria.");
   	document.form1.file.focus();
  	return false;
	}
	if(!((video.lastIndexOf('.WMV')==-1)||
       (video.lastIndexOf('.AVI')==-1)||
       (video.lastIndexOf('.MOV')==-1)||
       (video.lastIndexOf('.MP4')==-1)||
       (video.lastIndexOf('.3GP')==-1)||
       (video.lastIndexOf('.MPG')==-1))){
      alert("I formati in cui puoi inviare i filmati sono: .wmv, .avi, .mov, .mp4, .3gp, .mpg");
   	  document.form1.video.focus();
   	  return false;
  }
	if (!disclaimer.checked) {
  	alert("Per poter inviare un video e' necessario accettare il disclaimer.");
   	document.form1.disclaimer.focus();
  	return false;
	}
  document.form1.submit();
}

function maxLength(field,maxChars) {
  if(field.value.length >= maxChars) {
    event.returnValue=false;
    return false;
  }
}  

function maxLengthPaste(field,maxChars) {
  event.returnValue=false;
  if((field.value.length +  window.clipboardData.getData("Text").length) > maxChars) {
    return false;
  }
  event.returnValue=true;
}

function createTarget(t){
  var h=280;
  var w=780;
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  var win = window.open("",t,settings);
  win.window.focus();
  return true;
}

function loadTeamPage(form, team){
  //alert(team);
  var teamString = team.replace(/ /g, "-");
  form.squadra.value = team;
  form.action="sito.php?team="+teamString; 
  form.submit();
}
