$(document).ready(function()
	{

		$('#subs').tabs({ fx: { opacity: 'toggle' } });
				
		// One day, time-permitting, clean up this terrible mess.
		// Like, SO embarassing. TOTALLY.
		
		// Set initial prices
		$.USp = $('#usa select.subType option:selected').val();
		$('#usa .total span').text($.USp);
		
		$.CANp = $('#canada select.subType option:selected').val();
		$('#canada .total span').text($.CANp);
		
		$.INTLp = $('#intl select.subType option:selected').val();
		$('#intl .total span').text($.INTLp);
		
		// Price changer
		$('#usa select.subType').change(function()
			{
				$.USp = $('#usa select.subType option:selected').val();
				$('#usa .total span').text($.USp);
				$('#usa input.code').val("");
				$.UScode = 0;
			}
		);
		
		$('#canada select.subType').change(function()
			{
				$.CANp = $('#canada select.subType option:selected').val();
				$('#canada .total span').text($.CANp);
				$('#canada input.code').val("");
				$.CANcode = 0;
			}
		);
		
		$('#intl select.subType').change(function()
			{
				$.INTLp = $('#intl select.subType option:selected').val();
				$('#intl .total span').text($.INTLp);
				$('#intl input.code').val("");
				$.INTLcode = 0;
			}
		);
		
		// Check refferal code
		$('#usa input.code').blur(function()
			{
				if ($(this).val() != "") {
					
					var fetch = "http://americancity.org/subscribe/codes/"+$(this).val();
					
					$.get(fetch, function(data)
						{						
							$.badcode = "Invalid referral code."
							
							if (data == -1) {
								$('#usa input.code').val($.badcode);
							} else {
								if($.UScode != 1) {
									$.UScode = 1;
									$.USp = ($.USp - data);
									$('#usa .total span').text($.USp);
								}
							}
						}
					);
					
				} else {
					$.UScode = 0;
					$.USp = $('#usa select.subType option:selected').val();
					$('#usa .total span').text($.USp);
				}
			}
		);
		
		$('#canada input.code').blur(function()
			{
				if ($(this).val() != "") {
					
					var fetch = "http://americancity.org/subscribe/codes/"+$(this).val();
					
					$.get(fetch, function(data)
						{						
							$.badcode = "Invalid referral code."
							
							if (data == -1) {
								$('#canada input.code').val($.badcode);
							} else {
								if($.CANcode != 1) {
									$.CANcode = 1;
									$.CANp = ($.CANp - data);
									$('#canada .total span').text($.CANp);
								}
							}
						}
					);
					
				} else {
					$.CANcode = 0;
					$.CANp = $('#canada select.subType option:selected').val();
					$('#canada .total span').text($.CANp);
				}
			}
		);
		
		$('#intl input.code').blur(function()
			{
				if ($(this).val() != "") {
					
					var fetch = "http://americancity.org/subscribe/codes/"+$(this).val();
					
					$.get(fetch, function(data)
						{						
							$.badcode = "Invalid referral code"
							
							if (data == -1) {
								$('#intl input.code').val($.badcode);
							} else {
								if($.INTLcode != 1) {
									$.INTLcode = 1;
									$.INTLp = ($.INTLp - data);
									$('#intl .total span').text($.INTLp);
								}
							}
						}
					);
					
				} else {
					$.INTLcode = 0;
					$.INTLp = $('#intl select.subType option:selected').val();
					$('#intl .total span').text($.INTLp);
				}
			}
		);
		
		// Clear referral code if its bad
		$('#usa input.code').focus(function()
			{
				if	( $('#usa input.code').val() == $.badcode ) {
					$('#usa input.code').val("");
				}
			}
		);

		$('#canada input.code').focus(function()
			{
				if	( $('#canada input.code').val() == $.badcode ) {
					$('#canada input.code').val("");
				}
			}
		);

		$('#intl input.code').focus(function()
			{
				if	( $('#intl input.code').val() == $.badcode ) {
					$('#intl input.code').val("");
				}
			}
		);
		
		
	}
);
