function update_display() { 
	update_styles(); 
	update_spells();
	JT_init();
}
function update_styles() {
	//get current level
	var current_level = Number( $("#level").val() );
	
	// stuff by mike document.write (current_level);
	
	//get spec points
	var total_pts = Number( $("#total").html() );
	var spent_pts = Number( $("#spent").html() );
	var left_pts  = Number( $("#left").html()  );
	
	//loop through style lines
	for(line_id in styles) {
		//get current line level
		var current_line_level = Number( $("#"+line_id+"_level").val() );
		
		//loop through styles
		var levels = ''; //will contain the html for all levels
		var info = ''; //will contain the info for the current style
		for(style_id in styles[line_id]['styles']) {
			var style = styles[line_id]['styles'][style_id];
			
			//determine class for this level
			var c = ''; // will contain the class for the level
			if(style['level'] <= current_line_level) { c = 'available'; }
			else { c = 'not_avail'; }
			
			//add to the levels html
			levels = levels 
				+ "<a href='style_info.php?style_id="+style_id+"' "
				+	"name='"+style["name"]+"' "
				+ 	"class='jTip' id='level_"+style_id+"'>"
				+ 	"<span class='"+c+"'>"+style['level']+"</span></a> ";
			
			//set current style
			if(style['level'] <= current_line_level) {
				info = "<span class='item_name'>"+style['name']+"</span>"
					+ "&nbsp;::&nbsp;Pre: "+style['pre']+"&nbsp;::&nbsp;"
					+ "<span class='"+style['attack']+"'>ATT</span>-"
					+ "<span class='"+style['defense']+"'>DEF</span>-"
					+ "<span class='"+style['fatigue']+"'>FAT</span>-"
					+ "<span class='"+style['damage']+"'>DAM</span>"
					+ "&nbsp;::&nbsp;<span class='effect'>"+style['effect']+"</span>";
			}
		}
		
		//update this style line
		$("#spec_levels_"+line_id).html(levels);
		$("#spec_info_"+line_id).html(info);
		
		//update next cost for this line
		if(current_line_level < 50) {
			$("#spec_cost_"+line_id).html(current_line_level+1);	
		}
	}
}

function update_spells() {
	//get current level
	var current_level = Number( $("#level").val() );
	//get spec points
	var total_pts = Number( $("#total").html() );
	var spent_pts = Number( $("#spent").html() );
	var left_pts  = Number( $("#left").html()  );
	
	//loop through style lines
	for(line_id in spells) {
		//get current line level
		var current_line_level = Number( $("#"+line_id+"_level").val() );
		var parent_level = Number( $("#"+spells[line_id]['parent']+"_level").val() );
		
		//loop through types
		for(type_id in spells[line_id]['types']) {
			var type = spells[line_id]['types'][type_id];
			//loop through spells
			var levels = ''; //will contain the html for all levels
			var info = ''; //will contain the info for the current spell
			for(spell_id in type['spells']) {
				var spell = type['spells'][spell_id];
				
				//determine what level to check
				var check_level = 0;
				if(spells[line_id]['mastery'] != 0) {
					//this is a base-line so check current level
					check_level = current_level;
				} else {
					//this is a spec-line so check base level
					check_level = parent_level;
				}
				
				//determine class for this level
				var c = ''; // will contain the class for the level
				if(spell['level'] <= check_level) { c = 'available'; }
				else { c = 'not_avail'; }
				
				//add to the levels html
				levels = levels 
					+ "<a href='spell_info.php?spell_id="+spell_id+"' "
					+	"name='"+spell["name"]+"' "
					+ 	"class='jTip' id='level_"+spell_id+"'>"
					+ 	"<span class='"+c+"'>"+spell['level']+"</span></a> ";
				
				//set current style
				if(spell['level'] <= check_level) {
					var effect = '';
					if(spell['effect'].length > 25) { 
						effect = spell['effect'].substring(0,20) + " ...";
					} else { effect = spell['effect']; }
					info = "<span class='item_name'>"+spell['name']+"</span>"
						+"&nbsp;::&nbsp;<span class='effect'>"+effect+"</span>";
				}
			}
		
			//update this style line
			$("#spec_levels_"+type_id).html(levels);
			$("#spec_info_"+type_id).html(info);
		}
		
		//update next cost for this line
		if(current_line_level < 50  && spells[line_id]['train'] == 1) {
			$("#spec_cost_"+line_id).html(current_line_level+1);	
		}
	}
}

function inc_level(inc) {
	//get current level
	var level = Number( $("#level").val() );
	inc = Number(inc);
	
	//compute new level
	level = level < 40 ? level+inc : level+(inc/2);
	
	//check bounds
	if(level < 1)  { level = 1; }
	if(level > 50) { level = 50; }
	
	//set the level
	set_level(level); 
}

function set_level(level) {
	//check bounds
	if(level < 1)  { level = 1; }
	if(level > 50) { level = 50; }
	
	//set our current level
	$("#level").val(level);
	
	//calculate spec points
	var tot_pts = 0;
	//pts for lvl 2-5
	for (var i = 2; i <= (level<5?level:5); i++) { tot_pts += i; }			
	//pts for lvl 6-50
	for (var i = 6; i <= Math.floor(level); i++) { 
		tot_pts += Math.floor(i * spec_pts); 
	}			
	//pts for mini-dings
	for (var i = 40.5; i <= level; i++) { 
		tot_pts += Math.floor(Math.floor(i) / 2 * spec_pts); 
	}
		
	//update display
	$("#total").html( tot_pts );
	$("#left").html( tot_pts - Number( $("#spent").html() ) );
	
	update_display();
}

function inc_spec_level(spec_id, inc, auto) {
	//compute the new level
	var level = Number( $("#"+spec_id+"_level").val() );
	level = level + Number(inc);
	
	//check bounds
	if (level < 1)   { level = 1; }
	if (level > 50)  { level = 50; } 
	
	//set the level
	set_spec_level(spec_id, level, auto);
}

function set_spec_level(spec_id, level, auto) {
	var left = Number( $("#left").html() )
	var spec_spent = Number( $("#spec_spent_"+spec_id).val() );
	var current_level = Number( $("#level").val() );
	
	//auto train points
	var auto_points = 0;
	if(current_level > 8 && auto == 1) {
		for(var c = 2; c <= Math.floor(current_level / 4); c++) {
			auto_points+=c;
		}
	}
	
	//calculate the cost
	// if trying to set higher then they can,
	// set it to the highest they can
	var cost = 0;
	for (var i = 2; i <= level; i++) {
		//check and see if we have enough pts to spend
		// refund what they've already spent on this spec
		// and any auto-training points they may have
		if (cost + i > left + spec_spent + auto_points) { break; }
		//check if we are above the level
		if (i > current_level) { break; }
		cost += i;
	}
	
	//adjust for auto-train
	cost = cost <= auto_points ? 0 : cost - auto_points;
	
	//the change from the last spec
	var change = cost - spec_spent;
	
	//update values
	$("#spec_spent_"+spec_id).val(cost);
	$("#spent").html( Number( $("#spent").html() ) + change );
	$("#left").html( Number( $("#left").html() ) - change );
	$("#"+spec_id+"_level").val(i-1);
	$("#"+spec_id+"_lvl").val(i-1);
	
	//update levels
	update_display();
}

function check_input(e) {
	var key;
	var keychar;
	
	if (window.event) key = window.event.keyCode;
	else if (e) key = e.which;
	else return true;
	keychar = String.fromCharCode(key);
	
	if ( (key==null) || (key==0) || (key==8) || (key==9) 
		|| (key==13) || (key==27) ) return true;
	else if ( ("0123456789.").indexOf(keychar) > -1 ) return true;
	else return false;
}

