var TendmeValidateForm = {
	init: function() {
		/** 
		 * validates text fields within a form
		 *
		 * needed id's ( <id> to be set by programmer ):
		 * form_validate_<id> -> id of the form for submit
		 * div_validate_present_<id> -> id of the div which holds the error message for not present (empty field)
		 * div_validate_invalid_<id> -> id of the div which holds the error message for invalid
		 * field_validate_<id> -> id of the field to be validated
		 * submit_validate_<id> -> id of the submit button
		 *
		 * class of button: btnS
		 */ 
		var ref = this;
		
		var submits = document.getElementsByClassName("btnS");
		
		for (var i = 0; i < submits.length; i++) {
			var index = submits[i].id.lastIndexOf('_');
			var firstPart = submits[i].id.substring(0, index);
			
			if (firstPart == 'submit_validate') {
				submits[i].onclick = function(){ref.validateField(this); return false;};
			}
		}
	},
	
	validateField: function(button) {
		var index = button.id.lastIndexOf('_');
		var submitId = button.id.substr(index + 1);
		
		var field = document.getElementById("field_validate_"+submitId);
		if (field) {
			var value = field.value;
			
			if (value == '') {
				document.getElementById("div_validate_present_"+submitId).style.display = "block";
				document.getElementById("field_validate_"+submitId).style.color = "#b0232a";
				document.getElementById("field_validate_"+submitId).style.borderColor = "#b0232a";
			}
			else {
				if (value.match(/\d{5}/)) {
					var form = document.getElementById("form_validate_"+submitId).submit();
				}
				else {
					document.getElementById("div_validate_invalid_"+submitId).style.display = "block";
					document.getElementById("field_validate_"+submitId).style.color = "#b0232a";
					document.getElementById("field_validate_"+submitId).style.borderColor = "#b0232a";
				}
			}
		}
		
	}
}

Tendme.addPageLoadModule(TendmeValidateForm);