var oFCKeditor = null;
var wysiwyg = null;
var oFCKactivated = false;

function showElement(x)
{
	if (document.getElementById(x))
		document.getElementById(x).style.display = '';
}

function hideElement(x)
{
	if (document.getElementById(x))
		document.getElementById(x).style.display = 'none';
}

function toggleDisplayElement(x)
{
	if (document.getElementById(x))
		document.getElementById(x).style.display = document.getElementById(x).style.display == 'none' ? '' : 'none';
}

function getParentOrSelf( contextElm, nodeName )
{
	nodeName = nodeName.toLowerCase();
	while(contextElm.nodeName.toLowerCase() != nodeName && contextElm.parentNode)
		contextElm = contextElm.parentNode;
	return contextElm;
}

function centerElement(id)
{
	if (document.getElementById)
	{
		var el = document.getElementById(id);
		if (el.style.display != "block")
		{
			var box = getRelativeDimension(el,'container');
			el.style.left = '' + ( ( document.body.offsetWidth - box.w ) / 2 ) + 'px';
			el.style.top = '' + ( ( document.body.offsetHeight - box.h ) / 2 ) + 'px';
		}
	}
}

function centerElementHoriz(id)
{
	if (document.getElementById)
	{
		var el = document.getElementById(id);
		if (el.style.display != "block")
		{
			var box = getRelativeDimension(el,'container');
			el.style.left = '' + ( ( document.body.offsetWidth - box.w ) / 2 ) + 'px';
		}
	}
}

function showAndTrackElement (el, div, alignX, alignY)
{
	if (document.getElementById)
	{
		var i = document.getElementById(el);
		var c = document.getElementById(div);
		if (c.style.display != "block")
		{
			var box = getRelativeDimension(i,'');
			var left = box.x, top = box.y;
			c.style.visibility = 'hidden';
			c.style.display = "block";
//alert(""+left+","+top)
			if(alignX == 'left')
				left -= c.offsetWidth;
			else
				left += i.offsetWidth;
			if(alignY == 'top')
				top -= c.offsetHeight;
			else
				top += i.offsetHeight;
//alert(""+c.offsetWidth+","+i.offsetHeight)
			if(top<10)
				top = 10;
			if(left<0)
				left = 10;
			//if(gClientIsIE5)
			if(false)
			{
				left += document.body.scrollLeft;
				top += document.body.scrollTop;
			}
			c.style.left = left+'px';
			c.style.top = top+'px';
			c.style.visibility = 'visible';
		}
		else
		{
			c.style.display="none";
		}
	}
}

function activateFck(areaId,areaWidth,areaHeight,toolbarSet)
{
	if ( !oFCKactivated[areaId] )
	{
		oFCKeditor = new FCKeditor(areaId);
		oFCKeditor.BasePath = "/javascript/fckeditor/";
		oFCKeditor.Width = areaWidth;
		oFCKeditor.Height = areaHeight;
		oFCKeditor.Config["ToolbarStartExpanded"] = true;
		oFCKeditor.ToolbarSet=toolbarSet;
		//oFCKeditor.Config["SkinPath"] = "/editor/fck/editor/skins/default/";
		oFCKeditor.ReplaceTextarea()
		wysiwyg = oFCKeditor._IsCompatibleBrowser();

		oFCKactivated[areaId] = true;
	}
}

function transposeAddress(userid,domainName,domainExt,subject)
{
	var email = domainName + '.' + domainExt +'@' + userid;
	var TempBefore = '';
	var TempAfter = '';
	var NewString = '';
	var Start = 0;

	var WhereHash = email.indexOf ('@');
	for (Count = 1; Count <= WhereHash; Count ++)
	{
		TempBefore += email.substring (Start, Count);
		Start++
	}
	Start = WhereHash;
	Start ++
	WhereHash +=2;
	for (Count = WhereHash; Count <=email.length; Count++)
	{
		TempAfter +=email.substring (Start, Count)
		Start++
	}
	NewString = TempAfter +'@' + TempBefore;
	parent.location = 'mailto:'+NewString+'?subject='+subject;
}

function chiocciola()
{
	document.write('@');
}

function urldecode(str)
{
	var unescaped = unescape(str);
	return unescaped.replace(/\+/g,' ');
}

function setFieldValue(fieldId,fieldvalue)
{
	document.getElementById(fieldId).value=fieldvalue;
}

function showForm(formId)
{
	el = document.getElementById(formId);
	el.reset();
	el.style.display = '';
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
function markAllRows(container_id,rowTag) {
    var rows = document.getElementById(container_id).getElementsByTagName(rowTag);

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
            }
        }
    }

    return true;
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
function unMarkAllRows( container_id,rowTag ) {
    var rows = document.getElementById(container_id).getElementsByTagName(rowTag);

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            checkbox.checked = false;
        }
    }

    return true;
}


function toggleFieldsOnSelection(objId)
{
//	Determino se gli elementi sono da nascondere o mostrare in base alla selezione o meno di un valore nella select
	var obj = document.getElementById(objId);
	
	if ( obj )
	{
		var showElements = obj.value <= 0;
		
	//	Cerco l'elemento 
		while ( obj!=null && obj.tagName.toLowerCase()!='fieldset' )
		{
			obj.isCaller = true;
			obj=obj.parentNode;
		}
			
		for ( i=0; i<obj.childNodes.length; i++ )
			if ( obj.childNodes[i].tagName.toLowerCase()!='legend' && !obj.childNodes[i].isCaller )
				obj.childNodes[i].style.display = showElements ? '' : 'none';
	}	
	//alert(obj)
}