var iBattleships = 0;
var iBattlecruisers = 0;
var iCruisers = 0;
var iDestroyers = 0;
var iFrigates = 0;

var iMaxShips = 5;
var iMaxPoints = 28;


function calculatePoints(){
	var iTotal = 0;
	iTotal += parseInt(ships.factionbs.value) + parseInt(ships.t3bs.value) + parseInt(ships.t2bs.value) + parseInt(ships.t1bs.value);
	iTotal += parseInt(ships.cmdship.value) + parseInt(ships.t2bc.value) + parseInt(ships.t1bcs.value);
	iTotal += parseInt(ships.recon.value) + parseInt(ships.logistics.value) + parseInt(ships.hac.value);
	iTotal += parseInt(ships.inties.value) + parseInt(ships.afs.value) + parseInt(ships.cruisers.value) + parseInt(ships.dictors.value) + parseInt(ships.destroyers.value) + parseInt(ships.frigates.value);
	points.innerHTML = iMaxPoints - iTotal;
	return iTotal;

}

function calculateShips(){
	var iTotal = iBattleships + iBattlecruisers + iCruisers + iDestroyers + iFrigates;
	shipTotal.innerHTML = iTotal;
	return iTotal;
}

function updateBattleship(strShipType){
	if (strShipType == "factionbs"){
		if (ships.factionbs.selectedIndex > 0){
			iBattleships = 1;
		} else {
			iBattleships = 0;
		}
	} else {
		ships.factionbs.selectedIndex = 0;
	}
	if (strShipType == "t3bs"){
		if (ships.t3bs.selectedIndex > 0){
			iBattleships = 1;
		} else {
			iBattleships = 0;
		}
	} else {
		ships.t3bs.selectedIndex = 0;
	}
	if (strShipType == "t2bs"){
		if (ships.t2bs.selectedIndex > 0){
			iBattleships = 1;
		} else {
			iBattleships = 0;
		}
	} else {
		ships.t2bs.selectedIndex = 0;
	}
	if (strShipType == "t1bs"){
		if (ships.t1bs.selectedIndex > 0){
			iBattleships = 1;
		} else {
			iBattleships = 0;
		}
	} else {
		ships.t1bs.selectedIndex = 0;
	}
	calculateShips();
	calculatePoints();
}

function updateBattlecruisers(){
	iBattlecruisers = 0;

	if (ships.cmdship.selectedIndex > 0){
		iBattlecruisers++;
	}
	if (ships.t2bc.selectedIndex > 0){
		iBattlecruisers++;
	}
	iBattlecruisers += ships.t1bcs.selectedIndex;
	
	calculateShips();
	calculatePoints();
}

function updateCruisers(){
	iCruisers = 0;
	
	if (ships.hac.selectedIndex > 0){
		iCruisers++;
	}
	if (ships.recon.selectedIndex > 0){
		iCruisers++;
	}
	if (ships.logistics.selectedIndex > 0){
		iCruisers++;
	}
	iCruisers += ships.cruisers.selectedIndex;
	
	calculateShips();
	calculatePoints();
}

function updateDestroyers(){
	iDestroyers = ships.destroyers.selectedIndex + ships.dictors.selectedIndex;
	
	calculateShips();
	calculatePoints();
}

function updateFrigates() {
	iFrigates = ships.frigates.selectedIndex + ships.afs.selectedIndex + ships.inties.selectedIndex;
	
	calculateShips();
	calculatePoints();
}

function shipReset(){
	iCruisers = 0;
	iBattlecruisers = 0;
	iBattleships = 0;
	iFrigates = 0;
	iDestroyers = 0;
	
	calculateShips();
	calculatePoints();
}

function displayListing(){
	var strListing = "<b>Your Fleet Setup:</b><br/>";
	if (ships.factionbs.selectedIndex > 0){
		strListing += ships.factionbs.options[ships.factionbs.selectedIndex].text + " (" + ships.factionbs.value + ")" + "<br/>";
	}
	if (ships.t3bs.selectedIndex > 0){
		strListing += ships.t3bs.options[ships.t3bs.selectedIndex].text + " (" + ships.t3bs.value + ")" + "<br/>";
	}
	if (ships.t2bs.selectedIndex > 0){
		strListing += ships.t2bs.options[ships.t2bs.selectedIndex].text + " (" + ships.t2bs.value + ")" + "<br/>";
	}
	if (ships.t1bs.selectedIndex > 0){
		strListing += ships.t1bs.options[ships.t1bs.selectedIndex].text + " (" + ships.t1bs.value + ")" + "<br/>";
	}
	if (ships.cmdship.selectedIndex > 0){
		strListing += ships.cmdship.options[ships.cmdship.selectedIndex].text + " (" + ships.cmdship.value + ")" + "<br/>";
	}
	if (ships.t2bc.selectedIndex > 0){
		strListing += ships.t2bc.options[ships.t2bc.selectedIndex].text + " (" + ships.t2bc.value + ")" + "<br/>";
	}
	if (ships.t1bcs.selectedIndex > 0){
		strListing += ships.t1bcs.options[ships.t1bcs.selectedIndex].text + " (" + ships.t1bcs.value + ")" + "<br/>";
	}
	if (ships.hac.selectedIndex > 0){
		strListing += ships.hac.options[ships.hac.selectedIndex].text + " (" + ships.hac.value + ")" + "<br/>";
	}
	if (ships.logistics.selectedIndex > 0){
		strListing += ships.logistics.options[ships.logistics.selectedIndex].text + " (" + ships.logistics.value + ")" + "<br/>";
	}
	if (ships.recon.selectedIndex > 0){
		strListing += ships.recon.options[ships.recon.selectedIndex].text + " (" + ships.recon.value + ")" + "<br/>";
	}
	if (ships.cruisers.selectedIndex > 0){
		strListing += ships.cruisers.options[ships.cruisers.selectedIndex].text + " (" + ships.cruisers.value + ")" + "<br/>";
	}
	if (ships.afs.selectedIndex > 0){
		strListing += ships.afs.options[ships.afs.selectedIndex].text + " (" + ships.afs.value + ")" + "<br/>";
	}
	if (ships.inties.selectedIndex > 0){
		strListing += ships.inties.options[ships.inties.selectedIndex].text + " (" + ships.inties.value + ")" + "<br/>";
	}
	if (ships.dictors.selectedIndex > 0){
		strListing += ships.dictors.options[ships.dictors.selectedIndex].text + " (" + ships.dictors.value + ")" + "<br/>";
	}
	if (ships.destroyers.selectedIndex > 0){
		strListing += ships.destroyers.options[ships.destroyers.selectedIndex].text + " (" + ships.destroyers.value + ")" + "<br/>";
	}
	if (ships.frigates.selectedIndex > 0){
		strListing += ships.frigates.options[ships.frigates.selectedIndex].text + " (" + ships.frigates.value + ")" + "<br/>";
	}
	
	var iShips = calculateShips();
	var iPoints = calculatePoints();

	strListing += "<br/>This fleet setup uses "+iShips+ " of the "+iMaxShips+ " ships available.";
	if (iShips > iMaxShips || iShips == 0){
		strListing += " (INVALID)";
	}
	strListing += "<br/>This fleet setup uses " +iPoints+" of the "+iMaxPoints+" points available.";
	if (iPoints > iMaxPoints){
		strListing += " (INVALID)";
	}
	listing.innerHTML = strListing;
}
	