/**
 * JavaScript by Jeffrey Ouma.
 * Copyright© 2008 artkenya.net Ltd.
 * All rights reserved.
 *
 * Script name: login.js
 * Description: Defines functions for the password recovery process.
 */
// Create the CPANEL.recover Namespace
CPANEL.recover = {};
CPANEL.recover.Validate = function() {
	try {
		if (!BLEEZ.form.field.Validate("email")) { return false; } else if (!BLEEZ.form.field.Validate("securitycode")) { return false; }
		return true;
	} catch (e) {
		errorHandler(e);
	}
};
CPANEL.recover.Submit = function() {
	try {
		if (CPANEL.recover.Validate()) {
			// Show progress
			CPANEL.recover.dialog = new BLEEZ.dialog.modal;
			CPANEL.recover.dialog.Show("status-dialog", "Status", "<p><strong>Please wait while your new login information is sent to you.</strong></p>", [{
				text: "Cancel",
				handler: function() {
					Connect.abort(CPANEL.recover.xhr);
					this.hide();
				}
			}]);
			var response = {
				timeout: 60000,
				success: function(x) {
					// Hide status dialog
					CPANEL.recover.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(function() {
							history.go(0);
						});
					} else {
						// The recovery was successful
						var dialog = new BLEEZ.dialog.modal;
						dialog.Show("success-dialog", "Success", "<p>Your login information has been successfully recovered and sent to you.<br>" +
						"You should receive an email soon with the subject:</p>" +
						"<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>\"CPANEL.NET: Recovered Login\".</strong></p>" +
						"<p>Once you receive this email, you can use the new login information to gain access to <strong>CPANEL.NET <sub>2.0</sub></strong></p>" +
						"<p><strong>Remember to change your password as soon as possible to something that will be easier for you to remember.</strong></p>", [{
							text: "Continue",
							handler: function() {
								this.hide();
							}
						}], "450px");
						dialog.box.hideEvent.subscribe(function() {
							location.href = "./";
						});
					}
				},
				failure: function(x) {
					// The request failed
					// Hide status dialog
					CPANEL.recover.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.recover.Submit);
							this.hideEvent.unsubscribe(CPANEL.recover.Clear);
							this.hideEvent.subscribe(CPANEL.recover.Submit);
							this.hide();
						}
					}, {
						text: "Cancel",
						handler: function() {
							this.hideEvent.unsubscribe(CPANEL.recover.Submit);
							this.hideEvent.unsubscribe(CPANEL.recover.Clear);
							this.hideEvent.subscribe(CPANEL.recover.Clear);
							this.hide();
						}
					}]);
					dialog.box.hideEvent.subscribe(CPANEL.recover.Clear);
				}
			};
			var elm, postVars;
			elm = Dom.get("email");
			postVars = "email=" + encodeURIComponent(elm.value);
			elm = Dom.get("dateofbirth");
			postVars += "&dateofbirth=" + encodeURIComponent(elm.getAttribute("timestamp"));
			elm = Dom.get("securitycode");
			postVars += "&securitycode=" + encodeURIComponent(elm.value);
			CPANEL.recover.xhr = Connect.asyncRequest("POST", "recover_submit.php", response, postVars);
		}
		//Event.preventDefault(e);
	} catch (e) {
		errorHandler(e);
	}
};
CPANEL.recover.Clear = function() {
	try {
		BLEEZ.form.field.Clear("email");
		BLEEZ.form.field.Clear("dateofbirth");
		BLEEZ.form.field.Clear("securitycode");
	} catch (e) {
		errorHandler(e);
	}
};
// Initialization function
CPANEL.recover.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.recover.Clear();
			BLEEZ.form.field.Init("email", "email");
			// Initialize Date of Birth Field
			elm = Dom.get("dateofbirth");
			elm.setAttribute("datetype", "dateofbirth");
			elm.setAttribute("icon", "dateofbirth-calendaricon");
			elm.setAttribute("container", "dateofbirth-calendar");
			BLEEZ.form.field.Init("dateofbirth", "date");
			BLEEZ.form.field.Init("securitycode", "securitycode");
			/*
			 * Submit Button
			 * =============
			 */
			Event.on("submitbutton", "click", function(e) {
				try {
					CPANEL.recover.Submit();
					Event.preventDefault(e);
				} catch (e) {
					errorHandler(e);
				}
			});
		}
	} catch (e) {
		errorHandler(e);
	}
};
// Execute initialization function after the document has rendered
try {
	Event.onDOMReady(CPANEL.recover.init);
} catch (e) {
	errorHandler(e);
}