jQuery(document).ready(function(){
	var thisID;
	var returnValue = true;
	// Place error messages after all mandatory field
	jQuery(".mandatory").each(function(){
		thisID = jQuery(this).attr("id");
		jQuery("#"+thisID).after(" <span class=\"errorMandatory_"+thisID+"\" style=\"display:none;color:#FF0000;\">This field is Mandatory</span>");
	});
	jQuery(".mandatory").blur(function(){							// When mandatory field unforcused
		thisID = jQuery(this).attr("id");							// Get ID of this
		returnValue = switchMandatoryError(thisID);
	});
	jQuery("form").submit(function(){								// When form is submitted
		 returnValue = true;
		jQuery(".mandatory").each(function(){						// Check all mandatory field
			thisID = jQuery(this).attr("id");						// Get ID of this
			returnValue = switchMandatoryError(thisID);
		})
		if(!returnValue){
			// If error is existed
			alert("Please fill all mandatory fields.");				// Popup error message
		}
		return returnValue;
	})
	function switchMandatoryError(thisID){
		returnValue = true;
		if(jQuery("#"+thisID).val()==""){
			jQuery("#"+thisID).css("background-color","#FFCCCC");	// Change background colour
			jQuery(".errorMandatory_"+thisID).show();				// Display error message
			returnValue = false;
		} else {
			jQuery("#"+thisID).css("background-color","");			// Delete background colour
			jQuery(".errorMandatory_"+thisID).hide();				// Hide error messate

		}
		return returnValue;
	}
});
