/**************************************************
 SI_isValidEmail()
 
 Returns true if the value passed is a valid email 
 address. Duh.
 **************************************************/
function SI_isValidEmail(email) { return (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)?true:false; }

/**************************************************
 SI_empty()
 
 Similar to the php function empty()
 Returns true if val contains an empty string
 or contains only whitespace.
 ie. spaces or returns
 **************************************************/
function SI_empty(val) { var empty = /^\s*$/; return empty.test(val); }

/**************************************************
 SI_handleErrors/SI_clearErrors()
 
 Used to present the user feedback.
 The first index of the SI_errors array is presented
 in paragraph form. The remaining indexes are output
 in an unordered list.
 **************************************************/
var SI_errors = new Array();
function SI_handleErrors() {
	var d = document;
//	errorMsg = '<p>'+SI_errors[0]+'<'+'/p><ul class="error">';
	errorMsg = '<ul class="errors"><h4>Please check the following fields and make sure they are complete.</h4>';
	for (var i=1; i<SI_errors.length; i++) {
		errorMsg += '<li>'+SI_errors[i]+'<'+'/li>';
		}
	errorMsg += '<'+'/ul>';
	d.getElementById('container-errors').innerHTML = errorMsg;

	SI_clearErrors();

	}

function SI_clearErrors() { SI_errors = new Array(); }


 
//spinner_on  = new Image(16,16); spinner_on.src  = "/images/spinner.gif";
//spinner_off = new Image(16,16); spinner_off.src = "/images/spacer.gif";

alert_on  = new Image(15,15); alert_on.src = "/images/alert.gif";
alert_off = new Image(15,15); alert_off.src = "/images/required.gif";




/**************************************************
Puts the cursor in the first field encountered
with an error
 **************************************************/
var error_fields = new Array();
function go_to_error(val) {
	var d = document;
	
	d.getElementById(error_fields[0]).focus();
	
	clear_error_fields();
}

function clear_error_fields() { error_fields = new Array(); }



function val_login(f) {
	var d = document;
	if (!d.getElementById) return true;
	
	var email = f.email.value;
	var password = f.password.value;

	if (SI_empty(email)) {
		SI_errors[SI_errors.length] = 'Please enter an email address.';
		d.getElementById('alert-email').src = alert_on.src;	
		error_fields[error_fields.length] = 'email';
	} else if (!SI_isValidEmail(email)) {
		SI_errors[SI_errors.length] = 'Your email address doesn&#8217;t appear to be in a valid format.';
		d.getElementById('alert-email').src = alert_on.src;
		error_fields[error_fields.length] = 'email';
	} else {
		d.getElementById('alert-email').src = alert_off.src;
	}


	if (SI_empty(password)) {
		SI_errors[SI_errors.length] = 'Please enter your password.';
		d.getElementById('alert-password').src = alert_on.src;
		error_fields[error_fields.length] = 'password';
	} else {
		d.getElementById('alert-password').src = alert_off.src;
	}


if (!SI_errors.length) {
		SI_clearErrors();
		return true;
	} else {
		var failed = [''];
		SI_errors = failed.concat(SI_errors);
		SI_handleErrors();
		go_to_error();
		return false;
		}


}



/**************************************************
 Called from the registration form's onsubmit handler
 and makes sure that email, first name, last name, 
 and password provided are complete and valid.
 **************************************************/
function val_register(f) {
	var d = document;
	if (!d.getElementById) return true;
	
	var fname = f.fname.value;
	var lname = f.lname.value;
	var email = f.email.value;


	if (SI_empty(fname)) {
		SI_errors[SI_errors.length] = 'First name.';
		d.getElementById('alert-fname').src = alert_on.src;
		error_fields[error_fields.length] = 'fname';
	} else {
		d.getElementById('alert-fname').src = alert_off.src;
	}
	
	if (SI_empty(lname)) {
		SI_errors[SI_errors.length] = 'Last name.';
		d.getElementById('alert-lname').src = alert_on.src;
		error_fields[error_fields.length] = 'lname';
	} else {
		d.getElementById('alert-lname').src = alert_off.src;
	}
	
	if (SI_empty(email)) {
		SI_errors[SI_errors.length] = 'Email address.';
		d.getElementById('alert-email').src = alert_on.src;	
		error_fields[error_fields.length] = 'email';
	} else if (!SI_isValidEmail(email)) {
		SI_errors[SI_errors.length] = 'Your email address doesn&#8217;t appear to be in a valid format.';
		d.getElementById('alert-email').src = alert_on.src;
		error_fields[error_fields.length] = 'email';
	} else {
		d.getElementById('alert-email').src = alert_off.src;
	}

if (!SI_errors.length) {
		SI_clearErrors();
		return true;
	} else {
		var failed = [''];
		SI_errors = failed.concat(SI_errors);
		SI_handleErrors();
		go_to_error();
		return false;
		}
//	return this.val_shipping(f);
}


/**************************************************
 Called from the registration form's onsubmit handler
 and makes sure that email, first name, last name, 
 and password provided are complete and valid.
 **************************************************/
function val_myinfo(f) {
	var d = document;
	if (!d.getElementById) return true;
	
	var email = f.email.value;
	var password1 = f.password1.value;
	var password2 = f.password2.value;


	if (SI_empty(email)) {
		SI_errors[SI_errors.length] = 'Email address.';
		d.getElementById('alert-email').src = alert_on.src;	
		error_fields[error_fields.length] = 'email';
	} else if (!SI_isValidEmail(email)) {
		SI_errors[SI_errors.length] = 'Your email address doesn&#8217;t appear to be in a valid format.';
		d.getElementById('alert-email').src = alert_on.src;
		error_fields[error_fields.length] = 'email';
	} else {
		d.getElementById('alert-email').src = alert_off.src;
	}


	if (SI_empty(password1)) {
		SI_errors[SI_errors.length] = 'Enter a password.';
		d.getElementById('alert-password1').src = alert_on.src;
		error_fields[error_fields.length] = 'password1';
	} else if ((!SI_empty(password1)) && (SI_empty(password2))) {
		SI_errors[SI_errors.length] = 'Confirm your password.';
		d.getElementById('alert-password1').src = alert_off.src;
		d.getElementById('alert-password2').src = alert_on.src;
		error_fields[error_fields.length] = 'password2';
	} else if ((!SI_empty(password1)) && (!SI_empty(password2)) && (password1 != password2)) {
		SI_errors[SI_errors.length] = 'Passwords do not match.';
		d.getElementById('alert-password1').src = alert_off.src;
		d.getElementById('alert-password2').src = alert_on.src;
		error_fields[error_fields.length] = 'password2';
	} else {
		d.getElementById('alert-password1').src = alert_off.src;
		d.getElementById('alert-password2').src = alert_off.src;
	}


if (!SI_errors.length) {
		SI_clearErrors();
		return true;
	} else {
		var failed = [''];
		SI_errors = failed.concat(SI_errors);
		SI_handleErrors();
		go_to_error();
		return false;
		}
//	return this.val_shipping(f);
}










/**************************************************
 Called from the forgot password form's onsubmit handler and
 makes sure that email provided is complete and valid.
 **************************************************/
function SI_prescreenForgotPassword(f) {
	var d = document;
	if (!d.getElementById) return true;
	
	var e = f.email.value;
		
	
	if (SI_empty(e)) {
		SI_errors[SI_errors.length] = 'Email address.';
		d.getElementById('alert-email').src = alert_on.src;
		error_fields[error_fields.length] = 'email';
	
	} else if (!SI_isValidEmail(e)) {
		SI_errors[SI_errors.length] = 'Your email address doesn&#8217;t appear to be in a valid format.';
		d.getElementById('alert-email').src = alert_on.src;
		error_fields[error_fields.length] = 'email';
	} else {
		d.getElementById('alert-email').src = alert_off.src;
	}	

	if (!SI_errors.length) {
		SI_clearErrors();
		return true;
		}
	else {
		var failed = [''];
		SI_errors = failed.concat(SI_errors);
		SI_handleErrors();
		go_to_error();

		return false;
		}
	}
