
Number.prototype.isInt     = function () { return ! (/\.+/).test (this.toString ()); }
Number.prototype.Redondear = function (iDecimales) { return Math.round (this * Math.pow (10, iDecimales)) / Precision; }
Number.prototype.asInteger = function () 
{ 
		var Aux = parseInt (this);

		if (isNaN (Aux)) return 0;
		return Aux;
}

	
String.prototype.isInt   = function () { return (/^[0-9]+$/).test (this.Trim ()); }
String.prototype.isFloat = function () { return (/^-?[0-9]+[\.|,]?[0-9]*$/).test (this.Trim ()); }
String.prototype.LTrim   = function () { return (this.replace (/^\s*/g, '')); }
String.prototype.RTrim   = function () { return (this.replace (/\s*$/g, '')); }
String.prototype.Texto   = function () { return (this.replace(/<\/?[^>]+>/gi, '')); }
String.prototype.Trim    = function () { return (this.replace (/^\s*|\s*$/g, '')); }
String.prototype.Vacio   = function () { return (this.Trim () == ''); }
Number.prototype.Format  = function (Decimales) { return FlotanteACadena (this, Decimales); }


String.prototype.asBoolean = function ()
{
	var Aux = this.Trim ();

	return Aux == 'S' || Aux == 's' || Aux == 'T' || Aux == 't' || Aux == '1' || Aux == 'true';
}


String.prototype.asCharCode = function ()
{
	var Aux    = this.Trim ();
	var l      = Aux.length;
	var aCodes = new Array ();
	var Car     = 0;

	for (var i = 0; i < l; i++)
		aCodes.push ((Car = Aux.charCodeAt (i)) > 256 ? '&#' + Car + ';' : Aux.charAt (i));
	return aCodes.join ('');
}


String.prototype.asFloat = function ()
{
	var Numero = /^-?\d*[\.,]?\d*$/;
	var Aux    = this.Trim ();

	if (Numero.test (Aux))
	{	if (Aux) return parseFloat (Aux.replace (',', '.'));
		return (0);
	}
	return NaN;
}


String.prototype.asInteger = function ()
{
	var Aux = this;
	
	while (Aux.charAt (0) == '0') Aux = Aux.substr (1);
	Aux = parseInt (Aux);
	if (isNaN (Aux)) return 0;
	return Aux;
}



//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

function FlotanteACadena (fValor, iDecimales)
{
	var ParteEntera  = 0;
	var ParteDecimal = 0;
	var i, l;
	var Result       = '';
	var Aux;

	if (typeof fValor == 'undefined') fValor = 0;
	if (typeof iDecimales == 'undefined') iDecimales = 0;
	fValor = fValor.Redondear (iDecimales);
	if (fValor < 0)
	{	Result = '-';
		fValor = Math.abs (fValor);
	}
	ParteEntera  = Math.floor (fValor);
	ParteDecimal = String ((fValor - ParteEntera).Redondear (iDecimales)).substring (2);

	Aux = String (ParteEntera);
	l = Aux.length;
	if (l > 3)
	{	Result += Aux.charAt (0);
		for (i = 1; i <= l; i++)
		{	if (i < l &&  (l - i) % 3 == 0) Result += '.';
			Result += Aux.charAt (i);
		}
	} else Result += Aux;
	if (iDecimales > 0)
	{	Result += ',' + ParteDecimal;
		for (i = (iDecimales - ParteDecimal.length); i > 0; i--) Result += '0';
	}
	return (Result);
}






function RadioValue (oRadio)
{
	for (var i = oRadio.length - 1; i > 0 && ! oRadio [i].checked; i--);
	if (oRadio [i].checked) return oRadio [i].value;
	return '';
}


function CompararFechas (sFecha1, sFecha2)
{
	// Las cadenas de entrada son del tipo dd/mm/aaaa
	var aFecha1 = sFecha1.split ('/');
	var aFecha2 = sFecha2.split ('/');
	
	if (aFecha1 [2] < aFecha2 [2]) return -1;
	if (aFecha1 [2] > aFecha2 [2]) return 1;
	if (aFecha1 [1] < aFecha2 [1]) return -1;
	if (aFecha1 [1] > aFecha2 [1]) return 1;
	if (aFecha1 [0] < aFecha2 [0]) return -1;
	if (aFecha1 [0] > aFecha2 [0]) return 1;
	return 0;
}

function PageSize ()
{
	var xScroll, yScroll;
	var windowWidth, windowHeight;
	
	if (window.innerHeight && window.scrollMaxY) 
	{	xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight)
	{ xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else 
	{ xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	if (self.innerHeight) 
	{	if(document.documentElement.clientWidth) windowWidth = document.documentElement.clientWidth; 
		else windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) 
	{	windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) 
	{ windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight) pageHeight = windowHeight;
	else pageHeight = yScroll;

	if(xScroll < windowWidth) pageWidth = xScroll;		
	else pageWidth = windowWidth;

	return new Array (pageWidth, pageHeight, windowWidth, windowHeight);
}



function $ ()
{
	var Aux  = document.getElementById (arguments [0]);
	var Pars = null;
	
	switch (arguments.length)
	{	case 1: return Aux;
		case 2: return Aux ? Aux [arguments [1]] : null;
		case 3: 
			if (Aux) 
			{	Pars = arguments [1].split ('.');
				for (var i = 0; i < Pars.length - 1; i++) Aux = Aux [Pars [i]];
			  Aux [Pars [i]] = arguments [2]; 
			}
			break;
		default: alert ('$, número de parámetros incorrectos.');
	}
}


function TextAreaMaxLength (oText, iMaxLength)
{
	if (oText.value.length > iMaxLength) oText.value = oText.value.substring (0, iMaxLength);
}

function EliminarHijos (Padre)
{
	if (Padre.hasChildNodes())
		for (var i = Padre.childNodes.length - 1; i >= 0; i--) Padre.removeChild (Padre.childNodes [i]);
}


function Serialize (oObj)
{
	if (typeof (oObj) != 'object') alert ('Serialize: Tipo de dato incorrecto.');
	else return 'O:8:"TDatosJS":' + SerializePropiedades (oObj) + '}';
}


function SerializeArray (aValor)
{
	var Propiedad;
	var Result = '';
	var n = 0;

	for (Propiedad in aValor)
	{	switch (typeof (Propiedad))
		{	case 'string':
				n++;
				if (Propiedad.isInt ()) Result += 'i:' + Propiedad + ';' + _SerializeValor (Propiedad, aValor [Propiedad]);
				else Result += 's:' + Propiedad.length + ':"' + Propiedad + '";' + _SerializeValor (Propiedad, aValor [Propiedad]);
				break;
			case 'number':
				n++;
				Result += 'i:' + Propiedad + ';' + _SerializeValor (Propiedad, aValor [Propiedad]);
				break;
			case 'object':
				n++;
				Result += Serialize (aValor [Propiedad], Propiedad);
				break;
		}
	}
	return 'a:' + n + ':{' + Result + '}';
}


function SerializePropiedades (oObj)
{
	var n = 0;
	var Result = '';
	var Propiedad;
	var Valor;

	for (Propiedad in oObj)
	{	Valor = _SerializeValor (Propiedad, oObj [Propiedad]);
		if (Valor)
		{	n++;
			Result += SerializeVariable (Propiedad) + Valor;
		}
	}
	return n + ':{' + Result;
}


function _SerializeValor (sPropiedad, vValor)
{
	switch (typeof (vValor))
	{	case 'string': return 's:' + vValor.length + ':"' + vValor + '";';
		case 'boolean': return 'b:' + (vValor ? '1;' : '0;');
		case 'number':
			if (vValor.isInt ()) return 'i:'+ vValor + ';';
			return 'd:' + vValor + ';';
		case 'object':
			if (vValor instanceof Array) return SerializeArray (vValor);
			return '';
	}
	return '';
}


function SerializeVariable (sNom)
{
	return 's:' + sNom.length + ':"' + sNom + '";';
}




