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) {
    if (!getObject(elem)) return false;
    var str = elem.value;
    if (elem.name == "inputc_privacy") {
        if (!elem.checked) {
            textComm(isCheckedText);
            setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
            return false;
        }
    }
    else {
        if (!elem.checked) {
            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 that the user enter the same value in all required fields
function isEqual() {
    var arg = new Array();
    for (i = 0; i < isEqual.arguments.length; i++)
        arg[i] = isEqual.arguments[i].value;
    var str = arg[0];
    while (arg[0] || arg[0] == "") {
        if (arg[0] != str || !arg[0]) {
            textComm(isEqualText);
            return false;
        }
        arg.shift();
    }
    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 form cd
function validate_form_cd (form) {
    if (isNotEmpty(form.input_name)) {
        if (isNotEmpty(form.input_surname)) {
            if (isNotEmpty(form.input_address1)) {
                if (isNotEmpty(form.input_zip)) {
                    if (isNotEmpty(form.input_city)) {
                        if (isNotEmpty(form.input_state1)) {
                            if (isChosen(form.select_state)) {
                                if (isEMailAddr(form.input_email)) {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    return false;
}

// validation form request
function validate_form_booking (form) {
    if (isNotEmpty(form.input_name)) {
        if (isNotEmpty(form.input_surname)) {
            if (isEMailAddr(form.input_email)) {
                if (isNotEmpty(form.input_number_normal)) {
                    if (isNumber(form.input_number_normal)) {
                        if (isChecked(form.inputc_privacy)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    
    return false;
}

// alerts
languageText	= new Array	(
							"en",
							"it",
							"fr",
							"es"
							);
isChosenText	= new Array (
							"Please select all required data!",
							"Alcuni campi obbligatori, non sono stati selezionati!",
							"",
							""
							);
isEMailAddrText	= new Array (
							"The inserted e-mail address is not valid!",
							"L'indirizzo e-mail inserito non e' valido!",
							"L'adresse e-mail remplié n'est pas valide.",
							"La direcciòn electronica (e-mail) no es vàlida."
							);
isNotEmptyText	= new Array (
							"Nothing has been inserted in an obligatory domain!",
							"In un campo obbligatorio, non e' stato inserito nessun valore!",
							"Dans un demain obligé aucun valeur a été inséré!",
							"En un campo obligatorio, no ha sido definido algùn valor!"
							);
isNumberText	= new Array (
							"Only numbers can be inserted in this domain!",
							"In questo campo possono essere inseriti solo numeri!",
							"Dans cet demain c'est possible de mettre seulement des nombres!",
							"En este campo son permitidos solo nùmeros!"
							);
isCheckedText   = new Array (
							"You did not accept the proceeding of personal data!",
							"Non hai acconsentito al trattamento dei dati personali!",
							"Tu n'a pas accédé au traitement des dates personnels!",
							"No ha sido autorizado el uso de tus datos personales!"
							);
isCheckedText2  = new Array (
							"",
							"Non hai selezionato la sale!",
							"",
							""
							);
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",
							"",
							"",
							""
							);
isEqualText =     new Array (
                            "Password incorrect!",
                            "La password incorretta!",
							"",
							""
                          )
