var agt = navigator.userAgent.toLowerCase();
var IE = (agt.indexOf("msie") != -1 || agt.indexOf("internet explorer") != -1);
var NS = document.layers;

function openWindow(new_url, new_width, new_height,scrollbars) {
	var x, y, w, h;
	var param = "";
	if (new_width == null)
		new_width = 600;
	if (new_height == null)
		new_height = 400;
	if (scrollbars == null)
		scrollbars = true;
	if (scrollbars)
		scrollbars = "1";
	else
		scrollbars = "0";
	x = screen.width / 2 - new_width / 2;
	y = screen.height / 2 - new_height / 2;
	param += "width=" + new_width;
	param += ",height=" + new_height;
	param += ",left=" + x;
	param += ",top=" + y;
	param += ",scrollbars=" + scrollbars;
	param += ",toolbar=0,location=0,directories=0,menuBar=0,resizable=0";
	win = window.open(new_url, "", param);
	if (new_url.indexOf('emoticons/default.asp')>0)
		return win;
}

function getDateISO (thisDate) {
	var dateISO = split(thisDate, " ");
	var ret = split(thisDate, "/");
	return ret[2] + ret[1] + ret[0];
}

function selectDate (formName, formField) {
	var formValue = document.forms[formName].elements[formField].value;
	openWindow("/modules/common/select_date.asp?form_name=" + formName + "&form_field=" + formField + "&form_value=" + formValue, 250, 220);
}

function isValidForm (formName) {
	var ret = true;
	var oForm = document.forms[formName];
	var bRequired, bIsEmpty;
	for (var i = 0; i < oForm.elements.length; i++) {
		oInput = oForm.elements[i];
		bRequired = (oInput.getAttribute("required") == "true");
		bIsEmpty = false;
		switch (oInput.type.toLowerCase()) {
			case "text":
			case "textarea":
			case "password":
				bIsEmpty = (oInput.value == "");
				break;
			case "select":
				bIsEmpty = (oInput.selectedIndex == -1 ? false : oInput.options[oInput.selectedIndex].value);
				break;
			case "checkbox":
			case "radio":
				var atLeast1 = false;
				for (var j = 0; j < oInput.length; j++) {
					if (oInput(j).checked) {
						atLeast1 = true;
						break;
					}
				}
				bIsEmpty = atLeast1;
				break;
		}
		if (bRequired && bIsEmpty) {
			if (document.getElementById(oInput.name + "_label") != null)
				alert(document.getElementById(oInput.name + "_label").innerText + " è un campo obbligatorio.");
			else
				alert("Alcuni campi obbligatori non sono stati compilati.");
			oInput.focus();
			ret = false;
			break;
		}
	}
	return ret;
}

function initForms(){
	aInput = document.getElementsByTagName("input")
	aTextAreas = document.getElementsByTagName("textarea")
	aButtons = [];
	aText = [];
	
	for (var i=0; i<aInput.length; i++){
		switch(aInput[i].type){
			case 'text':
				aText[aText.length] = aInput[i];
				break;
			case 'password':
				aText[aText.length] = aInput[i];
				break;	
			case 'button':
			case 'submit':
			case 'reset':
				aButtons[aButtons.length] = aInput[i];
				break;
		}
	}
	
	for (var i=0; i<aButtons.length; i++){
		aButtons[i].onmouseover = function(){
			this.className += 'hover';
		}
		aButtons[i].onmouseout = function(){
			this.className = this.className.replace('hover', '');
		}
	}
	
	for (var i=0; i<aText.length; i++){
		aText[i].onfocus = function(){
			this.className += 'focus';
		}
		aText[i].onblur = function(){
			this.className = this.className.replace('focus', '');
		}
	}

	for (var i=0; i<aTextAreas.length; i++){
		aTextAreas[i].onfocus = function(){
			this.className += 'focus';
		}
		aTextAreas[i].onblur = function(){
			this.className = this.className.replace('focus', '');
		}
	}		

}

