/*
 * Copyright 2009-2011 Okanagan Linux Solutions All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to Allegretto Publishing Ltd., 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sub-license,
 * and/or sell copies of the Software, subject to the following conditions:
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

function displayErrors(errors) {
	for (var i = 0; i < errors.length; i++) {		
		var error = errors[i];
		if (error.element) {			
			error.element.oldClassName = error.element.className;
			error.element.className = "error";			
		}
	}
}

function clearError(element) {
	if (element.oldClassName != null) {
		element.className = element.oldClassName;
	}
}

function validate() {
	var signupErrors = new Array();
	var error = new Error();
	
	error.element = document.getElementById("login");
	error.errors = new Array();
	var username = error.element.value;
	if (error.element.value.length <= 0) {
		error.errors.push("Login Missing");
		signupErrors.push(error);
	}
	
	if (username.match(/^\s/) || username.match(/\s$/)) {
		error.errors.push("Login does not allow leading or trailing spaces");
		signupErrors.push(error);
	}
		
	error = new Error();
	error.element = document.getElementById("password");
	error.errors = new Array();
	var password = error.element.value;
	if (error.element.value.length <= 0) {
		error.errors.push("Password Missing");
		signupErrors.push(error);		
	}
	
	error = new Error();
	error.element = document.getElementById("confirm");
	error.errors = new Array();
	var confirmText = error.element.value;
	if (error.element.value.length <= 0) {
		error.errors.push("Password Confirmation Missing");
	}
	
	if (confirmText != password) {
		error.errors.push("Passwords Don't Match");
	}
	
	if (error.errors.length > 0) {
		signupErrors.push(error);
	}
	
	error = new Error();
	error.element = document.getElementById("emailaddress");
	error.errors = new Array();
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	if (error.element.value.length <= 0 || !emailPattern.test(error.element.value)) {
		error.errors.push("Email Address Missing");
		signupErrors.push(error);
	}
	
	error = new Error();
	error.element = document.getElementById("firstname");
	error.errors = new Array();
	if (error.element.value.length <= 0) {
		error.errors.push("First Name Missing");
		signupErrors.push(error);
	}
	
	error = new Error();
	error.element = document.getElementById("lastname");
	error.errors = new Array();
	if (error.element.value.length <= 0) {
		error.errors.push("Last Name Missing");
		signupErrors.push(error);
	}
	if (signupErrors != null && signupErrors.length <= 0) {
		return true;
	}
	
	else {
		displayErrors(signupErrors);
		return false;
	}
}

