var field_id;
var profile_field;

$(document).ready(function() { //Let's reset Moodle profile field;
	field_id = "id_profile_field_companycategory";
	profile_field = $("#"+field_id);	
	if(profile_field.length > 0) //Does the profile field exist?
	{	
		if(profile_field.val() == 0) //If there is no value selected;
		{
			resetDropdown(field_id, false); //Let's reset
		}
		else {
			addResetBtn(); //Otherwise, show the Reset button;
		}
	}
});

/*
	Other Functions
*/

function resetDropdown(dropdownID, booSetValues) //Used to clear/hide the dropdowns;
{
	$("#id_submitbutton").attr("disabled",true);
		$('label[for="'+dropdownID+'"]').parent().parent().hide();
		$('label[for="'+dropdownID+'"]').hide();
		$("#"+dropdownID).empty();
		$("#"+dropdownID).hide();
		
		if(!booSetValues) //Are we showing the last combined dropdown?
		{
		//No, let's do the AJAX request;
			$.ajax({
				type: "POST",
				url: base_url+"/ajax/company_category.php",
				data: "company_cat=1",
				cache:false,
				success: function(html){
				$("#category_2").append(html);
				}
			});
		}
		else {
		//Otherwise, enable the submit button;
			$("#id_submitbutton").attr("disabled",false);
		}
}

function showSubCategories(strCategory)
{
		//Get the dropdown box from PHP with AJAX;
		$.ajax({
				type: "POST",
				url: base_url+"/ajax/company_category.php",
				data: "company_sub_cat=1&cat="+strCategory+"",
				cache:false,
				success: function(html){
				resetDropdown('company_sub_sub_category',true);
				if($('#company_sub_category').length > 0)
				{
					$('#id_company_sub_category').remove();
					$("#category_2").append(html);
				}
				else {
					$("#category_2").append(html);
				}
				
			}
		});
}
function showSubCategories_sub(strCategory)
{
	
		//Get the dropdown box from PHP with AJAX;
		$.ajax({
				type: "POST",
				url: base_url+"/ajax/company_category.php",
				data: "company_sub_sub_cat=1&subcat="+strCategory+"",
				cache:false,
				success: function(html){
				if($('#company_sub_sub_category').length > 0)
				{
					$('#id_company_sub_sub_category').remove();
					$("#category_2").append(html);
				}
				else {
					$("#category_2").append(html);
				}
			}
		});
}
function setValues()
{
	//Lets set the selected values;
	if($('#company_category').val() != "Choose" && $('#company_sub_category').val() != "Choose" && $('#company_sub_sub_category').val() != "Choose")
	{
	//var strValue = $('#company_category').val()+' - '+ $('#company_sub_category').val()+' - '+ $('#company_sub_sub_category').val(); 
	var strValue =  $('#company_sub_category').val()+' - '+ $('#company_sub_sub_category').val(); 
	
	$.ajax({
			type: "POST",
			url: base_url+"/ajax/company_category.php",
			data: "set_values=1&strValue="+strValue+"",
			cache:false,
			success: function(value){
				resetDropdown('company_category',true);
				resetDropdown('company_sub_category',true);
				resetDropdown('company_sub_sub_category',true);
				//alert(strValue);
				//alert(value);
				$("#"+field_id).append(
					$('<option selected="selected"></option>').val(value).html(strValue)
				);
				$('label[for="'+field_id+'"]').parent().parent().show();
				$('label[for="'+field_id+'"]').show();
				$("#"+field_id).show();
				
				addResetBtn();
			}
			});
			}
	
}

function resetAll()
{
	$('#id_company_sub_category').remove();
	$('#id_company_category').remove();
	$('#reset_btn').remove();
	resetDropdown(field_id, false);
	
}

function addResetBtn()
{
	//Build the reset Button;
	$("#"+field_id).parent().append('<div><input type="button" id="reset_btn" value="Change" onclick="javascript:resetAll()" /></div>');
}	
