var xmlHttp;// global instance of XMLHttpRequest
var vieneDe = '';

function createXmlHttpRequest()
{
       if(window.ActiveXObject)
       {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }

    else if(window.XMLHttpRequest)
    {
        xmlHttp=new XMLHttpRequest();
     }

}

function loginTop()
{
	//document.getElementById("msg").innerHTML="Enviando...";
  var cargando = document.getElementById('cargando');
	cargando.style.display = 'block';  
//  var formulario = document.getElementById('formulario');
//	formulario.style.display = 'none';  
		
  var usuario=document.loginTopFrm.usuario.value;
  var password=document.loginTopFrm.password.value;
		
	document.getElementById("msg").innerHTML="";
	if ((usuario=="")||(password=="")||(usuario=="Usuario")){
	    document.getElementById("msg").innerHTML="Usuario o contrase&ntilde;a inv&aacute;lidos";
			cargando.style.display = "none";
			formulario.style.display = "block";	 																
	}else{
	      createXmlHttpRequest();
  			xmlHttp.open("GET","php_proc/login.php?usuario=" + usuario + "&password=" + password,true);
  			xmlHttp.onreadystatechange=handleState;
  			xmlHttp.send(null)
	}
}


function handleState()
{ 
  var cargando = document.getElementById('cargando');
  var formulario = document.getElementById('formulario');	
	  
    if(xmlHttp.readyState==4)
    {
        if(xmlHttp.status==200)
        {
          		 var res =  xmlHttp.responseText;
				
                //window.alert("res=" + res);  

				posicionDe = res.lastIndexOf ("Error");
				
                 if (posicionDe>0)
                 {
					 //window.alert("Entro en error 1");								 
	                 document.getElementById("msg").innerHTML="Usuario o contrase&ntilde;a inv&aacute;lidos";
           			 cargando.style.display = 'none';  
                    // formulario.style.display = 'block';  
                  
                 } else {
//						window.alert("Entro en href=" + unescape(window.location.pathname));
                        if (vieneDe != ''){
                            window.location.href = vieneDe;										 
                        }else{
                            window.location.href = unescape(window.location.pathname);										                             
                        }

                 }
        } 
        else {
//					   window.alert("Entro en error 2");
                       document.getElementById("msg").innerHTML="Ha ocurrido un error, int&eacute;ntalo m&aacute;s tarde";
           			   cargando.style.display = 'none';  
                       formulario.style.display = 'block';  
        }
    }
}

function muestraForgot()
{
	
  var forgot = document.getElementById('forgot');
	forgot.style.display = 'block';  
  var formulario = document.getElementById('formulario');
	formulario.style.display = 'none';
	var btnRegresa = document.getElementById('boton_regresa');
	btnRegresa.style.display = 'block';
	
	/*var formulario = document.getElementById('boton_login');
	formulario.style.display = 'none';
	var formulario = document.getElementById('boton_forgot');
	formulario.style.display = 'block';*/
}

function muestraForm()
{
	
  var forgot = document.getElementById('forgot');
	forgot.style.display = 'none';  
  var formulario = document.getElementById('formulario');
	formulario.style.display = 'block'; 
	
	/*var formulario = document.getElementById('boton_login');
	formulario.style.display = 'block';
	var formulario = document.getElementById('boton_forgot');
	formulario.style.display = 'none'; */
}	

function forgotTop()
{
	
  var cargando = document.getElementById('cargando');
	cargando.style.display = 'block';  
  var formulario = document.getElementById('formulario');
	formulario.style.display = 'none';  
//  var forgot = document.getElementById('forgot');
//	forgot.style.display = 'none';  	
		
  var mail=document.ForgotTopFrm.mail.value;
  		
	document.getElementById("msg2").innerHTML="";
	
	
	if (mail==""){			
	    document.getElementById("msg2").innerHTML="Error, E-Mail vacio";
			cargando.style.display = "none";
			forgot.style.display = "block";	 																
	}else{
	      createXmlHttpRequest();
  			xmlHttp.open("GET","php_xml/forgot.php?mail=" + mail,true)
  			xmlHttp.onreadystatechange=handleStateForgot;
  			xmlHttp.send(null)
	}
}

function handleStateForgot()
{ 
  var cargando = document.getElementById('cargando');
  var formulario = document.getElementById('formulario');	
  var forgot = document.getElementById('forgot');
		  
    if(xmlHttp.readyState==4)
    {
        if(xmlHttp.status==200)
        {
          		 var res =  xmlHttp.responseText;   
                 if (res=="Error")
                 {
	                   document.getElementById("msg2").innerHTML="E-Mail no encontrado.";
           			 		 cargando.style.display = 'none';  
                     forgot.style.display = 'block';  
                  
                 } else {							 
	                   document.getElementById("msg2").innerHTML="Datos enviados a " + res;
           			 		 cargando.style.display = 'none';  
                     //formulario.style.display = 'block'; 
					 
                 }
        } 
        else {
                       document.getElementById("msg2").innerHTML="Ha ocurrido un error, int&eacute;ntalo m&aacute;s tarde";
           			 			 cargando.style.display = 'none';  
                       forgot.style.display = 'block'; 
        }
    }
}
