/**
*
* Some very useful functions
*
*/


function _id(id){
	return document.getElementById(id);
}

function pad0(strinteger){
	strinteger = new String(strinteger);
	if (strinteger.length==1)
		return "0"+strinteger;
	return  strinteger;
}


// clears options in a select box
function clear_select(id){
	_id(id).options.length = 0;
}
function select_size(id){
	return _id(id).options.length;
}
function set_select(id, value){
	var i, si;
	if ((obj=_id(id)) != null){
		for(i=0; i<obj.length; i++){
			if(obj.options[i].value == value){
				si = i;
			}
		}
		obj.selectedIndex = si;
	}
}
function get_select(id){
	if (!_id(id).options.length) // no items
		return 0;
	else // return the value of the selected option
		return _id(id).options[_id(id).selectedIndex].value;
}
function remove_selected(id){
	var selectbox = _id(id);
	var i = 0;
	for(i = 0; selectbox.options.length > i; i++){
		if(selectbox.options[i].selected){
			selectbox.remove(i);
		}
	}
}
// takes a 2d array - an array of [0(value), 1(text)] 
function populate_select(id,rows){
	clear_select(id);
	var i = 0;
	while (rows[i]){
		select_add(id,rows[i][0],rows[i][1]);
		i++;
	}
}
function select_add(id,value,text){
	var select = _id(id);
	select.options[select.length] = new Option(text, value);
	
}
function get_select_text(id){
	var select = document.getElementById(id);
	return select.options[select.selectedIndex].text;
}

/**
*
* Timegadget specific stuff
*
*/


function select_new(id){
	switch(id){
		case 'client':
		case 'clients':
			var name = prompt('Client name:');
			if (name){
				x_ajax_select_new(id, name, 0, receive_select_new);
				set_loading('select_new');
			}
		break;
		case 'project':
		case 'projects':
			if (select_size('clients')){
				var name = prompt('Project name:');
				if (name){
					x_ajax_select_new(id, name, get_select('clients'), receive_select_new);
					set_loading('select_new');
				}
			} else {
				alert('Please create or select a client first.');
			}
		break;
		case 'task':
		case 'tasks':
			if (select_size('projects')){
				var name = prompt('Task name:');
				if (name){
					x_ajax_select_new(id, name, get_select('projects'), receive_select_new);
					set_loading('select_new');
				}
			} else {
				alert('Please first select (or create if necessary) a client and a project.')
			}
		break;
		default:
		break;
	}
}


function select_delete(id){
	switch(id){
		case 'client':
			if (select_size('clients')){
				var selected = get_select('clients');
				if (confirm('Delete client '+get_select_text('clients')+' and all associated projects, tasks and entries??')){
					x_ajax_select_delete(id, selected, receive_select_delete);
					set_loading('select_delete');
				}
			} else {
				alert("There are no clients to delete");
			}
		break;
		case 'project':
			if (select_size('projects')){
				var selected = get_select('projects');
				if (confirm('Delete project '+get_select_text('projects')+' and all associated tasks and entries??')){
					x_ajax_select_delete(id, selected, receive_select_delete);
					set_loading('select_delete');
				}
			} else {
				alert("There are no projects to delete");
			}
		break;
		case 'task':
			if (select_size('tasks')){
				var selected = get_select('tasks');
				if (confirm('Delete task '+get_select_text('tasks')+' and all associated entries??')){
					x_ajax_select_delete(id, selected, receive_select_delete);
					set_loading('select_delete');
				}
			} else {
				alert("There are no tasks to delete");
			}
		break;
		default:
		break;
	}
}

function populate_from(type){
	switch(type){
		case 'clients':
		case 'client':
			populate_selects_from('client');
		break;
		case 'projects':
		case 'project':
			populate_selects_from('project', get_select('clients'));
		break;
		case 'tasks':
		case 'task':
			populate_selects_from('task', get_select('projects'));
		break;
		default:
		break;
	}
}

function receive_select_delete(type){
	unset_loading('select_delete');
//	populate_selects_from(type+'s');
	populate_from(type);
}
function receive_select_new(type){
	unset_loading('select_new');
//	populate_selects_from(type+'s');
	populate_from(type);
}
function populate_selects_from(selectId, parentSelectOptionId){
	x_ajax_populate_selects_from(selectId, parentSelectOptionId, receive_populate_selects_from);
	set_loading('populate_selects_from');
	//alert(parentSelectOptionId);
}


function validate_entry(){
	var errors = '';
	
	if (!getDateFromFormat(
			_id('fromdate').value+' '+_id('fromtime').value,
			'y-M-d H:m:s'
		)){
		errors += "Check the 'from' date/time is in the format yyyy-MM-dd and hh:mm:ss";
	}
	if (!getDateFromFormat(
			_id('todate').value+' '+_id('totime').value,
			'y-M-d H:m:s'
		)){
		errors += "\nCheck the 'to' date/time is in the format yyyy-MM-dd and hh:mm:ss";
	}

	if (!select_size('tasks')){
		errors += "\nThere are no tasks. Please create a task first. To do this you will also need to have created a client and a project.";
	}

	return errors;
}











/**
*
* page bits and pieces
*
*/

// array of tags for which the number of responses still expected is the integer value
var loading = new Array();
function set_loading(name){
	var name_loads = loading[name];
	name_loads = name_loads ? name_loads : 0
	name_loads++;
	loading[name] = name_loads;
	show_loading();
}
function unset_loading(name){

	// see if name is in list
	var name_loads = loading[name];
	name_loads = name_loads ? name_loads : 0;
	if (name_loads){
		name_loads--;
	}
	if (!name_loads) {
		loading.splice(name, 1);
	}
	if (!loading.length) {
		hide_loading();
	}
	loading[name] = name_loads;
}
function show_loading(){
	if (_id('loading'))
	_id('loading').style.display = 'block';
}
function hide_loading(){
	if (_id('loading'))
	_id('loading').style.display = 'none';
}







