
/** Calendar ********************************************************** **/
// Variables
var calendar_callback;

// Open Calendar
function openCalendar(event, id, start_date, callback) {
	field_type = "date";
	field_id = id;
	calendar_callback = callback;
	
	// Position Calendar
	$('calendar').style.left = Event.pointerX(event) + 'px';
	$('calendar').style.top  = Event.pointerY(event) + 'px';
	
	// Call Calendar
	if (typeof(start_date) != "undefined" && start_date != "") {
		date = start_date.split("/");
		switchCalendar(id, date[0], date[1], date[2]);
	} else {
		switchCalendar(id, '', '', '');
	}
}

// Switch Calendar
function switchCalendar(id, m, d, y) {
	if (typeof(m) == "undefined") {
		m = $(id + '_month').value;
		d = 1;
		y = $(id + '_year').value;
	}	
	
	new Ajax.Request('index.php?ajaxReq=loadCalendar&m=' + m + '&d=' + d + '&y=' + y + '&id=' + id, {
		method:'get',
		onSuccess: function(t){
			var r = t.responseText || "";
			$('calendar').innerHTML = r;
			$('calendar').show();
		}
	});
}

// Save Date
function saveDate(date, id) {
	$('calendar').hide();
	$("date_" + id).innerHTML = date;
	
	if (typeof(calendar_callback) != "undefined") {
		calendar_callback(date, id);
	}
}

/** Various Support Functions ***************************************** **/
var backOld = "";
var fontOld = "";
function bg(obj, color, font) {
	if (obj) {
		// If Color Is Undefined, Use backOld
		if (typeof(color) == "undefined") {
			obj.style.backgroundColor = backOld;
			obj.style.color = fontOld;
		} else {
			backOld = obj.style.backgroundColor;
			obj.style.backgroundColor = color;
			fontOld = obj.style.color;
			if (typeof(color) != "undefined") {
				obj.style.color = font;
			}
		}
	}
}

function clearDefault(e) {
	if (e.defaultValue==e.value){
		e.value = "";
		e.style.color = "#333333";
	}
}

function restoreDefault(e) {
	if (!e.value){
		e.value = e.defaultValue;
		e.style.color = "#b2b2b2";
	}
}
