$(document).ready(function() {
   $("#selectPais").change(function (){change_selectPais()});
   $("#selectRegion").change(function (){change_selectRegion()});
   $("#selectProvincia").change(function (){change_selectProvincia()});
   
   $("#aFinalizarRegistro").click(function(){ click_aFinalizarRegistro()});
   /*$("#inputFechaNacimiento").datepicker({
	   changeMonth: true,
	   changeYear: true,
	   yearRange: '1910:',
	   buttonImage: 'img/iconos/calendario.png',
	   showButtonPanel: true,
	   showOn: "both",
	   buttonImageOnly: true
	});*/

  //$("#inputFechaNacimiento").datepicker('setDate', '01/01/1980');
  //$("#inputFechaNacimiento").val('');

   
});

function change_selectPais(){
	$.ajax({
  		 	url: "eventos/registro.php?evento=change_selectPais&pais="+$("#selectPais").val(),
  			context: document.body,
  			success: function(html){
				if(html!=""){
					$("#selectRegion").html(html);
					$("#selectRegion").attr('disabled',false);
				}else{
					$("#selectProvincia").val("");
					$("#selectComuna").val("");
					$("#selectRegion").val("");
					$("#selectProvincia").attr('disabled',true);
					$("#selectComuna").attr('disabled',true);
					$("#selectRegion").attr('disabled',true);
				}
    			//alert(html);
			}
		});
}

function change_selectRegion(){
	$.ajax({
  		 	url: "eventos/registro.php?evento=change_selectRegion&region="+$("#selectRegion").val(),
  			context: document.body,
  			success: function(html){
				if(html!=""){
					$("#selectProvincia").html(html);
					$("#selectProvincia").attr('disabled',false);
				}else{
					$("#selectProvincia").val("");
					$("#selectComuna").val("");
					$("#selectProvincia").attr('disabled',true);
					$("#selectComuna").attr('disabled',true);
					
				}
    			//alert(html);
			}
		});
}

function change_selectProvincia(){
	$.ajax({
  		 	url: "eventos/registro.php?evento=change_selectProvincia&provincia="+$("#selectProvincia").val(),
  			context: document.body,
  			success: function(html){
				if(html!=""){
					$("#selectComuna").html(html);
					$("#selectComuna").attr('disabled',false);
				}else{
					$("#selectComuna").val("");
					$("#selectComuna").attr('disabled',true);
				}
    			//alert(html);
			}
		});
}

function change_selectRegionCarro(){
	$.ajax({
  		 	url: "eventos/registro.php?evento=change_selectRegionCarro&region="+$("#selectRegionCarro").val(),
  			context: document.body,
			dataType:'json',			
  			success: function(json){
				if(json.html!=""){
					$("#selectProvinciaCarro").html(Base64.decode(json.html));
					$("#selectProvinciaCarro").change();
    				$("#valor_flete").html("$"+json.valor);
				}else{
					$("#selectProvinciaCarro").val("");
					$("#selectComunaCarro").val("");
					
				}
    			//alert(html);
			}
		});
}

function change_selectProvinciaCarro(){
	$.ajax({
  		 	url: "eventos/registro.php?evento=change_selectProvincia&provincia="+$("#selectProvinciaCarro").val(),
  			context: document.body,
  			success: function(html){
				if(html!=""){
					$("#selectComunaCarro").html(html);
				}else{
					$("#selectComunaCarro").val("");
				}
    			//alert(html);
			}
		});
}

function click_aFinalizarRegistro(){
	if($("#formRegistro").valid()){
		$.ajax({
				url: "eventos/registro.php?evento=click_aFinalizarRegistro",
				dataType:'json',
				type:'post',
				data:$("#formRegistro").serialize(),
				context: document.body,
				success: function(json){
					$("#resetformRegistro").click();
					if(json.resultado == 'S'){
						$("#modal").hide();
						$("#modalLoginBien").show();
						$("body").scrollTo( 0, 800, {queue:true} );
					}else{
						$("#modal").hide();
						$("#alerta").html("<strong>Ocurrio un error en el registro.</strong><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;"+json.detalle);
						$("#alerta").dialog('option','title','Error en el registro');
						$("#alerta").dialog('option','buttons', { "Ok": function() { $("#alerta").dialog('option','title','');$("#alerta").dialog('option','buttons',{});$(this).dialog("close"); } } );
						$("#alerta").dialog('open');
						//$("#alerta").dialog('option','title','');
					}
					
				}
			});	
	}
}


function click_registrarse(){
		 $.ajax({
  		 	url: "registro.php",
  			context: document.body,
  			success: function(html){
    			$("#rightcolumn2").html(html);
			}
		});
}
