var defaultLanguage = "it";

// 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 (!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
function validateFormRegistration(form) {
    switch (getRawObject("inputhtype").value) {
        case "azienda": {
            if (isNotEmpty(form.inputnome)) {
                if (isNotEmpty(form.inputcitta)) {
                    if (isNotEmpty(form.inputcap)) {
                        if (isNotEmpty(form.inputprovincia)) {
                            if (isChosen(form.selectnazione)) {
                                if (isChosen(form.selectsettore)) {
                                    if(isEMailAddr(form.inputemail)) {
                                        if (isChecked(form.inputdati)) {
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
        case "registrato": {
            if (isNotEmpty(form.inputnome1)) {
                if (isNotEmpty(form.inputcognome1)) {
                    if (isChosen(form.selectnazione1)) {
                        if (isChosen(form.selectsettore1)) {
                            if(isEMailAddr(form.inputemail1)) {
                                if (isChecked(form.inputdati1)) {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
    }
}

// alerts
languageText	= new Array	(
							"en",
							"pl",
							"it"
							);
isChosenText	= new Array (
							"Please select all required data!",
							"Prosimy o wybranie wszystkich wymaganych danych z listy!",
							"Alcuni campi obbligatori, non sono stati selezionati!"
							);
isEMailAddrText	= new Array (
							"Verify the e-mail address format!",
							"Niepoprawny adres e-mail!",
							"L'indirizzo e-mail inserito non č valido!"
							);
isNotEmptyText	= new Array (
							"Please fill in the required field!",
							"Prosimy o wypełnienie wszystkich wymaganych pól!",
							"In un campo obbligatorio, non č stato inserito nessun valore!"
							);
isNumberText	= new Array (
							"Enter only numbers into the field!",
							"Tylko liczby są dozwolone w tym polu!",
							"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!",
							"Niepoprawna data przyjazdu!",
							""
							);
printCurrentDocText=new Array(
							"Error! Cannot perform this operation. Your browser doesn't support this function!",
							"Błą! Twoja przeglądarka nie nie obsługuje tej funkcji!",
							"Errore! Non č possibile eseguire l'operazione in quanto il tuo browser non supporta questa funzionalitŕ!"
							);
isContactFormEmpty=new	Array(
							"Please, do not send empty form!",
							"Prosimy o nie przesyłanie pustego formularza!",
							""
							);
							
closeBrowserText= new Array	(
							"close image browser",
							"zamknij okno",
							""
							);
