/*********************************
 * Add file to upload list
 *********************************/
function MultipleFileUpload(labelfield, hiddenfield, maximum){
	this.labels 	= document.getElementById(labelfield);
	this.files 		= document.getElementById(hiddenfield);
	this.counter 	= 1;
	this.fileid 	= 1;

	if (maximum){
		this.maxfiles = maximum;
	}else{
		this.maxfiles = 1;
	}

	this.init = function(element){
		element.ref = this;

		element.onchange = function(){
			if (this.ref.counter <= this.ref.maxfiles){
				var text 		= document.createTextNode(this.value);
				var labeltext 	= 'flabel_'+ (this.ref.fileid-1);
				var inputtext 	= 'file_'+ this.ref.fileid;

				var newinput = document.createElement("input");
				newinput.setAttribute('type','file');
				newinput.setAttribute('name','media[]');
				newinput.setAttribute('id', inputtext);
				this.ref.files.insertBefore(newinput,this);
				this.ref.init(newinput);

				this.style.display	= 'none';
				this.style.left 	= '-5000px';
				this.style.position = 'absolute';

				var delinput = document.createElement("input");
				delinput.setAttribute('type','button');
				delinput.setAttribute('value','L' + unescape("%F6") + 'schen');
				delinput.ref = this.ref;
				delinput.fileid = this.ref.fileid-1;

				delinput.onclick = function(){
					var dlabel = document.getElementById('flabel_'+ this.fileid);
					dlabel.parentNode.removeChild(dlabel);
					var dinput = document.getElementById('file_'+ this.fileid);
					dinput.parentNode.removeChild(dinput);

					this.ref.counter--;

					if (this.ref.counter <= 1){
						document.getElementById('uploadButton').value = "-----";
						document.getElementById('uploadButton').disabled = true;
					}else{
						document.getElementById('uploadButton').value = "Hochladen";
						document.getElementById('uploadButton').disabled = false;
					}

					var start = 1;

					for (var i = 0; i < this.ref.fileid-1; i++){
						if (document.getElementById('flabel_' + i)){
							var tmp = document.getElementById('flabel_' + i);
							tmp.getElementsByTagName("b")[0].firstChild.data = start;
							start++;
						}
					}

					return false;
				};
				var container = createTable(labeltext, this.ref.counter, text, delinput);

				this.ref.labels.appendChild(container);
				this.ref.counter++;
				this.ref.fileid++;

				if (this.ref.counter <= 1){
					document.getElementById('uploadButton').value = "-----";
					document.getElementById('uploadButton').disabled = true;
				}else{
					document.getElementById('uploadButton').value = "Hochladen";
					document.getElementById('uploadButton').disabled = false;
				}
			}
		};
	};
};

/*********************************
 * Used in MultipleFileUpload
 *********************************/
function createTable(table_id, rownum, text, button){
	var table 	= document.createElement("table");
	/*
	DOM von IE benoetigt tbody als direkt
	dem table untergeordnetetes Element
	*/
	var tbody 	= document.createElement("tbody");
	var tr 		= document.createElement("tr");
	var td1		= document.createElement("td");
	var td2		= document.createElement("td");
	var td3		= document.createElement("td");
	var b		= document.createElement("b");

	var row 	= document.createTextNode(rownum);
	b.appendChild(row);
	td1.appendChild(b);

	tr.setAttribute('height','23');

	td1.setAttribute('align','left');
	td2.setAttribute('valign','middle');
	td2.setAttribute('align','left');
	td2.setAttribute('style','overflow:hidden;');
	td3.setAttribute('valign','middle');
	td3.setAttribute('align','right');

	table.setAttribute('id', table_id);

	td2.appendChild(text);
	td3.appendChild(button);
	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);
	tbody.appendChild(tr);
	table.appendChild(tbody);
	return table;
}

function getRandom( min, max ){
    if( min > max ) {
         return( -1 );
    }
    if( min == max ) {
         return( min );
   }

   return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

function trim(tmp){
	if (tmp != null || tmp != ""){
	  while(tmp.substring(0,1)==' ')
	    tmp = tmp.substring(1,tmp.length);
	  while(tmp.substring(tmp.length-1,tmp.length)==' ')
	    tmp = tmp.substring(0,tmp.length-1);
	  return tmp;
	}
	return "";
}

function switchDiv(outdiv, indiv)
{
	document.getElementById(outdiv).style.display = "none";
	document.getElementById(indiv).style.display = "block";
}

