﻿$(document).ready(function() {

	$('a.nudge').hover(function() { //mouse in
			$(this).animate({ paddingLeft: '35px' }, 400);
		}, function() { //mouse out
			$(this).animate({ paddingLeft: '15px' }, 400);
	});
	
	/*$.ajax({
			
			url:"http://www.google.com",
			error:function(obj,error){
				alert("no hay internet");
			},
			success: function(datos){
				alert("si hay internet");
			},
		
	});*/
	
	load_google_maps();
	
	$("#inicio").click(function(){
		load_google_maps();
	});
	
	$("#adherentes").click(function(){
		$.post('diseno/adherentes.php',{},function(datos){
				$("#resultado").html(datos);
				//window.open($(this),"mywindow","status=1,toolbar=1");
		});
	});
	
	//ABQIAAAAQbPb-ZXP-u4WFIl9huag9RT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSzuYpl3sISuO3toEKBXgMpxqFsBw

		
	$("#inscribete").click(function(){
		$.ajaxSetup({async:false});
		$("#resultado").liberar();
		$.post('diseno/inscribete.php',{},function(datos){
				$("#resultado").html(datos);
				$("#txt_fecha_nac").datepicker({
					changeMonth: true,
					changeYear: true,
					showOn: 'button',
					buttonImage: 'images/calendar.gif',
					buttonImageOnly: true
				});
				$("#txt_documento").numeric();
				
				//$("#txt_telefono").mask("999-9999");	
				
				$("#txt_celular").mask("999-999-999");
				
				/*$("#txt_telefono").blur(function(){
					valida_telefono();
				});*/
				
				llena_cbo("proceso/pais_result.php","td_pais","cbo_pais","150","","11");
				$("#cbo_pais").val(1).change(function(){
					llena_departamento();
					if($(this).val() != 1){
						llena_provincia();
						llena_distrito();
					}
				});
				
				llena_cbo("proceso/departamento_result.php","td_departamento","cbo_departamento","150","","11");
				llena_cbo("proceso/tipodoc_result.php","td_tipo_doc","cbo_tipo_doc","100","","11");
				
				$("#cbo_tipo_doc").change(function(){
					tipo_documento($(this).val());
				});
				
				$("#cbo_departamento").val(15)
				llena_provincia();
				$("#cbo_tipo_doc").val(1);
				$("#cbo_departamento").change(function(){
					llena_provincia();
				});

				$("#grabar_inscripcion").click(function(){
					Grabar_Inscripcion();
				});
		})
		$.ajaxSetup({async:true});
	});
	
	$("#resoluciones").click(function(){
		$("#resultado").liberar();
		$.post('diseno/pdf.php',{},function(datos){
				$("#resultado").html(datos);
		});
	});
		
	$("#imagenes").click(function(){
		$("#resultado").liberar();
		$.post('diseno/imagenes.php',{},function(datos){
			$("#resultado").html(datos);
			$('#tbl_imagenes tr td img').click(function(ev) {
					var $item = $(this).attr('id');
					var $target = $(ev.target);
					var enlace = $(this).attr('src');
						viewLargerImage(enlace,$item);
			});
			
			
			
		})
	});
});


function valida_telefono() {
	//$.ajaxSetup({async:false});
//alert("aqui");
/*
 if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) && email!=''){
 	
}
 */
	//\d{3}-\d{2}-\d{7}-\d
	//Encajaría con 123-12-1234567-1, por ejemplo

	var telefono = $("#txt_telefono").val();
	//if(!/\+\(\d{3}\)\d{3}-\d{3}-\d{3}-\d{4}/.test(telefono)){
	//if(!/^(\+\(\d{3}\))?(^\d{3}-\d{3})?(-)?^(\d{3}-\d{4}){1}/.test(telefono)){	
	if(!/^(\+\(\d{3}\)\d{3}-\d{3}-)?(\d{3}-\d{4})?(\d{7})?$/.test(telefono)){	
		
		alert("Formato de numero telefonico no valido \n Formato 1 : +(123)123-123-123-1234 \n Formato 2 : 123-1234 \n Formato 3 : 1234567");
		$("#txt_telefono").focus();
		return false;
		//document.getElementById("txt_telefono").focus();
	}
	//$.ajaxSetup({async:true});
}


function tipo_documento(valor){
	switch(valor){
		case "0":
			$("#txt_documento").val('').attr("disabled","disabled");
			break;
		case "1":
			$("#txt_documento").val('').attr("maxlength","8").attr("disabled","");
			break;
		case "2":
			$("#txt_documento").val('').attr("maxlength","11").attr("disabled","");
			break;
		case "3":
			$("#txt_documento").val('').attr("maxlength","14").attr("disabled","");
			break;
	}
}

jQuery.fn.liberar = function(){
	var id_obj = this.attr('id');
	document.getElementById(id_obj).innerHTML="";
	document.getElementById(id_obj).innerHTML = "<p align='center'><img src='images/loading.gif'><br/>Cargando...</p>";
}

function Grabar_Inscripcion(){
	$.ajaxSetup({async:false});
	var control = '';
	$("input, select").not("#txt_email, #txt_telefono, #txt_celular, #txt_fecha_nac, #cbo_sexo, #txt_domicilio").each(function(){
		if($(this).val() == 0 || $(this).val == ''){
			control = $(this).attr("id");
			return false;
		}
	});
	
	if(control != ''){
		arreglo_control = control.split("_");
		alert("Falta Ingresar "+arreglo_control[1]);
		$("#"+control).focus();
		return false;
	}
	
	var telefono = $("#txt_telefono").val();
	if(!/^(\+\(\d{3}\)\d{3}-\d{3}-)?(\d{3}-\d{4})?(\d{7})?$/.test(telefono)){	
		
		alert("Formato de numero telefonico no valido \n Elegir un formato : \n Formato 1 : +(999)999-999-999-9999 \n Formato 2 : 999-9999 \n Formato 3 : 9999999");
		$("#txt_telefono").focus();
		return false;
	}
	
	if($("#txt_documento").val().length != $("#txt_documento").attr("maxlength")){
		alert("El numero de digitos no concuerda con el tipo de Documento");
		$("#txt_documento").focus();
		return false;
	}
	
	

	var email = $("#txt_email").val();
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) && email!=''){
				alert('El E-mail '+email+' es incorrecto');
				$("#txt_email").focus();
				return false;
	}
	
	if(confirm("Seguro de enviar inscripcion...") == false){
		return false;
	}
	var url = "proceso/grabar_adherente.php";
	//var fecha = $("#txt_fecha_nac").val() == '' ? null : $("#txt_fecha_nac").val();
	$.post(url,
				{
					nombre		: $("#txt_nombre").val(),
					apellido1	: $("#txt_apellido1").val(),
					apellido2	: $("#txt_apellido2").val(),
					pais		: $("#cbo_pais").val(),
					departamento	: $("#cbo_departamento").val(),
					provincia	: $("#cbo_provincia").val(),
					distrito	: $("#cbo_distrito").val(),
					domicilio	: $("#txt_domicilio").val(),
					telefono	: $("#txt_telefono").val(),
					celular		: $("#txt_celular").val(),
					email		: $("#txt_email").val(),
					fecha_nac	: $("#txt_fecha_nac").val(),
					documento	: $("#txt_documento").val(),
					tipo_doc	: $("#cbo_tipo_doc").val(),
					sexo		: $("#cbo_sexo").val()
					
				},function(datos){
					//alert(datos);
					var arreglo = datos.split("|");
					alert(arreglo[1]);
					
					//$("input").val('');
					//$("select").val(0);
	});
	$.ajaxSetup({async:true});
	
}

function viewLargerImage($link,$item) {
	//alert($item);
	$.ajaxSetup({async:false});
	var foto = 'images/imagenes/'+$item.toUpperCase();
	//alert(foto);
	//$("div_img").prepend('body');
	//$("body").prepend("<div id='div_img'></div>");
	 //$("body").prepend("<div id='div_recuperainf' class='flora' title='Búsqueda de Orden de Compra' style='display:none;'></div>");
	 
	var img = '<img alt="Imagen" style="padding: 8px;" width="650" height="430" src="'+foto+'" />';
	//alert(img);
	$("body").prepend("<div id='div_img' style='display:none;'>"+img+"</div>");
	
			//.attr('src',$link).appendTo('body');
		setTimeout(function() {
			$("#div_img").show().dialog({
					title: 'Imagen',
					width: 700,
					height:500,
					modal: true
				});
		}, 1);
	$.ajaxSetup({async:true});
}


function llena_cbo(url, td_objeto, cbo_objeto, longpx, ctrl, sizetxt){
	$.post(url, 
		   {	//ejercicio:var_sistema.periodo
			},
				function(datos){
				   var valor=datos.split("|");				   					
					if ( $("#"+td_objeto).length>0 )
					{	
						$("#"+td_objeto).html("<select id="+cbo_objeto+" name="+cbo_objeto+" style='width:"+longpx+"px; font-family:Verdana, Geneva, sans-serif; font-size:"+sizetxt+"px;'></select>");
						eval(valor[1]);
						$("#"+cbo_objeto).addOption(miRespta , false);
					}
				}
	);
	
}

function llena_cbo_esp(url, ejercicio, cencos ,td_objeto, cbo_objeto, longpx, ctrl, sizetxt){
	$.post(url, 
		   {	ejercicio:ejercicio,
				cencos:cencos
			},
				function(datos){				
				   var valor=datos.split("|");				   					
					if ( $("#"+td_objeto).length>0 )					
					{	
						$("#"+td_objeto).html("<select id="+cbo_objeto+" name="+cbo_objeto+" class='txtcolor s_big' style='width:"+longpx+"px; font-family:Verdana, Geneva, sans-serif; font-size:"+sizetxt+"px;'></select>")
						eval(valor[1]);
						$("#"+cbo_objeto).addOption(miRespta , false);
					}
					eval(ctrl);
			}
	);
}

function llena_departamento(){
	$.ajaxSetup({async:false});
	llena_cbo_esp("proceso/departamento_result.php","",$("#cbo_pais").val(),"td_departamento","cbo_departamento","150","","11");
	
	$("#cbo_departamento").change(function(){
		llena_provincia();
	});
	$.ajaxSetup({async:true});
}

function llena_provincia(){
	$.ajaxSetup({async:false});
	llena_cbo_esp("proceso/provincia_result.php","",$("#cbo_departamento").val(),"td_provincia","cbo_provincia","150","","11");
	
		$("#cbo_provincia").change(function(){
			llena_distrito();
		});
	$.ajaxSetup({async:true});
}

function llena_distrito(){
	llena_cbo_esp("proceso/distrito_result.php","",$("#cbo_provincia").val(),"td_distrito","cbo_distrito","150","","11");
}

function salida_reporte(){
	var url = "proceso/adherentes.php";
	idalm 			= $("#txt_idegreso").val();	
	und_organica 	= cbo_retorna_texto("cbo_und_organica");//$("#cbo_und_organica").val();
	destinatario	= $("#txt_destinatario").val();		
	nrocompd 		= $("#txt_nrocompd_result").val();		
	fecha_bien 		= $("#txt_fecha_bien").val();		
	observacion 	= $("#txt_observacion").val();		
	
	window.open (url+"?idalm="+idalm+"&und_organica="+und_organica+"&destinatario="+destinatario+"&nrocompd="+nrocompd+"&fecha_bien="+fecha_bien+"&observacion="+observacion,"mywindow","status=1,toolbar=1");
}

		var theInt = null;
		var $crosslink, $navthumb;
		var curclicked = 0;
		
		theInterval = function(cur){
			clearInterval(theInt);
			
			if( typeof cur != 'undefined' )
				curclicked = cur;
			
			$crosslink.removeClass("active-thumb");
			$navthumb.eq(curclicked).parent().addClass("active-thumb");
				$(".stripNav ul li a").eq(curclicked).trigger('click');
			
			theInt = setInterval(function(){
				$crosslink.removeClass("active-thumb");
				$navthumb.eq(curclicked).parent().addClass("active-thumb");
				$(".stripNav ul li a").eq(curclicked).trigger('click');
				curclicked++;
				if( 6 == curclicked )
					curclicked = 0;
				
			}, 2500);
		};
		
		$(function(){
			
			$("#main-photo-slider").codaSlider();
			
			$navthumb = $(".nav-thumb");
			$crosslink = $(".cross-link");
			
			$navthumb
			.click(function() {
				var $this = $(this);
				theInterval($this.parent().attr('href').slice(1) - 1);
				return false;
			});
			
			theInterval();
		});

	


