//global variables that can be used by ALL the function son this page.
var inputs;
var imgFalse = '/images/false.png';
var imgTrue = '/images/true.png';

//this function runs when the page is loaded, put all your other onload stuff in here too.
function init() {
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

	replaceChecks();
}

function replaceChecks() {
	
	//get all the input fields on the page
	inputs = document.getElementsByTagName('input');
    select = document.getElementById("Pages");
	var pageCost=0;
	var total=0;
	var txt='';
	var total = parseInt(document.getElementById('total').innerHTML);
	//cycle trough the input fields
	for(var i=0; i < inputs.length; i++) {

		//check if the input is a checkbox
		if(inputs[i].getAttribute('type') == 'checkbox') {
			
			//create a new image
			var img = document.createElement('img');
			
			
			//check if the checkbox is checked
			if(inputs[i].checked) {				
				img.src = imgTrue;
				var value = parseInt(inputs[i].value);	
				total += value;
			} else {
				img.src = imgFalse;
			}
			
			//set image ID and onclick action
			img.id = 'checkImage'+i;
			//set image 
			img.onclick = new Function('checkChange('+i+')');
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
		}
	}
	
	if((select.value != 0) || (select.change)) {
		total += parseInt(select.value);
	}
	
	document.getElementById('total').innerHTML = total;
}
	
//change the checkbox status and the replacement image
function checkChange(i) {

	var total = parseInt(document.getElementById('total').innerHTML);
	
	if(inputs[i].checked) {
		inputs[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalse;	
		var value = parseInt(inputs[i].value);	
		var sum = total - value;
		document.getElementById('total').innerHTML = sum;
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrue;
		var value = parseInt(inputs[i].value);	
		var sum = total + value;
		document.getElementById('total').innerHTML = sum;
	}
}

function updateTotal() {
	inputs = document.getElementsByTagName('input');
	var total=990;
	
	for(var i=0; i < inputs.length; i++) {
		if(inputs[i].checked) {
			var value = parseInt(inputs[i].value);	
			total += value;
		}
	}
	total += parseInt(select.value);
	document.getElementById('total').innerHTML = total;
}

/* for Mozilla */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script defer src=/js/ie_onload.js><"+"/script>");
 /*@end @*/

/* for other browsers */ 
window.onload = init;
