loaded=false

for (i=0; i<5; i++) {
	curAtt=attributeNames[i]
	redrawAttribute(curAtt)
	for (k=0; k<skillNames[curAtt].length; k++) {
		redrawSkill(skillNames[curAtt][k])
	}
}

loaded=true

redrawTally()

function incrementAttribute(attribute) {
	if (attributes[attribute]<5) {
		attributes[attribute]++
		redrawAttribute(attribute)
	}
}

function decrementAttribute(attribute) {
	if (attributes[attribute]>attributeMins[attribute]) {
		attributes[attribute]--
		redrawAttribute(attribute)
	}
}

function incrementSkill(skill) {
	if (skills[skill]<5) {
		skills[skill]++
		redrawSkill(skill)
	}
}

function decrementSkill(skill) {
	if (skills[skill]>skillMins[skill]) {
		skills[skill]--
		redrawSkill(skill)
	}
}

function redrawAttribute(attribute) {
	value=attributes[attribute]
	toWrite=diceTypes[value]
	toWrite+="<br>"
	for (j=0; j<5; j++) {
		toWrite+="<img src='"
		if (j<value) {
			toWrite+="closedDot.gif"
		} else {
			toWrite+="openDot.gif"
		}
		toWrite+="'>"
	}
	document.getElementById(attribute).innerHTML=toWrite
	if (loaded)
		redrawTally()
}

function redrawSkill(skill) {
	value=skills[skill]
	toWrite=diceTypes[value]
	document.getElementById(skill+"Die").innerHTML=toWrite
	toWrite=""
	for (j=0; j<5; j++) {
		toWrite+="<img src='"
		if (j<value) {
			toWrite+="closedDot.gif"
		} else {
			toWrite+="openDot.gif"
		}
		toWrite+="'>"
	}
	document.getElementById(skill+"Dots").innerHTML=toWrite
	if (loaded)
		redrawTally()

}

function redrawTally() {
	skillPoints=0
	attributePoints=-5
	
	for (i=0; i<5; i++) {
		curAtt=attributeNames[i]
		att=attributes[curAtt]
		attributePoints+=att
		for (j=0; j<skillNames[curAtt].length; j++) {
			value=skills[skillNames[curAtt][j]]
			if (value>att) {
				skillPoints+=2*value-att
			} else {
				skillPoints+=value
			}
		}
	}
	document.getElementById("pointTally").innerHTML="<b>Attribute points spent:</b> "+attributePoints+"<br><b>Skill points spent:</b> "+skillPoints
	
	output()
}

function output() {
	toWrite="<table id=outputTable><tr>"
	for (i=0; i<5; i++) {
		curAtt=attributeNames[i]
		capitalAtt=curAtt.substring(0,1).toUpperCase()+curAtt.substring(1)
		toWrite+="<td><span class=outputAttributeName><b>"+capitalAtt+":</b> "+diceTypes[attributes[curAtt]]+"</span><br>"
		for (j=0; j<skillNames[curAtt].length; j++) {
			curSkill=skillNames[curAtt][j]
			value=skills[curSkill]
			if (value>0) {
				if (curSkill.indexOf("cust")!=-1) {
					curSkill=document.getElementById(curSkill+"Input").value
				}
				capitalSkill=curSkill.substring(0,1).toUpperCase()+curSkill.substring(1)
				toWrite+="<b>"+capitalSkill+":</b> "+diceTypes[value]+"<br>"
			}
		}
		toWrite+="</td>"
	}
	toWrite+="</tr></table>"
	document.getElementById("output").innerHTML=toWrite
}

function msg(toWrite) {
	document.getElementById("output").innerHTML+=toWrite+"<br>"
}