// controlla il numero di caratteri di una textarea
function ismaxlength(obj){
var mlength = obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
if (obj.getAttribute && obj.value.length > mlength)
	obj.value = obj.value.substring(0,mlength);
}

// Aggiunge un parametro per ricerca Editoriale
function AddParam(nomeform, campo, valore) {
	campo.value = valore;
	nomeform.submit();
}

function avanti(nomeform, dove, chk) {
	if (dove!=null) {
		if (dove.lastIndexOf(".php")!=-1) {
			nomeform.action=dove;
		} else {
			nomeform.action='home.php?pag=' + dove;
		}
	}
	if (chk) {
		if (nomeform==null) {
			nomeform=valida();
			if(nomeform) { nomeform.submit(); }
		} else {
			if (valida(nomeform)) { nomeform.submit(); }
		}
	} else {
		nomeform.submit();
	}
}
function checkData(data) {
   var date_array = data.split('/');
   var day = date_array[0];
   var month = date_array[1] - 1;
   var year = date_array[2];
   source_date = new Date(year,month,day);
	if((year != source_date.getFullYear())||(month != source_date.getMonth())) {
		if(linguaggio=="it") {
      		alert('La data non è valida.');
		} else {
      		alert('The date is not valid.');
		}
	  return false;
   }
  return true;
}


function ColorValidate(ris,campo,ValColor) {
	var campoTXT = campo.name.toString();
	var cmp = campoTXT + "Error";
	if (ris) {
		if(ValColor) document.getElementById(cmp).style.display='none';
		if(document.getElementById(campoTXT)!=null) {
			document.getElementById(campoTXT).className='';
		} else {
			campo.className='';
		}
	} else {
		if(ValColor) document.getElementById(cmp).style.display='inline';
		if(document.getElementById(campoTXT)!=null) {
			document.getElementById(campoTXT).className='CampoErrore';
		} else {
			campo.className='CampoErrore';
		}
	}
}
function RBCheck(radio) {
	var radioLength = radio.length;
	if(radioLength == undefined)
		if(radio.checked)
			return radio.value;
		else
			return "";
		for(var i = 0; i < radioLength; i++) {
			if(radio[i].checked) {
			return radio[i].value;
		}
	}
	return "";
}

/*
function RBCheck(radio) {
	var cosa = "";
	var radioLength = radio.length;
	if(radioLength == undefined) {
		if(radio.checked) {
			cosa = radio.value;
		} else {
			for(var i = 0; i < radioLength; i++) {
				if(radio[i].checked) cosa = radio[i].value;
			}
		}
	}
	return cosa;
}
*/

function Pieno(campo, LabelCampo,check){
	var cosa=true
	if (campo.value=="") {
		if (check) {
			if(linguaggio=="it") {
				alert("Il campo " + LabelCampo + " è obbligatorio.\nPer proseguire è necessario inserire un valore.");
			} else {
				alert("The field " + LabelCampo + " is required.\nTo continue, you must enter a value.");
			}
		}
		cosa=false;
	}
	return cosa;
}

function Stringa(check, obbl,campo,LabelCampo, minvalue,maxvalue,ValColor){
	var cosa=true;
	// sostituisco il valore della stringa con il trim del valore in modo da scartare le stringhe formate solo da spazi
	campo.value=campo.value.replace(/^\s+|\s+$/g, '');
	if ((obbl==false)&&(campo.value!=""))obbl=true;
	if (obbl==true){
		var cosa=Pieno(campo,LabelCampo,check);
		if((minvalue!=null) &&(maxvalue!=null)) {
			if(campo.value.length < minvalue) {
				if(check) {
					if(linguaggio=="it") {
						alert("Il campo " + LabelCampo + " deve avere almeno " + minvalue + " caratteri.");
					} else {
						alert("The field " + LabelCampo + " must have at least " + minvalue + " characters.");
					}					
				}
				cosa=false;
			} else if(campo.value.length > maxvalue) {
				if(check) {
					if(linguaggio=="it") {
						alert("Il campo " + LabelCampo + " deve avere al massimo " + maxvalue + " caratteri.");
					} else {
						alert("The field " + LabelCampo + " must have a maximum of " + maxvalue + " characters.");
					}						
				}
				cosa=false;
			} 
		}
	}
	if (obbl) ColorValidate(cosa,campo,ValColor);
	if(!check) cosa=false; 
	return cosa;
}
function DataJS(check, obbl,campogg,campomm,campoaa,LabelCampo,ValColor){
	var cosa=true;
	var gg = campogg[campogg.selectedIndex].value;
	var mm = campomm[campomm.selectedIndex].value;
	var aa = campoaa[campoaa.selectedIndex].value;
	var data=gg + "/" + mm + "/" + aa;
	cosa=checkData(data);
	if(!cosa) {
		ColorValidate(cosa,campogg,true);
		ColorValidate(cosa,campomm,false);
		ColorValidate(cosa,campoaa,false);
	}
	if(!check) cosa=false; 
	return cosa;
}

function CheckEmail(check,obbl,campo,LabelCampo,ValColor){
	var cosa=true;
	if ((obbl==false)&&(campo.value!="")){
		obbl=true;
	}
	if (obbl==true){
		cosa=Pieno(campo,LabelCampo,check);
		if (cosa) {
			var posat=campo.value.indexOf("@");
			if(posat==-1) {
				if(check) {
					if(linguaggio=="it") {
						alert("Inserire correttamente i valori del campo " + LabelCampo + ".");
					} else {
						alert("Enter correct values into the " + LabelCampo + "field.");
					}						
				}
				cosa=false;
			} else {
				var dominio = campo.value.substr(posat+1);
				if (dominio=="") {
					if(check) {
						if(linguaggio=="it") {
							alert("Inserire correttamente il dominio (es. nomeutente@NOMEDOMINIO.VA) del campo " + LabelCampo + ".");
						} else {
							alert("Enter correct domain (i.e. nameuser@DOMAIN.VA) of the field " + LabelCampo + ".");
						}						
						
					}
					cosa=false;
				} else {
					var ext=dominio.lastIndexOf(".");
					if (ext==-1) {
						if(check) {
							if(linguaggio=="it") {
								alert("Inserire correttamente il punto dopo il dominio (es. nomeutente@nomedominio.va) del campo " + LabelCampo + ".");
							} else {
								alert("Fill in the right position, the dot after the domain (i.e. nameuser@domain.va) of the field " + LabelCampo + ".");
							}						
						}						
						cosa=false;
					} else {
						if(ext<2){
							if(check) {
								if(linguaggio=="it") {
									alert("Inserire correttamente il nome del dominio (es. nomeutente@NOMEDOMINIO.va) del campo " + LabelCampo + ".");
								} else {
									alert("Fill in the right position, the domain (i.e. nameuser@DOMAIN.va) of the field " + LabelCampo + ".");
								}						
							}
							cosa=false;
						} else{
							var extdom=dominio.substr(ext+1);
							if ((extdom=="")||(extdom.length < 2)) {
								if(check) {
									if(linguaggio=="it") {
										alert("Inserire correttamente l\'estenzione del dominio (es. nomeutente@nomedominio.VA) del campo " + LabelCampo + ".");
									} else {
										alert("Enter the Extension of the domain (es. nameuser@domain.VA) of the field " + LabelCampo + ".");
									}						
								}
								cosa=false;
							} 
						}
					}
				}
			}
			
			//if (campo.value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
			if (campo.value.search(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/) != -1) {
				cosa = true;
			} else {
		    	if(linguaggio=="it") {
					alert("Inserire correttamente il valore del campo " + LabelCampo + ".");
				} else {
					alert("Enter a correct value of field " + LabelCampo + ".");
				}				
				cosa = false;
			}
		}
	}
	if (obbl) ColorValidate(cosa,campo,ValColor);
	if(!check) cosa=false; 
	return cosa;
}

function Caratteri(check, obbl,campo,LabelCampo, minvalue,maxvalue,ValColor){
	var cosa=true;
	if ((obbl==false)&&(campo.value!="")){
		obbl=true;
	}
	if (obbl==true){
		var cosa=Pieno(campo,LabelCampo,check);

		if((minvalue!=null)&&(maxvalue!=null)) {
			if(campo.value.length < minvalue) {
				if(check) {
					if(linguaggio=="it") {
						alert("Il campo " + LabelCampo + " deve avere almeno " + minvalue + " caratteri.");
					} else {
						alert("The field " + LabelCampo + " must have at least " + minvalue + " characters.");
					}						
					
				}
				cosa=false;
			} else if(campo.value.length > maxvalue) {
				if(check) {
					if(linguaggio=="it") {
						alert("Il campo " + LabelCampo + " deve avere al massimo " + maxvalue + " caratteri.");
					} else {
						alert("The field " + LabelCampo + " must have a maximum of " + maxvalue + " characters.");
					}						
					
					
				}
				cosa=false;
			} 
		}
		if((check)&&(cosa)) {
			var strexp="[a-zA-Z]";
			if(minvalue!=null) strexp=strexp + "{" + minvalue;
			if(maxvalue!=null) {
				if(minvalue!=null) strexp=strexp + ",";
				strexp=strexp + maxvalue;
			}
			if ((minvalue!=null)||(maxvalue!=null)) strexp+="}";
 
			var espressione = new RegExp(strexp);
			if (!espressione.test(campo.value)){
					if(linguaggio=="it") {
						alert("Per il campo " + LabelCampo + " sono ammessi solo i caratteri alfabetici da A a Z.");
					} else {
						alert("The field " + LabelCampo + " accepts only alphanumeric characters (A - Z).");
					}						
				
				
				cosa=false;
			}
		}
	}
	if (obbl) ColorValidate(cosa,campo,ValColor);
	if(!check) cosa=false; 
	return cosa;
}

function Nota(check, obbl,campo,LabelCampo, minvalue,maxvalue,ValColor){
	var cosa=true;
	if ((obbl==false)&&(campo.value!="")){
		obbl=true;
	}
	if (obbl==true){
		var cosa=Pieno(campo,LabelCampo,check);
		if((minvalue!=null)&&(maxvalue!=null)) {
			if(campo.value.length < minvalue) {
				if(check) {
					if(linguaggio=="it") {
						alert("Il campo " + LabelCampo + " deve avere almeno " + minvalue + " caratteri.");
					} else {
						alert("The field " + LabelCampo + " must have at least " + minvalue + " characters.");
					}						
					
				}
				cosa=false;
			} else if(campo.value.length > maxvalue) {
				if(check) {
					if(linguaggio=="it") {
						alert("Il campo " + LabelCampo + " deve avere al massimo " + maxvalue + " caratteri.");
					} else {
						alert("The field " + LabelCampo + " must have a maximum of " + maxvalue + " characters.");
					}						
					
					
				}
				cosa=false;
			} 
		}
		if((check)&&(cosa)) {
			var stringa=campo.value;
			var lunghezza=stringa.length;
			stringa=stringa.replace("(", "");
			stringa=stringa.replace(")", "");
			stringa=stringa.replace("{", "");
			stringa=stringa.replace("}", "");
			stringa=stringa.replace("[", "");
			stringa=stringa.replace("]", "");
			stringa=stringa.replace("$", "");
			stringa=stringa.replace("#", "");
			if (lunghezza != stringa.length){
					if(linguaggio=="it") {
						alert("Per il campo " + LabelCampo + " non sono ammessi i caratteri ( ) { } [ ] $ #");
					} else {
						alert("The field " + LabelCampo + " does not accept characters ( ) { } [ ] $ #");
					}						
				
				
				cosa=false;
			}
		}
	}
	if (obbl) ColorValidate(cosa,campo,ValColor);
	if(!check) cosa=false; 
	return cosa;
}


function SelectCheck(check, obbl,campo,LabelCampo, ValidaIndex, ValColor){
	var cosa=true;
	var NoItem = 0;
	var scelta = campo.selectedIndex;
	if (ValidaIndex != null) NoItem=ValidaIndex;
	if (obbl) {
		if(check) {
			if (scelta == 0) { 
				cosa=false;
				if(linguaggio=="it") {
					alert("Selezionare per il campo " + LabelCampo + " un valore valido.");
				} else {
					alert("Select from the field " + LabelCampo + " a valid item.");
				}					
			}
		}
	}
	if(scelta == NoItem) cosa=false; 
	if (obbl) ColorValidate(cosa,campo,ValColor);
	if(!check) cosa=false; 
	return cosa;
}

function NumRomano(check, obbl,campo,LabelCampo,ValColor){
	var cosa=true;
	if ((obbl==false)&&(campo.value!="")){
		obbl=true;
	}
	if (obbl==true){
		cosa=Pieno(campo,LabelCampo,check);
		if (cosa==true) {
			var valore = campo.value;
			if (isNaN(valore)){
				if(check) {
					//SOSTITUISCO carattere vuoto PER TUTTI CARATTERI ROMANI
					valore = valore.toUpperCase;
					valore = valore.replace("I","");
					valore = valore.replace("V","");
					valore = valore.replace("X","");
					valore = valore.replace("L","");
					valore = valore.replace("C","");
					valore = valore.replace("M","");
					if(valore!="") {
						if(linguaggio=="it") {
							alert("Per il campo " + LabelCampo + " sono ammessi solo i numeri da 0 a 9 o numeri romani.");
						} else {
							alert("The field " + LabelCampo + " accepts only digits from 0 to 9 or roman numerals.");
						}						
						cosa=false;
					}
				}
			}
		}
	}
	if (obbl) ColorValidate(cosa,campo,ValColor);
	if(!check) cosa=false; 
	return cosa;
}
function IsNumero(check, campo){
	var cosa = check;
	if(check) {
		if(campo.value!="") {
			if (isNaN(campo.value)){
				cosa=false;
			} else {
				cosa=true;
			}
		} else {
			cosa = false;
		}
	}
	return cosa;
}

function Importo(check, obbl,campo,LabelCampo, ValColor){
	var cosa=false;
	if ((obbl==false)&&(campo.value!="")){
		obbl=true;
	}
	if (obbl==true){
		if(check) {
			// E' un numero (cosa = false)
			// Numero intero o decimale
			if (parseInt(campo.value)!=campo.value) {
					if(linguaggio=="it") {
						alert("Per il campo " + LabelCampo + " sono ammessi solo valori interi e superiori a zero.");
					} else {
						alert("The field " + LabelCampo + " accept only value integer and greater than zero.");
					}	
			} else {
				if(campo.value <= 0) {
					if(linguaggio=="it") {
						alert("Per il campo " + LabelCampo + " sono ammessi solo valori superiori a zero.");
					} else {
						alert("The field " + LabelCampo + " accept only value greater than zero.");
					}	
				} else {
					cosa = true;
				}
			}
		}
	}
	if (obbl) ColorValidate(cosa,campo,ValColor);
	if(!check) cosa=false; 
	return cosa;
}

function Numero(check, obbl,campo,LabelCampo, minvalue,maxvalue,ValColor){
	var cosa=true;
	if ((obbl==false)&&(campo.value!="")){
		obbl=true;
	}
	if (obbl==true){
		cosa=Pieno(campo,LabelCampo,check);
		if (cosa==true) {
			if((minvalue!=null) && (maxvalue!=null)) {
				if((campo.value.length < minvalue)) {
					if(check) {
					if(linguaggio=="it") {
						alert("Il campo " + LabelCampo + " deve avere almeno " + minvalue + " cifre.");
					} else {
						alert("The field " + LabelCampo + " must have at least " + minvalue + " digits.");
					}						
						
						
					}
					cosa=false;
				} else if(campo.value.length > maxvalue){
					if(check) {
					if(linguaggio=="it") {
						alert("Il campo " + LabelCampo + " deve avere al massimo " + maxvalue + " cifre.");
					} else {
						alert("The field " + LabelCampo + " must have a maximum of " + maxvalue + " digits.");
					}						
						
						
					}
					cosa=false;
				}
			} else {
				if (isNaN(campo.value)){
					if(check) {
						if(linguaggio=="it") {
							alert("Per il campo " + LabelCampo + " sono ammessi solo i caratteri numerici da 0 a 9.");
						} else {
							alert("The field " + LabelCampo + " accepts only digits from 0 to 9.");
						}						
					}
					cosa=false;
				}
			}
		}
	}
	if (obbl) ColorValidate(cosa,campo,ValColor);
	if(!check) cosa=false; 
	return cosa;
}

function Password(check, obbl,campo,lbl,minvalue, maxvalue,ValColor){
	var cosa=true;
	if (cosa){
		if (obbl==true){
			cosa=Pieno(campo,lbl,check);
			if (cosa==true){
				if(campo.value.length < minvalue) {
					if(check) {
						if(linguaggio=="it") {
							alert("Il campo " + lbl + " deve avere almeno " + minvalue + " caratteri.");
						} else {
							alert("The field " + lbl + " must have at least " + minvalue + " characters.");
						}						
					}
					cosa=false;
				} else if(campo.value.length > maxvalue){
					if(check) {
						if(linguaggio=="it") {
							alert("Il campo " + lbl + " deve avere al massimo " + maxvalue + " caratteri.");
						} else {
							alert("The field " + lbl + " must have a maximum of " + maxvalue + " characters.");
						}						
					}
					cosa=false;
				}
			}
		}
	}
	if (obbl) ColorValidate(cosa,campo,ValColor);
	if(!check) cosa=false; 
	/* FINE VERIFICA DELLA PASSWORD CRYPTO PAROLA */
	if ((check)&&(cosa)) {
		var input = campo.value;
		input = input.replace(" ","");
		pwd=hex_md5(input);
		campo.maxLenght = 32;
		campo.value = pwd;
		if(campo.value != pwd) {
			cosa=false;
			campo.value = "";
		}
	}
	return cosa;
}

function Segreta(check, obbl,p,cp, minvalue, maxvalue,ValColor){
	var cosa=true;
	var cosa2=false;
	if (cosa){
		if (obbl==true){
			cosa=Pieno(p,'Password',check);
			if (cosa){
				if(p.value.length < minvalue) {
					if(check) {
						if(linguaggio=="it") {
							alert("Il campo Password deve avere almeno " + minvalue + " caratteri alfanumerici.");
						} else {
							alert("The field Password must have at least" + minvalue + " alphanumeric characters.");
						}						
					}
					cosa=false;
				} else if(p.value.length > maxvalue){
					if(check) {
						if(linguaggio=="it") {
							alert("Il campo Password deve avere al massimo " + maxvalue + " caratteri alfanumerici.");
						} else {
							alert("The field Password must have a maximum of " + maxvalue + " alphanumeric characters.");
						}						
					}
					cosa=false;
				} else {
					var cosa2=Pieno(cp,'Conferma Password',check);	
					if ((cosa2)&&(cosa)){
						for(i=0; i <= p.value.length-1; i++){
							var car=p.value.substr(i,1);
							if ((car=="&") || (car==" ") || (car==",") || (car=="|") || (car=="\"") || (car=="'")|| (car=="\\")|| (car=="(")|| (car==")")|| (car=="[")|| (car=="]")){
								cosa=false;
								if(check) {
									if(linguaggio=="it") {
										alert("Per il campo Password non sono ammessi i seguenti caratteri:\n' \" & | , \ ( ) [ ] <spazio>");
									} else {
										alert("The field Password not accept the following characters:\n' \" & | , \ ( ) [ ] <space>");
									}						
								}
								break;
							}
						}
						if ((cosa2)&&(cosa)){
							if (p.value != cp.value){
								cosa2=false;
								if(check) {
									if(linguaggio=="it") {
										alert("Il campo Password e il campo Conferma Password non coincidono.");
									} else {
										alert("The field Password and the field Confirm Password not are equal.");
									}						
								}
							} 
						}
					}
				}
			} 
		}
	}
	if (obbl) ColorValidate(cosa,p,ValColor);
	if (obbl) ColorValidate(cosa2,cp,ValColor);
	if(!cosa2) cosa=false;
	if(!check) cosa=false; 
	/* FINE VERIFICA DELLA PASSWORD CRYPTO PAROLA */
	/*if ((check)&&(cosa)) {
		var input = p.value;
		var input2 = cp.value;
		input = input.replace(" ","");
		input2 = input2.replace(" ","");
		pwd=hex_md5(input);
		cnfpwd=hex_md5(input2);
		if(pwd==cnfpwd) {
			p.value = pwd;
			cp.value = cnfpwd;
		} else {
			p.value = "";
			cp.value = "";
			ret = false;
		}
	}*/
	return cosa;
}

