$(document).ready(function(){
	if (typeof(selectedPlan.id) !== "undefined") {
		var licenses = selectedPlan.licenses;
		var billPeriod = selectedPlan.billPeriod;
		var discount = selectedPlan.discount;
		
		selectedPlan = plans[selectedPlan.id];
		selectedPlan.licenses = licenses;
		selectedPlan.billPeriod = billPeriod;
		selectedPlan.discount = discount;
		
		// Scroll window
		var targetOffset = ($('#upgradePlanDiv').length > 0) ? $('#upgradePlanDiv').offset().top : $('#content').offset().top + 60;
		$('html,body').animate({scrollTop: targetOffset}, 1000);
		
		showSelectedPaidPlan(true);
	}
	
	$('div.plan_select_btn').click(function() {
		var id = $(this).attr('id');
		
		if (id == 'selectFreeBtn') {
			
		} else {
			window.location = context + '/user_portals/upgrade/' + id.replace('selectPaidBtn_', '');
		}
	});
	
	$('#processPlanBtn').live('click', function() {
		processPlan();
	});
	
	$('#validatePromoCodeDiv').live('click', function() {
		validatePromoCode();
	});
	
	$('#billPeriod').live('change', function() {
		selectedPlan.billPeriod = $(this).val();
		showSelectedPaidPlan(false);
	});
	
	$('#licenses').live('keyup', function() {
		selectedPlan.licenses = $(this).val().replace(/[^0-9]/g, '');
		showSelectedPaidPlan(false);
	});
	
});	

function showSelectedPaidPlan (isPlanChanged) {
	if (isPlanChanged) {
		// Step 1: Reset previous selected plan
		$('div.plan_details_selected').html('');
		
		var prevSelectedPlan = $('div.plan_selected');
		if (prevSelectedPlan.length > 0) {
			var prevSelectedPlanId = prevSelectedPlan
										.removeClass('plan_selected')
										.attr('id');
			$('#planDetails_' + prevSelectedPlanId.replace('selectedPlanDetails_', '')).css('background-color', '#E3F5FF');		
		}
		
		// Step 2: Show the selected plan 
		$('#selectedPlanDetails_' + selectedPlan.id)
			.html( $('#selectedPlanDetailsTemplate').html() )
			.addClass('plan_selected')
			.show('blind', null, 1000, null);
		$('#planDetails_' + selectedPlan.id).css('background-color', '#FFFFFF');
		
		// Hide Select button
		$('#selectPaidBtn_' + selectedPlan.id).html("<p style='color: #2880D4; font-size: 1.1em !important; font-weight: bold;'>Enter The Amount Of Users You Need.</p>");
	}
	
	// Init data
	if (typeof(selectedPlan.licenses) === "undefined" || selectedPlan.licenses === null) {
		selectedPlan.licenses = selectedPlan.base_licenses;
	}
	if (typeof(selectedPlan.billPeriod) === "undefined" || selectedPlan.billPeriod === null) {
		selectedPlan.billPeriod = BILL_PERIOD.M;
	}
	if (typeof(selectedPlan.discount) === "undefined" || selectedPlan.discount === null) {
		selectedPlan.discount = 0;
	}
	
	// Data
	$('#licenses').val(selectedPlan.licenses);
	$('#billPeriod').val(selectedPlan.billPeriod);
	
	var price;
	switch (selectedPlan.billPeriod) {
		case BILL_PERIOD.M:
			price = selectedPlan.cost_monthly + (selectedPlan.licenses - selectedPlan.base_licenses) * selectedPlan.rate_monthly;
			if (price < selectedPlan.cost_monthly) 
				price = selectedPlan.cost_monthly;
			
			$('#billPeriodSummary').html('Billed Monthly');
			break;
			
		case BILL_PERIOD.Y:
			price = selectedPlan.cost_yearly + (selectedPlan.licenses - selectedPlan.base_licenses) * selectedPlan.rate_yearly;
			if (price < selectedPlan.cost_yearly)
				price = selectedPlan.cost_yearly;
			
			$('#billPeriodSummary').html('Billed Yearly');
			break;
			
		default:
			price = selectedPlan.cost_monthly + (selectedPlan.licenses - selectedPlan.base_licenses) * selectedPlan.rate_monthly;
			if (price < selectedPlan.cost_monthly) 
				price = selectedPlan.cost_monthly;
			
			$('#billPeriodSummary').html('Billed Monthly');
			break;
			
	}
	price = price.toFixed(2);
	$('#price').html('$' + price);
	
	var discountAmount = (price * selectedPlan.discount)/100; 
	discountAmount = discountAmount.toFixed(2);
	
	var total = price - discountAmount;
	total = total.toFixed(2);
	
	$('#discountAmount').html('-$' + discountAmount);
	$('#totalPrice').html('$' + total);
	$('#planIdHidden').val(selectedPlan.id);
	
}

function processPlan() {
	if (!validateSelectedPlan())
		return;
	
	$('#selectedPlanFrm').submit();
}

function validateSelectedPlan() {
	return true;
}

function validatePromoCode() {
	var code = $.trim($('#promo').val());
	
	if(code == '') {
		return;
	}
	
	window.location = context + '/payments/checkPromotionCode?code=' + code + 
															'&plan=' + selectedPlan.id +
															'&licenses=' + selectedPlan.licenses + 
															'&billPeriod=' + selectedPlan.billPeriod;
}
