/**
 * Convert a single file-input element into a 'multiple' input list
 *
 * Usage:
 *
 *   1. Create a file input element (no name)
 *      eg. <input type="file" id="first_file_element">
 *
 *   2. Create a DIV for the output to be written to
 *      eg. <div id="files_list"></div>
 *
 *   3. Instantiate a MultiSelector object, passing in the DIV and an (optional) maximum number of files
 *      eg. var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
 *
 *   4. Add the first element
 *      eg. multi_selector.addElement( document.getElementById( 'first_file_element' ) );
 *
 *   5. That's it.
 *
 *   You might (will) want to play around with the addListRow() method to make the output prettier.
 *
 *   You might also want to change the line 
 *       element.name = 'file_' + this.count;
 *   ...to a naming convention that makes more sense to you.
 * 
 * Licence:
 *   Use this however/wherever you like, just don't blame me if it breaks anything.
 *
 * Credit:
 *   If you're nice, you'll leave this bit:
 *  
 *   Class 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'
 *
 * Mobilefish.com modifications:
 * MultiSelector(file_list, max number of files, mode, upload box size, textbox)
 *	list_target 	= file_list
 *  max 			= Specify the max number of files that can be uploaded 
 *  mode 			= Add your own variations of this tool:
 *						1 - [Default]
 *						2 - This mode is specifically used when uploading jad and jar files
 *						3 - This mode is specifically used by jadmaker/wapupload
 *  size 			= The length of the upload text field, next to Browse button
 *  textbox 		= The length of the text field, next to the Remove button
 *  uploadfunction 	= Call a javascript function, AFTER you upload a file
 *
 * Always define function multifileRemove()
 * This function will be called if you press the remove button
 * You determine which actions you want.
 *					
 */
function MultiSelector( list_target, max, mode, size, textbox, uploadfunction, removefunction ){
	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = -1;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};	
	
	this.file= new Array();
	this.file[0]="";
	
	if( mode ){
		this.mode = mode;
	} else {
		this.mode = 1;
	};	
	if( size ){
		this.size = size;
	} else {
		this.size= 20;
	};		
	if( textbox ){
		this.textbox = textbox;
	} else {
		this.textbox = 130;
	};	
	if( uploadfunction){
		this.uploadfunction = uploadfunction;
	} else {
		this.uploadfunction = "";
	};		
	
	/**
	 * Add a new file input element
	 */
	this.addElement = function( element ){

		// Make sure it's a file input element
		if( element.tagName == 'INPUT' && element.type == 'file' ){

			// Element name -- what number am I?
			//element.name = 'file_' + this.id++;
			this.count++;
			element.name = 'file_' + this.count;
			
			// Execute my own defined function	
			if( this.uploadfunction != ""){
				eval(this.uploadfunction);
				element.setAttribute('onclick',this.uploadfunction);
			}	

			// Add reference to this object
			element.multi_selector = this;
			
			// What to do when a file is selected
			element.onchange = function(){

				// New file input
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';
				new_element.size = this.size;

				//Assign id
				new_element.id = 'inputfile';
				
				// Add new element
				this.parentNode.insertBefore( new_element, this );

				// Apply 'update' to element
				this.multi_selector.addElement( new_element );

				// Update list				
				this.name = "file_" + element.multi_selector.count;
				this.multi_selector.addListRow( this, new_element );

				// Hide this: we can't use display:none because Safari doesn't like it
				this.style.position = 'absolute';
				this.style.left = '-1000px';

			}; // element.onchange
			
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};
			
			// Most recent element
			this.current_element = element;
			
		} else {
			// This can only be applied to file input elements!
			alert( 'Error: not a file input element' );
		}; //if( element.tagName == 'INPUT' && element.type == 'file' ) 

	}; // // this.addElement

	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function(element, new_element){
		// Row div
		var new_row = document.createElement( 'div' );

		// Delete button
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'Remove';

		// References
		new_row.element = element;

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.removeChild( this.parentNode );

			// Decrement counter
			this.parentNode.element.multi_selector.count--;

			// Re-enable input element (if it's disabled)
			this.parentNode.element.multi_selector.current_element.disabled = false;
			
			multifileRemove();
			
			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			return false;
		}; // new_row_button.onclick

		// Set row value
		if (this.mode == 1) {
			new_row.innerHTML = element.value;
		}		

		// This mode is specifically used when uploading jad and jar files
		if (this.mode == 2) {	
			new_row.innerHTML = "<div style=\'width:" + this.textbox +"px; float: left;line-height:20px\'>" + getUploadFileName(element.value) + "</div>";		
				
			mname = this.file[this.count-1];
			isJad = false;
			isJar = false;
			if (mname != null && mname.lastIndexOf(".jad")>0) {
				isJad = true;
			}
			if (mname != null && mname.lastIndexOf(".jar")>0) {
				isJar = true;
			}
							
			if(element.value.lastIndexOf(".jad")>0 && !isJar) {
				if (isJad ) {
					alert("This is not correct!\nYou must upload the accompanied .JAR file.\n\nPlease remove the file you just uploaded and try again.");
				} else {
					alert("The next file you should upload is the accompanied .JAR file.\n\nNote:\nWhen the .JAD and .JAR files are uploaded, the MIDlet-Jar-URL field inside the .JAD file will automaticaly modified containing the actual .JAR file location and file name.\n\n ")
				}	
			} else if(element.value.lastIndexOf(".jar")>0 && !isJad) {
				if (isJar) {
   					alert("This is not correct!\nYou must upload the accompanied .JAD file.\n\nPlease remove the file you just uploaded and try again.");
				} else {
					alert("The next file you should upload is the accompanied .JAD file.\n\nNote:\nWhen the .JAD and .JAR files are uploaded, the MIDlet-Jar-URL field inside the .JAD file will automaticaly modified containing the actual .JAR file location and file name.\n\n ")
				}	
			} else {
				new_element.disabled = true;
			} 				
		}			
		
		// This mode is specifically used by jadmaker/wapupload
		if (this.mode == 3) {	
			new_row.innerHTML = "<div style=\'width:" + this.textbox +"px; float: left;line-height:20px\'>" + getUploadFileName(element.value) + "</div>";		
		}			
		
					
		this.file[this.count] = element.value;

		
		// Add button
		new_row.appendChild( new_row_button );

		// Add it to the list
		this.list_target.appendChild( new_row );
	
	}; // this.addListRow 

};