// JavaScript Document

//data from form
var mem = 0;    //MB
var cpu = 0;    //Cores
var storage = 0; //GB 

//blocks
var b_mem  = 256;
var b_cpu  = 0.1;
var b_stor = 10;

//number of blocks
var n_mem  = 0;
var n_cpu  = 0.0;
var n_stor = 0;

var val_agree = 0;
var val_prof =  0;



function calcTotalPerMonth()
{
   var total = 0;
   var prefix = "";
   
   total = parseFloat((n_mem * comp_price[currency][val_mem])) + 
           parseFloat((n_cpu * comp_price[currency][val_cpu])) + 
		   parseFloat((n_stor * comp_price[currency][val_stor])) + parseFloat(val_agree);
   
   if (currency == EURO)
       prefix = "EURO";
   
   if (currency == RS)
       prefix = "R$";

   if (currency == USD)
       prefix = "USD $";
   
   if (currency == NOK)
       prefix = "NOK";

   total = total+""; //convert to string
   idx = total.indexOf(".");
   
   
   //remove unecessary numbers, checks if there are more then 2 numbers after 
   //separator . (dot), in this case only 2 numbers will remain
   if ( idx > 0)
   {
       if ( (total.length - idx) > 2)
	       total = total.substring(0,idx+3);
   }
   
   //implementing security: when user sets 0 to all fields the total value is not calculated.
   //Its avoid the user to discover what is the value of service agreement when just its combo was selected
   if (n_mem == 0 && n_cpu == 0 && n_stor ==0)
     document.getElementById("div_val_total").innerHTML = prefix + " -- ";
   else
     document.getElementById("div_val_total").innerHTML = prefix + " " + total;
   
   return total;
}


function checkRangeMem( field )
{
	var mem_up_limit = 8192;
	
  	try
	{
          
          if ( field.value.Trim() == "" )
	      mem = 0;
	   else
	      mem = parseFloat( field.value ); //convert to integer
	  
	  //check up range
	  if (mem > mem_up_limit)
	    {
	       mem = mem_up_limit;
	       field.value = mem_up_limit; //sets max range
            }
            
            
          
	}
	catch ( exception ) 
	{
	  alert(exception);	
	}
}



function checkRangeCPU( field )
{
	var max_cpu = 8;
  	try
	{
	  //handle blank field
	  if ( field.value.Trim() == "" )
	      cpu = 0;
	   else
	      cpu = parseFloat( changeCommaDot(field.value) ); //convert to integer
	 
	   //check up range
	   if (cpu > max_cpu)
	    {
	       cpu = max_cpu;
	       field.value = max_cpu; //sets max range
            }
            
          
	}
	catch ( exception ) 
	{
	  alert(exception);	
	}
}





function checkRangeStorage( field )
{
	var max_storage = 250;
  	try
	{
	  //handle blank field
	  if ( field.value.Trim() == "" )
	      storage = 0;
	  else 
	      storage = parseFloat( field.value ); //convert to integer
	 
	   //check up range
	   if (storage > max_storage)
	    {
	       storage = max_storage;
               field.value = max_storage; //sets max range
            }
            
	}
	catch ( exception ) 
	{
	  alert(exception);	
	}
}




function calcMemUnits()
{
	var msg = "";
	n_mem = Math.floor(mem/b_mem);
	
	if ( (mem % b_mem) != 0 )
	    n_mem++;
	
	//case of value less than block
	if (mem < b_mem && mem != 0)
	  n_mem = 1;
        
        
	msg = n_mem + " x " + b_mem + "MB = " + n_mem * b_mem + "MB";
	document.getElementById("mem").innerHTML = msg;
        
        calcTotalPerMonth(); //calculate and refreshes total
}



function getDataComboAgree(cmb)
{
	document.getElementById("serv").innerHTML = cmb[cmb.selectedIndex].text;
	val_agree = parseFloat(serv_val[currency][cmb.selectedIndex]); //get the service money value 
        
        calcTotalPerMonth(); //calculate and refreshes total
}


function getDataComboProf(cmb)
{
	document.getElementById("prof").innerHTML = cmb[cmb.selectedIndex].text;
	
	document.getElementById("txtMem").value = mem = parseInt(prof_data[cmb.selectedIndex][2]);
	document.getElementById("txtCPU").value = cpu = parseFloat(prof_data[cmb.selectedIndex][1]);
	document.getElementById("txtStor").value = storage = parseInt(prof_data[cmb.selectedIndex][3]);
	
	calcCPUUnits();
	calcMemUnits();
	calcStorageUnits();
        
        calcTotalPerMonth(); //calculate and refreshes total
}

function calcCPUUnits()
{
	var msg = "";
	n_cpu = Math.round(cpu/b_cpu);
	
	//alert(cpu + "%" + b_cpu + "=" + n_cpu + " - " + Math.round ( parseFloat(cpu) % b_cpu ) );
	
	if ( Math.round ( parseFloat(cpu) % b_cpu ) != 0.0 )
	    n_cpu++;
	
	//case of value less than block
	if (cpu < b_cpu && cpu != 0)
	    n_cpu = 1;
	
	val = (n_cpu * b_cpu) + "  ";
	
	msg = n_cpu + " x " + b_cpu + " Cores = " + val.substring(0,3) + " Cores";
	document.getElementById("core").innerHTML = msg;
        
        calcTotalPerMonth(); //calculate and refreshes total
}



function calcStorageUnits()
{
    var msg = "";
	
	n_stor = Math.floor(storage/b_stor);
	
	if ( (storage % b_stor) != 0 )
	    n_stor++;
	
	//case of value less than block
	if (storage < b_stor && storage != 0)
	  n_stor = 1;
	   
	msg = n_stor + " x " + b_stor + "GB = " + n_stor * b_stor + "GB";
	document.getElementById("stor").innerHTML = msg;
        
        calcTotalPerMonth(); //calculate and refreshes total
}
