/*---------------------------------------------------\
|                                                    |  
| This file contains general purpose functions       |
|                                                    |
\---------------------------------------------------*/

//**************************************************
//Check the user client
var ie = navigator.userAgent.indexOf ( "MSIE") >= 0;///MSIE/.test(navigator.userAgent);
var moz = !ie && ( navigator.product == "Gecko" );


//**************************************************
//Trim white-spaces at both the begin and the end of a string
function Trim(s) 
{
	// Remove leading spaces and carriage returns		  
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))		
		s = s.substring(1,s.length);
	// Remove trailing spaces and carriage returns
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))		
		s = s.substring(0,s.length-1);		
	return s;
}

//**************************************************
//function to handle DataViewItemType object

//Item type constants
var ITEM_TYPE_FILE			= 1
var ITEM_TYPE_DIRECTORY	= 2

//return an array from the serialized form
function deserializeItem ( serializedObj )
{
	return serializedObj.split ( "|$|" );	
}

//return the type of a DataViewItemType object
function getItemKey ( item )
{
	if ( item.length == 4 )	
		return item[0];	
	return "";	
}

//return the type of a DataViewItemType object
function getItemType ( item )
{
	if ( item.length == 4 )	
		return item[3] * 1;
	
	return 0;	
}

//return the value of a DataViewItemType object
function getItemValue ( item )
{
	if ( item.length == 4 )	
		return item[2];
	
	return 0;	
}

//return the text of a DataViewItemType object
function getItemText ( item )
{
	if ( item.length == 4 )	
		return item[1];
	
	return "";	
}

//display all the info of a DataViewItemType object
function getItemInfo ( item )
{
	if ( item.length != 4 )	
		return "Numero errato di paramentri: " + item.length;
	return " ( " + ( (getItemType ( item ) == ITEM_TYPE_FILE ) ? "File" : "Directory" ) + " ) '" + getItemText ( item ) + "' = " + getItemValue ( item );
}


//**************************************************
// Functions to handle callbacks

// Get the callback arguments array
function getCallbackArgs ( args )
{	
	return	args[0];
}

// Join an array in to a string for postback
function joinCallbackArgs ( args )
{
	var	str = "";
	for ( var a = 0; a < args.length; a++ )
		if ( a == args.length -1 )
			str	+= args[a];
		else
			str	+= args[a] + "|$|";
	
	return str;			
}


function objPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}
function objWindowOffsetLeft (el) {
	var ca	=	document.getElementById ( "contentArea" );
	//return objPageOffsetLeft(el)-document.body.scrollLeft;
	return objPageOffsetLeft(el);
}
function objPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}
function objWindowOffsetTop (el) {
	var ca	=	document.getElementById ( "contentArea" );
	///return objPageOffsetTop(el)-document.body.scrollTop;
	return objPageOffsetTop(el);
}

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;
}

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;
}

function getScrollTop(source)
{
	var	el	=	source;	
	var	sc	=	0;
	var	i		=	0;	
	while((el=el.offsetParent) != null) { sc += el.scrollTop; }		
	
	return sc;
}

function getScrollLeft(source)
{
	var	el	=	source;	
	var	sc	=	0;	
	while((el=el.offsetParent) != null) { sc += parseNum(el.scrollLeft); }	
	return sc;
}

//Gets the x coordinate of the mouse relative to the element
function getMouseX (element)
{	
	return event.clientX + getScrollLeft(element) + window.document.documentElement.scrollLeft;
}
//Gets the y coordinate of the mouse relative to the element
function getMouseY (element)
{	
	return event.clientY + getScrollTop(element) + window.document.documentElement.scrollTop;
}

function debug ( value )
{
	var dbg	=	document.getElementById ( "debugger" );
	if ( dbg )
		dbg.innerHTML	=	value + "<br>" + dbg.innerHTML;
}

function parseNum ( num )
{		
	return  num ? parseInt(num) : 0;	
}

function isEmpty ( s )
{
	return (s && s != "") ? false: true;
}

function getElement ( id ) 
{
	if ( !id ) return new Object();
	return document.getElementById ( id );	
}

function getOptionSelectedText ( selectID  )
{
	var select = getElement ( selectID );
	if ( !select )
		return "";	
	return 	select.options[select.selectedIndex].text;	
}

function getOptionSelectedValue( selectID  )
{
	var select = getElement ( selectID );
	if ( !select )
		return "";	
	return 	select.options[select.selectedIndex].value;	
}

function selectOptionByText ( selectID , text )
{
	var select = getElement ( selectID );
	if ( !select )
		return;
	
	for ( var i = 0; i<select.options.length;i++)
	{
		if ( select.options[i].text == text )
		{
			select.selectedIndex = i;
			return;
		}	
	}
}

function selectOptionByValue( selectID , value )
{
	var select = getElement ( selectID );
	if ( !select )
		return;
	
	for ( var i = 0; i<select.options.length;i++)
	{
		if ( select.options[i].value.toString() == value.toString() )
		{
			select.selectedIndex = i;
			return;
		}	
	}
}



function $E() {
	var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}
