function validaciones()
{
	$(".pagina_web").keyup(function(){
		var correo = /^http:\/\/+.(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,5})+$/;
		
		if (correo.test($(this).attr("value")))
		{
			$(this).removeClass("ui-state-error");
			/*$(this).css('background-color','white');
			$(this).css('border-color','#cccccc');*/
			$(".enviar").removeAttr('disabled');
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/si.gif");
			$("#flag_" + $(this).attr("id")).attr("rel","ok");
		}
		else
		{
			$(this).addClass("ui-state-error");
			/*$(this).css('background-color','#f3a3a3');
			$(this).css('border-color','#a91c1c');*/
			$(".enviar").attr('disabled', 'disabled');
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/close.png");
			$("#flag_" + $(this).attr("id")).attr("rel","error");
		}
	});
	
	$(".correo").keyup(function(){
		var correo = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,5})+$/;
		
		if (correo.test($(this).attr("value")))
		{
			$(this).removeClass("ui-state-error");
			/*$(this).css('background-color','white');
			$(this).css('border-color','#cccccc');*/
			$(".enviar").removeAttr('disabled');
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/si.gif");
			$("#flag_" + $(this).attr("id")).attr("rel","ok");
		}
		else
		{
			$(this).addClass("ui-state-error");
			/*$(this).css('background-color','#f3a3a3');
			$(this).css('border-color','#a91c1c');*/
			$(".enviar").attr('disabled', 'disabled');
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/close.png");
			$("#flag_" + $(this).attr("id")).attr("rel","error");
		}
	});
	
	$(".letras").keyup(function(){
		if ($(this).attr("rel") == "nombres" && ($(this).attr("value").length ==0 || $(this).attr("value").length < 3))
		{
			$(this).addClass("ui-state-error");
			$(".enviar").attr('disabled', 'disabled');
			
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/close.png");
			$("#flag_" + $(this).attr("id")).attr("rel","error");
		}
		else if ($(this).attr("rel") == "apellidos" && ($(this).attr("value").length ==0 || $(this).attr("value").length < 3))
		{
			$(this).addClass("ui-state-error");
			$(".enviar").attr('disabled', 'disabled');
			
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/close.png");
			$("#flag_" + $(this).attr("id")).attr("rel","error");
		}
		else
		{
			$(this).removeClass("ui-state-error");
			//$(this).addClass("ui-state-default");
			$(".enviar").removeAttr('disabled');
			
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/si.gif");
			$("#flag_" + $(this).attr("id")).attr("rel","ok");
		}
	}).alpha({ichars: "!@#$%^&*()+=[]\\\';,/{}|\":<>¿?~`.-"});
	
	$(".mayusculas").keyup(function()
	{
		$(this).attr("value", $(this).attr("value").toUpperCase());
	});
	
	$(".minusculas").keyup(function()
	{
		$(this).attr("value", $(this).attr("value").toLowerCase());
	});
	
	$(".alfanumerico").keyup(function(){
		if ($(this).attr("rel") == "cedula" && ($(this).attr("value").length < 7 || $(this).attr("value").length > 9))
		{
			$(this).addClass("ui-state-error");
			$(".enviar").attr('disabled', 'disabled');
			
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/close.png");
			$("#flag_" + $(this).attr("id")).attr("rel","error");
		}
		else
		{
			$(this).removeClass("ui-state-error");
			$(".enviar").removeAttr('disabled');
			
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/si.gif");
			$("#flag_" + $(this).attr("id")).attr("rel","ok");
		}
	}).alphanumeric({allow:"., "});
	
	$(".numeros").keyup(function(){
		if (($(this).attr("rel") == "cedula" || $(this).attr("rel") == "nit") && $(this).attr("value").length > 0 && ($(this).attr("value").length < 7 || $(this).attr("value").length > 9))
		{
			$(this).addClass("ui-state-error");
			$(".enviar").attr('disabled', 'disabled');
			
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/close.png");
			$("#flag_" + $(this).attr("id")).attr("rel","error");
		}
		else if ($(this).attr("rel") == "telefono" && $(this).attr("value") != "" && $(this).attr("value").length != 7)
		{
			$(this).addClass("ui-state-error");
			$(".enviar").attr('disabled', 'disabled');
			
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/close.png");
			$("#flag_" + $(this).attr("id")).attr("rel","error");
		}
		else if ($(this).attr("rel") == "celular" && $(this).attr("value") != "" && ($(this).attr("value").length < 10 || $(this).attr("value").length > 12))
		{
			$(this).addClass("ui-state-error");
			$(".enviar").attr('disabled', 'disabled');
			
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/close.png");
			$("#flag_" + $(this).attr("id")).attr("rel","error");
		}
		else if ($(this).attr("rel") == "distancia" && ($(this).attr("value") == 0 || $(this).attr("value").length == 0))
		{
			$(this).addClass("ui-state-error");
			$(".enviar").attr('disabled', 'disabled');
		}
		else
		{
			$(this).removeClass("ui-state-error");
			$(".enviar").removeAttr('disabled');
			
			$("#flag_" + $(this).attr("id")).attr("src","images/16x16/si.gif");
			$("#flag_" + $(this).attr("id")).attr("rel","ok");
		}
	}).numeric({ichars: "ñÑ ¡!@#$%^&*()+=[]\\\';,/{}|\":<>¿?~`.-°¬"});
	
	$(".numeros-letras-derecha").keyup(function(){
		if ((($(this).attr("rel") == "no_via" || $(this).attr("rel") == "interseccion") && ($(this).attr("value") == 0 || $(this).attr("value").length == 0)) || !numeros_letras_derecha($(this).attr("value")))
		{
			$(this).addClass("ui-state-error");
			$(".enviar").attr('disabled', 'disabled');
		}
		else
		{
			$(this).removeClass("ui-state-error");
			$(".enviar").removeAttr('disabled');
		}
	}).alphanumeric({ichars: "ñÑ ¡!@#$%^&*()+=[]\\\';,/{}|\":<>¿?~`.-°¬"});
	
	$("#direccion").change(function()
	{
		$("#texto_direccion").html($(this).attr("value"));
	});
	
	$(".clave, .re-clave").keyup(function(){
		$("#no_" + $(this).attr("name")).html("" + $(this).attr("value").length);
		
		if ($(this).attr("name") == "clave")
		{
			if ($(this).attr("value") != $("#re-clave").attr("value") || $(this).attr("value").length < 6 || !ContieneNumerosyLetras($(this).attr("value")))
			{
				$(this).addClass("ui-state-error");
				$(".enviar").attr('disabled', 'disabled');
			}
			else
			{
				$(this).removeClass("ui-state-error");
				$("#re-clave").removeClass("ui-state-error");
				$(".enviar").removeAttr('disabled');
			}
		}
		else
		{
			if ($(this).attr("value") != $("#clave").attr("value") || $(this).attr("value").length < 6 || !ContieneNumerosyLetras($(this).attr("value")))
			{
				$(this).addClass("ui-state-error");
				$(".enviar").attr('disabled', 'disabled');
			}
			else
			{
				$(this).removeClass("ui-state-error");
				$("#clave").removeClass("ui-state-error");
				$(".enviar").removeAttr('disabled');
			}
		}
	});
}

function numeros_letras_derecha(valor)
{
	var numeros = 0;
	var letras = 0;
	
	var array = valor.split("");
	
	for (var i=0; i<array.length; i++)
	{
		//Numeros
		if(/^([0-9])$/.test(array[i]))
		{
			numeros++;
		}
		
		//Letras
		if(/^([a-zA-Z])$/.test(array[i]))
		{
			letras++;
		}
	}
	
	if (numeros==0 && letras !=0)
	{
		return false;
	}
	else
	{
		if(array.length > 0 && /^([0-9])$/.test(array[0]))
		{
			var cont = 0;
			for (var i=0; i<array.length; i++)
			{
				if(/^([0-9])$/.test(array[i]))
				{
					cont++;
				}
				else
				{
					break;
				}
			}
			
			if (cont != numeros)
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	return true;
}

function ContieneNumerosyLetras(string)
{
	var n = 0, l = 0;
	
	var array = string.split("");
	
	for (var i=0; i<array.length; i++)
	{
		//Numeros
		if(/^([0-9])$/.test(array[i]))
		{
			n++;
		}
		
		//Letras
		if(/^([a-zA-Z])$/.test(array[i]))
		{
			l++;
		}
	}
	
	if (n > 0 && l > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function validar_direccion()
{
	if ($("#no_via").attr("value") != "" && $("#no_via").attr("value") != "0" && $("#interseccion").attr("value") != "" && $("#interseccion").attr("value") != "0" && $("#distancia").attr("value") != "" && $("#distancia").attr("value") != "0" && numeros_letras_derecha($("#no_via").attr("value")) && numeros_letras_derecha($("#interseccion").attr("value")))
	{
		$("#direccion").attr("value", ($("#tipo_via").attr("value") + " " + $("#no_via").attr("value") + " " + $("#interseccion").attr("value") + " " + $("#distancia").attr("value") + (($("#tipo_complemento").attr("value").length > 0 && $("#texto_complemento").attr("value").length > 0) ? " " + $("#tipo_complemento").attr("value") + " " + $("#texto_complemento").attr("value") : "")).toUpperCase());
		
		$("#texto_direccion").html($("#direccion").attr("value"));
		
		return true;
	}
	else
	{
		if (TextoOpcionCombo('paises_id').toLowerCase() == 'colombia')
		{
			$("#direccion").attr("value", "");
			$("#texto_direccion").html($("#direccion").attr("value"));
			return false;
		}
		
		if ($("#direccion").attr("value") != "")
		{
			$("#texto_direccion").html($("#direccion").attr("value"));
			return true;
		}
	}
}

$(document).ready(function(){
	validaciones();
	$(this).ajaxStop(function(){validaciones();});
});

