// Multiple file selector by Stickman -- http://www.the-stickman.com 
// with thanks to: [for Safari fixes] Luis Torrefranca -- http://www.law.pitt.edu and Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com [for duplicate name bug] 'neal'
function MultiSelector( list_target, max ){
	this.list_target = list_target;
	this.count = 0;
	this.id = 0;
	if( max ){
		this.max = max;
	} 
	else {
		this.max = -1;
	};
	this.addElement = function( element ){
		if( element.tagName == 'INPUT' && element.type == 'file' ){
			//element.name = 'file_' + this.id++;
			element.size = '35';
			element.style.width = '200px'
			element.multi_selector = this;
			
			if (this.value.split('.').reverse()[0] == 'doc' || this.value.split('.').reverse()[0] == 'pdf'){ 
					var new_element = document.createElement( 'input' );
					new_element.type = 'file';
					//new_element.value = this.value;
					this.parentNode.insertBefore( new_element, this );
					this.multi_selector.addElement( new_element );
					this.multi_selector.addListRow( this );
					//this.style.position = 'absolute';
					this.style.left = '-1000px';
					this.style.width = '200px'
				
			}
			else{
				alert("L'extension du fichier selectionné doit être un document WORD ou PDF");
			}
					
			
			if(this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};
			this.count++;
			this.current_element = element;
		} 
		else {
			alert( 'Error: not a file input element' );
		};
		
	};
	this.addListRow = function( element ){
		
			var new_row = document.createElement( 'div' );
			new_row.style.width = '315px';
			var new_row2 = document.createElement( 'div' );
			new_row2.style.width = '200px';
			new_row2.style.cssFloat = 'left';
			var new_row_button = document.createElement( 'input' );
			new_row_button.type = 'button';
			new_row_button.value = 'Delete';
			new_row_button.style.cssFloat = 'right';
			new_row.element = element;
			new_row_button.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;return false;
			};
			//new_row.innerHTML = element.value.split('\\').reverse()[0];
			new_row2.innerHTML = element.value.split('\\').reverse()[0];
			new_row.appendChild(new_row2);
			new_row.appendChild( new_row_button );
			var both = document.createElement( 'div' );
			both.style.clear = 'both';
			//new_row.appendChild( both );
			//var script = document.createElement( 'script' );
			
			//new_row.appendChild( script );
			this.list_target.appendChild( new_row );
			
		
		
		
	};
};
