// Utils for logging in a group admin user
//
// (c) 2007 Textensor Limited, www.textensor.com
// for http://publicationslist.org
//
var rootpath = "";

function loginGroup() {
     var em = document.getElementById("adminemail").value;
     var pwd = document.getElementById("adminpwd").value;
	 //     var gname = document.getElementById("groupname").value;

     if (em.indexOf("@") == -1 || em.indexOf(".") == -1) {
       alert("Please enter a valid email address");
       return;
     }
     if (pwd == "") {
       alert("Please enter a password");
       return;
     }
	 /*
     if (gname == "") {
       alert("Please enter a group name");
       return;
     }
	 */
     eem = encodeURIComponent(em);
     epwd = encodeURIComponent(pwd);
     // egname = encodeURIComponent(gname);

     var ret = postURL(rootpath+"php/loginGroupUser.php", "email="+eem+"&password="+epwd);//&groupname="+egname);

     var bits = ret.split(" ");
     if (bits[0]=="OK") {
       document.location = rootpath+"php/groupAdmin.php";
     }
     else if (bits[0]=="PWD") {
       alert("Wrong password - please type it again");
     }
     else if (bits[0]=="USR") {
       alert("No group account found for "+em);
     }
     else {
       alert("Problem logging in : \n"+ret);
     }     
}


function newGroupUser() {
  var gname = trim(document.getElementById("groupname").value);
  var em = trim(document.getElementById("newgroupemail").value);
  var pwd = trim(document.getElementById("newgrouppassword").value);
  var first = trim(document.getElementById("groupfirstname").value);
  var middle = trim(document.getElementById("groupmiddlename").value);
  var last = trim(document.getElementById("grouplastname").value);

  if (em.indexOf("@") == -1 || em.indexOf(".") == -1) {
    alert("Please enter a valid email address");
    return;
  }

  // Check the names - at least
  if (last == "" || last=="last") {
    alert("Please type your name");
    return;
  }

  if (pwd == "") {
    alert("Please choose a password");
    return;
  }
  if (gname == "") {
    alert("Please enter a name for the new group");
    return;
  }

  // Try logging in to see if email already has an account.
  eem = encodeURIComponent(em);
  epwd = encodeURIComponent(pwd);
  egname = encodeURIComponent(gname);

  var ret = postURL(rootpath+"php/loginGroupUser.php", "email="+eem+"&password="+epwd+"&groupname="+egname);

  var bits = ret.split(" ");
  if (bits[0]=="OK") {
    document.location = rootpath+"php/groupAdmin.php";
  }
  else if (bits[0]=="PWD") {
    alert("Account already exists with a different password");
  }
  else if (bits[0]=="ERR") {
    // OK - new account
    // alert("No account found for "+em);
    var form = document.getElementById('newgroupuserform');
    form.submit();
  }     
}




function sendAdminReminder() {
  var em = document.getElementById("adminemail").value;
  if (em.indexOf("@") == -1 || em.indexOf(".") == -1) {
    alert("To get a password reminder sent by email, first type your email address into the login box");
    return;
  }

  // OK, try sending out a reminder to the given email.
  var eem = encodeURIComponent(em);

  var ret = postURL(rootpath+"php/sendAdminReminder.php", "e="+eem);
  
  var bits = ret.split(" ");
  if (bits[0]=="OK") {
    alert("Sent a password reminder email to "+em);
  }
  else if (bits[0]=="USR") {
    alert("No account found for email "+em);
  }
  else {
    alert("Problem sending reminder "+ret);
  }      
}


function checkGroupLoggedIn() {
  var t = new Date().getTime();
  var txt = fetchURL("php/grouploginping.php?f="+t);
  var bits = txt.split(" ");
  if (bits[0] == "OK") {
	var groupname = txt.substring(3);
	document.getElementById("registerbox").style.display = "none";
	document.getElementById("loginbox").style.display = "none";
	var logbody = document.getElementById("loggedinbody");

	var txt = "Logged in as admin for group: <a href='php/groupAdmin.php'>"+groupname+"</a> <br/><br/>  ";
	txt += "<table class='iconmenu'><tr><td><a href='php/groupAdmin.php'><img src='page-grey.png'/></a></td>";
    txt += "<td><a href='php/groupAdmin.php'>Group Admin Page</a></td></tr>";

    txt += "<tr><td><a href='php/logoutGroup.php'><img src='logout-grey.png'/></a></td>";
    txt += "<td><a href='php/logoutGroup.php'>Logout</a></td></tr></table>";
     
	
	divInnerHtml( logbody, txt);

	document.getElementById("loggedinbox").style.display = "block";
  }
  else {
	//	alert("not logged in");
  }
}


function zapInput(inp) {
  inp.onfocus = "";
  inp.value = "";
  inp.style.color = "#000000";
}


setTimeout(checkGroupLoggedIn, 50);

