/* These are the functions for OrderForm.aspx */
function buyProduct( senderID, targetID, amount ){ 
  text = document.getElementById(senderID).value;
  document.getElementById(targetID).value = formatAsMoney( text * amount );
  sumUp();
}

function sumUp()
{
	a = document.getElementById('Total1').value;
	b = document.getElementById('Total2').value;
	c = document.getElementById('Total3').value;
	d = document.getElementById('Total4a').value;
	e = document.getElementById('Total4b').value;
	
	subtotal = (a * 1) + (b * 1) + (c * 1) + (d * 1) + (e * 1);
		
	discount = document.getElementById('Discount').value;
	
	if( ( document.getElementById('State').value.toUpperCase() == 'AZ' ) && !document.getElementById('exempt').checked )
		tax = ((subtotal + (discount*1)) * 5.6) /100;
	else
		tax = 0;
	
	ship1 = document.getElementById('Ship1').value;
	ship2 = document.getElementById('Ship2').value;
	ship3 = document.getElementById('Ship3').value;
	
	shipping = (ship1*1) + (ship2*1) + (ship3*1);
	
	document.getElementById('SubTotal').value = formatAsMoney(subtotal);	
	document.getElementById('Tax').value = formatAsMoney(tax);
	document.getElementById('Total').value = formatAsMoney( (subtotal * 1) + (tax  * 1) + (discount * 1) + (shipping * 1) );
}

/* These are the functions for RegistrationForm.aspx */
function registerWS( partNum, price, targetID ){ 
  a = document.getElementById(partNum).value;
  b = document.getElementById(price).value;  
  
  document.getElementById(targetID).value = formatAsMoney( a * b ); 
  sumUp2();
}

function sumUp2()
{
	a = document.getElementById('Total1').value;
	b = document.getElementById('Total2').value;
	c = document.getElementById('Total3').value;
	
	ship1 = document.getElementById('Ship1').value;
	
	subtotal = (a * 1) + (b * 1) + (c * 1) + (ship1 * 1);
	
	discount = document.getElementById('Discount').value;
	
	shipping = (ship1*1);
	
	document.getElementById('SubTotal').value = formatAsMoney( subtotal );	
	document.getElementById('Total').value = formatAsMoney( (subtotal * 1) + (discount * 1) );
}

// useful function from http://www.rgagnon.com/jsdetails/js-0076.html
function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}


// For calculating Discount
function calculateDisc( form ){
	if( form == 'pub' ){
		isCodeValid('DiscCode', 'DiscAmount', document.getElementById('SubTotal').value *1 );
		buyProduct('DiscAmount','Discount', -1);
	} else {
		//isCodeValid('DiscCode', 'DiscAmount'); //No valid discount for workshops as for now
		registerWS('DiscAmount','negative', 'Discount');
	}
}