<!--

//GLOBALS
var w3c = (document.getElementById) ? 1:0
var ns4 = (document.layers) ? 1:0  //browser detect for NS4 & W3C standards
var hasCookies = false;
var baseColor = '#FFFFFF';

// verifica se il browser accetta i cookie
if (document.cookie == '') {
	document.cookie = 'hasCookies=yes';
    if (document.cookie.indexOf('hasCookies=yes') != -1) hasCookies = true;
} else
	hasCookies = true;


function getObject(sObj) {
	if (w3c)
		theObj = document.getElementById(sObj);
	else if (ns4)
		theObj = eval("document." + sObj);
	return theObj;
}



function visual(aa){
	obj=getObject(aa);
	if (obj){
		if(obj.style.display=='none'){
			obj.style.display='';
		}else{
			obj.style.display='none';
		}
	}
}


// swaps text in a layer.
function swapText(sText, sDivID, sInnerDivID) {
	sContent = "<span class=\"commandDesc\">" + sText + "</span>";
	if (w3c) {
		theObj = getObject(sDivID);
		if (theObj) theObj.innerHTML = sText;
	} else if (ns4) {
		innerObj = divID + ".document." + sInnerDivID;
		theObj = getObject(innerObj);
		if (theObj) {
			theObj.document.open();
			theObj.document.write(sContent);
			theObj.document.close();
		}
	}
}

// sets a cookie in the browser.
function setCookie(sName, sValue, iHours, sPath) {
	if (hasCookies) {
		if (iHours) {
			if ((typeof(iHours) == 'string') && Date.parse(iHours)) numHours = iHours;
			else if (typeof(iHours) == 'number') numHours = (new Date((new Date()).getTime() + iHours * 3600000)).toGMTString();
		}
		document.cookie = sName + '=' + escape(sValue) + (numHours ? (';expires=' + numHours) : '') + ((sPath) ? (';path=' + sPath) : (''));
	}
}

// reads a cookie from the browser
function readCookie(name) {
	if (document.cookie == '') return '';
	else {
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);
		if (firstChar != -1) {
			firstChar += name.length + 1;
			lastChar = theBigCookie.indexOf(';', firstChar);
			if (lastChar == -1) lastChar = theBigCookie.length;
			return unescape(theBigCookie.substring(firstChar, lastChar));
		} else
			return '';
	}
}

//  Netscape 4 resize fix
function WM_netscapeCssFix() {
	if (document.WM.WM_netscapeCssFix.initWindowWidth != window.innerWidth || document.WM.WM_netscapeCssFix.initWindowHeight != window.innerHeight) {
		document.location = document.location;
	}
}

function WM_netscapeCssFixCheckIn() {
	if ((navigator.appName == 'Netscape') && (parseInt(navigator.appVersion) == 4)) {
		if (typeof document.WM == 'undefined') {
			document.WM = new Object;
		}
		if (typeof document.WM.WM_scaleFont == 'undefined') {
			document.WM.WM_netscapeCssFix = new Object;
			document.WM.WM_netscapeCssFix.initWindowWidth = window.innerWidth;
			document.WM.WM_netscapeCssFix.initWindowHeight = window.innerHeight;
		}
		window.onresize = WM_netscapeCssFix;
	}
}

WM_netscapeCssFixCheckIn();

function MM_swapImgRestore() { //v3.0
	var i, x, a = document.MM_sr;
	for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d = document;
	if (d.images) {
		if (!d.MM_p) d.MM_p = new Array();
		var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
		for (i = 0; i < a.length; i++) {
			if (a[i].indexOf("#")!=0) {
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
		}
	}
}

function MM_findObj(n, d) { //v4.0
	var p, i, x;
	if (!d) d = document;
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0,p);
	}
	if (!(x = d[n]) && d.all) x = d.all[n];
	for (i=0; !x && i < d.forms.length; i++) x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
	if (!x && document.getElementById) x = document.getElementById(n);
	return x;
}

function MM_swapImage() { //v3.0
	var i, j = 0, x, a = MM_swapImage.arguments;
	document.MM_sr = new Array;
	for (i = 0; i < (a.length - 2); i += 3)
		if ((x = MM_findObj(a[i])) != null) {
			document.MM_sr[j++] = x;
			if (!x.oSrc) x.oSrc = x.src;
			x.src = a[i + 2];
		}
}

// Remove an array item at n
// 0-based
function MM_removeNthArrayItem(array, n) {
	var lhs = new Array();
	if (n > 0) lhs = array.slice(0, n);
	var rhs = new Array();
	if (n < array.length) rhs = array.slice(n + 1);
	var result = lhs.concat(rhs);
	return result;
}

// Does the array contain the given string?
function MM_arrayContainsString(array, item) {
	if (array == null) return false;
	var count = array.length;
	for (i = 0; i < count; i++) {
		if (array[i] == item) return true;
	}
	return false;
}

// Restituisce l'indice dell'elemento cercato, se esiste
function MMJ_arrayContainedStringIndex(oArray, sItem) {
	if (oArray == null) return -1;
	var count = oArray.length;
	var found = false;
	var i = 0;
	for (i = 0; i < count; i++) {
		if (oArray[i] == sItem) return i;
	}
	return -1;
}

// remove the given string from the array of strings
function MM_removeStringFromArray(array, item) {
	if (array == null) return null;
	var count = array.length;
	for (i = 0; i < count; i++) {
		if (array[i] == item) return MM_removeNthArrayItem(array, i);
	}
	return array;
}

// a selectedItems array is kept in the form. It is an array of strings, each
// string being the name of a checkbox image. It doesn't actually have to be
// the name="foo" attribute of the HTML object itself, just any arbitrary name
// that is associated with this checkbox. The image name is the actual name of the image.
function MM_toggleItem(form, itemName, imageName, theme) {
	if (form.selectedItems == null)	form.selectedItems = new Array();
	if (MM_arrayContainsString(form.selectedItems, itemName)) {
		form.selectedItems = MM_removeStringFromArray(form.selectedItems, itemName);
		document[imageName].src = '../themes/'+theme+'/checkbox_off_16.gif';
		//MM_swapImage(imageName, '', '../themes/'+theme+'/checkbox_off_16.gif', '1');
	} else {
		form.selectedItems[form.selectedItems.length] = itemName;
		document[imageName].src = '../themes/'+theme+'/checkbox_on_16.gif';
		//MM_swapImage(imageName, '', '../themes/'+theme+'/checkbox_on_16.gif', '1');
	}
	MM_updateButtons2(form, form.selectedItems);
}

function MM_selectAllItems(oForm, theme) {
	oForm.selectedItems = new Array();
	if (oForm.checkboxes) {
		var checkboxCount = oForm.checkboxes.length;
		for (i = 0; i < checkboxCount; i++) {
			var checkbox = oForm.checkboxes[i];
			if (-1 == document[checkbox.mImageName].src.indexOf('dim_16.gif')) {
				document[checkbox.mImageName].src = '../themes/'+theme+'/checkbox_on_16.gif';
				oForm.selectedItems[oForm.selectedItems.length] = checkbox.mName;
			}
		}
	}
	MM_updateButtons2(form, form.selectedItems);
}

function MM_deselectAllItems(oForm, theme) {
	oForm.selectedItems = new Array();
	if (oForm.checkboxes) {
		var checkboxCount = oForm.checkboxes.length;
		for (i = 0; i < checkboxCount; i++) {
			var checkbox = oForm.checkboxes[i];
			if (-1 == document[checkbox.mImageName].src.indexOf('dim_16.gif')) {
				document[checkbox.mImageName].src = '../themes/'+theme+'/checkbox_off_16.gif';
			}
		}
	}
	MM_updateButtons2(oForm, form.selectedItems);
}

// If all items are selected, deselect all. Otherwise select all.
function MM_toggleSelectedItems(oForm, theme) {
	if (!oForm.selectedItems) oForm.selectedItems = new Array();
	if (oForm.checkboxes) {
		if (oForm.selectedItems.length == oForm.checkboxes.length - MM_countDisabledCheckboxes(oForm))
			MM_deselectAllItems(oForm, theme);
		else
			MM_selectAllItems(oForm, theme);
	}
}

// counts the number of disabled checkboxes - used for deselect all
function MM_countDisabledCheckboxes(oForm) {
	disabledCount = 0;
	if (oForm.checkboxes) {
		for (i = 0; i < oForm.checkboxes.length; i++) {
			oCheckbox = oForm.checkboxes[i];
			if (-1 != document[oCheckbox.mImageName].src.indexOf('dim_16.gif')) {
				disabledCount++;
			}
		}
	}
	return disabledCount;
}

///// Funzioni MM

// restituisci il valore del primo elemento selezionato
function MMJ_getFirstSelectedItem(oForm, sSwitchElementName) {
	for (i = 0; i < oForm.elements.length; i++) {
		oElem = oForm.elements[i];
		if (oElem && oElem.type && oElem.type == "checkbox") {
			if (oElem.name != sSwitchElementName && oElem.checked) return oElem.value;
		}
	}
	return null;
}

// seleziona o deseleziona tutti gli elementi
function MMJ_toggleSelectedItems(oForm, sSwitchElementName) {
	var aSelectedItems = new Array();
	oElem = oForm.elements[sSwitchElementName];
	if (oElem && oElem.type && oElem.type == "checkbox") bValue = oElem.checked;
	else bValue = false;
	j = 0;
	for (i = 0; i < oForm.elements.length; i++) {
		oElem = oForm.elements[i];
		if (oElem && oElem.type && oElem.type == "checkbox") {
			oElem.checked = bValue;
			if (oElem.name != sSwitchElementName && bValue) aSelectedItems[j++] = oElem;
		}
	}
	MMJ_updateButtons2(oForm, aSelectedItems);
}

// restituisce il valore del primo elemento selezionato
function MMJ_getFirstSelectedItem(oForm, sSwitchElementName) {
	for (i = 0; i < oForm.elements.length; i++) {
		oElem = oForm.elements[i];
		if (oElem && oElem.type && oElem.type == "checkbox") {
			if (oElem.name != sSwitchElementName && oElem.checked) return oElem.value;
		}
	}
	return null;
}

// restituisce il valore dell'elemento se questo è l'unico selezionato
function MMJ_getUniqueSelectedItem(oForm, sSwitchElementName) {
	val = null;
	iCount = 0;
	for (i = 0; i < oForm.elements.length; i++) {
		oElem = oForm.elements[i];
		if (oElem && oElem.type && oElem.type == "checkbox") {
			if (oElem.name != sSwitchElementName && oElem.checked) {
				iCount++;
				if (iCount == 1) val = oElem.value;
				else val = null;
			}
		}
	}
	return val;
}

function MMJ_toggleSelection(oForm, sSwitchElementName) {
	var aSelectedItems = new Array();
	j = 0;
	for (i = 0; i < oForm.elements.length; i++) {
		oElem = oForm.elements[i];
		if (oElem && oElem.type && oElem.type == "checkbox") {
			if (oElem.name != sSwitchElementName && oElem.checked) aSelectedItems[j++] = oForm.elements[i];
		}
	}
	MMJ_updateButtons2(oForm, aSelectedItems);
}

function MMJ_toggleUniqueSelection(oForm) {
	var aSelectedItems = new Array();
	j = 0;
	for (i = 0; i < oForm.elements.length; i++) {
		oElem = oForm.elements[i];
		if (oElem && oElem.type && oElem.type == "checkbox") {
			if (oElem.checked) aSelectedItems[j++] = oForm.elements[i];
		}
	}
	MMJ_updateButtons2(oForm, aSelectedItems);
}

// visualizza/nasconde una sezione
// ATTENZIONE: il valore del cookie va sincronizzato con quello utilizzato nella 
//             funzione Section->set_collapsable nella libreria interface.inc.php
function MMJ_toggleVisible(visibleElementID, toggleIconID, toggleIconVisible, toggleIconNotVisible) {
	theElem = getObject(visibleElementID);
	theIcon = getObject(toggleIconID);
	theCookie = readCookie(visibleElementID);
	if (theElem) {
		if (theElem.style.display == "none") sElementStatus = "e";
		else sElementStatus = "c";
		if (theCookie == "e") sElementStatus = "c";
		setCookie(visibleElementID, sElementStatus, 48, "/");
		if (sElementStatus == "c") {
			theElem.style.display = "none";
			if (theIcon) theIcon.src = toggleIconNotVisible;
		} else {
			theElem.style.display = "block";
			if (theIcon) theIcon.src = toggleIconVisible;
		}
	}
	window.status = "";
}

// imposta la visibilità di un elemento
function MMJ_setVisible(visibleElementID, toggleIconID, toggleIconVisible, toggleIconNotVisible, elementStatus, displayBlock) {
	theElem = getObject(visibleElementID);
	theIcon = getObject(toggleIconID);
	theCookie = readCookie(visibleElementID);
	if (theElem) {
		if (elementStatus) {
			theElem.style.display = (displayBlock ? "block" : "inline");
			setCookie(visibleElementID, "c", 48, "/");
			if (theIcon) theIcon.src = toggleIconVisible;
		} else {
			theElem.style.display = "none";
			setCookie(visibleElementID, "e", 48, "/");
			if (theIcon) theIcon.src = toggleIconNotVisible;
		}
	}
	window.status = "";
}

// imposta il valore di un elemento
function MMJ_setValue(visibleElementID, newValue) {
	theElem = getObject(visibleElementID);
	if (theElem) {
		if (theElem.value) {	// input type text, area, button...
			theElem.value = newValue;
		} else if (theElem.selected) {	// input type select
			theElem.selected = newValue;
		} else if (theElem.innerHTML) {	// altri tag
			theElem.innerHTML = newValue;
		}
	}
}

// imposta il messaggio della Status Bar
function MMJ_setStatus(sMessage) {
	if (typeof(sMessage) == "string") window.status = sMessage;
	return true;
}

/*function MMJ_toggleVisible(visibleElementID, toggleIconID, toggleIconVisible, toggleIconNotVisible) {
	var theElem = getObject(visibleElementID);
	var theIcon = getObject(toggleIconID);
	if (theElem) {
		
		if (theElem.style.display == "none") {
			theElem.style.display = "";
			theIcon.src = toggleIconVisible;
		} else {
			theElem.style.display = "none";
			theIcon.src = toggleIconNotVisible;
		}
	}
	window.status = '';
}*/

// bottone sensibile
function MMJCommandButton(sName, sDescription, sAction, bEnabled, sEnabledImage, sOverImage, sDisabledImage, bEnableOnNoSelection, bEnableOnSingleSelection, bEnableOnMultipleSelection, sEnabledCheckSelectionJS, bConfirmation, sConfirmationMessage) {
	this.mName = sName;												// nome dell'immagine
	this.mDesc = sDescription;										// descrizione dell'immagine
	this.mAction = sAction;											// azione da compiere al click
	this.mEnabled = bEnabled;										// flag: icona inizialmente abilitata?
	this.mEnabledImage = sEnabledImage;								// immagine icona abilitata (stringa)
	this.mOverImage = sOverImage;									// immagine icona selezionata (stringa)
	this.mDisabledImage = sDisabledImage;							// immagine icona disabilitata (stringa)
	this.mEnabledOnNoSelection = bEnableOnNoSelection;				// pulsante abilitato su "nessuna selezione"
	this.mEnabledOnSingleSelection = bEnableOnSingleSelection;		// pulsante abilitato su "selezione singola"
	this.mEnabledOnMultipleSelection = bEnableOnMultipleSelection;	// pulsante abilitato su "selezione multipla"
	if (sEnabledCheckSelectionJS)									// codice da valutare per abilitare il pulsante
		this.mEnabledCheckSelectionJS = eval(sEnabledCheckSelectionJS);
	this.mConfirmation = bConfirmation;								// flag: richiede conferma?
	this.mConfirmationMessage = (bConfirmation ? sConfirmationMessage : "");	// messaggio di conferma
	this.update = MMJCommandButton_update;
	this.over = MMJCommandButton_over;
	this.out = MMJCommandButton_out;
	this.click = MMJCommandButton_click;
}

function MMJCommandButton_over() {
	if (this.mEnabled) {
		document[this.mName].src = this.mOverImage;
	}
	window.status = this.mDesc;
}

function MMJCommandButton_out() {
	if (this.mEnabled) {
		document[this.mName].src = this.mEnabledImage;
	}
	window.status = "";
}

function MMJCommandButton_click() {
	if (this.mEnabled) {
		if (this.mConfirmation) {
			if (!confirm(this.mConfirmationMessage)) {
				return;
			}
		}
		if ((this.mAction.indexOf('javascript') == 0) || (this.mAction.indexOf('Javascript') == 0)) {
			eval(this.mAction.substring(this.mAction.indexOf(':') + 1));
		} else {
			window.location = this.mAction;
		}
	}
	window.status = "";
}

function MMJCommandButton_update(aSelectedItems) {
	if (this.mEnabledCheckSelectionJS != '' && typeof(this.mEnabledCheckSelectionJS) == "function") {
		bIsEnabled = this.mEnabledCheckSelectionJS(aSelectedItems);
		if (bIsEnabled == true) {
			document[this.mName].src = this.mEnabledImage;
			this.mEnabled = true;
		} else {
			document[this.mName].src = this.mDisabledImage;
			this.mEnabled = false;
		}
	} else {
		bEnable = false;
		if (aSelectedItems.length == 0) {
			bEnable = this.mEnabledOnNoSelection;
		}
		if (aSelectedItems.length == 1) {
			bEnable = this.mEnabledOnSingleSelection;
		}
		if (aSelectedItems.length > 1) {
			bEnable = this.mEnabledOnMultipleSelection;
		}
		if (bEnable) {
			document[this.mName].src = this.mEnabledImage;
			this.mEnabled = true;
		} else {
			document[this.mName].src = this.mDisabledImage;
			this.mEnabled = false;
		}
	}
}

function MMJ_getButton(oForm, sButtonName) {
	if (oForm.buttons) {
		for (var i = 0; i < oForm.buttons.length; i++) {
			if (oForm.buttons[i]) {
				if (oForm.buttons[i].mName == sButtonName) return oForm.buttons[i];
			}
		}
	}
	return null;
}

function MMJ_updateButtons(oForm) {
	var dummy = new Array();
	MMJ_updateButtons2(oForm, dummy);
}

function MMJ_updateButtons2(oForm, aSelectedItems) {
	if (oForm.buttons) {
		for (var i = 0; i < oForm.buttons.length; i++) {
			if (oForm.buttons[i]) oForm.buttons[i].update(aSelectedItems);
		}
	}
}

// supporto multilingua
function MMJLanguage(sName, sReferer, oForm, bSelected) {
	this.mName = sName;
	this.mOwnerForm = oForm;
	this.mReferer = sReferer;
	this.mSelected = bSelected;
	this.mLastState = bSelected;
	this.click = MMJLanguage_click;
}

function MMJLanguage_click() {
	this.mSelected = true;
	this.mLastState = false;
	MMJ_updateLanguages(this.mOwnerForm);
}

function MMJ_updateLanguages(oForm) {
	if (oForm.languages) {
		for (i = 0; i < oForm.languages.length; i++) {
			lan = oForm.languages[i];
			lan.mSelected = (lan.mSelected && !lan.mLastState);
			lan.mLastState = lan.mSelected;
			obj = getObject(lan.mReferer);
			if (obj) obj.style.display = (lan.mSelected ? 'block' : 'none');
		}
	}
}

function MMJ_getLanguage(oForm, sName) {
	if (oForm.languages) {
		var languageCount = oForm.languages.length;
		for (var i = 0; i < languageCount; i++) {
			var lan = oForm.languages[i];
			if (lan.mName == sName) return lan;
		}
	}
	return null;
}
// fine supporto multilingua


/**

MMJ_switchBackgroundColor: imposta il valore della variabile globale baseColor
------------------------------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oColor					colore da impostare

OUTPUT: void

**/
function MMJ_setEvidenceColor(sColor) {
	baseColor = sColor;
}

/**

MMJ_switchBackgroundColor: scambia il colore di sfondo dell'elemento con quello della variabile globale baseColor
-----------------------------------------------------------------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oElement				elemento da modificare

OUTPUT: void

**/
function MMJ_switchBackgroundColor(oElement) {
	if (oElement) {
		var tmp = oElement.style.backgroundColor;
		oElement.style.backgroundColor = baseColor;
		baseColor = tmp;
	}
}

/**

MMJ_selectFocused: sposta il focus sull'elemento specificato
------------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oElement				elemento da selezionare

OUTPUT: void

**/
function MMJ_selectFocused(oElement) {
	if (oElement) {
		try {
			if ((oElement.type == "text" || oElement.type == "textarea") && oElement.value != "") oElement.select();
			oElement.focus();
		} catch (e) {}
	}
}

/**

MMJ_mmcode_title: codificata l'inserimento di un titolo in un messaggio del CRM
-------------------------------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oArea					TextArea di destinazione
oTitle					titolo (Text o altro)

OUTPUT: void

**/
function MMJ_mmcode_title(oArea, oTitle) {
	if (oArea && oTitle) {
		oArea.value = oArea.value + '{TITLE="' + oTitle.value + '"}';
		oTitle.value = '';
	}
}

/**

MMJ_mmcode_URL: codificata l'inserimento di un URL in un messaggio del CRM
--------------------------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oArea					TextArea di destinazione
oURL					URL (Text o altro)
oDesc					descrizione URL (Text o altro)

OUTPUT: void

**/
function MMJ_mmcode_URL(oArea, oURL, oDesc) {
	if (oArea && oURL && oDesc) {
		oArea.value = oArea.value + '{URL="' + oURL.value + '"="' + oDesc.value + '"}';
		oURL.value = '';
		oDesc.value = '';
	}
}

/**

MMJ_mmcode_mail: codificata l'inserimento di un indirizzo e-mail in un messaggio del CRM
----------------------------------------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oArea					TextArea di destinazione
oMail					indirizzo e-mail (Text o altro)
oDesc					descrizione e-mail (Text o altro)

OUTPUT: void

**/
function MMJ_mmcode_mail(oArea, oMail, oDesc) {
	if (oArea && oMail && oDesc) {
		oArea.value = oArea.value + '{MAIL="' + oMail.value + '"="' + oDesc.value + '"}';
		oMail.value = '';
		oDesc.value = '';
	}
}

/**

MMJ_mmcode_image: codificata l'inserimento di un'immagine in un messaggio del CRM
---------------------------------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oArea					TextArea di destinazione
oImage					origine immagine (Select o File)
oLeft					RadioButton di allineamento dell'immagine a sinistra
oCenter					RadioButton di allineamento dell'immagine al centro
oRight					RadioButton di allineamento dell'immagine a destra

OUTPUT: void

**/
function MMJ_mmcode_image(oArea, oImage, oLeft, oCenter, oRight) {
	if (oArea && oImage) {
		var imgValue = '';
		var align = '';
		if (oImage.type == "select-one") {
			var imgValue = oImage.options[oImage.selectedIndex].text;
		} else if (oImage.type == "file") {
			var imgValue = oImage.value;
			imgValue = imgValue.replace(/\\/g, '/');
			var parts = imgValue.split('/');
			imgValue = parts[parts.length - 1];
		}
		if (oLeft.checked) align = "left";
		else if (oCenter.checked) align = "center";
		else if (oRight.checked) align = "right";
		if (imgValue != '' && align != '') {
			oArea.value = oArea.value + '{IMAGE="' + align + '"="' + imgValue + '"}';
		}
	}
}

/**

MMJ_addOption: aggiunge un'opzione alla Select fornita
------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oSelect					Select su cui operare
sText					descrizione dell'opzione
sValue					valore dell'opzione
bUnique					inserire l'opzione solo se non è già presente il valore fornito?

OUTPUT: boolean
		restituisce True se l'inserimento ha avuto successo, False altrimenti

**/
function MMJ_addOption(oSelect, sText, sValue, bUnique) {
	if (oSelect) {
		var sErrorMsg = "";
		var bFound = (sText == '' || sValue == '');
		if (bUnique) {
			for (var i = 0; i < oSelect.options.length; i++) {
				if(oSelect.options[i].value == sValue && oSelect.options[i].text == sText)
					bFound = true;
			}
		}
		if (!bFound)
			oSelect.options[oSelect.length] = new Option(sText, sValue);
		return (!bFound);
	} else {
		return false;
	}
}

/**

MMJ_removeOptions: rimuove le opzioni dalla Select fornita ai parametri specificati
-----------------------------------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oSelect					Select su cui operare
bSelectedOnly			rimuovere solo le opzioni selezionate?
sValue					valore delle opzioni da rimuovere

Se bSelectedOnly è falso e sValue è vuoto, rimuove tutte le opzioni
Se bSelectedOnly è vero e sValue è vuoto, rimuove solo le opzioni selezionate
Se bSelectedOnly è vero e sValue è valorizzato, rimuove solo le opzioni selezionate che hanno valore sValue
Se bSelectedOnly è false e sValue è valorizzato, rimuove solo le opzioni che hanno valore sValue

OUTPUT: void

**/
function MMJ_removeOptions(oSelect, bSelectedOnly, sValue) {
	if (oSelect) {
		for (var i = oSelect.options.length - 1; i >= 0; i--) {
			if (sValue) {
				if (sValue == oSelect.options[i].value)
					oSelect.options.remove(i);
			} else {
				if (bSelectedOnly && oSelect.options[i].selected)
					oSelect.options.remove(i);
				else if(!bSelectedOnly)
					oSelect.options.remove(i);
			}
		}
	}
}

/**

MMJ_getOptionsList: restituisce la lista delle opzioni della Select fornita
---------------------------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oSelect					Select su cui operare
sSeparator1				stringa di separazione tra elementi della lista
sSeparator2				stringa di separazione tra valore e descrizione

OUTPUT: lista delle opzioni sotto forma di stringa di valori e descrizioni
		<valore1><separatore2><descrizione1><separatore1><valore2><separatore2><descrizione2>...

**/
function MMJ_getOptionsList(oSelect, sSeparator1, sSeparator2) {
	if (oSelect) {
		var sList = '';
		for (var i = 0; i < oSelect.options.length; i++) {
			sList = sList + sSeparator1 + oSelect.options[i].value + sSeparator2 + oSelect.options[i].text;
		}
		return sList.substring(1, sList.length);
	} else {
		return null;
	}
}

/**

MMJ_popup: apre una finestra di popup
-------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
sTarget					URL destinazione
sName					nome della finestra
iWidth					larghezza (in pixel)
iHeight					altezza (in pixel)
bResizable				popup ridimensionabile?

OUTPUT: void

**/
function MMJ_popup(sTarget, sName, iWidth, iHeight, bResizable) {
	if (!iWidth) iWidth = 400;
	if (!iHeight) iHeight = 300;
	var sParams = "width=" + iWidth + ",height=" + iHeight + ",menubar=no,toolbar=no,location=no,status=no,";
	if (bResizable) sParams = sParams + "resizable=yes";
	else sParams = sParams + "resizable=no";
	window.open(sTarget, sName, sParams);
}

/**

MMJ_sincSelect: sincronizza i valori di due Select gerarchiche
--------------------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oSelMain				Select principale
oSelSub					Select secondaria
bUseMain				sincronizzare in base a Select principale?
bForceValue				forzare il valore della Select sincronizzata al primo valore
						corretto?
sSeparator				stringa di separazione tra valore principale e secondario

OUTPUT: void

**/
function MMJ_sincSelect(oSelMain, oSelSub, bUseMain, bForceValue, sSeparator) {
	var sVal1 = oSelMain.options[oSelMain.selectedIndex].value;
	var sVal2 = oSelSub.options[oSelSub.selectedIndex].value;
	var re = new RegExp("^" + sVal1 + sSeparator);
	var results = sVal2.match(re);
	if (results[0].length == 0) {
		var found = false;
		var i = 0;
		if (bUseMain) {
			oSel = oSelSub;
		} else {
			oSel = oSelMain;
			re = new RegExp("^" + sVal2.substring(0, sVal2.indexOf(sSeparator)));
		}
		if (bForceValue) {
			while (i < oSel.options.length && !found) {
				results = oSel.options[i++].value.match(re);
				if (results) found = (results[0].length > 0);
			}
			if (found) oSel.selectedIndex = i - 1;
			else oSel.selectedIndex = 0;
		} else {
			oSel.selectedIndex = 0;
		}
	}
}

/**

MMJ_clearValues: resetta i valori della Form indicata
-----------------------------------------------------
author: Luca Beltrame
updated: 2005/07/19

INPUT:
oForm					Form da resettare
sPrefix					prefisso degli elementi della Form da resettare
						(se sPrefix.lenght=0 resetta tutti gli elementi)

OUTPUT: void

**/
function MMJ_clearValues(oForm, sPrefix) {
	if (oForm) {
		for (i = 0; i < oForm.elements.length; i++) {
			oElem = oForm.elements[i];
			if (sPrefix.length == 0 || oElem.name.substring(0, sPrefix.length) === sPrefix) {
				switch (oElem.type) {
					case "hidden":
					case "password":
					case "text":
					case "textarea":
						oElem.value = "";
						break;
					case "select-one":
					case "select-multiple":
						oElem.selectedIndex = 0;
						break;
					case "checkbox":
					case "radio":
						oElem.checked = oElem.defaultChecked;
						break;
				}
			}
		}
	}
}

/**

MMJ_getElementValue: ottiene il valore dell'elemento della form
---------------------------------------------------------------
author: Luca Beltrame
updated: 2005/10/14

INPUT:
oElement				elemento

OUTPUT: il valore dell'elemento fornito, null se non è 
		del tipo corretto oppure undefined se sono verificati errori

**/
function MMJ_getElementValue(oElement) {
	if (oElement) {
		switch (oElement.type) {
			case "hidden":
			case "password":
			case "text":
			case "textarea":
				return oElement.value;
			case "select-one":
			case "select-multiple":
				return oElement.options[oElement.selectedIndex].value;
			case "checkbox":
			case "radio":
				return oElement.checked;
			default:
				return null;
		}
	}
	return undefined;
}

/**

MMJ_getElementValue: imposta il valore fornito all'elemento della form
----------------------------------------------------------------------
author: Luca Beltrame
updated: 2005/10/14

INPUT:
oElement				elemento

OUTPUT: il valore dell'elemento fornito, null se non è 
		del tipo corretto oppure undefined se sono verificati errori

**/
function MMJ_setElementValue(oElement, sValue) {
	if (oElement) {
		switch (oElement.type) {
			case "hidden":
			case "password":
			case "text":
			case "textarea":
				oElement.value = sValue;
				break;
			case "select-one":
			case "select-multiple":
				oElement.selectedIndex = Number(sValue);
				break;
			case "checkbox":
			case "radio":
				oElement.checked = (sValue ? true : false);
				break;
		}
	}
}

function MMJ_getAllElementsQuerystring(oForm) {
	var out = new Array;
	if (oForm) {
		var val = false;
		var j = 0;
		for (i = 0; i < oForm.elements.length; i++) {
			if (val = MMJ_getElementValue(oForm.elements[i])) out[j++] = oForm.elements[i].name + "=" + val;
		}
	}
	return out.join("&");
}

// imposta i valori predefiniti di checkbox e radio button: se iNum è positivo, imposta tutti quelli con prefisso sElem
function MMJ_setDefault(oForm, sElem, iNum) {
	if (oForm) {
		var oElem = null;
		if (iNum > 0) {
			for (i = 0; i < iNum; i++) {
				oElem = oForm.elements[sElem + i];
				if (oElem.type == "radio" || oElem.type == "checkbox") oElem.defaultChecked = true;
			}
		} else {
			oElem = oForm.elements[sElem];
			if (oElem.type == "radio" || oElem.type == "checkbox") oElem.defaultChecked = true;
		}
	}
}

// -->


function submit_lang(lang){
	document.frmlang.lang.value=lang;
	document.frmlang.submit();
}
