/*
	_Dom.js : Wrapper DOM
*/

// ------------------------------------
// Datos

// ------------------------------------
// Ds_GetElementById : Wrapper
function Ds_GetElementById( e )
{
	if( typeof(e) !='string' )
		return e;

	if( document.getElementById )	// DOM level 1 conformance
		e = document.getElementById( e );
	else if( document.all )			// IE 4.x browsers
		e = document.all[e];
	else							// No support
		e = null;
	
	return e;
}

// ------------------------------------
// Ds_Display : Set
function Ds_Display( e, sDisplay )
{
	if( !(e = Ds_GetElementById(e)) )
		return false;
		
	if( __IsDefined(e.style) )
		e.style.display = sDisplay;
}

// ------------------------------------
// Ds_SetClass : Set
function Ds_SetClass( e, sClass )
{
	if( !(e = Ds_GetElementById(e)) )
		return false;
		
	if( __IsDefined(e.className) )
		e.className = sClass;
}

// ------------------------------------
// Ds_Width : Set/Get
function Ds_Width( e, nW )
{
	if( !(e = Ds_GetElementById(e)) )
		return 0;
	
	if( __IsNum( nW ) )
	{
		if( nW < 0 )
			nW = 0;
		else
			nW = Math.round( nW );
	}
	else
		nW = -1;
	
	var bCss = __IsDefined( e.style );

	if( __IsDefined(e.style) && __IsDefined(e.offsetWidth) && __IsString(e.style.width) )
	{
		if( nW >= 0 )
			e.style.width = (nW + "px"); // Mejora, tener en cuenta borde y padding
			
		nW = e.offsetWidth;
	}
	else if( __IsDefined(e.style) && __IsDefined(e.style.pixelWidth) )
	{
		if( nW >= 0 ) 
			e.style.pixelWidth = nW;
			
		nW = e.style.pixelWidth;
	}
	
  return nW;
}

// ------------------------------------
// Ds_SetText : Set
function Ds_SetText( e, sText )
{
	if( !(e = Ds_GetElementById(e)) )
		return;
		
	if( null != document.getElementById )
		e.childNodes[0].nodeValue = sText;
	else if( null != document.all )
		e.innerText = sText;
}

// ------------------------------------
// SetAttribute : Set
function Ds_SetAttribute( e, sAttr, sValue )
{
	if( !(e = Ds_GetElementById(e)) )
		return;
		
	e.setAttribute( sAttr, sValue );
}



// ------------------------------------
// Ds_SetFocus : Set
function Ds_SetFocus( e )
{
	if( !(e = Ds_GetElementById(e)) )
		return;
		
	e.focus();
}

// ------------------------------------
// Ds_SetReadOnly : Set
function Ds_SetReadOnly( e, bSet )
{
	if( !(e = Ds_GetElementById(e)) )
		return;
		
	e.readOnly = bSet;
}

// ------------------------------------
// Ds_SetCheck : Set
function Ds_SetCheck( e, bSet )
{
	if( !(e = Ds_GetElementById(e)) )
		return;
		
	e.checked = bSet;
}

// ------------------------------------
// Ds_IsChecked : Get
function Ds_IsChecked( e )
{
	if( !(e = Ds_GetElementById(e)) )
		return;
		
	return e.checked;
}

// --------------------------------------------------------
// Funciones de Formularios

// ------------------------------------
// Ds_SetFieldValue: Asigna un valor a un campo de un formulario
function Ds_SetFieldValue( e, xValue )
{
	var sTag = "";
	
	if( !(e = Ds_GetElementById(e)) )
		return false;

	sTag = e.tagName.toUpperCase();
		
	if ( sTag == "INPUT" )
	{
		e.value = xValue;

		if( e.type.toUpperCase() == "CHECKBOX" )
			e.checked = ((parseInt(xValue)==1)?true:false);
	}
	else if ( sTag == "SELECT" )
		__SelectOption( e.options, xValue );
	else if ( sTag == "TEXTAREA" )
		e.value = __Text2CRLF(xValue); // Por compatibilidad
}

// ------------------------------------
// __SelectOption: Selecciona un elemento de un "select"
function __SelectOption( aOptions, xValue )
{
	var n = 0;

	for ( n = 0; n < aOptions.length; n++ )
	{
		if ( aOptions[n].value == xValue )
		{
			aOptions[n].selected = true;
			break;
		}
	}
}

// ------------------------------------
// Ds_GetFieldValue: Obtiene un valor de un campo de un formulario
function Ds_GetFieldValue( e )
{
	var sTag   = "";
	var xValue = "";

	if( !(e = Ds_GetElementById(e)) )
		return false;

	sTag = e.tagName.toUpperCase();
		
	if ( (sTag == "INPUT") || sTag == ("TEXTAREA") )
	{
		xValue = e.value;

		if( e.type.toUpperCase() == "CHECKBOX" )
			xValue = e.checked;
	}
	else if ( sTag == "SELECT" )
		xValue = e.options[e.selectedIndex].value;
	
	return xValue;
}

// ------------------------------------
// Ds_GetComboValue: Obtiene al valor del Elemento seleccionado en el Combo
function Ds_GetComboValue( e )
{
	return Ds_GetFieldValue( e );
}

// ------------------------------------
// Ds_GetComboText: Obtiene el Texto del Elemento seleccionado en el Combo
function Ds_GetComboText( e )
{
	var sTag   = "";
	var xValue = "";

	if( !(e = Ds_GetElementById(e)) )
		return "";
	
	sTag = e.tagName.toUpperCase();
		
	if ( sTag == "SELECT" )
		xValue = e.options[e.selectedIndex].text;
	
	return xValue;
}



// --------------------------------------------------------
// Funciones de Utilidad

// ------------------------------------
function __IsTypeof( sType, args )
{
	for( var n = 0; n < args.length; n++ )
	{
		if( typeof(args[n]) != sType ) 
			return false;
	}
	
	return true;
}

// ------------------------------------
function __IsDefined()
{
	for( var n = 0; n < arguments.length; n++ )
	{
		if( typeof(arguments[n]) == "undefined" ) 
			return false;
	}
	
	return true;
}

// ------------------------------------
function __IsNum()
{
	return __IsTypeof( "number", arguments );
}

// ------------------------------------
function __IsString()
{
	return __IsTypeof( "string", arguments );
}

// ------------------------------------
// __Text2CRLF : Vuelve a Poner los retornos de carro
function __Text2CRLF( s )
{
	var re = /\|/gm;
	s      = s.replace( re, "\n" ) ;

	return s;
}
