//Almeno uno
function ckFrm_if_only(campo_if,campo_then,allerta){
	var re = /[\s]/g;
	var str_if = campo_if.value.replace(re,'');
	var str_then = campo_then.value.replace(re,'');
	if (campo_if.value != "" && campo_then.value == ""){
		alert("Attenzione: manca il contenuto di un campo !");
		campo.focus();
		return false;
	}
	return true;
}
//Controlla Se il campo è pieno o vuoto con controllo JS x SQL Injection
function ckFrm_empty(campo,allerta){
	var re = /[\s]/g;
	var str = campo.value.replace(re,'');
	if (str==""){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: il campo è vuoto!");
		campo.focus();
		return false;
	}
	return true;
}
//Controlla Se il campo è pieno o vuoto senza dare alert
function ckFrm_empty2(campo){
	var re = /[\s]/g;
	var str = campo.value.replace(re,'');
	if (str==""){
		return false;
	}
	return true;
}
//Controlla Se il campo è pieno o vuoto senza controllo JS x SQL Injection
function ckFrm_empty3(campo,allerta){
	var re = /[\s]/g;
	var str = campo.value.replace(re,'');
	if (str==""){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: il campo è vuoto!");
		campo.focus();
		return false;
	}
	return true;
}
//Controlla se almeno uno dei due campi è pieno
function ckFrm_empty_or(campoA,campoB,allerta){
	var str = campoA.value+campoB.value;
	var re = /[\s]/g;
	str = str.replace(re,'');
	if (str == ''){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: i due campi sono entrabi vuoti!");
		return false;
	}
	return true;
}
//Controlla la lunghezza di un valore inserito in un campo con controllo SQL injection
function ckFrm_length(campo,lunghezza,allerta){
	if(campo.value.length != lunghezza){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: la dimensione del campo non è uguale a "+lunghezza+" caratteri!");
		campo.focus();
		return false;
	}
	return true;
}
//Controlla la lunghezza di un valore inserito in un campo senza controllo SQL injection
function ckFrm_length2(campo,lunghezza,allerta){
	if(campo.value.length != lunghezza){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: la dimensione del campo non è uguale a "+lunghezza+" caratteri!");
		campo.focus();
		return false;
	}
	return true;
}
//Controlla la lunghezza fissa di un valore inserito in un campo
function ckFrm_length(campo,lunghezza,allerta){
	if(campo.value.length != lunghezza){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: la dimensione del campo non è di "+lunghezza+" caratteri!");
		campo.focus();
		return false;
	}
	return true;
}
//Controlla la lunghezza massima di un valore inserito in un campo con controllo SQL injection
function ckFrm_maxlength(campo,lunghezza,allerta){
	if(campo.value.length > lunghezza){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: la dimensione del campo supera i "+lunghezza+" caratteri!");
		campo.focus();
		return false;
	}
	return true;
}
//Controlla la lunghezza massima di un valore inserito in un campo senza controllo SQL injection
function ckFrm_maxlength2(campo,lunghezza,allerta){
	if(campo.value.length > lunghezza){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: la dimensione del campo supera i "+lunghezza+" caratteri!");
		campo.focus();
		return false;
	}
	return true;
}
//Controlla la lunghezza minima di un valore inserito in un campo
function ckFrm_minlength(campo,lunghezza,allerta){
	if(campo.length < lunghezza){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: la dimensione del campo è infeririore di "+lunghezza+" caretteri!");
		campo.focus();
		return false;
	}
	return true;
}
//Controlla se un valore di un campo, ricade trà due valori
function ckFrm_between(campo,strart_da,stop_a,allerta){
	if (campo.value.length < strart_da || campo.value.length > stop_a){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: il valore del campo\ndeve essere compreso tra "+strart_da+" e "+stop_a+"!");
		campo.focus();
		return false;
	}
	return true;
}
//Controllo del campo Email
function ckFrm_email(campo,allerta){
	value = campo.value;
	pattern = /[^@_\-\.\w\d]|@@|\.\.|__|^@|^\.|^_|@$|\.$|_$|@\.|\.@|\._|_\.|(@)[^@]*\1/g; 
	if((((value.match(/@/)) && (value.match(/\./))) == null) || (value.match(pattern) != null)){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: l'indirizzo e-mail inserito non è corretto!");
		campo.focus();
		return false;
	}
	return true;
}
//Controllo di un campo numerico
function ckFrm_number(campo,allerta){
	if (isNaN(campo.value)){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: il campo non è un numero!");
		campo.focus();
		return false;
	}
	return true;
}
//Controllo campo Data da valore completo
function ckFrm_isDateVal(campo,allerta){
	var regiornomese = new Array(2);
	var ok = true;
	var strdata = campo;
	var redata = new RegExp('^([0][1-9]|[1-2][0-9]|[3][0-1])[\/]([0][1-9]|[1][0-2])[\/](19|20)[0-9]{2}$');
	regiornomese[0] = new RegExp('^(((0[1-9]|1[0-9]|2[0-8])[\/]02)|((0[1-9]|[12][0-9]|30)[\/](04|06|09|11))|((0[1-9]|[12][0-9]|30|31)[\/](01|03|05|07|08|10|12)))$');
	regiornomese[1] = new RegExp('^(((0[1-9]|1[0-9]|2[0-9])[\/]02)|((0[1-9]|[12][0-9]|30)[\/](04|06|09|11))|((0[1-9]|[12][0-9]|30|31)[\/](01|03|05|07|08|10|12)))$');
	if(strdata.search(redata) == -1) ok = false;
	else{
		anno = strdata.substring(6,10);
		giornomese = strdata.substring(0,5);
		anno1 = anno.substring(0,2);
		anno2 = anno.substring(2,4);
		bisestile = 0;
		if((parseInt(anno/4) == anno/4) && ((anno2 != '00') || ((anno2 == '00') && (parseInt(anno1/4) == anno1/4)))) bisestile = 1;
		if(giornomese.search(regiornomese[bisestile]) == -1) ok=false;
	}
	if(!ok){
		if(allerta!=null) alert(allerta);
		else alert("Attention: Insert the date in the format gg/mm/aaaa");
		campo.focus();
		return false;
	}
	return true;
}
//Controllo campo Data da variabile
function ckFrm_isDateVar(campo,allerta){
	var regiornomese = new Array(2);
	var ok = true;
	var strdata = campo;
	var redata = new RegExp('^([0][1-9]|[1-2][0-9]|[3][0-1])[\/]([0][1-9]|[1][0-2])[\/](19|20)[0-9]{2}$');
	regiornomese[0] = new RegExp('^(((0[1-9]|1[0-9]|2[0-8])[\/]02)|((0[1-9]|[12][0-9]|30)[\/](04|06|09|11))|((0[1-9]|[12][0-9]|30|31)[\/](01|03|05|07|08|10|12)))$');
	regiornomese[1] = new RegExp('^(((0[1-9]|1[0-9]|2[0-9])[\/]02)|((0[1-9]|[12][0-9]|30)[\/](04|06|09|11))|((0[1-9]|[12][0-9]|30|31)[\/](01|03|05|07|08|10|12)))$');
	if(strdata.search(redata) == -1) ok = false;
	else{
		anno = strdata.substring(6,10);
		giornomese = strdata.substring(0,5);
		anno1 = anno.substring(0,2);
		anno2 = anno.substring(2,4);
		bisestile = 0;
		if((parseInt(anno/4) == anno/4) && ((anno2 != '00') || ((anno2 == '00') && (parseInt(anno1/4) == anno1/4)))) bisestile = 1;
		if(giornomese.search(regiornomese[bisestile]) == -1) ok=false;
	}
	if(!ok){
		if(allerta!=null) alert(allerta);
		else alert("Attention: Insert the date in the format gg/mm/aaaa");
		return false;
	}
	return true;
}
//Controllo campo Data
function ckFrm_isDate(campo,allerta){
	var regiornomese = new Array(2);
	var ok = true;
	var strdata = campo.value;
	var redata = new RegExp('^([0][1-9]|[1-2][0-9]|[3][0-1])[\/]([0][1-9]|[1][0-2])[\/](19|20)[0-9]{2}$');
	regiornomese[0] = new RegExp('^(((0[1-9]|1[0-9]|2[0-8])[\/]02)|((0[1-9]|[12][0-9]|30)[\/](04|06|09|11))|((0[1-9]|[12][0-9]|30|31)[\/](01|03|05|07|08|10|12)))$');
	regiornomese[1] = new RegExp('^(((0[1-9]|1[0-9]|2[0-9])[\/]02)|((0[1-9]|[12][0-9]|30)[\/](04|06|09|11))|((0[1-9]|[12][0-9]|30|31)[\/](01|03|05|07|08|10|12)))$');
	if(strdata.search(redata) == -1) ok = false;
	else{
		anno = strdata.substring(6,10);
		giornomese = strdata.substring(0,5);
		anno1 = anno.substring(0,2);
		anno2 = anno.substring(2,4);
		bisestile = 0;
		if((parseInt(anno/4) == anno/4) && ((anno2 != '00') || ((anno2 == '00') && (parseInt(anno1/4) == anno1/4)))) bisestile = 1;
		if(giornomese.search(regiornomese[bisestile]) == -1) ok=false;
	}
	if(!ok){
		if(allerta!=null) alert(allerta);
		else alert("Attention: Insert the date in the format gg/mm/aaaa");
		campo.focus();
		return false;
	}
	return true;
}
//RegExp Controllo dei caratteri speciali, consentendo le lettere accentate minuscole
function ckFrm_RegExpNick_Psw(campo,allerta){
	var StrTxt = campo.value;
	var re = /[\s]/g;
	StrTxt = StrTxt.replace("'","");
	StrTxt = StrTxt.replace("è","");
	StrTxt = StrTxt.replace("é","");
	StrTxt = StrTxt.replace("à","");
	StrTxt = StrTxt.replace("ò","");
	StrTxt = StrTxt.replace("ù","");
	StrTxt = StrTxt.replace("ì","");
	if(StrTxt.match(/[\W]/g)!=null){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: hai inserito dei caratteri non validi!");
		campo.focus();
		return false;
	}
	return true;
}
//RegExp Controllo dei caratteri speciali, consentendo le lettere accentate minuscole
function ckFrm_RegExp1(campo,allerta){
	var StrTxt = campo.value;
	var re = /[\s]/g;
	StrTxt = StrTxt.replace(re,"");
	StrTxt = StrTxt.replace("'","");
	StrTxt = StrTxt.replace("è","");
	StrTxt = StrTxt.replace("é","");
	StrTxt = StrTxt.replace("à","");
	StrTxt = StrTxt.replace("ò","");
	StrTxt = StrTxt.replace("ù","");
	StrTxt = StrTxt.replace("ì","");
	if(StrTxt.match(/[\W]/g)!=null){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: hai inserito dei caratteri non validi!");
		campo.focus();
		return false;
	}
	return true;
}
//RegExp Controllo dei caratteri speciali, consentendo le lettere accentate minuscole e non consentendo numeri e "_"
function ckFrm_RegExp2(campo,allerta){
	var StrTxt = campo.value;
	var re = /[\s]/g;
	StrTxt = StrTxt.replace(re,"");
	StrTxt = StrTxt.replace("'","");
	StrTxt = StrTxt.replace("è","");
	StrTxt = StrTxt.replace("é","");
	StrTxt = StrTxt.replace("à","");
	StrTxt = StrTxt.replace("ò","");
	StrTxt = StrTxt.replace("ù","");
	StrTxt = StrTxt.replace("ì","");
	var Txt = /[\W\^0123456789\^_]/g;
	if(StrTxt.search(Txt) != -1){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: hai inserito dei caratteri non validi!");
		campo.focus();
		return false;
	}
	return true;
}
//Controllo di disugualianza trà 2 campi
function ckFrm_diversi(campo_A,campo_B,allerta){
	if(campo_A.value != campo_B.value){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: i due campi non contengono lo stesso valore!");
		return false;
	}
	return true;
}
//Controllo di ugualianza trà 2 campi
function ckFrm_uguali(campo_A,campo_B,allerta){
	if(campo_A.value == campo_B.value){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: i due campi contengono lo stesso valore!");
		return false;
	}
	return true;
}
//Controlla se è stato selezionato un valore in una select
function ckFrm_selected(campo,allerta){
	var re = /[\s]/g;
	var str = campo.options[campo.selectedIndex].value.replace(re,"");
	if (str==""){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: seleziona la tua scelta!");
		campo.focus();
		return false;
	}
	return true;
}
//Controlla se è stato selezionato un valore in una select senza alert per poter associare la scelta ad un altro campo
function ckFrm_selected2(campo){
	var re = /[\s]/g;
	var str = campo.options[campo.selectedIndex].value.replace(re,"");
	if (str == "") return false;
	return true;
}
//Controlla se è stato selezionato un radio
function ckFrm_radio(campo,allerta){
	var found = false;
	for (var j=0;j<campo.length;j++){	
		if (campo[j].checked){
			found = true;
			break;
		}
	}
	if (!found){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: seleziona la tua scelta!");
		campo[0].focus();
		return false;
	}
	return true;
}
function ckFrm_file_caracters(campo,estensioni,allerta){//campo da controllare e messaggio di errore da mostrare(optionale)
	var tp=campo.value.substring(campo.value.lastIndexOf("\\")+1);
	var re = /[^.\-A-Za-z0-9_]/g;
	if (tp.match(re)){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: Il file caricato contiene caratteri non consentiti.");
		campo.value="";
		campo.focus();
		return false;
	}
	tp=campo.value.substring(campo.value.indexOf(".")+1);
	var tparray=estensioni.split(",");
	var tpflag=true
	for (key in tparray){
		if(tp==tparray[key]){
			tpflag=false;
			break;
		}
	}
	if(tpflag){
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: Il file caricato non e' consentito.");
		campo.value="";
		campo.focus();
		return false;
	}
	return true;
}
//Controlla se è stato selezionato un checkbox
function ckFrm_checkbox(campo,allerta){
	if(!campo.checked) {
		if(allerta!=null) alert(allerta);
		else alert("Attenzione: seleziona la tua scelta!");
		campo.focus();
		return false;
	}
	return true;
}