QUICK_ACTN = -1;

function showDayEvents(elem, timestamp) {
	var myDate = new Date();
	myDate.setTime(timestamp*1000);
	var b = popupFactory();
	b.setText("Loading Events...");
	
	b.showBox(elem, 250);
	
	
	ajax("calendarComponent.php?ajax=getDayEvents&timestamp=" + timestamp + "&elemlnk=" + elem.id + "&poplnk=" + b.instance(), function(res) {
		if (ADMINMODE) {
			res += "<input type=\"button\" onmouseup=\"addEventMacro('" + elem.id + "', " + timestamp + ", "+b.instance()+");\" value=\"Quick Add\" />";
			res += "<input type=\"button\" onmouseup=\"showAddEvent('" + elem.id + "', "+b.instance()+", "+timestamp+", this);\" value=\"Add New Event\" />";
		}
		b.setText(res);
		//create_box(res, elem, 300);
	});
}

function qea_updateActions(acName) {
	ajax("calendarComponent.php?ajax=qea_actionList&a=" + acName, function(res) {
		var list = ajaxResponseArray(res);
		var ih = String();
		
		for(i = 0; i < parseInt(list['num']); i++) {
			ih += '<option value="'+list['id' + i]+'">'+list['name' + i]+'</option>';
		}
		
		document.getElementById("qea_macro").innerHTML = ih;
	}); 
}

function qea_setQuickAction(id) {
	if (id != "null") {
		QUICK_ACTN = id;
		alert("Your Action is now " + id);
	}	
}

function addEventMacro(parentID, timestamp, pb_inst) {
	box = document.getElementById(parentID);
	//alert( parentID + " " + QUICK_ACTN + " " + timestamp);
	
	if (QUICK_ACTN != -1) {
		ajax("calendarComponent.php?ajax=add_event_macro&macID=" + QUICK_ACTN + "&timestamp=" + timestamp, function(res){
			document.getElementById(parentID).innerHTML = res;
			pb_inst.destroy();
		});
	}else{
		alert("No quick action set!");
	}	
}

function showAddEvent(elem, pb_inst, timestamp, pos) {
	var ih = String();
	var cb = pb_inst.generateChild();
	cb.setText("Loading...");
	cb.showBox(pos, 250);
	
	ajax("calendarComponent.php?ajax=get_types", function(res) {
		var row = ajaxResponseArray(res);
		numTypes = parseInt(row['typenum']);
		elem = document.getElementById(elem);
		q = "style=\"width: 20px\"";
		
		ih += "<form>"
		ih += "<input name=\"evt_desc\" value=\"Event Description\" /><br>";
		ih += "<table>";
		ih += "<tr><td>Start Time:</td><td>h:<input "+q+" name=\"st_h\" /> m:<input "+q+" name=\"st_m\" /> <select name=\"st_ampm\"><option value=\"0\">am</option><option value=\"12\">pm</option></select></td></tr>";
		ih += "<tr><td>End Time:</td><td>h:<input "+q+" name=\"et_h\" /> m:<input "+q+" name=\"et_m\" /> <select name=\"et_ampm\"><option value=\"0\">am</option><option value=\"12\">pm</option></select></td></tr>";
		ih += "</table>"
		ih += "<input name=\"evt_addr\" value=\"Event Address\" />";
		ih += "<input name=\"evt_locName\" value=\"Event Location Name\" /><br>";
		
		ih += "Type: <select name=\"evt_type\">";
		ih += "<option value=\"0\">event</option>"
		for (var i = 0; i < numTypes; i++) {
			ih += "<option value=\"" + row['typeid' + i] + "\">" + row['type' + i] + "</option>"
		}
		ih += "</select><br>";
		
		
		ih += "<input name=\"timestamp\" value=\""+timestamp+"\" type=\"hidden\" />";
		ih += "<input value=\"Add Event\" type=\"button\" onmouseup=\"sendAddEvent(this.parentNode, '"+elem.id+"', "+cb.instance()+");\" /><br>";
		ih += "</form>";
		
		cb.setText(ih);
	});
	
	
}

function sendAddEvent(elem, p, cpb_inst) {
	ajaxSend("calendarComponent.php?ajax=add_event", formToURI(elem), function (res){
		document.getElementById(p).innerHTML = res;
		cpb_inst.destroyTree();
	});
}

function delete_evt(evt, pb_inst, elem_id, timestamp, elem) {
	var elem_day = document.getElementById(elem_id);
	var cb = pb_inst.generateChild();
	cb.setText("Deleting article...");
	cb.showBox(elem, 150);
	
	ajax("calendarComponent.php?ajax=delete_event&event=" + evt + "&timestamp=" + timestamp, function(res) {
		pb_inst.destroy();
		elem_day.innerHTML = res;
	});
}

function edit_evt(evt, pb_inst, elem_id, timestamp, elem) {
	var elem_day = document.getElementById(elem_id);
	var cb = pb_inst.generateChild();
	cb.setText("Loading Article Data...");
	cb.showBox(elem, 250);
	
	ajax("calendarComponent.php?ajax=get_event_details&event=" + evt + "&timestamp=" + timestamp, function(res) {
		var row = ajaxResponseArray(res);
		var ih = new String();
		var numTypes = parseInt(row['typenum']);
		var q = "style=\"width: 20px\"";
		
		ih += "<form>"
		ih += popup_input("evt_desc", "text", row['description'])+"<br>";
		ih += "<table>";
		ih += "<tr><td>Start Time:</td><td>h:<input "+q+" name=\"st_h\" value=\""+row['st_h']+"\" class=\"hover_form\" /> m:<input "+q+" name=\"st_m\" value=\""+row['st_m']+"\" class=\"hover_form\" /> <select name=\"st_ampm\"><option value=\"0\" "+((row['st_ampm'] == "am") ? "selected=\"selected\"" : "")+">am</option><option value=\"12\" "+((row['st_ampm'] != "am") ? "selected=\"selected\"" : "")+">pm</option></select></td></tr>";
		ih += "<tr><td>End Time:</td><td>h:<input "+q+" name=\"et_h\" value=\""+row['et_h']+"\" class=\"hover_form\" /> m:<input "+q+" name=\"et_m\" value=\""+row['et_m']+"\" class=\"hover_form\" /> <select name=\"et_ampm\"><option value=\"0\" "+((row['st_ampm'] == "am") ? "selected=\"selected\"" : "")+">am</option><option value=\"12\" "+((row['st_ampm'] != "am") ? "selected=\"selected\"" : "")+">pm</option></select></td></tr>";
		ih += "</table>"
		ih += "<input name=\"evt_addr\" value=\""+row['address']+"\" class=\"hover_form\" />";
		ih += "<input name=\"evt_locName\" value=\""+row['locName']+"\" class=\"hover_form\" /><br>";
		
		ih += "Type: <select name=\"evt_type\">";
		ih += "<option value=\"0\" "+ ((row['eventType'] == "0") ? "selected=\"selected\"" : "") +">event</option>"
		for (var i = 0; i < numTypes; i++) {
			ih += "<option value=\"" + row['typeid' + i] + "\" "+ ((row['eventType'] == row['typeid' + i]) ? "selected=\"selected\"" : "") +">" + row['type' + i] + "</option>"
		}
		ih += "</select><br>";
		
		ih += "<input name=\"timestamp\" value=\""+timestamp+"\" type=\"hidden\" />";
		ih += "<input value=\"Edit Event\" type=\"button\" onmouseup=\"sendEditEvent(this.parentNode, '"+elem_day.id+"', "+cb.instance()+", "+evt+");\" /><br>";
		ih += "</form>";
		
		cb.setText(ih);
	});
}

function sendEditEvent(fd, elemID, pb_b, eventID) {
	var elem_day = document.getElementById(elemID);
	
	ajaxSend("calendarComponent.php?ajax=edit_event&e="+eventID, formToURI(fd), function(res) {
		pb_b.destroyTree();
		elem_day.innerHTML = res;
	});
}
















