
//
//	28/04/2010	Se quita la vinculación de TEventos.js
//	28/01/2010	Se añaden los mensajes por idioma
//	29/12/2009 	Se añade el método Init
//

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

Formulario.Lng = 0;


Formulario._addEvento = function (oElemento, sEvento, oFuncion)
{
	if (oElemento.addEventListener)	oElemento.addEventListener (sEvento, oFuncion, false);
	else if (oElemento.attachEvent) oElemento.attachEvent ('on' + sEvento, oFuncion);
}


Formulario.isInt = function (oItem)
{
	var Mask   = /^-?[0-9]+$/;

	return Mask.test (oItem.value.Trim ());
}


Formulario.MascaraEmail = function (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;
	Formulario.MsgIncorrecto (sMsg);
	Formulario.setFocus (oItem);
	return false;
}


Formulario.MascaraFecha = function (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;
			}
		} 
	}
	Formulario.MsgIncorrecto (sMsg);
	Formulario.setFocus (oItem);
	return false;
}


Formulario.MascaraFloat = function (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;
	}
	Formulario.MsgIncorrecto (sMsg);
	Formulario.setFocus (oItem);
	return false;
}


Formulario.MascaraInt = function (oItem, sMsg)
{
	if (Formulario.isInt (oItem)) return true;
	Formulario.MsgIncorrecto (sMsg);
	Formulario.setFocus (oItem);
	return false;
}


Formulario.MsgIncorrecto = function (sMsg)
{
	if (Formulario.Lng == 2) alert ('Incorrect value: ' + sMsg);
	else if (Formulario.Lng == 3) alert ('Valeur incorrecte: ' + sMsg);
	else alert ('Valor incorrecto: ' + sMsg);
}


Formulario.MsgVacio = function (sMsg)
{
	if (Formulario.Lng == 2) alert ('You must indicate the following information: ' + sMsg);
	else if (Formulario.Lng == 3) alert ('Vous devez indiquer les données suivantes: ' + sMsg);
	else alert ('Tiene que indicar el siguiente dato: ' + sMsg);
}


Formulario.Requerido = function (oItem, iReq, sMsg, oForm)
{
	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 ())
		{	Formulario.MsgVacio (sMsg);
			Formulario.setFocus (oItem);
			return false;
		}
		return true;
	} else if (oItem.type == 'select-one')
	{	if (oItem.value.Vacio ())
		{	Formulario.MsgVacio (sMsg);
			Formulario.setFocus (oItem);
			return false;
		}
	} else if (oItem.type == 'radio')
	{	for (var i = oForm [oItem.name].length - 1; i >= 0 && ! Check; i--) 
			Check = oForm [oItem.name][i].checked;
		if (! Check)
		{	Formulario.MsgVacio (sMsg);
			Formulario.setFocus (oItem);
			return false;
		}
	}
	return true;
}


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


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

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

// requerido			
			Aux = item.getAttribute ('req');
			if (Aux != null)
			{	Req = Aux == '' ? 1 : Aux.asInteger ();
				if (Req && ! Formulario.Requerido (item, Req, Msg, oForm)) return false;
			}
// min
			Aux = item.getAttribute ('min');
			if (Aux && Aux.asInteger () && item.value.length < Aux.asInteger () && (item.type == 'text' || item.type == 'password'))
			{	alert ('El dato ' + Msg + ' tiene que tener un mínimo de ' + Aux + ' caracteres.');
				Formulario.setFocus (item);
				return false;
			}
// mask
			Aux = item.getAttribute ('mask');
			if (Aux && ! item.value.Vacio () && item.type == 'text' &&! Formulario.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 ()))
						{	Formulario.MsgIncorrecto (sMsg);
							Formulario.setFocus (item);
							return false;
						}
						break;
					case 'INT':
						if (isNaN (item.value.asInteger ()))
						{	Formulario.MsgIncorrecto (sMsg);
							Formulario.setFocus (item);
							return false;
						}
						break;
					default:
						alert ('El tipo ' + Tipo + ' indicado en ' + Msg + ' es incorrecto.');
						return false;
				}
			}
		}
	}
	return true;
}


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


function Formulario ()
{
}


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

Formulario.Init = function (oFormulario)
{
	var l      = oFormulario.length;
	var Pos    = 1;
	var Controles = Array ();
		
	for (var i = 0; i < l; i++)
	{ var oControl = oFormulario [i];
		if (oControl.type == 'text' || oControl.type == 'password' || oControl.type == 'checkbox')
		{	Controles.push (i);
			oControl.setAttribute ('Pos', Pos++);
			this._addEvento (oControl, 'keydown', function (event) { Formulario._KeyDownText (event, oFormulario); });
		} else if (oControl.type == 'button')
		{	Controles.push (i);
			oControl.setAttribute ('Pos', Pos++);
			this._addEvento (oControl, 'keydown', function (event) { Formulario._KeyDownButton (event, oFormulario); });
		}
	}
	
	var oControl = document.createElement ('input');
	oControl.type  = 'hidden';
	oControl.name  = 'aControles';
	oControl.value = Controles.toString ();
	oFormulario.appendChild (oControl);

	Formulario.setFocus (oFormulario [Controles [0]]);
}


Formulario._KeyDownButton = function (oEvent, oFormulario)
{
	if (! oEvent) oEvent = window.event;
	if (oEvent.keyCode == 13) 
	{	var Aux = oEvent.srcElement ? oEvent.srcElement : oEvent.currentTarget;
		Aux.blur ();
		Aux.onclick ();
	}	else if (oEvent.keyCode == 38) Formulario.SigControl (oFormulario, oEvent, -1, false);
	else if (oEvent.keyCode == 40) Formulario.SigControl (oFormulario, oEvent, +1, false);
}


Formulario._KeyDownText = function (oEvent, oFormulario)
{
	if (! oEvent) oEvent = window.event;
	if (oEvent.keyCode == 13) Formulario.SigControl (oFormulario, oEvent, +1, true);
	else if (oEvent.keyCode == 38) Formulario.SigControl (oFormulario, oEvent, -1, false);
	else if (oEvent.keyCode == 40) Formulario.SigControl (oFormulario, oEvent, +1, false);
}


Formulario._PreventDefault = function (oEvent)
{
	if (window.event) oEvent.returnValue = false;
	else oEvent.preventDefault ();
}


Formulario.SigControl = function (oFormulario, oEvent, iAvance, bPreventDefault)
{
	var oControl   = oEvent.srcElement ? oEvent.srcElement : oEvent.currentTarget;

	var iPos       = $A (oControl, 'Pos').asInteger ();
	var aControles = oFormulario.aControles ? oFormulario.aControles.value.split (',') : oFormulario [oFormulario.length -1].value.split (',');

	iPos += iAvance;		
	if (iPos <= aControles.length && iPos > 0) Formulario.setFocus (oFormulario [aControles [iPos - 1]]);
	if (bPreventDefault) Formulario._PreventDefault (oEvent);
}




