var opcio;

function logAction()
{
	var loginName = $('l').value;
	var password = $('p').value;
	onLogIN(loginName,password);
}

function forgetPassword()
{
	var email = $('OlvidoPassword').value;
	opcio=2;
	onAOlvidadoSuContrasenya(email);
}

function sendEvaluation(){
	var nom = $('nom').value;
	var telef1 = $('telefono').value;
	var telef2 = $('telefono2').value;
	var email = $('email').value;
	var empresa = $('empresa').value;
	var comentaris = $('comentarios').value;
	opcio=3;
	onQuieroProvarlo(nom,telef1,telef2,email,empresa,comentaris);
}

/**
* Funció a omplir per realitzar login
*/
function onLogIN(login,password)
{
	var url="sp3/comprovar.php?l="+login+"&p="+password+"&on=sp";
	window.location = url;
	//alert("Login: " + login + ", password: " + password);
}

function onAOlvidadoSuContrasenya(email)
{
	if(correuValid(email))
	{

		url="recordarPassword.php?mail=" + email;

		if (isWorking)
		{
			http.onreadystatechange = function () {} //assignem una funcio buida
			http.abort();
		}		
		if (http)
		{
			http.open("GET", url, true);
			http.onreadystatechange = handleHttpResponse;
			isWorking = true;
			http.send(null);
			//confio que anira
			alert("Correo enviado correctamente. Gracias.");
		}

	}
	else
	{
		alert("Correo no válido.");	
	}
}

/**
* Funció a omplir per realitzar al peticio de periode d'evalucaio
*/
function onQuieroProvarlo(nom,telef1,telef2,email,empresa,comentaris)
{
	//alert("Nom: " + nom + ", Telf1: " + telef1 + ", Telef2: " + telef2 + ", email: " + email + ", empresa: " + empresa + ", comentaris: " + comentaris);
	
	if(correuValid(email))
	{
		if(noBuit(empresa) && noBuit(nom) && noBuit(telef1))
		{

	    url="enviarCorreuProva.php?nom=" + nom + "&telf1=" + telef1 + "&telf2=" + telef2 + "&email=" + email + "&empresa=" + empresa + "&comentaris=" + comentaris;
	    //apanyo per varies crides seguides
	    if (isWorking)
	    {
	        http.onreadystatechange = function () {} //assignem una funcio buida
	        http.abort();
	    }		
	    if (http)
	    {
	        http.open("GET", url, true);
	        http.onreadystatechange = handleHttpResponse;
	        isWorking = true;
	        http.send(null);
			//confio que anira
			alert("Correo enviado correctamente. Gracias.");
	    }
	  }
		else
		{
			alert("Debe rellenar todos los campos.");
		}
	}
	else
	{
		alert("Correo no válido.");	
	}
}

function correuValid(correu)
{         
   //expresio regular  
   var v=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/             

   return v.test(correu)  
}  

function noBuit(t) 
{  
   for ( i = 0; i < t.length; i++ ) 
   {  
     if ( t.charAt(i) != " " ) 
     {  
             return true  
     }  
   }  
   return false  
}  

function handleHttpResponse()
{
    if (http.readyState ==1)
    {
    
    }
    if (http.readyState == 4)  
    {			 
        var resp = http.responseText; //no necessito fer res
        
        isWorking = false;
		if (opcio==1)
		{

		}
		else if(opcio==2) //recordar contrasenya
		{
			$('OlvidoPassword').value="";
		}
		else if(opcio==3) //quiero provarlo
		{
			
		}
    }
}
