var defaultLanguage = "en";

// select undefined field
function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus();
    if(!elem.options) {
        elem.select();
    }
}

// validates that the user made a selection other than default
function isChosen(select) {
    if (!select.selectedIndex) {
    	textComm(isChosenText);
    	setTimeout("focusElement('" + select.form.name + "', '" + select.name + "')", 0);
        return false;
    } else
        return true;
}

// validates that input checkbox is selected
function isChecked(elem) {
    var str = elem.value;
    if (!elem.checked) {
        textComm(isCheckedText);
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    }
    return true;
}

// validates that the entry is formatted as an e-mail address
function isEMailAddr(elem) {
	var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
    	textComm(isEMailAddrText);
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else
        return true;
}

// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
	var str = elem.value;
	var re = /.+/;
	if(!str.match(re)) {
		textComm(isNotEmptyText);
		setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
		return false;
	} else
		return true;
}

//validates that the entry is a positive or negative number
function isNumber(elem) {
	var str = elem.value;
    var re = /^[-]?\d*\.?\d*$/;
    str = str.toString();
    if (!str.match(re)) {
    	textComm(isNumberText);
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    }
    return true;
}

// validates date checking if is not older than the current one
function isDatePossible(elemday, elemmonth, elemyear) {
	var today = new Date();
	var numday = eval(elemday.value);
	var nummonth = eval(elemmonth.value);
	var numyear = eval(elemyear.value);
	var todayYear = today.getYear();
	if (isNN4 || isNN6)
		todayYear = todayYear + 1900; // update year!
	if (todayYear <= numyear) {
		if ((todayYear == numyear) && ((today.getMonth() + 1) >= nummonth)) {
			if ((today.getDate() <= numday) && ((today.getMonth() + 1) == nummonth))
				return true;
		}
		else
			return true;
	}
	textComm(isDatePossibleText);
	return false;
}

// removes leading and trailing spaces
function trimAll(strValue) {
	var objRegExp = /^(\s*)$/;
	
	//check for all spaces
	if (objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, '');
		if (!strValue.length)
			return strValue;
	}
	
	//check for leading & trailing spaces
	objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	if (objRegExp.test(strValue)) {
		//remove leading and trailing whitespace characters
		strValue = strValue.replace(objRegExp, '$2');
	}
	return strValue;
}

// get alert
function textComm(arrayText) {
	for (i = 0; i < languageText.length; i++) {
		if (languageText[i] == defaultLanguage)
			alert(arrayText[i]);
	}
	return;
}

// validation MuseoVisitePrenotazione
function validateFormMuseoVisitePrenotazione(form) {
    if (isNotEmpty(form.inputgruppo)) {
        if (isNotEmpty(form.inputnome)) {
            if (isNotEmpty(form.inputcitta)) {
                if (isNotEmpty(form.inputnazione)) {
                    if (isNotEmpty(form.inputtel)) {
                        if (isEMailAddr(form.inputemail)) {
                            if (isNotEmpty(form.inputnumero)) {
                                if (isNotEmpty(form.inputgiorno)) {
                                    if (isNotEmpty(form.inputora)) {
                                        if (isNotEmpty(form.inputlingua)) {
                                            if (isNotEmpty(form.inputluogo)) {
                                                return true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}

// validation VisitareVisitePrenotazione
function validateFormVisitareVisitePrenotazione(form) {
    if (isNotEmpty(form.inputgruppo)) {
        if (isNotEmpty(form.inputnome)) {
            if (isNotEmpty(form.inputcitta)) {
                if (isNotEmpty(form.inputnazione)) {
                    if (isNotEmpty(form.inputtel)) {
                        if (isEMailAddr(form.inputemail)) {
                            if (isNotEmpty(form.inputnumero)) {
                                if (isNotEmpty(form.inputgiorno)) {
                                    if (isNotEmpty(form.inputora)) {
                                        if (isNotEmpty(form.inputlingua)) {
                                            if (isNotEmpty(form.inputluogo)) {
                                                return true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}

// validation UfficiRegistrazione
function validateFormUfficiRegistrazione(form) {
    if (isNotEmpty(form.inputnome)) {
        if (isNotEmpty(form.inputcognome)) {
            if (isEMailAddr(form.inputemail)) {
                return true;
            }
        }
    }
    return false;
}

// validation Search
function validateFormSearch(form) {
    if (isNotEmpty(form.inputsearch)) {
        return true;
    }
    return false;
}

// alerts
languageText	= new Array	(
							"en",
							"it"
							);
isChosenText	= new Array (
							"Please select all required data!",
							"Alcuni campi obbligatori, non sono stati selezionati!"
							);
isEMailAddrText	= new Array (
							"Verify the e-mail address format!",
							"L'indirizzo e-mail inserito non e' valido!"
							);
isNotEmptyText	= new Array (
							"Please fill in the required field!",
							"In un campo obbligatorio, non e' stato inserito nessun valore!"
							);
isNumberText	= new Array (
							"Enter only numbers into the field!",
							"In questo campo possono essere inseriti solo numeri!"
							);
isCheckedText	= new Array (
							"",
							"Non hai acconsentito al trattamento dei dati personali!"
							);
isDatePossibleText= new Array (
							"Verify the arrival's date!",
							""
							);
printCurrentDocText=new Array(
							"Error! Cannot perform this operation. Your browser doesn't support this function!",
							"Errore! Non e' possibile eseguire l'operazione in quanto il tuo browser non supporta questa funzionalita'!"
							);
isContactFormEmpty=new	Array(
							"Please, do not send empty form!",
							""
							);
							
closeBrowserText= new Array	(
							"close image browser",
							""
							);

