//<script> 
/*----------------------------------------------------------------------------
    f_sort: Performs a client side sort of the contents of a HTML Table.

    args:   ao_table, The object reference to the table to be sorted.
            ai_sortcol, The zero based column number to be sorted.
            ab_header, Bool to indicate if the table have a header
                row to be ignored.

    vars:   lastcol, used to store the last column sorted.
            lastseq, used to store the sequence the last column was sorted by.
----------------------------------------------------------------------------*/
var lastcol, lastseq;

function f_cmp(type,seq,a,b) {
	var x,y;
	var bs = { " B" : 1,"KB" : 1024,"MB" : 1024*1024 , "GB" : 1024*1024*1024 };
	
	if (type == 'BS') {
		x = a;y=b;
		a = (a.length>3) ? parseInt(a.substr(0,a.length-2))*bs[a.substr(a.length-2)] : 0;
		b = (b.length>3) ? parseInt(b.substr(0,b.length-2))*bs[b.substr(b.length-2)] : 0;
		type = 'N';
		//if (!confirm(x + ":" + a + " | " + y + ":"  + b)) exit;
	}
		
	if (seq == 'A') {
		if (type == 'N') {
			cmp = (parseInt(a) > parseInt(b));
		} else if (type == 'D-M-Y') {
			cmp = ((''+a.substr(6,4)+a.substr(3,2)+a.substr(0,2)) > (''+b.substr(6,4)+b.substr(3,2)+b.substr(0,2)));
		} else if (type == 'D-M-Y+') {
			cmp = ((''+a.substr(6,4)+a.substr(3,2)+a.substr(0,2)+a.substr(11)) > (''+b.substr(6,4)+b.substr(3,2)+b.substr(0,2)+b.substr(11)));
		} else {
			cmp = (a > b);
		}
	} else {
		if (type == 'N') {
			cmp = (parseInt(a) < parseInt(b));
		} else if (type == 'D-M-Y') {
			cmp = ((''+a.substr(6,4)+a.substr(3,2)+a.substr(0,2)) < (''+b.substr(6,4)+b.substr(3,2)+b.substr(0,2)));
		} else if (type == 'D-M-Y+') {
			cmp = ((''+a.substr(6,4)+a.substr(3,2)+a.substr(0,2)+a.substr(11)) < (''+b.substr(6,4)+b.substr(3,2)+b.substr(0,2)+b.substr(11)));
		} else {
			cmp = (a < b);
		}
	}
	return cmp;
}

function f_sort( ao_table, ai_sortcol, ab_header,ai_type )
{
    var ir, ic, is, ii, id;

    ir = ao_table.rows.length;
    if( ir < 2 ) return;

    ic = ao_table.rows[1].cells.length;
    // if we have a header row, ignore the first row
    if( ab_header == true ) is=1; else is=0;
	
    // take a copy of the data to shuffle in memory
    var row_data = new Array( ir );
    ii=0;
    for( i=is; i < ir; i++ )
    {
        var col_data = new Array( ic );
        for( j=0; j < ic; j++ )
        {
            col_data[j] = ao_table.rows[i].cells[j].innerHTML;
        }
        row_data[ ii++ ] = col_data;
    }

    // sort the data
    var bswap = false,cmp;
    var row1, row2;
    
    if( ai_sortcol != lastcol )
        lastseq = 'A';
    else
    {
        if( lastseq == 'A' ) lastseq = 'D'; else lastseq = 'A';
    }

    // if we have a header row we have one less row to sort
    if( ab_header == true ) id=ir-1; else id=ir;
    for( i=0; i < id; i++ )
    {
        bswap = false;
        for( j=0; j < id - 1; j++ )
        {
            // test the current value + the next and
            // swap if required.
            row1 = row_data[j];
            row2 = row_data[j+1];
			/*
			if (j < 1) {
				a = row1[ ai_sortcol ];
				b = row2[ ai_sortcol ];
				//alert(a + ":" + a.length +  ":" + b + ":" + b.length );
				alert(a.substr(0,a.length-2) + ":" + a.substr(a.length-2));
				a = (a.length > 3) ? parseInt(a.substr(0,a.length-2))*bs[a.substr(a.length-2)] : 0;
				b = (b.length > 3) ? parseInt(b.substr(0,a.length-2))*bs[b.substr(b.length-2)] : 0;
				alert(a + ":" + b);
			}    		        
			*/
			cmp = f_cmp(ai_type,lastseq,row1[ ai_sortcol ],row2[ ai_sortcol ]);
            if(cmp) {
                row_data[j+1] = row1;
                row_data[j] = row2;
                bswap=true;
            }
        }
        if( bswap == false ) break;
    }

    // load the data back into the table
    ii = is;
    for( i=0; i < id; i++ )
    {
        row1 = row_data[ i ];
        for( j=0; j < ic; j++ )
        {
            ao_table.rows[ii].cells[j].innerHTML = row1[j];
        }
        ii++;
    }
    lastcol = ai_sortcol;
}


