var baseuploadrate = 500;
var valueuploadrate = 500;
var minuploadrate = 1000;

var basedownrate = 500;
var valuedownrate = 100;
var mindownrate = 1000;


var basetorrent = 1;
var valuetorrent = 400;
var mintorrent=1;

var basesize = 10;
var valuesize = 200;
var minsize = 10;

function calcPice(a, b, c, d)
{
  var e = ((a-b) / c)*d;
  return e;
}

function recalcPice()
{
  var uploadrate = parseFloat(document.getElementById("uploadrate").value);
  var downrate = parseFloat(document.getElementById("downrate").value);
  var size = parseFloat(document.getElementById("size").value);
  var torrent = parseFloat(document.getElementById("maxtorrent").value);
  var time = parseFloat(document.getElementById("time").value);
  var radio = parseFloat(document.getElementById("radio").value);


  var total = 
    calcPice(uploadrate, minuploadrate, baseuploadrate, valueuploadrate)+
    calcPice(downrate, mindownrate, basedownrate, valuedownrate) + 
    calcPice(size, minsize, basesize, valuesize) + 
    calcPice(torrent, mintorrent, basetorrent, valuetorrent) +
    (radio/25*1000)
    + 200; 

  if (time < 6)  
    total = (total * 1) * time;
  
  if ( (time >= 6) && (time<12) )  
    total = (total * 0.95) * time;

  if (time == 12)  
    total = (total * 0.90) * time;
  
//  if (time > 1) 
//    total = total - 1000;
    

  document.getElementById("total").value = Math.round(total);

  var pp = document.getElementById("cpin");
  if (pp != null)
  {
    document.getElementById("amount").value = Math.round(total+90+(total*0.034));
    var cpin = parseFloat(document.getElementById("cpin").value);
    document.getElementById("item_name").value = 
      "FTP - Size: "+size+"GB, Up: "+uploadrate+"KByte/s, torrent: "+torrent+"db, time: "+time+" month, radio:"+radio+" PIN: "+cpin + "+VAT";
  }


}




function incDecValue(id, delta, min, max)
{
  var ctrl = document.getElementById(id);
  var value = parseFloat(ctrl.value) + delta;
  
  if (value < min)
    value = min;
  if (value > max)
    value = max;

//  if (id == "time" && value == 7)
//    value = 6;
    
  ctrl.value = value;
  recalcPice();    
  
}



var _secs;
var _timerID = null;
var _timerRunning = false;
var _delay = 1000;
var _proc;

function _InitializeTimer(sec, proc)
{
// Set the length of the timer, in seconds
  _secs = sec;
  _StopTheClock()
  _proc = proc;
  _StartTheTimer()
}

function _StopTheClock()
{
  if(_timerRunning)
    clearTimeout(timerID)
  _timerRunning = false
}


function _StartTheTimer()
{
  if (_secs==0)
  {
    _StopTheClock()
    _proc();
  }
  else
  {
    _secs = _secs - 1
    _timerRunning = true
    _timerID = self.setTimeout("StartTheTimer()", delay)
  }
}
												
