var explainCopies;
var ccInfo;

function prepareForm() {
	if(!document.getElementById) return false;
	if(!document.getElementById('ccInfo')) return false;
	
	explainCopies = document.getElementById('explainCopies');
	ccInfo = document.getElementById('ccInfo');
	
	var needExplain = document.getElementById('gen_file').checked;
	if(!needExplain) {
		explainCopies.style.display = 'none';
	}
	
	if(document.getElementById('totalPlaceHolder')) {
		var totalPlaceHolder = document.getElementById('totalPlaceHolder')
		var currentTotal = totalPlaceHolder.value;
		updateTotal(currentTotal);
		ccInfo.style.display = 'block';
	}else{
		ccInfo.style.display = 'none';
	}

/////////////////////////////////////////////////////////////////
//
//--check boxes and number of copies inputs
//
//
	//----first grab all of the checkboxes and inputs
	var trans_gen = document.getElementById('trans_gen');
	var trans_genNum = document.getElementById('trans_genNum');
	var trans_sealed = document.getElementById('trans_sealed');
	var trans_sealedNum = document.getElementById('trans_sealedNum');
	var diploma_gen = document.getElementById('diploma_gen');
	var diploma_genNum = document.getElementById('diploma_genNum');
	var diploma_official = document.getElementById('diploma_official');
	var diploma_officialNum = document.getElementById('diploma_officialNum');
	var gen_file = document.getElementById('gen_file');
	var gen_fileNum = document.getElementById('gen_fileNum');
	
	var inputs = [
					[trans_gen, trans_genNum, 'transGenCopy', trans_genNum.value], 
					[trans_sealed, trans_sealedNum, 'transSealedCopy', trans_sealedNum.value],
					[diploma_gen, diploma_genNum, 'diplomaGenCopy', diploma_genNum.value],
					[diploma_official, diploma_officialNum, 'diplomaOfficialCopy', diploma_officialNum.value],
					[gen_file, gen_fileNum, 'genFileCopy', gen_fileNum.value]
	];
	
	for(var i=0; i<inputs.length; i++) {
		if(inputs[i][1].value == '') {
			inputs[i][1].value = 0;
		}
		
	// first create the check box behavior
		inputs[i][0].numOfCopies = inputs[i][1];
		inputs[i][0].stringValue = inputs[i][2];
		
		inputs[i][0].onclick = function() {
			if(this.checked == true) {
				if(this.numOfCopies.value == '' || this.numOfCopies.value == 0) {
					this.numOfCopies.value = 1;
				}
			}else{
				this.numOfCopies.value = 0;
			}
			calculateTotal(this.stringValue, this.numOfCopies.value);
		}
	// second create the number of copies input behavior
		inputs[i][1].checkBox = inputs[i][0];
		inputs[i][1].stringValue = inputs[i][2];
		
		inputs[i][1].onchange = function() {
			if(this.value != 0) {
				if(this.checkBox.checked == false) {
					this.checkBox.checked = true;
				}
			}else{
				this.checkBox.checked = false;
			}
			calculateTotal(this.stringValue, this.value);
		}
	}

// alright, so we need to set a starting number for each of the items
// if they have none selected, set them to 0
// if they have an amount already (sent to POST) then multiply that number by it's item price
	if(inputs[0][3] == '') {
		trans_genAmount = 0;
	}else{
		trans_genAmount = (parseFloat(inputs[0][3]) * 5) - 5;
		if(trans_genAmount < 0) {
			trans_genAmount = 0;
		}
	}
	
	if(inputs[1][3] == '') {
		trans_sealedAmount = 0;
	}else{
		trans_sealedAmount = parseFloat(inputs[1][3]) * 10;
	}
	
	if(inputs[2][3] == '') {
		diploma_genAmount = 0;
	}else{
		diploma_genAmount = parseFloat(inputs[2][3]) * 5;	
	}
	
	if(inputs[3][3] == '') {
		diploma_officialAmount = 0;
	}else{
		diploma_officialAmount = parseFloat(inputs[3][3]) * 20;	
	}
	
	if(inputs[4][3] == '') {
		gen_fileAmount = 0;
	}else{
		gen_fileAmount = parseFloat(inputs[4][3]) * .25;	
	}
}

////////////////////////////// updating the total amount and display ////////////////////////////

function calculateTotal(item, quantity) {
	switch(item) {
		case 'transGenCopy':
			if(quantity > 0) {
				trans_genAmount = (quantity * 5) - 5;
			}else{
				trans_genAmount = 0;
			}
			break
		case 'transSealedCopy':
			if(quantity > 0) {
				trans_sealedAmount = quantity * 10;
			}else{
				trans_sealedAmount = 0;
			}
			break
		case 'diplomaGenCopy':
			if(quantity > 0) {
				diploma_genAmount = quantity * 5;
			}else{
				diploma_genAmount = 0;
			}
			break
		case 'diplomaOfficialCopy':
			if(quantity > 0) {
				diploma_officialAmount = quantity * 20;
			}else{
				diploma_officialAmount = 0;
			}
			break
		case 'genFileCopy':
			if(quantity > 0) {
				gen_fileAmount = quantity * .25;
				explainCopies.style.display = 'block';
			}else{
				gen_fileAmount = 0;
				explainCopies.style.display = 'none';
			}
			
	}
	var totalDue = trans_genAmount;
	totalDue += trans_sealedAmount;
	totalDue += diploma_genAmount;
	totalDue += diploma_officialAmount;
	totalDue += gen_fileAmount;
	
	updateTotal(totalDue);
}
	
function updateTotal(newTotal) {
	if(typeof(newTotal) == 'number') {
		newTotal = newTotal.toFixed(2);
	}
	if(newTotal > 0.00) {
		var totalHolder = document.getElementById('total');
		if(totalHolder.firstChild) {
			totalHolder.firstChild.nodeValue = newTotal;
		}else{
			var totalText = document.createTextNode(newTotal);
			totalHolder.appendChild(totalText);
		}
		ccInfo.style.display = 'block';
		
		if(!document.getElementById('postTotal')) {
			var hiddenTotal = document.createElement('input');
			hiddenTotal.setAttribute('type', 'hidden');
			hiddenTotal.setAttribute('name', 'total');
			hiddenTotal.setAttribute('id', 'postTotal');
			document.getElementById('ccTotal').appendChild(hiddenTotal);
		}
		var postTotal = document.getElementById('postTotal');
		postTotal.value = newTotal;
	}else{
		var parentElem = document.getElementById('ccTotal')
		var targetElem = document.getElementById('postTotal');
		parentElem.removeChild(targetElem);
		ccInfo.style.display = 'none';
	}
}
addLoadEvent(prepareForm);