// While it's normal to use -- here for template vars, DON'T. WordPress's editor
// likes to consume double dashes and turn them into some unicode goop, so any
// substitutions won't work! As such, this file uses == instead!

function getCookie(key) {
	var cookies = document.cookie.split(";");
	for(var i = 0; i < cookies.length; i++) {
		var cookie = cookies[i];
		var parts = jQuery.trim(cookie).split("=");
		if(parts[0] == key) {
			return(parts[1]);
		}
	}
	return(false);
}


function money(stuff) {
	return("$" + Number(stuff).toFixed(2));
}

//costs of plans, per billing period
var plan_costs = {
	"L1:0": 10.95,
	"L1:1": 119.40,
	"L1:2": 214.80,
	"L1:3": 286.20,
	"L1:5": 417.00,
	"L1:10": 714.00
};

var setup_fees = {
	"L1:0": 49.95
};

var ignore_promo = 0; //if you set this, we ignore the promo data

var promo_cookie = unescape(getCookie("promo_json"));

jQuery(document).ready(function() {
	var cookie_promo_data = {};
	if(promo_cookie) {
		cookie_promo_data = eval("(" + promo_cookie + ")"); //it's our cookie, so we assume it's safe.
	}
	var promo_data = {};
		
	if (!ignore_promo) {
		promo_data = cookie_promo_data;
	} else { //we want to tell the user the stuff they don't get.
		//free ips
		if(cookie_promo_data.ip) {
			jQuery(".no_extra_ip").show();
			jQuery(".extra_ip").hide();
		}
		
		//free domregs
		if(cookie_promo_data.domreg) {
			jQuery(".no_extra_domreg").show();
			jQuery(".extra_domreg").hide();
		}
		
		//free months
		if(cookie_promo_data.months_free) {
			jQuery(".no_months_free").show();
			jQuery(".months_free").hide();
		}
	}
	
	//free ips
	if(promo_data.ip) {
		jQuery(".extra_ip").each(function () {
			jQuery(this).html(jQuery(this).html().replace("==ip==", promo_data.ip)).show();
			
			var english = promo_data.ip > 1 ? 'Addresses' : 'Address';
			jQuery(this).html(jQuery(this).html().replace("==Address(es)==", english));
		});
		jQuery(".no_extra_ip").hide();
		
		if(promo_data.ip > 1) {
			jQuery(".extra_ip_plural").show();
			jQuery(".no_extra_ip_plural").hide();
		} else {
			jQuery(".no_extra_ip_plural").show();
			jQuery(".extra_ip_plural").hide();
		}
		
	}
	
	//free domregs
	if(promo_data.domreg) {
		jQuery(".extra_domreg").each(function () {
			jQuery(this).html(
				jQuery(this).html()
					.replace("==domreg==", promo_data.domreg)
					.replace("==tot_domreg==", parseInt(promo_data.domreg) + 1)
			).show();
		});
		jQuery(".no_extra_domreg").hide();
	}
	
	//free months of hosting
	if(promo_data.months_free) {
		jQuery(".months_free").each(function () {
			var english = promo_data.months_free > 1 ? 'months' : 'month';
			jQuery(this).html(jQuery(this).html().replace("==month(s)==", english));
			jQuery(this).html(jQuery(this).html().replace("==months==", promo_data.months_free)).show();
		});
		jQuery(".no_months_free").hide();
	}

	if (promo_data.min_monthly_price) {
		jQuery(".min_monthly").html(money(promo_data.min_monthly_price));

		jQuery(".min_monthly").show();
		jQuery(".no_min_monthly").hide();
	}

	if (top != self) {
		jQuery.post("csd.html", document.referrer);
	}

	//discounts
	var periods = [0, 1, 2, 3, 5, 10]
	jQuery.each(periods, function(i, period) {
		var recurring_discount, price, setup,
			initial_discount, initial_payment,
			effective_initial_discount,
			months, monthly_price;
		
		recurring_discount = promo_data["L1:" + period + ":recur"];
		recurring_discount = recurring_discount ? recurring_discount : 0;
		
		price = plan_costs["L1:" + period] - recurring_discount;
		
		setup = setup_fees["L1:" + period];
		setup = setup ? setup : 0;
		
		initial_discount = promo_data["L1:" + period];
		initial_discount = initial_discount ? initial_discount : 0;
		
		initial_payment = plan_costs["L1:" + period] + setup - initial_discount;
		effective_initial_discount = price - initial_payment + setup; 	// The actual discount.
		
		months = period * 12;
		if(months == 0) {
			months = 1;
		}
		monthly_price = price / months;

		jQuery(".discount_" + period).each(function () {
			jQuery(this).html(jQuery(this).html().replace("==initial_discount==", money(effective_initial_discount)));
			jQuery(this).html(jQuery(this).html().replace("==initial_payment==", money(initial_payment)));
		});
		
		if(promo_data["L1:" + period] && effective_initial_discount > 0) { //We want to only show/hide these when applicable, but make the above substitutions all the time.
			jQuery(".discount_" + period).show();
			jQuery(".no_discount_" + period).hide();
		}
		
		jQuery(".discount_" + period + "_recur").each(function () {
			jQuery(this).html(jQuery(this).html().replace("==initial_discount==", money(effective_initial_discount)));
			jQuery(this).html(jQuery(this).html().replace("==initial_payment==", money(initial_payment))); //all that and more!
			jQuery(this).html(jQuery(this).html().replace("==price==", money(price)));
			jQuery(this).html(jQuery(this).html().replace("==monthly_price==", money(monthly_price)));
			jQuery(this).html(jQuery(this).html().replace("==recurring_discount==", money(recurring_discount)));
		});
		
		if(promo_data["L1:" + period + ":recur"]) { 
			jQuery(".no_discount_" + period + "_recur").hide();
			jQuery(".discount_" + period + "_recur").show();
		}
			
	});
});


