
function update_total_price() {
	var courses = courses_selected();
	var course_count = courses.length;
	var course_ids = [];
	var total_units = 0;
	var old_classes = 0;
	var ce_credit_hours = 0;
	for( var course_index = 0; course_index < course_count; course_index++ ) {
		var course_id = courses[course_index].id.match( /\d+$/ )[0];
		course_ids.push( course_id );
		if( course_descriptions[course_id].price != null ) {
			old_classes += parseFloat(course_descriptions[course_id].price);
		} else if( !course_descriptions[course_id].is_free ) {
			total_units += course_descriptions[course_id].units;
		}
		if (course_descriptions[course_id].ce ==1) {
			ce_credit_hours += parseFloat(course_descriptions[course_id].duration);
		}
	}
	var total_price = 0;
	var price_per_unit = 0;
	if( total_units > 9 ) {
		total_price = (offers[10].price_per_unit * total_units) - 0.05;
		price_per_unit = offers[10].price_per_unit;
	} else {
		total_price = offers[total_units].total_price;
		price_per_unit = offers[total_units].price_per_unit;
	}

	total_price += old_classes;

	var new_price_box;
 	var price_box = ID("price_box");
	clear(price_box);

	price_box.appendChild( _( "span", null, _T( "Selected classes:" ) ) );
	var class_list = _( "ul", null );
	price_box.appendChild( class_list );;

	var price;
	var units;
	var full_price = 0;
	for( var i = 0; i < course_ids.length; i++ ) {
		course_id = course_ids[i];
		units = course_descriptions[course_id].units;

		if( course_descriptions[course_id].price != null ) {
			price = course_descriptions[course_id].price;
		} else {
			price = offers[units].total_price;
		}

		full_price += parseFloat( price, 10 );
		price = Math.round( price * 100 )/100;

		class_list.appendChild(
			_( "li", null,
				_T( course_descriptions[course_id].name ),
				_T( " ($" + price + ")" )
			)
		);
	}

	var discount = full_price - total_price;
	full_price = format_price( full_price );
	total_price = format_price( total_price );	
	discount = format_price( discount );

	if( discount > 0 ) {
		price_box.appendChild(
			_( "p", { id: "full_price" }, _T( "Full Price: $"),
				_( "span", null, _T( full_price ))
			)
		);
		price_box.appendChild(
			_( "p", { id: "discount" }, _T( "Discount: -$" ),
				_( "span", null, _T( discount ))
			)
		);
		price_box.appendChild(
			_( "p", { id: "total_price" }, _T( "You Pay: $" ),
				_( "span", null, _T( total_price ))
			)
		);
	} else {
		price_box.appendChild(
			_( "p", { id: "total_price" }, _T( "Total: $" ),
				_( "span", null, _T( total_price ) )
			)
		);
	}
	if (ce_credit_hours >0) {
		price_box.appendChild(
			_( "p", { id: "credit_hours" }, _T( "CE Credit Hours: " ),
				_( "span", null, _T( ce_credit_hours ) )
			)
		);
	}
}

function courses_selected() {
	var inputs = YAHOO.util.Selector.query( "input[name=course_id]", "courses" );
	var courses = [];
	for( var i = 0; i < inputs.length; i++ ) {
		if( inputs[i].checked && !inputs[i].disabled ) {
			courses.push( inputs[i] );
		}
	}

	return courses;
}

function format_price( price ) {
	price = Math.round( price * 100 ) / 100;
	price = price.toString();
	if( /\.\d$/.test( price ) ) {
		price += "0";
	}
	return price;
}
