var dragsort = ToolMan.dragsort()
var junkdrawer = ToolMan.junkdrawer()

window.onload = function() {
	junkdrawer.restoreListOrder("boxes");
	dragsort.makeListSortable(document.getElementById("boxes"),saveOrder);
	changeDate(document.getElementById('RatesDate').value);
}

function saveOrder(item) {
	var group = item.toolManDragGroup
	var list = group.element.parentNode
	var id = list.getAttribute("id")
	if (id == null) return
	group.register('dragend', function() {
		ToolMan.cookies().set("list-" + id, 
				junkdrawer.serializeList(list), 365)
	})
}

function Node (nomeValuta, tassoValuta) {
  this.nome = nomeValuta;
  this.tasso = tassoValuta;
}

function trovaTasso(N) {
  for (var i=0; i<Valute.length; i++) {
    if (Valute[i].nome == N) return Valute[i].tasso;
  }
}
function trovaIndiceTasso(N) {
  for (var i=0; i<Valute.length; i++) {
    if (Valute[i].nome == N) return i;
  }
}
function round(number,X) {
  X = 4;
  return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}
function adjust(element) {
  return element.value.replace (',', '.');
}
function Edit(element) {
  // if (element.value == '') {
  if ((element.value == '') || (isNaN(element.value))) {
    element.value = '0';
    element.select();
  }
  Input = parseFloat(element.value);
  Nome  = element.name.substring(6); // input_VAL
  tassoValuta = trovaTasso(Nome);
  euroEquivalenti = Input/tassoValuta;
  for (var i=0; i<Valute.length; i++) {
    if (Valute[i].nome == Nome) continue;
    output = round(euroEquivalenti*Valute[i].tasso, 2);
    buffer = "document.getElementById('input_" + Valute[i].nome + "').value = output;";
    eval(buffer);
  }
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

