//Make sure jQuery is in noConflict mode
jQuery.noConflict();

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(); //fake NZA!
			if(promo_data.ip > 1) {
				jQuery(this).html(jQuery(this).html().replace("--Address(es)--", "Addresses"));
			} else {
				jQuery(this).html(jQuery(this).html().replace("--Address(es)--", "Address"));
			}
		});
		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--", promo_data.domreg + 1)).show();
		});
		jQuery(".no_extra_domreg").hide();
	}
	
	//free months of hosting
	if(promo_data.months_free) {
		jQuery(".months_free").each(function () {
			if(promo_data.months_free > 1) {
				jQuery(this).html(jQuery(this).html().replace("--month(s)--", "months"));
			} else {
				jQuery(this).html(jQuery(this).html().replace("--month(s)--", "month"));
			}
			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();
	}

	//discounts
	var periods = [0, 1, 2, 3, 5, 10]
	for(var i = 0; i < 6; i++) { //Prototype breaks for in
		var period = periods[i];
		
		var recurring_discount = promo_data["L1:" + period + ":recur"];
		recurring_discount = recurring_discount ? recurring_discount : 0;
		var price = plan_costs["L1:" + period] - recurring_discount;
		var setup = setup_fees["L1:" + period];
		setup = setup ? setup : 0;
		var initial_discount = promo_data["L1:" + period];
		initial_discount = initial_discount ? initial_discount : 0;
		var initial_payment = plan_costs["L1:" + period] + setup - initial_discount;
		var effective_initial_discount = price - initial_payment + setup; 	// The actual discount. MUDD code has equal 
																			// recurring and initial discounts to give it 
																			// 0 net cost, but we don't pay peeps money to use it.
		
		var months = period * 12;
		if(months == 0) {
			months = 1;
		}
		var 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();
		}
			
	}
});
