$(document).ready(function(){
	
	// Savings ticker
	$('#savings-counter .dollars').customer_savings_ticker();
	
	////////////////////////////////
	// Login dialog
	var dlg_login = $('#dlg-login');
	var frm_login = $('form[name=frm_login]');
	
	dlg_login.dialog({
		width: 400,
		modal: true,
		autoOpen: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Login': function() {
				frm_login.find('.response').html('<p class="notice">Loading...</p>').hide().slideDown('fast', function() {
					$.getJSON(frm_login.attr('action'), frm_login.serialize(), function( callback ) {
						try {
							if( callback.s_status != 'success' ) throw(callback.s_message);
							
							// Forward
							window.location = callback.s_location;
							
						} catch( s_error ) {
							frm_login.find('.response').html('<p class="error">' + s_error + '</p>').delay(10000).slideUp('fast');
						}
					});
				});
				
				return false;
			}
		}
	});
	
	$('.btn-login').click(function() {
		dlg_login.dialog('open');
		return false;
	});
	
	// Check for required fields
	$('.required').parent().prev().children().after('<span style="color: red;">*</span>');
	$('form').submit(function() {
		var ready = true;
		$('.required').each(function() {
			if ($(this).val() == '') {
				$(this).css('background','#FFDBDB').css('border','1px solid red').focus()
					.animate({marginLeft: '-15', marginRight: '15'}, 100)
					.animate({marginLeft: '20', marginRight: '-20'}, 100)
					.animate({marginLeft: '0', marginRight: '0'}, 100);
				ready = false;
				return false;
			}
		});
		
		if (ready == true) {
			// Go ahead
		} else {
			return false;
		}
	});
	
	// Require login for restricted tools
	$('.members_only').click(function() {
		alert('This application is for members only.');
		return false;
	});
	$('.gold_members_only').click(function() {
		alert('You must be a Gold Level Member to access this feature.');
		return false;
	});
	
	// RFI Processor
	$('.add_to_rfi').click(function() {
		var rfi_key = $(this).attr('rel');
		$.post('tiny.php', { page: 'rfi_add_remove', rfi_key: rfi_key },
		function(data) {
			var answer = confirm('Your request to get more information has been added to the inquiry list.\n\nClick "OK" to send your request.\nClick "Cancel" to continue browsing.');
			if (answer) {
				window.location = "secondary.php?page=directory_rfi";
			} else {
				return false;
			}
		});
	});
	
	// Phone Format Validation
	$('.phone').blur(function() {
		var string = $(this).val();
		var phone_expression = /^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$/i.test(string);
		if (! phone_expression && string != '') {
			alert('Phone needs to be formatted as 999-999-9999.');
			return false;
		}
	});
	
	// Credit Card Format Validation
	$('.credit_card').blur(function() {
		var string = $(this).val();
		var phone_expression = /^[0-9]{4}[-][0-9]{4}[-][0-9]{4}[-][0-9]{2,}$/i.test(string);
		if (! phone_expression && string != '') {
			alert('Credit Card needs to be formatted as 9999-9999-9999-99XX.');
			return false;
		}
	});
	
	// Keep PIN length within bounds
	$('.pin').blur(function() {
		var min = 4;
		var max = 8;
		var string_length = $(this).val().length;
		if (min <= string_length && string_length <= max) {
			// Do nothing
		} else {
			alert('PIN must be between '+min+' and '+max+' characters');
		}
	});
	
	// Disabled Links / Buttons
	$('.disabled').live('click', function() {
		return false;
	});
});
