$(document).ready(function(){
	
	//Toggle shipping fields for a catalog item.
	if ($("#shippingFlag").length) {
		toggleCatalogShippingOptions();
		$("#shippingFlag").change( function() {
			toggleCatalogShippingOptions();
		});	
	}
});

//Open a popup window.
function wopen(url, name, w, h) {

	w = parseInt(w) + 40;
	h = parseInt(h) + 150;
	var win = window.open(url,name,'width=' + w + ', height=' + h + ', ' + 'location=no, menubar=no, ' +'status=no, toolbar=no, scrollbars=yes, resizable=yes');
	win.resizeTo(w, h);
	win.focus();
}

function removeFormField(divID) {
	
	// How many form fields are there?
	var numFormFields = $("#catalogformfield td.field div").length;
	
	// Delete this form field.
	if (numFormFields > 1) {
		var obj = $("#"+divID);
		obj.empty();
		obj.remove();
		
	// We can't delete the last form field, but we can clear out the values.
	} else if (numFormFields == 1) {
		$('div.formFieldContainer input').each( function() {
			if ($(this).is(':checkbox')) {
				$(this).attr('checked', false);
			} else {
				$(this).attr('value', '');
			}
		});
	}
	
}

function addCatalogFormField(obj) {
	
	/*Get the id for the next field.*/
	container = $("#catalogformfield");
	fieldsContainer = $("td.field", container);
	lastFormField = $("div:last", fieldsContainer);
	split = lastFormField.attr("id").split("-");
	lastFormFieldID = parseInt(split[1]);
	nextNum = lastFormFieldID + 1;
	
	/*Get the sort for the next field.*/
	lastSort = $("#fieldSort-"+lastFormFieldID).attr("value");
	nextSort = parseInt(lastSort) + 1;
	
	var url = SETTINGS.webRoot + "admin/catalog/getformfieldXML&i="+nextNum+"&sort="+nextSort;
	$.ajax({
	    url: url,
	    type: 'GET',
	    success: function(html){
	    	$("#catalogformfield td.field").append(html);			
	    }
	});

}

function displayCatalogFormFieldTypeOptions(obj) {

	obj = $("#"+(obj.id));
	formFieldType = obj.attr("value");
	
	/*Get the fieldID for this form field.*/
	split = obj.attr("id").split("-");
	fieldID = parseInt(split[1]);		
	
	/*We only add the first option here. All others are added with addFormFieldOption().*/
	optionID = 1;
	
	var url = SETTINGS.webRoot + "admin/catalog/getformfieldtypeXML&type="+formFieldType+"&fieldID="+fieldID+"&optionID="+optionID;					
	
	$.ajax({
	    url: url,
	    type: 'GET',
	    success: function(html){
	    	obj.parent().parent().parent().append(html);
	    }
	});
}
function addCatalogFormFieldOption(divID, type) {

	//Remove the add button. We will create a new one in the new 
	//HTML that is to be appended in just a moment. This makes sure 
	//that only the last option has an add button.
	$("span.addOption").remove();

	/*Get the fieldID and optionID.*/
	split = divID.split("fieldID-")[1];
	splitAgain = split.split("-optionID-");
	fieldID = splitAgain[0];
	optionID = parseInt(splitAgain[1])+1;	
	
	var url = SETTINGS.webRoot + "admin/catalog/getformfieldtypeXML&type="+type+"&fieldID="+fieldID+"&optionID="+optionID;					

	$.ajax({
	    url: url,
	    type: 'GET',
	    success: function(html){
			$("#"+divID).parent().append(html);
	    }
	});	

}

function toggleCatalogShippingOptions() {
	
	//This item is shippable.
	if ($('#shippingFlag').attr("value") == "1") {
		$("#pounds").parents("tr.field").removeClass("hidden");;
		$("#ounces").parents("tr.field").removeClass("hidden");;
		$("#width").parents("tr.field").removeClass("hidden");;
		$("#length").parents("tr.field").removeClass("hidden");;
		$("#height").parents("tr.field").removeClass("hidden");;
	
	//This item is not shippable.
	} else {
		$("#pounds").parents("tr.field").addClass("hidden");
		$("#ounces").parents("tr.field").addClass("hidden");
		$("#width").parents("tr.field").addClass("hidden");
		$("#length").parents("tr.field").addClass("hidden");
		$("#height").parents("tr.field").addClass("hidden");
	}
}
