//- 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;
function openStatusWindow(f1)
{ 
 var cgi_url = f1.action.split('upload.cgi')[0]+'upload_status.cgi';

// if(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=320,height=240,resizable=1');
//   win1.window.focus();
// }
// if(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;
// }
}

function generateSID(f1)
{
 UID='';
 for(var i=0;i<12;i++)UID+=''+Math.floor(Math.random() * 10);
 f1.action = f1.action.split('?')[0]+'?upload_id='+UID+'&js_on=1';
}

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("Extension not allowed for file: \"" + value + "\"\nOnly these extensions are allowed: "+ext_allowed.replace(/\|/g,',')+" \n\n");
        return false;
    }
    return true;
}

function addUploadSlot()
{
  cx++;
  document.getElementById('slots').insertAdjacentHTML("afterEnd", 'Send file: <input name="userfile'+cx+'" type="file" class="uploadform" onChange="checkExt(this)"><br>');
}

function fixLength(str)
{
 if(str.length<show_fname_chars)return str;
// return '...'+str.substring(str.length-show_fname_chars-1,str.length);

 return show_fname;


}


function MultiSelector( list_target, max_files, max_size )

{
	this.list_target = document.getElementById(list_target);
	this.count = 0;
	this.id = 0;
	if( max_files ){
		this.max = max_files;
	} else {
		this.max = -1;
	};

// START NOEL WAS AAN
//	document.getElementById('x_max_files').innerHTML = max_files;
//    document.getElementById('x_max_size').innerHTML = " ("+max_size+" Mb total)";
// END NOEL WAS AAN
	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 = 'Delete';

		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 );

		this.list_target.appendChild( new_row );
		
	};

};