function make()
{
	var e = document.createElement( arguments[0] );
	if ( arguments.length > 1)
	{
		e.id = arguments[1];
	}
	return e;
}

function NewId()
{
	return "@@"+Number(NewId.Id++).toString(36);
}
NewId.Id = 0;

if ( Position )
{
    Position.center = function(big, small)
    {
        var wx = (big.offsetWidth-small.offsetWidth) / 2;
        var wy = (big.offsetHeight-small.offsetHeight) / 2;
        small.style.position = "absolute";
        small.style.left = wx+"px";
        small.style.top = wy+"px";
    }
}
if ( Element )
{
    Element.appear = function()
    {
        for ( var i=0;i<arguments.length; i++ )
        {
          var element = $(arguments[i]);
          if ( element )
          element.style.visibility = 'visible';
        }
    }

    Element.vanish = function()
    {
        for ( var i=0;i<arguments.length; i++ )
        {
          var element = $(arguments[i]);
          if ( element )
          element.style.visibility = 'hidden';
        }
    }

    Element.disable = function()
    {
        for ( var i=0;i<arguments.length; i++ )
        {
          var element = $(arguments[i]);
          if ( element )
            element.disabled = true;
        }
    }

    Element.enable = function()
    {
        for ( var i=0;i<arguments.length; i++ )
        {
          var element = $(arguments[i]);
          if ( element )
          element.disabled = false;
        }
    }
}

function Fileman()
{
	this.FileCount = 0;
	this.SelectedRow = null;
	this.FileList = new Object();
}

Fileman.prototype.Login = function( )
{
    var stp = Form.getInputs("uploadForm", "hidden", "step-to")[0];
    stp.value = arguments[0];
    if ( arguments[1] instanceof Function )
        arguments[1]();
    else
    if ( arguments[1] != true )
        Center();
    $("uploadForm").submit();
}

Fileman.prototype.DeleteRow = function( arg )
{
    var id = arg.id;
    var imp = this.FileList[ id ];
    imp.parentNode.removeChild( imp );
    arg.parentNode.removeChild( arg );
    delete this.FileList[ id ];
    Element.disable("DelFile");
    $("DelFile").onclick = null;
	$("AddSel").value = --this.FileCount == 0 ? "Select File" : "Add file";
	$("Sub").disabled = this.FileCount == 0;
	this.SelectedRow = null;
}

Fileman.prototype.DeleteRow2 = function( arg, id )
{
    arg.parentNode.removeChild( arg );
    delete this.FileList[ id ];
    Element.disable("DelFile");
    $("DelFile").onclick = null;
	$("AddSel").value = --this.FileCount == 0 ? "Select File" : "Add file";
	$("Sub").disabled = this.FileCount == 0;
	this.SelectedRow = null;
    var f = Form.getInputs("uploadForm", "hidden", "exclude-files")[0];
    if ( f.value != "" )
        f.value +=",";
    f.value += id;
	
}

Fileman.prototype.SelectUploaded = function( id )
{
    var fid = "ul_"+id;
    var arg = $(fid );
    var DF = $("DelFile");
    if ( this.SelectedRow == arg )
    {
        this.SelectedRow = null;
        Element.removeClassName( arg, "selected" );
        // disable remove
    	Element.disable( DF );
    	if ( DF )
    	    DF.onclick = null;
    }
    else
    {
        if ( this.SelectedRow )
            Element.removeClassName( this.SelectedRow, "selected" );
        this.SelectedRow = arg;
        Element.addClassName( this.SelectedRow, "selected" );
        // enable remove
    	Element.enable( DF );
    	if  (DF )
    	    DF.onclick = this.DeleteRow2.bind( this, arg, id );
    	}
    
}

Fileman.prototype.SelectPrinter = function( id )
{
    var fid = "ul_"+id;
    var arg = $(fid );
    if ( !arg )
        return;
    if ( this.SelectedRow == arg )
    {
        this.SelectedRow = null;
        Element.removeClassName( arg, "selected2" );
        //arg.className = "";
        // disable remove
    	Element.disable( "Print" );
    	Form.getInputs("uploadForm", "hidden", "print-on")[0].value= "";
    }
    else
    {
        if ( this.SelectedRow )
            Element.removeClassName( this.SelectedRow, "selected2" );
            //this.SelectedRow.className = "";
        this.SelectedRow = arg;
        //this.SelectedRow.className = "selected";
        Element.addClassName( this.SelectedRow, "selected2" );
        // enable remove
    	Form.getInputs("uploadForm", "hidden", "print-on")[0].value= id;
    	Element.enable( "Print" );
	}
    
}


Fileman.prototype.RowSelected = function( arg )
{
    if ( this.SelectedRow == arg )
    {
        this.SelectedRow = null;
        Element.removeClassName( arg, "selected" );
        //arg.className = "";
        // disable remove
    	Element.disable( "DelFile" );
    	$("DelFile").onclick = null;
    }
    else
    {
        if ( this.SelectedRow )
            Element.removeClassName( this.SelectedRow, "selected" );
            //this.SelectedRow.className = "";
        this.SelectedRow = arg;
        //this.SelectedRow.className = "selected";
        Element.addClassName( this.SelectedRow, "selected" );
        // enable remove
    	Element.enable( "DelFile" );
    	$("DelFile").onclick = this.DeleteRow.bind( this, arg );
        
    }
}

Fileman.prototype.FileSet = function (arg)
{
	arg.blur();
	var dvid = NewId();
	var div = make("div", dvid );
	var im = make( "img" );
	im.src = "/Images/unknown.gif";
	div.appendChild( im );
	var argValue = arg.value.substr( arg.value.lastIndexOf("\\")+1);;
	div.appendChild( document.createTextNode( argValue ) );
	div.title = arg.value;
	div.onclick = this.RowSelected.bind( this, div );
	div.onselectstart = function() {return false;};
	$("grid").appendChild( div );
	this.FileList[ dvid ] = arg;
	Element.hide( arg );
	var newI = make( "input", "fileUpload" );
	newI.type = "file";
	newI.name = "file[]";
	newI.size = 1;
    Element.addClassName( newI, "fileupload" );
    $("uploadbuttons").appendChild( newI );	
    newI.onchange = this.FileSet.bind( this, newI );
    newI.onmousedown = this.MouseUpDown.bind( this, "inset" );
    newI.onmouseup = this.MouseUpDown.bind( this, "outset" );

	/*
	var div = make("tr", dvid );
	var td = make( "td" );
	td.appendChild( document.createTextNode( arg.value ) );
	div.appendChild( td );
	td = make( "td" );
	var cross = make("input", NewId());
	cross.type = "button";
	cross.value = "x";
	td.appendChild( cross );
	cross.onclick = this.DelRow.bind( this, dvid );
	div.appendChild( td );
	div.onclick = this.RowSelected.bind( this, div );
	$("uploadFiles").appendChild( div );
	*/
	this.FileCount++;
	$("AddSel").value = "Add file";
	Element.enable( "Sub" );
	Element.disable( "DelFile" );
	//Element.appear( "DelFile" );
}

Fileman.prototype.MouseUpDown = function(arg)
{
	var Ads = $("AddSel");
	if ( !Ads.currentStyle || Ads.currentStyle.borderStyle != "both" )
	    Ads.style.borderStyle = arg;
}

function Init()
{
    ResizeContainer();
    if ( Init2 )
        Init2();
}

function ResizeContainer()
{
    it = $("Container");
    //debugger;
    var body = document.documentElement ? document.documentElement : document.body;
    /*it.style.width = document.body.clientWidth+'px';*/
    it.style.height = body.clientHeight+'px';
}

function Center()
{
    var splash = $("splash");
    if ( !splash )
        return;
    var splash2 = $("splash2");
    var hdr = $("TopHeader");
    if ( hdr )
        splash.style.top = (hdr.offsetTop + hdr.offsetHeight)+"px";
    splash.style.width = document.body.clientWidth+"px";
    var splashH =0;
    var splashH2 = 0;
    Element.appear( splash );
    if ( hdr )
    {
        splashH = (document.body.clientHeight-(hdr.offsetTop + hdr.offsetHeight));
        splash.style.height = splashH + "px";
        if ( splash2 )
        {
            splashH2 = (splashH-splash2.offsetTop );
            if ( splashH2 < 0 )
                splashH2 = 1;
            splash2.style.height = splashH2+"px";
        }
    }
    /*
    var grid = $("grid");
    if ( splash && grid )
    {
        Element.appear( splash );
        Position.center( grid, splash );
    }
    */
    return true;
}

function HideSplash()
{
    var splash = $("splash");
    if ( !splash )
        return;
    Element.vanish( splash );
}

if( !String.prototype.trim )
	String.prototype.trim = 
	function() {
		return this.replace(/^\s+|\s+$/g, "");
	};
