function tidyDate(d, longformat) {	
	var currYear = d.getFullYear();
	var currMonth = d.getMonth() + 1;
	var currDate = d.getDate();

	var result = currYear + "-" + (currMonth < 10 ? "0" : "") + currMonth + "-" + (currDate < 10 ? "0" : "") + currDate;
	if(longformat != null) {
		var currHour = d.getHours();
		var currMinute = d.getMinutes();
		var currSecond = d.getSeconds();
		result += ' ' + (currHour < 10 ? "0" : "") + currHour + ':' + (currMinute < 10 ? "0" : "") + currMinute + ':' + (currSecond < 10 ? "0" : "") + currSecond;
	}

	return result;
}

function fill_ad(me) {
	var d = new Date();
	var startdate = me.parentNode.nextSibling.nextSibling.firstChild.nextSibling.nextSibling;
	var stopdate  = me.parentNode.nextSibling.nextSibling.nextSibling.firstChild.nextSibling.nextSibling;
	var priority  = me.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nextSibling.nextSibling;
	if(me.checked) {
		if(startdate.value == '')
			startdate.value = tidyDate(d);
		if(priority.value == '')
			priority.value = tidyDate(d, true);			
		d.setMonth(d.getMonth() + 1);
		if(stopdate.value == '')
			stopdate.value = tidyDate(d);
	} else {
		//startdate.value = '';
		//stopdate.value = '';
	}
}

function fill_banner(me) {
	var d = new Date();
	var startdate = me.parentNode.nextSibling.firstChild.nextSibling.nextSibling;
	var stopdate  = me.parentNode.nextSibling.nextSibling.firstChild.nextSibling.nextSibling;
	var priority  = me.parentNode.nextSibling.nextSibling.nextSibling.firstChild.nextSibling.nextSibling;
	if(me.checked) {
		if(startdate.value == '')
			startdate.value = tidyDate(d);
		if(priority.value == '')
			priority.value = tidyDate(d);			
		d.setMonth(d.getMonth() + 1);
		if(stopdate.value == '')
			stopdate.value = tidyDate(d);			
	} else {
		//startdate.value = '';
		//stopdate.value = '';
	}
}

function fill_presentation(me) {
	var d = new Date();
	var startdate = me.parentNode.nextSibling.firstChild.nextSibling.nextSibling;
	var stopdate  = me.parentNode.nextSibling.nextSibling.firstChild.nextSibling.nextSibling;
	var priority  = me.parentNode.nextSibling.nextSibling.nextSibling.firstChild.nextSibling.nextSibling;
	if(me.checked) {
		if(startdate.value == '')
			startdate.value = tidyDate(d);
		if(priority.value == '')
			priority.value = tidyDate(d);			
		d.setMonth(d.getMonth() + 1);
		if(stopdate.value == '')
			stopdate.value = tidyDate(d);			
	} else {
		//startdate.value = '';
		//stopdate.value = '';
	}
}


function set_state(node, state) {
	var d = node.parentNode.nextSibling.style.display;
	node.innerHTML = (state ? '<img src="/images/npil.gif" alt="-" />' : '<img src="/images/hpil.gif" alt="+" />');
	node.parentNode.nextSibling.style.display = (state ? 'block' : 'none');
}

function toggle(node) {
	var d = node.parentNode.nextSibling.style.display;
	var state = d == 'none';
	node.innerHTML = (state ? '<img src="/images/npil.gif" alt="-" />' : '<img src="/images/hpil.gif" alt="+" />');
	node.parentNode.nextSibling.style.display = (state ? 'block' : 'none');
}

function set_children(node, state) {
	var c = node.parentNode.getElementsByTagName('input');
	if(node.parentNode.nextSibling) {
		var checkboxes = node.parentNode.nextSibling.getElementsByTagName('input');
		for(i = 0; i < checkboxes.length; i++)
			checkboxes[i].checked = c[0].checked;		
	}
}

function tree_open_helper(node) {
	set_state(node.parentNode.parentNode.firstChild.firstChild, true);
	if(!node.parentNode.parentNode.parentNode.treeview)
		tree_open_helper(node.parentNode.parentNode);
}

function tree_open(root) {
	root.treeview = true;
	var checkboxes = root.getElementsByTagName('input');
	for(i = 0; i < checkboxes.length; i++) {
		if(checkboxes[i].checked) {
			tree_open_helper(checkboxes[i].parentNode.parentNode);
		}
	}
}
