var opmode = 1;
function leftTrim(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	return sString;
}
function rightTrim(sString) {
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
function trimAll(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
var locked = false;
function showlogin() {
	$("choice1area").style.display = "block";
	$("choice2area").style.display = "none";
	$("choice3area").style.display = "none";
	if (locked) {
		$("captchaarea").style.display = "block";
	} else {
		$("captchaarea").style.display = "none";
	}
	$("password").focus();
	opmode=1;
}
function showforgot() {
	$("choice1area").style.display = "none";
	$("choice2area").style.display = "block";
	$("choice3area").style.display = "none";
	$("captchaarea").style.display = "block";
	$("captcha").focus();
	opmode=2;
}
function showcreate() {
	$("choice1area").style.display = "none";
	$("choice2area").style.display = "none";
	$("choice3area").style.display = "block";
	$("captchaarea").style.display = "block";
	$("captcha").focus();
	opmode=3;
}
function dologin() {
	clear();
	$("email").value = trimAll($("email").value.toLowerCase());
	if (! valid($("email"),"email","your e-mail address")) { return null; }
	if ($("email").value == '') { $("msg9").style.display = "block"; return null; }
	new Ajax.Request("/include/ajax-login.php", {
		parameters: {email:$("email").value, pw:$("password").value, mode:"login", captcha:$("captcha").value},
		onSuccess: function(transport) {
			var resp = transport.responseText;
			var data;
			data = resp.split("|");
			switch (data[0]) {
				case "OK":
					createCookie("user",data[1],30);
					if (standalone) {
						window.location = "/mylibrary.php";
					} else {
						if (file == 'vitc') {
							window.location = '/'+chapdata+'.php';
						}
						else {
							window.location = "/register.php?pubcode=" + pubcode + "&file=" + file + "&data=" + chapdata + "&pubid=" + id;
						}
					}
					break;
				case "NC":
					createCookie("user",data[1],30);
					if (standalone) {
						window.location = "/confirm.php";
					} else {
						window.location = "/confirm.php?data="+confirmdata;
					}
					break;
				case "UN":
					$("msg11").style.display = "block";
					captchareset();					
					break;
				case "PW":
					$("msg3").style.display = "block";
					$("captchaarea").style.display = "block";
					captchareset();
				case "AL":
					$("msg4").style.display = "block";
					$("captchaarea").style.display = "block";
					captchareset();
					break;
			}
		},
		onFailure: function() {
			alert("Communication with server failed; please try again.");
		}
	});
}
function doforgot() {
	clear();
	$("email").value = trimAll($("email").value.toLowerCase());
	if ($("email").value == '') { $("msg9").style.display = "block"; return null; }
	if ($("captcha").value =='') { $("msg10").style.display = "block"; return null; }
	if (! valid($("email"),"email","your e-mail address")) { return null; }
	new Ajax.Request("/include/ajax-login.php", {
		parameters: {email:$("email").value, mode:"forgot", captcha:$("captcha").value},
		onSuccess: function(transport) {
			var resp = transport.responseText;
			switch (resp) {
				case "RD":
					window.location = "http://reminder.realtimepublishers.com/sendemail.asp?pubid=0&email="+$("email").value;
				case "OK":
					$("msg1").style.display = "block";
					$("captchaarea").style.display = "none";
					$("choice1area").style.display = "block";
					$("choice2area").style.display = "none";
					$("choice1").click();
					captchareset();
					break;
				case "UN":
					$("msg2").style.display = "block";
					captchareset();
					break;
				case "SC":
					$("msg6").style.display = "block";
					captchareset();
					break;
			}
		},
		onFailure: function() {
			alert("Communication with server failed; please try again.");
		}
	});
}
function docreate() {
	clear();
	$("email").value = trimAll($("email").value.toLowerCase());
	if ($("email").value == '') { $("msg9").style.display = "block"; return null; }
	if ($("pw").value =='') { $("msg10").style.display = "block"; return null; }
	if ($("pw2").value =='') { $("msg10").style.display = "block"; return null; }
	if ($("pw").value != $("pw2").value) { $("msg8").style.display = "block"; return null; }
	if ($("captcha").value =='') { $("msg10").style.display = "block"; return null; }
	if (! valid($("email"),"email","your e-mail address")) { return null; }
	new Ajax.Request("/include/ajax-login.php", {
		parameters: {email:$("email").value, password:$("pw").value, mode:"create", captcha:$("captcha").value, confirmdata:confirmdata},
		onSuccess: function(transport) {
			var resp = transport.responseText;
			var data = resp.split("|");
			switch (data[0]) {
				case "OK":
					createCookie("user",data[1],30);
					alert("Your account has been created.  However, your account will not be active until it has been confirmed.  We have sent a confirmation email to you; please follow the instructions in the email to continue.");
					if (standalone) {
						window.location = "/confirm.php";	
					} else {
						window.location = "/confirm.php?from=li&data="+confirmdata;
					}
					break;
				case "UN":
					$("msg7").style.display = "block";
					captchareset();
					break;
				case "SC":
					$("msg6").style.display = "block";
					captchareset();
					break;
			}
		},
		onFailure: function() {
			alert("Communication with server failed; please try again.");
		}
	});
}
function clear() {
	$("msg1").style.display = "none";
	$("msg2").style.display = "none";
	$("msg3").style.display = "none";
	$("msg4").style.display = "none";
	$("msg5").style.display = "none";
	$("msg6").style.display = "none";
	$("msg7").style.display = "none";
	$("msg8").style.display = "none";
	$("msg9").style.display = "none";
	$("msg10").style.display = "none";
	$("msg11").style.display = "none";
}
function captchareset() {
	var now = new Date();
	var seed = String(now.getSeconds()) + String(now.getMilliseconds());
	$("captchapic").src = "/include/captcha.php?"+seed;
	$("captcha").value = "";
}
