
if (document.getElementById("proceed_to_checkout"))
	document.getElementById("proceed_to_checkout").disabled = 0;

// const variables
var CURRENCY_PREFIX = "$",

	PLIMUS_CHECKOUT_PAGE = "https://secure.plimus.com/jsp/checkout.jsp?developerId=88706&submitAction=buynow",
	PLIMUS_CONTRACT_ID_1 = "2074786",
	PLIMUS_CONTRACT_ID_2 = "2242186",

	// GUI controls
	ctrl_unit_price_1 = null,
	ctrl_unit_price_2 = null,
	
	ctrl_quantity_1 = null,
	ctrl_quantity_2 = null,

	ctrl_total_1 = null,
	ctrl_total_2 = null,
	ctrl_total = null;


var get_gui_controls = function(){

	ctrl_unit_price_1 = document.getElementById("unit_price_1");
	ctrl_unit_price_2 = document.getElementById("unit_price_2");
	
	ctrl_quantity_1 = document.getElementById("quantity_1");
	ctrl_quantity_2 = document.getElementById("quantity_2");
	
	ctrl_total_1 = document.getElementById("total_1");
	ctrl_total_2 = document.getElementById("total_2");
	
	ctrl_total = document.getElementById("total");
	
	// make sure they are correct
	if (null == ctrl_unit_price_1 || null == ctrl_unit_price_2 
			|| null == ctrl_quantity_1 || null == ctrl_quantity_2 
			|| null == ctrl_total_1 || null == ctrl_total_2 
			|| null == ctrl_total){
			
		return false;
	}
	
	return true;
}

var increase_product_quantity = function(product_number){

	// try to get GUI controls
	if (false == get_gui_controls()){
		return;
	}

	// increase proper product quantity
	if (1 == product_number){
		ctrl_quantity_1.value++;
	} else if (2 == product_number){
		ctrl_quantity_2.value++;
	}

	// update cart
	update_cart();
}

var decrease_product_quantity = function(product_number){

	// try to get GUI controls
	if (false == get_gui_controls()){
		return;
	}

	// decrease proper product quantity
	if (1 == product_number){

		if (ctrl_quantity_1.value > 0)
			ctrl_quantity_1.value--;

	} else if (2 == product_number){

		if (ctrl_quantity_2.value > 0)
			ctrl_quantity_2.value--;
	}

	// update cart
	update_cart();
}

var update_cart = function(){

	// try to get GUI controls
	if (false == get_gui_controls()){
		return;
	}
	
	// prepare data for calculations
	var quantity_1 = ctrl_quantity_1.value,
		quantity_2 = ctrl_quantity_2.value,
		total_1  = 0,
		total_2 = 0;
	
	
	// select price per unit value for product 1
	if (quantity_1 >= 1 && quantity_1 < 2){
		
		total_1  = 29.95;

	} else if (quantity_1 >= 2 && quantity_1 < 7){

		total_1 = 29.95 + ((quantity_1-1) * 19.47);

	} else if (quantity_1 >= 7 && quantity_1 < 21){

		total_1 = 29.95 + 5*19.47 + ((quantity_1-6) * 16.47);

	} else if (quantity_1 >= 21 && quantity_1 < 51){

		total_1 = 29.95 + 5*19.47 + 14*16.47 + ((quantity_1-20) * 14.97);

	} else if (quantity_1 >= 51){

		total_1 = 29.95 + 5*19.47 + 14*16.47 + 30*14.97 + ((quantity_1-50) * 11.98);
	}
	
	// select price per unit value for product 2
	if (quantity_2 >= 1 && quantity_2 < 2){
		
		total_2 = 39.95;

	} else if (quantity_2 >= 2 && quantity_2 < 7){

		total_2 = 39.95 + ((quantity_2-1) * 25.97);

	} else if (quantity_2 >= 7 && quantity_2 < 21){
		
		total_2 = 39.95 + 5*25.97 + ((quantity_2-6) * 21.97);

	} else if (quantity_2 >= 21 && quantity_2 < 51){

		total_2 = 39.95 + 5*25.97 + 14*21.97 + ((quantity_2-20) * 19.97);

	} else if (quantity_2 >= 51){

		total_2 = 39.95 + 5*25.97 + 14*21.97 + 30*19.97 + ((quantity_2-50) * 15.98);
	}
	
	// calculate total amounts
	var total = total_1 + total_2;

	// calculate price per unit
	var pp_unit_1 = quantity_1 > 0 ? total_1 / quantity_1 : 29.95;
	var pp_unit_2 = quantity_2 > 0 ? total_2 / quantity_2 : 39.95;
	
	// update GUI controls
	ctrl_unit_price_1.innerHTML = CURRENCY_PREFIX + pp_unit_1.toFixed(2);
	ctrl_unit_price_2.innerHTML = CURRENCY_PREFIX + pp_unit_2.toFixed(2);
	
	ctrl_total_1.innerHTML = CURRENCY_PREFIX + total_1.toFixed(2);
	ctrl_total_2.innerHTML = CURRENCY_PREFIX + total_2.toFixed(2);
	ctrl_total.innerHTML = CURRENCY_PREFIX + total.toFixed(2);
}


var ensure_quantity_is_filled = function(){

	// try to get GUI controls
	if (false == get_gui_controls()){
		return;
	}

	// there should be at least one item in the cart
	if (ctrl_quantity_1.value <= 0 && ctrl_quantity_2.value <= 0){
		ctrl_quantity_1.value = 1;
	}
	
	if (ctrl_quantity_2.value <= 0){
		ctrl_quantity_2.value = 0;
	}
	
	update_cart();
}


var get_purchase_link = function(){

	// try to get GUI controls
	if (false == get_gui_controls()){
		return;
	}
	
	// get quantity
	var quantity_1 = ctrl_quantity_1.value;
	var quantity_2 = ctrl_quantity_2.value;	

	// there should be at least one item in the cart
	if (quantity_1 <= 0 && quantity_2 <= 0){
		quantity_1 = 1;
	}
	
	var ret_link = PLIMUS_CHECKOUT_PAGE;
	var number_of_contracts = 0;
	
	if (quantity_1 > 0){
		number_of_contracts++;
		ret_link += "&" + "contractId" + number_of_contracts + "=" + 
			PLIMUS_CONTRACT_ID_1 + "&quantity" + number_of_contracts + "=" + quantity_1;		
	}
	
	if (quantity_2 > 0){
		number_of_contracts++;
		ret_link += "&" + "contractId" + number_of_contracts + "=" + 
			PLIMUS_CONTRACT_ID_2 + "&quantity" + number_of_contracts + "=" + quantity_2;
	}	
	
	ret_link += "&numberOfContracts=" + number_of_contracts;
	
	return ret_link;
}


var navigate_to_registrator = function(){

	ensure_quantity_is_filled();

	if (document.getElementById("proceed_to_checkout"))
		document.getElementById("proceed_to_checkout").disabled = 1;
	
	window.location = get_purchase_link();
}