/*
 STYLES TO BE USED BY PLUGIN
 --------------------------------------
.error_msg{text-align: right; color: red; font-style: italic;}
.hide {display: none;}

#regiBody span.error_img, .error_img {vertical-align: middle; margin-top: 5px; margin-left: 2px; background: url(../images/global/ico_required_filed.png) no-repeat; display: block; width: 12px; height: 12px; float: left; border: none; padding: 0;}
#regiBody span.error_msg_image, .error_msg_image{vertical-align: middle; margin-top: 0; margin-left: 2px; background: url(../images/global/ico_required_filed.png) no-repeat; display: block; width: 12px; height: 12px; float: right;}

.error_input{background: #FFD9DD}
----------------------------------------
CLASSES FOR VALIDATION
----------------------------------------
required, numeric, email, password and password_confirm (has to match value of password class and only one allowed per page)

*/


jQuery.fn.validate_form  = function(error_message) { 
	valid = true;
	form_name = $(this).attr('name');
	form_id = $(this).attr('id');

	// remove all previous error indicator
	$('.error_img').remove();
	$('.error_input').removeClass('error_input');
	
	// check all required fields
	$(this).find('.required').each(
			function(index) {
			if ($(this).attr('name') != form_name && $(this).attr('id') != form_id) {
				// get element value
				var value = $(this).val();
				
				if (! value) {
					$(this).addClass('error_input');
					$(this).css('background', '#FFD9DD');
					
					$(this).parent("fieldset").append("<span class='error_img'>");
					
					// set the form as invalid
					valid = false;
				} else {
					$(this).css('background', '#FFFFFF');
				}
			}
		}
	);
	
	// check all email fields
	$(this).find('.email').each(
		function(index) {
			if ($(this).attr('name') != form_name && $(this).attr('id') != form_id && $(this).val()) {
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if (reg.test($(this).val()) == false) {
					$(this).addClass("error_input");
					$(this).css('background', '#FFD9DD');
					$(this).parent("fieldset").append("<span class='error_img'>");
					
					// set the form as invalid
					valid = false;
				}				
			}
		}
	);
	
	// check all numeric fields
	$(this).find('.numeric').each(
		function(index) {
			if ($(this).attr('name') != form_name && $(this).attr('id') != form_id && $(this).val()) {
			    var value = $($(this)).val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			    var intRegex = /^\d+$/;
			    if(!intRegex.test(value)) {
					$(this).addClass("error_input");
					$(this).css('background', '#FFD9DD');
					$(this).parent("fieldset").append("<span class='error_img'>");
					
					// set the form as invalid
					valid = false;
			    }
			}
		}
	);
	
	// handle passwords
	if($('.password').val() != '') {
		if ($('.password').val() != $('.password_confirm').val()) {
			$('.password_confirm').addClass("error_input");
			$('.password_confirm').parent("fieldset").append("<span class='error_img'>");
			
			// set form as invalid
			valid = false;
		}
	}
	

	// remove the error message to avoid duplicates or staying for valid entries
	$('.error_msg').remove();
	
	if (error_message != '') {
		if (!valid) {
			// add a new error message to the bottom of the form
			$(this).append('<p class="error_msg">' + error_message + '<span class="error_msg_image"></span></p>');
		} 
	}

	return valid;
	
}
