function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

function IsNumeric(sText){
	return numberControl(sText,"0123456789");
}

function numberControl(sText,ValidChars){
	var IsNumber=true;
	var Char;
	for (i=0; i<sText.length && IsNumber==true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

function controllaCampoNumerico(sText, input_name){
	// sText.value = sText.value.replace(/[\,]/g,".");
	if(!IsNumeric(sText.value)){
		alert('Attenzione! I caratteri inseriti nel campo "' + input_name + '" non sono numerici!');
		// sText.value = "";
		sText.focus();
		return false;
	}
	return true;
}

function controllaLogin() {
	var minLen;
	minLen = 2;
	if (document.login_user.username.value.length < minLen) {
		alert("Il campo username \xE8 obbligatorio, deve contenere almeno due caratteri.\nPer favore, inserisci lo username !");
		document.login_user.username.focus();
		return false;
	}
	if (document.login_user.password.value.length < minLen) {
		alert("Il campo password \xE8 obbligatorio, deve contenere almeno due caratteri.\nPer favore, inserisci la password!");
		document.login_user.password.focus();
		return false;
	}
	document.login_user.submit();
}

function createLogin(){
	modulo = document.manage_admin;
	modulo.username.value = modulo.cognome.value.toUpperCase() + '_' + modulo.nome.value.toLowerCase();
}

function popNew(url, pop_name, y ,w) {
	t = (screen.height - y) / 2; 
	l = (screen.width - w) / 2;

	searchWin = window.open(url, pop_name, "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=" + w + ",height=" + y + ",left="+ l +",top="+ t);
	searchWin.focus();
}


function confermaAzione(frase, url_redirect){
	if (window.confirm(frase)){
		if (window.confirm('Sei sicuro di voler confermare l\'eliminazione? Tutti i files associati verranno eliminati!')){
			document.location = url_redirect;
		}
	}
}

function checkFormContatti(theForm){
	var minlen = 3;
	
	if(theForm.nome.value.length < minlen){
		alert ("ATTENZIONE! Il campo 'Nome' deve contenere almeno " + minlen + " caratteri!");
		theForm.nome.focus();
		return false;	
	}
	
	if(theForm.cognome.value.length < minlen){
		alert ("ATTENZIONE! Il campo 'Cognome' deve contenere almeno " + minlen + " caratteri!");
		theForm.cognome.focus();
		return false;	
	}
	
	if(theForm.email.value.length < minlen){
		alert ("ATTENZIONE! Il campo 'Email' deve contenere almeno " + minlen + " caratteri!");
		theForm.email.focus();
		return false;	
	}
	
	if(theForm.oggetto.value.length < minlen){
		alert ("ATTENZIONE! Il campo 'Oggetto della richiesta' deve contenere almeno " + minlen + " caratteri!");
		theForm.oggetto.focus();
		return false;	
	}
	
	if(theForm.testo.value.length < minlen){
		alert ("ATTENZIONE! Il campo 'Testo della richiesta' deve contenere almeno " + minlen + " caratteri!");
		theForm.testo.focus();
		return false;	
	}
	
	return true;
}

