
function LoginForm(id, usernameText, passwordText, loginText, logoutText, signupText, rememberText, resetText, emailText, scriptURL, signupURL, resetURL, emailURL, reloadPage, normalAttrib, smallAttrib, fieldAttrib) {
	this.id = id;
	this.usernameText = usernameText;
	this.passwordText = passwordText;
	this.loginText = loginText;
	this.logoutText = logoutText;
	this.signupText = signupText;
	this.rememberText = rememberText;
	this.resetText = resetText;
	this.emailText = emailText;
	this.scriptURL = scriptURL;
	this.signupURL = signupURL;
	this.resetURL = resetURL;
	this.emailURL = emailURL;
	this.reloadPage = reloadPage;
	this.normalAttrib = normalAttrib || "class=\"smaller\" style=\"color: #000;\"";
	this.smallAttrib = smallAttrib || "class=\"smallest\" style=\"color: #000;\"";
	this.fieldAttrib = fieldAttrib || "class=\"smaller\"";
	this.title = getCookie("title");
	this.emails = getCookie("emails");
	this.userId = getCookie("user_id");
}

p = LoginForm.prototype;

p.show = function() {
	var content = "";
	if (!this.isLoggedIn())
    	content = "<form name='login' style=\"padding: 0px; margin: 0px;\"><font " + this.normalAttrib + ">" +
														this.usernameText + ": </font><input type='text' name='email' size=10 " + this.fieldAttrib + "><font " + this.normalAttrib + "> " +
														this.passwordText + ": </font><input type='password' name='password' size=10 " + this.fieldAttrib + "><font " + this.normalAttrib + "> </font>" +
														"<input type='submit' onClick='loginObj.login(); return false;' value='" + this.loginText + "' " + this.fieldAttrib + ">" +
														"<font " + this.normalAttrib + "> </font>" +
														"<a href=\"" + this.signupURL + "\" " + this.smallAttrib + ">" + this.signupText + "</a> <a href=\"" + this.resetURL + "\" " + this.smallAttrib + ">" + this.resetText + "</a></form>";
	else
	{
    	content = "<table border=0 cellpadding=0 cellspacing=0><tr><td><font " + this.normalAttrib + ">" +
								this.title +
								" </font><a href='javascript: " + this.id + ".logout();' " + this.smallAttrib + ">" + this.logoutText + "</a>";
		if (this.emails > 0)
			content += '<br><a href="' + this.emailURL + '" ' + this.smallAttrib + '> ' + this.emails + ' ' + this.emailText + '</a>';
		content += '</td></tr></table>';
	}

	document.getElementById(this.id).innerHTML = content;
}

p.login = function() {
	loadData(this, this.scriptURL + "login.php?email=" + escape(document.forms['login'].email.value) + "&password=" + escape(document.forms['login'].password.value), true);
}

p.logout = function() {
	loadData(this, this.scriptURL + "logout.php?", false);
}

p.load = function(data, login) {

	if (!login) {
		this.userId = null;
		this.title = "";
		this.show();
		if (this.reloadPage)
			location.reload(true);
		return;
	}

	if (data.substring(0, 2) != "OK") {
		var errorText = data.substring(data.indexOf(' ') + 1, data.length);
		if (errorText.length > 100)
			errorText = errorText.substring(0, 100) + "...";
		alert(errorText);
		return;
	}
	var items = data.split(' ');
	this.userId = items[1];
	this.emails = items[2];
	this.title = decodeURIComponent(items[3]);

	this.show();

	if (this.reloadPage)
		location.reload(true);

}

p.isLoggedIn = function()
{
	return this.userId != null && this.userId != "";
}

loginJSLoaded = true;