$(document).ready(function(){	
	validate_input = function(){
		$(this).val(jQuery.trim($(this).attr("value")));
		var id = $(this).attr("id");
		var validation = $(this).attr("validation");
		var validation_message_div = "#" + id + "_validation";
		if ($(validation_message_div).length == 0)
			$(this).parents(".input_field").append("<div id=" + id + "_validation" + " class=validation_message></div>");
		
		var pass = true;
		
		if (validation != "none")
		{
			switch(validation)
			{
				case "required":
					if ($(this).attr("value") == "")
						pass = false;
					break;
				case "email":
					pass = checkemail($(this).attr("value"));
					break;
				case "confirm_password":
					var compare_object_id = "#" + remove_string_from_string(id, "confirm_", false);	//Get the main password field.
					if ($(compare_object_id).attr("value") != $(this).attr("value"))	//Compare two password fields.
						pass = false;
					break;
			}
			if (pass == false)
			{
				$(validation_message_div).html("<img src='images/icon_stop.gif' alt='Required' />");
				//$(this).css("background-color", "#FFFFD5");
			}
			else
			{
				$(validation_message_div).html("<img src='images/icon_accept.gif' alt='' />");
				//$(this).css("background-color", "#FFF");
				if ($(this).attr("id") == "username")
					$("#password").focus();
			}
		}
		else
		{
			$(validation_message_div).html("<img src='images/icon_accept.gif' alt='Available' />");
			$(this).css("background-color", "#FFF");
		}
	};
	
	validate_form = function(){
		var focus_object = "";
		var form_id = $(this).attr("id");
		$(':input', this).each(function() 
		{
			var type = $(this).attr("type");
			var tag = $(this).attr("tagName").toLowerCase(); // normalize case
			var id = $(this).attr("id");
			var validation = $(this).attr("validation");
				
			var validation_message_div = "#" + id + "_validation";	//Set the validation message div
			if ($(validation_message_div).length == 0)
				$(this).parents(".input_field").append("<div id=" + id + "_validation" + " class=validation_message></div>");
				
			switch(type)
			{
				case "text":
				case "password":
				case "textarea":
					if (validation != "none")
					{
						var pass = true;
						switch(validation)
						{
							case "required":
								if ($(this).attr("value") == "")
									pass = false;
								break;
							case "email":
								pass = checkemail($(this).attr("value"));
								break;
							case "confirm_password":
								var compare_object_id = "#" + remove_string_from_string(id, "confirm_", false);	//Get the main password field.
								if ($(compare_object_id).attr("value") != $(this).attr("value"))	//Compare two password fields.
									pass = false;
								break;
						}
						
						if (pass == false)	//$(this).attr("value") == "" || 
						{
							$(validation_message_div).html("<img src='images/icon_stop.gif' alt='Not Available' />");
							//$(this).css("background-color", "#FFFFD5");
							focus_object = (focus_object == "") ? "#" + id : focus_object;
						}
						else
						{
							$(validation_message_div).html("<img src='images/icon_accept.gif' alt='Available' />");
							//$(this).css("background-color", "#FFF");
						}
					}
					else
					{
						$(validation_message_div).html("<img src='images/icon_accept.gif' alt='Available' />");
						$(this).css("background-color", "#FFF");
					}
					break;
				case "file":
					obj = "#" + $(this).attr("id") + "_mask";
					if (validation != "none")
					{
						if ($(this).attr("value") == "")
						{
							$(validation_message_div).html("<img src='images/icon_stop.gif' alt='Not Available' style='margin-top: 10px;' />");
							//$(obj).css("background-color", "#FFFFD5");
							focus_object = (focus_object == "") ? obj : focus_object;
						}	
						else
						{
							$(validation_message_div).html("<img src='images/icon_accept.gif' alt='Available' style='margin-top: 10px;' />");
							//$(obj).css("background-color", "#FFF");
						}
					}
					else
					{
						$(validation_message_div).html("<img src='images/icon_accept.gif' alt='Available' style='margin-top: 10px;' />");
						//$(obj).css("background-color", "#FFF");
					}
					break;
			}
  		});
		if (focus_object != "")
		{
			$(focus_object).focus();
			$("#" + form_id + "_valid").attr("value", "0");
			return false;
		}
		else
		{
			$("#" + form_id + "_valid").attr("value", "1");
			return true;
		}
	};
});
