String.prototype.endsWith = function(str) {return (this.match(str+"$")==str)}

Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}

function onContent(f){//(C)webreflection.blogspot.com
var a=onContent,b=navigator.userAgent,d=document,w=window,c="onContent",e="addEventListener",o="opera",r="readyState",
s="<scr".concat("ipt defer src='//:' on",r,"change='if(this.",r,"==\"complete\"){this.parentNode.removeChild(this);",c,".",c,"()}'></scr","ipt>");
a[c]=(function(o){return function(){a[c]=function(){};for(a=arguments.callee;!a.done;a.done=1)f(o?o():o)}})(a[c]);
if(d[e])d[e]("DOMContentLoaded",a[c],false);
if(/WebKit|Khtml/i.test(b)||(w[o]&&parseInt(w[o].version())<9))(function(){/loaded|complete/.test(d[r])?a[c]():setTimeout(arguments.callee,1)})();
else if(/MSIE/i.test(b))d.write(s);
};

function boldLabel(cbox, label)
{
	if (cbox.checked == true)
	{
		document.getElementById(label).style.fontWeight = 'bold';
	}
	else
	{
		document.getElementById(label).style.fontWeight = 'normal';	
	}
}

function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}

function chkEstensioneImmagine(file)
{
	var ext = file.substring(file.length - 3, file.length);
	
	if (ext != "png" && ext != "jpg" && ext != "gif")
	{
		return false;
	}
	else
	{
		return true;
	}
}

function trim(stringa)
{
	while (stringa.substring(0,1) == ' ')
	{
		stringa = stringa.substring(1, stringa.length);
	}
	while (stringa.substring(stringa.length-1, stringa.length) == ' ')
	{
		stringa = stringa.substring(0,stringa.length-1);
	}
	
	return stringa;
}

function isChecked(frm, pType)
{
	if (pType == "")
	{
		pType = "checkbox";
	}
	
	var formElements = frm.elements;
	var el = new Array();
	j = 0;
	for (i=0; i<formElements.length;i++)
	{
		if (formElements[i].type == pType)
		{
			el[j] = formElements[i];
			j++;
		}
	}
	
	countUnchecked = 0;
	
	for (j=0; j<el.length;j++)
	{
		if (!el[j].checked)
		{
			countUnchecked++;
		}
	}
	
	if (countUnchecked == el.length)
	{
		return false;
	}
	else
	{
		return true;
	}
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function mngChkSelect(partName, selectId)
{
	var chk = "chk_"+partName;
	var ico = "ico_"+partName;
	
	if ($(selectId).value != "" && $(selectId).value != 0)
	{
		$(chk).checked = true;
		$(ico).src = "../../template/default/image/layout/chk_on.png";
		
		if ($("v_selezionati").value != "")
		{
			var valori = $("v_selezionati").value.split(";");
			if (valori.length > 2 && valori.indexOf(partName) == -1)
			{
				$("chk_"+valori[0]).checked = false;
				$("ico_"+valori[0]).src = "../../template/default/image/layout/chk_off.png";
				valori[0] = null;
				
				valori = valori.clean();
				$("v_selezionati").value = valori.join(";");
			}
		}
		
		if ($("v_selezionati").value == "" || (isset(valori) && valori.indexOf(partName) == -1))
		{
			$("v_selezionati").value += partName + ";";
		}
	}
	else
	{
		$(chk).checked = false;
		$(ico).src = "../../template/default/image/layout/chk_off.png";
		
		if ($("v_selezionati").value != "")
		{
			var valori = $("v_selezionati").value.split(";");
			for (i=0;i<valori.length;i++)
			{
				if (valori[i] == partName)
				{
					valori[i] = null;
					break;
				}
			}
			
			valori = valori.clean();
			$("v_selezionati").value = valori.join(";");
		}
	}
}

function mngChk(partName, select, selectId)
{
	var chk = "chk_"+partName;
	var ico = "ico_"+partName;
	
	if ($(chk).checked)
	{
		$(chk).checked = false;
		$(ico).src = "../../template/default/image/layout/chk_off.png";
		
		if (select == 1)
		{
			//$(selectId).options[0].selected = true;
		}
		
		if ($("v_selezionati").value != "")
		{
			var valori = $("v_selezionati").value.split(";");
			for (i=0;i<valori.length;i++)
			{
				if (valori[i] == partName)
				{
					valori[i] = null;
					break;
				}
			}
			
			valori = valori.clean();
			$("v_selezionati").value = valori.join(";");
		}
	}
	else
	{
		$(chk).checked = true;
		$(ico).src = "../../template/default/image/layout/chk_on.png";
		
		if ($("v_selezionati").value != "")
		{
			var valori = $("v_selezionati").value.split(";");
			if (valori.length > 2 && valori.indexOf(partName) == -1)
			{
				$("chk_"+valori[0]).checked = false;
				$("ico_"+valori[0]).src = "../../template/default/image/layout/chk_off.png";
				valori[0] = null;
				
				valori = valori.clean();
				$("v_selezionati").value = valori.join(";");
			}
		}
		
		if ($("v_selezionati").value == "" || (isset(valori) && valori.indexOf(partName) == -1))
		{
			$("v_selezionati").value += partName + ";";
		}
	}
}

function isset(variable_name)
{
	try
    {
        if (typeof(eval(variable_name)) != "undefined")
        if (eval(variable_name) != null)
        return true;
    }
    catch(e) { }
    return false;
}
