// JavaScript Document
function guardaUsuario()
{
 
 			var fui = 0; 
			var email = document.getElementById("email").value;
			var password = document.getElementById("password1").value;
			var fechanac = document.getElementById("fechanac").value;
			var horanac = document.getElementById("horanac").value;
			var pais = encodeURIComponent(document.getElementById("paiss").options[document.getElementById("paiss").selectedIndex].value);
			var estado = encodeURIComponent(document.getElementById("estadoo").options[document.getElementById("estadoo").selectedIndex].value);
			var ciudad = encodeURIComponent(document.getElementById("ciudadd").options[document.getElementById("ciudadd").selectedIndex].value);
			var nombre = document.getElementById("nombre").value;
			var apellido = document.getElementById("apellido").value;
			var passwordencript = hex_md5(password);
			var ajax=nuevoAjax();
			ajax.open("POST", "metodos/guardausuario.php?", true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.send("email="+email+"&password="+passwordencript+"&fechanac="+fechanac+"&horanac="+horanac+"&pais="+pais+"&estado="+estado+"&ciudad="+ciudad+"&nombre="+nombre+"&apellido="+apellido+"&lugar=index");
			ajax.onreadystatechange=function()
			{	
				if (ajax.readyState==4)
				{   
					if(!ajax.responseText) { /*alert("no hay nada obtiene pais");*/}
					else 
					{      //alert("Entro pais");
							respuesta=ajax.responseText.split("$");	
							if(respuesta[1]==1){
								
								//location.href = 'indexb.php';
								location.href="gracias.php";
							}else{
								if(respuesta[1]==0){
								//document.getElementById("registronoexitoso").style.display="block";	
								alert("No se pudo procesar su solicitud de registro por favor verifique los datos");
								}
							}
							
							
					}
				}
			}
		
}
function guardaUsuarioFacebook(nombre,apellido,email,fecha,id)
{
			var ajax=nuevoAjax();
			var url = "modificarsuscripcion.php?email="+email+"&fechanac="+fecha+"&nombre="+nombre+"&id="+id+"&apellido="+apellido+"&horanac=00:00";
			ajax.open("POST", "metodos/guardauserfacebook.php?", true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.send("email="+email+"&fechanac="+fecha+"&nombre="+nombre+"&id="+id+"&apellido="+apellido);
			ajax.onreadystatechange=function()
			{	
				if (ajax.readyState==4)
				{   
					if(!ajax.responseText) { /*alert("no hay nada guarda facebook");*/}
					else 
					{      //alert("Entro guarda facebook");
							respuesta=ajax.responseText.split("$");
							
							if(respuesta[2]==2){
										Boxy.confirm("Para una respuesta mas completa debe llenar algunos datos desea llenarlos ahora?",
													 function() { Boxy.load(url,{type:"POST",
																				 title:"Modificar datos",
																				 draggable:true,
																				 modal:true}) });
							}else{
									if(respuesta[1]==1){
															
												location.href="gracias.php";
										
									}else{
												if(respuesta[1]==0){
												location.href="index.php";
												}
									}
							}
							
							
					}
				}
			}
		
}
function login(){
         FB.api('/me', function(response){ 
								var nombre = response.first_name;
								var apellido = response.last_name;
								var email = response.email;
								var fecha = response.birthday;
								var id = response.id;
								guardaUsuarioFacebook(nombre,apellido,email,fecha,id);	});
		 
		 
		 
}
function logout(){
          location.href = 'metodos/cerrar.php';
		  //alert("logout");
}
function colocaLogout(){
	lugar = getCookie('lugar');
	if(lugar=='facebook'){
		document.getElementById("cerrar").style.display="none";
		document.getElementById("fboton2").style.display="block";
	}
	if(lugar=='index'){
		document.getElementById("fboton2").style.display="none";
		document.getElementById("cerrar").style.display="block";
	}
}
/*function setCookie(name, value)
         {
         //If name is the empty string, it places a ; at the beginning
         //of document.cookie, causing clearCookies() to malfunction.
         if(name != '')
            document.cookie = name + '=' + value;
}*/

function getCookie(name)
         {
         //Without this, it will return the first value 
         //in document.cookie when name is the empty string.
         if(name == '')
            return('');
         
         name_index = document.cookie.indexOf(name );
         
         if(name_index == -1)
            return('');
         
         cookie_value =  document.cookie.substr(name_index + name.length + 1, 
                                                document.cookie.length);
         
         //All cookie name-value pairs end with a semi-colon, except the last one.
         end_of_cookie = cookie_value.indexOf(';');
         if(end_of_cookie != -1)
            cookie_value = cookie_value.substr(0, end_of_cookie);

         //Restores all the blank spaces.
         space = cookie_value.indexOf('+');
         while(space != -1)
              { 
              cookie_value = cookie_value.substr(0, space) + ' ' + 
              cookie_value.substr(space + 1, cookie_value.length);
							 
              space = cookie_value.indexOf('+');
              }

         return(cookie_value);
}
function actualizaUsuario()
{
 
 			var fui = 0; 
			var email = document.getElementById("emailm").value;
			var password = document.getElementById("password1m").value;
			var fechanac = document.getElementById("fechanacm").value;
			var horanac = document.getElementById("horanacm").value;
			var pais = encodeURIComponent(document.getElementById("paissm").options[document.getElementById("paissm").selectedIndex].value);
			var estado = encodeURIComponent(document.getElementById("estadoom").options[document.getElementById("estadoom").selectedIndex].value);
			var ciudad = encodeURIComponent(document.getElementById("ciudaddm").options[document.getElementById("ciudaddm").selectedIndex].value);
			var nombre = document.getElementById("nombrem").value;
			var apellido = document.getElementById("apellidom").value;
			var passwordencript = hex_md5(password);
			var ajax=nuevoAjax();
			ajax.open("POST", "metodos/guardausuario.php?", true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.send("email="+email+"&password="+passwordencript+"&fechanac="+fechanac+"&horanac="+horanac+"&pais="+pais+"&estado="+estado+"&ciudad="+ciudad+"&nombre="+nombre+"&apellido="+apellido+"&lugar=facebook");
			ajax.onreadystatechange=function()
			{	
				if (ajax.readyState==4)
				{   
					if(!ajax.responseText) { /*alert("no hay nada obtiene pais");*/}
					else 
					{      //alert("Entro pais");
							respuesta=ajax.responseText.split("$");	
							if(respuesta[1]==1){
								
								location.href = 'gracias.php';
								
							}else{
								if(respuesta[1]==0){
								document.getElementById("registronoexitoso").style.display="block";	
								}
							}
							
							
					}
				}
			}
		
}
