/** Class for handling login box and login cookies. */
var Login = {

	// Cookie names
	cookies: {username: 'JChemUser', password: 'JChemPass'},
	// Cookie expiration time in days
	expiry: 365,

	/**
	 * Display a login box if necessary, recieve authentication data and store them in browser cookies.
	 *
	 * @param elementId         Id of the element that contains login inputs.
	 * @param successCallback   Callback function which is executed after clicking on 'Login' button.
	 */
	doit: function(elementId, successCallback) {
		var username = Login.getCookie(Login.cookies.username);
		var password = Login.getCookie(Login.cookies.password);
		if (username != null && password != null) {
			successCallback(username, password);
			return;
		}
		document.getElementById(elementId).innerHTML = Login.generateForm();
		document.getElementById(elementId).style.display = 'block';
		$('#loginButton').click(function() {
			username = $('#username').val();
			password = $('#password').val();
			if (document.getElementById('storeInCookie').checked == true) {
				Login.setCookie(Login.cookies.username, username, Login.expiry);
				Login.setCookie(Login.cookies.password, password, Login.expiry);
			}
			document.getElementById(elementId).style.display = 'none';
			successCallback(username, password);
		});
	},

	/**
	 * Get a cookie value.
	 *
	 * @param cookieName   Name of the cookie.
	 * @return string, the value.
	 */
	getCookie: function(cookieName) {
		if (document.cookie.length > 0) {	
			var cookieStart = document.cookie.indexOf(cookieName+'=');
			if (cookieStart != -1) {
				cookieStart = cookieStart + cookieName.length + 1;
				cookieEnd = document.cookie.indexOf(";", cookieStart);
				if (cookieEnd == -1) {
					cookieEnd = document.cookie.length;
				}
				return unescape(document.cookie.substring(cookieStart, cookieEnd));
			} else if (document.cookie.indexOf(cookieName) != -1) {
				return '';
			}
		}
		return null;
	},

	/**
	 * Set the value of a cookie.
	 *
	 * @param cookieName   Name of the cookie.
	 * @param value        The value to be set.
	 * @param expireDays   Expiration time of the cookie in days.
	 */
	setCookie: function(cookieName, value, expireDays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expireDays);
		document.cookie = cookieName + "=" + escape(value) + ((expireDays == null) ? "" : ";expires="+exdate.toGMTString());
	},

	/**
	 * Remove login cookies to force user login next time.
	 */
	deleteCookies: function() {
		Login.setCookie(Login.cookies.username, null, -1);
		Login.setCookie(Login.cookies.password, null, -1);
	},

	/**
	 * Generate the html representation of the login form.
	 *
	 * @return  string of the result html.
	 */
	generateForm: function() {
		var html = '<p style="margin-top: 20px;">';
		html += '<label for="username" class="loginText">Username:</label>';
		html += '<input id="username" type="text" value="" class="loginInput" />';
		html += '</p>';
		html += '<p style="margin-top: 50px;">';
		html += '<label for="password" class="loginText">Password:</label>';
		html += '<input id="password" type="password" value="" class="loginInput" />';
		html += '</p>';
		html += '<p style="margin-top: 85px;">';
		html += '<input id="storeInCookie" type="checkbox" value="" />';
		html += '<label for="storeInCookie" style="margin-left: 5px;">Remember password</label>';
		html += '</p>';
		html += '<p class="loginButton"><input id="loginButton" type="button" value="Login" /></p>';
		return html;
	}
};
