
var application = {};

application.longDistancePlans = {
		"starter_plan": 0,
		"all_world_plan": 2.00,
		"world_world_plan": 2.00,
		"asia_for_you": 2.00,
		"north_america": 19.95,
		"intl_europe": 24.95,
		"intl_asian": 24.95,
		"intl_plus": 34.95
	}

addLoadEvent(function() {
//	detailsOff();
	
	
	application.longDistancePrice = 0;
	application.packagePriceRegular = 0;
	
	var features = getElementsByClassName("feature");
	var nf = features.length;

	if (nf > 0) {
		application.features = [];
	
		for (i=0; i < nf; i++) {
			application.features[i] = features[i];
		
			features[i].onchange = calculateNumFeatures;
			features[i].onclick = calculateNumFeatures;
		}
	
		calculateNumFeatures();
	}
	
	$("radio_LD_NorthAmerica").onchange = calculateNumFeatures;
	$("radio_LD_NorthAmerica").onclick = calculateNumFeatures;
	
	$("radio_LD_International").onchange = calculateNumFeatures;
	$("radio_LD_International").onclick = calculateNumFeatures;


	$("province").onclick = synchProvinceToggles;
	$("province").onchange = synchProvinceToggles;

	if ($("province_pick_ON")) {
		$("province_pick_ON").onclick = synchProvinceToggles;
		$("province_pick_ON").onchange = synchProvinceToggles;
		$("province_pick_NB").onclick = synchProvinceToggles;
		$("province_pick_NB").onchange = synchProvinceToggles;
		$("province_pick_NF").onclick = synchProvinceToggles;
		$("province_pick_NF").onchange = synchProvinceToggles;

		setProvinceRadios($("province").value);
	}

	application.longDistanceRadios = getElementsByClassName("long_distance");
	var nc = application.longDistanceRadios.length;

	if (nc > 0) {
		updateLongDistanceTip();
	
		for (i=0; i < nc; i++) {
			application.longDistanceRadios[i].onchange = updateLongDistanceTip;
			application.longDistanceRadios[i].onclick = updateLongDistanceTip;
		}
	}

	if ($("yes_keep")) {
		$("yes_keep").onchange = togglePortInfo;
		$("yes_keep").onclick = togglePortInfo;
		$("get_new").onchange = togglePortInfo;
		$("get_new").onclick = togglePortInfo;
		togglePortInfo()
	}

	if ($("yes_ex_int")) {
		$("yes_ex_int").onchange = setHighSpeedDisplay;
		$("yes_ex_int").onclick = setHighSpeedDisplay;
		$("no_ex_int").onchange = setHighSpeedDisplay;
		$("no_ex_int").onclick = setHighSpeedDisplay;
		setHighSpeedDisplay();
	}
	
 	jQuery("#details_rogers").hover(detailsOn);


	var detailsClose = getElementsByClassName("details_close");
	var nd = detailsClose.length;
	for (i=0; i < nd; i++) {
		detailsClose[i].onclick = detailsOff;
	}	

	$("unlisted_number").onclick = updatePriceCalculator;
	$("unlisted_number").onchange = updatePriceCalculator;

	$("distinctive_ring").onclick = updatePriceCalculator;
	$("distinctive_ring").onchange = updatePriceCalculator;

});

function checkServiceability() {
	//UserAjax.UpdateStatus({userID: this.user_id.value, status: update}, statusUpdated);

	var pc = $("pc").value;

	ServiceabilityAjax.CheckServiceability(
							{postal_code: pc, product: "RHP"},
							function(result) {
								if (!result) {
									$("no_service").style.display = "block";
									$("yes_service").style.display = "none";
								} else {
									$("no_service").style.display = "none";
									$("yes_service").style.display = "block";
								}
								
								ServiceabilityAjax.getServiceabilityRecord({postal_code: $("pc").value}, populateServiceabilityData);
							});
	
	return false;
}

function populateServiceabilityData(response) {
	$("city").value = response["CITY"];
	$("postal_code").value = response["POSTAL_CODE"];
	$("address").value = "   " + response["STREET_NAME"] + " " + response["STREET_TYPE"] + " " + response["COMPASS_DIRECTION_CODE"];
	$("prov_" + response["PROV_CODE"]).selected = true;
	
	calculateNumFeatures();
}

function synchProvinceToggles() {
	if ($("province_pick_ON")) {
		// Two province selectors exist, we need to synch.
		if (this.id == "province") {
			// The change was made with the drop-down, so we change the radios to match.
			setProvinceRadios(this.value);
		} else {
			// The change was made with the top radio buttons, so we change the drop-down to match.
			var prov = ["ON", "NB", "NL"];
			for (i = 0; i < 3; i++) {
				if ($("province_pick_" + prov[i]).checked) {
					$("province").value = prov[i];
					break;
				}
			} 
		}
	}

	calculateNumFeatures();
	return true;
}

function setProvinceRadios(prv) {
	$("province_pick_ON").checked = false;
	$("province_pick_NB").checked = false;
	$("province_pick_NF").checked = false;
	
	$("province_pick_" + prv).checked = true;
}


function togglePortInfo() {
	if ($("yes_keep").checked) {
		$("number_port_wrap").style.display = "block";
		
		//$("tips_get_new_number").style.display = "none";
		//$("tips_keep_existing").style.display = "block";
	} else {
		$("number_port_wrap").style.display = "none";

		//$("tips_get_new_number").style.display = "block";
		//$("tips_keep_existing").style.display = "none";
	}
}

function updateLongDistanceTip() {
	var nc = application.longDistanceRadios.length;

	var selectedId = false;

	for (i=0; i < nc; i++) {
		if (application.longDistanceRadios[i].checked) {
			//$("tips_" + application.longDistanceRadios[i].id).style.display = "block";
			selectedId = application.longDistanceRadios[i].id;
		} else {
			//$("tips_" + application.longDistanceRadios[i].id).style.display = "none";
		}
	}	

	
	if (!selectedId) {
		//$("tips_starter_plan").style.display = "block";
		$("starter_plan").checked = "true";
		application.longDistancePrice = 0;
	} else {
		application.longDistancePrice = application.longDistancePlans[selectedId];
	}
	updatePriceCalculator();
}

function updateFees(province) {
	var feeWraps = getElementsByClassName("fees");
	var nfw = feeWraps.length;
	
	for (i=0; i < nfw; i++) {
		feeWraps[i].style.display = "none";
	}
	
	feeWraps = getElementsByClassName("fees_" + province);
	nfw = feeWraps.length;
	
	for (i=0; i < nfw; i++) {
		feeWraps[i].style.display = "inline";
	}
}

function calculateNumFeatures() {
	var nf = application.features.length;
	
	var numChecked = 0;
	
	for (i=0; i < nf; i++)
		if (application.features[i].checked)
			numChecked++;

    var plan = "";
	var localPlan;
       
	if ($("radio_LD_International").checked) {
		plan = {
			"name": config.rogers.intl.name,
			"price": roundNumber(calculateRogersPlanPrice(config.rogers.intl, numChecked), 2, true),
			"keyword": config.rogers.intl.keyword
		};
		localPlan = calculateRogersPlans(numChecked);
	} else {
		plan = calculateRogersPlans(numChecked);
	}

	var region = $("province").value.toLowerCase();
	updateFees(region);
	
	if (region != "on") {
		plan.price = roundNumber(plan.price * 1 + config.rogers.differentials[region], 2, true);
	}
	
	setCost("rogers", plan.price);
	$("plan_name").innerHTML = plan.name;

	jQuery("#basic_ld_minutes").hide();
	jQuery("#classic_ld_minutes").hide();
	jQuery("#extras_ld_minutes").hide();
	jQuery("#intl_ld_minutes").hide();
	jQuery("#" + plan.keyword + "_ld_minutes").show();
	
	jQuery("#basic_incl_minutes").hide();
	jQuery("#classic_incl_minutes").hide();
	jQuery("#extras_incl_minutes").hide();
	
	if (plan.keyword != "intl") {
		jQuery("#" + plan.keyword + "_incl_minutes").show();
	} else if (localPlan) {
		jQuery("#" + localPlan.keyword + "_incl_minutes").show();		
	} else {
		jQuery("#basic_incl_minutes").show();
	}

	
	application.packagePriceRegular = plan.price - 1 + 1;
	application.currentPlan = plan;

	updateDetails();
	
	if (!$("radio_LD_International").checked) {
	
		jQuery('#LD_caveat').hide();
		
		if (numChecked == 0)
			showUpgradeCallout("upgrade_classic");
		else if (numChecked == 1)
			showUpgradeCallout("add_feature_classic");
		else if (numChecked == 3)
			showUpgradeCallout("upgrade_extras");
		else if (numChecked == 4)
			showUpgradeCallout("add_feature_extras");
		else
			hideUpgradeCallout();
		} 
	else {
		hideUpgradeCallout();    
			if (numChecked < 2){
		jQuery('#LD_caveat').show();
	}
	else {
		jQuery('#LD_caveat').hide();
	}    

	}

	
	updatePriceCalculator();
}

function showUpgradeCallout(box) {
	$("callout_upgrade_extras").style.display = "none";
	$("callout_upgrade_classic").style.display = "none";
	$("callout_add_feature_classic").style.display = "none";
	$("callout_add_feature_extras").style.display = "none";
	
	$("callout_" + box).style.display = "block";
	
	$("upgrade_callout").style.display = "block";
}

function hideUpgradeCallout() {
	$("upgrade_callout").style.display = "none";
}


function detailsOn() {
	detailsOff();
//	$("details_rogers_" + application.currentPlan.keyword).style.display = "block";
	jQuery(".details_rogers_" + application.currentPlan.keyword).show();
	return false;
}


function detailsOff() {
//  $("details_rogers_basic").style.display = "none";
//	$("details_rogers_classic").style.display = "none";
//	$("details_rogers_extras").style.display = "none";
	
	jQuery(".details_rogers_basic").hide();
	jQuery(".details_rogers_classic").hide();
	jQuery(".details_rogers_extras").hide();
	jQuery(".details_rogers_intl").hide();
	
	return false;
}

/** If the details pane is open, make sure it's the right one.
 */
function updateDetails() {
	if ($("details_rogers_basic").style.display == "block" || 
		$("details_rogers_classic").style.display == "block" ||
		$("details_rogers_extras").style.display == "block" ||
		$("details_rogers_intl").style.display == "block")

	{
		detailsOff();
		$("details_rogers_" + application.currentPlan.keyword).style.display = "block";
	}
}

/** To be used if the user changes feature or location options when the details pane is open
 */
function updateDetailsPaneIfNecessary() {
	
}


function updatePriceCalculator() {
	extras = 0;
	
	if ($("unlisted_number").checked)
		extras += 2.00;

	if ($("distinctive_ring").checked)
		extras += 4.00;
	
	$("fee_regular").innerHTML = roundNumber(application.packagePriceRegular + application.longDistancePrice + extras, 2, true)
}

function setHighSpeedDisplay() {
	if ($("yes_ex_int").checked) {
		$("high_speed_details").style.display = "inline";
	} else {
		$("high_speed_details").style.display = "none";
	}
}

/**	plan must have the following properties:
 *		- basePrice
 *		- featuredIncluded
 */
function calculateRogersPlanPrice(plan, numFeatures) {
	var price = plan.basePrice;
	if (numFeatures > plan.featuresIncluded) {
		price += (numFeatures - plan.featuresIncluded) * config.rogers.costPerFeature;
	}
	return price;
}

function calculateRogersPlans(numFeatures) {
	if (!numFeatures) {
		numFeatures = 0;
	}
	
	var basicPrice = calculateRogersPlanPrice(config.rogers.basic, numFeatures);
	var classicPrice = calculateRogersPlanPrice(config.rogers.classic, numFeatures);
	var extrasPrice = calculateRogersPlanPrice(config.rogers.extras, numFeatures);
	
	var finalPrice = 0;
	var finalPlan = {};
	
	if (basicPrice < classicPrice) {
		finalPrice = basicPrice;
		finalPlan = config.rogers.basic;
	} else {
		finalPrice = classicPrice;
		finalPlan = config.rogers.classic;
	}
	
	if (extrasPrice <= finalPrice) {
		finalPrice = extrasPrice;
		finalPlan = config.rogers.extras;
	}
	
	return {
		"name": finalPlan.name,
		"price": roundNumber(finalPrice, 2, true),
		"keyword": finalPlan.keyword
	};
}

/**	provider is something like "rogers", or "aliant_nf".
 *	price is something like 28.62.
 */
function setCost(provider, price) {
	if (!$(provider + "_dollars"))
		return;

	var splat = price.split(".");
	
	$(provider + "_dollars").innerHTML = splat[0];
	
	if (splat[1]) {
		$(provider + "_cents").innerHTML = splat[1];
	} else {
		$(provider + "_cents").innerHTML = "00";
	}
}
