// Unility element selection function
function $(id){return document.getElementById(id);}

function init(){

	$('calculate').onclick = calculateCost;

	var elements = ["os", "ram", "vcpu", "storage", "uploads",
"downloads", "firewall", "loadbalance", "hours"];

	for(var x=0;x<elements.length; x++){

		$(elements[x]).onchange = calculateCost;

	}

}

function formatCurrency(num) {

	num = num.toString().replace(/[^0-9.-]/g, '');

	if(isNaN(num)) num = '0';
/* old code
	sign = (num < 0);
	num = Math.floor((Math.abs(num) * 100) + 0.50000000001);
	pennies = num % 100;
	num = Math.floor(num / 100).toString();

	if (num < 0) num = "0";
	if (pennies < 10) pennies = "0" + pennies;

	return "\u00A3 " + (((sign) ? '-' : '') + + num + '.' + pennies);
*/

    num = roundNumber(num,3);

    return "\u00A3 " + num;

}

function roundNumber(number,decimals) {
    var newString;  // The new rounded number
    decimals = Number(decimals);
    if (decimals < 1) {
        newString = (Math.round(number)).toString();
    } else {
        var numString = number.toString();
        // If there is no decimal point
        if (numString.lastIndexOf(".") == -1) {
            numString += ".";// give it one at the end
        }
        // The point at which to truncate the number
        var cutoff = numString.lastIndexOf(".") + decimals;
        // The value of the last decimal place that we'll end up with
        var d1 = Number(numString.substring(cutoff,cutoff+1));
        // The next decimal, after the last one we want
        var d2 = Number(numString.substring(cutoff+1,cutoff+2));
        // Do we need to round up at all? If not, the string will just be truncated
        if (d2 >= 5) {
            // If the last digit is 9, find a new cutoff point
            if (d1 == 9 && cutoff > 0) {
                while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
                    if (d1 != ".") {
                        cutoff -= 1;
                        d1 = Number(numString.substring(cutoff,cutoff+1));
                    } else {
                        cutoff -= 1;
                    }
                }
            }
            d1 += 1;
        }
        if (d1 == 10) {
            numString = numString.substring(0, numString.lastIndexOf("."));
            var roundedNum = Number(numString) + 1;
            newString = roundedNum.toString() + '.';
        } else {
            newString = numString.substring(0,cutoff) + d1.toString();
        }
    }
    // Do this again, to the new string
    if (newString.lastIndexOf(".") == -1) {
        newString += ".";
    }
    var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
    for(var i=0;i<decimals-decs;i++) newString += "0";
    //var newNumber = Number(newString);// make it a number if you like
    // Output the result to the form field (change for your purposes)
    return newString;
}

function calculateCost(){

	$('messages').innerHTML = "";

	// Pence per hour
	var perHour = 0;

	perHour += (Number($('os').value)/100);

	var ram = Number($('ram').value);
	var vcpu = Number($('vcpu').value);

	if(isNaN(vcpu)) vcpu = 0;

	// ram cost per hour - doesn't take into account extra cpu's
	var base = ram / 20;

	if(ram >= 8 && vcpu < 4){
		// 3 CPU's
		$('messages').innerHTML = "You can have 4 cpu's with no extra cost";

	} else if (ram >= 4){
		// 3 CPU's
		if (vcpu < 3) {

			$('messages').innerHTML = "You can have 3 cpu's with no extra cost";

		} else if(vcpu == 4){

			if(ram == 4)		base = 0.260;
			else if(ram == 5)	base = 0.305;
			else if(ram == 6)	base = 0.345;
			else if(ram == 7)	base = 0.380;

		}

	} else if(ram >= 2){
		// 2 CPU's
		if (vcpu < 2) {

			$('messages').innerHTML = "You can have 2 cpu's with no extra cost";

		} else if(vcpu == 3){

			if(ram == 2)		base = 0.140;
			else if(ram == 3)	base = 0.195;

		} else if(vcpu == 4){

			if(ram == 2)		base = 0.170;
			else if(ram == 3)	base = 0.240;

		}

	} else if(ram >= 0){

		if (vcpu < 1) {

			$('vcpu').value = 1;

		} else if(vcpu == 2){

			if(ram == 0.5)		base = 0.040;
			else if(ram == 1)	base = 0.070;
			else if(ram == 1.5)	base = 0.095;

		} else if(vcpu == 3){

			if(ram == 0.5)		base = 0.050;
			else if(ram == 1)	base = 0.090;
			else if(ram == 1.5)	base = 0.125;

		} else if(vcpu == 4){

			if(ram == 0.5)		base = 0.055;
			else if(ram == 1)	base = 0.100;
			else if(ram == 1.5)	base = 0.140;

		}

	}

	perHour += base;

	if($('firewall').checked) perHour += 0.01;
	//if($('loadbalance').checked) perHour += 0.02;

	var storage = Number($('storage').value);
	if(storage < 1024) 			perHour += storage * (0.30 / 720);
	else if (storage < 10240)	perHour += storage * (0.28 / 720);
	else 						perHour += storage * (0.26 / 720);

	var subtotal  = (perHour * Number($('hours').value));

	subtotal += Number($('uploads').value) * 0.07;


	var downloads = Number($('downloads').value);
	if(downloads < 1024) 		subtotal += downloads * 0.10;
	else if (downloads < 5120)	subtotal += downloads * 0.09;
	else 						subtotal += downloads * 0.08;

	if(Number($('hours').value) > 0){
		$('cost').innerHTML = formatCurrency(subtotal);
	} else {
		$('cost').innerHTML = "\u00A3 --.--";
	}

}