
if(typeof ($.AWA) == "undefined") {
	$.AWA = Object;
}

$.AWA.validateForm = {
	
	fields:null,
	
	validate:function() {
		var validated = true;
		for(field in $.AWA.validateForm.fields) {	
			var passed = $.AWA.validateForm.checkField(field);
			if (!passed) validated = false;
		}		
		
		if($.AWA.validateForm.fields['email_address_confirm']) {
			if($('#email_address').val() != $('#email_address_confirm').val() && validated == true){
				var email_confirm_field_obj = $('#email_address_confirm');
				$.AWA.validateForm.clearfeedback(email_confirm_field_obj);
				$.AWA.validateForm.addfeedback("Emails do not match",email_confirm_field_obj);
				validated = false;
			}				
		}
		return validated;
	},
	
	checkField:function(field) {
		
		var feedback = $.AWA.validateForm.fields[field][0];
		var invalid = $.AWA.validateForm.fields[field][1];
		
		var error = 0;
		var email = (typeof(invalid) != 'undefined');
		var field_obj = document.getElementById(field);
		
		if(!$.AWA.validateForm.isFilled(field_obj)) {			
			error = feedback;
		} else {
			if(email) {
				if (!$.AWA.validateForm.isEmail(field_obj)) {
					error = invalid;
				}
			}
		}
		
		if (error != 0) {
			$.AWA.validateForm.clearfeedback(field_obj);
			$.AWA.validateForm.addfeedback(error,field_obj);
			return false;
		} else {
			$.AWA.validateForm.clearfeedback(field_obj);
			return true;
		}
		return false;
	},
	
	isFilled:function(x,i) {
		if(x.nodeName == "SELECT" ) {
			if(x.options[x.selectedIndex].text != "None") {
				return (x.options[x.selectedIndex].value == 0 ? false : true);
			} else {
			return true;
			}
		} else {
			return (x.value.length < 1 ? false : true);
		}		
	},
	
	isEmail:function(field) {
		var regEx = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
		return (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1 || !field.value.match(regEx) ? false : true);
	},
	
	addfeedback:function(feedback,field) {
		$(field).addClass('error').parent().addClass('error');
		$($(field)).parent().prepend('<div class="errorfeedback">' + feedback + '</div>');			
	},
	
	clearfeedback:function(field) {
		$(field).removeClass('error');
		$(field).parent().removeClass('error');
		$($(field)).parent().find('.errorfeedback').remove();
	},
}


$.AWA.formFieldFocus = {
	
	init: function(){
		
		//Toggle a class on the parent of a form field when giving and removing focus on the field
		$('form div input:text').each( function(intIndex){
			$(this).bind ('focus',function(){
				($(this).parent().addClass('focus'));
			});
			$(this).bind ('blur',function(){
				($(this).parent().removeClass('focus'));
			});
		});
		$('form div textarea').each( function(intIndex){
			$(this).bind ('focus',function(){
				($(this).parent().addClass('focus'));
			});
			$(this).bind ('blur',function(){
				($(this).parent().removeClass('focus'));
			});
		});
		$('form div select').each( function(intIndex){
			$(this).bind ('focus',function(){
				($(this).parent().addClass('focus'));
			});
			$(this).bind ('blur',function(){
				($(this).parent().removeClass('focus'));
			});
		});
		$('form div input:file').each( function(intIndex){
			$(this).bind ('focus',function(){
				($(this).parent().addClass('focus'));
			});
			$(this).bind ('blur',function(){
				($(this).parent().removeClass('focus'));
			});
		});
		
	} // End init
}

