
Number.prototype.isInt = function () { return ! (/\.+/).test (this.toString ()); }

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');
}


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 = parseInt (this);

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


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 (aValor [Propiedad]);
				else Result += 's:' + Propiedad.length + ':"' + Propiedad + '";' + SerializeValor (aValor [Propiedad]);
				break;
			case 'number':
				n++;
				Result += 'i:' + Propiedad + ';' + SerializeValor (aValor [Propiedad]);
				break;
		}
	}
	return 'a:' + n + ':{' + Result + '}';
}


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

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


function SerializeValor (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 + '";';
}

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

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 = Redondear (fValor, iDecimales);
	if (fValor < 0)
	{	Result = '-';
		fValor = Math.abs (fValor);
	}
	ParteEntera  = Math.floor (fValor);
	ParteDecimal = String (Redondear (fValor - ParteEntera, 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 Redondear (fValor, iDecimales)
{
	var Precision = Math.pow (10, iDecimales);

	return (Math.round (fValor * Precision) / Precision);
}


function $ ()
{
	if (arguments.length == 1) return document.getElementById (arguments [0]);

  var Elementos = new Array();
  for (var i = 0; i < arguments.length; i++)
  	Elementos.push (document.getElementById (arguments [i]));
  return Elementos;
}


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 Clase (Etiqueta, Valor)
{
	var Aux = document.getElementById (Etiqueta);

	if (Aux) Aux.className = Valor;
}
