var whitespace = " \t\n\r";
var mPrefix = "Il campo "
var mSuffix = " non è stato inserito correttamente."
var sUSUsername = "Nome utente"     
var sUSPassword = "Password (minimo 4 caratteri)" 
var sUSLastName = "Cognome"
var sUSFirstName = "Nome"
var sEmail = "E-mail"
var sUSAddress = "Indirizzo"
var sWorldAddress = "Via e n."
var sCity = "Località"
var sStateCode = "State Code"
var sWorldState = "Provincia"
var sZIPCode = "CAP"
var sDate = "Data di nascita (gg/mm/aaaa)"
var defaultEmptyOK = false
var iYear = "This field must be a 2 or 4 digit year number.  Please reenter it now."

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isWhitespace (s) {
	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}

function warnField(theField,msg) {
	theField.focus();
	// if the browser is Netscape 6 or IE
	if(document.all || document.getElementByID){
		// change the color of text field
		theField.style.background = "yellow";
	}
	alert(msg);
	return false;
}

function warnEmpty (theField,fieldLabel,minSize,maxSize) {
    var msg=mPrefix + fieldLabel + mSuffix;
    if (minSize && maxSize) 
	msg+="\n[ min "+minSize+", max "+ maxSize+" caratteri ]";
    return warnField(theField,msg);
}

function checkString (theField, s, minSize, maxSize) {
	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.
	if (checkString.arguments.length == 2) minSize = defaultEmptyOK?0:1;
	if ((minSize == 0) && (isEmpty(theField.value))) return true;
	if (isWhitespace(theField.value) || (theField.value.length < minSize) || (theField.value.length > maxSize)) 
		return warnEmpty (theField, s, minSize, maxSize);
	return true;
}

function checkEUDate(theField,fieldLabel,required) {
	// thanks to Fritz the Blank - from http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20335556.html 
	var strDate=theField.value;
	if (isWhitespace(theField.value)) {
	    if (!required) return true;
	    return warnEmpty (theField, fieldLabel);
	}
	if(strDate.length>0){
		var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{2,2})[ ]*$/;
		var match=strDate.match(dateregex);
		if (match) {
		    match[3]=2000+match[3];
			var tmpdate=new Date(match[3],parseInt(match[2],10)-1,match[1]);
			if (tmpdate.getDate()==parseInt(match[1],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[2],10)) {
				return true;
			}
		}
	}
	return warnField(theField,"La data inserita nel campo "+fieldLabel+" non e' valida."+"\n [ formato: gg/mm/aaaa ]");
}

function checkPassword (theField, thePasswordCheck) {
	// Next line is needed on NN3 to avoid "undefined is not a number" error
	// in equality comparison below.
	if (theField.value!=thePasswordCheck.value)
		return warnField(theField,"Le password inserite non corrispondono.");
	return true;
}

function strCheckMail (e) {
	var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){ 
			return 1;
		}	
	} 
	
	if (document.images) { //????
		re1 = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re1) && e.match(re2))
			return 0;
		else
			return 2;
	}
	return 0;
}

function checkEmail (theField, fieldLabel, minSize, maxSize) {
	if (!checkString (theField, fieldLabel, minSize, maxSize))
		return false;
	r=strCheckMail(theField.value);
	if (r==0) return true;
	if (r==1) return warnField(theField,"L'indirizzo di E-mail inserito nel campo "+fieldLabel+" contiene caratteri non validi.");
	return warnField(theField,"L'indirizzo di E-mail inserito nel campo "+fieldLabel+" non ha forma corretta.");
}

function checkChkBox(theField,fieldLabel,refValue) {
	if (!theField.checked) 
	{
		return warnField(theField,"Il campo "+fieldLabel+" e' obbligatorio.");
	}
	return true;
}

