function clickedcat1(catid, category) {
	var listsize = $('#cat_level_1').attr('size');

	values = "catid="+catid+"&level=2&listsize="+listsize;
	$.ajax({
        type:'GET',
        url:'/includes/html/forms/load_list_items.php',
        dataType:'html',
        data:values,
        processData:true,
        timeout:60000,
        success:function(html) { 
        	$("#subcats").html(html); 
        	$("#subsubcats").html(''); 
        	$("#cat_breadcrumb").html(''); 
        	$("#cat_breadcrumb").append(category); 
        	cat2bind(); 
        	cat3bind(); 
       }
     });
}

function cat1bind() { 
	$('#cat_level_1').click(function() {
				var catid = $('#cat_level_1 option:selected').val()
				var category = $('#cat_level_1 option:selected').html();
				var listsize = $('#cat_level_1').attr('size');
				
				values = "catid="+catid+"&level=2&listsize="+listsize;
				$.ajax({
				       type:'GET',
				       url:'/includes/html/forms/load_list_items.php',
				       dataType:'html',
				       data:values,
				       processData:true,
				       timeout:60000,
				       success:function(html) { 
				       	$("#subcats").html(html); 
				       	$("#subsubcats").html(''); 
				       	$("#cat_breadcrumb").html(''); 
				       	$("#cat_breadcrumb").append(category); 
				       	cat2bind(); 
				       	cat3bind(); 
				      }
				});
				
			});
}

function cat2bind() { 
	$('#cat_level_2').click(function() {
				var catid = $('#cat_level_2 option:selected').val()
				var category = $('#cat_level_2 option:selected').html();
				var listsize = $('#cat_level_1').attr('size');
				
				values = "catid="+catid+"&level=3&listsize="+listsize;
				var thisoptclass = ($('#cat_level_2 option:selected').attr('class'));
				if (thisoptclass == 'cat2') {
					$.ajax({
	                       type:'GET',
	                       url:'/includes/html/forms/load_list_items.php',
	                       dataType:'html',
	                       data:values,
	                       processData:true,
	                       timeout:60000,
	                       success:function(html) { 
	                        var catone = $('#cat_level_1 option:selected').html();
	                       
	                       	var newbc = catone+category;
	
	                       	$("#cat_breadcrumb").html(''); 
	                       	$("#cat_breadcrumb").append(newbc); 
	                       	$("#subsubcats").html(html); 
	                       	cat3bind();
	                    }
	               });
	               } else {
	                    var catone = $('#cat_level_1 option:selected').html();
                       	var newbc = catone+category;

                       	$("#cat_breadcrumb").html(''); 
                       	$("#cat_breadcrumb").append(newbc); 
                       	$("#subsubcats").html(''); 
                       	cat3bind();
	               }
		}
	);

} 

function cat3bind() { 
	$('#cat_level_3').click(function() {
				var category = $('#cat_level_3 option:selected').html();
                var catone = $('#cat_level_1 option:selected').html();
                var cattwo = $('#cat_level_2 option:selected').html();
                var newbc = catone+cattwo+category;
                $("#cat_breadcrumb").html('');
                $("#cat_breadcrumb").append(newbc); 
			});
} 

function submitCategorizeForm() {
	var catone = $('#cat_level_1 option:selected').html();
	var cattwo = $('#cat_level_2 option:selected').html();
	var catthree = $('#cat_level_3 option:selected').html();
	var catoneid = $('#cat_level_1 option:selected').val();
	var cattwoid = $('#cat_level_2 option:selected').val();
	var catthreeid = $('#cat_level_3 option:selected').val();
	
//	alert('Cat 1: '+catone+' Cat 2: '+cattwo+' Cat 3: '+catthree);
//	alert('Cat 1 id: '+catoneid+' Cat 2 id: '+cattwoid+' Cat 3 id: '+catthreeid);
	if (catthreeid != null)
	{
		var catid = catthreeid;
//		alert('ID to save '+catthreeid);
	} else if (cattwoid != null) {
		var catid = cattwoid;
//		alert('ID to save '+cattwoid);	
	} else if (catoneid != null) {
		var catid = catoneid;	
//		alert('ID to save '+catoneid);
	} else {
		//they did not choose categories, but they did hit submit.  consider it a "skip"
		skipCategorizeForm();
	}
	
	if (catid != null)
	{
		var questionid = $('#question_id').attr('value');
		values = "catid="+catid+"&questionid="+questionid;
	//	alert (values);
	
		$.ajax({
	         type:'GET',
	         url:'/includes/php/save_question_category.php',
	         dataType:'json',
	         data:values,
	         processData:true,
	         timeout:60000,
	         success:function(data) { if (data.error == 0) { } else { alert("Something went wrong."); } },
	         error:function() { alert("Something went wrong."); }
         }); 
         
         closeThickBox();
	}	
}

function skipCategorizeForm() {
/*** if user chooses to skip, save user_category as 0, that way we can tell when they skipped over it ***/
		var questionid = $('#question_id').attr('value');
		values = "catid=0&questionid="+questionid;
	//	alert (values);
	
		$.ajax({
	         type:'GET',
	         url:'/includes/php/save_question_category.php',
	         dataType:'json',
	         data:values,
	         processData:true,
	         timeout:60000,
	         success:function(data) { if (data.error == 0) { } else { alert("Something went wrong."); } },
	         error:function() { alert("Something went wrong."); }
         }); 
         
         closeThickBox();
}

function closeThickBox()
{
	alert('Your question will be reviewed soon. Thank you.');
	clear_form();
	//ADD COMMAND TO CLOSE BOX HERE
	parent.tb_remove();


}

function clear_form()
{
//	$("#subsubcats").html(html); 
	$("#cat_breadcrumb").html('');
	$("#subcats").html(''); 
	$("#subsubcats").html(''); 
	$('#cat_level_1 option:selected').removeAttr("selected")

}

$(document).ready
(
	function()
	{
		
		cat1bind();
	
		$('#btn_cat_submit').click(submitCategorizeForm);	
		$('#btn_cat_skip').click(skipCategorizeForm);	
		
	}
	
); 