//*******************************************************************************************
function checkIsNumber(myObj) {
	if (myObj.value == ""){
		return (true);
	}
	if ( isNaN(myObj.value) ) {
		alert ("Per questo campo sono ammessi solo valori numerici.");
		myObj.value="";
		myObj.focus();
		return (false);
	}
	return (true);
}


function checkIsNumber2(myObj) {
	if (myObj.value == ""){
		return (true);
	}
	
	myObj.value = myObj.value.replace(".",",")
	
	valore = myObj.value.replace(",",".")
	
	if ( isNaN(valore) ) {
		alert ("Per questo campo sono ammessi solo valori numerici.");
		myObj.value="";
		myObj.focus();
		return (false);
	}
	else{
		if (valore < 0){
			alert ("Per questo campo sono ammessi solo valori numerici > 0.");
			myObj.value="";
			myObj.focus();
			return (false);
		}
	}
	return (true);
}

//*******************************************************************************************
//*** Inizio funzioni per il controllo sulla DATA
//*******************************************************************************************

//Restituisce true se l'anno è bisestile, false altrimenti
function isLeap(year) {
   return ((0 == year % 4) && (0 != year % 100)) || (0 == year % 400);
}

//Aggiunge uno zero al numero
function addZero(n) {	
	if (n < 10) {
		return ("0" + n);
	}
	return("" + n);
}

//Alla stringa n elimina lo 0
function subZero(n) {	
	if (n.charAt(0) == '0'){
		n = n.substr(1,2);
		}
	return(n);
}

//Controlla e restituisce la data nel formato gg/mm/aaaa
//oppure ritorna "" in caso di data errata
function checkFormatoData(myObj){
	var strData = myObj.value;
	if (myObj.value == "") {
		return ("");
	}

	var m_risultato;
	var first = strData.indexOf('/');
	var last = strData.indexOf('/',first+1);
	if ((first <= 0) || (last - first <= 1) || (last == strData.length - 1)) {
		alert("Attenzione la data inserita è errata. Es:gg/mm/aaaa.");
		myObj.focus();
		return("");
	}

	//*** Estraggo giorno mese e anno ***
	// Errore: la parseInt('08') restituisce 0
	// per evitare questo problema elimino lo 0 da tutti i numeri (giono e mese)
	var dd = parseInt(subZero(strData.substr(0,first)));
	var mm = parseInt(subZero(strData.substr(first + 1, last - first - 1)));
	var yy = parseInt(strData.substr(last + 1, strData.length - 1));

	//Giorni dei mesi. Il primo elemento vale 0 e serve solo perchè
	//l'indicizzazione degli array è a partire da 0.
	var daysInMonth = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);

	//Controllo la data	
	if ((mm > 0) && (mm < 13)) {
		if ((yy > 1800) && (yy < 3000)) {
			if (isLeap(yy)) {
				daysInMonth[2] = 29;
			}
			if ((dd > 0) && (dd <= daysInMonth[mm])) {
				return(addZero(dd)+"/"+addZero(mm)+"/"+addZero(yy));
			}
			else {
				alert("Attenzione il giorno è errato. Es:gg/mm/aaaa.");
				myObj.focus();
			}
		}
		else {
			alert("Attenzione l'anno è errato. Es:gg/mm/aaaa.");
			myObj.focus();
		}
	}
	else {
		alert("Attenzione il mese è errato. Es:gg/mm/aaaa.");
		myObj.focus();
	}
	
	//Caso di errore generale
	return("");

}

//*******************************************************************************************
//*** Fine funzioni per il controllo sulla DATA
//*******************************************************************************************



//*******************************************************************************************
function checkEmail(myObj) {
		var strEmail= myObj.value;
		var indice = strEmail.search("@");
		//alert (indice);
		
		if (indice == -1) {
			alert ("Spiacenti, l'indirizzo e-mail inserito è sbagliato, per favore controlla di aver inserito il segno '@'");
			return (0);
		}
		else{
			indice = (strEmail.length) - indice;
			if (indice <2){
				alert("Spiacenti, l'indirizzo e-mail inserito è sbagliato, per favore controlla di aver inserito il segno '@'");
				return (0);
			}
		}
	
		indice = (strEmail.length) - (strEmail.lastIndexOf("."))
//		alert (indice + " " +  strEmail.lastIndexOf(".") );
		if ((indice - 1)== (strEmail.length)) {
			alert("Spiacenti, l'indirizzo e-mail inserito è sbagliato, per favore controlla il suffisso di apparteneza (.com, .edu, .net, .it ..)");
			return (0);
		}
		
		if ( indice <2 ) {
			alert("Spiacenti, l'indirizzo e-mail inserito è sbagliato, per favore controlla il suffisso di apparteneza  (.com, .edu, .net, .it ..)");
			return (0);
		}
		
		return (1);
	}

//*******************************************************************************************
	
	function checkPrezzo(myObj){
//		alert ("checkPrezzo: " + myObj.value);
		// sono ammesi solo numeri, -, / , . 
		var strPrezzo= myObj.value;
		strPrezzo= m_replace( strPrezzo, ",", ".");
		if ( isNaN(strPrezzo) ) {
			alert ("Attenzione il prezzo inserito è sbagliato.\n\rSono ammessi solo valori numerici.");
			return (false);
			}
			
		a= parseFloat(strPrezzo);
		if ( a < 0) {
			alert ("Attenzione il prezzo inserito è sbagliato.\n\rSono ammessi solo valori maggiori di 0.");
			return (false);
		}
	
		return (true);
	}

//*******************************************************************************************
function checkTelefono(myObj){
		var strTelefono = myObj.value;
/*		if (strTelefono.charAt(0) != "0"){
			alert ("Spiacenti, il numero telefonico deve iniziare con il prefisso.\n\rEsempio: 0328/111111");
			return (0);
		}
	*/	
		if (strTelefono.length < 6){
			alert ("Attenzione il numero telefonico non è corretto, deve essere lungo almeno 6 cifre.");
			myObj.focus();
			return (0);
		}
		
		// sono ammesi solo numeri, -, / , . 
		a= parseInt(strTelefono);
		//alert (a);
		if ( isNaN(a) ) {
			alert ("Attenzione il numero telefonico è sbagliato.\n\rEsempio: 328/111111");
			myObj.focus();
			return (0);
			}
		return (1);
	}

//*******************************************************************************************

function checkEuro(obj){
    if (obj.value == ""){
		return ("");
	}

    var value = obj.value;
    value= m_replace(value, ",", ".");
    if ( isNaN(value) ) {
		alert ("Per questo campo sono ammessi solo valori numerici.");
		obj.value="";
		obj.focus();
		return ("");
	}
	//alert("x");
	value= m_replace(value, ".", ",");
	var i = value.indexOf(",");
	if(i == -1)  value += ",00";
    return (value)
}


//*******************************************************************************************
function checkSpecialWord(obj){
	//alert("checkSpecialWord: " + obj.value);
	var value = obj.value;
    var a = value.indexOf('*')+value.indexOf('?');
    a += value.indexOf('%')+value.indexOf('+')+value.indexOf('&');
    a += value.indexOf('\\')+value.indexOf("'");
    //alert("a: " + a);
    if(a>-7){
        alert("I seguenti caratteri non sono ammessi: * ? % + & \\ ' ");
        obj.value = value.substring(0,value.length-1);
    }
}


//********************************************************************************************



function m_replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += m_replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}