$(document).ready(function()
{
	// Hide or show certain elements on load (graceful degradation)
	$('.short-tip').hide();
	$('.inline-tip').hide();
	$('.extended-tip').hide();
	$('#currency-selector').show();
	$('.disable').attr('disabled', true);
	$('a.new-window').attr('target', '_blank');
	
	// Remove the input hints when the user clicks on the field
	$('.has-default-value').each(function() {
		$(this).focus(function() {
			if (this.value == this.defaultValue) {
				this.value = '';
				// this.select();
			}
			if (this.value != this.defaultValue) {
				this.select();
			}
		});
		$(this).blur(function() {
			if ($.trim(this.value) == '') {
				this.value = (this.defaultValue ? this.defaultValue : '');
			}
		});
	});

	// Inline form tips for when a form is thin enough to accomodate tips beside input
	$('.has-inline-tip').each(function() {
		$(this).focus(function() {
			if ($(this).next('label').length === 0) {
				$(this).nextAll('.inline-tip').show();
			}
		});
		$(this).blur(function() {
			$(this).nextAll('.inline-tip').hide();
		});
	});

   // Show form tips as required (see forms/generate)
   $('.has-tip').each(function() {
		$(this).focus(function() {
			$('.extended-tip').hide();
			// Horrible hack to work with the currencies nested in a span child
			if ($(this).attr('name') != 'CurrencyInput[]') {
				$(this).nextAll('.extended-tip').show();
			}
			else {
				$(this).parent().nextAll('.extended-tip').show();
			}
		});
	});

	// Convenience link to select All/No currencies
	$('#currency-all').toggle(
		function(e) {
			$(this).parent().siblings().each(function() {
				if ($(this).attr('type') == 'checkbox') {
					$(this).attr('checked', true);
				}
				// test = test + ' ' + $(this).attr('type');
			});
			e.preventDefault();
			$(this).text('None');
		},
		function(e) {
			$(this).parent().siblings().each(function() {
				if ($(this).attr('type') == 'checkbox') {
					$(this).attr('checked', false);
				}
			});
			e.preventDefault();
			$(this).text('All');
		}
	);

	// Enable or disable email template editing based on whether user requires it
	$('#merchant_updates, #customer_updates').click(function() {
		var textarea = $(this).parent().next().children('textarea');
		if ($(this).attr('checked') === true) {
			if (textarea.attr('disabled')) {
				textarea.attr('disabled', false);
			}
		}
		else {
			textarea.attr('disabled', true);
		}
	});

	/********************
	 *  jQuery Validate
	 ********************/

	 // Check if the demo form exists on the page
	if ($('#demoForm').length) {

		$.validator.setDefaults({
			errorClass: "js-error",
			// only one possible error so stick it beside submit button
			errorPlacement: function(error, element) {
				error.insertAfter($('#demo-submit'));
			}
		});

		// Add rule for AmountInput
		$.validator.addMethod('amount',
			function(value,element) {
				if ($.trim(value) != '') {
					return (/^[0-9]{1,6}\.[0-9]{2}$/).test(value);
				}
				else {
					return true;
				}
			},
			"The amount entered is invalid"
		);

		$("#demoForm").validate({
			rules: {
				CurrencyInput: "required",
				AmountInput: {
					required: true,
					amount: true
				}
			}
		});
	}

	// Check if the generate form exists on the page first
	if ($('#generate').length) {
		$.validator.setDefaults({
			// debug: true,
			errorClass: "js-error",
			// the errorPlacement has to take the table layout into account
			errorPlacement: function(error, element) {
				if ( element.is(":radio") ) {
					error.appendTo( element.parent() );
				}
				else if ( element.is(":checkbox") ) {
					error.appendTo ( element.parent() );
				}
				else
					error.appendTo( element.parent() );
				}
		});
		
		// Add rule for pxpay key
		$.validator.addMethod('key',
			function(value,element) {
				return (/^[a-f0-9]{64}$/).test(value);
			},
			"Should be 64 hexadecimal characters"
		);
		// Add rule for AmountInput
		$.validator.addMethod('amount',
			function(value,element) {
				if ($.trim(value) != '') {
					return (/^[0-9]{1,6}.[0-9]{2}$/).test(value);
				}
				else {
					return true;
				}
			},
			"Should be in the format 10.00"
		);

		$("#generate").validate({
			rules: {
				PxPayUserId: "required",
				PxPayKey: {
					required: true,
					key: true
				},
				TxnType: "required",
				AmountInput: {
					amount: true
				},
				"CurrencyInput[]": "required",
				url_success: {
					required: true,
					url: true
				},
				url_fail: {
					required: true,
					url: true
				},
				admin_email: {
					required: true,
					email: true
				},
				terms : "required"
			}
		});
	}
});