// 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,2);

    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;

	
       
	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="";
		
		
		

       //CPU 1 Logic
       if((ram>=3)&&(vcpu==1))
        {          
              $('messages').innerHTML = "Unsupported Configuration";
            }else if (ram<=2 && vcpu==1){

                        if(ram == 0.5)	base = 0.02;
			else if(ram == 1)	base = 0.03;
			else if(ram == 2)	base = 0.05;
      
        
        }

       //cpu 2 logic
        if(((ram>=6)&&(vcpu==2))|| ((ram<=1)&&(vcpu==2)))
        {
              $('messages').innerHTML = "Unsupported Configuration";
            }else if (ram<=4 && vcpu==2){

                        if(ram == 2)	base = 0.06;
			else if(ram == 4)	base = 0.10;
			


        }

        //cpu 3 logic
        if(((ram>=8)&&(vcpu==3))|| ((ram<=2)&&(vcpu==3)))
        {
              $('messages').innerHTML = "Unsupported Configuration";
            }else if (ram>=4 && vcpu==3){

                        if(ram == 4)	base = 0.11;
			else if(ram == 6)	base = 0.15;



        }

        //CPU 4 Logic
       if((ram<=2)&&(vcpu==4))
        {
              $('messages').innerHTML = "Unsupported Configuration";
            }else if (ram>=4 && vcpu==4){

                        if(ram == 4)	base = 0.12;
			else if(ram == 6)	base = 0.16;
			else if(ram == 8)	base = 0.20;


        }

         //CPU 5 Logic
       if((ram<=4)&&(vcpu==5))
        {
              $('messages').innerHTML = "Unsupported Configuration";
            }else if (ram>=6 && vcpu==5){

                        if(ram == 6)	base = 0.17;
			else if(ram == 8)	base = 0.21;

		 }

        //CPU 6 Logic
       if((ram<=4)&&(vcpu==6))
        {
              $('messages').innerHTML = "Unsupported Configuration";
            }else if (ram>=6 && vcpu==6){

                        if(ram == 6)	base = 0.18;
			else if(ram == 8)	base = 0.22;

		 }

        //CPU 6 Logic
       if((ram<=4)&&(vcpu==6))
        {
              $('messages').innerHTML = "Unsupported Configuration";
            }else if (ram>=6 && vcpu==6){

                        if(ram == 6)	base = 0.18;
			else if(ram == 8)	base = 0.22;

		 }

         //CPU 7 Logic
       if((ram<=6)&&(vcpu==7))
        {
              $('messages').innerHTML = "Unsupported Configuration";
            }else if (ram>=8 && vcpu==7){

                        if(ram == 8)	base = 0.23;
			

		 }

        //CPU 8 Logic
       if((ram<=6)&&(vcpu==8))
        {
              $('messages').innerHTML = "Unsupported Configuration";
            }else if (ram>=8 && vcpu==8){

                        if(ram == 8)	base = 0.24;


		 }
		 if(base>0)
		 {
			perHour += (Number($('os').value)/10);

			perHour += base;
		 }
		 else
		 perHour="";

	if($('firewall').checked) perHour += 0.01;
	//if($('loadbalance').checked) perHour += 0.02;

	var storage = Number($('storage').value);
	if(storage < 1) 			perHour += storage * (0.05 / 720);
	else if (storage < 10)	perHour += storage * (0.05 / 720);
	else 						perHour += storage * (0.05 / 720);

	var subtotal  = (perHour * Number($('hours').value));

	subtotal += Number($('uploads').value) * 0.05;


	var downloads = Number($('downloads').value);
	if(downloads < 1024) 		subtotal += downloads * 0.05;
	else if (downloads < 5120)	subtotal += downloads * 0.05;
	else 						subtotal += downloads * 0.05;

	if(Number($('hours').value) > 0){
		$('cost').innerHTML = formatCurrency(subtotal);
	} else {

		$('cost').innerHTML = "\u00A3 --.--";
	}

}

