// JavaScript Document
$(function() {
			// jQuery formatCurrency plugin: http://plugins.jquery.com/project/formatCurrency

			// Format while typing & warn on decimals entered, 2 decimal places
			$('.dollarformat1').blur(function() {
				$(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: 2 });
			})
			.keyup(function(e) {
				var e = window.event || e;
				var keyUnicode = e.charCode || e.keyCode;
				if (e !== undefined) {
					switch (keyUnicode) {
						case 27: this.value = ''; break; // Esc
						case 37: break; // cursor left
						case 38: break; // cursor up
						case 39: break; // cursor right
						case 40: break; // cursor down
						case 78: break; // N (Opera 9.63+ maps the "." from the number key section to the "N" key too!) (See: http://unixpapa.com/js/key.html search for ". Del")
						case 110: break; // . number block (Opera 9.63+ maps the "." from the number block to the "N" key (78) !!!)
						case 190: break; // .
						default: $(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: -1, eventOnDecimalsEntered: true });
					}
				}
			})
			.bind('decimalsEntered', function(e, cents) {
				if (String(cents).length > 2) {
					var errorMsg = 'Please do not enter any cents (0.' + cents + ')';
					alert( errorMsg );
					log('Event on decimals entered: ' + errorMsg);
				}
			});


			// Warn on decimals entered, 2 decimal places
			$('.dollarformat').blur(function() {
				$('#warnOnDecimalsEnteredNotification2').html(null);
				$(this).formatCurrency({ roundToDecimalPlace: 2, eventOnDecimalsEntered: true });
			})
			.bind('decimalsEntered', function(e, cents) {
				if (String(cents).length > 2) {
					var errorMsg = 'Please do not enter any cents (0.' + cents + ')';
					$('#warnOnDecimalsEnteredNotification2').html(errorMsg);
					log('Event on decimals entered: ' + errorMsg);
				}
			});


			// Format while typing & warn on decimals entered, no cents
			$('#formatWhileTypingAndWarnOnDecimalsEntered').blur(function() {
				$('#formatWhileTypingAndWarnOnDecimalsEnteredNotification').html(null);
				$(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: 0 });
			})
			.keyup(function(e) {
				var e = window.event || e;
				var keyUnicode = e.charCode || e.keyCode;
				if (e !== undefined) {
					switch (keyUnicode) {
						case 27: this.value = ''; break; // Esc
						case 37: break; // cursor left
						case 38: break; // cursor up
						case 39: break; // cursor right
						case 40: break; // cursor down
						case 78: break; // N (Opera 9.63+ maps the "." from the number key section to the "N" key too!) (See: http://unixpapa.com/js/key.html search for ". Del")
						case 110: break; // . number block (Opera 9.63+ maps the "." from the number block to the "N" key (78) !!!)
						case 190: break; // .
						default: $(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: -1, eventOnDecimalsEntered: true });
					}
				}
			})
			.bind('decimalsEntered', function(e, cents) {
				var errorMsg = 'Please do not enter any cents (0.' + cents + ')';
				$('#formatWhileTypingAndWarnOnDecimalsEnteredNotification').html(errorMsg);
				log('Event on decimals entered: ' + errorMsg);
			});


			// Warn on decimals entered, no cents
			$('#warnOnDecimalsEntered').blur(function() {
				$('#warnOnDecimalsEnteredNotification').html(null);
				$(this).formatCurrency({ roundToDecimalPlace: 0, eventOnDecimalsEntered: true });
			})
			.bind('decimalsEntered', function(e, cents) {
				var errorMsg = 'Please do not enter any cents (0.' + cents + ')';
				$('#warnOnDecimalsEnteredNotification').html(errorMsg);
				log('Event on decimals entered: ' + errorMsg);
			});


			function log(text) {
				$('#divLog').prepend('<div>' + text + '</div>');
			}
			
			$('#clearLog').click(function() {
				$('#divLog').empty();
			});

		});

