﻿var lastform = -1;

function initFileButton()
{
	for (var i = 0; i < document.all.length; i++)
	{
		var control = document.all[i];
		
		if (control.tagName == "FORM")
		{
			lastform = i;
		}
		else if (control.getAttribute("filebutton"))
		{
			if (lastform >= 0)
				control.setAttribute("fileform", "document.all["+lastform+"]."+control.getAttribute("filebutton"));
		}
		else if (control.tagName == "DIV" && control.getAttribute("filename"))
		{
			var layertext = "";
			
			control.id = control.getAttribute("filename")+"_layer"+parseInt(Math.random() * 1000000000000000);

			layertext += "<input type='file' name='"+control.getAttribute("filename")+"' layerid='"+control.id+"'";
			layertext += " style='width:50px; height:50px; border:1px; cursor:default;'";
			
			if (control.getAttribute("displaytext"))
				layertext += " onchange='onChangeFile(this, this.form."+control.getAttribute("displaytext")+");'";
				
			layertext += ">";

			control.style.position = "absolute";
			control.style.left = 0;
			control.style.top = 0;
			control.style.width = 6;
			control.style.height = 6;
			control.style.overflow = "hidden";
			control.style.filter = "alpha(opacity=0)";
			control.style.clip = "rect(5, 9, 9, 5)";

			control.innerHTML = layertext;
		}
	}
}

function document.onmousemove()
{
	var eobj = window.event.srcElement;
	
	if (eobj.getAttribute("fileform"))
	{
		eval("var file = "+eobj.getAttribute("fileform")+";");
		var form = file.form;
		
		eval("var layer = "+file.getAttribute("layerid")+";");
		
		layer.style.left = event.x - 7 + document.body.scrollLeft;
		layer.style.top = event.y - 7 + document.body.scrollTop;
	}
}

function onChangeFile(file, text)
{
	text.value = file.value;
}