var dateType='week';

$(document).ready(function(){
    	
		$('#today').click(function(){
			dateType='today';
			getTodayTerm();
		});
		$('#week').click(function(){
			dateType='week';
			getWeeklyTerm();
		});  	
		$('#two_weeks').click(function(){
			dateType='two_weeks';
			getTwoWeeksTerm();
		});
		$('#month').click(function(){
			dateType='month';
			getMonthlyTerm();
		});

	});


function setCustomerType(type){
	
	$.ajax({
		type: "POST",
		async: false,
		url: "/index/setcustomertype/" + type + "/",
		success: function(){
			
			switch(dateType){
				case 'day':
					if(day_ != 0)
						daySelected(day_);
					break;
					
				case 'today':
					getTodayTerm();
					break;
					
				case 'two_weeks':
					getTwoWeeksTerm();
					break;
				
				case 'month':
					getMonthlyTerm();
					break;
					
				case 'week': default:
					getWeeklyTerm();
					break;	
			}		
		}
	});
	
	
}

var ajaxHash = 0;

function liveSearch(){
	
	ajaxLoading();
	ajaxHash = Math.random() * 100000 + 1;
	var results = $('#liveSearchResults');
	results.slideUp('fast');
	setTimeout('liveSearch_(' + ajaxHash + ')', 500);
}


function liveSearch_(hash){
	
	if(hash != ajaxHash){
		ajaxLoaded();
		return false;
	}
		
					
	var phrase = document.getElementById('search_training_text').value;
	var results = $('#liveSearchResults');
	
	if (phrase.length > 2) {
        
		$.ajax({
			type: "POST",
			data: 'searchPhrase=' + encodeURIComponent(phrase),
			url: "/index/livesearch/",
			success: function(data){
				
				results.html(data);
				if(data!='')
				    results.slideDown('fast');
					
				ajaxLoaded();
			}
		})
		
	}else{
		ajaxTimeout(0);
		results.html('');
		results.fadeOut('slow');
	}
		
}

