// js/subscribe.js

function buy_subscription(type)
{
	if ( false == buy_subscription_GATEKEEPER() ) return;
	buy_subscription_SETUP_AJAX(type);
	ajax.onCompletion = buy_subscription_AJAX_COMPLETE;
	ajax.runAJAX();
}

function buy_subscription_GATEKEEPER()
{
	$('#butPurchase1,#butPurchase2,#butApplyCoupon').attr('disabled','disabled');
	var question;
	if ( $('#txtCouponCode').val() != '' )
	{
		question = "Are you sure you want to purchase a subscription using this coupon code?  If not, click cancel, change or clear the coupon code, and re-submit your purchase.\n\n(Note: any invalid or previously used coupon code will simply be disregarded.)";
	}
	else
	{
		question = "Do you have a coupon code you forgot to enter?  If so, click cancel, input your coupon code, and re-submit your purchase.\n\n(Note: any invalid or previously used coupon code will simply be disregarded.)";
	}
	var answer = safe_confirm(question);
	if ( !answer ) $('#butPurchase1,#butPurchase2,#butApplyCoupon').attr('disabled','');
	return answer;
}

function buy_subscription_SETUP_AJAX(type)
{
	ajax = new sack();
	ajax.setVar('subscription_type', type);
	ajax.setVar('coupon_code', $('#txtCouponCode').val());
	ajax.setVar("action", '1');
	ajax.setVar("submission", 'subscribe');
	ajax.requestFile = "backdoor.php";
}

function buy_subscription_AJAX_COMPLETE()
{
	if ( typeof ajax.response != undefined )
	{
		if ( ajax.response.substring(0,5) == 'ERROR' )
		{
			safe_alert(ajax.response);
			$('#butPurchase1,#butPurchase2,#butApplyCoupon').attr('disabled','');
		}
		else if ( ajax.response.substring(0,5) == 'ABORT' )
		{
			safe_alert(ajax.response.substring(5));
			$('#butPurchase1,#butPurchase2,#butApplyCoupon').attr('disabled','');
		}
		else
		{
			$('hidEncrypted').value = ajax.response;
			if ( $('hidEncrypted').value != '' )
			{
				safe_alert("You will now be taken to PayPal's website to complete your payment.");
				$('frmPayment').submit();
			}
		}
	}
}

function apply_coupon()
{
	if ( false == apply_coupon_GATEKEEPER() ) return;
	apply_coupon_SETUP_AJAX();
	ajax.onCompletion = apply_coupon_AJAX_COMPLETE;
	ajax.runAJAX();
}

function apply_coupon_GATEKEEPER()
{
	$('#butPurchase1,#butPurchase2,#butApplyCoupon').attr('disabled','disabled');
	if ( $('#txtCouponCode').val() == '' )
	{
		safe_alert('Please enter a coupon code first.');
		$('#butPurchase1,#butPurchase2,#butApplyCoupon').attr('disabled','');
		return false;
	}
	return true;
}

function apply_coupon_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar('coupon_code', $('#txtCouponCode').val());
	ajax.setVar("action", '2');
	ajax.setVar("submission", 'subscribe');
	ajax.requestFile = "backdoor.php";
}

function apply_coupon_AJAX_COMPLETE()
{
	if ( ajax.response.substring(0,7) == 'success' )
	{
		// passing any argument at all to this function will prevent the page from reloading
		// this is a slight compromise to help with testing
		if ( arguments.length == 0 )
		{
			redirect('index.php?page=subscribe');
		}
		return true;
	}
	else
	{
		safe_alert(ajax.response);
		$('#butPurchase1,#butPurchase2,#butApplyCoupon').attr('disabled','');
		return false;
	}
}

// end of file
