$(document).ready(function(){

	// Text Box Default Text
	$("#search-prod input[type='text']")
		.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

	function f_formfieldset_click() {
		// Add Extra Filtersets Functionality
		$("input[name^='formfieldset']").parents("fieldset").find("legend span a").click(function(){
			// Clone current filterset and insert after current filterset
			$(this).parents("fieldset").clone().insertAfter($(this).parents("fieldset")).each(function(){
				// IMPORTANT IE CANT READ IN THE CURRENT NAME OR ID! (Had to change functionality so value is the current id)
				// Get Current filterset ID
				var flt_set_cur_id = $(this).find("input[name^='formfieldset']").attr("value");
				// Get Current Filterset ID (numeric)
				var flt_set_new_id = parseInt(flt_set_cur_id.replace("formfieldsetoscillator","").replace("formfieldsetcrystal",""));
				//alert(flt_set_new_id);
				// Set New Filterset ID
				flt_set_new_id = flt_set_cur_id.replace(flt_set_new_id,flt_set_new_id+1);
				// Rewrite names and IDs (remove current filterset ID if any, as this may be a clone of a clone!)
				$(this).find("input,select,textarea").each(function(){
				//$(this).find("input,select,textarea").each(function(){
					$(this).attr("name",flt_set_new_id + $(this).attr("name").replace(flt_set_cur_id,""));
					$(this).attr("id",flt_set_new_id + $(this).attr("id").replace(flt_set_cur_id,""));
					if ($(this).attr("value") == flt_set_cur_id) {
						$(this).attr("name",flt_set_new_id);
						$(this).attr("id",flt_set_new_id);
						$(this).attr("value",flt_set_new_id);
					}
				});
				// Rewrite Labels
				$(this).find("label").each(function(){
					$(this).attr("for",flt_set_new_id + $(this).attr("for").replace(flt_set_cur_id,""));
				});
			});
			// Move current filterset "Add Extra" link
			$(this).remove();
			// Setup Event Handlers for new clone
			f_formfieldset_click();
		});
	}
	f_formfieldset_click();












	// 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)
			
			$(".optgroup option").each(function(){
				if ($(this).attr("value") == $(this).parents("select").val()) {
					$(this).parent().parent().parent().html('<input type="text" name="' + $(this).parent().parent().attr("name") + '" id="' + $(this).parent().parent().attr("id") + '" value="' + $(this).text() + '" tabindex="' + $(this).parent().parent().attr("tabindex") + '" />');
				}
			});	
			
			$(".option option").each(function(){
				if ($(this).attr("value") == $(this).parents().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") + '" />');
				}
			});
			
			// Disable Elements (To prevent double submission)
			$("#status").html("Please Wait...");
			$("#form-request input, #form-request textarea, #form-request select").attr("readonly","readonly");
			$("#form-request legend a, #form-request input[type='submit']").hide();			
			
			// Post Form (set value to indicate response should be returned as JSON)
			$("#form").val("json");
			$.post("index.php",$("#form-request").serialize(),function(data){
				// Clear "Please Wait" Status
				$("#status").html("");
				// Process Response
				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' : '');
					});
					alert(v_msg);
					if (typeof data.Response[0].Error != 'undefined') {
						// Re-Enable Elements if error (To prevent double submission)
						$("#form-request input, #form-request textarea, #form-request select").removeAttr("readonly");
						$("#form-request legend a, #form-request input[type='submit']").show();	
					}
				}
			},"json");
		}
	});

});