// Ultima modificacion: JCCM 2008-11-18 11:58
$(document).ready(function()
{
	$(".requerido").each(function()
	{
		$("#" + this.id).after("<span class=\"span_requerido\" id=\"rq" + this.id + "\">*</span>");
		if (jQuery.trim($("#" + this.id).val()) != "") {
			$("#rq" + this.id).html("");
		}
	});

	$(".fecha").each(function()
	{
		if ($("#rq" + this.id).val() != "")
		{
			$("#" + this.id).after("<span class=\"span_requerido\" id=\"rq" + this.id + "\"></span>");
		}
		$("#rq" + this.id).after(" (dd/mm/aaaa)");
	});

	$(".hora").each(function()
	{
		if ($("#rq" + this.id).val() != "")
		{
			$("#" + this.id).after("<span class=\"span_requerido\" id=\"rq" + this.id + "\"></span>");
		}
		$("#rq" + this.id).after(" (00:00 - 23:59)");
	});

	$(".entero").each(function()
	{
		if ($("#rq" + this.id).val() != "")
		{
			$("#" + this.id).after("<span class=\"span_requerido\" id=\"rq" + this.id + "\"></span>");
		}
	});

	$(".decimal").each(function()
	{
		if ($("#rq" + this.id).val() != "")
		{
			$("#" + this.id).after("<span class=\"span_requerido\" id=\"rq" + this.id + "\"></span>");
		}
	});

	$(".correo").each(function()
	{
		if ($("#rq" + this.id).val() != "")
		{
			$("#" + this.id).after("<span class=\"span_requerido\" id=\"rq" + this.id + "\"></span>");
		}
	});

	// Validacion
	$("#frmDatos").submit(validar);

	$(".fecha").bind("change blur", function(e)
	{
		if (jQuery.trim($("#" + this.id).val()) == "" || fecha_valida($("#" + this.id).val()))
		{
			$("#rq" + this.id).html("");
		}
		else
		{
			$("#rq" + this.id).html("X");
		}
	});

	$(".hora").bind("change blur", function(e)
	{
		if (jQuery.trim($("#" + this.id).val()) == "" || hora_valida($("#" + this.id).val()))
		{
			$("#rq" + this.id).html("");
		}
		else
		{
			$("#rq" + this.id).html("X");
		}
	});

	$(".entero").bind("change blur", function(e)
	{
		if (jQuery.trim($("#" + this.id).val()) == "" || es_entero($("#" + this.id).val()))
		{
			$("#rq" + this.id).html("");
		}
		else
		{
			$("#rq" + this.id).html("X");
		}
	});

	$(".decimal").bind("change blur", function(e)
	{
		if (jQuery.trim($("#" + this.id).val()) == "" || es_decimal($("#" + this.id).val()))
		{
			$("#rq" + this.id).html("");
		}
		else
		{
			$("#rq" + this.id).html("X");
		}
	});

	$(".correo").bind("change blur", function(e)
	{
		if (jQuery.trim($("#" + this.id).val()) == "" || correo_valido($("#" + this.id).val()))
		{
			$("#rq" + this.id).html("");
		}
		else
		{
			$("#rq" + this.id).html("X");
		}
	});

	$(".requerido").bind("change blur", function(e)
	{ 
		if (jQuery.trim($("#" + this.id).val()) == "")
		{
			$("#rq" + this.id).html("*");
		}
		else
		{
			if ($("#rq" + this.id).html() != "X")
			{
				$("#rq" + this.id).html("");
			}
		}
	});

	

	$(".confirmacion").bind("change blur", function(e)
	{
		if (jQuery.trim($("#" + this.id).val()) != "")
		{
			if (jQuery.trim($("#" + this.id.replace("1", "")).val()) != jQuery.trim($("#" + this.id).val()))
			{
				$("#rq" + this.id).html("X");
			}
			else
			{
				$("#rq" + this.id).html("");
			}
		}
	});
});

// Funciones de validacion
function validar()
{
	var valido = true;
	var control = "";
	$(".requerido").each(function()
	{
		if (jQuery.trim($("#" + this.id).val()) == "")
		{
			if (control == "")
			{
				control = $("#" + this.id);
			}
			valido = false;
		}
	});
	$(".fecha").each(function()
	{
		if (jQuery.trim($("#" + this.id).val()) != "")
		{
			if (!fecha_valida($("#" + this.id).val()))
			{
				if (control == "")
				{
					control = $("#" + this.id);
				}
				valido = false;
			}
		}
	});

	$(".hora").each(function()
	{
		if (jQuery.trim($("#" + this.id).val()) != "")
		{
			if (!hora_valida($("#" + this.id).val()))
			{
				if (control == "")
				{
					control = $("#" + this.id);
				}
				valido = false;
			}
		}
	});

	$(".entero").each(function()
	{
		if (jQuery.trim($("#" + this.id).val()) != "")
		{
			if (!es_entero($("#" + this.id).val()))
			{
				if (control == "")
				{
					control = $("#" + this.id);
				}
				valido = false;
			}
		}
	});

	$(".decimal").each(function()
	{
		if (jQuery.trim($("#" + this.id).val()) != "")
		{
			if (!es_decimal($("#" + this.id).val()))
			{
				if (control == "")
				{
					control = $("#" + this.id);
				}
				valido = false;
			}
		}
	});

	$(".correo").each(function()
	{
		if (jQuery.trim($("#" + this.id).val()) != "")
		{
			if (!(jQuery.trim($("#" + this.id).val()) == "" || correo_valido($("#" + this.id).val())))
			{
				if (control == "")
				{
					control = $("#" + this.id);
				}
				valido = false;
			}
		}
	});


	$(".confirmacion").each(function()
	{
		if (jQuery.trim($("#" + this.id).val()) != "")
		{
			if (jQuery.trim($("#" + this.id.replace("1", "")).val()) != jQuery.trim($("#" + this.id).val()))
			{
				if (control == "")
				{
					control = $("#" + this.id);
				}
				valido = false;
			}
		}
	});

	if (!valido)
	{
		alert("Complete los datos requeridos\nmarcados con * (asterisco) a la derecha.");
		control.focus();
	}
	return valido;
}

function fecha_valida(fecha)
{
	return fecha.search(/([0-2][0-9]|30|31)\/(00|01|02|03|04|05|06|07|08|09|10|11|12)\/20[0-1][0-9]/) == 0;
}

function hora_valida(hora)
{
	return hora.search(/([0-1][0-9]|20|21|22|23):[0-5][0-9]/) == 0;
}

function es_entero(numero)
{
	return ((!isNaN(numero)) && numero.indexOf(".") == -1);
}

function es_decimal(numero)
{
		return (!isNaN(numero));
}

function correo_valido(correo)
{
	return !((correo == "") || (correo.search(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig)));
}