/**
 * Javascript Document by Jeffrey Ouma.
 * Copyright© 2008 artkenya.net Ltd.
 * All rights reserved.
 *
 * Form Function Library:
 * ======================
 * Event handlers and validations for generic form fields
 * such as name and email among others
 *
 */
// Create the BLEEZ.form namespace
BLEEZ.form = {};
BLEEZ.form.step = {
	parentElm: "",
	Show: function(elm) {
		// Get the step to be activated
		var activeElm = (Dom.getAncestorByClassName(elm, "step")) ? Dom.getAncestorByClassName(elm, "step") : Dom.getAncestorByClassName(elm, "active-step");
		// Get all currently active steps and deactivate them
		var elmArray = Dom.getElementsByClassName("active-step", "div", BLEEZ.form.step.parentElm);
		Dom.replaceClass(elmArray, "active-step", "step");
		if (activeElm) {
			// Activate the step if it was not active
			// This method allows for opening and closing of steps, i.e. one-at-a-time collapsible panel
			Dom.replaceClass(activeElm, "step", "active-step");
		}
	}
};
BLEEZ.form.error = {
	Show: function(elmId, message) {
		// Hide all visible notifications
		var parentElm = Dom.get(elmId + "-notification");
		var elmArray = parentElm.getElementsByTagName("p");
		//Dom.get(elmId+"-label").scrollIntoView(true);
		Dom.addClass(elmArray, "hide");
		// Append the "error" class to the field label to give it a red color
		Dom.addClass(elmId + "-label", "error");
		// Append the "input-error" class to the field to give it a red color
		// and border
		Dom.addClass(elmId, "input-error");
		// Show the error message
		Dom.get(elmId + "-error").innerHTML = message;
		Dom.setStyle(elmId + "-error", "opacity", "0");
		Dom.removeClass(elmId + "-error", "hide");
		var anim = new YAHOO.util.Anim(elmId + "-error", {
			opacity: {
				to: 1
			}
		}, 0.25, Easing.easeOut);
		anim.animate();
		// Hide all notification icons
		Dom.removeClass(elmId + "-notification", "status-notification");
		Dom.removeClass(elmId + "-notification", "success-notification");
		// Display the error notification icon
		Dom.addClass(elmId + "-notification", "error-notification");
		// End of BLEEZ.form.error.Show
	},
	Hide: function(elmId) {
		var elm;
		var anim = new YAHOO.util.Anim(elmId + "-error", {
			opacity: {
				to: 0
			}
		}, 0.25, Easing.easeIn);
		anim.animate();
		// Hide the error notification icon
		Dom.removeClass(elmId + "-notification", "error-notification");
		// Hide the error message
		if (elm = Dom.get(elmId + "-error")) {
			elm.innerHTML = "";
		}
		Dom.addClass(elmId + "-error", "hide");
		// Remove the "input-error" class from the field to return it to normal
		Dom.removeClass(elmId, "input-error");
		// Remove the "error" class from field label to return it to normal
		Dom.removeClass(elmId + "-label", "error");
		// End of BLEEZ.form.error.Hide
	}
};
BLEEZ.form.field = {
	Clear: function(elmId) {
		// Clear the field
		var elm = Dom.get(elmId);
		elm.value = "";
		// Hide the error message
		BLEEZ.form.error.Hide(elmId);
		BLEEZ.form.field.Blur(elmId);
		// End of BLEEZ.form.field.Clear
	},
	Focus: function(elmId) {
		if (Dom.hasClass(elmId + "-error", "hide")) {
			var parentElm = Dom.get(elmId + "-notification");
			var elmArray = parentElm.getElementsByTagName("p");
			Dom.addClass(elmArray, "hide");
			// Show the error message
			Dom.setStyle(elmId + "-hint", "opacity", "0");
			Dom.removeClass(elmId + "-hint", "hide");
			var anim = new YAHOO.util.Anim(elmId + "-hint", {
				opacity: {
					to: 1
				}
			}, 0.25, Easing.easeOut);
			anim.animate();
		}
		// End of BLEEZ.form.field.Focus
	},
	Blur: function(elmId) {
		var elm = Dom.get(elmId);
		if (!Dom.hasClass(elmId + "-hint", "hide")) { // The hint is visible

			var anim = new YAHOO.util.Anim(elmId + "-hint", {
				opacity: {
					to: 0
				}
			}, 0.25, Easing.easeIn);
			anim.animate();
			// Hide the hint
			Dom.addClass(elmId + "-hint", "hide");
		}
		// End of BLEEZ.form.field.Blur
	},
	Validate: function(elmId) {
		var msg, required = Dom.hasClass(elmId + "-label", "required");
		var elm = Dom.get(elmId);
		if (elm) {
			elm.value = BLEEZ.util.format.Trim(elm.value);
			if (required && !elm.value) {
				// The value is required but is empty or default
				BLEEZ.form.error.Show(elmId, "This information is required.");
				return false;
			}
			switch (elm.getAttribute("validate")) {
				case "name":
					if (elm.value && !BLEEZ.util.regX.Name(elm.value)) {
						// Invalid format
						msg = "Invalid name format. " +
						"Only letters, hyphens (-) and apostrophes (')<br>are allowed.";
					}
					break;
				case "email":
					if (elm.value && !BLEEZ.util.regX.Email(elm.value)) {
						// Invalid format
						msg = "This is not a valid email address.";
					}
					break;
				case "uridomain":
					if (elm.value && !BLEEZ.util.regX.URIDomain(elm.value)) {
						// Invalid format
						msg = "This is not a valid URL.";
					}
					break;
				case "decimal":
					if (elm.value && !BLEEZ.util.regX.Decimal(elm.value)) {
						// Invalid format
						msg = "This is not a valid decimal number.";
					}
					break;
				case "securitycode":
					if (BLEEZ.util.format.Trim(elm.value).length != 5) {
						msg = "Please enter the 5-character security code as shown.";
					} else {
						BLEEZ.form.securityCode.Validate(elmId);
						return BLEEZ.form.securityCode.valid;
					}
					break;
			}
			if (msg) {
				if (Dom.get(elmId + "-error").innerHTML != msg) {
					// Display an error message
					BLEEZ.form.error.Show(elmId, msg);
				}
			} else {
				BLEEZ.form.error.Hide(elmId);
				return true;
			}
		}
		// End of BLEEZ.form.field.Validate
	},
	Init: function(elmId, fieldType) {
		elm = Dom.get(elmId);
		if (!elm) { return; }
		if (fieldType) {
			elm.setAttribute("validate", fieldType);
		}
		Event.addFocusListener(elm, function() {
			try {
				BLEEZ.form.field.Focus(elmId);
			} catch (e) {
				errorHandler(e);
			}
		});
		Event.on(elm, "blur", function() {
			try {
				BLEEZ.form.field.Blur(elmId);
			} catch (e) {
				errorHandler(e);
			}
		});
		switch (fieldType) {
			case "securitycode":
				Event.on(elm, "change", function() {
					try {
						if (Dom.hasClass("securitycode", "input-error") ||
						this.value.length == 5) {
							BLEEZ.form.field.Validate("securitycode");
						}
					} catch (e) {
						errorHandler(e);
					}
				});
				Event.on(elm, "keyup", function(e) {
					try {
						if (BLEEZ.util.KeyCode(e.keyCode)) {
							if (Dom.hasClass("securitycode", "input-error") ||
							this.value.length == 5) {
								BLEEZ.form.field.Validate("securitycode");
							}
						}
					} catch (e) {
						errorHandler(e);
					}
				});
				break;
			case "date":
				var maxdate, mindate, startdate, title = Dom.get(elm.id + "-label").innerHTML;
				if (elm.getAttribute("datetype") == "dateofbirth") {
					maxdate = new Date();
					maxdate.setFullYear(maxdate.getFullYear() - 3);
					mindate = new Date();
					mindate.setFullYear(mindate.getFullYear() - 100);
					startdate = new Date();
					startdate.setFullYear(startdate.getFullYear() - 25);
				} else {
					maxdate = (elm.getAttribute("maxdate")) ? new Date(elm.getAttribute("maxdate")) : null;
					mindate = (elm.getAttribute("mindate")) ? new Date(elm.getAttribute("mindate")) : null;
					startdate = (elm.getAttribute("mindate")) ? new Date(elm.getAttribute("startdate")) : new Date();
				}
				BLEEZ.form.date.Calendar[elm.getAttribute("container")] = new YAHOO.widget.Calendar(elm.getAttribute("container"), {
					close: true,
					maxdate: maxdate,
					mindate: mindate,
					navigator: true,
					pagedate: startdate,
					selected: elm.value,
					start_weekday: 1,
					title: title
				});
				BLEEZ.form.date.Calendar[elm.getAttribute("container")].render();
				BLEEZ.form.date.Calendar[elm.getAttribute("container")].hide();
				Dom.removeClass(elm.getAttribute("container"), "hide");
				// Subscribe to the show event on Calendar cells
				BLEEZ.form.date.Calendar[elm.getAttribute("container")].showEvent.subscribe(function() {
					BLEEZ.form.field.Focus(elmId);
				});
				// Subscribe to the hide event on Calendar cells
				BLEEZ.form.date.Calendar[elm.getAttribute("container")].hideEvent.subscribe(function() {
					BLEEZ.form.field.Blur(elmId);
				});
				// Subscribe to the select event on Calendar cells
				BLEEZ.form.date.Calendar[elm.getAttribute("container")].selectEvent.subscribe(function(type, args) {
					BLEEZ.form.date.Rip(elmId, type, args);
					BLEEZ.form.field.Validate(elmId);
					BLEEZ.form.date.Calendar[Dom.get(elmId).getAttribute("container")].hide();
				});
				Event.on(elm.getAttribute("icon"), "click", function() {
					try {
						BLEEZ.form.date.Calendar[Dom.get(elmId).getAttribute("container")].show();
					} catch(e){
						errorHandler(e);
					}
				});
				Dom.removeClass(elm.getAttribute("icon"), "hide");
				elm.disabled = true;
				Event.on(elm, "change", function() {
					try {
						if (Dom.hasClass(elmId, "input-error")) {
							BLEEZ.form.field.Validate(elmId);
						}
					} catch (e) {
						errorHandler(e);
					}
				});
				Event.on(elm, "keyup", function(e) {
					try {
						if (BLEEZ.util.KeyCode(e.keyCode)) {
							if (Dom.hasClass(elmId, "input-error")) {
								BLEEZ.form.field.Validate(elmId);
							}
						}
					} catch (e) {
						errorHandler(e);
					}
				});
				break;
			// INTEGER	
			case "integer":
				Event.on(elm, "keydown", function(e) {
					try {
						if(e.keyCode == 32) {
							Event.preventDefault(e);
							
						} else if(e.shiftKey == true && e.keyCode < 48) {
							
						} else if(e.keyCode <= 57 || (e.keyCode >= 91 && e.keyCode <= 105) || (e.keyCode >= 112 && e.keyCode <= 145)) {
							
						} else {
							Event.preventDefault(e);
							
						}
					} catch (e) {
						errorHandler(e);
					}
				});
				Event.on(elm, "change", function() {
					try {
						if (Dom.hasClass(elmId, "input-error")) {
							BLEEZ.form.field.Validate(elmId);
						}
					} catch (e) {
						errorHandler(e);
					}
				});
				Event.on(elm, "keyup", function(e) {
					try {
						if (BLEEZ.util.KeyCode(e.keyCode)) {
							if (Dom.hasClass(elmId, "input-error")) {
								BLEEZ.form.field.Validate(elmId);
							}
						}
					} catch (e) {
						errorHandler(e);
					}
				});
				break;
			default:
				Event.on(elm, "change", function() {
					try {
						if (Dom.hasClass(elmId, "input-error")) {
							BLEEZ.form.field.Validate(elmId);
						}
					} catch (e) {
						errorHandler(e);
					}
				});
				Event.on(elm, "keyup", function(e) {
					try {
						if (BLEEZ.util.KeyCode(e.keyCode)) {
							if (Dom.hasClass(elmId, "input-error")) {
								BLEEZ.form.field.Validate(elmId);
							}
						}
					} catch (e) {
						errorHandler(e);
					}
				});
		}
		BLEEZ.form.field.Blur(elmId);
		// End of BLEEZ.form.field.Init
	}
};
BLEEZ.form.securityCode = {
	valid: false,
	Validate: function(elmId) {
		// Initialize variables
		var elm = Dom.get(elmId + "-status");
		var parentElm = Dom.get(elmId + "-notification");
		var elmArray = parentElm.getElementsByTagName("p");
		Dom.addClass(elmArray, "hide");
		elm.innerHTML = "Verifying security code.<br>Please wait...";
		Dom.setStyle(elm, "opacity", "0");
		Dom.removeClass(elm, "hide");
		var anim = new YAHOO.util.Anim(elm, {
			opacity: {
				to: 1
			}
		}, 0.25, Easing.easeOut);
		anim.animate();
		// Hide all notification icons
		Dom.removeClass(elmId + "-notification", "error-notification");
		Dom.removeClass(elmId + "-notification", "success-notification");
		// Display the status notification icon
		Dom.addClass(elmId + "-notification", "status-notification");
		elm = Dom.get(elmId);
		var response = {
			timeout: 60000,
			success: function(x) {
				var xmlResponse = x.responseXML.documentElement;
				var success = parseInt(BLEEZ.util.readXML.Data(xmlResponse, "success"));
				if (!success) {
					BLEEZ.form.error.Show(elmId, "Please enter the correct security code.");
					BLEEZ.form.securityCode.valid = false;
				} else {
					// The security code is valid
					BLEEZ.form.securityCode.valid = true;
					BLEEZ.form.error.Hide(elmId);
					parentElm = Dom.get(elmId + "-notification");
					elmArray = parentElm.getElementsByTagName("p");
					Dom.addClass(elmArray, "hide");
					elm = Dom.get(elmId + "-success");
					elm.innerHTML = "Correct";
					Dom.setStyle(elm, "opacity", "0");
					Dom.removeClass(elm, "hide");
					var anim = new YAHOO.util.Anim(elm, {
						opacity: {
							to: 1
						}
					}, 0.25, Easing.easeOut);
					anim.animate();
					// Hide all notification icons
					Dom.removeClass(elmId + "-notification", "error-notification");
					Dom.removeClass(elmId + "-notification", "status-notification");
					// Display the status notification icon
					Dom.addClass(elmId + "-notification", "success-notification");
				}
			},
			failure: function(x) {
				// The request failed
				// Show error dialog
				var dialog = new BLEEZ.dialog.modal;
				dialog.Show("error-dialog", "Problem Encountered", "There was a problem accessing the server: " + x.statusText, [{
					text: "OK",
					handler: function() {
						BLEEZ.dialog.modal.Cancel();
						CPANEL.login.Clear();
					}
				}]);
			}
		};
		var postVars = "code=" + BLEEZ.util.format.Trim(elm.value);
		var xhr = Connect.asyncRequest("POST", "../../Includes/security.php", response, postVars);
	}
};
BLEEZ.form.date = {
	Calendar: new Array(),
	Rip: function(elmId, type, args) {
		// Get the date components
		var dateArray = args[0], dt = dateArray[0];
		var y = dt[0], m = dt[1], d = dt[2];
		// Create the formatted date string
		var dateString = m + "/" + d + "/" + y;
		// Insert the formatted date into the text field
		var elm = Dom.get(elmId);
		elm.value = dateString;
		elm.setAttribute("timestamp", y + "-" + m + "-" + d);
		elm.setAttribute("startdate", y + "," + m + "," + d);
	}
};
