function cargarfecha(dia, mes, anio){
	var oDia, oMes, oAnio, objOption, valueDia, cantDias;
	var aDaysMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	//Objeto Mes
	if(document.all)
		oDia = document.all[dia];
	else
		oDia = document.getElementById(dia);
	
	//Objeto Mes
	if(document.all)
		oMes = document.all[mes];
	else
		oMes = document.getElementById(mes);
	
	//Objeto Año
	if(document.all)
		oAnio = document.all[anio];
	else
		oAnio = document.getElementById(anio);
	
	valueDia = oDia.value;
	oDia.length = null;
	if(oAnio.value%4 == 0 && oAnio%100 != 0 && oMes.value == 2){
		cantDias = 29;
	}
	else{
		cantDias = aDaysMonth[oMes.value-1];
	}
	
	for(i=0; i<cantDias; i++){
		objOption = new Option((i+1), (i+1));
		oDia[i] = objOption;
	}
	
	if(aDaysMonth[oMes.value-1] < valueDia){
		oDia[0].selected = true;
	}
	else{
		oDia[valueDia-1].selected = true;
	}
}

function isEmail(strEmail){
		regExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	
		return regExp.test(strEmail);
	}

function onlyNumbers(evt)
/* Function que no permite ingresar caracteres en campo telefono
	USO onKeyPress="try {return cuilInput(event)} catch (e){}"

 */
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	
	if (charCode != 8 && (charCode < 48 || charCode > 57))
			return false;
	return true;
}

// Devuelve el KeyCode
function checkKeycode(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;

return keycode

}

function telInput(evt)
/* Function que no permite ingresar caracteres en campo telefono
	USO onKeyPress="try {return cuilInput(event)} catch (e){}"

 */
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 45 && charCode != 41 && charCode != 40)
			return false;
	return true;
}

// VARIABLES DE TRABAJO
var URL = "http://www.10encuentros.com/";
//var URL = "http://localhost/10Encuentros/";
	
// MODULO DE LUGARES
var lugares;
var indexImagenes = 0;
var imgLugar;

function showLugar(index)
{
	var htmlText = lugares[index].nombre + "<br/><br/>" + (lugares[index].descripcion!=null && lugares[index].descripcion!=""?lugares[index].descripcion:"")+ "<br/><br/>" + (lugares[index].web!=null && lugares[index].web!=""?"<a target='_blank' href='"+lugares[index].web+"'>Pagina web</a>":"");
	
	$("#text_bg").show();
	$("#text3").html(htmlText);
	$("#imgL").hide();
	indexImagenes = 0;
	
	$("#button_fotosLugares").hide();
	
	if(lugares[index].imagenes.length>0) 
	{
		imgLugar = lugares[index].imagenes;
		
		$("#imagenLugar").show();
		$("#imagenLugar").attr("src",URL + "images/backend/"+imgLugar[indexImagenes]);
		//$("#button_fotosLugares").show();
		/*if(imgLugar.length>1){
			$("#imgR").show();
			$("#button_fotosLugares").show();
		} else {
			$("#imgR").hide();
			$("#button_fotosLugares").hide();
		}*/
		
		setInterval("showNextImgLugar()",3000);
		
	}
	else
	{
		//$("#imagenLugar").hide();
		$("#imagenLugar").attr("src","images/lugares_fotos-09.png");
		//$("#button_fotosLugares").hide();
		//$("#imgR").hide();
	}
}

function showNextImgLugar()
{
	indexImagenes++;
	$("#imagenLugar").attr("src",URL + "images/backend/"+imgLugar[indexImagenes]);
	
	//if(indexImagenes>0) $("#imgL").show();
	if(indexImagenes==(imgLugar.length-1)) indexImagenes = -1;
	
	//$("#imgR").hide(); 
}

function showPrevImgLugar()
{
	$("#imgR").show();
	indexImagenes--;
	$("#imagenLugar").attr("src",URL + "images/backend/"+imgLugar[indexImagenes]);
	
	if(indexImagenes==0) $("#imgL").hide();
}
// FIN MODULO LUGARES

var reuniones;
function showReunion(index)
{
	var htmlText = "<span style='text-shadow:#000 0px 0px 25px, #999 0px 0px 10px, #000 0px 0px 5px; font-size:36px; font-weight:bolder'>" + reuniones[index].fecha.substr(0,5) + "<br/>" + reuniones[index].fecha.substr(11,5) + "hs</span><br />Ciudad: "+ reuniones[index].ciudad+"<br />Rango mujeres: "+ reuniones[index].rango_mujeres+"<br />Rango varones: "+ reuniones[index].rango_varones+"<br />Arancel: $"+ reuniones[index].arancel +"<br /><a href='reuniones_ficha.php?id="+ reuniones[index].idReunion +"' class='others'>+ Mas informacion</a>";
	
	$("#text_bg").show();
	$("#text3").html(htmlText);
	$("#imgL").hide();
	indexImagenes = 0;
	
	if(reuniones[index].imagenes.length>0) 
	{
		imgLugar = reuniones[index].imagenes;
		
		$("#imagenLugar").show();
		$("#imagenLugar").attr("src",URL + "images/backend/"+imgLugar[indexImagenes]);
		if(imgLugar.length>1){
			$("#imgR").show();
			$("#button_fotosLugares").show();
		} else {
			$("#imgR").hide();
			$("#button_fotosLugares").hide();
		}
	}
	else
	{
		//$("#imagenLugar").hide();
		$("#imagenLugar").attr("src","images/lugares_fotos-09.png");
		$("#button_fotosLugares").hide();
		$("#imgR").hide();
	}
}

var d = new Date();
var curr_month = d.getMonth()+1;
var curr_year = d.getFullYear();

function getPrevMonth()
{
	curr_month--;
	if(curr_month==0)
	{
		curr_month = 12;
		curr_year--;
	}
	
	getCalendar();
}

function getNextMonth()
{
	curr_month++;
	if(curr_month==13)
	{
		curr_month = 1;
		curr_year++;
	}
	
	getCalendar();
}

function getCalendar()
{
	$.post(URL+"includes/ajax.php",{ action:"getCalendar", mes: curr_month, year: curr_year} ,function(data)
	{	
	  if(data!="empty") //if correct login detail
	  {
		$("#calendar").html(data);

	  }
	  else
	  {
		 // alert("Ocurrio un error al traer el calendario");
	  }
	
	 });
		
}


$(document).ready(function() {
	  
	//Check login
	$.post(URL+"includes/ajax.php",{ action:"checklogin"} ,function(data)
	{	

	  if(data!='no') //if correct login detail
	  {
		$("#login").html("<form action='"+URL+"includes/ajax.php' method='post' id='formLogin' name='formLogin'><table width='600' border='0' cellspacing='0' cellpadding='0' height='103'><tr><td width='200'>&nbsp;</td><td width='69'>&nbsp;</td><td width='192' align='left' valign='middle'>Bienvenido "+data+"</td><td style='background:url("+URL+"images/button_bg-08.png); background-repeat:no-repeat; background-position:right; padding-right:25px; text-align:right; cursor:pointer;' onclick='location.href=\""+URL+"profile_datos_1.php\"' width='140'>Mi perfil</td><td width='80' style='background:url("+URL+"images/button_bg-08.png); background-repeat:no-repeat; background-position:right; padding-right:33px; text-align:right; cursor:pointer;' onclick='document.formLogin.submit();'>Salir</td></tr><tr><td>&nbsp; </td><td>&nbsp;</td><td align='left' colspan='2'><input type='hidden' id='action' name='action' value='logout' /></td></tr></table></form>");
	  }
	  else
	  {
		$("#login").html("<form action='"+URL+"includes/ajax.php' method='post' id='formLogin' name='formLogin'><table width='600' border='0' cellspacing='0' cellpadding='0' height='103'><tr><td width='200'>&nbsp;</td><td width='69'>&nbsp;</td><td width='192' align='left' valign='bottom'>Usuario</td><td width='179' align='left' valign='bottom'>Contrase&ntilde;a</td></tr><tr><td>Dej&aacute; que te suceda<br/>Cre&aacute; una cuenta gratis </td><td>&nbsp;</td><td align='left'><input name='usuario' id='usuario' type='text' onkeydown='if(checkKeycode(event)==13){document.formLogin.submit();}'/></td><td align='left'><input name='clave' name='clave' type='password' onkeydown='if(checkKeycode(event)==13){document.formLogin.submit();}' /><input type='hidden' id='action' name='action' value='login' /></td></tr><tr><td onclick='location.href=\""+URL+"reg_form_1.php\"' style='background:url("+URL+"images/button_bg-08.png); cursor:pointer; background-repeat:no-repeat; background-position:right; padding-right:15px; text-align:right; color:#090' >Registrate</td><td>&nbsp;</td><td><a href='password.html' class='others'>¿Olvidaste tu contraseña?</a></td><td id='enviarBtn' height='42' style='background:url("+URL+"images/button_bg-08.png); background-repeat:no-repeat; background-position:right; padding-right:30px; text-align:right; cursor:pointer;' onclick='document.formLogin.submit();'>Entrar</td></tr></table></form>");
	  }
	});
	
	// Construccion menu lugares
	var currentUrl = document.URL;
	
	if(currentUrl.indexOf("lugares.html")>-1)
	{
		$.post(URL+"includes/ajax.php",{ action:"getLugares"} ,function(data)
	{	
	  if(data!="empty") //if correct login detail
	  {
		var htmlMenu = "<table width=\"185\" border=\"0\" cellspacing=\"0\" cellpadding=\"18\">";
		var obj = jQuery.parseJSON(data);
		lugares = obj;
		for(var i=0;i<obj.length;i++)
		{
			htmlMenu += "<tr><td height='18' align='right' background='images/lugares_menu-14.png' valign='middle' ><a id='"+i+"' onclick='showLugar(this.id);' href='#' class='text'>"+obj[i].nombre+"</a></td></tr>";	
		}
		
		htmlMenu += "</table>";
	  }
	  else
	  {
		  
	  }
	  
	  $("#icon").html(htmlMenu);
	  
	  });
	}
	
	if(currentUrl.indexOf("reuniones.html")>-1)
	{
		$.post(URL+"includes/ajax.php",{ action:"getReuniones"} ,function(data)
	{	
	  if(data!="empty") //if correct login detail
	  {
		var htmlMenu = "<table width=\"185\" border=\"0\" cellspacing=\"0\" cellpadding=\"18\">";
		var obj = jQuery.parseJSON(data);
		reuniones = obj;
		for(var i=0;i<obj.length;i++)
		{
			htmlMenu += "<tr><td height='18' align='right' background='images/lugares_menu-14.png' valign='middle' ><a id='"+i+"' onclick='showReunion(this.id);' href='#' class='text'>"+obj[i].fecha+"hs</a></td></tr>";	
		}
		
		htmlMenu += "</table>";
	  }
	  else
	  {
		  
	  }
	  
	  $("#icon").html(htmlMenu);
	  
	  });
	}
	
	if($("#calendar")!=null)
	{
		$.post(URL+"includes/ajax.php",{ action:"getCalendar"} ,function(data)
	{	
	  if(data!="empty") //if correct login detail
	  {
		$("#calendar").html(data);

	  }
	  else
	  {
		  alert("Ocurrio un error al traer el calendario");
	  }
	  
	  });	
	}
	
	$("#sexo_m").click(function()
	{
		$("#imagen_sexo").attr("src","images/img_m.png");		
	});	
	
	$("#sexo_f").click(function()
	{
		$("#imagen_sexo").attr("src","images/img_f.png");		
	});	
	
	// UPDATE FORM REGISTRO
	$("#updForm1").submit(function()	
		{
			$("#msgbox2").removeClass().addClass('messagebox').text('Validando...').fadeIn(1000);
			
			if($("#fullname").val()!="" && isEmail($("#email").val()))
			{
				$("#msgbox2").removeClass().addClass('messagebox').text('Procesando...').fadeIn(1000);
				return true;
			}
			else
			{
				$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				  $(this).html('Complete todos los campos por favor').addClass('messageboxerror').fadeTo(900,1);
				  return false;
				});	
			}
			
			
		}
	);
	
	$("#updForm2").submit(function()	
		{	
			$("#msgbox2").removeClass().addClass('messagebox').text('Validando...').fadeIn(1000);
			
			if($("#dni").val()!="" && $("#telefono1").val()!="" && $("#telefono2").val()!="" && $("#civil").val()!="" && $("#hijos").val()!="" && $("#educacion").val()!="" && $("#ocupacion").val()!="" && $("#religion").val()!="" && $("#idioma").val()!="" && $("#buscas").val()!="" && $("#relacion").val()!="")
			{
				$("#msgbox2").removeClass().addClass('messagebox').text('Procesando...').fadeIn(1000);
				return true;	
			}
			else
			{
				$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				  $(this).html('Complete todos los campos por favor').addClass('messageboxerror').fadeTo(900,1);
				  return false;
				});	
			}
			
			return false;
			
		});
	
	
	// FORMULARIO DE REGISTRO
	$("#regForm1").submit(function()	
		{
			$("#msgbox2").removeClass().addClass('messagebox').text('Validando...').fadeIn(1000);
			
			if($("#username").val()!="" && ($("#password").val()==$("#password2").val()) && $("#password").val()!="" && $("#firstname").val()!="" && $("#lastname").val()!="" && isEmail($("#email").val()))
			{
				$("#msgbox2").removeClass().addClass('messagebox').text('Procesando...').fadeIn(1000);
				return true;
			}
			else
			{
				$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				  $(this).html('Complete todos los campos por favor').addClass('messageboxerror').fadeTo(900,1);
				  return false;
				});	
			}
			
			
		}
	);
	
	$("#regForm2").submit(function()	
		{	
			$("#msgbox2").removeClass().addClass('messagebox').text('Validando...').fadeIn(1000);
			
			
					
					if($("#dni").val()!="" && $("#telefono1").val()!="" && $("#telefono2").val()!="" && $("#civil").val()!="" && $("#hijos").val()!="" && $("#educacion").val()!="" && $("#ocupacion").val()!="" && $("#religion").val()!="" && $("#idioma").val()!="" && $("#buscas").val()!="" && $("#relacion").val()!="")
					{
						if($('#tos').attr('checked'))
						{
							$("#msgbox2").removeClass().addClass('messagebox').text('Procesando...').fadeIn(1000);
							return true;	
						}
						else
						{
							$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
							{ 
							  //add message and change the class of the box and start fading
							  $(this).html('Debe aceptar los terminos y condiciones').addClass('messageboxerror').fadeTo(900,1);
							  return false;
							});	
						}
					}
					else
					{
						$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
						{ 
						  //add message and change the class of the box and start fading
						  $(this).html('Complete todos los campos por favor').addClass('messageboxerror').fadeTo(900,1);
						  return false;
						});	
					}
			
			return false;
			
		});
	
	$("#updateForm").submit(function()	
		{
			$("#msgbox2").removeClass().addClass('messagebox').text('Validando...').fadeIn(1000);
			
			if($("#presentacion").val()!="")
			{
				$("#msgbox2").removeClass().addClass('messagebox').text('Procesando...').fadeIn(1000);
				$.post("includes/ajax.php",{ action:"updatePresentacion", presentacion:$('#presentacion').val(),idCliente:$('#idCliente').val()} ,function(data)
        		{
				  if(data=='yes') //if correct login detail
				  {
					$("#msgbox2").fadeTo(200,0.1,function()  //start fading the messagebox
					{ 
					  //add message and change the class of the box and start fading
					  $(this).html('Tu perfil ha sido actualizado. Gracias!').addClass('messageboxok').fadeTo(900,1);
					  return false;
					  //redirect to secure page	  
					});
					setInterval("location.href='index.html'",2000);	
				  }
				  else
				  {
						$(this).html('Ocurrio un error, por favor intentelo mas tarde.').addClass('messageboxok').fadeTo(900,1);
						return false;
				  }
				});
			}
			else
			{
				$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				  $(this).html('Complete todos los campos por favor').addClass('messageboxerror').fadeTo(900,1);
				  
				});	
			}
			
			return false;
		}
	);
	
	 $("a[rel='pop-up']").click(function () {  
        var caracteristicas = "height=700,width=800,scrollTo,resizable=1,scrollbars=1,location=0";  
        nueva=window.open(this.href, 'Popup', caracteristicas);  
		return false;
	});  
	
	// FORMULARIO DE CONTACTO
	// FORMULARIO DE CONTACTO
	$("#contacto").submit(function()	
		{
			$.post(URL+"includes/ajax.php",{ action:"checkCaptcha", captcha: $("#tmptxt").val()} ,function(data)
			{	
				if(data=="ok")
				{
						
					$("#msgbox2").removeClass().addClass('messagebox').text('Validando...').fadeIn(1000);
					
					if($("#telefono").val()!="" && $("#nombre").val()!="" && isEmail($("#email").val()) && $('#mensaje').val()!="")
					{
						$("#msgbox2").removeClass().addClass('messagebox').text('Enviando...').fadeIn(1000);
						
						$.post("includes/ajax.php",{ action:"contacto", nombre:$('#nombre').val(),email:$('#email').val(),telefono:$('#telefono').val(),mensaje:$('#mensaje').val()} ,function(data)
						{
						  if(data=='yes') //if correct login detail
						  {
							$("#msgbox2").fadeTo(200,0.1,function()  //start fading the messagebox
							{ 
							  //add message and change the class of the box and start fading
							  $(this).html('Gracias por contactarnos!!!').addClass('messageboxok').fadeTo(900,1);
							  return false;
							  //redirect to secure page	  
							});
							setInterval("window.location.reload( true );",2000);	
						  }
						  else
						  {
								$(this).html('Ocurrio un error al enviar el correo, por favor intentelo mas tarde.').addClass('messageboxok').fadeTo(900,1);
								return false;
						  }
						});
					}
					else
					{
						$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
						{ 
						  //add message and change the class of the box and start fading
						  $(this).html('Complete todos los campos por favor').addClass('messageboxerror').fadeTo(900,1);
						  return false;
						});	
					}
				}
				else
				{
					$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
					  //add message and change the class of the box and start fading
					  $(this).html('Codigo de verificacion incorrecto').addClass('messageboxerror').fadeTo(900,1);
					  return false;
					});	
				}
			});
		return false; //not to post the  form physically
		}
	);
	
	$("#pwdButton").click(function()
		{
			var caracteristicas = "height= 330,width=340,scrollTo,resizable=0,scrollbars=0,location=0";  
        	nueva=window.open("password_change.php", 'Popup', caracteristicas); 
			return false;
		});
	
	
	$("#pwdChange").submit(function()	
		{
		$("#msgbox2").removeClass().addClass('messagebox').text('Validando...').fadeIn(1000);
		
		$.post(URL+"includes/ajax.php",{ action:"checkCaptcha", captcha: $("#tmptxt").val()} ,function(data)
	{	
		if(data=="ok")
		{	
			if($("#pwd1").val()!="" && $("#pwd2").val()!="" && $("#pwd3").val()!="")
			{
				if($("#pwd2").val()==$("#pwd3").val())
				{
					$("#msgbox2").removeClass().addClass('messagebox').text('Enviando...').fadeIn(1000);
				
					$.post("includes/ajax.php",{ action:"pswChange", pwd1:$('#pwd1').val(), pwd2:$('#pwd2').val()} ,function(data)
					{	
											
					  if(data=='yes') //if correct login detail
					  {
						$("#msgbox2").fadeTo(200,0.1,function()  //start fading the messagebox
						{ 
						  //add message and change the class of the box and start fading
						  $(this).html('Tu  contraseña ha sido modificada con exito.').addClass('messageboxok').fadeTo(900,1);
						  return false;
						  //redirect to secure page	  
						});
						setInterval("self.close();",3000);	
					  }
					  else
					  {
						 	$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
							{ 
							  //add message and change the class of the box and start fading
							  $(this).html('La contraseña anterior ingresada es incorrecta.').addClass('messageboxerror').fadeTo(900,1);
							  return false;
							});	
						}
					});
				}
				else
				{
					$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
					  //add message and change the class of the box and start fading
					  $(this).html('La nueva contraseña no coincide con la repetición').addClass('messageboxerror').fadeTo(900,1);
					  return false;
					});		
				}
				
			}
			else
			{
				$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				  $(this).html('Complete todos los campos por favor').addClass('messageboxerror').fadeTo(900,1);
				  return false;
				});		
			}
			}
				else
				{
					$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
					  //add message and change the class of the box and start fading
					  $(this).html('Codigo de verificacion incorrecto').addClass('messageboxerror').fadeTo(900,1);
					  return false;
					});	
				}
			});
			
			return false;
			
		});
	
	// RECUPERAR PASSWORD
	$("#pswForm").submit(function()	
		{
			$("#msgbox2").removeClass().addClass('messagebox').text('Validando...').fadeIn(1000);
			
			if(isEmail($("#email").val()))
			{
				$("#msgbox2").removeClass().addClass('messagebox').text('Enviando...').fadeIn(1000);
				
				$.post("includes/ajax.php",{ action:"pswReset", email:$('#email').val()} ,function(data)
        		{					
				  if(data=='yes') //if correct login detail
				  {
					$("#msgbox2").fadeTo(200,0.1,function()  //start fading the messagebox
					{ 
					  //add message and change the class of the box and start fading
					  $(this).html('Tu nueva contraseña ha sido enviada a tu cuenta de correo.').addClass('messageboxok').fadeTo(900,1);
					  return false;
					  //redirect to secure page	  
					});
					setInterval("location.href='index.html'",3000);	
				  }
				  else
				  {
					  if(data=="errorMail")
					  {
						$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
						{ 
						  //add message and change the class of the box and start fading
						  $(this).html('El correo electronico ingresado no es correcto.').addClass('messageboxerror').fadeTo(900,1);
						  return false;
						});	
					  }
					  else
					  {
						 $("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
						{ 
						  //add message and change the class of the box and start fading
						  $(this).html('Ocurrio un error al enviar el correo. Por favor intentalo de nuevo.').addClass('messageboxerror').fadeTo(900,1);
						  return false;
						});		 
					  }
						
						return false;
				  }
				});
			}
			else
			{
				$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				  $(this).html('Complete todos los campos por favor').addClass('messageboxerror').fadeTo(900,1);
				  return false;
				});	
			}
		return false; //not to post the  form physically
		}
	);
});
