$(document).ready(function(){

	// Text Box Default Text
	$("#form2")
		.focus(function(){
			if ($(this).attr("value").substr(0,5) == "Enter") {
				$(this).attr("value","");
			}
		})
		.blur(function() {
			($(this).attr("value") == "") ? $(this).attr("value","Enter...") : '';
		});

	// Add Initial Extra Filtersets Links
	$("input[name^='formfieldset']").parents("fieldset").find("legend span").append('&nbsp;&nbsp;&nbsp;<a>[+] Add Another</a>');
	// Had to move this functionality into a separate function because clone(true) would cause jscript errors in IE7 if you cloned a clone

	// Highlighting Current Inputs/Labels
	$("#form-request input,#form-request textarea,#form-request select")
		.focus(function(){
			$(this).parents(".field").addClass('hover');
		})
		.blur(function(){
			$(this).parents(".field").removeClass('hover');
				// Validate Single Form Element
				$(this).valid();
		});
	$("#form-request select").change(function(){
		// Validate Single Form Element
		$(this).valid();
	});

	// Main Form Validation/Submit
	$("#form-request").validate({
		highlight: function(element, errorClass) {
			$(element).parents(".field").addClass(errorClass);
		},
		unhighlight: function(element, errorClass) {
			$(element).parents(".field").removeClass(errorClass);
		},
		errorElement: "span",
		errorPlacement: function(error, element) {
			//error.appendTo("#main_content");
		},
		submitHandler: function(form) {
			
			// Clear Unentered Prompts
			$("input[value^='Enter']").each(function(){
				$(this).attr("value",$(this).val().replace("Enter...",""));
			});			

			// Convert Selects to Input and set the value to the Text value (instead of id)
			$("option").each(function(){
				if ($(this).attr("value") == $(this).parent().val()) {
					$(this).parent().parent().html('<input type="text" name="' + $(this).parent().attr("name") + '" id="' + $(this).parent().attr("id") + '" value="' + $(this).text() + '" tabindex="' + $(this).parent().attr("tabindex") + '" />');
				}
			});

			$("#form").val("json");
			$.post("index.php",$("#form-request").serialize(),function(data){

				$("#form-request input, #form-request textarea, #form-request select").attr("readonly","readonly");
				$("#form-request input[type='submit']").hide();	

				if (typeof data.Response[0].Message == 'undefined') {
					alert("Error: Unexpected Response");
				} else {

					// Response Message(s)
					var v_msg = "";
					$.each(data.Response,function(i,item) {
						v_msg += ((typeof item.Message != "undefined") ? item.Message + '\r\n' : '');
					});
				
					// Disable Elements (To prevent double submission)
					$("#status").html(v_msg);

				}	

			},"json");
			
			//form.submit();
		}
	});










});
