//-----------------------------------------------------------------------------
// FUNCTION: Preload Images
//-----------------------------------------------------------------------------
tshirtimage1 = new Image();
tshirtimage1.src = "images/tshirt_front_sm.jpg";
tshirtimage2 = new Image();
tshirtimage2.src = "images/tshirt_back_sm.jpg";
tshirtimage3 = new Image();
tshirtimage3.src = "images/tshirt_front_lg.jpg";
tshirtimage3 = new Image();
tshirtimage3.src = "images/tshirt_back_lg.jpg";

//-----------------------------------------------------------------------------
// FUNCTION: Zoom In
//-----------------------------------------------------------------------------
function zoomIn(photo) {
	document.photolg.src = photo.src.replace('_sm','_lg');
	document.front.style.display = 'none';
	document.back.style.display = 'none';
	document.photolg.style.display = 'inline';
}
//-----------------------------------------------------------------------------
// FUNCTION: Zoom Out
//-----------------------------------------------------------------------------
function zoomOut() {
	document.front.style.display = 'inline';
	document.back.style.display = 'inline';
	document.photolg.style.display = 'none';
}

//-----------------------------------------------------------------------------
// FUNCTION: Add Row To Table
//-----------------------------------------------------------------------------
function addRowToTable(){
  add1RowCount();
  if (document.addtshirt.rowcount.value > 0) {
  	document.getElementById('itemtable').style.display = 'inline';
  	document.getElementById('itemtableheader').style.display = 'inline';
  	document.getElementById('itemtableremovebttn').style.display = 'inline';
  	document.getElementById('itemtableremovebttn').disabled = false;
  	document.getElementById('itemtablesubmitbttn').disabled = false;
  	document.getElementById('itemtabletotal').style.display = 'inline';
  }

  var tbl = document.getElementById('itemtable');
  var lastRow = tbl.rows.length;
  var row = tbl.insertRow(lastRow);
  var fPrice = document.addtshirt.price.value;
  var fSize = document.addtshirt.size.value;
  var fQty = document.addtshirt.qty.value;
  var fShipping = document.addtshirt.shipping.value;
  var fShipping2 = document.addtshirt.shipping2.value;
	
	// Create hidden inputs for PayPal
	var itemDesc = '\"Fight The Uglies\" T-Shirt (' + fSize + ')';
  
  	//item_name_x
  	var inputItemName = document.createElement('input');
  	inputItemName.type = 'hidden';
  	inputItemName.name = 'item_name_' + document.addtshirt.rowcount.value;
  	inputItemName.id = 'item_name_' + document.addtshirt.rowcount.value;
		inputItemName.value = itemDesc;
		
		//amount_x
  	var inputAmount = document.createElement('input');
  	inputAmount.type = 'hidden';
  	inputAmount.name = 'amount_' + document.addtshirt.rowcount.value;
  	inputAmount.id = 'amount_' + document.addtshirt.rowcount.value;
		inputAmount.value = fPrice;
		
		//quantity_x
  	var inputQuantity = document.createElement('input');
  	inputQuantity.type = 'hidden';
  	inputQuantity.name = 'quantity_' + document.addtshirt.rowcount.value;
  	inputQuantity.id = 'quantity_' + document.addtshirt.rowcount.value;
		inputQuantity.value = fQty;
		
		//shipping_x
  	var inputShipping = document.createElement('input');
  	inputShipping.type = 'hidden';
  	inputShipping.name = 'shipping_' + document.addtshirt.rowcount.value;
  	inputShipping.id = 'shipping_' + document.addtshirt.rowcount.value;
		if (document.addtshirt.rowcount.value > 1) {
			inputShipping.value = fShipping2;
		} else {
			inputShipping.value = fShipping;
		}
		
		//shipping2_x
  	var inputShipping2 = document.createElement('input');
  	inputShipping2.type = 'hidden';
  	inputShipping2.name = 'shipping2_' + document.addtshirt.rowcount.value;
  	inputShipping2.id = 'shipping2_' + document.addtshirt.rowcount.value;
		inputShipping2.value = fShipping2;
  
  //-------------------------
  // Build row, cell by cell
  //-------------------------
  var cellQty = row.insertCell(0);
  cellQty.setAttribute("align","center");
  cellQty.appendChild(document.createTextNode(fQty));
  cellQty.appendChild(inputQuantity);
  
  var cellDesc = row.insertCell(1);
  cellDesc.appendChild(document.createTextNode(itemDesc));
  cellDesc.appendChild(inputItemName);
	
	var cellAmt = row.insertCell(2);
	cellAmt.setAttribute("align","right");
	cellAmt.appendChild(document.createTextNode('$' + formatCurrency(parseFloat(fPrice) * parseFloat(fQty))));
	cellAmt.appendChild(inputAmount);
	cellAmt.appendChild(inputShipping);
	cellAmt.appendChild(inputShipping2);
	
	updateItemTotal();
	
	//reset form defaults
	document.addtshirt.size.selectedIndex = "0";
	document.addtshirt.qty.value = "1";
}

//-----------------------------------------------------------------------------
// FUNCTION: Remove Row To Table
//-----------------------------------------------------------------------------
function removeRowFromTable(){
  if (document.addtshirt.rowcount.value > 0) {
  	minus1RowCount();
  	var tbl = document.getElementById('itemtable');
  	var lastRow = tbl.rows.length;
  	if (lastRow > 1) tbl.deleteRow(lastRow - 1);
	}
  if (document.addtshirt.rowcount.value < 1) {
  	document.getElementById('itemtable').style.display = 'none';
  	document.getElementById('itemtableheader').style.display = 'none';
  	document.getElementById('itemtableremovebttn').style.display = 'none';
  	document.getElementById('itemtableremovebttn').disabled = true;
  	document.getElementById('itemtablesubmitbttn').disabled = true;
  	document.getElementById('itemtabletotal').style.display = 'none';
  }
  updateItemTotal();
}

//-----------------------------------------------------------------------------
// FUNCTION: Update Item Total
//-----------------------------------------------------------------------------
function updateItemTotal(){
	//Get item totals
	var itmTotal = 0;
	var itmTotalSH = 0;
	var itmTotalQty = 0;
	var itmCount;
	for(itmCount = 1; itmCount <= document.addtshirt.rowcount.value; itmCount++) {
		var itmAmt = document.getElementById('amount_' + itmCount).value;
		var itmQty = document.getElementById('quantity_' + itmCount).value;
		itmTotalQty = parseInt(itmTotalQty) + parseInt(itmQty);
		itmTotal = parseFloat(itmTotal) + (parseFloat(itmAmt) * parseFloat(itmQty));
	}
	//Calculate Shipping
	var fShipping = document.addtshirt.shipping.value;
  var fShipping2 = document.addtshirt.shipping2.value;
  
  if(itmTotalQty > 1) {
  	itmTotalSH = parseFloat(fShipping) + (parseFloat(fShipping2) * parseFloat(itmTotalQty - 1));
  } else {
  	itmTotalSH = parseFloat(fShipping);
  }
  //Show totals
  document.getElementById('itemsubtotaldiv').innerHTML = '$' + formatCurrency(itmTotal);
  document.getElementById('itemshippingdiv').innerHTML = '$' + formatCurrency(itmTotalSH);
	document.getElementById('itemtotaldiv').innerHTML = '$' + formatCurrency(parseFloat(itmTotal) + (parseFloat(itmTotalSH)));
}

//-----------------------------------------------------------------------------
// FUNCTION: Add 1 Row Count
//-----------------------------------------------------------------------------
function add1RowCount(){
	document.addtshirt.rowcount.value = parseInt(document.addtshirt.rowcount.value) + 1;
}

//-----------------------------------------------------------------------------
// FUNCTION: Minus 1 Row Count
//-----------------------------------------------------------------------------
function minus1RowCount(){
	document.addtshirt.rowcount.value = parseInt(document.addtshirt.rowcount.value) - 1;
}

//-----------------------------------------------------------------------------
// FUNCTION: Format Currency (no dollar sign)
//-----------------------------------------------------------------------------
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}