$(document).ready( function() {
	 
	function validateField(field) {
		var error = false;
		
		// required fields
		if ($(field).attr("class").indexOf("required") != -1) {
			if (!$(field).val().length)
				error = true;
		}
		
		// required fieldspa
		if ($(field).attr("class").indexOf("requireda") != -1) {
			var theItem = document.getElementById('objloan_type').value;
			if (theItem == "Purchase") 
			document.getElementById('ex_loanfmtgbal').disabled = true;
			//if (!$(field).val().length)
				//error = true;
		}
		// numeric fields
		if ($(field).attr("class").indexOf("numeric") != -1) {
			if (!/^[0-9]*$/.test($(field).val()))
				error = true;
		}
		// characters (letters)
		if ($(field).attr("class").indexOf("character") != -1) {
			if (!/^[a-zA-ZÃ¶Ã–Ã¤Ã„Ã¥Ã…]*$/.test($(field).val()))
				error = true;
		}
		// emails
		if ($(field).attr("class").indexOf("email") != -1) {
			if (!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test($(field).val()))
				error = true;
		}
		
		if (error) {
			$(field).addClass("error");
		} else {
			$(field).removeClass("error");
		}
		
		return !error;
	}
	
	$("form").each( function() {
		// handle submissions without filling any field
		$(this).submit(function () {
			var validationError = false;
			// for each field test it
			$("input, select, textarea", this).each( function() {
				if ($(this).attr("class")) {
					if (!validateField(this))
						validationError = true;
						
				}
			});
			
			if (validationError==true) { alert("ERROR! Please check the fields in red background!"); }
			return !validationError;
		});
	
		// handle changes on the fly
		$("input, select, textarea", this).each( function() {
			if ($(this).attr("class")) {
				$(this).blur( function() { validateField(this) } );
    			}
		});
	});
});

