// (c) onNeutral Limited

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function updSummary()
{
	var selRSB = document.getElementById("RSB");
	var RSB_ddl = selRSB.options[selRSB.selectedIndex].value;

	var selBXSize = document.getElementById("BXSize");
	var BXSize_ddl = selBXSize.options[selBXSize.selectedIndex].value;

	var selDuration = document.getElementById("Duration");
	var Duration_ddl = selDuration.options[selDuration.selectedIndex].value;
	
	var ppg;
	var RSBdesc = '';
	var prcEuro = '';
	var VATEuro = '';
	var grossEuro = '';
	
	if ((RSB_ddl != '') && (BXSize_ddl != '') && (Duration_ddl != '')) 
	{
		BXSize_ddl = parseFloat(BXSize_ddl);
		Duration_ddl = parseInt(Duration_ddl);
		
		switch (RSB_ddl)
		{
			case 'BUS':
			  switch (true)
			  {
			  	case (BXSize_ddl <= 5.0):
				  ppg = 4.80;
				  break;

			  	case (BXSize_ddl <= 10.0):
				  ppg = 4.60;
				  break;

			  	case (BXSize_ddl <= 20.0):
				  ppg = 4.40;
				  break;

			  	case (BXSize_ddl <= 30.0):
				  ppg = 4.20;
				  break;

			  	case (BXSize_ddl <= 40.0):
				  ppg = 4.00;
				  break;

					default:
				  ppg = 3.80;
				  break;
			  }
			  RSBdesc = 'Business';
			  break;
			  
			case 'OFS':
			  switch (true)
			  {
			  	case (BXSize_ddl <= 10.0):
				  ppg = 3.20;
				  break;

			  	case (BXSize_ddl <= 20.0):
				  ppg = 3.04;
				  break;

			  	case (BXSize_ddl <= 30.0):
				  ppg = 2.88;
				  break;

			  	case (BXSize_ddl <= 40.0):
				  ppg = 2.72;
				  break;

					default:
				  ppg = 2.56;
				  break;
			  }
			  RSBdesc = 'Offsite';
			  break;
			  
			// ent  
			default:
			  switch (true)
			  {
			  	case (BXSize_ddl <= 5.0):
				  ppg = 20.00;
				  break;

			  	case (BXSize_ddl <= 10.0):
				  ppg = 19.20;
				  break;

			  	case (BXSize_ddl <= 20.0):
				  ppg = 18.80;
				  break;

			  	case (BXSize_ddl <= 30.0):
				  ppg = 18.40;
				  break;

			  	case (BXSize_ddl <= 40.0):
				  ppg = 18.00;
				  break;

					default:
				  ppg = 17.60;
				  break;
			  }
			  RSBdesc = 'Enterprise';
			  break;
		}
		
		var mnts = Duration_ddl;
		if (mnts == 12)
		{
			mnts = 11;
		}
		
		var proc_pp =document.getElementById("proc_pp").value;

		// update the display
  	ppg = ppg * 1.04;
		ppg = ppg * BXSize_ddl;

		Code_txt = RSB_ddl + '-' + Duration_ddl + '-' + BXSize_ddl; + '<BR />';
		Desc_txt = 'Remote Storage Backup ' + RSBdesc + ' ' + BXSize_ddl + 'GB vault ' + Duration_ddl + ' month subscription.';
		prcEuro = round_decimals(mnts * ppg, 2);
		VATEuro = round_decimals(prcEuro * 0.18, 2);
		grossEuro = round_decimals(parseFloat(prcEuro) + parseFloat(VATEuro), 2);
	}	
	else
	{
		Code_txt = '';
		Desc_txt = '';
	}
	
	document.getElementById("item_name").value = Code_txt;
	document.getElementById("item_number").value = Desc_txt;
	document.getElementById("amount").value = prcEuro.toString();
	document.getElementById("tax").value = VATEuro.toString();
	document.getElementById("s_Total").value = grossEuro.toString();
	
	return true;
}
