
String.prototype.filename = function()
{
	if (this.indexOf("\\") > -1){
		//windows system
		return this.substring(this.lastIndexOf("\\") + 1);
	}
	else if (this.indexOf("/") > -1){
		//unix system
		return this.substring(this.lastIndexOf("/") + 1);
	}
	else return this 
}

String.prototype.fileExt = function()
{
	return this.substring(this.lastIndexOf(".") + 1)
}

String.prototype.trim = function()
{
	return this.replace(/^\s*|\s*$/g,"");
}


function fileAttacher(el){
	if (el.type != "file") alert("Error, first paramater must be an input element of type 'file'")
	var form = el.form
	var canvas = document.createElement("div")
	canvas.className = "attachment_canvas"
	
	
	el.allowedExtensions = el.form.elements["allowedext"] ? el.form.elements["allowedext"].value.split(",") : ""
	
	el.parentNode.insertBefore(canvas, el);

	var browse = document.createElement("div")
	browse.className = "attachment_browse"
	
	canvas.appendChild(browse)
	browse.appendChild(el)

	var attacher = document.createElement("input")
	attacher.type = "button"
	attacher.value = "Attach"
	attacher.className = "attach_button"
	 
	var attachmentList = document.createElement("ul")	
	attachmentList.className = "attachment_list"
	
	
	attacher.fileInput = el
	attacher.attachmentList = attachmentList
	
	attacher.postName = el.name.replace(/\[.*\]/, "");
	
	//populate it with existing files, if there are any
	var existingList = document.getElementById(attacher.postName + "_existinglist")
	if (existingList){
		while (existingList.getElementsByTagName("li").length){
			attachmentList.appendChild(existingList.getElementsByTagName("li")[0])
		}
		existingList.parentNode.removeChild(existingList)
	}

	
	browse.appendChild(attacher)
	canvas.appendChild(attachmentList)
	
	attacher.onclick = function()
	{
		var file = this.fileInput
		flagValidFile = false
		if (file.allowedExtensions){
			//validate the file
			for (var i=0; i<file.allowedExtensions.length; i++){
				if (file.allowedExtensions[i].trim() == file.value.fileExt()){
					flagValidFile = true
					break
				}
			}
		}		
		//alert("javascript validation temporarily disabled");	flagValidFile = true
		if (flagValidFile){
			file.style.display = "none"
			form = file.form
			var li = document.createElement("li")
			var deleter = "<input name='attachdel_" + file.name + "' type='checkbox' value='" + file.value.filename() + "[]' />Delete";
			li.innerHTML = file.value.filename() + deleter
			li.className = "file " + file.value.fileExt()
		}
		else{
			alert("Error: " + file.value.fileExt() + " files are not allowed.")
			
		}
		var newFile = document.createElement("input")
		newFile.name = attacher.postName + "[" + (attacher.attachmentList.getElementsByTagName("li").length + 1) + "]"
		newFile.type = "file"
		newFile.className = "file_attacher"
		newFile.allowedExtensions = file.allowedExtensions
		this.fileInput.parentNode.insertBefore(newFile, this.fileInput)
		if (!flagValidFile){
			file.parentNode.removeChild(file)
		}
		this.fileInput = newFile
		this.attachmentList.appendChild(li)
		//alert(form.innerHTML)
		for (var i=0; i<form.elements.length; i++)
		{
			//alert (form.elements[i].name)
		}
	}
}

function fileAttacherInit(){
	for (var i=0, els=document.getElementsByClassName("file_attacher"); i<els.length; i++){
		fileAttacher(els[i])
	}
}

addLoadEvent(fileAttacherInit)
	
								
