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 submitWholeForm() 
{
	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();
	var question 	= $('#question').val();
	var email 		= $('#email').val();
	var location 	= $('#location').val();
	var keyword 	= $('#keyword').val();
	
	if (!question || !email || !location || !keyword)
	{
		return false;
	}
	
//	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"
		skipCategorizeWholeForm();
		return;
	}
	
	if (catid != null)
	{
		var questionid = $('#question_id').attr('value');
		values = "catid="+catid+"&question="+question+"&email="+email+"&location="+location+"&keyword="+keyword;
//		alert (values);
	
		$.ajax({
	         type:'GET',
	         url:'/includes/php/ask_question.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();
         window.location.replace("/");
	}	
}

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 skipCategorizeWholeForm() {
/*** Used to skip category step when using single page form at /ask.php ***/
	var questionid = $('#question_id').attr('value');
	var question 	= $('#question').val();
	var email 		= $('#email').val();
	var location 	= $('#location').val();
	var keyword 	= $('#keyword').val();

	values = "catid=0"+"&question="+question+"&email="+email+"&location="+location+"&keyword="+keyword;
//	alert (values);

	$.ajax({
		 type:'GET',
		 url:'/includes/php/ask_question.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();
	 window.location.replace("/");
}

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")

}

function check_vars(question, email, location, keyword)
{
	if (question.length < 10)
	{
		disable_ask_button();
		return false;
	}
	if (email.length < 6)
	{
		disable_ask_button();
		return false;
	}
	if (!util.validate_email(email))
	{
		disable_ask_button();
		return false;
	}
	if (location.length < 4)
	{
		disable_ask_button();
		return false;
	}
	if (keyword.length < 3)
	{
		disable_ask_button();
		return false;
	}
	enable_ask_button();
	return true;
}

function disable_ask_button()
{
	$('#ask_placeholder').css('display', 'block');
	$('#ask_link').css('display', 'none');
}

function enable_ask_button()
{
	$('#ask_link').css('display', 'block');
	$('#ask_placeholder').css('display', 'none');
}

function ask_enter_keypress()
{
	$(document).bind('keypress', function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13) {
			var ask_link = $('#ask_link');
			if(ask_link.css('display') == 'block'){
				ask_link.click();
			}
		}
	});
} 

$(document).ready
(
	function()
	{
		$('#ask_link').click(function(){
			var new_question = $('#complete_question_form').serialize();

			$.ajax({
			   url:'ajax/set/new/question.php?type=general&'+new_question,
			   dataType:'json',
			   success:function(data){
			     if(data.result == true)
			     {
					$('#categorize_container #question_id').attr('value', data.question_id);
					$('#categorize_container #categorize h3').html("");
					q.get_question(data.question_id);
					tb_show("Choose a Category", '#TB_inline?&height=420&width=700&inlineId=categorize_container&modal=false');
			     }
			    }
			});
			return false;
		})
		
		var question 	= $('#question').val();
		var email 		= $('#email').val();
		var location 	= $('#location').val();
		var keyword 	= $('#keyword').val();
		var validated	= false;

		cat1bind();
		ask_enter_keypress();
	
		$('#btn_cat_submit').click(submitCategorizeForm);
		$('#submit_whole_question').click(submitWholeForm);
		$('#whole_question_cat_skip').click(skipCategorizeWholeForm);	
		$('#btn_cat_skip').click(skipCategorizeForm);
//		$("#complete_question_form").validate();
		$("#question").keyup(function()
			{
				question = $('#question').val();
				if (question.length > 500)
				{	
					question = question.substring(0, 500);
					$('#question').val(question);
				}
				$('#char_count').html((500 - question.length) + ' characters left');
				validated = check_vars(question, email, location, keyword);
			});
		$("#question").change(function() 
			{
				question = $('#question').val();
				validated = check_vars(question, email, location, keyword);
			});
		$("#email").change(function() 
			{
				email = $('#email').val();
				validated = check_vars(question, email, location, keyword);
			});
		$("#email").keyup(function() 
			{
				email = $('#email').val();
				validated = check_vars(question, email, location, keyword);
			});
		$("#location").change(function() 
			{
				location = $('#location').val();
				validated = check_vars(question, email, location, keyword);
			});
		$("#location").keyup(function() 
			{
				location = $('#location').val();
				validated = check_vars(question, email, location, keyword);
			});
		$("#keyword").change(function() 
			{
				keyword = $('#keyword').val();
				validated = check_vars(question, email, location, keyword);
			});
		$("#keyword").keyup(function() 
			{
				keyword = $('#keyword').val();
				validated = check_vars(question, email, location, keyword);
			});
	}
	
); 
