/*****************************************/
//MoCS.js 1.0b
// Modular Contacts Suite - Javascript gear
// by DWJ 2006 (CC-GNU GPL)
// dwjack/at/tin.it
//Published under CC-GNU GPL
//http://creativecommons.org/licenses/GPL/2.0/
/*****************************************/
//
//Form Verifier 3.2.1b
/**********************************************/

/************ helper functions ****************/
function getText (el) {
         // from http://www.thescripts.com/forum/thread596105.html
         if (el.textContent) {return el.textContent;}
         if (el.innerText) {return el.innerText;}
         if (typeof el.innerHTML == 'string') {return el.innerHTML.replace(/<[^<>]+>/g,'');}
}
// verifica se l'elemento è nell'array
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
// verifica se l'elemento esiste nell'array errors e se non esiste lo inserisce
function checkAgainst (campo) {
         if (errors.inArray(campo) != true) {
            errors.push(campo);
         }
         return errors;
}
// array degli errori
var errors = new Array();
var formparser = "";
////////////////////////////////// funzione principale //////////////////////////////////////////
function verify() {
         // imposta la variabile di controllo errori
         // ciclo attraverso i campi del modulo
         var x = document.forms[0].elements;
         for (i=0;i<x.length;i++) {
             var valore = x[i].value;
             var campo = x[i].name;
             var tipo = x[i].type;
             var stile = x[i].style;
			 var stato = x[i].title;
//ï¿½richiesto?
                 if (stato=="obbligatorio") {
//campo di testo
                    if ((tipo == "text" || tipo == "select-one" || tipo == "textarea") && valore == '') {
                       checkAgainst (campo);
                    }
//fine if stato==obbligatorio / fine controllo campi obbligatori
          }
// fine ciclo for
        }
}
//funzioni speciali//
///////////////////////////controllo correttezza sintattica campo email /////////////////////////////////
function checkMail (mail) {
var x = document.getElementsByName(mail)[0];
filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
if (x.value !="" && !filtro.test(x.value)) {
checkAgainst (mail);
}
}
///////////////////////////controllo per campi telefonici con cellulari non consentiti (beta script) ////////////////////
// consente rete fissa e numeri verdi con prima cifra 8
function checkphone (phonefield) {
var x = document.getElementsByName(phonefield)[0];
if (x.value !="" && (x.value.indexOf("8")+x.value.indexOf("0"))==-2) {
checkAgainst (phonefield);
}
}
///////////////////////////controllo per campi con soli numeri /////////////////////////////////
function checkNumbers (field,limit) {
var x = document.getElementsByName(field)[0];
var ren =new RegExp("[0-9]{"+limit+"}");
if (ren.test(x.value)==false && x.value !="") {
checkAgainst (field);
}
}
///////////////////////////controllo per campi con soli caratteri /////////////////////////////////
function checkLetters (field,limit) {
var x = document.getElementsByName(field)[0];
var ren =new RegExp("[a-zA-Z]{"+limit+"}");
if (ren.test(x.value)==false && x.value !="") {
checkAgainst (field);
}
}
///////////////////////////controllo per campi con soli caratteri /////////////////////////////////
function checkChars (cType,field,limit) {
  if (cType == "l") {var toCheck = "[a-zA-Z]";}
  else if (cType == "n") {var toCheck = "[0-9]";}
var x = document.getElementsByName(field)[0];
var ren =new RegExp(toCheck+"{"+limit+"}");
if (ren.test(x.value)==false && x.value !="") {
checkAgainst (field);
}
}
/***************************************************************/
function checkbox (checkbox,min) {
var minCheck = !min ? 1 : min;
var xs = document.getElementsByName(checkbox);
if (xs.length > 1 ) {
var checkErrors = 0;
for (i=0;i<xs.length;i++) {
if (xs[i].type == "checkbox" && xs[i].checked == false) {
checkErrors++;
}
}
alert (checkErrors);
if (checkErrors > xs.length-minCheck) {checkAgainst (checkbox);}
}
else if (xs.length == 1 && xs[0].type == "checkbox" && xs[0].checked == false) {
checkAgainst (checkbox);
}
}
////////////////////////  rosso su campi non compilati o errati  /////////////////////////////////
function redhighlight (metodo) {
// valore iniziale dell'errorlog nel caso il metodo sia verboso x tutti i campi
   var errorlog = metodo=="verbose" ? "I seguenti campi sono vuoti o errati:\n\n" : "";
// raccolta delle etichette
   var etichette = document.getElementsByTagName('form')[0].getElementsByTagName('label');
   var x = document.forms[0].elements;
   for (i=0;i<x.length;i++) {
       var campo = x[i].name;
       if (errors.inArray(campo) && metodo) {
          for (a=0;a<etichette.length;a++) {
// verifica se il metodo getAttribute per for è supportato altrimenti usa il metodo IE
          var labelValue = etichette[a].getAttribute('for') == null ? etichette[a].attributes['for'].value : etichette[a].getAttribute('for');
//
          if(labelValue == campo && metodo == "single") {
                       errorlog = "Il campo "+getText(etichette[a])+" è vuoto o errato";
                       x[i].focus();
                       alert(errorlog);
                       return;
          }
          if(labelValue == campo && metodo == "verbose") {
                        errorlog += getText(etichette[a])+"\n";
          }
// fine for labels
          }
// fine metodo
}
        else if (errors.inArray(campo) && !metodo) {
             x[i].style.backgroundColor = "#FF5656";
             errorlog = "Attenzione: i campi evidenziati in rosso sono vuoti o errati";
        }
        else if (!errors.inArray(campo) && !metodo) {
             x[i].style.backgroundColor = "";
             }
// fine for campi
   }
alert (errorlog);
}
//////////////////// verifica numero errori ////////////////////////
function errorcheck (metodo){
         if (errors.length !=0){
            redhighlight (metodo);
            errors = null;
            errors = new Array();
// uccidi il ciclo
            return false;
          }
          else {
//invio modulo a php
///////////////////////////////////////// indirizzo form php ////////////////////
                 document.forms[0].action = formparser+"?check=1";
                 }
}
