// Funzione STANDARD per la verifica dei campi obbligatori in un form e la verifica di validita' dell'indirizzo email.
// note: aggiongere ad ogni input obbligatorio l'attributo -> class="obbligatorio"
var err_color = '#f90600';
var ok_color = '#07c830';

var codiceCaptcha = creaCodice();
var codice = codiceCaptcha[1];
var codiceTestuale = codiceCaptcha[0];
var msg_ita = new Array("non selezionato ", "Campi obbligatori non compilati\n", "Indirizzo email non corretto\n", "Avete dimenticato di accettare l'informativa sulla privacy\n", "Caratteri di controllo errati!","\n- Carattere chiocciola (@) mancante o in posizione errata","\n- Hai digitato dei caratteri non ammessi","\n- Il punto (.) e' in posizione errata","\n- Ci sono due caratteri punto (.) vicini","\n- Ci sono due caratteri trattino (-) vicini","\n- Non hai indicato il dominio (.it .com .net ecc..)");

var msg_eng = new Array (" is not selected", "Required field missing\n", "Wrong email address\n", "You forgot to accept the privacy policy \n", "Wrong control characters!","\n- The @ is missing or in the wrong place","\n- You typed in inadmissible characters","\n- The dot (.) is in the wrong place","\n- There are two dots (.) near each other","\n- There are two dashes (-) near each other","\n- You did not specify the domain (.it .com .net etc.)");

/*
* # Controllo dei campi obbligatori in input
* @ theForm: l'oggetto form (obbligatorio)
* @ language: string - la lingua (default 'italiano')
* @ noCaptcha: int - se settato a 1 salta il controllo del captcha
* @ noPrivacy: int - se settato a 1 salta il controllo dell'accetazione della privacy
*/

function checkForm(theForm, language, noCaptcha, noPrivacy)
{
	language = ( typeof language != "undefined" )?language:'italiano';
	if(language == 'italiano') {
			msg = msg_ita;
			messaggio_alert = 'ATTENZIONE!\n';}
	else {
		msg = msg_eng;
		messaggio_alert = 'WARNING!\n';
	}
	
	
	errore = 0;
	ret = true;
	
	f = theForm.elements;
	len = f.length;

	for(i=0;i<len;i++) {
		theInput = f[i];

		if(theInput.className == 'obbligatorio'){
			switch(theInput.type) {
				case 'checkbox': case 'radio':
					ok = false;
					nome = theInput.name;
					while(f[i].name == nome) {
						if (f[i].checked || f[i].name == 'privacy'){
							ok = true;
						}
						i++;
					}
					if(!ok) { 
						messaggio_alert += "| "+nome+" | "+msg[0];
						errore++;
					}
					break;
				default:
					if(theInput.value == '') {
						//theInput.style.background = '#F0E68C';
						theInput.style.border='2px solid '+err_color;
						errore++;
					}else
						//theInput.style.background = '#fff';	
						theInput.style.border='2px solid '+ok_color;
					break;
			}	
		}	
	}
	if(errore > 0)	messaggio_alert += msg[1];
	
	if(typeof f.email != "undefined"){ err_mail = check_email(f.email.value, language); m = f.email}
	else if(typeof f.email_utente != "undefined"){ err_mail = check_email(f.email_utente.value, language); m = f.email_utente }
	else err_mail = '';
	if(err_mail != '') {
		messaggio_alert += msg[2];
		errore++;
		m.style.border='2px solid '+err_color;
	}else {
	/*	if(f.email != undefined)*/  m.style.border='2px solid '+ok_color;
	}
	
	if (typeof noPrivacy == "undefined") {
		if (f.privacy.checked==false) {	  
	  		messaggio_alert += msg[3];
	  		errore++;
		}
	}
	
	if (typeof noCaptcha == "undefined") {
		if(f.ciapcia.value != codiceTestuale){	  
	  		messaggio_alert += msg[4];
	 		errore++;
		}
	}
	
	if(errore > 0){ 
		ret = false;
		alert(messaggio_alert);
	}
	
	return ret;
}

function AddRemoveInputs(op, htmlInputs, targetID)
{	
	if(op == "aggiungi") {
		document.getElementById(targetID).innerHTML = htmlInputs;
	}else{
		document.getElementById(targetID).innerHTML = '';
	}
}


function creaCodice() 
{ 	
	var captcha			= new Array();
	var chars 			= "xabcdefghijklmnopqrstuvwxyz";
	var string_length 	= 4;
	var dotpos			= 10;
	var randomstring 	= '';
	var randomimgstring	= '';
	var stringa = '';

	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
		randomimgstring += "<img src='http://www.gate2000.com/utility/captcha/lettera"+rnum+".gif' />";
	}

	captcha [0] = randomstring;
	captcha [1] = randomimgstring;
	
	return(captcha);
} 


function check_email(email, language) {

errors = '';
if(language == 'italiano') msg = msg_ita;
else msg = msg_eng;
var filter = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i

if (!filter.test(email)) {
	errors = '1';
}

return errors;
}


function hidediv(id)
{ 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById(id).style.display = 'none'; 
	}else { 
		if (document.layers) { // Netscape 4 
			document.id.visibility = 'hidden'; 
		}else { // IE 4 
			document.all.id.style.visibility = 'hidden'; 
		} 
	} 
}

// Mostra l'elemento nascosto con id = id
//    compatibile FF iE Safari Netscape
function showdiv(id) 
{ 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById(id).style.display = 'block'; 
	} 
	else { 
		if (document.layers) { // Netscape 4 
			document.pof.visibility = 'visible'; 
		}else { // IE 4 
			document.all.pof.style.visibility = 'visible'; 
		} 
	} 
} 

// Mostra/Nasconde l'elemento con id = id a seconda del suo stato.
//  nota: a volte per funzionare ha bisogno che la proprieta' display sia dichiarata
//		inline (ie: style="display:none")
function ShowHide(id) {
	if(document.getElementById(id).style.display == 'none')
		showdiv(id);
	else 
		hidediv(id);
}
