/**
 * JavaScript by Jeffrey Ouma.
 * Copyright© 2008 artkenya.net Ltd.
 * All rights reserved.
 *
 * Script name: login.js
 * Description: Defines functions for the user authentication sequence.
 */
// Create the CPANEL.login Namespace
CPANEL.login = {}
CPANEL.login.Validate = function() {
	try {
		if (!BLEEZ.form.field.Validate("email")) { return false; } else if (!BLEEZ.form.field.Validate("password")) { return false; }
		return true;
	} catch (e) {
		errorHandler(e)
	}
}
CPANEL.login.Submit = function() {
	try {
		if (CPANEL.login.Validate()) {
			// Show progress
			CPANEL.login.dialog = new BLEEZ.dialog.modal;
			CPANEL.login.dialog.Show("status-dialog", "Status", "<p><strong>Please wait while your login information is verified.</strong></p>", [{
				text: "Cancel",
				handler: function() {
					Connect.abort(CPANEL.login.xhr);
					this.hide();
				}
			}]);
			var response = {
				timeout: 60000,
				success: function(x) {
					// Hide status dialog
					CPANEL.login.dialog.box.hide();
					var xmlResponse = x.responseXML.documentElement;
					var success = parseInt(BLEEZ.util.readXML.Data(xmlResponse, "success"));
					if (!success) {
						// Invalid email/password
						var error = BLEEZ.util.readXML.Data(xmlResponse, "error");
						var dialog = new BLEEZ.dialog.modal;
						dialog.Show("error-dialog", "Problem Encountered", "<p><strong>" + error + "</strong></p>", [{
							text: "OK",
							handler: function() {
								this.hide();
							}
						}]);
						dialog.box.hideEvent.subscribe(CPANEL.login.Clear);
					} else {
						// The login was successful
						var locked = parseInt(BLEEZ.util.readXML.Data(xmlResponse, "locked"));
						var website = BLEEZ.util.readXML.Data(xmlResponse, "website");
						var firstname = BLEEZ.util.readXML.Data(xmlResponse, "firstname");
						var dialog = new BLEEZ.dialog.modal;
						// Check if the website is,locked
						if(locked) {
							dialog.Show("info-dialog", "Notice", "<p><strong>Sorry " + firstname + ", the " + website + " website has been temporarily locked for maintenance.</strong></p>" +
							"<p><strong>This measure is being taken to prevent you from losing any changes that you make while CPANEL.NET is being updated.</strong></p>" + 
							"<p><strong>We apologize for the inconvenience. Please try again after a few hours.</strong></p>", [{
								text: "OK",
								handler: function() {
									this.hide();
								}
							}], "450px");
							dialog.box.hideEvent.subscribe(function() {
								location.href = "logout.php";
							});
							
						} else {
							dialog.Show("success-dialog", "Success", "<p><strong>Welcome " + firstname + "</strong><br>You have successfully logged in to CPANEL.NET <sub>2.0</sub></p>" +
							"<p><strong>Remember to log out when you are finished.</strong></p>", [{
								text: "Continue",
								handler: function() {
									this.hide();
								}
							}]);
							dialog.box.hideEvent.subscribe(function() {
								location.href = "../";
							});
						}
					}
				},
				failure: function(x) {
					// The request failed
					// Hide status dialog
					CPANEL.login.dialog.box.hide();
					// Show error dialog
					var dialog = new BLEEZ.dialog.modal;
					dialog.Show("error-dialog", "Problem Encountered", "<p><strong>There was a problem accessing the server:\r\n\r\n" + x.statusText + "</strong></p>", [{
						text: "Retry",
						handler: function() {
							this.hideEvent.unsubscribe(CPANEL.login.Submit);
							this.hideEvent.unsubscribe(CPANEL.login.Clear);
							this.hideEvent.subscribe(CPANEL.login.Submit);
							this.hide();
						}
					}, {
						text: "Cancel",
						handler: function() {
							this.hideEvent.unsubscribe(CPANEL.login.Submit);
							this.hideEvent.unsubscribe(CPANEL.login.Clear);
							this.hideEvent.subscribe(CPANEL.login.Clear);
							this.hide();
						}
					}]);
					dialog.box.hideEvent.subscribe(CPANEL.login.Clear);
				}
			}
			var elm, postvars;
			elm = Dom.get("email");
			postvars = "email=" + encodeURIComponent(elm.value);
			elm = Dom.get("password");
			postvars += "&password=" + encodeURIComponent(elm.value);
			elm = Dom.get("rememberpassword");
			postvars += "&rememberpassword=";
			postvars += (elm.checked) ? "1" : "0";
			CPANEL.login.xhr = Connect.asyncRequest("POST", "login_submit.php", response, postvars);
		}
		//Event.preventDefault(e);
	} catch (e) {
		errorHandler(e)
	}
}
CPANEL.login.Clear = function() {
	try {
		BLEEZ.form.field.Clear("email");
		BLEEZ.form.field.Clear("password");
		var elm = Dom.get("rememberpassword");
		elm.checked = false;
	} catch (e) {
		errorHandler(e)
	}
}
// Initialization function
CPANEL.login.Init = function() {
	try {
		var elm = Dom.get("maincontentbox");
		// Continue only if the main content box is visible
		if (!Dom.hasClass(elm, "hide")) {
			// Clear all fields
			CPANEL.login.Clear();
			BLEEZ.form.field.Init("email", "email");
			BLEEZ.form.field.Init("password");
			/*
			 * Submit Button
			 * =============
			 */
			elm = Dom.get("submit-button");
			// Blur button links on focus
			Event.addListener(elm, "focus", function() {
				try {
					BLEEZ.util.Scrub(this);
				} catch (e) {
					errorHandler(e)
				}
			});
			Event.addListener(elm, "click", function() {
				try {
					CPANEL.login.Submit();
				} catch (e) {
					errorHandler(e)
				}
			});
		}
	} catch (e) {
		errorHandler(e)
	}
}
// Execute initialization function after the document has rendered
try {
	Event.onDOMReady(CPANEL.login.Init);
} catch (e) {
	errorHandler(e)
}
