//Collapsable fieldsets.
$(document).ready(function() { 
	
	//When the user is finished completing the fields in an open fieldset, 
	//collapse it and open the next one.
	//live() is not supported for blur() yet, so this event has to be re-attached
	//every time it's fired.
	function toggleFieldsets() {
		
		$("table.formWrap tr.open tr.field:last").find("input, select, textarea")
		.blur( function() {
				
			//Get the next fieldset, if there is one.
			var nextFieldset = $(this).parents("tr.fieldset").next();
			
			//Perform the following actions if there is another fieldset.
			if (nextFieldset.length > 0) {
				
				//Close this fieldset if there is another fieldset after this one.
				$(this).parents("tr.fieldset").removeClass("open").addClass("collapsed");
				
				//Open the next fieldset, if there is one.
				nextFieldset.removeClass("collapsed").addClass("open");
				
				//Focus the user's cursor on the first form field in the next fieldset.
				nextFieldset.find("tr.field:first").find("input, select, textarea").focus();
			}
			
			//Re-attach this event.
			toggleFieldsets();			
		});
	}	
	
	//Open the first fieldset.
	$("table.formWrap tr.fieldset:first").removeClass("collapsed").addClass("open");
	
	//When a collapsed fieldset is clicked, open it.
	$("table.formWrap tr.collapsed legend").live("click", function(){
		$(this).parents("tr.fieldset").removeClass("collapsed").addClass("open");
	});
	
	//When an open fieldset is clicked, collapse it.
	$("table.formWrap tr.open legend").live("click", function(){
		$(this).parents("tr.fieldset").removeClass("open").addClass("collapsed");
	});
	
	//Toggle the fieldset shown to the user.
	toggleFieldsets();
	
});

//For form field add/edit form.
$(document).ready(function() { 
	
	if ($("form#formfieldedit").length || $("form#formfieldedit").length) {
		
		//As the label is entered, copy it to the name and id.
		$("input#label").blur( function() {
			
			$("input#name").attr("value", $(this).attr("value"));
			$("input#id").attr("value", $(this).attr("value"));
		});
		
		$("select#type").change( function() {
			
			//If a form field type was selected that requires options.
			if (
				($(this).attr("value") == "3") || 
				($(this).attr("value") == "4") || 
				($(this).attr("value") == "5")
			) {
				
				//Get the html for the options field and add the html to the page.
				$.ajax({
				    url: SETTINGS.webRoot + "admin/form/getformfieldoptionhtml",
				    type: 'GET',
				    success: function(html){
						//Insert the html after #description.
						$("textarea#description").parents("tr").after(html);
				    }
				});				
				
			//If a form field type was selected that does not require options.
			} else {
				//Remove the options field.
				$("textarea#formFieldOptions").parents("tr").remove();
			}
		});
	}
});
