// JavaScript Document
var B2Bach = {};

B2Bach.selectInstrumentPattern = / \- \$ [0-9]{1,}\.[0-9]{2}$/i;

B2Bach.getRentCart = function() {
	var url = "request/getrentcart.php?ts=" + Math.random();
	GabeUtil.ajaxRequest('get', url, {
		onLoad : function(response) {
			GabeUtil.screenDimLoading(false);
			jQuery("#rentcartinstruments").html(response);
			B2Bach.initAddRentCart(function(response) {}, ".removerentcart", "remove");
			B2Bach.initAddRentCart(function(response) {}, ".clearcartbtn", "clear");
		}
	});	
}

B2Bach.initAddRentCart = function(callback, selector, action) {
	
	if(!selector) {
		selector = ".addrentcart";	
	}
	
	if(!action) {
		action = "add";
	}
	
	if(!callback) {
		callback = function(response) {};
	}
	
	jQuery(selector).click(function(event) {
									 
		if(action == 'remove') {
			var msg = "Would you like to remove this instrument from your Instrument Rental order?";
			if(!confirm(msg)) {
				return;
			}
		} else if(action == 'clear') {
			var msg = "Would you like to remove all the instruments from your reservation";
			if(!confirm(msg)) {
				return;
			}
		}
		
		GabeUtil.screenDimLoading(true);
		var id = jQuery(this).metadata().id;
		var url = "request/addrentcart.php?action=" + action + "&id=" + id + "&ts=" + Math.random();
		// window.location.href = url;
		GabeUtil.ajaxRequest('get', url, {
			onLoad : function(response) {
				// alert(response.status);
				GabeUtil.screenDimLoading(false);
			},
			onOK : function(response) {
				if(document.getElementById('rentcartinstruments')) {
					B2Bach.getRentCart();
				}
				callback(response);
			}					 	
		}, 'json');
		
	});
}

B2Bach.checkItemIds = function(oform) {
	var res = true;
	jQuery.each(jQuery(".resinstrumenttable select[name='itemid[]']", jQuery(oform)), function(i, val) {
		if(res == false) {
			return;	
		}
		var html = jQuery("option:selected", jQuery(val)).html().replace(/&nbsp;/, '');
		if(!html.match(B2Bach.selectInstrumentPattern)) {
			alert(html + " is a category.  Please select an instrument.");
			jQuery(val).focus();
			res = false;
		}
	});
	return res;
}

B2Bach.initSelectInstrument = function(elem) {
	var html = jQuery("option:selected", jQuery(elem)).html();
	if(!html) {
		return;	
	}
	
	if(html.search(B2Bach.selectInstrumentPattern) > 1) {
		if(!jQuery(elem).next().hasClass("selectinstrumentlink")) {
			jQuery(elem).after("<a target='_blank' href='instrument.php?id=" + jQuery(elem).val() + "' class='selectinstrumentlink'>View Info</a>");	
		}
	}

	jQuery(elem).change(function(event) {
		var html = GabeUtil.htmlDecode(jQuery("option:selected", jQuery(elem)).html());
		if(html.search(B2Bach.selectInstrumentPattern) > 1) {
			if(!jQuery(elem).next().hasClass("selectinstrumentlink")) {
				jQuery(elem).after("<a target='_blank' href='instrument.php?id=" + jQuery(elem).val() + "' class='selectinstrumentlink'>View Info</a>");	
			}
		}
		var currsel = this;
		var myval = jQuery(this).val();
		jQuery.each(jQuery(".resinstrumenttable select[name='itemid[]']"), function(i, val) {
			if(val == currsel) {
				return;	
			}
			if(jQuery(val).val() == myval) {
				jQuery("option:first", jQuery(val)).attr("selected", "selected");
			}
		});
	});
	
}

B2Bach.initResInstruments = function(parent) {
	if(!parent) {
		parent = document.body;	
	}
	jQuery.each(jQuery(".resinstrumenttable"), function(i, val) {
		B2Bach.initResInstrument(val);
	});
}

B2Bach.initResInstrument = function(elem) {
	jQuery(".removeinstrumentbtn", jQuery(elem)).click(function(event) {
		var conf = confirm("Would you like to remove this instrument?");
		if(!conf) {
			return false;	
		}
		jQuery(elem).remove();										 
	});
	jQuery(".datepickerfld", jQuery(elem)).datepicker({
		dateFormat : 'yy-mm-dd'
	});
	jQuery("select[name='itemid[]']", jQuery(elem)).change(function(event) {
		var currprice = parseFloat( jQuery("input[name='price[]']", jQuery(elem)).val() );
		if(currprice < 1.00 || isNaN(currprice)) {
			var html = jQuery("option:selected", jQuery(this)).html();
			var price = html.match(/\$ ([0-9]+\.[0-9]{2})/);
			jQuery("input[name='price[]']", jQuery(elem)).val(price[1]);
		}
	});
	B2Bach.initSelectInstrument(jQuery("select[name='itemid[]']", jQuery(elem)));
}

B2Bach.initAddResInstrument = function() {
	var itmp = GabeUtil.htmlDecode(jQuery("#resitemtmp").html());
	jQuery("#addresitem").click(function(event) {
		jQuery("#resitemswrapper").append(itmp);
		B2Bach.initResInstrument(jQuery("#resitemswrapper .resinstrumenttable:last"));
	});	
}
