function campoVacio(campo,nombreCampo)
{
	if (campo.value=="") {
		alert("El campo "+nombreCampo+" no puede estar vacío");
		campo.focus();
		return true;
	}
	return false;
}

function isNumeric(n)
{
	var number = '0';
	var longi = n.length;
	for (var i = 0; i < longi; i++)
	{
		number = n.charAt(i);
		if (number >= '0' && number <= '9')
			continue;
		else 
			return false;
	}
	return true;
}

function isFloat (n)
{
	var number = '0';
	var nPoint = 0;
	for (var i = 0; i < n.length; i++)
	{
		number = n.charAt(i);
		if (number >= '0' && number <= '9')
			continue;
		else if (number == '-' && i==0)
			continue;
		else if (number == '.' && nPoint==0) {
			nPoint=1;
			continue;
		}
		else 
			return false;
	}
	return true;
}

function isEmail(email)
{
	var posArroba = email.indexOf('@',0);
	
	if (posArroba <= 0)
		return false;

	var posPunto = email.indexOf('.',posArroba);
		
	if (posPunto == -1)
		return false;
		
	if (posPunto+1 == email.length)
		return false;
	// Despues del punto solo puede haber: a-z 0-9 . _-
	if (!contieneCaracteresPermitidos(email.substr(posPunto+1), "._-"))
		return false;

	return true;
}

function contieneCaracteresPermitidos(valor, caracteresValidos)
{
	var longi = valor.length;
	var c;
	valor = valor.toLowerCase();
	
	for (var i = 0; i < longi; i++)
	{
		c = valor.charAt(i);
		if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
			continue;
		} else {
			for (var j=0; j<caracteresValidos.length; j++) {
				if (caracteresValidos.indexOf(c)==-1) {
					return false;
				}
			}
		}
	}
	return true;
}


function bisestile (anno)
{
	return ( (anno % 4 == 0 && anno % 100 != 0) || (anno % 400 == 0) );
}

function isLogical (gg, mm, aaaa)
{
	var MM	 = new Array(0,1,2,3,4,5,6,7,8,9,10,11,12);
	var GG	 = new Array(00,31,28,31,30,31,30,31,31,30,31,30,31);


	// Il giorno non deve essere < 1 || > 31
	if ( (gg < 1) || (gg > 31) ) return 1;
	// Il mese non deve essere < 1 && > 12
	else if ( (mm < 1)  || (mm > 12) ) return 2;
	// Se vero l'anno Utente e' Bisestile e il mese e' Febbraio
	else if ( bisestile(aaaa)  && (mm == 2) )
	{
		if ( gg > 29 ) return 5;
		return 0;
	}
	else if (gg > GG[mm])
	{
		if (mm == 2) return 4;
		else return 5;
	}
	else return 0;
}


/*************************************************************
Funzione che verifica la lunghezza della stringa, il formato,
la corretteza logica...
************************************************************/
function isDate (DATA)
{
	var gg 		= DATA.value.substring(0,2); //	'02'
	var token1  = DATA.value.substring(2,3); //	'/ -'
	var mm 		= DATA.value.substring(3,5); //	'08'
	var token2  = DATA.value.substring(5,6); //	'/ -'
	var aaaa	= DATA.value.substring(6,10);//	'1971'

	if (DATA.value.length != 10)
	{
		alert("El formato de fecha es: dd/mm/aaaa, dd-mm-aaaa.");
		DATA.focus();
		return false;
	}
	else if ((token1 != "/") && (token1 != "-"))
	{
		alert("El formato de fecha es: dd/mm/aaaa, dd-mm-aaaa.");
		DATA.focus();
		return false;
	}
	else if ((token2 != "/") && (token2 != "-"))
	{
		alert("El formato de fecha es: dd/mm/aaaa, dd-mm-aaaa.");
		DATA.focus();
		return false;
	}
	else if (token1 != token2)
	{
		alert("El formato de fecha es: dd/mm/aaaa, dd-mm-aaaa.");
		DATA.focus();
		return false;
	}
	else if	(!isNumeric(gg))
	{
		alert("El dia no es un valor numerico.");
		DATA.focus();
		return false;
	}
	else if	(!isNumeric(mm))
	{
		alert("El mes no es un valor numerico.");
		DATA.focus();
		return false;
	}
	else if	(!isNumeric(aaaa))
	{
		alert("El año no es un valor numerico.");
		DATA.focus();
		return false;
	}
	else
	{
		var g = parseInt(gg,10);
		var m = parseInt(mm,10);
		var a = parseInt(aaaa,10);

		var error=isLogical(g,m,a);
		if (error == 1)
		{
			alert("El dia no es valido: " + gg);
			DATA.focus();
			return false;
		}
		else if (error == 2)
		{
			alert("El mes no es valido: " + mm);
			DATA.focus();
			return false;
		}
		else if (error == 4)
		{
			alert("La Fecha no es correcta porque el año no es bisiesto: " + aaaa);
			DATA.focus();
			return false;
		}
		else if (error == 5)
		{
			alert("La fecha no es correcta porque el dia: " + gg + " no es coherente con el mes: " + mm);
			DATA.focus();
			return false;
		}
		else return true;
	}
	return true;
}




function data_attuale(data)
{
	var fecha = new Date ();
    var dia = fecha.getDate();
	if (dia < 10)
	   dia = '0'+dia;
    var mes = fecha.getMonth ()+1;
	if (mes < 10)
		mes = '0'+mes;
    var anio = fecha.getYear();
	if(!document.all) // Para Netscape
  		anio=anio+1900;
	else if (anio >= 80 && anio <= 99)
	   		 anio = '19' + anio;
   
	if (data.value.length=='')
         data.value=dia+'/'+mes+'/'+anio;
	else data.value='';

}

function TrimLeft( str ) {
	var resultStr = "";
	var i = len = 0;
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null) 
	return null;
	// Make sure the argument is a string
	str += "";
	if (str.length == 0) 
	resultStr = "";
	else { 
	// Loop through string starting at the beginning as long as there
	// are spaces.
	// len = str.length - 1;
	len = str.length;
	
	while ((i <= len) && (str.charAt(i) == " "))
	i++;
	// When the loop is done, we're sitting at the first non-space char,
	// so return that char plus the remaining chars of the string.
	resultStr = str.substring(i, len);
	}
	return resultStr;
}	


	function isMail(_email) {
		var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i
	return emailReg.test(_email);
}
