var SCRIPT_NAME = '/v2_code/ajax_misc.php';
var creator_levels = new Array();

function B4UK_blinds_creator_type(s) {
	product_type_id = s.options[s.selectedIndex].value;
	// load in the available sub categories for this type
	$.ajax({
		url: SCRIPT_NAME,
		data: {
			c: "creator_get_categories_from_type",
			product_type_id: product_type_id
		},
		dataType: "json",
		success: function(json) {
			if(json.error) { alert(json.error); }
			else {
				// check the levels to see if we're displaying the correct
				// select lists, remove any that don't match
				if(creator_levels[0] != product_type_id) {
					$(("#creatorPID"+creator_levels[0])).remove();
					for(i=1; i<=creator_levels.length; i++) {
						$(("#creatorCat"+creator_levels[i])).remove();
					}
				}

				// generate the select list for the available categories
				html_str = '<p id="creatorPID'+product_type_id+'" class="creatorPID" >'+
					'<select id="pid'+product_type_id+'" name="pid'+product_type_id+'" size="1" '+
					'onchange="B4UK_blinds_creator_category(this, '+product_type_id+', false, 1)">'+
					'<option value="0">Please select...</option>';
				$.each(json.list, function(name,value) {
					html_str += '<option value="'+name+'">'+value+'</option>';
            	});
				html_str += '</select></p>';
				$("#creatorPID").after(html_str);

				// set the creator levels
				creator_levels[0] = product_type_id;
			}
		}
	});
}
function B4UK_blinds_creator_category(s, product_type_id, parent_id, level) {
	id = s.options[s.selectedIndex].value;
	next_level = level + 1;

	// load in the available sub categories for this category
	$.ajax({
		url: SCRIPT_NAME,
		data: {
			c: "creator_get_children_categories",
			id: id
		},
		dataType: "json",
		success: function(json) {
			if(json.error) { alert(json.error); }
			// if a list, means this category has sub categories, so display
			else if(json.list) {
				// check the levels to see if we're displaying the correct
				// select lists, remove any that don't match
				if(creator_levels[level] != id) {
					for(i=level; i<=creator_levels.length; i++) {
						$(("#creatorCat"+creator_levels[i])).remove();
					}
				}

				// generate the select list for the available categories
				html_str = '<p id="creatorCat'+id+'" class="creatorNewCat" >'+
					'<select id="cat'+id+'" name="cat'+id+'" size="1" '+
					'onchange="B4UK_blinds_creator_category(this, '+product_type_id+', '+id+', '+next_level+')">'+
					'<option value="0">Please select...</option>';
				$.each(json.list, function(name,value) {
					html_str += '<option value="'+name+'">'+value+'</option>';
            	});
				html_str += '</select></p>';
				if(parent_id) {
					$(("#creatorCat"+parent_id)).after(html_str);
				}
				else {
					$(("#creatorPID"+product_type_id)).after(html_str);
				}

				// set the creator levels
				creator_levels[level] = id;
			}
			// else if no sub categories, means this is a product, so go to it
			else if(json.url) {
				document.location.href = json.url;
			}
			else {
				alert('There has been an unknown error');
			}
		}
	});
}
