//- Fix for Netscape 6/Mozilla ------------
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
HTMLElement.prototype.insertAdjacentElement = function
(where,parsedNode)
{
switch (where){
case 'beforeBegin':
this.parentNode.insertBefore(parsedNode,this)
break;
case 'afterBegin':
this.insertBefore(parsedNode,this.firstChild);
break;
case 'beforeEnd':
this.appendChild(parsedNode);
break;
case 'afterEnd':
if (this.nextSibling) 
this.parentNode.insertBefore(parsedNode,this.nextSibling);
else this.parentNode.appendChild(parsedNode);
break;
}
}

HTMLElement.prototype.insertAdjacentHTML = function
(where,htmlStr)
{
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var parsedHTML = r.createContextualFragment(htmlStr);
this.insertAdjacentElement(where,parsedHTML)
}
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

//---------------------

var UID,NF=0,cx=0,slots=1;
function openStatusWindow(f1)
{ 
 var cgi_url = f1.action.split('upload.cgi')[0]+'upload_status.cgi';

 if(f1.popup && f1.popup.checked == true)
 {
   win1 = window.open(cgi_url+'?upload_id='+UID+'&num_files='+NF+'&css_name='+f1.css_name.value+'&tmpl_name='+f1.tmpl_name.value,'win1','width=390,height=320,resizable=1,status=0');
   win1.window.focus();
 }
 if(f1.inline && f1.inline.checked == true)
 {
   ss=getScrollXY();
   h1=document.body.scrollHeight;
   h2=document.body.parentNode.scrollHeight;
   if(h1>h2){hx=h1;} else {hx=h2;}

   w1=document.body.scrollWidth;
   w2=document.body.parentNode.scrollWidth;
   if(w1>w2){wx=w1;} else {wx=w2;}

   document.getElementById('div_inline').style.width = wx;
   document.getElementById('div_inline').style.height = hx;
   document.getElementById('div_inline').style.paddingTop=ss[1]+50;

   document.getElementById('div_inline').style.visibility='visible';
   self.transfer.document.location = cgi_url+'?inline=1&upload_id='+UID+'&num_files='+NF+'&css_name='+f1.css_name.value+'&tmpl_name='+f1.tmpl_name.value;
 }
 if(f1.inline2 && f1.inline2.checked == true)
 {
   document.getElementById('div1').style.display='none';
   document.getElementById('progress2').style.display='block';
   self.transfer2.document.location = cgi_url+'?inline=1&upload_id='+UID+'&num_files='+NF+'&css_name='+f1.css_name.value+'&tmpl_name='+f1.tmpl_name.value;
 }
}

function generateSID(f1)
{
 UID='';
 for(var i=0;i<12;i++)UID+=''+Math.floor(Math.random() * 10);
 var password='';
 if(f1.upload_password)password=f1.upload_password.value;
 f1.action = f1.action.split('?')[0]+'?upload_id='+UID+'&js_on=1&ref='+document.location+'&upload_password='+password;
}

function StartUpload(f1)
{
    NF=0;
    f1.target='upload';

    // Check that external folder don't contain '..'
    if(f1.ext_folder && f1.ext_folder.value.indexOf('..')!=-1){alert("Invalid upload folder!");return false;}

    for (var i=0;i<f1.length;i++)
    {
     current = f1.elements[i];
     if(current.type=='file' && current.value!='')
      {
         if(!checkExt(current))return false;
         NF++;
      }
    }
    if(NF==0){alert('Select at least one file to upload');return false;};
    generateSID(f1);
    openStatusWindow(f1);
}

function checkExt(obj)
{
    value = obj.value;
    if(value=="")return true;
    var re = new RegExp("^.+\.("+ext_allowed+")$","i");
    if(!re.test(value))
    {
        alert("Dateiendung nicht erlaubt für Datei: \"" + value + "\"\n Folgende Dateiendungen sind erlaubt: "+ext_allowed.replace(/\|/g,',')+" \n\n");
        return false;
    }
    return true;
}

function addUploadSlot()
{
  cx++;
  slots++;
  if(slots==max_upload_files){document.getElementById('x_add_slot').style.visibility='hidden';}

  var new_slot = document.createElement( 'input' );
  new_slot.type = 'file';
  new_slot.name = 'file_'+cx;
  document.getElementById('slots').appendChild(new_slot);
}

function fixLength(str)
{
 if(str.length<show_fname_chars)return str;
 return '...'+str.substring(str.length-show_fname_chars-1,str.length);
}


function MultiSelector( list_target, max_files, max_size, descr_mode )
{
	this.list_target = document.getElementById(list_target);
	this.count = 0;
	this.id = 0;
	if( max_files ){
		this.max = max_files;
	} else {
		this.max = -1;
	};
	document.getElementById('x_max_files').innerHTML = max_files;
    document.getElementById('x_max_size').innerHTML = max_size;
	this.addElement = function( element )
    {
		if( element.tagName == 'INPUT' && element.type == 'file' )
        {
			element.name = 'file_' + this.id++;
			element.multi_selector = this;
			element.onchange = function()
            {
                if(!checkExt(element))return;
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';
				this.parentNode.insertBefore( new_element, this );
				this.multi_selector.addElement( new_element );
				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;};

			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 = 'Löschen';

		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.appendChild( new_row_button );

		currenttext=document.createTextNode(" "+fixLength(element.value));
        new_row.appendChild( currenttext );

        if(descr_mode)
        {
            var new_row_descr = document.createElement( 'input' );
    		new_row_descr.type = 'text';
    		new_row_descr.value = '';
            new_row_descr.name = element.name+'_descr';
            new_row_descr.className='fdescr';
            new_row.appendChild( document.createElement('br') );
            new_row.appendChild( document.createTextNode('Beschreibung:') );
            new_row.appendChild( new_row_descr );
        }
        

		this.list_target.appendChild( new_row );
		
	};

};


//--Initialization------
document.write('<div id="div_inline" style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%; background: url(trans_bg.gif); text-align: center; padding-top: 50px; visibility: hidden;"><iframe src="about:blank" name="transfer" border=0 SCROLLING=NO topmargin=0 leftmargin=0 frameborder=0 style="width: 390px; height: 320px; border: 1px solid #a3a3a3;"></iframe></div>');
var show_fname_chars=50;
if(document.getElementById('files_list'))
{
    var multi_selector = new MultiSelector( 'files_list', max_upload_files, max_upload_size, descr_mode );
    multi_selector.addElement( document.getElementById( 'my_file_element' ) );
}
