/*
  FILE lib.php
*/

/* gestisce il pulstante, effettuando il submit
 * richiede:
 * nomeform, nome del form di cui fare il submit
 * nomepulsante, nome della variabile da passare col form
*/
function submitForm(nomeform, nomepulsante) {
	eval("document.forms." + nomeform + "." + nomepulsante + ".checked=true;");
	eval("document.forms." + nomeform + ".submit();");
}

/*
  FILE query_di_selezione.php
*/

// controlla che ci sia almeno un elemento in lista condizioni 
// prima di passare alla fase successiva
function checknumCond(f){
  var noError = true;
  if (f.numcond.value == 0){
    alert("La LISTA CONDIZIONI è vuota.\n\n1)Se si vuole procedere alla spedizione \ndei messaggi senza effettuare selezioni,\ntornare al MENU e selezionare \nl'opzione SPEDISCI SMS RAPIDO.\n\n2) Se è stata selezionata una opzione \noccorre premere il tasto AGGIUNGI \nper inserirla nella LISTA CONDIZIONI.");
    noError = false;
  }
  if (noError){
    f.submit();
  }
}

//gestisce i valori multipli con la relazione di ugualianza
function checkSelection(r,sel){
  var div_char ="|§|"; // variabile globale di include/lib.php
  var val = "";
  var multival = "";
  var multival_app = "";
  var vuoto = false;
  if (r.value == "="){
    for (var i=0;i<sel.length;i++){
      if (sel[i].selected && sel[i].value != "vuoto"){
	      val = sel[i].value;
        multival += val + div_char;
      } else if (sel[i].selected && sel[i].value == "vuoto"){
	      vuoto = true;
	    }
    } // end for
	
   if (!vuoto){
     multival = multival.substring(0, multival.length - div_char.length);
     document.form_selezione.multival.value = multival;
   }
  } // end if
}

// aggiorna la pagina automaticamente solo se 
// è selezionata la relazione 'compreso tra'
function aggiorna_relazione() {
  thisform = document.form_selezione;
  thiselem = document.form_selezione.rel1;
  rel = thiselem.value;
  if ((rel == "between") || (rel == "=")) {
    thisform.submit();
  }
}

/*
  FILE prepara_query_di_selezione.php
*/

	function prepareFields(f1,f2){
      var sel = false;
      if (f2.eseguiCond.length > 0){
	  for (var i=0;i < f2.eseguiCond.length;i++){
	    if (f2.eseguiCond[i].checked){
		  sel = true;
		  var k = 0;
 	      clausola_query = "clausola_query["+(i+1)+"]["+k+"]";
 	      ex_clausola_query = "exclausola_query[1]["+k+"]";

		  while (document.getElementById(clausola_query) != null){
		    val_clausola_query = document.getElementById(clausola_query);
			val_ex_clausola_query = document.getElementById(ex_clausola_query);
			val_ex_clausola_query.value =  val_clausola_query.value;
		    k++;
 		    clausola_query = "clausola_query["+(i+1)+"]["+k+"]";
 		    ex_clausola_query = "exclausola_query[1]["+k+"]";
		  }
		  id_campi_query = "id_campi_query["+(i+1)+"][0]";
		  
		  val_id_campi_query = document.getElementById(id_campi_query);
		  f1.eseguiCond.value     = f2.eseguiCond[i].value;
		  f1.id_campi_query.value = val_id_campi_query.value;
		} // end if
	  } // end for
	  } else {
	    sel = true;
	  }
	  if (!sel){
	   alert("Selezionare una condizione");
	   return(false);
	  } else {
	   return(true);
	  }
	}

	function selectAll(f){
      var i=1;
	  condNum = "condNum["+i+"]";  
	  val_condNum = document.getElementById(condNum);
//alert(val_condNum.value);	

	  while (val_condNum != null){
	      val_condNum.checked = true;
          i++;
		  condNum = "condNum["+i+"]";  
		  val_condNum = document.getElementById(condNum);
	  }
	}

/*
  FILE esegui_query_di_selezione.php
*/

//conta i caratteri digitati nell'area di testo
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}

//controlla il corretto inserimento dei dati prima di passare
//alla pagina successiva
function checkFields(f){
  var noError = true;

  if (f.testo_sms.value == ""){
    alert("Non è possibile spedire SMS vuoti");
	  noError =  false;
  } else {
  
  var trovato = false;
	var i = 0;
	var var_socio = "socio["+(i+1)+"]";
	var socio = document.getElementById(var_socio);
  while (!trovato && socio != null) {
	  if (socio.checked){
	    trovato = true;
	  }
	  i++;
	  var_socio = "socio["+(i+1)+"]";
	  socio = document.getElementById(var_socio);
	}
	
	if (trovato){
      if (confirm ("Ci sono numeri da eliminare, sei sicuro di volerli spedire?")) {
	      noError = false;
			}
	}//end trovato
  if (f.cellulare.value != "") {
	    if (!confirm ("Vuoi aggiungere un'altro numero?")){
	      noError = false;
		}
	}
	} // end if testo vuoto
  if (noError){
    return true;
  } else {
		return false;
  }
}

