function findFirstNode(elem) {
	if (elem.childNodes) {
		for (var i = 0; i < elem.childNodes.length; i++) {
			var node = elem.childNodes[i];
			if (node.nodeType == 1)
				return node;
		}
	}
	return elem;
}

function getByClassName(className, parentElement) {
	var children = parentElement.getElementsByTagName("*");
	for (var i = 0; i < children.length; i++) {
		if ((" " + children[i].className + " ").indexOf(" " + className + " ") >= 0)
			return children[i];
	}
	return null;
}

function getEl(id){
	return document.getElementById(id);
}

var urlRegEx = RegExp(/(http:((\/\/)|(\\\\))+)?[\w\d:#@%\/;$()~_?\+-=\\\.&]/);

var MS = {
	count: 0,
	id: 0,
	max: 10,
	
	init: function(listTarget, fileInput, addFileBtn, addUrlBtn){
		this.total = getEl("total");
		this.maxNum = getEl("maxNumber");
		this.prototype = findFirstNode(getEl("rowProto"));
		this.submitBtn = getEl("submitBtn");
		this.submitBtn.disabled = true;
		this.listTarget = getEl(listTarget);
		this.listTarget.innerHTML = "";
		this.addFileElement(getEl(fileInput));
		var MS = this;
		
		var addFile = getEl(addFileBtn);
		addFile.onclick = function(){
			var inp = findFirstNode(getEl("upfile"));
			
			if (inp.value != "") {
				var newElement = document.createElement("input");
				newElement.type = "file";
				newElement.size = "25";
				newElement.className = "inputwb";
				
				inp.style.position = "absolute";
				inp.style.left = "-2000px";
				inp.parentNode.insertBefore(newElement, inp);
					
				inp.MS.addFileElement(newElement);
				inp.MS.addListRow(inp);
			}
		}
		
		var addURL = getEl(addUrlBtn);
		addURL.onclick = function(){
			var inp = findFirstNode(getEl("upurl"));
			inp.MS = MS;
			
			if (inp.value != "") {
				if (urlRegEx.test(inp.value) == false){
					alert("Invalid url!");
					return;
				}
				MS.addURLElement();
				MS.addListRow(inp, true);
				getEl("mId").value = MS.id;
				inp.value = "";
			}
		}
		
		var obj = getEl("urlinput");

		obj.onkeyup = function(e){
			var keycode = -1; 
			
			if (window.event) {
				keycode = window.event.keyCode;
				window.event.stopPropagation();
				window.event.preventDefault();
			} 
			else if (e) {
				keycode = e.which;
				e.stopPropagation();
				e.preventDefault();
			}
			
			var inp = this;
			inp.MS = MS;	
			if (keycode == 13) {
				if (inp.value != "") {
					if (urlRegEx.test(inp.value) == false){
						return;
					}
					MS.addURLElement(inp);
					MS.addListRow(inp, true);
					getEl("mId").value = MS.id;
					inp.value = "";
				}	
			}
		}
		obj.onkeypress = function(e){
			var keycode = -1; 
			if (window.event) {
				keycode = window.event.keyCode;
			} 
			else if (e) {
				keycode = e.which;
			}
			return (keycode != 13);
		}
	},
	
	toggleAvailability: function(enable){
		var c = enable || !(this.max != -1 && this.count >= this.max);
		findFirstNode(getEl("upfile")).disabled = !c;
		findFirstNode(getEl("upurl")).disabled = !c;
	},
	
	addFileElement: function(element){
		if (element.tagName == "INPUT" && element.type == "file"){
			element.name = "file_" + this.id++;
			element.MS = this;

			element.onchange = function(){
				var inp = findFirstNode(getEl("upfile"));

				if (inp.value != "") {
					var newElement = document.createElement("input");
					newElement.type = "file";
					newElement.size = "25";
					newElement.className = "inputwb";

					inp.style.position = "absolute";
					inp.style.left = "-2000px";
					inp.parentNode.insertBefore(newElement, inp);

					inp.MS.addFileElement(newElement);
					inp.MS.addListRow(inp);
				}
			}
			
			this.toggleAvailability();
			this.count++;
		}
	},
	
	addURLElement: function(){
		this.toggleAvailability();
		this.count++;
	},
	
	updateTotal: function(){
		this.total.innerHTML = "Files (" + (this.count-1) + "/" + this.max + ")";
		this.maxNum.value = this.count;
		this.submitBtn.disabled = (this.count <= 1);
	},

	addListRow: function(element, isUrl){
		var node = this.prototype.cloneNode(true);
		var MS = element.MS;
		
		var link = getByClassName("dellink", node);
		var inp = getByClassName("fname", node);
		node.element = element;
		inp.value = element.value;
		if (isUrl) inp.name = "url_" + MS.id++;
		
		MS.updateTotal();
		
		link.onclick = function(){
			node.parentNode.removeChild(node);
			if (!isUrl) element.parentNode.removeChild(element);
			MS.count--;
			MS.updateTotal();
			MS.toggleAvailability(true);
			return false;
		};

		this.listTarget.appendChild(node);
	}
}