/*
** Check user informations (signup form)
*/
function  check_username(username)
{
  var obj;
  
  obj = ajax_req_s("/ajax/check_username.php?username="+username+"");
document.getElementById('result').innerHTML = obj;
  
}

function check_password(password, id)
{
  var c_password = document.getElementById('c_password').value;
  var password = document.getElementById('password').value;
  
  if (c_password.length > 0)
  {
    if (c_password != password)
      ;
    else
    ;
  }
  return (0);
  if (password.length < 6)
   ;
  else
   ;
}

function check_email(email, id)
{
  if(verif_email(email))
  ;
  else
  ;
  
}

function verif_email(email) { 
   var arobase = email.indexOf("@")
   var point = email.lastIndexOf(".")
   if((arobase < 3)||(point + 2 > email.length)
      ||(point < arobase+3)) return false
   return true
}

function  check_invitation_code(code, id)
{
  if (ajax_req_s("/ajax/check_invitation_code.php?code="+code) == "y")
  {
    document.getElementById(id).className = "invitation_code_ok";
    return (1);
  }
  else
   {
    document.getElementById(id).className = "invitation_code_wrong";
    return (0);
    }
}

function signup_user(id_div)
{
  var req;
  var username;
  var password;
  var email;
  var fullname;
  var company;
  var position;
  var code;
  var timezone;


  username = document.forms["signup"].elements["username"].value;
  firstname = document.forms["signup"].elements["firstname"].value;
  lastname = document.forms["signup"].elements["lastname"].value;
  company = document.forms["signup"].elements["company"].value;
  position = document.forms["signup"].elements["position"].value;
  email = document.forms["signup"].elements["email"].value;
  password = document.forms["signup"].elements["password"].value;
  password = document.forms["signup"].elements["password"].value;
  c_password = document.forms["signup"].elements["c_password"].value;
  code = document.forms["signup"].elements["code"].value;
  timezone = document.forms["signup"].elements["timezone"].value;
  if (!check_invitation_code(code, "code"))
  {
    alert("Please put a valid invitation code or request one");
    return (0);
  }
  if (!username || !password || !c_password || !email || !company || !position || !lastname || !firstname)
  {
    alert("Please fill out the form");
    return (0);
  }
  if (password != c_password)
  {
    alert("Please enter the same password");
    return (0);
  }
  if (!verif_email(email))
  {
    alert("Please enter a valid email address");
    return (0);
  }

  email = document.forms["signup"].elements["email"].value;
  req = "/ajax/signup_user.php?username="+username+"&password="+password+"&email="+email+"&name="+name+"&company="+company+"&position="+position+"&code="+code+"&timezone="+timezone+"&firstname="+firstname+"&lastname="+lastname+"";
  ajax_req_a_signup(req, id_div, "Please wait...");
  return (0);

}



function Timer(){   //Boucle de 3 seçondes (3000 miliseçondes)
   timeoutID = window.setTimeout("check_signuped()",3000);
}

function Stop(){      //Arret de la boucle
   window.clearTimeout(timeoutID)
}

function dec_hex(dec) {
	var hexa = '0123456789ABCDEF',hex=''
	while (dec>15) {
		tmp = dec-(Math.floor(dec/16))*16;
		hex = hexa.charAt(tmp)+hex;
		dec = Math.floor(dec/16);
	}
	hex = hexa.charAt(dec)+hex;
	return(hex);
}
function hex_dec(hex) {
	dec = parseInt(hex,16);
	return dec;
}

function request_invitation_code(id_div)
{
  var req;
  var email;
  var fullname;
  var company;
  var position;
  var obj;
  
  obj = document.getElementById(id_div);
  name = document.forms["signup"].elements["fullname"].value;
  company = document.forms["signup"].elements["company"].value;
  position = document.forms["signup"].elements["position"].value;
  email = document.forms["signup"].elements["email"].value;
  

  if (!email || !name || !company || !position)
  {
    obj.innerHTML = "Please fill out the form : (Fullname, <br />Company, Position, email)";
    return (0);
  }
 
  if (!verif_email(email))
  {
    obj.innerHTML = "Please enter a valid email address";
    return (0);
  }
  ajax_req_a("/ajax/request_invitation_code.php?email="+email+"&name="+name+"&company="+company+"&position="+position+"", id_div, "");
}
  
function my_count(tab)
{
  var i = 0;
  
  while (tab[i])
    i++;
  return (i);
}

function url_encode(clearString) 
{
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function url_decode(encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

function  free_text(text, id)
{
  var obj;
  
  obj = document.getElementById(id);
  if (obj.value == text)
    obj.value= "";
  
}

function  send_email()
{
  var name;
  var email;
  var message;
 
  name = document.forms["send_mail"].elements["name"].value;
  email = document.forms["send_mail"].elements["email"].value;
  message = document.forms["send_mail"].elements["message"].value;
  ajax_req_s("/ajax/send_mail.php?name="+name+"&email="+email+"&message="+message+"");
  document.getElementById("result").innerHTML = "Thank you";
  
}

function  manage_weekly_report()
{ 
  var result;
  
  if (document.getElementById('weekly_report').checked)
  {
     ajax_req_s("/ajax/manage_weekly_report.php?statut=1");
    result ="Weekly report enabled";
  }
  else
  {
     ajax_req_s("/ajax/manage_weekly_report.php?statut=activate");
    result = "Weekly report disabled";
  }
  document.getElementById('result_wr').innerHTML = result;
}

function  manage_follower_alert()
{ 
  var result;
  
  if (document.getElementById('follower_alert').checked)
  {
    ajax_req_s("/ajax/manage_follower_alert.php?statut=1");
    result ="Follower alert enabled";
  }
  else
  {
    ajax_req_s("/ajax/manage_follower_alert.php?statut=0");
    result = "Follower alert disabled";
  }
  document.getElementById('result_alert').innerHTML = result;
}

function  manage_task_alert()
{ 
  var result;
  
  if (document.getElementById('task_alert').checked)
  {
    ajax_req_s("/ajax/manage_task_alert.php?statut=1");
    result ="Task alert enabled";
  }
  else
  {
    ajax_req_s("/ajax/manage_task_alert.php?statut=0");
    result = "Task alert disabled";
  }
  document.getElementById('result_alert').innerHTML = result;
}

function  send_my_report()
{
  ajax_req_s("/send_my_report.php");
  alert("Your report has been sent!");
}

function  valid_new_passwd(last_password, password, c_password)
{
  last_password = url_encode(last_password);
  password = url_encode(password);
  c_password = url_encode(c_password);

  ajax_req_z("/ajax/change_password.php?last_password="+last_password+"&password="+password+"&c_password="+c_password, "result_password");
}

function  save_account(url, location)
{
  url = url_encode(url);
  location = url_encode(location);
  ajax_req_a("/ajax/save_account.php?url="+url+"&location="+location+"", "result_account", "");
}

function  forgot_password(email, id_result)
{
  ajax_req_a("/ajax/generate_forgot_password.php?email="+email, id_result, "");
}

function	send_feedback(obj)
{
	ajax_req_a("/ajax/send_feedback.php?feedback="+obj.value, "result_feedback", "");
	obj.value = "Thanks for your feedback";
}

function	help_tape_feedback(obj)
{

	if (obj.value == "Welcome to producteev private beta!\nPlease leave us your suggestions, comments or feature requests in this box.")
		obj.value = "";
	if (obj.value == "Thanks for your feedback")
		obj.value = "";
}

function  get_id_of_elem(elem)
{
  var tab = new Array(2);
  
  tab = elem.split("_");
  return (tab[1]);
}