$(document).ready(function(){

	/*ALERT DIALOG*/
	$("#alertsDialog").dialog({
		bgiframe: true,
		autoOpen: false,
		width: 420,
		heigh: 173,
		modal: true,
		draggable: false,
		resizable: false,
		position: 'center',
		zIndex: -1000,
		autosize: true,
		closeOnEscape: false
	});
	/*END ALERT DIALOG*/

	$("#btnRegistrarDatos").bind("click",function(){
		location.href=document_root+"formulario";
	});


	$("#alerts").empty();
	if($("#alertaenvio").val()=="1"){
		$("#alerts").append('<li id="alertaenv"><label>Sus datos se enviaron exitosamente!.</label></li>');
		$("#alertsDialog").dialog('open');
		$("#alerts").css("display","block");
	}
	else
	if($("#alertaenvio").val()=="2")	{
		$("#alerts").append('<li id="alertaenv"><label>Ocurrio un error al enviar sus datos por favor intente nuevamente.</label></li>');
		$("#alertsDialog").dialog('open');
		$("#alerts").css("display","block");	
	}

	$("#btnEnviar").bind("click",function(){

		$("#alerts").empty();
		$("#indicadoremp").val("0");
		$(".opempresas").each(function(){

			if($(this).is(':checked')){
				$("#indicadoremp").val("1");
			}
			
		});
		


		if( $("#aplicantesFrm").valid() && $("#indicadoremp").val()=="1"){
			$("#indicador").val("1");
			$("#aplicantesFrm").submit();
		}
		else
		if($("#indicadoremp").val()=="1"){
			$("#alertsDialog").dialog('open');
		}
		if($("#indicadoremp").val()=="0"){
			$("#alerts").append('<li id="faltaempresa"><label>Debe seleccionar al menos una empresa por favor.</label></li>');
			$("#alertsDialog").dialog('open');
			$("#alerts").css("display","block");
		}

		

	})



	jQuery.validator.addMethod(
		"celular",
		function(value, element) {
			if (element.value.substring(0,2) != "07" && element.value.substring(0,2) != "08" && element.value.substring(0,2) != "09")
			{
				return false;
			}
			else return true;
		},
		"Debe empezar por 07,08 o 09"
		);

	$("#aplicantesFrm").validate({
		errorLabelContainer: "#alerts",
		wrapper: "li",
		onfocusout: false,
		onkeyup: false,
		rules:{
			txtNombrefrm:{
				required: true,
				textOnly: true
			},
			txtApellidofrm:{
				required: true,
				textOnly: true
			},
			txtDireccionfrm:{
				required: true
			},
			txtTelefonofrm:{
				required: true,
				digits: true
			},
			txtCelularfrm:{
				required: true,
				digits: true,
				rangelength: [9, 9],
				celular: true
			},
			txtEmailfrm:{
				required: true,
				email: true
			},
			empresa:{
				required: true
			}

		},
		messages:{
			txtNombrefrm:{
				textOnly: "S&oacute;lo se aceptan caracteres en el campo 'Nombre'."
			},
			txtApellidofrm:{
				textOnly: "S&oacute;lo se aceptan caracteres en el campo 'Apellido'."
			},
			txtTelefonofrm:{
				digits: "S&oacute;lo se aceptan d&iacute;gitos en el campo 'Tel&eacute;fono'."
			},
			txtCelularfrm:{
				digits:"S&oacute;lo se aceptan d&iacute;gitos en el campo 'Celular'.",
				rangelength:"Se deben ingresar 9 d&iacute;gitos en el campo 'Celular'.",
				celular: "El campo 'Celular' debe comenzar con 07, 08 o 09."
			},
			txtEmailfrm:{
				email: "Por favor ingrese un 'e-mail' v&aacute;lido."
			},
			empresa:{
				required: "Debe seleccionar almenos una empresa."
			}

		}

	});

});
