function checkLogin()
{
   var cutilizator, clogat;
   cutilizator = document.getElementById('utilizator');
   cparola = document.getElementById('parola'); // global
   clogat = document.getElementById('logat');
   if(trim(cutilizator.value) == '')
   {
      // in parametru ok folosesc # in loc de apostrof pentru ca sqlProtect pune \ ca anti sql injection.
      // o alta solutie folosesc mai jos, variabila cparola o fac globala
      //showModal("baseui/MsgBox.php?mesaj=Completaţi utilizatorul!&tip=16&ok=document.getElementById(#utilizator#).focus()");
      mesaj("Completaţi utilizatorul!", "$('#utilizator').focus()", 1);
      return false;
   }
   /*else if(trim(cemail.value) == '')
   {
      alert('Please enter your email');
      cemail.focus();
      return false;
   }
   else if(!isEmail(trim(cemail.value)))
   {
      alert('Email address is not valid');
      cemail.focus();
      return false;
   }*/
   if(trim(cparola.value) == '')
   {
      //showModal('baseui/MsgBox.php?mesaj=Completaţi parola!&tip=16&ok=cparola.focus()');
      mesaj("Completaţi parola!", "$('#parola').focus()", 1);
      return false;
   }

   // Totul OK formul a fost completat acum verificam loginul
   cutilizator.value = trim(cutilizator.value);
   cparola.value = trim(cparola.value); 
   return login(cutilizator.value, cparola.value, clogat.checked, false);
}

function login(utilizator, parola, logat, lnewuser)
{
    var http = getHTTPObject();
    //var url = this.action;
    //de transmis prin post codul din imagine
    //var params = "anti_spam_code="+document.getElementById("anti_spam_code").value;
        
    setCookie("CateringLogat",logat,2);    
    http.open("post", "./base/checklogin.php", false, utilizator, parola);
    
    // setari necesare transmiterii datelor prin POST
    //http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //http.setRequestHeader("Content-length", params.length);
    //http.setRequestHeader("Connection", "close");    
    
    // facem cererea cu trimitere de date prin POST
    //http.send(params);
    http.send("");
    //alert(http.responseText);
    if (http.status == 200) {
        window.location="mainpage.php";
        return true;
    } else {
        //alert('Login esuat!');
        //showModal('baseui/MsgBox.php?mesaj=Login eşuat!&tip=16');        
        mesaj("Login eşuat! Utilizator şi/sau parola incorecte!", "", 1);
        return false;
    }
}

function logout()
{
    var http = getHTTPObject();
    http.open("get", this.parentNode.action, false, "null", "null");
    http.send("");
    alert("You have been logged out.");
    return false;
}

function newLogin()
{
    var _utilizatorN,
    _parolaN,
    _parolaNCF,
    _numeN,
    _prenumeN,
    _telN,
    _emailN,
    _id_messN,
    _anti_spam_code
    
    _utilizatorN = document.getElementById('utilizatorN');
    _parolaN = document.getElementById('parolaN');
    _parolaNCF = document.getElementById('parolaNCF');
    _numeN = document.getElementById('numeN');
    _prenumeN = document.getElementById('prenumeN');
    _telN = document.getElementById('telN');
    _emailN = document.getElementById('emailN');
    _id_messN = document.getElementById('id_messN');
    _anti_spam_code = document.getElementById('anti_spam_code'); 
    _chkCond = document.getElementById('chkCond'); 
    
    if(trim(_utilizatorN.value) == '')
    {
        //showModal('baseui/MsgBox.php?mesaj=Completati id-ul utilizator!&tip=16&ok=_utilizatorN.focus();');
        mesaj("Completaţi id-ul utilizator!", "$('#utilizatorN').focus()", 1);
        return false;
    }
    if(trim(_parolaN.value) == '')
    {
        //showModal('baseui/MsgBox.php?mesaj=Completati parola!&tip=16&ok= _parolaN.focus();');
        mesaj("Completaţi parola!", "$('#parolaN').focus()", 1);
        return false;
    }
    if (_parolaN.value != _parolaNCF.value)
    {
        //showModal('baseui/MsgBox.php?mesaj=Nu ati confirmat corect parola!&tip=16&ok=_parolaNCF.focus();');
        mesaj("Nu aţi confirmat corect parola!", "$('#parolaNCF').focus()", 1);
        return false;  
    }
    if(trim(_numeN.value) == '')
    {
        //showModal('baseui/MsgBox.php?mesaj=Completati parola!&tip=16&ok= _parolaN.focus();');
        mesaj("Completaţi numele!", "$('#numeN').focus()", 1);
        return false;
    }
    if(trim(_prenumeN.value) == '')
    {
        //showModal('baseui/MsgBox.php?mesaj=Completati parola!&tip=16&ok= _parolaN.focus();');
        mesaj("Completaţi prenumele!", "$('#prenumeN').focus()", 1);
        return false;
    }
    if(trim(_telN.value) == '')
    {
        //showModal('baseui/MsgBox.php?mesaj=Completati parola!&tip=16&ok= _parolaN.focus();');
        mesaj("Completaţi telefonul la care puteti fi contactat!", "$('#telN').focus()", 1);
        return false;
    }
    if (!_chkCond.checked) 
    {
        mesaj("Este absolut necesar să fiți de acord cu termenii si condițiile de utilizare a acestui site. Vă rugam sa le citiți cu atenție.", "$('#chkCond').focus()", 1);
        return false;  
    }  
    

    _utilizatorN.value = trim(_utilizatorN.value);
    _parolaN.value = trim(_parolaN.value); 
    return createLogin(_utilizatorN.value, _parolaN.value, _anti_spam_code.value);
}

function createLogin(utilizator, parola, antispam)
{
    var http = getHTTPObject();
    // de transmis prin post codul din imagine
    var params = "anti_spam_code="+document.getElementById("anti_spam_code").value+
                 "&numeN="+document.getElementById("numeN").value+
                 "&prenumeN="+document.getElementById("prenumeN").value+
                 "&telN="+document.getElementById("telN").value+
                 "&emailN="+document.getElementById("emailN").value+
                 "&id_messN="+document.getElementById("id_messN").value+
                 "&idGrup="+document.getElementById("idGrup").value+
                 "&parolaG="+document.getElementById("parolaG").value;
    //alert(params);
    http.open("post", "./base/newlogin.php", false, utilizator, parola);
    
    // setari necesare transmiterii datelor prin POST
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    //http.setRequestHeader("Connection", "close");    
    
    // facem cererea cu trimitere de date prin POST
    http.send(params);
    //alert(http.responseText);
    
    if (http.status == 200) {
        //cont creat, ne intoarcem la fereastra de login, sa se logheze omul nostru.
        //window.location="index.php";
        login(utilizator, parola, false, true);
        return true;
    } else {
        //alert('A aparut o eroare. Mesaj:\n'+http.responseText);
        //showModal('baseui/MsgBox.php?mesaj=A aparut o eroare. Mesaj: '+http.responseText+'&tip=16');
        mesaj("A apărut o eroare! " + http.responseText, "$('#chkCond').focus()", 1);
        AjaxLoad('base/newloginantispam.php', document.getElementById('antispamimg'));  
        return false;
    }
}


function showTerms()
{
    window.open ('./baseui/terms.php', 'newwindow', config='height=600, width=800, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');
}

function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}

/*function isEmail(str)
{
   var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|
ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|
bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|
ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|
dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|
gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|
hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|
kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|
ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|
mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|
nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|
re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|
su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|
ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|
zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;

return regex.test(str);
}*/

function getHTTPObject() {
    var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
    return xmlHttp;
}



