
/*******************************************************
 *	Form Validation Functions
 *******************************************************/

function validate_isNotEmpty(elem){
	var str = (typeof(elem.value)!='undefined') ? elem.value : elem.val();
	var reg_ex = /.+/;
	if(!str.match(reg_ex)){
		return false;
	}
	return true;
}

function validate_isNumber(elem, limit){
	var str = (typeof(elem.value)!='undefined') ? elem.value : elem.val();
	var reg_ex = /^[-]?\d*\.?\d*$/;
	if(!str.match(reg_ex) || (limit && str.length > limit)) {
			return false
	}
	return true;
}

function validate_isUsername(elem){
	var str = (typeof(elem.value)!='undefined') ? elem.value : elem.val();
	var reg_ex = /([a-zA-Z0-9]{6,15})$/;
	if(!str.match(reg_ex)){
		return false;
	}
	return true;
}

function validate_isEmailAddr(elem) {
	var str = (typeof(elem.value)!='undefined') ? elem.value : elem.val();
	var reg_ex = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if(!str.match(reg_ex)) {
		return false;
	}
	return true;
}



/**************************************
 *	Customized form validation
 **************************************/
 
function validateLogin() {
	var validForm = true;
	var focusField;
	var msg;
	
	if(!validate_isNotEmpty($('input#username'))){
		validForm = false;
		msg = "entering a username usually helps.";
		focusField = (typeof(focusField)!='undefined') ? focusField : $('input#username');
	} else if(!validate_isNotEmpty($('input#password'))){
		validForm = false;
		msg = "put your password in there!";
		focusField = (typeof(focusField)!='undefined') ? focusField : $('input#password');
	}//if()

	if(!validForm) {
		alert(msg);
		focusField.focus();
		return false;
	} else {
		return true;
	}//if()
	
}//validateLogin()
 
function validateResidentRegistration() {
	var validForm = true;
	var msg = "Please correct the following:"
	var focusField;
	
	if(!validate_isUsername($('input#username'))) {
		validForm = false;
		msg += "\n\t* you gotta enter an username with between 6 and 15 letters/numbers";
		focusField = (typeof(focusField)!='undefined') ? focusField : $('input#username');
	}
	
	if(!validate_isNotEmpty($('input#password')) || !validate_isNotEmpty($('input#password2'))){
		validForm = false;
		msg += "\n\t* you gotta put in some passwords";
		focusField = (typeof(focusField)!='undefined') ? focusField : $('input#password');
	} else if($('input#password').val() != $('input#password2').val()){
		validForm = false;
		msg += "\n\t* your passwords gotta match";
		focusField = (typeof(focusField)!='undefined') ? focusField : $('input#password');
	}
	
	if(!validate_isEmailAddr($('input#email'))){
		validForm = false;
		msg += "\n\t* you gotta enter an email address that is for real.";
		focusField = (typeof(focusField)!='undefined') ? focusField : $('input#email');
	}
	
	if(!validate_isNotEmpty($('input#human_img_code'))) {
		validForm = false;
		msg += "\n\t* you gotta enter that code that proves your human";
		focusField = (typeof(focusField)!='undefined') ? focusField : $('input#human_img_code');
	}
	
	if(!validForm) {
		alert(msg);
		focusField.focus();
		return false;
	} else {
		return true;
	}
	
} //validateResidentRegistration()





