errorMsg= "Wrong file extention.Supported formats are .jpg,.jpeg,.gif,.png";
var globalCount = 0; // count used in setting num_files

function MultiSelector( list_target, max ){

	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};

	/**
	 * 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++;

           // 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';

				// Add new element
				this.parentNode.insertBefore( new_element, this );

				// 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.position = 'absolute';
				this.style.left = '-1000px';

			};
			// 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++;
			// 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 ){
        refreshErrorMessages();
		var imageAddedPath=element.value; // getting the complete path of the file
		var imgExt=imageAddedPath.substring(imageAddedPath.lastIndexOf(".")+1,imageAddedPath.length).toLowerCase();		 
       //alert(imageAddedPath);
	   
	   if (imgExt=="gif" || imgExt=="jpeg" || imgExt=="png" || imgExt=="JPG" || imgExt=="jpg" || imgExt=="JPEG"  )
		 {
		// Row div
		var newlistRow = document.createElement( 'div' );
		newlistRow.className = 'divrow';
		newlistRow.setAttribute("id","divShow_"+this.id);
      // Adding the Remove Image Link
	   
/*	   	var extraSpan= document.createElement('span');
		extraSpan.className = 'extraspan';
		extraSpan.setAttribute("id", "extraSpan_"+ this.id );
*/		
		var removeSpan = document.createElement( 'span' ); // span tag for remove Link
		removeSpan.className = 'removespan';
		removeSpan.setAttribute("id", "linkRemove_"+ this.id);
		var removeLink = document.createElement( 'a' );
		removeLink.setAttribute("id", "anchorRemove_"+ this.id);
		removeLink.setAttribute("href","javascript:void(0);"); // For hiding the URL passed in the window status bar
		removeLink.className = 'removelink';
		var remText = document.createTextNode("Remove");			
		removeLink.appendChild(remText);
		//removeSpan.appendChild(removeLink);	

	   
	   
	   
/*		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'Delete';*/

		// References
		newlistRow.element = element;

	    // Delete function to delete the Images selected
		// Delete function
		removeLink.onclick= function(){
			
/*            remId= this.id;
			remId= remId.split("anchorRemove_");
			
			var d = document.getElementById('files_list');
			var olddiv = document.getElementById("divShow_"+remId[1]);
			d.removeChild(olddiv);
*/			
			// 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;

			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			//this.count--;
			document.getElementById('clientsideError').style.display = 'none';

			//this.id--;

			return false;
		};
		// Set row value
		//newlistRow.innerHTML = element.value;
		
		    //Displaying the image name
	var firstpos,lastpos;
	var imageSpan = document.createElement( 'span' ); //  span tag for Image
	imageSpan.className = 'imagespan';
	imageSpan.appendChild(getImg()); // appending the bullet image
	// Extracting image name from the complete path URL
	if (imageAddedPath.lastIndexOf("/") > -1){
		firstpos = imageAddedPath.lastIndexOf('/')+1;
		lastpos = imageAddedPath.length;
	}
	else{
		firstpos = imageAddedPath.lastIndexOf('\\')+1;
		lastpos = imageAddedPath.length;
	}
	var imageName = document.createTextNode(imageAddedPath.substring(firstpos,lastpos));
	imageSpan.appendChild( imageName );		 
	newlistRow.appendChild( imageSpan ); // appending Image Name


		// Add button
		newlistRow.appendChild( removeSpan ); // appending Remove Link
        newlistRow.appendChild( removeLink );
		// Add it to the list
		this.list_target.appendChild( newlistRow );
		
	   }
	  else
	   {
		refreshErrorMessages();
		var throw_error = document.createElement( 'div' );
		throw_error.setAttribute("id", "errormessage");
		throw_error.className = 'alert_box';
		var error_text = document.createTextNode(errorMsg);
		throw_error.appendChild(error_text);
		document.getElementById('clientsideError').appendChild( throw_error );	
		document.getElementById('clientsideError').style.display = 'block';
		element.name = element.id = 'badimage_'+this.id;
		this.count--;
		//this.id--;

	   }
    };

  };


// Refresh error messages
function refreshErrorMessages(){
	errormessage = document.getElementById("errormessage");
	if(errormessage) {
		errormessage.parentNode.removeChild(errormessage);
		document.getElementById('clientsideError').style.display = 'none';
	}
	//deleting the server side message if any
	document.getElementById('serversideError').style.display = 'none';
	
}
// Bullet Image
function getImg(){
	var newImg = document.createElement('img');
	newImg.src =  'images/tick.gif';			
	newImg.className = 'bullet';	
	return newImg;
}//End

