/**
 * upload File Tool
**/
function MultiSelector( list_target, max, mainTD )
{

	// Where to write the list
	this.list_target = list_target;
	
	// Tabellen TD
	//alert(mainTD);
	this.mainTD = mainTD;
	// How many elements?
	this.count = -1;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	this.max = (max)	?	max	:	-1;
	
	/**
	 * Add a new file input element
	 */
	this.addElement = function( element )
	{

		// Make sure it's a file input element
		//alert(element.value);
		if( element.tagName == "INPUT" && element.type == "file"){

			// Element name -- what number am I?

			// Add reference to this object
			element.multi_selector = this;
			

			// What to do when a file is selected
			element.onchange = function()
			{
				element.name = "uploads[]";

				// New file input
				var new_element = document.createElement( "input" );
				new_element.type = "file";
				new_element.name = "anyfile[]";
				//alert(new_element);

				// Add new element
				//alert(this.multi_selector.count);
				document.getElementById('mainTD').appendChild(new_element);

				// Apply 'update' to element
				this.multi_selector.addElement( new_element );

				// Update list
				this.multi_selector.addListRow( this );

				// Hide this: we can't use display:none because Safari doesn't like it
				this.style.display="none";
				//this.style.position = 'absolute';
				//this.style.left ="-1000px";
				//alert(this.style.left);

			};
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};

			// File element counter
			this.count++;
			(this.count > 0)	?	this.validSubmitButton(this.count)	:	"";
			// Most recent element
			this.current_element = element;
			
		} else {
			// This can only be applied to file input elements!
			alert( "Error: not a file input element" );
		};

	};

	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function( element )
	{

		// Row tbody (for ie..)
		var new_tbody = document.createElement("tbody");
		
		var new_row = document.createElement("TR");
		
		new_row.id="id_"+this.count;

		this.addListColumnsFileText(new_row,element);
		
		this.addListColumns(new_row,"legende[]","Name");
		
		this.addListColumns(new_row,"description[]","Beschreibung");
		
		this.addListColumnButton(new_row,element);
		
		new_tbody.appendChild(new_row);

		
		
		// References
		new_row.element = element;
		// Delete function
		this.list_target.appendChild( new_tbody );

		// Add it to the list
		
	};
	
	this.addListColumnsFileText = function (new_row,element)
	{
		new_td=document.createElement("td");
		
		new_td_span=document.createElement("span");
		
		new_td_span.className="standard";
		
		new_td.appendChild(new_td_span);
		
		var strSuchePos = element.value.lastIndexOf("\\");
		
		stringValue = element.value.substr((strSuchePos+1));
		
		//alert(element.value);
		new_td_text=document.createTextNode(stringValue);
		
		new_td_span.appendChild(new_td_text);
		
		new_row.appendChild(new_td);
		
	}
	
	this.addListColumns = function (new_row,inputName,strInp)
	{
		new_td=document.createElement("td");
		
		if (strInp)
		{
			new_td_span=document.createElement("span");
			
			new_td_span.className="standard";
			
			new_td.appendChild(new_td_span);
			
			new_td_text=document.createTextNode(strInp+":");
			
			new_td_span.appendChild(new_td_text);

			new_td_span=document.createElement("BR");
			
			new_td.appendChild(new_td_span);
			
		}
		
		new_td_input=document.createElement("input");

		new_td_input.type="text";
		
		new_td_input.name=inputName;
		
		new_td_input.style.width="80px";
		
		new_td_input.style.border = "1px solid #999999";
		
		new_td.appendChild(new_td_input);
		
		new_row.appendChild(new_td);
		
	}

	this.addListColumnButton = function (new_row,element)
	{
		new_td=document.createElement("td");
		
		new_td_input=document.createElement("input");

		new_td_input.type = "button";

		new_td_input.value = "Delete";
		
		new_td.appendChild(new_td_input);
		
		new_row.appendChild(new_td);

		new_td_input.onclick= function()
		{

			// Remove element from form
			//alert(this.parentNode.element.name);
			//alert(this.parentNode.element.multi_selector.count);
			element.parentNode.removeChild ( element );

			// Decrement counter
			//alert(element.multi_selector.count);
			element.multi_selector.count--;
			
			element.multi_selector.validSubmitButton(element.multi_selector.count);
			// Re-enable input element (if it's disabled)
			element.multi_selector.current_element.disabled = false;
			//this.parentNode.parentNode.element.parentNode.removeChild( this.parentNode.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.parentNode.removeChild ( new_row );


			return false;
		};

	}
	
	this.validSubmitButton = function(count)
	{
		//element = document.getElementById('submitFileUpload');
		
		//element.style.display = (count > 0)	?	"block"	:	"none";
	}

};
