function showIcon(id, path, url)
{ if (document.getElementById && document.createElement)
  { var div = document.getElementById(id);
    if (!div)
      return;
    while (div.lastChild)
    { div.removeChild(div.lastChild);
    }
    if (path)
    { var img = new Image();
      img.src = path;
      img.align = 'right';

      if (url)
      { var link = document.createElement("a");
        link.href = url;
        link.className = 'thumbnail';
        link.target = '_blank';

        link.appendChild(img);
        div.appendChild(link);
      } else
      { div.appendChild(img);
      }
    }
  }
}


function showText(id, text, url)
{ if (document.getElementById && document.createElement)
  { var div = document.getElementById(id);
    if (div)
    { while (div.lastChild)
      { div.removeChild(div.lastChild);
      }
      if (text)
      { var txt = document.createTextNode(text);
        if (url)
        { var link = document.createElement("a");
          link.href = url;
          link.className = 'thumbnail';
          link.target = '_blank';

          link.appendChild(txt);
          div.appendChild(link);
        } else
        { div.appendChild(txt);
        }
      }
    }
	//div.innerHTML = '';
	//if (text) div.innerHTML = text;
  }
}


function clearFile(attr)
{ if (document.getElementById && document.createElement)
  { if (attr['path_id']) document.getElementById(attr['path_id']).value = '';
    if (attr['icon_id']) document.getElementById(attr['icon_id']).value = '';
    if (attr['name_id']) document.getElementById(attr['name_id']).value = '';
    if (attr['show_path_id']) showText(attr['show_path_id']);
    if (attr['show_icon_id']) showIcon(attr['show_icon_id']);
    if (attr['show_name_id']) showText(attr['show_name_id']);
  }
}


function createFileFinish(attr)
{ if (document.getElementById && document.createElement)
  { if (attr && save)
    { if (save['path_id']) document.getElementById(save['path_id']).value = attr['path'];
      if (save['icon_id']) document.getElementById(save['icon_id']).value = attr['icon'];
      if (save['name_id']) document.getElementById(save['name_id']).value = attr['name'];
      if (save['show_path_id']) showText(save['show_path_id'], attr['path'], attr['path']);
      if (save['show_icon_id']) showIcon(save['show_icon_id'], attr['icon'], attr['path']);
      if (save['show_name_id']) showText(save['show_name_id'], attr['name'], attr['path']);
    } else
    { alert('An error occurred while creating your file');
    }
  }
}


var save;
function createFile(attr, template)
{ if (document.getElementById && document.createElement)
  { save = attr;
    var query = template ? '?template=' + template : '';
    var win = window.open('/cgi-bin/file-create.cgi' + query, 'win', 'top=50, left=50, width=600, height=500');
  }
}


function initFile(attr, link)
{ if (document.getElementById && document.createElement)
  { var path, icon, name;
    if (attr['path_id'])
      path = document.getElementById(attr['path_id']).value;
    if (attr['icon_id'])
      icon = document.getElementById(attr['icon_id']).value;
    if (attr['name_id'])
      name = document.getElementById(attr['name_id']).value;

    if (attr['show_path_id'] && path)
      showText(attr['show_path_id'], path, link ? path : undefined);
    if (attr['show_icon_id'] && icon)
      showIcon(attr['show_icon_id'], icon, link ? path : undefined);
    if (attr['show_name_id'] && name)
      showText(attr['show_name_id'], name, link ? path : undefined);
  }
}
