// add a court to a list - check for duplicates
function add_court(court_name, court_id, where, member) {
	add_court(court_name, court_id, where, member, false)
}

function add_court(court_name, court_id, where, member, add_map) {
	var dupl = false;
	for (var i = 0; i < where.length; i++)
		if (where.options[i].value.substring(1) == court_id) { dupl = true; }
	if (dupl) { alert ("This court is already on your list."); }
	else {
		var text = "";
		var value = "";
		var i = where.length;
		if (member == 0) {
			text = (i + 1) + ". (G) " + court_name;
			value = " " + court_id;
		} else if (member == 1) {
			text = (i + 1) + ". (M) " + court_name;
			value = "M" + court_id;
		}  else if (member == 2) {
			text = (i + 1) + ". " + court_name;
			value = "P" + court_id;
		}
		where.options[i] = new Option(text, value); 
		if (add_map == true && window.parent.document.getElementById("map")) {
			courts = parent.courts;
			currentMarker = courts[court_id][5];
			parent.currentPoint = new parent.GPoint(courts[court_id][0], courts[court_id][1]);
			parent.currentDesc = courts[court_id][3];
	    	parent.courts[court_id][5] = parent.switchMarker(courts[court_id][5],1,courts[court_id][7],court_id,courts[court_id][6],member);
		}
	}
}

function set_court(court_name, court_id, textfield, hiddenid, add_map) {
	textfield.value = court_name;
	hiddenid.value = court_id;
	if (add_map == true && window.parent.document.getElementById("map")) {
		courts = parent.courts;
		currentMarker = courts[court_id][5];
		parent.currentPoint = new parent.GPoint(courts[court_id][0], courts[court_id][1]);
		parent.currentDesc = courts[court_id][3];
    	parent.courts[court_id][5] = parent.switchMarker(courts[court_id][5],1,courts[court_id][7],court_id,courts[court_id][6],member);
	}
}

function toggle_member_guest(where) {
	var pos = where.selectedIndex;
	if (pos == -1) { 
		alert("Please choose a court to edit."); 
		return;
	}
	member = where.options[pos].value.substr(0,1);
	if (member == "M") {
		var text = where.options[pos].text.split(".");
		var value = where.options[pos].value;
		var ind = where.options[pos].text.indexOf("(M) ",0);
		where.options[pos].text = text[0] + ". (G) " + text[1].substr(5);
		where.options[pos].value = " " + value.substr(1,5);
	} else if (member == " ") {
		var text = where.options[pos].text.split(".");
		var value = where.options[pos].value;
		var ind = where.options[pos].text.indexOf("(G) ",0);
		where.options[pos].text = text[0] + ". (M) " + text[1].substr(5);
		where.options[pos].value = "M" + value.substr(1,5);
	} else {
		alert("No membership required for this court");
	}
}

function add_court_fast(court_name, court_id, where, member) {
	var text = "";
	var value = "";
	var i = where.length;
	if (member == 0) {
		text = (i + 1) + ". (G) " + court_name;
		value = " " + court_id;
	} else if (member == 1) {
		text = (i + 1) + ". (M) " + court_name;
		value = "M" + court_id;
	} else if (member == 2) {
		text = (i + 1) + ". " + court_name;
		value = "P" + court_id;
	}
	where.options[i] = new Option(text, value); 
}

// move a court on the list - check for the boundary position, retain the number, set new selection
function move_court(where, up) {
	var pos = where.selectedIndex;
	var limit;
	var other;
	if (up) { limit = 0; other = pos - 1; }
	else { limit = where.length - 1; other = pos + 1; }
	if (pos == -1) { alert("Please choose a court to move."); }
	else {
		if (pos == limit) { alert("This court cannot be moved."); }
		else {
			// switching global courts
			if (courts) {
				temp_el = courts[other];
				courts[other] = courts[pos];
				courts[pos] = temp_el;
			}
			// splitting texts
			var text1 = where.options[pos].text.split(".");
			var text2 = where.options[other].text.split(".");
			// switching values
			var temp = new Option(where.options[pos].text, where.options[pos].value);
			where.options[pos].text = text1[0] + "." + text2[1];
			where.options[pos].value = where.options[other].value;
			where.options[other].text = text2[0] + "." + text1[1];
			where.options[other].value = temp.value;
			// setting selection
			where.selectedIndex = other;
		}
	}
}

// remove a court from a list - re-number following elements
function remove_court(where) {
	var pos = where.selectedIndex;
	if (pos == -1) { 
		alert("Please choose a court to remove.");
		return;
	}
	toRemove = new Array;
	var j = 0;
	for (var i = where.length-1; i >= 0; i--) {
		if (where.options[i].selected) {
			toRemove[j] = i;
			j++;
		}
	}
	// check if there will be any court left
	if (toRemove.length == where.length) {
		alert("Please leave at least one court in your list.");
		return;
	}
	for(i = 0; i < j; i++) {
		court_id = where.options[toRemove[i]].value.substr(1,5);
		if (document.getElementById("map")) {
			currentMarker = courts[court_id][5];
			currentPoint = new GPoint(courts[court_id][0], courts[court_id][1]);
			currentDesc = courts[court_id][3];
	    	courts[court_id][5] = switchMarker(courts[court_id][5],0,courts[court_id][7],court_id,courts[court_id][6],0);
		}
		removeSingleCourt(where, toRemove[i]);
	}
}

function removeSingleCourt(where, pos) {
	var text;
	for (var i = pos+1; i < where.length; i++) {
		text = where.options[i].text.split(".");
		where.options[i].text = (text[0] - 1) + "." + text[1];
	}
	// deleting an option
	where.selectedIndex = -1;
	where.options[pos] = null;
}

// show courts total - invalid
function courts_total(where) {
	var count = where.length;
	document.write("No.: " + count);
}

// select court by id
function select_court(where, court_id) {
	var i = 0;
	while (where.options[i].value.substring(1) != court_id) { i++;}
	where.options[i].selected = true;
}

// select all courts
function select_all_courts(where) {
	for (var i = 0; i < where.length; i++)
		where.options[i].selected = true;
}

// deselect all courts
function deselect_all_courts(where) {
	where.selectedIndex = -1;
}

// toggle membership info
function member_court(where) {
	var pos = where.selectedIndex;
	if (pos == -1) { alert("Please choose a court to edit."); }
	else {
		// check if text contains (M)
		var text = where.options[pos].text.split(".");
		var value = where.options[pos].value;
		var ind = where.options[pos].text.indexOf("(M) ",0);
		if (ind == -1) { // add a member indicator
			where.options[pos].text = text[0] + ". (M)" + text[1];
			where.options[pos].value = "M" + value;
		}
		else { // remove a member indicator
			where.options[pos].text = text[0] + ". " + text[1].substr(5);
			where.options[pos].value = value.substr(0,1);
		}
	}
}
// show court info popup
function show_court_info(base_href, where) {
	var pos = where.selectedIndex;
	if (pos == -1) { alert("Please choose a court."); }
	else
	{
		window.open(base_href + '?choice=view_court&suppressAll&court_id='+where.options[pos].value,'myWin','resizable,scrollbars,width=500,height=400');
	}
}

function showOnMap(court_id) {
	if (parent.map) {
		courts = parent.courts;
		currentMarker = courts[court_id][5];
		currentDesc = courts[court_id][3];
		currentLinks = courts[court_id][4];
//		parent.map.recenterOrPanToLatLng(new parent.GPoint(courts[court_id][0], courts[court_id][1]));
		parent.map.panTo(new parent.GLatLng(courts[court_id][1], courts[court_id][0]));
	}
	// currentMarker.openInfoWindowHtml(currentDesc + currentLinks);
}
