 /***************************************************************************
 Jupiter Portal @ Jupiterportal.com
 Copyright (C) 2006 Cosmin Flavius (highstrike@gmail.com)

 Attribution-NonCommercial-ShareAlike 2.5 of Creative Commons
 (http://creativecommons.org)

 You are free:

    * to copy, distribute, display, and perform the work
    * to make derivative works

 Under the following conditions:

 Attribution - You must attribute the work in the manner specified
 by the author or licensor.

 Noncommercial - You may not use this work for commercial purposes.

 Share Alike - If you alter, transform, or build upon this work, you may
 distribute the resulting work only under a license identical to this one.

    * For any reuse or distribution, you must make
	  clear to others the license terms of this work.
    * Any of these conditions can be waived if you get
      permission from the copyright holder.

 Your fair use and other rights are in no way affected by the above.

 This is a human-readable summary of the Legal Code (the full license).
 (http://creativecommons.org/licenses/by-nc-sa/2.5/legalcode)

****************************************************************************/

////////////////////////////////////////////////////////
// Function:         disableme
// Description: Disables a button and gives a specified value

function disableme (what, text)
{
	what = document.getElementById(what);
	what.disabled = true;
	what.value= text;

	return true;
}

////////////////////////////////////////////////////////
// Function:         MultiSelector
// Description: Start Multiselector main Function

function MultiSelector(list_target, max, denied_types)
{
	//-----------------------------------------------------------------
	//  Take care of vars
	//-----------------------------------------------------------------

	this.list_target = list_target;
	this.count = 0;
	this.id = 0;
	this.max = max;
	this.selectedFiles = new Array();
	this.denied_types = denied_types;

	////////////////////////////////////////////////////////
	// Function:         addElement
	// Description: Adds the element into the list

	this.addElement = function( element )
	{
		if( element.tagName == 'INPUT' && element.type == 'file' )
		{
			element.name = 'file_' + this.id++;
			element.className = 'box';
			element.size = "40";
			element.multi_selector = this;

			element.onchange = function()
			{
				var filename = element.value.split('\\').pop();
				var extension = element.value.split('.').pop();

				for(var i = 0; i < multi_selector.denied_types.length; ++i)
				{
					if(multi_selector.denied_types[i] == extension.toLowerCase())
					{
						alert('You are not allowed to upload files with a .'+ extension +' extension.');
						element.value = '';
						return false;
					}
				}

				for(var i = 0; i < multi_selector.selectedFiles.length; ++i)
				{
					if(multi_selector.selectedFiles[i].path == element.value)
					{
						alert('You have already selected '+ filename);
						element.value = '';
						return false;
					}
				}

				var new_element = document.createElement( 'input' );
				new_element.type = 'file';

				this.parentNode.insertBefore( new_element, this );
				this.multi_selector.addElement( new_element );
				this.multi_selector.addListRow( this );
				multi_selector.selectedFiles.push({path: element.value});

				this.style.position = 'absolute';
				this.style.left = '-1000px';
			};

			if(this.count >= this.max || this.max == 0)
			{
				element.disabled = true;
				element.style.display='none';
			};

			this.count++;
			this.current_element = element;
		}
		else { alert( 'Error: not a file input element' ); };
	};

	////////////////////////////////////////////////////////
	// Function:         addListRow
	// Description: Adds the files into our list

	this.addListRow = function( element )
	{
		var new_row = document.createElement('div');
		var leftSpan = document.createElement('span');
		var rightSpan = document.createElement('span');

		leftSpan.className = 'mselect-left';
		leftSpan.innerHTML = element.value.split('\\').pop();
		rightSpan.className = 'mselect-right';
		rightSpan.innerHTML = '';

		new_row.element = element;
		rightSpan.onclick= function()
		{
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );
			this.parentNode.parentNode.removeChild( this.parentNode );
			this.parentNode.element.multi_selector.count--;
			this.parentNode.element.multi_selector.current_element.disabled = false;
			this.parentNode.element.multi_selector.current_element.style.display='inline';

			return false;
		};

		new_row.appendChild(leftSpan);
		new_row.appendChild(rightSpan);

		this.list_target.appendChild(new_row);
	};
};