// TAREA

function task(sFormId, sAction, sConfirm)
{
	if(!sFormId)
	{
		window.location.replace(sAction);
		return false;
	}

	document.forms[0].action = sAction;

	if(sConfirm)
	{
		if(!window.confirm(sConfirm))
		{
			return false;
		}
		document.forms[0].submit();
		return true;
	}

	if(validacion(document.forms[0]))
	{
		document.forms[0].submit();
	}
}

// VALIDACION

var bValidar = true;
// Mensajes
FIELD_EMPTY = 'Este campo es obligatorio';
FIELD_INT = 'Este campo debe ser numerico (enteros)';
FIELD_FLOAT = 'Este campo debe ser numerico';
FIELD_DATE = 'Este campo debe ser una fecha (Formato dd/mm/yyyy)';
FIELD_MAIL = 'Este campo debe ser un correo electronico';
FIELD_PASS = 'Los campos de contraseņa deben ser iguales';
FIELD_PASS2 = 'Debe escribir la contraseņa anterior';
// Validacion
function validacion(oForm)
{
	if( bValidar )
	{
		var aElements = oForm.elements;
		// Recorre los campos
		for( var nElement = 0; nElement < aElements.length; nElement++ )
		{
			// Variables del Campo
			oElement = aElements[nElement];
			Value = oElement.value;
			Type = oElement.type;
			Name = oElement.name;
			// En el ID irian las declaraciones
			if(oElement.id)
			{
				aDeclarations = oElement.id.split('_'); // separador de declaraciones
				// Campo Vacio
				if( Value.replace(' ','').length == 0 )
				{
					// Campo Requerido
					if(aDeclarations[1])
					{
						// ...no puede ser vacio
						window.alert(FIELD_EMPTY);
						oElement.focus();
						return false;
					}
				}
				else
				{		
					// Tipos de Datos Especiales
					if(aDeclarations[2])
					{
						switch(aDeclarations[2])
						{
							// Campo NUMERICO Entero
							case "int":
								if(isNaN(parseInt(Value)))
								{
									window.alert(FIELD_INT);
									oElement.select();
									return false;
								}
								oElement.value = parseInt(Value);
							break;
							// Campo NUMERICO Flotante
							case "float":
								Value = Value.replace(',','.'); // Reemplaza la coma por el punto
								if(isNaN(parseFloat(Value)))
								{
									window.alert(FIELD_FLOAT);
									oElement.select();
									return false;
								}
								oElement.value = parseFloat(Value);
							break;
							// Campo FECHA
							case "date":
								oDate = new RegExp("(0[1-9]|[12][0-9]|3[01]).(0[1-9]|1[012]).([09][0-9]|[0-9]{4})");
								if(!Value.match(oDate))
								{
									window.alert(FIELD_DATE);
									oElement.select();
									return false;
								}
							break;
							// Campo MAIL
							case "mail":
								oMail = new RegExp("[A-z0-9._%-]*@[A-z0-9._%-]*[\.][A-z0-9_-]*");
								if(!Value.match(oMail))
								{
									window.alert(FIELD_MAIL);
									oElement.select();
									return false;
								}
							break;
							// Campo PASS
							case "pass":
								
								sPassRepeat = aDeclarations[0] + "__passrepeat";
								sPassRepeated = aDeclarations[0] + "__passrepeated";
								oPassRepeat = document.getElementById(sPassRepeat);
								oPassRepeated = document.getElementById(sPassRepeated);
								if( oPassRepeat.value != oPassRepeated.value )
								{
									window.alert(FIELD_PASS);
									oPassRepeat.select();
									return false;
								}
								
							break;
							case "passrepeat":
								sPass = aDeclarations[0] + "__pass";
								oPass = document.getElementById(sPass);
								
								if(oPass.value.replace(' ','').length == 0)
								{
									window.alert(FIELD_PASS2);
									oPass.select();
									return false;
								}

							break;
							}
						}
				}
			}
			// Dependiendo del Tipo
			switch(Type)
			{
				case 'select-one':
					if(Value.substr(0,3) == '___')
					{
						alert(Value.substr(3));
						return false;
					}
				break;
			}
		}
		return true;
	}
}

// Muestra la alerta si no es vacia

NO_OBSERVACIONES = "No hay observaciones";

function obs_valida(sObservacion)
{
	if( sObservacion.length <= 1 )
	{	
		sObservacion = NO_OBSERVACIONES
	}
	window.alert(sObservacion);

}

// Cerrar En

function cerrarEn(idWarning, contador)
{
	if(contador <= 0)
	{
		window.location.reload();
	}
	else
	{
		document.getElementById(idWarning).innerHTML = contador;
		contador--;
		setTimeout("cerrarEn('"+idWarning+"', "+contador+")",1000);
	}		
}

function openStats(url){
	window.open(url,"","fullscreen=1,scrollbars=1");
}

