$(document).ready(function() {
	booking_base = "/booking/";
	GetTeritories();
	GetSubjects();
	$('#teritory').change(function(){
		resetSelect('location','Please Select Venue',true);
		resetSelect('season','Please Select Season',true);
		resetSelect('subject','Please Select Camp/Course',false);
		GetLocations();

	});
	$('#location').change(function(){
		GetSeasons();
		//resetSelect('season','Please Select Season');
		//resetSelect('subject','Please Select Camp/Course');

	});
	$('#season').change(function(){
		//resetSelect('subject','Please Select Camp/Course');
		//GetSubjects();
		

	});
	
	$('form[name=finderform]').submit(function(){
		if($('#teritory').val()!=''){
			return true;
		}
		else{
			alert('Please Select County');
			return false;
		}
	}
	);

});

function resetSelect(dropdown,defaultStr,full){
	var select = $('#'+dropdown);
	if(full){
		select.html('');
		select.append("<option value=''>"+defaultStr+"</option>");
	}
	else{
		select.val('');
	}
	$.uniform.update('#'+dropdown); 
}

function getFinderData(server_url,nodeName,dropdown,defaultStr){
	$.ajax({
		type: "GET",
		url: server_url,
		dataType: "xml",
		success: function(xml) {
			var select = $('#'+dropdown);
			select.html('');
			select.append("<option value=''>"+defaultStr+"</option>");
			$(xml).find(nodeName).each(function(){
				var title = $(this).find('Name').text();
				var id = $(this).find('ID').text();
				select.append("<option value='"+id+"'>"+title+"</option>");
			});
			$.uniform.update('#'+dropdown); 
		}
	});
}

function GetTeritories(){
	var url = booking_base+"FindTheRightCamp.aspx?context=getTeritories";
	getFinderData(url,'Teritory','teritory','Please Select County');
}

function GetSubjects(){
	var url = booking_base+"FindTheRightCamp.aspx?context=getSubjects";
	getFinderData(url,'Subject','subject','Please Select Camp/Course');
}

function GetLocations(){
	// Booking.aspx?context=getLocations&teritoryid=[teritory_id];
	// location
	if($('#teritory').val()){
		var url = booking_base+"FindTheRightCamp.aspx?context=getLocations&teritoryid="+$('#teritory').val();
		getFinderData(url,'Location','location','Please Select Venue');
	}
}

function GetSeasons(){
	// Booking.aspx?context=getSeasons&location=[location_id]
	// season
	if($('#location').val()){
		var url = booking_base+"FindTheRightCamp.aspx?context=getSeasons&location="+$('#location').val();
		getFinderData(url,'Season','season', 'Please Select Season');
	}
}
