// ############# - GETELEMENTBYID - #############
function getObj(itemId) {
	return document.getElementById(itemId);
}
// ################### - FINE - ###################

// ############# - SHOW HIDE ALERT DIV - #############
function toggleAlertDiv(itemId) {
	if (getObj(itemId).style.display == '') {
		getObj(itemId).style.display = 'none'
	} else {
		getObj(itemId).style.display = ''
	}
}

function openAlert(contentId, idAlertDiv) {
	getObj(idAlertDiv).innerHTML = getObj(contentId).innerHTML;
	toggleAlertDiv(idAlertDiv);
}
// ################### - FINE - ###################

// ############# - OPEN POPUP O OPEN _SELF LINK JAVASCRIPT - #############
function openPage(pagURL, newPage, resizable, scrolBars, pageH, pageW) {
	if (newPage) {
		if (newPage == '_blank') {
			newWindow = window.open(pagURL, newPage, 'toolbar=yes ,location=yes ,status=yes ,menubar=yes ,scrollbars=auto ,resizable=yes');
			newWindow.focus();
		} else {
			newWindow = window.open(pagURL, newPage, 'width=' + pageW + ',height=' + pageH + ',toolbar=no,location=no,status=no,menubar=no,scrollbars='+ scrolBars +',resizable='+ resizable);
			newWindow.focus();
		}
	} else {
		window.location.href=pagURL;
	}
}
// ################### - FINE - ###################


// ############# - APPLY STYLES TO INPUTS - #############
window.onload = attachInputStyles;
var inputObj = document.getElementsByTagName('input');
function attachInputStyles() {
	for (var i=0;i<inputObj.length;i++) {
		if ( (inputObj[i].type == 'checkbox') || (inputObj[i].type == 'radio') ) {
			inputObj[i].className = 'inputCheckRadio';
		}
	}
}
// ################### - FINE - ###################

// ############# - BROWSER DETECT - #############
// BROWSER TYPE		= BrowserDetect.browser
// BROWSER VERSION	= BrowserDetect.version
// OPERATVE SYSTEM	= BrowserDetect.OS
// ################################################
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

if (BrowserDetect.browser == 'Explorer')  {
	if (BrowserDetect.version == 7) {
		var BrowserType = 'msie7';
	} else {
		var BrowserType = 'msie';
	}
} else if (BrowserDetect.browser == 'Firefox') {
	var BrowserType = 'firefox';
} else {
	var BrowserType = '';
}
// ################### - FINE - ###################

function swithSearch() {
	var advCatObj = getObj('adv_categorie');
	var basCatObj = getObj('bas_categorie');
	var advTxtObj = getObj('adv_text');
	var basTxtObj = getObj('bas_text');
	
	var baseObj = getObj('search_base');
	var advObj = getObj('search_advanced');
	
	if (baseObj.style.display == '') {
		advTxtObj.value = basTxtObj.value;
		advCatObj.value = basCatObj.value;
		baseObj.style.display = 'none';
		advObj.style.display = '';
	} else {
		basTxtObj.value = advTxtObj.value;
		basCatObj.value = advCatObj.value;
		baseObj.style.display = '';
		advObj.style.display = 'none';
	} 
}

function loginBlurEmail(itemObj) {
	if (itemObj.value == '') {
		itemObj.value = itemObj.lang;	
	}		
}

function loginFocusEmail(itemObj) {
	if (itemObj.value == itemObj.lang) {
		itemObj.value = '';
	}		
}

function loginFocusPwd(positionItemId) {
	getObj('login_password_text'+ positionItemId).style.display = 'none';
	getObj('login_password'+ positionItemId).style.display = '';
	getObj('login_password'+ positionItemId).focus();
}

function loginBlurPwd(positionItemId) {
	if (getObj('login_password'+ positionItemId).style.display == '') {
		if (getObj('login_password'+ positionItemId).value.length == 0) {
			getObj('login_password_text'+ positionItemId).style.display = '';
			getObj('login_password'+ positionItemId).style.display = 'none';
		}
	}
}

function mediaPrintAutostart() {
	getObj('print_css').media = 'print';
	window.print();
	getObj('print_css').media = 'none';
}

function chgClass(itemObj, classItem) {
	itemObj.className = classItem;
}

function doLogin(theForm)
{
	if (theForm.login.value == '' || theForm.cpwd.value == '')
	{
		alert('Inserire email e password.');
		return false;
	}
	
	pwdhash = hex_md5(theForm.cpwd.value);
	theForm.cpwd.value = '';
	theForm.hpwd.value = pwdhash;
	//alert(theForm.hpwd.value);
	return true;
}

function cngClr(itemObj, itemColor) {
	if (BrowserType == 'msie7') {
		itemObj.firstChild.style.backgroundColor = itemColor;
	}
}


// funzioni per le select
function clearSelect( oSelect, disable ) {
	if ( oSelect ) {
		n_options = oSelect.options.length;
		for(var i=n_options-1; i>=0; i--) {
			oSelect.options[i] = null;
		}
	
		oSelect.disabled = disable;
	}
}

function fillSelect( oSelect, oList, insert_default_option, first_option_label, first_option_value ) {
	
	if ( oSelect && oList ) {
		
		if ( insert_default_option ) {
			oSelect.options[0] = new Option(first_option_label, first_option_value);
		}
		
		for(var i=0; i<oList.length; i++) {
			var value = oList[i].getElementsByTagName('id').item(0).firstChild.data;
			var label = oList[i].getElementsByTagName('nome').item(0).firstChild.data;
			//alert('value = ' + value + ' - label = ' + label);
			oSelect.options[oSelect.options.length] = new Option(label, value);
		}
	}
	
}

function appendOptionSelect( oSelect, option_label, option_value ) {
	
	if ( oSelect ) {
		oSelect.options[oSelect.options.length] = new Option(option_label, option_value);
	}
}
// FINE funzioni per le select

function checkPasswordFiled() {
	try {
		pwdBox = document.form_login_box.cpwd;
		txtBox = document.form_login_box.password_text;
		if (pwdBox.value) {
			pwdBox.style.display = '';
			txtBox.style.display = 'none';
		} else {
			pwdBox.style.display = 'none';
			txtBox.style.display = '';
		}
	} catch(e) {
	}
	
	try {
		pwdDef = document.form_login_body.cpwd;
		txtDef = document.form_login_body.password_text;
		if (pwdDef.value) {
			pwdDef.style.display = '';
			txtDef.style.display = 'none';
		} else {
			pwdDef.style.display = 'none';
			txtDef.style.display = '';
		}
	} catch(e) {
	}
}

function onloadFunctions() {
	checkPasswordFiled();
}

var defSearchValue = 'Parola chiave';
function searchFocus(itemObj) {
	if (defSearchValue == itemObj.value) {
		itemObj.value = '';
	}
}

function searchBlur(itemObj) {
	if (itemObj.value == '') {
		itemObj.value = defSearchValue;
	}
}