var optionPrices 	= new Array();
var printSetup 		= new Array();
var printPerShirt	= new Array();


optionPrices[0] = 0.00;
optionPrices[1] = 24.99;
optionPrices[2] = 29.99;
optionPrices[3] = 39.99;

//printing setup costs
printSetup[0] = 0;
printSetup[1] = 19.99;
printSetup[2] = 24.99;

//print per shirt costs
printPerShirt[0] = 2.99;
printPerShirt[1] = 2.99;
printPerShirt[2] = 3.49;

//init sections
calcBadges 		= false;
calcInitials 	= false;
calcNames 		= false;
calcNumbering 	= false;
calcPrinting 	= false;

//calcBadges      = 0;
//calcInitials    = 0;
//calcNames       = 0;
//calcNumbering   = 0;
//calcPrinting    = 0;

/**
* the following array's control which of the embroidery
* fields are visible for each option.
*
* it may seem like a lot of code but this is the simplest
* and most flexible way to do it.
*/
var displayElem = Array();
												
displayElem[0] = Array();
displayElem[1] = Array();
displayElem[2] = Array();
displayElem[3] = Array();
displayElem[4] = Array();
displayElem[5] = Array();
												
displayElem[0]['faboveArea'] = 'none';
displayElem[0]['fbelowArea'] = 'none';
displayElem[0]['finsetArea'] = 'none';
displayElem[0]['embroidered'] = 'none';
												
displayElem[1]['faboveArea'] = 'none';
displayElem[1]['fbelowArea'] = 'none';
displayElem[1]['finsetArea'] = 'block';
												
displayElem[2]['faboveArea'] = 'block';
displayElem[2]['fbelowArea'] = 'block';
displayElem[2]['finsetArea'] = 'none';
displayElem[2]['embroidered'] = 'none';
												
displayElem[3]['faboveArea'] = 'block';
displayElem[3]['fbelowArea'] = 'none';
displayElem[3]['finsetArea'] = 'none';
displayElem[3]['embroidered'] = 'none';
												
displayElem[4]['faboveArea'] = 'none';
displayElem[4]['fbelowArea'] = 'block';
displayElem[4]['finsetArea'] = 'none';
displayElem[4]['embroidered'] = 'none';
												
displayElem[5]['faboveArea'] = 'none';
displayElem[5]['fbelowArea'] = 'none';
displayElem[5]['finsetArea'] = 'none';
displayElem[5]['embroidered'] = 'block';

/**
* update prices when a new size option is selected
*/
function updatePrices(dropfield, priceid){
	//init
	var pricefield 		= document.getElementById(priceid);
	
	//both fields set>
	if(dropfield && pricefield){
		sel = dropfield.selectedIndex;
		
		newprice = optionPrices[sel];
		
		pricefield.innerHTML = newprice.toFixed(2);
	}
	
	calcTotals();
}

function setCheckbox(box){
	//box already checked?
	if(box.checked){
		
		//clear all
		for(i = 1; i <= 3; i++){
			var tempfield = document.getElementById('printing' + i);
			
			tempfield.checked = false;
		}
		
		box.checked = true;
	}
	
	calcTotals();
}

function calcTotals(){
	//init
	var subfield 	= document.getElementById('subtotal');
	var totalfield 	= document.getElementById('totalcost');
	var subtotal = 0;
	var total 	 = 0;
	
	//loop and grab individual costs
	for(i = 1; i <= 15; i++){
		subtotal += Number(eval('document.getElementById(\'price' + i + '\').innerHTML'));
	}
		
	//get sub total
	total = subtotal;
	
	//badges cost to be calculated?
	if(calcBadges){
			
		total += Number(document.getElementById('badgecost').innerHTML);
	}
	//names to be calculated
	if(calcNames){
		calcNamesTotals();
		
		total += Number(document.getElementById('playercost').innerHTML);
	}
	//numbering to be calculated?
	if(calcNumbering){
		//need to calculate cost of product numbering
		calcNumberTotals();
		
		//add this cost to total
		total += Number(document.getElementById('numbercost').innerHTML);
	}
	//printing cost to be calculated
	if(calcPrinting){
		calcPrintingTotals();
		
		total += Number(document.getElementById('printingcost').innerHTML);
	}
	//badges cost to be calculated
	if(calcBadges){
		calcBadgesTotals();
		
		total += Number(document.getElementById('badgecost').innerHTML);
	}
	
	//set fields
	subfield.innerHTML = subtotal.toFixed(2);
	totalfield.innerHTML = total.toFixed(2);
	
}

function calcNumberTotals(){
    	var numberfield = document.getElementById('numbercost');
    	var frontcheck	= document.getElementById('shirtfront');
    	var rearcheck 	= document.getElementById('shirtrear');
    	var numbertotal = 0;
    	
    	if(numberfield && (frontcheck.checked == true || rearcheck.checked == true)){
    		var fieldcount = 0;
    		
    		for(i = 1; i <= 15; i++){
    			var tempfield = document.getElementById('prod' + i + 'no');
    			
    			if(tempfield.selectedIndex != 0){
    				fieldcount++;
    			}
    		}
    		
    		if(fieldcount >= 13){
    			if(frontcheck.checked == true){
    				//numbertotal += 16.99;
    				numbertotal += 19.99;
    			}
    			
    			if(rearcheck.checked == true){
    				//numbertotal += 19.99;
    				numbertotal += 24.99;
    			}	
    		} else {			
    			if(frontcheck.checked == true){				
    				numbertotal += (fieldcount * 1.85);
    			}
    			
    			if(rearcheck.checked == true){				
    				numbertotal += (fieldcount * 2.85);
    			}	
    		}
    		
    		numberfield.innerHTML =  numbertotal.toFixed(2);
    	}
}

function calcNamesTotals(){
	var playerfield = document.getElementById('playercost');
	var playertotal = 0;
	
	if(playerfield){
		for(i = 1; i <= 15; i++){
			var tempfield = document.getElementById('prod' + i + 'name');
			
			if(tempfield.value.length != 0){
				playertotal += 2.99;
			}
		}
		
		playerfield.innerHTML =  playertotal.toFixed(2);
	}
}

function calcPrintingTotals(){
	var printingfield 	= document.getElementById('printingcost');
	var bypass 			= document.getElementById('printingbypass');
	var printing1 		= document.getElementById('printing1');
	var printing2 		= document.getElementById('printing2');
	var printing3 		= document.getElementById('printing3');
	var printingtotal 	= 0;
	
	if(bypass.checked != true){
		if(printing2.checked == true){
			printingtotal += printSetup[1];
		}
		if(printing3.checked == true){
			printingtotal += printSetup[2];
		}
	}
	
	for(i = 1; i <= 15; i++){
		var tempfield = document.getElementById('prod' + i + 'size');
			
		if(tempfield.selectedIndex != 0){
			if(printing1.checked == true || printing2.checked == true){
				printingtotal += (2.99);
			
			} else if(printing3.checked == true){
				printingtotal += (3.49);
			}
		}
	}
		
	printingfield.innerHTML = printingtotal.toFixed(2);	
}

function calcBadgesTotals(){
    var badgefield  = document.getElementById('badgecost');
    var badgeselect = document.getElementById('fclubbadge');
	var badgetotal = 0;
	
	if(badgeselect.selectedIndex > 0){
		for(i = 1; i <= 15; i++){
    		var tempfield = document.getElementById('prod' + i + 'size');
    		if(tempfield.selectedIndex > 0){
			    badgetotal += 2.99;
		    }
		}
		
		if(badgeselect.options[badgeselect.selectedIndex].value == 5){
    		var bypass = document.getElementById('fclubbadgebypass');
    		
    		if(bypass.checked != true){
        		badgetotal += 30.00;
    		}
		}
	}
	
	badgefield.innerHTML =  badgetotal.toFixed(2);
}

function init(){
    var badgeSelect = document.getElementById("fclubbadge");
    												
    if(badgeSelect){
        badgeSelect.onchange = function(){
            var selection = badgeSelect.options[badgeSelect.selectedIndex].value;
    												    
    		for(i in displayElem[selection]){
    		    eval("document.getElementById('" + i + "').style.display='" + displayElem[selection][i] + "'");
    		}
    		
    		calcTotals();
    	}
    }
    /*
    for(j = 1; j <= 15; j++){
        var elemName    = 'prod' + j + 'size';
        var priceName   = 'price' + j;
        
        var elemObj = document.getElementById(elemName);
        
        elemObj.onchange = function(){
            updatePrices(this, priceName);
        }
    }
    */
}										

/**
*
*
*
*/
if(window.attachEvent){
    window.attachEvent('onload', init);
} else {
    window.addEventListener('load', init, false);
}