//Get the list of Gamers
function getListOfGamers(){
	obj = document.forms[0].listgamers;
	return obj;
}

//Get Selection from List
function getSelection(){
	obj = getListOfGamers();
	return obj.selectedIndex;
}

//Get Length
function getListLength(){
	obj = getListOfGamers();
	return obj.length;
}

//Delete Gamer from List
function delGamer(){
	listobj = getListOfGamers();
	if(confirm("Delete '" + listobj.options[getSelection()].value + "' from list?" ,"")){
		listobj.options[getSelection()] = null;
		listobj.selectedIndex = -1;
		document.forms[0].editgamer.value = "";
		listSelUpdate();
	}
	return false;
}

//Add Gamer to the list
function saveGamer(){
	idx = getSelection();
	if(idx < 0)
		nidx = getListLength();
	else
		nidx = idx;
	
	sTag = document.forms[0].editgamer.value.replace(/^\s+|\s+$/g,"");
	
	listobj = getListOfGamers();
	listobj.options[nidx] = new Option(sTag);
	listobj.selectedIndex = -1;
	document.forms[0].editgamer.value = "";
	listSelUpdate();
	
	if(idx < 0){
		alert("'" + sTag + "'\nhas been added to the list.");
	} else {
		alert("Gamertag has been changed to\n'" + sTag + "'");
	}
}

//New Gamer
function newGamer(){
	obj = getListOfGamers();
	obj.selectedIndex = -1;
	document.forms[0].editgamer.value = "";
	listSelUpdate();
}

//Updates the list of selections
function listSelUpdate(){
	listobj = getListOfGamers();
	
	document.all["tottags"].innerHTML = "List contains " + getListLength() + " friend(s)";
	
	if(getSelection() >= 0){
	    document.forms[0].newItem.disabled = false;
		//document.forms[0].saveItem.disabled = false;
		document.forms[0].delItem.disabled = false;	
		
		document.forms[0].editgamer.value = listobj.options[getSelection()].value;
		
		if(getSelection() == 0){
			document.forms[0].upItem.disabled = true;
			if(getListLength() == 1)
				document.forms[0].downItem.disabled = true;
			else
				document.forms[0].downItem.disabled = false;	
		} else {
			document.forms[0].upItem.disabled = false;
			if(getSelection() == (getListLength() - 1))
				document.forms[0].downItem.disabled = true;
			else
				document.forms[0].downItem.disabled = false;
		}
	} else {
		//document.forms[0].newItem.disabled = true;
		document.forms[0].saveItem.disabled = true;
		document.forms[0].delItem.disabled = true;
		document.forms[0].upItem.disabled = true;
		document.forms[0].downItem.disabled = true;
	}
}

//Handle edit change
function changeEdit(){
	if(document.forms[0].editgamer.value != "")
		if(getSelection() >= 0){
			listobj = getListOfGamers();
			if(listobj.options[getSelection()].value == document.forms[0].editgamer.value)
				document.forms[0].saveItem.disabled = true;
			else
				document.forms[0].saveItem.disabled = false;
		} else
			document.forms[0].saveItem.disabled = false;
	else 
		document.forms[0].saveItem.disabled = true;
		
	setTimeout("changeEdit()",200);
}

//Bump us up one
function upGamer(){
	idx = getSelection();
	listobj = getListOfGamers();
	
	//Grab and do it
	gt = listobj.options[idx].value;
	listobj.options[idx] = new Option(listobj.options[idx-1].value);
	listobj.options[idx-1] = new Option(gt);
	listobj.selectedIndex = idx - 1;
	listSelUpdate();
}

//Bump us down one
function downGamer(){
	idx = getSelection();
	listobj = getListOfGamers();
	
	//Grab and do it
	gt = listobj.options[idx].value;
	listobj.options[idx] = new Option(listobj.options[idx+1].value);
	listobj.options[idx+1] = new Option(gt);
	listobj.selectedIndex = idx + 1;
	listSelUpdate();
}

//Build String of GamerTags
function buildGamerTag(){
	sTag = "";
	listobj = getListOfGamers();
	
	for(l=0;l<getListLength();l++){
		if(l > 0)
			sTag += ",";
		
		sTag += listobj.options[l].value;
	}

	//Handle Blank condition
	if(sTag == "")
		sTag = "borocouncilman";

	document.forms[0].gamertag.value = sTag;
	
	alert("REMINDER:\nAfter your XBOX Live Friends List displays, be sure to bookmark the application in Safari.  You can even add it to your iPhone/iPod Touch home screen.\n\nIf you previously bookmarked the application and made changes, you must re-bookmark the application.");
}

//Init Page
function initPage(){
	listobj = getListOfGamers();
	listobj.selectedIndex = -1;
	listSelUpdate();
	setTimeout("changeEdit()",200);
}