function fileObject(fileObj){
	this.tgt = fileObj;
	this.prnt = tgt.parentNode;
	this.fullFileName = fileObj.value.toLowerCase();
	var fne = -(fullFileName.lastIndexOf("\\") *  fullFileName.lastIndexOf("/"))+1;
	this.fileName = fullFileName.slice(fne);
	var eb = fileName.lastIndexOf(".")+1;
	this.fileExtension = fileName.slice(eb);
	this.filesAllowed= tgt.accept.toLowerCase().split("|");
	this.hide = document.getElementById(hideContainer);
	this.list = document.getElementById(listContainer);
	fileCheck = true;
	if(filesAllowed[0]!="") fileCheck = checkFile();
	switch(fileCheck){
		case 0:
			moveObject();
			createObject();
			createText();
		if(textArea!=""){
			addToText("[image:"+fileName+"]");
		}
		break;
		case 1:
			fa = "";
			for(i=0; i<filesAllowed.length; i++){
				fa += "*."+filesAllowed[i];
				if(i == filesAllowed.length - 1){fa += " ";}else{fa += "; ";}
			}
			alert('This file is not allowed.\nOnly ( ' + fa +') files are allowed');
		break;
		case 2:
			alert('File: ' +fullFileName+ ' is in the list.');
		break;
	}
};
function addToText(textToAdd){
ta = document.getElementById(textArea);
if (document.selection) {
    sel.text = textToAdd;
  }
  else if (ta.selectionStart) {
    var startPos = ta.selectionStart;
    var endPos = ta.selectionEnd;
    ta.value = ta.value.substr(0, startPos)  + textToAdd + ta.value.substr(endPos);
  } else {
    ta.value += textToAdd;
  }
};
function checkFile(){
	rtrn = 1;	
	for(i=0; i<filesAllowed.length; i++){
		if(filesAllowed[i] == fileExtension){
			rtrn = 0;
			break;
		}
	}
	if(rtrn == 0){
		lst = list.getElementsByTagName('span');
		for(j=0; j<lst.length; j++){
			if(lst[j].getAttribute('full_file') == fullFileName){
				rtrn=2;
				break;
			}	
		}
	}
	return rtrn;	
};
function moveObject(){
	tgt.setAttribute('name', 'toUpload[]');
	tgt.setAttribute('onchange', '');
	hide.appendChild(tgt);
};

function createObject(){
	fileNode = document.createElement("input");
	fileNode.setAttribute('type', 'file');
	fileNode.onchange = function(){fileObject(this);};
	fileNode.setAttribute('name', 'file_tmp');
	fileNode.setAttribute('accept', tgt.accept);
	prnt.appendChild(fileNode);
}
function createText(){
	div = document.createElement("span");
	div.setAttribute('full_file', fullFileName);
	div.setAttribute('file_name', fileName);
	a_delete = document.createElement("a");
	a_delete.setAttribute("href", "javascript:void(0);");
	a_delete.onclick = deleteFile;
	di = document.createElement("img");
	di.setAttribute("src", "images/delete.gif");
	di.setAttribute("alt", "delete");
	a_delete.appendChild(di);
	ii = document.createElement("img");
	ii.setAttribute("src", "images/insert.gif");
	ii.setAttribute("alt", "insert");
	/*a_edit = document.createElement("a");
	a_edit.appendChild(ii);
	a_edit.setAttribute("href", "javascript:void(0);");
	a_edit.onclick = addImageFile;
	div.appendChild(a_edit);*/
	div.appendChild(document.createTextNode(fileName+" "));
	div.appendChild(a_delete);
	list.appendChild(div);
};
function deleteFile(e){
	if (!e) var e = window.event;
	var tgt = e.target ? e.target : e.srcElement;
	deleteObject =tgt.parentNode.parentNode;
	prnt = deleteObject.parentNode;
	file_name = deleteObject.getAttribute('full_file');
	fN = deleteObject.getAttribute('file_name');
	fileFields = document.getElementById(hideContainer).getElementsByTagName('input');
	for(i=0; i<fileFields.length; i++){
		if(fileFields[i].value.toLowerCase() == file_name){
			document.getElementById(hideContainer).removeChild(fileFields[i]);
			break;
		}	
	}
	prnt.removeChild(deleteObject);
if(textArea!=""){
	str = "[image:"+fN+"]";
	txt = document.getElementById(textArea);
	while(txt.value.indexOf(str)!=-1){
		txt.value = txt.value.replace(str, "");

	}
}

};
function addImageFile(e){
	if (!e) var e = window.event;
	var tgt = e.target ? e.target : e.srcElement;
	Obj =tgt.parentNode.parentNode;
	fN = Obj.getAttribute('file_name');

	if(textArea!=""){addToText("[image:"+fN+"]");}
}
function createSelection(){if(document.selection){sel = document.selection.createRange();}}

fileObject.prototype.checkFile	= checkFile;
fileObject.prototype.createText	= createText;
fileObject.prototype.createText	= createObject;
fileObject.prototype.moveObject	= moveObject;