var eventOpen = -1;
var teamOpen = -1;
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var leagues = new Array();
leagues["all"] = "All Leagues";leagues["sun5v5under64"] = "Sunday 5v5, 6ft 4in and Under";leagues["mon5v5upper"] = "Monday 5v5, Upper Division";leagues["mon5v5mid"] = "Monday 5v5, Middle Division";leagues["mon5v5low"] = "Monday 5v5, Lower Division";leagues["mon5v5low2"] = "Monday 5v5, Lower Division2";leagues["mon5v5rec"] = "Monday 5v5, Recreational Division";leagues["mon4v4under64"] = "Monday 5v5, 6ft 4in and Under";leagues["mon5v5church"] = "Monday 5v5 Church League";leagues["mon5v5restaurant"] = "Monday 5v5 Restaurant League";leagues["tues3v3under64"] = "Tuesday 3v3, 6ft 4in and Under";leagues["tues3v3under61"] = "Tuesday 3v3, 6ft 1in and Under/Recreation";leagues["wed5v5mid"] = "Wednesday 5v5, Middle Division";leagues["wed5v5women"] = "Wednesday 5v5, Women's Division";leagues["wed4v4under64"] = "Wednesday 5v5 Lower Division";leagues["wed4v4under62"] = "Wednesday 4v4 in Orange";leagues["thu4v4under63"] = "Thursday 4v4, 6ft 3in and Under";leagues["thu4v4upper"] = "Thursday 4v4 Upper in Tustin";leagues["thu4v4low"] = "Thursday 4v4 Lower in Tustin";leagues["officials"] = "Officials' Schedule";leagues["other"] = "Other";function showEventForm(id) {
	if (eventOpen != -1)
    	hideEventForm(eventOpen);
    eventOpen = id;
	var i;
    var curDate = new Date();
    var date = new Date(document.getElementById("dateVal"+id).value);
	var val = "<form action=\"javascript:addEvent('"+id+"')\">";
    val += "<select id=\"eventLeague"+id+"\">";
	for (i in leagues)
		val += "<option value=\""+i+"\""+(i == document.getElementById("eventLeagueVal"+id).value ? " selected=\"selected\"" : "")+">"+leagues[i]+"</option>";
	val += "</select><br />";
    val += "<select id=\"month"+id+"\">";
	for (i=0; i<=11; i++)
		val += "<option value=\""+i+"\""+(i == date.getMonth() ? " selected=\"selected\"" : "")+">"+months[i]+"</option>";
	val += "</select>";
    val += "<select id=\"date"+id+"\">";
	for (i=1; i<=31; i++)
		val += "<option value=\""+i+"\""+(i == date.getDate() ? " selected=\"selected\"" : "")+">"+i+"</option>";
	val += "</select>";
    val += "<select id=\"year"+id+"\">";
	for (i=curDate.getFullYear(); i<=curDate.getFullYear()+10; i++)
		val += "<option value=\""+i+"\""+(i == date.getFullYear() ? " selected=\"selected\"" : "")+">"+i+"</option>";
	val += "</select>";
    val += "<select id=\"hour"+id+"\">";
	for (i=1; i<=12; i++) {
    	var dateHour = date.getHours()%12;
        if (dateHour == 0)
        	dateHour = 12;
		val += "<option value=\""+i+"\""+(i == dateHour ? " selected=\"selected\"" : "")+">"+i+"</option>";
    }
	val += "</select>";
    val += "<select id=\"minute"+id+"\">";
	for (i=0; i<=55; i+=5) {
		var lead = i;
		if (lead < 10)
			lead = "0"+lead;
		val += "<option value=\""+lead+"\""+(i == date.getMinutes() ? " selected=\"selected\"" : "")+">"+lead+"</option>"
	}
	val += "</select>";
    val += "<select id=\"ampm"+id+"\">";
    val += "<option value=\"AM\">AM</option>";
	val += "<option value=\"PM\""+(date.getHours() > 11 ? " selected=\"selected\"" : "")+">PM</option>";
	val += "</select><br />";
	val += "<input id=\"info"+id+"\" type=\"text\" style=\"width: 400px\" value=\""+document.getElementById("infoVal"+id).value+"\" /><br />";
	val += "<a href=\"javascript:void(0)\" onclick=\"javascript:addEvent('"+id+"')\">"+(id == "n" ? "Add" : "Update")+" Event</a> | ";
    if (id != "n")
    	val += "<a href=\"javascript:void(0)\" onclick=\"javascript:deleteEvent('"+id+"')\">Delete</a> | ";
    val += "<a href=\"javascript:void(0)\" onclick=\"javascript:hideEventForm('"+id+"', 1)\">Cancel</a>";
    val += "</form>";
    var o = document.getElementById("eventForm"+id);
	o.innerHTML = val;
    o.style.display = "";
    document.getElementById("eventBox"+id).style.display = "none";
    document.getElementById("info"+id).focus();
}

function hideEventForm(id, cancel) {
	if (cancel)
    	eventOpen = -1;
	document.getElementById("eventBox"+id).style.display = "";
    document.getElementById("eventForm"+id).style.display = "none";
}

function addEvent(id) {
	if (id == null)
		id = "n";
	var a = "addEvent";
	if (id != "n")
		a = "editEvent&id="+id;
	var timestamp = encodeURIComponent((Number(document.getElementById("month"+id).value)+1)+"/"+document.getElementById("date"+id).value+"/"+document.getElementById("year"+id).value+" "+document.getElementById("hour"+id).value+":"+document.getElementById("minute"+id).value+" "+document.getElementById("ampm"+id).value);
	location.replace("DBAction.php?a="+a+"&league="+encodeURIComponent(document.getElementById("eventLeague"+id).value)+"&timestamp="+timestamp+"&convert=1&info="+encodeURIComponent(document.getElementById("info"+id).value)+"&ret="+getReturnURL("#e"+id));
}

function deleteEvent(id) {
	location.replace("DBAction.php?a=deleteEvent&id="+id+"&ret="+getReturnURL(""));
}

function showTeamForm(id) {
	if (teamOpen != -1)
    	hideTeamForm(teamOpen);
    teamOpen = id;
	var i;
	var val = "<form action=\"javascript:addTeam('"+id+"')\">";
    val += "<select id=\"teamLeague"+id+"\">";
	for (i in leagues)
    	if (i != "all")
			val += "<option value=\""+i+"\""+(i == document.getElementById("teamLeagueVal"+id).value ? " selected=\"selected\"" : "")+">"+leagues[i]+"</option>";
	val += "</select><br />";
	val += "<input id=\"team"+id+"\" type=\"text\" style=\"width: 200px\" value=\""+document.getElementById("teamVal"+id).value+"\" />";
    val += "<input id=\"wins"+id+"\" type=\"text\" size=\"2\" value=\""+document.getElementById("winsVal"+id).value+"\" />";
	val += "<input id=\"losses"+id+"\" type=\"text\" size=\"2\" value=\""+document.getElementById("lossesVal"+id).value+"\" />";
	val += "<input id=\"ties"+id+"\" type=\"text\" size=\"2\" value=\""+document.getElementById("tiesVal"+id).value+"\" /><br />";
	val += "<a href=\"javascript:void(0)\" onclick=\"javascript:addTeam('"+id+"')\">"+((id == "n") ? "Add" : "Update")+" Team</a> | ";
    if (id != "n")
    	val += "<a href=\"javascript:void(0)\" onclick=\"javascript:deleteTeam('"+id+"')\">Delete</a> | ";
    val += "<a href=\"javascript:void(0)\" onclick=\"javascript:hideTeamForm('"+id+"', 1)\">Cancel</a>";
    val += "</form>";
	document.getElementById("teamForm"+id).innerHTML = val;
    document.getElementById("teamRow"+id).style.display = "";
    document.getElementById("teamBox"+id).style.display = "none";
    document.getElementById("team"+id).focus();
}

function hideTeamForm(id, cancel) {
	if (cancel)
    	teamOpen = -1;
	document.getElementById("teamBox"+id).style.display = "";
    document.getElementById("teamRow"+id).style.display = "none";
}

function addTeam(id) {
	if (id == null)
		id = "n";
	var a = "addTeam";
	if (id != "n")
		a = "editTeam&id="+id;
	location.replace("DBAction.php?a="+a+"&league="+encodeURIComponent(document.getElementById("teamLeague"+id).value)+"&team="+encodeURIComponent(document.getElementById("team"+id).value)+"&wins="+encodeURIComponent(document.getElementById("wins"+id).value)+"&losses="+encodeURIComponent(document.getElementById("losses"+id).value)+"&ties="+encodeURIComponent(document.getElementById("ties"+id).value)+"&ret="+getReturnURL("#t"+id));
}

function deleteTeam(id) {
	location.replace("DBAction.php?a=deleteTeam&id="+id+"&ret="+getReturnURL(""));
}

function changeLeague(url) {
	location.replace(url+"?filter="+encodeURIComponent(document.getElementById("leagueSelect").value));
}

function getReturnURL(append) {
	var url = location.href;
    url = encodeURIComponent(url.substring(0, (url.lastIndexOf("#") == -1 ? url.length : url.lastIndexOf("#")))+append);
    return url;
}

function showPicture(id, numPictures) {
	var next = id+1;
    if (next > numPictures)
    	next = 1;
    var prev = id-1;
    if (prev < 1)
    	prev = numPictures;
    var frame = document.getElementById("frameDiv");
	frame.innerHTML = "<a href=\"javascript:void(0)\" onclick=\"javascript:showPicture("+prev+", "+numPictures+")\">&lt;&lt; Prev</a>";
    frame.innerHTML += " | <a href=\"javascript:void(0)\" onclick=\"javascript:showPicture("+next+", "+numPictures+")\">Next &gt;&gt;</a>";
    frame.innerHTML += " | <a href=\"javascript:void(0)\" onclick=\"javascript:randomPicture("+numPictures+")\">Random Picture</a>";
    frame.innerHTML += " | "+id+" of "+numPictures;
	frame.innerHTML += "<br /><br /><a href=\"javascript:void(0)\" onclick=\"javascript:showPicture("+next+", "+numPictures+")\"><img src=\"/pictures/"+id+".jpg\" alt=\"Picture "+id+"\" /></a><br />";
}

function randomPicture(numPictures) {
	showPicture(Math.floor(Math.random()*numPictures)+1, numPictures);
}
