// JavaScript Document
function checkLogin()
{
			var ajax=nuevoAjax();
			var usuario = document.getElementById("myusername").value;
			var contra = hex_md5(document.getElementById("mypassword").value);
			var recordar = document.getElementsByName("remember_me").item(0).checked;
			var url = "modificarsuscripcion.php";
			ajax.open("POST", "http://"+location.hostname+"/componentes/metodos/checklogin.php?", true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.send("myusername="+usuario+"&mypassword="+contra+"&remember_me="+recordar);
			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.ask("Hemos notado que faltan algunos datos importantes en tu perfil, aproveche para completarlos ahora.", ["Seguir"], function(){ModificarPerfil()},{
							                            title:"Completar datos",
														draggable:true,
														modal:true});
								
							}else{
									if(respuesta[1]==1){
																	
										//document.getElementById("fboton").style.display="none";
										//document.getElementById("bienvenida").style.display="block";
										location.reload(true);
										
									}else{
										
										document.getElementById("registronoexitoso").style.display="block";
										setTimeout("location.reload(true)", 1000);
									}
							}
							
							
					}
				}
			}
		
}
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.split("/");
								var dia = fecha[1];
								var mes = fecha[0];
								var ano = fecha[2];
								fecha = dia+"-"+mes+"-"+ano; 
								var id = response.id;
								
								if(email!=""){
									var ajax=nuevoAjax();
									ajax.open("POST", "http://"+location.hostname+"/componentes/metodos/verificadatos.php?", true);
											ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
											ajax.send("email="+email);
											ajax.onreadystatechange=function()
											{	
												if (ajax.readyState==4)
												{   respuesta=ajax.responseText.split("$");	
													if(!ajax.responseText) {
														if(document.URL == "http://"+location.hostname+"/componentes/registro.php"){
																post("http://"+location.hostname+"/componentes/registro.php",{
									 							email:email,
									 							fechanac:fecha,
									 							nombre:nombre,
									 							apellido:apellido,
									 							m:1});
														}else{
																if(respuesta[6]==1){
																	
																	location.href = document.URL;
																}
															}	
													}
													else 
													{      
														if(document.URL == "http://"+location.hostname+"/componentes/registro.php"){
															
															if(respuesta[1]==3){
																var ciudad = respuesta[2];
																var estado = respuesta[3];
																var pais = respuesta[4];
																var hora = respuesta[5];
																post("http://"+location.hostname+"/componentes/registro.php",{
									 							email:email,
									 							fechanac:fecha,
																horanac:hora,
									 							nombre:nombre,
									 							apellido:apellido,
																ciudad:ciudad,
																pais:pais,
																estado:estado,
									 							m:1});
															}else{
																if(respuesta[6]==1){
																	//alert(document.URL+"1");
																	location.href = document.URL;
																}
															}	
																//var	url = "http://www.moonmentum.com/componentes/registro.php?email="+email+"&fechanac="+fecha+"&nombre="+nombre+"&apellido="+apellido+"&m=1"+"&pais="+pais+"&estado="+estado+"&ciudad="+ciudad; 
									
																//Boxy.ask("Hemos notado que faltan algunos datos importantes en tu perfil, aproveche para completarlos ahora.", ["Seguir"], function(){location.href=url;},{
							                           // title:"Completar datos",
														//draggable:true,
														//modal:true});
																//alert("Disculpe este correo ya esta registrado");
														}else{
																if(respuesta[6]==1){
																//alert(document.URL+"2");
																location.href = document.URL;
															}
														}	
													}
												}
											}
								}else{
									document.getElementById("correorepetido").style.display="none"; 
								}
								
								
								
								});
		
		 
		 
		 
		 
}

function logout(){
		  
          location.href = "http://"+location.hostname+"/componentes/metodos/cerrar.php?call="+document.URL;
		  //alert("logout");
}


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);
}
