// JavaScript Document
addEvent(window,'load',inicializarEventosLogin,false);

function inicializarEventosLogin()
{
  var ob;
  if (document.getElementById('formLogin')){
  	ob=document.getElementById('formLogin');
  	addEvent(ob,'submit',enviarDatosLogin,false);
  }
}

function enviarDatosLogin(e)
{
  if (window.event)
    window.event.returnValue=false;
  else
    if (e)
      e.preventDefault();
      presionOpcionLogin();
}


function retornarDatosLogin()
{
  var cad='';
  var inputUser = '';
  var inputPwd = '';


  inputUser = document.getElementById('inputUser').value;
  inputPwd = document.getElementById('inputPwd').value;


  cad ='inputUser='+encodeURIComponent(inputUser)
  cad = cad + '&inputPwd='+encodeURIComponent(inputPwd);

  return cad;

}


var conexionLogin;
function presionOpcionLogin()
{
  conexionLogin=crearXMLHttpRequest();
  conexionLogin.onreadystatechange = procesarEventosLogin;
  conexionLogin.open("POST","../../../modules/login/php/setlogin.php", true);
  conexionLogin.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  conexionLogin.send(retornarDatosLogin());
}


function procesarEventosLogin() {
    var detalles = document.getElementById("error_message");

    if(conexionLogin.readyState == 4) {
       detalles.innerHTML = "";

       OKmsg= conexionLogin.responseText.indexOf("OK"); //En este caso login tiene que retornar OK
       //detalles.innerHTML = conexion1.responseText;  /*test comentar*/
       //alert(conexionLogin.responseText);
       if (OKmsg > 0) {
                //window.alert("Updated");
                //location.reload();
            	//hideLogin();
            OKToken = conexionLogin.responseText.indexOf("Token");
            if (OKToken > 0) {
            	  AbrirCentrado ("../../../FR/php/menu/Moncompte.php?option=user","_self",800,600,"");
            } else {
            	  AbrirCentrado ("../../../FR/php/menu/Moncompte.php?option=inscrip","_self",800,600,"");
            }

       } else {
            //Si periodicity retorna algo distinto a OK (algun error)
            detalles.innerHTML = conexionLogin.responseText;
       }

    }
    else {

    }
}


//***************************************
//Funciones comunes a todos los problemas
//***************************************
function addEvent(elemento,nomevento,funcion,captura)
{
  if (elemento.attachEvent)
  {
    elemento.attachEvent('on'+nomevento,funcion);
    return true;
  }
  else
    if (elemento.addEventListener)
    {
      elemento.addEventListener(nomevento,funcion,captura);
      return true;
    }
    else
      return false;
}

function crearXMLHttpRequest()
{
  var xmlHttp=null;
  if (window.ActiveXObject)
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else
    if (window.XMLHttpRequest)
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}

//////////////////////////////////////////

function AbrirCentrado(Url,NombreVentana,width,height,extras) {
var largo = width;
var altura = height;
var adicionales= extras;
var top = (screen.height-altura)/2;
var izquierda = (screen.width-largo)/2; nuevaVentana=window.open(''+ Url + '',''+ NombreVentana + '','width=' + largo + ',height=' + altura + ',top=' + top + ',left=' + izquierda + ',features=' + adicionales + '');
nuevaVentana.focus();
}
function addEvent(elemento,nomevento,funcion,captura)
{
  if (elemento.attachEvent)
  {
    elemento.attachEvent('on'+nomevento,funcion);
    return true;
  }
  else
    if (elemento.addEventListener)
    {
      elemento.addEventListener(nomevento,funcion,captura);
      return true;
    }
    else
      return false;
}

function cleanField(){
       document.getElementById('inputUser').value ="";
       document.getElementById('inputPwd').value = "";
       document.getElementById("message").value = "";
}
