//==============================================
//	MyHTML
//==============================================

//Checkbox
function checkbox_click(img, id) {
	cb = document.getElementById(id);
	if (cb.checked) {
		cb.checked = false;
		img.src = "myhtml/checkbox_unsel.png";
	} else {
		cb.checked = true;
		img.src = "myhtml/checkbox_sel.png";
	}
}

//Select
function select(name, value, label) {
	document.getElementById(name).value = value;
	document.getElementById(name+"_value").innerHTML = label;
	hideSelect(name);
}

function selectClick(name) {
	if(document.getElementById(name+"_select").style.display != "none") {
		hideSelect(name);
	} else {
		showSelect(name);
	}
}

function showSelect(name) {
	document.getElementById(name+"_select").style.display = "block";
}

function hideSelect(name) {
	document.getElementById(name+"_select").style.display = "none";
}

function optionSelected(opt) {
	opt.style.backgroundColor = "#2a89b9";
	opt.style.color = "#ffffff";
}

function optionUnselected(opt) {
	opt.style.backgroundColor = "#ffffff";
	opt.style.color = "";
}

//Calendar
function change_month(m, y, cal) {
	//ajax.js MUST be included!
	requestPage("lib/calendar.inc.php?cal="+cal+"&m="+m+"&y="+y, 'get', null, month_changed, month_change_error, cal);
}

function month_changed(text, cal) {
	//change div content
	document.getElementById(cal).innerHTML = text;
}

function month_change_error() {
	alert("Errore di sistema.");
}

function showCalendar(cal, sel_button) {
	set_pos(document.getElementById("block_"+cal), sel_button);
}

function hideCalendar(cal) {
	document.getElementById("block_"+cal).style.display = "none";
}

function selectDate(cal, sel_button) {
	if(document.getElementById("block_"+cal).style.display != "none") {
		hideCalendar(cal);
	} else {
		showCalendar(cal, sel_button);
	}
}

function day_over(id) {
	document.dayOldColor = id.style.color;
	id.style.backgroundColor = "#2a89b9";
	id.style.color = "#ffffff";
	id.style.cursor = "pointer";
	
}

function day_out(id) {
	id.style.backgroundColor = "#ffffff";
	id.style.color = document.dayOldColor;
}

function select_day(d, m, y, cal) {
	document.getElementById(cal).value = d+"/"+m+"/"+y;
	hideCalendar("cal_"+cal);
}

function set_pos(element, sel_button) {
	el = sel_button;
	
	var top = 20;
	var left = 0;
	while(el != null) {
		top += el.offsetTop;
		left += el.offsetLeft;
		el = el.offsetParent;
	}
	
	/*
	if(el.offsetParent.offsetParent != null) {
		var top = el.offsetParent.offsetTop + el.offsetParent.offsetParent.offsetTop + el.offsetTop + 16;
	}  else {
		var top = el.offsetTop + 16;
	}
	
	if(el.offsetParent.offsetParent != null) {
		var left = el.offsetParent.offsetLeft + el.offsetParent.offsetParent.offsetLeft + el.offsetLeft + 1;
	} else {
		var left = el.offsetLeft;
	}
	*/
	
	element.style.top = top+"px";
	element.style.left = left+"px";
	element.style.display = "block";
}