if (window.JScriptPage==null)
    JScriptPage=new Object();

JScriptPage.lastSortedCols=new Array();
JScriptPage.lastSortedAsc=new Array();

JScriptPage.setSortedTable=function(tabla,types)
{
    if (tabla!=null)
    {
        if (types==null)
        {
            types=new Array(tabla.rows[0].cells.length);
            for (var i=0;i<types.length;i++)
            {
                types[i]=SortedTableType.TextWithNoCase;
            }
        }
        for (var i=0;i<tabla.rows[0].cells.length;i++)
        {
            var cell=tabla.rows[0].cells[i];
            var tipo=types[i];
            if (tipo!=SortedTableType.NoSorted)
            {
                cell.style.cursor='pointer';
                cell.indexForSorting=i;
                var fnAsignacion=function(tabla,cell,tipo)
                {
                    JScriptPage.attachJSEvent(cell,cell,'onclick',function(){
                        JScriptPage.lastSortedCols[tabla.id]=JScriptPage.tableSort(cell,tipo,JScriptPage.lastSortedCols[tabla.id]);
                        });
                }
                fnAsignacion(tabla,cell,tipo);
                JScriptPage.lastSortedAsc[tabla.id]=false;
            }
        }
    }
};
JScriptPage.tableSort=function(cell,tipo,lastCol)
{
    var table=cell.offsetParent;
    var column=cell.indexForSorting;
    var sortAsc=(column!=lastCol || !JScriptPage.lastSortedAsc[table.id]);
    JScriptPage.lastSortedAsc[table.id]=sortAsc;
    JScriptPage.quickSortTable(table,1,table.rows.length-1,column,tipo,sortAsc);

    //Flecha que indica el orden de la columna
    var flechaId=table.id+'__flecha';
    var flecha=document.getElementById(flechaId);
    if (flecha==null)
    {
        flecha=document.createElement('span');
        flecha.id=flechaId;
    }
    else
        flecha.parentElement.removeChild(flecha);
    cell.appendChild(flecha);     
    flecha.innerHTML=sortAsc?'&#9650;':'&#9660;';
    return column;
};
JScriptPage.valorDe=function(valor,tipo)
{
     if (tipo==SortedTableType.Number)
       {
         valorTmp=valor;
         valorTmp =JScriptPage.replaceCharBy( valorTmp, JScriptPage.SepMiles, "");
         valorTmp =JScriptPage.replaceCharBy(valorTmp, JScriptPage.SepDec, ".");
         var i=0;
        
         var valorNum=valorTmp/1;

        if (valorNum==valorTmp)
            return valorNum/1;
        else
        {
            valorNum='';
            while (Common.isNumeric( valorTmp.charAt(i)) && i<valorTmp.length)
                valorNum+=valorTmp.charAt(i++);
            
            if (valorNum.length>0)
                return valorNum/1;
                
            return Number.POSITIVE_INFINITY;
        }
    }
    else
    if (tipo==SortedTableType.TextWithNoCase)
    {
        valor=valor.toUpperCase();
    }
    return valor;
};
JScriptPage.quickSortTable=function(table,izq,der,columna,tipo,sortAsc)
{
   var i=izq;
   var j=der;
   var pivote=JScriptPage.valorDe(table.rows[(izq+der)/2].cells[columna].innerText,tipo);
   do
   {
        if (sortAsc)
        {
            while (JScriptPage.valorDe(table.rows[i].cells[columna].innerText,tipo)<pivote)i++;
            while (JScriptPage.valorDe(table.rows[j].cells[columna].innerText,tipo)>pivote)j--;
        }
        else
        {
            while (JScriptPage.valorDe(table.rows[i].cells[columna].innerText,tipo)>pivote)i++;
            while (JScriptPage.valorDe(table.rows[j].cells[columna].innerText,tipo)<pivote)j--;
        }
        if (i<=j)
        {
            JScriptPage.swapRow(table,i,j);
            i++;
            j--;
        }
    }while(i<=j);
    if (izq < j) JScriptPage.quickSortTable(table,izq,j,columna,tipo,sortAsc);
    if (i < der) JScriptPage.quickSortTable(table,i,der,columna,tipo,sortAsc);
};
JScriptPage.swapRow=function(table,i,j)
{
    if (i!=j)
    {
        table.moveRow(i,j);
        if (j>i+1)
            table.moveRow(j-1,i);
    }
};