// clear field
function clearField(valField) {
		valField.value = '';
		valField.focus();
}

// check to see if input field is empty
function isEmpty(valField) {
   if ((valField.value.length == 0) ||
   		 (valField.value == null)) {
      return true;
   } else { 
   		return false; 
   }
}

// check to see if input is numeric
function isNumeric(val) {
   var validChars = "0123456789.";
   var isNumber = true;
   var char;
 
   for (i = 0; i < val.length && isNumber == true; i++) { 
      char = val.charAt(i); 
      if (validChars.indexOf(char) == -1) {
         isNumber = false;
      }
   }
   
   return isNumber;   
}

// check to see if input is numeric + blank
function isNumericBlank(val) {
   var validChars = "0123456789. ";
   var isNumber = true;
   var char;
 
   for (i = 0; i < val.length && isNumber == true; i++) { 
      char = val.charAt(i); 
      if (validChars.indexOf(char) == -1) {
         isNumber = false;
      }
   }
   
   return isNumber;   
}

// check to see if input is alphabetic
function isAlphabetic(val) {
		if (val.match(/^[a-zA-Z]+$/)) {
				return true;
		} else {
				return false;
		} 
}

// check to see if input is alphanumeric
function isAlphaNumeric(val) {
		if (val.match(/^[a-zA-Z0-9]+$/)) {
				return true;
		} else {
				return false;
		} 
}

// check to see if input is alphanumeric + dot 
function isAlphaNumericDot(val) {
		if (val.match(/^[.a-zA-Z0-9]+$/)) {
				return true;
		} else {
				return false;
		} 
}

// check to see if input is a valid email address
function isValidEmail(val) {
		if (val.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/)) {
				return true;
		} else {
				return false;
		} 
}

// check if two password are equal
function isPassword2Equal(r_password1, r_password2) {
		if (r_password1.value != r_password2.value) {
    		return false;
    } else {
    		return true;
    }
}

function isDate(dtStr){
		// Declaring valid date character, minimum year and maximum year
		var dtCh= "/";
		var minYear=1900;
		var maxYear=2100;

		function stripCharsInBag(s, bag){
				var i;
    		var returnString = "";
    		
    		// Search through string's characters one by one.
    		// If character is not in bag, append to returnString.
    		for (i = 0; i < s.length; i++){   
        		var c = s.charAt(i);
        		if (bag.indexOf(c) == -1) returnString += c;
    		}
    		return returnString;
		}

		function daysInFebruary (year){
				// February has 29 days in any year evenly divisible by four,
    		// EXCEPT for centurial years which are not also divisible by 400.
    		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
		}

		function DaysArray(n) {
				for (var i = 1; i <= n; i++) {
						this[i] = 31;
						if (i==4 || i==6 || i==9 || i==11) {
								this[i] = 30;
						}
						if (i==2) {
								this[i] = 29;
						}
   			} 
   			return this;
		}

		var daysInMonth = DaysArray(12);		
		var pos1 = dtStr.indexOf(dtCh);
		var pos2 = dtStr.indexOf(dtCh, pos1+1);
		var strDay = dtStr.substring(0, pos1);
		var strMonth = dtStr.substring(pos1+1, pos2);
		var strYear = dtStr.substring(pos2+1);
		strYr = strYear;

		if ((strDay.charAt(0)=="0") && (strDay.length>1)) strDay=strDay.substring(1);
		if ((strMonth.charAt(0)=="0") && (strMonth.length>1)) strMonth=strMonth.substring(1);
		for (var i=1; i<=3; i++) {
				if ((strYr.charAt(0)=="0") && (strYr.length>1)) strYr=strYr.substring(1);
		}
		if (!isNumeric(strDay)) {
				alert("Il formato utilizzato per il giorno non e\' corretto!");
				return false;	
		}
		day = parseInt(strDay);
		if (!isNumeric(strMonth)) {
				alert("Il formato utilizzato per il mese non e\' corretto!");
				return false;	
		}
		month = parseInt(strMonth);		
		if (!isNumeric(strYr)) {
				alert("Il formato utilizzato per l'\anno non e\' corretto!");
				return false;	
		}
		year = parseInt(strYr);
		if ((pos1==-1) || (pos2 ==-1)) {
				alert("Il formato utilizzato per la data non e\' corretto: dd/mm/yyyy");
				return false;
		}
		if ((strDay.length<1) || (day<1) || (day>31) || 
				((month==2) && (day>daysInFebruary(year))) || (day > daysInMonth[month])) {
				alert("Il formato utilizzato per il giorno non e\' corretto!");
				return false;
		}				
		if ((strMonth.length<1) || (month<1) || (month>12)) {
				alert("Il formato utilizzato per il mese non e\' corretto!");
				return false;
		}		
		if ((strYear.length != 4) || (year==0) || (year<minYear) || (year>maxYear)) {
				alert("Il formato utilizzato per l\'anno non e\' corretto!");
				return false;
		}	
		if ((dtStr.indexOf(dtCh,pos2+1)!=-1) || (isNumeric(stripCharsInBag(dtStr, dtCh))==false)) {
				alert("Il formato utilizzato per la data non e\' corretto: dd/mm/yyyy");
				return false;
		}
		return true;
}

function isValidCF(cf) {
		var validi, i, s, set1, set2, setpari, setdisp;
		cf = cf.toUpperCase();
		if (cf.length != 16) {
				alert("La lunghezza del codice fiscale non e'\n" +
							"corretta: il codice fiscale dovrebbe essere lungo\n" +
							"esattamente 16 caratteri.\n");
				return false;
		}
		validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		for (i=0; i<16; i++) {
				if (validi.indexOf(cf.charAt(i)) == -1) {
						alert("Il codice fiscale contiene un carattere non valido '" +
									cf.charAt(i) + "'.\nI caratteri validi sono le lettere e le cifre.\n");
						return false;
				}
		}
		set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
		setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
		s = 0;
		for (i=1; i<=13; i+=2) {
				s += setpari.indexOf(set2.charAt(set1.indexOf(cf.charAt(i))));
		}
		for (i=0; i<=14; i+=2) { 
				s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
		}	
		if (s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0)) {
				alert("Il codice fiscale non e' corretto:\nil codice di controllo non corrisponde.\n");
				return false;
		}
		return true;
}
