
// req
// min
// mask
// msg
// tipo


function setFocus (oItem)
{
	try
	{	oItem.focus ();
	} catch (E)
	{}
}


function MascaraEmail (oItem, sMsg)
{
	var Email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (Email.test (oItem.value.Trim ())) return true;
	alert ('El campo ' + sMsg + ' no es correcto.');
	setFocus (oItem);
	return false;
}


function Requerido (oItem, iReq, sMsg, oFormulario)
{
	var Check = false;
	
	if (oItem.type == 'text' || oItem.type == 'password' || oItem.type == 'hidden' || oItem.type == 'textarea')
	{	if ((iReq == 2 && oItem.value.asFloat () <= 0) || oItem.value.Vacio ())
		{	alert ('Tiene que indicar el siguiente dato: ' + sMsg);
			setFocus (oItem);
			return false;
		}
		return true;
	} else if (oItem.type == 'select-one')
	{	if (oItem.value.Vacio ())
		{	alert ('Tiene que indicar el siguiente dato: ' + sMsg);
			setFocus (oItem);
			return false;
		}
	} else if (oItem.type == 'radio')
	{	for (var i = oFormulario [oItem.name].length - 1; i >= 0 && ! Check; i--) 
			Check = oFormulario [oItem.name][i].checked;
		if (! Check)
		{	alert ('Tiene que indicar el siguiente dato: ' + sMsg);
			setFocus (oItem);
			return false;
		}
	}
	return true;
}


function ValidarFormulario (Formulario)
{
	var Aux  = '';
	var i    = 0;
	var l    = Formulario.elements.length;
	var item = null;
	var Msg  = '';
	var Req  = false;

	for (i = 0; i < l; i++)
	{	item = Formulario.elements [i];
		Msg = item.getAttribute ('msg');
		if (! Msg) Msg = item.name;

// requerido			
		Aux = item.getAttribute ('req');
		if (Aux != null)
		{	Req = Aux == '' ? 1 : Aux.asInteger ();
			if (Req && ! Requerido (item, Req, Msg, Formulario)) return false;
		}
// min
			Aux = item.getAttribute ('min');
			if (Aux && Aux.asInteger () && item.value.length < Aux.asInteger () && item.type == 'text')
			{	alert ('El dato ' + Msg + ' tiene que tener un mínimo de ' + Aux + ' caracteres.');
				setFocus (item);
				return false;
			}
// mask
			Aux = item.getAttribute ('mask');
			if (Aux && ! item.value.Vacio () && item.type == 'text' &&! ValidarMascara (item, Aux)) return false;
// tipo
			Aux = item.getAttribute ('tipo');
			if (Aux && ! item.value.Vacio () && item.type == 'text')
			{	switch (Aux.toUpperCase ())
				{	case 'FLOAT': 
						if (isNaN (item.value.asFloat ()))
						{	alert ('El valor del campo ' + Msg + ' no es correcto.');
							setFocus (item);
							return false;
						}
						break;
					case 'INT':
						if (isNaN (item.value.asInteger ()))
						{	alert ('El valor del campo ' + Msg + ' no es correcto.');
							setFocus (item);
							return false;
						}
						break;
					default:
						alert ('El tipo ' + Tipo + ' indicado en ' + Msg + ' es incorrecto.');
						return false;
				}
			}
	}
	return true;
}


function ValidarMascara (Item, Mascara)
{
	var Msg    = Item.getAttribute ('msg');
	
	if (! Msg) Msg = Item.name;
	switch (Mascara.toUpperCase ())	
	{	case 'EMAIL': return MascaraEmail (Item, Msg);
		case 'FECHA': return MascaraFecha (Item, Msg);
		case 'FLOAT': return MascaraFloat (Item, Msg);
		case 'INT':   return MascaraInt (Item, Msg);
		default:
			alert ('Mascara ' + Mascara + ' desconocida.');
	}
	return false;
}


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



function MascaraFloat (oItem, sMsg)
{
	var sValor = oItem.value.Trim ();
	var Mask   = /^-?[0-9]+[\.|,]?[0-9]*$/;

	if (! sValor) return true;
	if (Mask.test (sValor))
	{	oItem.value = sValor.replace (',', '.');
		return true;
	}
	alert ('El campo ' + sMsg + ' no es correcto.');
	setFocus (oItem);
	return false;
}

function MascaraInt (oItem, sMsg)
{
	var sValor = oItem.value.Trim ();
	var Mask   = /^-?[0-9]+$/;

	if (Mask.test (sValor)) return true;
	alert ('El campo ' + sMsg + ' no es correcto.');
	setFocus (oItem);
	return false;
}


function MascaraFecha (oItem, sMsg)
{
	var Cad    = oItem.value.Trim ();
	var Sep    = /[\/\.\-]/;
	var Fecha  = /^([0-9]{1,2})+[\/\.\-]+([0-9]{1,2})+[\/\.\-]+([0-9]{1,4})$/;
	var Result = false;
	var aFecha;

	if (Fecha.test (Cad))
	{	aFecha = Cad.split (Sep);
		aFecha [0] = aFecha [0].replace (/^0*/g, '').asInteger ();
		aFecha [1] = aFecha [1].replace (/^0*/g, '').asInteger ();
		aFecha [2] = aFecha [2].replace (/^0*/g, '').asInteger ();
		if (aFecha [2] > 0 && aFecha [2] < 100) aFecha [2] += 2000;
		if (aFecha [0] > 0 && aFecha [1] > 0 && aFecha [1] <= 12 && aFecha [2] > 0)
		{	if (aFecha [1] == 2)
			{	if (aFecha [0] <= 28) Result = true;
				else Result = (aFecha [0] == 29) && (aFecha [2] % 4 == 0);
			} else if (aFecha [1] == 4 || aFecha [1] == 6 || aFecha [1] == 9 || aFecha [1] == 11) Result = aFecha [0] < 31;
			else Result = aFecha [0] < 32;
			if (Result) 
			{	oItem.value = (aFecha [0] < 10 ? '0' : '') + aFecha [0] + '/' + (aFecha [1] < 10 ? '0' : '') + aFecha [1] + '/' + aFecha [2];
				return true;
			}
		} 
	}
	alert ('El campo ' + sMsg + ' no es correcto.');
	setFocus (oItem);
	return false;
}
