

function formatCurrency(num) {
    num = isNaN(num) || num === '' || num === null ? 0.00 : num;
    return parseFloat(num).toFixed(2);
}

function addEmUp() {
	var deliv_total = 0;
	var count = 0;
	var prepay_check = 0;
	var prepay_paypal = 0;
	var pay_as_you_go = 0;
	
	$('tr td.ext_price').each(function(i){
		var price = $(this).html();
		price = price.replace("$", "");
		deliv_total = deliv_total + (+price);
		count = i;
	});
	//alert(count);
	if (deliv_total > 0) {
		prepay_check = ((deliv_total * 10) - (deliv_total * 10 * 0.1)) + 25;
		prepay_paypal = ((deliv_total * 10) - (deliv_total * 10 * 0.07)) + 25;
		pay_as_you_go = deliv_total + 25;
	}

	prepay_check = '$' + formatCurrency(prepay_check);
	$('label[for="payment1"] u.payment').html(prepay_check);
	
	prepay_paypal = '$' + formatCurrency(prepay_paypal);
	$('label[for="payment2"] u.payment').html(prepay_paypal);
	
	pay_as_you_go = '$' + formatCurrency(pay_as_you_go);
	$('label[for="payment3"] u.payment').html(pay_as_you_go);
	
	deliv_total = '$' + formatCurrency(deliv_total);
	$('tr td.total').html(deliv_total);
}

$(document).ready(function(){

	// Clear all inputs in form
	$(':input','#winter_reg')
	 .not(':button, :submit, :reset, :hidden')
	 .val('')
	 .removeAttr('checked')
	 .removeAttr('selected');
	
	$('form p input[name="signature"]').val('Enter Your Full Name Here');
	
	$('tr td.qty input').keyup(function(){
		var qty = $(this).val();
		var cost = $(this).closest('tr').find('td.cost').html();
		cost = cost.replace("$", "");
		var ext_price = formatCurrency(qty * cost);
		//alert(ext_price);
		$(this).closest('tr').find('td.ext_price').html('$'+ext_price);
		addEmUp();
	
	});

	$('form#winter_reg').submit(function(){
		//var allvalues = $(this).serialize();
		//alert(allvalues);
		
		$('label input.contact_info').each(function(i){
			var text_new = $(this).val();
			var myParent = $(this).parent();
			$(this).remove();
			myParent.append(' ' + text_new);
			//this.parent.html(dsdfs)
		});		
		
		$('tr td.qty input').each(function(i){
			var qty_new = $(this).val();
			var myParent = $(this).parent();
			$(this).remove();
			myParent.html(qty_new);
			//this.parent.html(dsdfs)
		});  //end loop
		
		$('table tr td input[name="pickup"]').each(function(i){
			var selected_pickup = $(this).is(':checked');
			var myParent = $(this).parent();
			$(this).remove();
			
			if (selected_pickup) {
				myParent.html('[X]');
			}
			else {
				myParent.html('[ ]');
			}

		}); //end loop		
		
		$('form input.meat').each(function(i){
			var selected_meat = $(this).is(':checked');
			
			if (selected_meat) {
				$(this).before('[X] ');
			}
			else {
				$(this).before('[ ] ');
			}
			$(this).remove();

		}); //end loop		

		$('form p input[name="signature"]').each(function(i){
			var signature = $(this).val();
			$(this).before('<u>' + signature + '</u>').remove();
			//alert(signature);
			//$(this).remove();
		}); //end loop

		
		// ('input[name=mycheckboxofdoom').is(':checked')
		
		
		// $('input[name="newsletter"]')
		
		$('form input#submit').remove();
		
		var snapshot = $('div#left_column form').html();
		snapshot = encodeURIComponent(snapshot); //encode the html to uri
		//alert(snapshot);
		
		$.ajax({
			type: "POST",
			url: '/winter_registration.php',
			data: "submitted=true&snapshot=" + snapshot,
			success: function(msg){
										
				//if (msg == '1') {
					$('div#left_column').html('<h3>Thank you for your submission.</h3>');
				/*}
				else {
					alert(msg);  //display any php message 
				}*/
			} // success
				
		}); //ajax
		
		return false;  //kills normal form handler functions
	});




});
