colorSelected       = "#f5e9eb";
colorNotSelected    = "#fff";
language            = "it";

// change all select color list for IE
function chngSelectColor() {
    var arg = chngSelectColor.arguments;
    for (i = 0; i < arg.length; i++) {
        obj = getRawObject(arg[i]);
        if (obj) {
			for (j = 0; j < obj.options.length; j++) {
			    if (obj.options[j].selected == false) {
				    chngInputColor(obj.options[j], true, true);
			    }
			}
        }
    }
}

// change Input Color
function chngInputColor(what, sw, init) {
	// on focus
	if (sw) {
		setBGColor(what, colorSelected);
		if (!init) {
			objElement = getRawObject(what.name);
			if (objElement.options)
				for (i = 0; i < objElement.options.length; i++)
					setBGColor(objElement.options[i], colorSelected);
		}
	}
	// on blur
	else {
		setBGColor(what, colorNotSelected);
		objElement = getRawObject(what.name);
		if (objElement.options)
			setBGColor(objElement.options[objElement.selectedIndex], colorNotSelected);
	}

}

// 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;
}

// contact form validation
function validateContactForm(form) {
	if (isNotEmpty(form.textareacontact)) {
		var strTemp = trimAll(form.textareacontact.value); // test for white spaces
		if (strTemp.length) {
			return true;
		}
		textComm(isContactFormEmpty);
	}
	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;
}

function validateFormContatti(form) {
    if (isNotEmpty(form.inputcognome)) {
        if (isNotEmpty(form.inputnome)) {
            if (isEMailAddr(form.inputemail)) {
                if (isChecked(form.inputdati)) {
                    return true;
                }
            }
        }
    }
    return false;
}

function validateFormRichiesta(form) {
    if (isNotEmpty(form.inputsocieta)) {
        if (isNotEmpty(form.inputrichiedente)) {
            if (isNotEmpty(form.inputtel)) {
                if (isNotEmpty(form.inputemail)) {
                    if (isEMailAddr(form.inputemail)) {
                        if (isNotEmpty(form.inputnumero)) {
                            if (isNumber(form.inputnumero)) {
                                if (isChosen(form.selecttipologia)) {
                                    if (isNotEmpty(form.textareasede)) {
                                        if (isChosen(form.selecttipo)) {
                                            if (isChosen(form.selectdatainiziogiorno)) {
                                                if (isChosen(form.selectdatainiziomese)) {
                                                    if (isChosen(form.selectdatainizioanno)) {
                                                        if (isChosen(form.selectdatafinegiorno)) {
                                                            if (isChosen(form.selectdatafinemese)) {
                                                                if (isChosen(form.selectdatafineanno)) {
                                                                    if (isDatePossible(form.selectdatainiziogiorno, form.selectdatainiziomese, form.selectdatainizioanno)) {
                                                                        if (isDatePossible(form.selectdatafinegiorno, form.selectdatafinemese, form.selectdatafineanno)) {
                                                                            if (isNotEmpty(form.inputsale1)) {
                                                                                if (isNumber(form.inputsale1)) {
                                                                                    if (isChosen(form.selectsale1)) {
                                                                                        return true;
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}

function validateFormSearch(form) {
    if (isNotEmpty(form.inputtext)) {
        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 date!",
							"Niepoprawna data!",
							"La data selezionata non e' corretta!"
							);
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",
							""
							);
							
function textComm(arrayText) {
	for (i = 0; i < languageText.length; i++) {
		if (languageText[i] == language)
			alert(arrayText[i]);
	}
	return;
}

