// 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;
	var flag=0;
	if( max ){
		this.max = max;
	} 
	else {
		this.max = -1;
	};
	this.addElement = function( element ){
		flag=0;
		if( element.tagName == 'INPUT' && element.type == 'file' ){
			element.id = 'image_data'+ this.id;
			element.name = 'file_' + this.id++;
			element.multi_selector = this;
			if(chkImage(this.id)==false){
			 	flag=1;
			 }
			element.onchange = function(){
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';
				this.parentNode.insertBefore( new_element, this );
				this.multi_selector.addElement( new_element );
				if(flag==0)
					this.multi_selector.addListRow( this );
				this.style.position = 'absolute';
				this.style.left = '-1000px';
			};
			if(flag==1)
				document.getElementById("fileCount").value=eval(document.getElementById("fileCount").value)+1;
			
			var fileCount=document.getElementById("fileCount").value;
			if( this.max+eval(fileCount) != -1 && this.count >= this.max+eval(fileCount) ){
				document.getElementById("image_browse").style.display = 'none';
				//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' );
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'Delete';
		new_row.element = element;
		new_row_button.onclick= function(){
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );
			this.parentNode.parentNode.removeChild( this.parentNode );
			delImage( this.parentNode.element);
			this.parentNode.element.multi_selector.count--;
			document.getElementById("image_browse").style.display = 'block';
//			this.parentNode.element.multi_selector.current_element.disabled = false;
			return false;
		};
		new_row.innerHTML = element.value;
		new_row.appendChild( new_row_button );
		this.list_target.appendChild( new_row );
	};
};


function chkImage(count){

	
	if (count>1){	
 		var imagedata=document.getElementById("image_data"+(count-2)).value;
	}else{
		return true;	
	}
 
	var tempimages=document.getElementById("tempimages").value;
	temp=imagedata.split('/');
	if(tempimages!='/' && tempimages!='')
		temp2=tempimages.split('/');
	else
	temp2='';
	
	for(i=0;i<temp2.length;i++){
		if (temp2[i]==temp[temp.length-1]){
			alert(temp2[i]+" is  already added in the list");
			return false;
		}
	}
	
	if (tempimages==null || tempimages=='/'){
		document.getElementById("tempimages").value=temp[temp.length-1];
	}else{
		document.getElementById("tempimages").value=tempimages+'/'+temp[temp.length-1];
	}
	
	return true;
}


function delImage(parentelement){
	imagedata=parentelement.value;
	temp=imagedata.split('/');
	repstr=temp[temp.length-1];
	var tempimages=document.getElementById("tempimages").value;
	document.getElementById("tempimages").value=replaceAll(tempimages,repstr,'');
}


function replaceAll(OldString,FindString,ReplaceString) {
   var SearchIndex = 0;
   var NewString = "";
   while (OldString.indexOf(FindString,SearchIndex) != -1) {
   NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
   NewString += ReplaceString;
   SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length);
   }
   NewString += OldString.substring(SearchIndex,OldString.length);
   return NewString;
}
