
// global popupWindow
var win = new PopupWindow('qWindow');


function displayQCalendar(m,y,path,cat)
{
	//alert("d("+m+","+y+","+path+","+cat+")");
	var ran_no=(Math.round((Math.random()*9999)));
	var req = new DataRequestor();
	
	req.setObjToReplace('quickCalendar');
	req.addArg(_GET, "month", m);
	req.addArg(_GET, "year", y);
	req.addArg(_GET, "cat", cat);
	req.addArg(_GET, "ran", ran_no);
	
	if(arguments.length > 4 && arguments[4] != null) req.addArg(_GET, "s", (arguments[4] ? "admin" : "")); // set to true if we're backend
	if(arguments.length > 5 && arguments[5] != null) req.addArg(_GET, "lang", arguments[5]); else if(document.getElementById("lang") != null) req.addArg(_GET, "lang", document.getElementById("lang").value); 
	if(arguments.length > 6 && arguments[6] != null) req.addArg(_GET, "css", arguments[6]); else if(document.getElementById("css") != null) req.addArg(_GET, "css", document.getElementById("css").value);
	if(document.getElementById("mainTmpl") != null) req.addArg(_GET, "mainTmpl", document.getElementById("mainTmpl").value);
	if(document.getElementById("eventTmpl") != null) req.addArg(_GET, "eventTmpl", document.getElementById("eventTmpl").value);
	req.getURL(path);
}

// replaces openEvent()
function openDayEvents(year, month, day, cal) 
{
	//alert("openDayEvents("+year+","+month+","+day+","+cal+")");
	var admin = (arguments.length == 5 ? arguments[4] : 0);
	var req = new DataRequestor();
	req.addArg(_GET, "s", "open");
	req.addArg(_GET, "day", day);
	req.addArg(_GET, "month", month);
	req.addArg(_GET, "year", year);
	req.addArg(_GET, "cat", cal);
	req.addArg(_POST, "admin", (admin ? 1 : 0));
	req.onload = function (data, obj) 
	{
        win = new PopupWindow('qWindow'); 
        win.autoHide();
        win.setSize(200,200);
        win.populate(data);
        win.showPopup("qWindow");
	}
	req.getURL("qcal.php");
	document.getElementById("qWindow").style.display="";
	return false;
}


function openEvent(eventInfo)
{
	document.getElementById("qEvents").innerHTML = decodeURI(eventInfo);
	document.getElementById("qEvents").style.display = "";
	document.getElementById("qEvents").style.visibility = "visible";
	return false;
}

function closeEvent()
{
	if(arguments.length > 0) { arguments[0].style.display = "none"; arguments[0].style.visibility = "hidden"; }
	else if(document.getElementById("qEvents")) { document.getElementById("qEvents").style.display = "none"; document.getElementById("qEvents").style.visibility = "hidden"; }
	if(win) win.hidePopup();
	return false;
}



function editEvent(id)
{
	var req = new DataRequestor();
	req.addArg(_GET, "s", "edit");
	req.addArg(_POST, "id", id);
	if(document.getElementById("calendar") != null) req.addArg(_GET, "cat", document.getElementById("calendar").value);
	if(document.getElementById("month") != null) req.addArg(_GET, "month", document.getElementById("month").value);
	if(document.getElementById("year") != null) req.addArg(_GET, "year", document.getElementById("year").value);
	req.onload = function (data, obj) 
	{
        win = new PopupWindow('qWindow'); 
        win.autoHide(); 
        win.setSize(200,200);
        win.populate(data);
        win.showPopup("qWindow");
	}
	req.getURL("qcal.php");
	return false;
}


function moveEvent(id)
{
	var req = new DataRequestor();
	var action = "move";
	if(arguments.length == 2) action = arguments[1];
	req.addArg(_GET, "s", action);
	req.addArg(_POST, "id", id);
	req.addArg(_POST, "do", action);
	if(document.getElementById("calendar") != null) req.addArg(_GET, "cat", document.getElementById("calendar").value);
	if(document.getElementById("month") != null) req.addArg(_GET, "month", document.getElementById("month").value);
	if(document.getElementById("year") != null) req.addArg(_GET, "year", document.getElementById("year").value);
	req.onload = function (data, obj) 
	{
        win = new PopupWindow('qWindow'); 
        win.autoHide(); 
        win.setSize(200,200);
        win.populate(data);
        win.showPopup("qWindow");
	}
	req.getURL("qcal.php");
	return false;
}

function  copyEvent(id)
{
	moveEvent(id, "copy"); 
}


//function moveEvents() 
//{
//	
//}

function handleEventAction(action) 
{
	var form = document.getElementById("eventActionForm");
	var j=0;
	var arr= new Array();
	var events = document.getElementsByName("chkboxEvent");
	
	for (i=0;i<events.length;i++)
	{
		if (events[i].checked)
		{
			arr[j] = events[i].value;
			j++;
		}
	}
	
	switch(action) 
	{
		case "move":
		case "copy":
			moveEvent(arr, action);
			break;
			
		case "del":
			delEvent(arr);
			break;
			
		case "export":
			break;
	}
	return false;
}


function delEventSet(id) 
{
	var req = new DataRequestor();
	req.addArg(_GET, "s", "delSet");
	req.addArg(_POST, "id", id);
	req.addArg(_GET, "cat", document.getElementById("select_calendar")[document.getElementById("select_calendar").selectedIndex].value);
	req.onload = function (data, obj) 
	{
		theJSON = eval('(' + data + ')');
		alert(theJSON.message);
		win.hidePopup();
		displayQCalendar(theJSON.month,theJSON.year,"qcal.php",theJSON.cal,true,theJSON.lang,theJSON.css);
	}
	req.getURL("qcal.php");
	return false;
}

function delEvent(id)
{
	var req = new DataRequestor();
	req.addArg(_GET, "s", "del");
	req.addArg(_POST, "id", id);
	req.addArg(_POST, "do", "del");
	req.addArg(_GET, "cat", document.getElementById("select_calendar")[document.getElementById("select_calendar").selectedIndex].value);
	req.addArg(_POST, "cal", document.getElementById("select_calendar")[document.getElementById("select_calendar").selectedIndex].value);
	if(document.getElementById("calendar") != null) req.addArg(_GET, "cat", document.getElementById("calendar").value);
	if(document.getElementById("month") != null) req.addArg(_GET, "month", document.getElementById("month").value);
	if(document.getElementById("year") != null) req.addArg(_GET, "year", document.getElementById("year").value);
	req.onload = function (data, obj) 
	{
		//alert(data);
		theJSON = eval('(' + data + ')');
		alert(theJSON.message);
		win.hidePopup();
		displayQCalendar(theJSON.month,theJSON.year,"qcal.php",theJSON.cal,true,theJSON.lang,theJSON.css);
	}
	req.getURL("qcal.php");
	return false;
}

function addEvent(year, month, day, cal) 
{
	var req = new DataRequestor();
	req.addArg(_GET, "s", "add");
	req.addArg(_GET, "day", day);
	req.addArg(_GET, "month", month);
	req.addArg(_GET, "year", year);
	req.addArg(_GET, "cal", cal);
	req.onload = function (data, obj) 
	{
        win = new PopupWindow('qWindow'); 
        win.autoHide(); 
        win.setSize(200,200);
        win.populate(data);
        win.showPopup("qWindow");
	}
	req.getURL("qcal.php");
	return false;
}


function validateForm(form) 
{
	var validator  = new Validator(form);
	 
	validator.addItem("span","req");
	validator.addItem("span","value>0");
	
	validator.addItem("date","req");
	validator.addItem("date","date=YYYY-MM-DD");
	 	
	validator.addItem("time","regexp=^\\s*\\d{1,2}[.:]{1}\\d{2}(\\s*[-]{1}\\s*\\d{1,2}[.:]{1}\\d{2})?\\s*$", "Format: 18:00[-20:00]");
	
	validator.addItem("title","req");
	
	var rc = validator.validate();						
	
	validator.clear();
	
	return rc;
}

function postEvent(action) 
{
	var form = document.getElementById("form_eventForm");

	if(validateForm(form)) 
	{
		var cal = document.getElementById("select_category")[document.getElementById("select_category").selectedIndex].value;
		var req = new DataRequestor();
		req.addArg(_GET, "s", action);
		req.addArg(_GET, "cal", cal);
		req.addArg(_POST, "do", action);
		req.addArg(_POST, "id", document.getElementById("id").value);
		req.addArg(_POST, "setid", document.getElementById("setid").value);
		req.addArg(_POST, "extrainfo", document.getElementById("extrainfo").value);
		req.addArg(_POST, "category", cal);
		req.addArg(_POST, "span", document.getElementById("span").value);
		req.addArg(_POST, "date", document.getElementById("date").value);
		req.addArg(_POST, "time", document.getElementById("time").value);
		req.addArg(_POST, "title", document.getElementById("title").value);
		req.addArg(_POST, "location", document.getElementById("location").value);
		
		if(document.getElementById("wysiwyg_editor") && document.getElementById("wysiwyg_editor").value == "fckeditor") // wysiwyg
		{
			parent.frames[0].FCK.UpdateLinkedField();
		}
		req.addArg(_POST, "description", document.getElementById("description").value);
		
		if(document.getElementById("checkbox_repeat") != null && document.getElementById("checkbox_repeat").checked) 
		{
			req.addArg(_POST, "repeat", document.getElementById("checkbox_repeat").checked);
			req.addArg(_POST, "repeat_interval", document.getElementById("text_repeat_interval").value);
			req.addArg(_POST, "repeat_type", document.getElementById("select_repeat_type")[document.getElementById("select_repeat_type").selectedIndex].value);
			req.addArg(_POST, "repeat_until", document.getElementById("text_recurrenceuntil").value);	
		}
		
		req.onload = function (data, obj) 
		{
			alert(data);
			win.hidePopup();
			displayQCalendar(document.getElementById("month").value,document.getElementById("year").value,"qcal.php",document.getElementById("calendar").value,true,document.getElementById("lang").value,document.getElementById("css").value);
		}
		req.getURL("qcal.php");
	}
	
	return false;
}

function updateEvent() 
{
	return postEvent("update");
}

function updateEventSet() 
{
	return postEvent("update_set");
}

function insertEvent() 
{
	return postEvent("insert");
}

function postMoveEvent(action) 
{
	var req = new DataRequestor();
	req.addArg(_GET, "s", action);
	req.addArg(_POST, "do", action);
	req.addArg(_POST, "id", document.getElementById("id").value);
	req.addArg(_POST, "setid", document.getElementById("setid").value);
	req.addArg(_POST, "cal", document.getElementById("select_category")[document.getElementById("select_category").selectedIndex].value);
	req.addArg(_POST, "events", document.getElementById("events").value);
	
	req.onload = function (data, obj) 
	{
		alert(data);
		win.hidePopup();
		displayQCalendar(document.getElementById("month").value,document.getElementById("year").value,"qcal.php",document.getElementById("calendar").value,true,document.getElementById("lang").value,document.getElementById("css").value);
	}
	req.getURL("qcal.php");

	return false;
}

function doMoveEvent() 
{
	return postMoveEvent("doMove");
}

function doMoveEventSet() 
{
	return postMoveEvent("doMoveSet");
}

function doCopyEvent() 
{
	return postMoveEvent("doCopy");
}

function doCopyEventSet() 
{
	return postMoveEvent("doCopySet");
}

function toggleRepeat() 
{
	document.getElementById('text_recurrenceuntil').disabled = !document.getElementById("checkbox_repeat").checked;
	document.getElementById('text_repeat_interval').disabled = !document.getElementById("checkbox_repeat").checked;
	document.getElementById('select_repeat_type').disabled = !document.getElementById("checkbox_repeat").checked;
}

function selectCalendar(selectObj, m, y, path)
{
	var admin = (arguments.length > 4 ? arguments[4] : false);
	var lang = (arguments.length > 5 ? arguments[5] : null);
	var css = (arguments.length > 6 ? arguments[6] : null);
	return displayQCalendar(m,y,path,selectObj[selectObj.selectedIndex].value, admin, lang, css);
}

function addCalendar()
{
	var req = new DataRequestor();
	var cal = document.getElementById('select_calendar')[document.getElementById('select_calendar').selectedIndex].value;
	
	req.addArg(_GET, "s", "addCal");
	req.addArg(_GET, "cal", cal);
	if(document.getElementById("month") != null) req.addArg(_GET, "month", document.getElementById("month").value);
	if(document.getElementById("year") != null) req.addArg(_GET, "year", document.getElementById("year").value);
	
	req.onload = function (data, obj) 
	{
        win = new PopupWindow('qWindow'); 
        win.autoHide(); 
        win.setSize(200,200);
        win.populate(data);
        win.showPopup("qWindow");
	}
	req.getURL("qcal.php");
	return false;
}

function editCalendar()
{
	var req = new DataRequestor();
	var cal = document.getElementById('select_calendar')[document.getElementById('select_calendar').selectedIndex].value;
	
	if(cal <= 0) 
	{
		alert("Please choose a calendar in the dropdown list.");
		return false;
	}
	
	req.addArg(_GET, "s", "editCal");
	req.addArg(_GET, "cal", cal);
	req.addArg(_POST, "id", cal);
	if(document.getElementById("month") != null) req.addArg(_GET, "month", document.getElementById("month").value);
	if(document.getElementById("year") != null) req.addArg(_GET, "year", document.getElementById("year").value);
	
	req.onload = function (data, obj) 
	{
        win = new PopupWindow('qWindow'); 
        win.autoHide(); 
        win.setSize(200,200);
        win.populate(data);
        win.showPopup("qWindow");
	}
	req.getURL("qcal.php");
	return false;
}

function updateCalendar() 
{
	var req = new DataRequestor();

	req.addArg(_GET, "s", "updateCal");
	req.addArg(_GET, "cal", document.getElementById("calendar").value);
	req.addArg(_POST, "name", document.getElementById("text_calendar_name").value);
	req.addArg(_POST, "id", document.getElementById("id").value);
	
	req.onload = function (data, obj) 
	{
        alert(data);
		win.hidePopup();
		displayQCalendar(document.getElementById("month").value,document.getElementById("year").value,"qcal.php",document.getElementById("calendar").value,true,document.getElementById("lang").value,document.getElementById("css").value);
	}
	req.getURL("qcal.php");
	
	return false;
}


function insertCalendar() 
{
	var req = new DataRequestor();

	req.addArg(_GET, "s", "newCal");
	req.addArg(_GET, "cal", document.getElementById("calendar").value);
	req.addArg(_POST, "name", document.getElementById("text_calendar_name").value);
	req.onload = function (data, obj) 
	{
        alert(data);
		win.hidePopup();
		displayQCalendar(document.getElementById("month").value,document.getElementById("year").value,"qcal.php",document.getElementById("calendar").value,true,document.getElementById("lang").value,document.getElementById("css").value);
	}
	req.getURL("qcal.php");
	return false;
}

function delCalendar()
{
	var req = new DataRequestor();
	var cal = document.getElementById('calendar').value;
	
	if(cal <= 0) 
	{
		alert("Please choose a calendar in the dropdown list.");
		return false;
	}
	
	req.addArg(_GET, "s", "delCal");
	req.addArg(_GET, "cal", cal);
	req.addArg(_POST, "id", cal);
	
	req.onload = function (data, obj) 
	{
		alert(data);
		win.hidePopup();
		displayQCalendar(document.getElementById("month").value,document.getElementById("year").value,"qcal.php",0,true,document.getElementById("lang").value,document.getElementById("css").value);
	}
	req.getURL("qcal.php");
	return false;
}

function exportCalendar(admin) 
{
	var req = new DataRequestor();
	var cal = 0;
	
	if(arguments.length == 2) 
	{
		cal = arguments[1];
	}
	else if(document.getElementById('select_calendar')) 
	{
		cal = document.getElementById('select_calendar')[document.getElementById('select_calendar').selectedIndex].value;	
	}
		
	req.addArg(_GET, "s", "exportCal");
	req.addArg(_GET, "cal", cal);
	req.addArg(_POST, "id", cal);
	req.addArg(_POST, "admin", (admin ? 1 : 0));
	if(document.getElementById("month") != null) req.addArg(_GET, "month", document.getElementById("month").value);
	if(document.getElementById("year") != null) req.addArg(_GET, "year", document.getElementById("year").value);
	
	req.onload = function (data, obj) 
	{
        win = new PopupWindow('qWindow'); 
        win.autoHide(); 
        win.setSize(200,200);
        win.populate(data);
        win.showPopup("qWindow");
	}
	req.getURL("qcal.php");
	return false;
}

function doExport()
{
	var cal = 0;
	
	if(document.getElementById("select_calendar")) 
	{
		cal = document.getElementById("select_calendar")[document.getElementById('select_calendar').selectedIndex].value;	
	}
	else 
	{
		cal = document.getElementById("calendar").value;
	}
	
	var req = new DataRequestor();
	req.addArg(_GET, "s", "doExport");
	req.addArg(_GET, "cal", cal);
	req.addArg(_POST, "id", cal);
	req.addArg(_POST, "fromdate", document.getElementById("fromdate").value);
	req.addArg(_POST, "todate", document.getElementById("todate").value);
	
	req.onload = function (data, obj) 
	{
		theJSON = eval('(' + data + ')');
		window.open('include/inc_module/mod_qcal/inc_act/ical.export.php?format='+theJSON.format+'&dirfile='+theJSON.dirfile, "Export iCal");
		win.hidePopup();
	}
	req.getURL("qcal.php");
	return false;
}

function mark(what) 
{
	var events = document.getElementsByName("chkboxEvent");
	
	for (i=0;i<events.length;i++)
	{
		events[i].checked = (what == "inverse" ? !events[i].checked : what == "none" ? false : true);
	}
}

