$(document).ready( function() {

	/* File modified by Danish Qamar 
	*/ 
	function validateField(field) {
		var error = false;
		
		// required fields
		if ($(field).attr("class").indexOf("required") != -1) {
			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;
		}
		
		/*// US Phone		
		if ($(field).attr("class").indexOf("phone") != -1) {
			
			if (!/^[2-9]\d{2}-\d{3}-\d{4}$/.test($(field).val()))
				error = true;
		}*/		
		
		if (error) {
			$(field).parent().addClass("error");
			$(field).parent().find(".errorField")[0].style.display = "block";
		} else {
			$(field).parent().removeClass("error");
			$(field).parent().find(".errorField")[0].style.display = "none";

		}
		
		return !error;
	}
	
	$("form").each( function() {
							 
		
		// handle submissions without filling any field
		$(this).submit(function () {
			return this.validate();
		});
		
		this.validate = function () {
			var validationError = false;
			// for each field test it
			$("input, select, textarea", this).each( function() {
				if ($(this).attr("class"))
				{
					if ( ($(this).attr("class").indexOf("validation"))>0)
					{
						
						if (!validateField(this))
						{
							validationError = true;
						}
					}
				}
			});
			return !validationError;
		};
		// handle changes on the fly
		$("input, select, textarea", this).each( function() {
			if ($(this).attr("class")) {
					if($(this).attr("class").indexOf("validation")!=-1)
					{
				$(this).blur( function() { validateField(this) } );
    			}
			}
		});
	});
});

