
var period = 3;
var coins = 2;
var cost = 79.80;
var basePerCoin = 14.00;
var baseExtraShipping = 0;
var subscriptionName = "Geocoin Club - 3 Month, 2 Coin Subscription";
var subscriptionID = "2COIN-3M";
var discount = 0;
var discountType = "p";
var discountMinCoins = 0;
var discountMinMonths = 0;
var shippingdestination = "UNSELECTED";
var userlocation = "UNDETECTED";



  
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function changeShipping(thediv)
{
	document.getElementById('usa').className="choice";
	document.getElementById('elsewhere').className="choice";
	
	thediv.className = "selected";
	var countrytest = thediv.id;
	
	if(countrytest == "usa")
	{
		baseExtraShipping = 0;
		shippingdestination = "northamerica";
	}
	if(countrytest == "elsewhere")
	{
		baseExtraShipping = 1.00;
		shippingdestination = "elsewhere";
	}
		
	shippingselected = 1;

	updateSummary();
}

function changePeriod(thediv)
{
	document.getElementById('3months').className="choice";
	document.getElementById('6months').className="choice";
	document.getElementById('12months').className="choice";
	var periodtest = thediv.id;
	
	thediv.className = "selected";
	
	if(periodtest == "3months")
		period = 3;
	
	if(periodtest == "6months")
		period = 6;
		
	if(periodtest == "12months")
		period = 12;
		
	updateSummary();
}
function changeCoins(thediv)
{
	document.getElementById('1coins').className="choice";
	document.getElementById('2coins').className="choice";
	document.getElementById('3coins').className="choice";
	document.getElementById('4coins').className="choice";
	document.getElementById('5coins').className="choice";
	document.getElementById('6coins').className="choice";
	document.getElementById('7coins').className="choice";
	document.getElementById('8coins').className="choice";
	document.getElementById('9coins').className="choice";
	document.getElementById('10coins').className="choice";
	document.getElementById('15coins').className="choice";
	document.getElementById('20coins').className="choice";
	document.getElementById('25coins').className="choice";

	var numcoinstest = thediv.id;
	
	thediv.className = "selected";
	
	if(numcoinstest == "1coins")
		coins = 1;
	if(numcoinstest == "2coins")
		coins = 2;
	if(numcoinstest == "3coins")
		coins = 3;
	if(numcoinstest == "4coins")
		coins = 4;
	if(numcoinstest == "5coins")
		coins = 5;
	if(numcoinstest == "6coins")
		coins = 6;
	if(numcoinstest == "7coins")
		coins = 7;
	if(numcoinstest == "8coins")
		coins = 8;
	if(numcoinstest == "9coins")
		coins = 9;
	if(numcoinstest == "10coins")
		coins = 10;
	if(numcoinstest == "15coins")
		coins = 15;
	if(numcoinstest == "20coins")
		coins = 20;
	if(numcoinstest == "25coins")
		coins = 25;
	
	updateSummary();
}

function updateSummary()
{
	var summaryText = "<h5>Subscription Summary:</h5>";
	summaryText += "<img src='images/23.gif' align='absmiddle'> <B>" + coins + " coin(s) per month</b> with a renewal period of <B>" + period + " months</b>.";
	summaryText += "<BR><img src='images/coins.png' align='absmiddle'> You will get <B>" + coins*period + " total coins per</B> renewal period.";
	summaryText += "<BR><img src='images/renew.png' align='absmiddle'> <B>Renewal occurs automatically</b> each period unless canceled.";
	
	summaryText += "<BR><img src='images/lorry.png' align='absmiddle'> Total cost <b>includes shipping fees</b> for your coins";

	if(period == 3)
	{
		if(coins == 1){ cost = 42; }
		if(coins == 2){ cost = 79.8; }
		if(coins == 3){ cost = 113.72; }
		if(coins == 4){ cost = 142.8; }
		if(coins == 5){ cost = 168; }
		if(coins == 6){ cost = 189; }
		if(coins == 7){ cost = 220.5; }
		if(coins == 8){ cost = 252; }
		if(coins == 9){ cost = 283.5; }
		if(coins == 10){ cost = 315; }
		if(coins == 15){ cost = 472.5; }
		if(coins == 20){ cost = 630; }
		if(coins == 25){ cost = 787.5; }
	}
	if(period == 6)
	{
		if(coins == 1){ cost = 80; }
		if(coins == 2){ cost = 152; }
		if(coins == 3){ cost = 216.6; }
		if(coins == 4){ cost = 272; }
		if(coins == 5){ cost = 320; }
		if(coins == 6){ cost = 360; }
		if(coins == 7){ cost = 420; }
		if(coins == 8){ cost = 480; }
		if(coins == 9){ cost = 540; }
		if(coins == 10){ cost = 600; }
		if(coins == 15){ cost = 900; }
		if(coins == 20){ cost = 1200; }
		if(coins == 25){ cost = 1500; }
	}
	if(period == 12)
	{
		if(coins == 1){ cost = 156; }
		if(coins == 2){ cost = 296.4; }
		if(coins == 3){ cost = 422.37; }
		if(coins == 4){ cost = 530.4; }
		if(coins == 5){ cost = 624; }
		if(coins == 6){ cost = 702; }
		if(coins == 7){ cost = 819; }
		if(coins == 8){ cost = 936; }
		if(coins == 9){ cost = 1053; }
		if(coins == 10){ cost = 1170; }
		if(coins == 15){ cost = 1755; }
		if(coins == 20){ cost = 2340; }
		if(coins == 25){ cost = 2925; }
	}

	// Calculate total cost
	shippingCost = baseExtraShipping * (coins*period);
	cost += shippingCost;
	
	// Check for sneaky changes with discount
	if(discountMinCoins > coins && discount != 0)
	{
		alert("The discount you entered requires a minimum number of coins to apply.  The selections you have made are now under that limit.  The discount will be removed.  To add it back, change your selections to meet the coupon requirements and re-enter the coupon code.");
		clearDiscount();
	}
	if(discountMinMonths > period && discount != 0)
	{
		alert("The discount you entered requires a minimum number of months to apply.  The selections you have made are now under that limit.  The discount will be removed.  To add it back, change your selections to meet the coupon requirements and re-enter the coupon code.");
		clearDiscount();
	}
	
	
	// Adjust for coupon discounts
	if(discount > 0)
	{
		typeText = "percent";
		if(discountType != "p")
		{
			typeText = "dollar"
			cost = cost - discount;
		}
		else
		{
			// % off
			cost = cost - (cost * discount / 100);
		}
		summaryText += "<BR><span id='discounttext'><img src='images/money_delete.png' align='absmiddle'> You received a " + discount + " " + typeText + " discount on this subscription!</span>  <a href='#' onclick='clearDiscount();updateSummary();return false;'>Remove</a>";
	}
	
	
	var savingPercent = Math.round(((basePerCoin-(cost/(coins*period)))/basePerCoin)*100);
	if(savingPercent < 0)
		savingPercent = 0;
	
	summaryText += "<br><img src='images/money_dollar.png' align='absmiddle'> <B>Total Cost:</B> " + formatCurrency(cost) + " <B>Price Per Coin:</B> " + formatCurrency(cost/(coins*period)) + " <B>Total Savings:</B> " + savingPercent + "%";
	summaryText += "<br><img src='images/lock.png' align='absmiddle'> Your subscription pricing will be <B>locked in forever</B> at this rate.";
	
	// Set the form values
	var theForm = document.subform;
	subform.item_name.value = "Geocoin Club - " + period + " Month, " + coins + " Coin Subscription";
	subform.item_number.value = coins + "COIN-" + period + "M";
	subform.a3.value = cost;
	subform.p3.value = period;
	
	document.getElementById('subsummary').innerHTML = summaryText;
	if(discount > 0)
	{
		Effect.Pulsate('discounttext', { pulses: 10, duration: 10 });
	}

}

function clearDiscount()
{
	discountMinCoins = 0
	discountMinMonths = 0
	discount = 0
	discountType = "p";
	$('autocomplete').value = "";
	var theForm = document.subform;
	subform.custom.value = "none";
}

function getSelectionId(text, li) {
	var returnval = li.id;
	if(returnval == "expired")
	{
		$('autocomplete').value = "EXPIRED";
	}
	else if(returnval == "invalid")
	{
		$('autocomplete').value = "Not A Valid Coupon Code";
	}
	else if(returnval == "alreadyredeemed")
	{
		$('autocomplete').value = "ALREADY REDEEMED";
	}
	else
	{
		// Must be okay!
		// Hey... anyone reading this code...
		//   we check the payments that come in on each subscription to make sure they jive
		//   Remember - cheaters never win and winners never cheat!
		var numerrors = 0;
		var errortext = "";
		var components = returnval.split(":");
		if(components[3] != 0)
		{
			if(components[3] > coins)
			{
				numerrors++;
				errortext += "This coupon code requires a subscription with at least " + components[3] + " coins per month to redeem.  Change your subscription options and re-enter the code to redeem\n\n";
			}
		}
		if(components[4] != 0)
		{
			if(components[4] > period)
			{
				numerrors++;
				errortext += "This coupon code requires a subscription for at least " + components[4] + " months to redeem.  Change your subscription options and re-enter the code to redeem\n\n";
			}
		}
		discountMinCoins = components[3];
		discountMinMonths = components[4];
		
		if(numerrors == 0)
		{
			discount = components[0];
			discountType = components[1];	
			$('autocomplete').value = components[2];	
			var theForm = document.subform;
			subform.custom.value = components[2];		
		}
		else
		{
			$('autocomplete').value = "";	
			alert(errortext);
		}
	}
	updateSummary();
}

function checkAndSubmit()
{
	if(shippingdestination == "UNSELECTED")
	{
		alert("You must select your location before proceeding.");
		return false;
	}
	else if( userlocation != "US" && userlocation != "CA" && shippingdestination == "northamerica")
	{
		alert(userlocation);
		if(confirm("You have selected shipping to Canada or the United States.  Are you sure?"))
		{
			document.subform.submit();
		}
	}
	else
	{
		document.subform.submit();
	}
}