		var instrumentList = new Array();
		var rentalId = 0;

		 var RecaptchaOptions = {
				    theme : 'white'
		 };
        
    	function Instrument() {

			this.__type      = null;
			this.__size      = new Array();
			this.__payment   = 0.00;
			this.__insurance = 0.00;
			this.__shipping  = 0.00;
        	
       	}

       	function instrument_setType(type) {
           	this.__type = type;
       	}

       	function instrument_getType() {
           	return this.__type;
        }

        function instrument_addSize(size) {
            this.__size[this.__size.length] = size;
        }

        function instrument_getSize(index) {
            return this.__size[index];
        }

        function instrument_getAllSizes() {
            return this.__size;
        }

        function instrument_getPayment() {
           	return this.__payment;
        }
        
		function instrument_setPayment(value) {
			this.__payment = value;
		}

		function instrument_setInsurance(value) {
			this.__insurance = value;
		}

		function instrument_getInsurance() {
			return this.__insurance;
		}		
		function instrument_setShipping(value) {
			this.__shipping = value;
		}

		function instrument_getShipping() {
			return this.__shipping;
		}		
				
        Instrument.prototype.setType 	 = instrument_setType;
        Instrument.prototype.getType 	 = instrument_getType;
        Instrument.prototype.addSize 	 = instrument_addSize;
        Instrument.prototype.getSize     = instrument_getSize;
        Instrument.prototype.getAllSizes = instrument_getAllSizes;
        Instrument.prototype.getPayment  = instrument_getPayment;
        Instrument.prototype.setPayment  = instrument_setPayment;
        Instrument.prototype.getInsurance  = instrument_getInsurance;
        Instrument.prototype.setInsurance  = instrument_setInsurance;
        Instrument.prototype.getShipping  = instrument_getShipping;
        Instrument.prototype.setShipping  = instrument_setShipping;        

       
		function initializeInstruments(rowdelete) {

				// Violin
				var instrument = new Instrument();
				instrument.setType("Violin");
				instrument.setPayment(19.00);
				instrument.setInsurance(5.24);
				instrument.setShipping(20.95);
				instrument.addSize("4/4");
				instrument.addSize("3/4");
				instrument.addSize("1/2");
				instrument.addSize("1/4");
				instrument.addSize("1/8");
				instrument.addSize("1/10");
				instrument.addSize("1/16");
				instrumentList[0] = instrument;

				// Viola
				var instrument = new Instrument();
				instrument.setType("Viola");
				instrument.setPayment(23.00)
				instrument.setInsurance(5.28);				
				instrument.setShipping(20.95);
				instrument.addSize("16 1/2");
				instrument.addSize("16");
				instrument.addSize("15 1/2");
				instrument.addSize("15");
				instrument.addSize("14");
				instrument.addSize("13 1/4");
				instrument.addSize("12");
				instrumentList[1] = instrument;

				// Cello 
				var instrument = new Instrument();
				instrument.setType("Cello");
				instrument.setPayment(31.00);
				instrument.setInsurance(7.38);
				instrument.setShipping(52.95);
				instrument.addSize("4/4");
				instrument.addSize("3/4");
				instrument.addSize("1/2");
				instrument.addSize("1/4");
				instrument.addSize("1/8");
				instrument.addSize("1/10");
				instrumentList[2] = instrument;				

				// Bass
				var instrument = new Instrument();
				instrument.setType("Bass");
				instrument.setPayment(50.00);
				instrument.setInsurance(10.00);
				instrument.setShipping(75.00);
				instrument.addSize("3/4");
				instrument.addSize("1/2");
				instrument.addSize("1/4");
				instrument.addSize("1/8");
				instrumentList[3] = instrument;				

				// Guitar
				var instrument = new Instrument();
				instrument.setType("Guitar");
				instrument.setPayment(23.00);
				instrument.setInsurance(5.28);
				instrument.setShipping(34.95);
				instrument.addSize("Full Size");
				instrument.addSize("3/4");
				instrument.addSize("1/2");
				instrumentList[4] = instrument;					

				// Guitarron
				var instrument = new Instrument();
				instrument.setType("Guitarron");
				instrument.setPayment(45.00);
				instrument.setInsurance(5.28);
				instrument.setShipping(124.95);
				instrument.addSize("Standard");
				instrumentList[5] = instrument;					
				
				// Vihuella
				var instrument = new Instrument();
				instrument.setType("Vihuella");
				instrument.setPayment(28.00);
				instrument.setInsurance(5.28);
				instrument.setShipping(29.95);
				instrument.addSize("Standard");
				instrumentList[6] = instrument;					

				
				// Set Instrument Selector
				$("#rental_instruments").append($('<option></option>').val("").html("Select Instrument"));
				$("#rental_sizes").append($('<option></option>').val("").html("Select Size"));
				for(var i=0; i<instrumentList.length; i++) {
					$("#rental_instruments").append($('<option></option>').val(instrumentList[i].getType()).html(instrumentList[i].getType()));					
				}

				// Set Initiali Quantity
				$("#rental_quantity").val(1);

	    		$("#rentalTableList").jqGrid({ 
	    			datatype: 'local', 
	    			height: 150, 
	    			width:  636,
	    			colNames:['id','','Instrument','Size', 'Quantity', 'Payment','Insurance','Shipping','Total'], 
	    			colModel:[ 
	    		    			{name:'id',index:'id',hidden:true},
	    		    			{name:'delete', index:'delete', width:30, editable: false, edittype:'select',formatter:'actions',formatoptions:{keys: false, editbutton: false,delbutton:rowdelete, event: "removeRow"}},
	    						{name:'instrument',index:'instrument', width:125}, 
	    						{name:'size',index:'size', width:60}, 
	    						{name:'quantity',index:'quantity', width:100,align:'right'}, 
	    						{name:'payment',index:'payment', formatter:'currency', width:100, align:'right'}, 
	    						{name:'insurance',index:'insurance', formatter:'currency', width:100, align:'right'}, 
	    						{name:'shipping',index:'shipping', formatter:'currency', width:100, align:'right', hidden:true},    						
	    						{name:'total',index:'total', formatter:'currency', width:100,align:'right'} ], 
	    			multiselect: false,
	    			sortable: false,
	    			emptyrecords: "No Instruments have been added for rental."
	    		}).navGrid("#rentalTablePager",{edit:false,add:false,del:true}); 				
				
				// Load Instruments
				loadInstruments();
				

		}

		// Load the sizes for the Instrument
		function loadSizes(selected) {

			var instrument = getInstrument(selected);			

			// Remove Current Sizes if Any
			$("#rental_sizes").children().remove();

			// Add New List
			$("#rental_sizes").append($('<option></option>').val("").html("Select Size"));
			if (instrument != null) { 
				for(var i=0; i<instrument.getAllSizes().length; i++) {
					$("#rental_sizes").append($('<option></option>').val(instrument.getSize(i)).html(instrument.getSize(i)));					
				}
			}			
		}


		function addInstrument() {

			clearError();

			var instrumentType = $("#rental_instruments").val();
			var instrumentSize = $("#rental_sizes").val();
			var quantity       = $("#rental_quantity").val();
			
			// Need to Select an Instrument
			if (!$.trim(instrumentType).length || !$.trim(instrumentSize).length ) {
				$("#err_instrument").html("Please select both an Instrument and a Size");
				return;
			} 
							
			// Add to Table
			addRow(instrumentType, instrumentSize, quantity);

			// Reset Instrument, Size and Quantity
			$("#rental_instruments").val("");
			$("#rental_sizes").val("");				
			$("#rental_quantity").val(1);
			
		}

		function addRow(instrumentType, instrumentSize, quantity) {

			var instrument     = getInstrument(instrumentType);
			var rowid          = instrumentType + "-" + instrumentSize;

			// Add Instrument to Rental Array
			var rentalData 	   = $("#rentalTableList").getRowData(rowid);
			var payment        = quantity * instrument.getPayment(); 
			var insurance      = quantity * instrument.getInsurance();
			var shipping       = ( (quantity * instrument.getShipping()));
			
			if ($.isEmptyObject(rentalData)) {

				 rentalData = {id:rowid, instrument: instrumentType, size:instrumentSize, 
						  quantity: quantity, payment:payment, insurance:insurance, shipping: shipping, total:lib.math.add(payment, insurance) };
				 $("#rentalTableList").addRowData(rowid, rentalData);
				 
			} else {
				
				rentalData.quantity   = lib.math.add(quantity,rentalData.quantity);
				rentalData.payment    = lib.math.add(payment,rentalData.payment);
				rentalData.insurance  = lib.math.add(insurance,rentalData.insurance);
				rentalData.shipping   = lib.math.add(shipping,rentalData.shipping);				
				rentalData.total      = lib.math.add(rentalData.payment, rentalData.insurance);
				$("#rentalTableList").setRowData(rowid, rentalData);
			}

			// Update Totals
			updateTotals();
			
		}
		
		function removeRow(rowid) {

			// Remove the Row
			$("#rentalTableList").delRowData(rowid);

			// Update Totals
			updateTotals();
			
		}
		
		function updateTotals() {

			// Calculate All Row Info
			var rentalData = $("#rentalTableList").jqGrid('getRowData');

			if (rentalData.length < 0) {
				return;
			}

			// Initialize Totals
			var im = 0.00;
			var ic = 0.00;

			var sm = 0.00;
			var sc = 0.00;
			
			var s = 0.00;
			var q = 0;

			// Monthly Charges
			for (var i = 0; i<rentalData.length; i++) {
				im = lib.math.add(im, rentalData[i].payment);
				sm = lib.math.add(sm, rentalData[i].insurance);
				q = lib.math.add(q, rentalData[i].quantity);
				s = lib.math.add(s, rentalData[i].shipping);
			}

			$("#instrument_monthly").html(lib.format.currency(im));
			$("#insurance_monthly").html(lib.format.currency(sm));
			$("#total_due_monthly").html(lib.format.currency(lib.math.add(im,sm)));

			// Due Today
			ic = im * 3;
			sc = sm * 3;
			$("#instrument_current").html(lib.format.currency(ic));
			$("#insurance_current").html(lib.format.currency(sc));
			$("#shipping").html(lib.format.currency(s));
			var t = lib.math.add(s, ic);
			t = lib.math.add(t, sc);
			$("#total_due_today").html(lib.format.currency(t));	

			
		}

		function saveInstruments() {

			// Calculate All Row Info
			var rentalData = $("#rentalTableList").jqGrid('getRowData');
			var cartData = "";

			for (var i = 0; i<rentalData.length; i++) {

				var instrument = rentalData[i].instrument;
				var size = rentalData[i].size;
				var quantity = rentalData[i].quantity;

				cartData = cartData +  ($.trim(cartData).length==0 ? "" : ";" )
							+ instrument + ":" + size + ":" + quantity;
				
			}

			// Save Info in form
			$("#rentalCart").val(cartData);

		}

		function loadInstruments() {

			var cartInfo = $("#rentalCart").val();

			// We don't have anything.. move on
			if (cartInfo == 'undefined' || $.trim(cartInfo).length==0) {
				return;
			}

			// Get All
			var instruments = cartInfo.split(";");
			
			for(var i =0; i<instruments.length; i++) {
				
				var instrument = instruments[i].split(":");
				var type = instrument[0];
				var size = instrument[1];
				var qty  = instrument[2];

				addRow(type, size, qty);
				
			}
			
		}
		
		function getInstrument(name) {

			var instrument = null;
			
			// find instrument
			for(var i=0; i<instrumentList.length; i++) {
				if (name == instrumentList[i].getType()) {
					instrument = instrumentList[i];
					break;
				}
			}			

			return instrument;
		}
	
		
		function clearError() {
			$("#err_instrument").html("&nbsp;");
		}

       
