function loadCategory(template_selector, category_id) {
	$.ajax({
		type: "POST",
  		contentType: "application/json; charset=utf-8",
  		url: "/services/query.asmx/ForCategory",
  		data: "{ 'categoryID' : '" + category_id + "' }",
  		dataType: "json",
		// on a successful request
		success: function(msg) {
			// hide the errors div
			$('#errors').hide();

			// fill the results div template with the json results
			$(template_selector).fillTemplate(msg);
			if($(template_selector).is(':hidden') ){
				$(template_selector).show();
			}

			// make the results visible
			$('#results').show();
    	}
	});
}
	
function addContainer() {
	// get the part id that was selected
	var partID = $(this).attr('id');
	
	var selected_parts = $('#selected_parts');
	var children = selected_parts.find('#' + partID);
	
	// test is a row for this part already exists
	if ( children.length > 0 ) {
			// if so increment the qty
			var qty_element = children.find('#qty');
			var qty = qty_element.html();
			qty_element.html(parseInt(qty) + 1);
	}
	else {
		//if not add a new row
		$('#selected_parts').append('<tr id="' + partID + '" class="selected_row"><td id="id" class="selected">' + partID + '</td><td id="qty" class="selected">1</td><td class="selected"><a href="#" id="' + partID + '" class="delete_selected">X</a></td></tr>');
	}
	
	// make sure the parts are visible
	if($('#selected_parts').is(':hidden')){
		$('#selected_parts').show();
	}
	
	toggleAddToCart();
	
	return false;
}

function removeContainer() {
	// get the part id that was selected
	var partID = $(this).attr('id');
	
	var selected_parts = $('#selected_parts');
	var children = selected_parts.find('#' + partID);
	
	if ( children.length > 0 ) {
		children.remove();
	}
	
	toggleAddToCart();
	
	return false;
}

function toggleAddToCart() {
	var children = $('#selected_parts').find('tr.selected_row');
	if ( children.length ) {
		if($('#addToCart').is(':hidden') ) {
			$('#addToCart').show();
		}
	}
	else {
		if($('#addToCart').is(':visible') ) {
			$('#addToCart').hide();
		}
	}
}

function addToCart() {
	var children = $('#selected_parts').find('tr.selected_row');
	
	// check to make sure we have some rows with part info.
	if ( children.length ) {
		var part_list = "?part=";
		
		// build the query string for the url
		for (var i = 0; i < children.length; i++) {
			var partID = $(children[i]).find('#id').html();
			var qty = $(children[i]).find('#qty').html();
			
			part_list = part_list + partID + ":" + qty + ",";
 		}

		// strip the last comma
		part_list = part_list.substr(0, part_list.length - 1);

		// set the url
		var url = "/services/addtocart.aspx" + part_list;
		
		window.top.location=(url);
	}
}

function setupDropdowns(prefix, categoryID) {
	getSubcategories(categoryID, function(categories) {
		if(categories.length > 0) {
			var parent_select = $('#' + prefix + '_select_1').get(0);
			var count = parent_select.options.length;
			for(var i=0; i<categories.length; i++) {
				parent_select.options[i + count] = new Option(categories[i].Name, categories[i].ID);
			}
			
			$('#' + prefix + '_select_1').change(function() {
				if($('#' + prefix + '_partlist_template').is(':visible') ) {
					$('#' + prefix + '_partlist_template').hide();
				}
				
				var selected_index = $(this).get(0).options.selectedIndex;
				var selected_categoryID = $(this).get(0).options[selected_index].value;
				
				var child_element = $('#' + prefix + '_select_2');
				child_element.children().remove();
				
				getSubcategories(selected_categoryID, function(subCategories) {
					if(subCategories.length > 0) {
						for(var c=0; c < subCategories.length; c++) {
							child_element.get(0).options[c] = new Option(subCategories[c].Name, subCategories[c].ID);
						}
						
						$('#' + prefix + '_go').click(function() {
							var child_select = child_element.get(0);
							var category_id = child_select.options[child_select.options.selectedIndex].value;
							loadCategory('#' + prefix + '_partlist_template', category_id);
						});
					}	
				});
				
			});
		}
	});
}

function getSubcategories(category_id, process) {
	var result = new Array();
	$.ajax({
		type: "POST",
  		contentType: "application/json; charset=utf-8",
  		url: "/services/query.asmx/ForSubCategory",
  		data: "{ 'categoryID' : '" + category_id + "' }",
  		dataType: "json",
		// on a successful request
		success: process
	});
	
	return result;
}


