
$().ready(function() {
	var template = jQuery.format($("#template").val());
	//var randomnumber;
	function addRow() {
		$(template(i++)).appendTo("#orderitems tbody");
	}

	function rand () {
	  return Math.random ();
	}
	
	var i = 1;
	// start with one row
	addRow(); addRow();addRow();
	// add more rows on click
	$("#add").click(addRow);
	//var totals = 0;
	// check keyup on width inputs to update totals field
	$("#orderform").delegate("focusout", ".trclass input", function(event) {
		var totals = 0;
		var total_clips =0;
		var total_clips_fee = 0;
		$("#orderitems .trclass").each(function() {
			var totalQuantity = 0;
			var totalCost = 0;
			var clips =0;
			var cost=0;
			var price =  7.65;
			var clips_fee = 5.00;
			var extra_cost = 0;
			price = isNaN(price) ? 0 : price;
			var width = ($('.width input', this).val());
			var height = ($('.height input', this).val());
			var anchor_id =($(".anchors select",this).val());
			var attachment_id =($(".attachment select",this).val());
			
			if (isNaN(Number(width))) width = 0;
			if (isNaN(Number(height))) height = 0;
			if (isNaN(Number(price))) total = 0;
			if (isNaN(Number(total_clips))) total_clips = 0;
			if (isNaN(Number(clips))) clips = 0;
			
			width = Math.ceil(width * 4) / 4;
            height = Math.ceil(height * 4) / 4;
			
			// Calculating the Clips Counts
			if(anchor_id == 5) {
				clips = Math.ceil(height/12)*2;
				// Add 2 extra clips per side
				clips = clips + 4;
				//clips = (((height)/12) + (height)/12);
			} else if(anchor_id == 6) {
				clips = Math.ceil(width/12)*2;
				// Add 2 extra clips per side
				clips = clips + 4;
				//clips = (((width)/12) + (width)/12);	
			} else if(anchor_id == 7) {
				clips = (((height*2)/12) + (width)/12);
				// Add 2 extra clips per side
				clips = clips + 6;
			/*} else if (anchor_id == 8) {
				clips = (((height*2)/12) + (width*2)/12);	*/
			} else {
				total_clips = 0;
			}
			total_clips = clips.toFixed(0);
			//total_clips_fee = clips_fee * total_clips;
			// Display value in field.
            if (width > 0) {
                $('.width input', this).attr("value", width.toFixed(0));
                //width += 2.5;
            } else width = '';
            if (height > 0) {
                $('.height input', this).attr("value", height.toFixed(0));
                //height += 2.5;
            } else height = '';
			
			totalqty =  (width * height) / (12 * 12);
			cost = (totalqty .toFixed(0) * price);
			// Add extra $30 for Top/Both sides 
			if(anchor_id == 7) {
				extra_cost = extra_cost + 30;
			}
			// Add extra $30 for Flush Mount Attachment 
			if(attachment_id == 14) {
				extra_cost = extra_cost + 30;
			}
			// Display value in field.
			if (cost > 0) { 	
				cost += extra_cost;
				//cost = cost + total_clips_fee;
				$('.price input', this).attr("value", cost.toFixed(0));
				$('.totalqty input', this).attr("value", totalqty.toFixed(0));
				$('.number input', this).attr("value", rand());
				$('.clips input', this).attr("value", total_clips);
				//$('.clipsFee input', this).attr("value", total_clips_fee.toFixed(0));
			} else {
				$('.price input', this).attr("value", '');
				$('.totalqty input', this).attr("value", '');
				$('.number input', this).attr("value", '');
				$('.clips input', this).attr("value", '');
				//$('.clipsFee input', this).attr("value",'');
			}
			totals += +cost.toFixed(0);
		});
		
		//$(".theCalcFieldsGR").attr("value", totals).valid();
		if(totals > 0)
			$("#totals").attr("value", totals.toFixed(0));
	});
	
});