/**
 * DHTML functions for dynamic webpages
 * Try to keep it small.
 *  
 * @author Bogdan Adrian Stanescu
 * @author Cristian Buda
 * 
 * @todo more...
 * @class DHTML
 * @version $Id: dhtmlUtilsLite.js 121 2005-05-08 20:17:42Z bstanescu $
 */


/**
 * Changes the class of an object
 * Use:
 * <code>
 *    we consider two classes (styles) defined before (newstyle and oldstyle)
 *    <tr onmouseover="setClassName(this, 'newstyle');" onmouseout="setClassName(this, 'oldstyle');"> 
 * </code>
 **/
function setClassName(obj, className) {
   //if the object should be reffered by its id
   //we should use:
   // document.getElementById(objId).className = className;
   obj.className = className;
}


/**
 * Function that hides, displays or toggles the state of a layer
 *
 * @param layerName  string   The name/id of the 'div' element
 * @param actionType integer  Type of action: -1=hide; 1=display; 2=toggle actual state.
 */
function showHideLayer(layerName, actionType, frameName)
{
	//if no action specified, this function will toggle the current state	
	if (actionType == 2)
	{
		if (frameName != null)
		{
   	   new_style = (top.frames[frameName].document.getElementById(layerName).style.display != "none") ? "none" : "block";		   
		}
		else
		{
   	   new_style = (document.getElementById(layerName).style.display != "none") ? "none" : "block";
   	}   
   }
   else
   {
   	new_style = (actionType == 1) ? "block" : "none";
   }
   if (frameName != null)
	{
	   top.frames[frameName].document.getElementById(layerName).style.display = new_style;
	}
	else
	{
	   document.getElementById(layerName).style.display = new_style;
	}   
}


/**
 * Verifies if layer 'divName' has already been loaded (if variable divName is == 1)
 * If not, it loads in tempFrame the 
 *
 * @param layerName  string   The name/id of the 'div' element
 * @param actionType integer  Type of action: -1=hide; 1=display; 2=toggle actual state.
 */
function expandOrReloadDiv(divName, tempFrame, uriToLoad)
{
	//daca div-ul deja a fost incarcat.. 
	if (eval('window.' + divName) != 1)
	{
		//in caz contrar, incarcam in frame-ul tempFrame javascript-ul care va completa continutul
		//layer-ului si va seta variabila divName = 1
		top.frames[tempFrame].document.location = uriToLoad;
	}
	showHideLayer(divName, 2);
}


/**
 * Populates div 'divName' in frame 'frameName' with content 'content'
 * Content can (and should) contain the string in hex-encoding (see link below) since parameters
 *  used when calling JavaScript functions cannot contain newlines or doublequotes when the
 *  parameter is delimited by doublequotes, etc. Hence, if 'content' is a plain alphanumeric string
 *  there is no need to encode it in the function calling. But it's always safe to do it.
 *
 * Smarty use:    populateDiv("some_div_name", "name_of_the_frame", "{$the_content|escape:"hex"}");
 *
 * @param divName    string   The name/id of the 'div' element
 * @param frameName  string   The name of the 'frame' where the div resides
 * @param content    string   The (possible HEX ENCODED) content. (using js function escape)
 * 
 * @link http://wp.netscape.com/eng/mozilla/3.0/handbook/javascript/ref_d-e.htm#48073
 */
function populateDiv(divName, frameName, content)
{
	top.frames[frameName].document.getElementById(divName).innerHTML = unescape(content);
}



/**
 * Returns the absolute layer's coordinate X
 *
 * @param obj the object (layer)
 */
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

/**
 * Returns the absolute layer's coordinate Y
 *
 * @param obj the object (layer)
 */
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}



/**
 * 
 *
 * @param selectObj   The name of the select
 * @param valueArray  the JS array
 */
function changeSelOption(selectObj, valueArray, somevalue)
{	
 	var j = 0;	

	if (selectObj != "")
	{
		document.getElementById(selectObj).selectedIndex = 0;
		
   	// delete all select's options
   	for(i=0; i<document.getElementById(selectObj).length; i++)
   	{
   	   document.getElementById(selectObj).options[i] = null;
   	}
	   document.getElementById(selectObj).length = 0;
	   
   	for (i=0; i<valueArray.length; i++)
   	{	
   		if (valueArray[i][0] == somevalue)
   		{
   		//	alert (selectObj + somevalue);
   	   	optionName = new Option(valueArray[i][2], valueArray[i][1], false, false);	   	   	
   	   	document.getElementById(selectObj).options[j] = optionName;
   		   j++;
   	   }	
   	}
   		
	}	
	
	document.getElementById(selectObj).selectedIndex = 0;

  	return j;
}