var datable = null;      
var OList;
init();
function init(){   
        datable = document.getElementById('datable').getElementsByTagName('TBODY')[0];
    document.getElementById('actionSelect').selectedIndex = 0;                
    document.getElementById('sortSelect').selectedIndex = 0;                
    
    var rows = datable.getElementsByTagName('tr');
    var cnt = 0;
    for(i=0;i<rows.length;i++)
        if(rows[i].id.indexOf('ignore') < 0)
            cnt++;
    OList = new Array(cnt);
    var offset = 0;
    for(i=0;i<rows.length;i++)
        if(rows[i].id.indexOf('ignore') < 0)
            OList[i-offset] = rows[i];
        else
            offset++;
    DoSort(nameCmp);
}

function SelectSort() {
    switch(document.getElementById('sortSelect').options[document.getElementById('sortSelect').selectedIndex].value){
        case 'n': DoSort(nameCmp);break;
        case 'r': DoSort(rowsCmp);break;
        case 's': DoSort(sizeCmp);break;
        case 't': DoSort(typeCmp); break;
        case 'd': DoSort(dateCmp); break;
    }
}   
function SelectAction(){
    var cbs = datable.getElementsByTagName('input');
    var checked = '';
    for(i=0;i<cbs.length;i++){
        if(cbs[i].checked) checked+=cbs[i].value+'\n';
    }
    if(checked == ''){
        alert('Please select one or more objects first.');
        document.getElementById('actionSelect').selectedIndex = 0;                
        return;
    }
    checked = checked.substring(0,checked.length-1);            
    switch(document.getElementById('actionSelect').options[document.getElementById('actionSelect').selectedIndex].value){
        case 'd': DoDrops(checked);break;
    }
    document.getElementById('actionSelect').selectedIndex = 0;                
}
function DoDrops(list){
    if(confirm("Are you sure you want to drop these tables?\n\n"+list)){
        document.getElementById('maction').value = 'd';
        document.getElementById('maobjs').value = list;
        document.forms[1].submit();
    }else{}
}
function DoSort(cmpFunk){
    OList.sort(cmpFunk);    
    for(i=0;i<OList.length;i++){
        datable.appendChild(OList[i]);        
    }
}
function getRowName(rnode) {
    var rtd = rnode.getElementsByTagName('td');
    
        return rtd[rtd.length-1].getElementsByTagName('a')[0].childNodes[0].nodeValue;
    
}
function getSize(rnode){
    var f = rnode.getElementsByTagName('td')[2].childNodes[0].nodeValue;
    if(f.indexOf('NA') > -1)return -1;
    else{
        f = f.replace(/,/g,'');
        return parseInt(f);
    }
}
function getRows(rnode){
    var f = rnode.getElementsByTagName('td')[1].childNodes[0].nodeValue;
    if(f.indexOf('NA') > -1)return -1;
    else{
        f = f.replace(/,/g,'');
        return parseInt(f);
    }
}
function getType(rnode){
    return rnode.getElementsByTagName('td')[4].getElementsByTagName('a')[0].getAttribute('class');
}
function getDate(rnode) {
    var f = rnode.getElementsByTagName('td')[3].childNodes[0].nodeValue;
    if (f.indexOf('NA') > -1) return -1;
    else {
        f = f.replace(/,/g, '');
        return parseInt(f);
    }
}
function nameCmp(rnode1,rnode2){
    var a1 = getRowName(rnode1).toLowerCase();
    var a2 = getRowName(rnode2).toLowerCase();
    if(a2>a1)return -1;
    if(a2<a1)return 1;
    return 0;
}     
function rowsCmp(rnode1,rnode2){
    return getRows(rnode2) - getRows(rnode1);
}     
function sizeCmp(rnode1,rnode2){
    return getSize(rnode2) - getSize(rnode1)  ;
}     
function typeCmp(rnode1,rnode2){
    var a1 = getType(rnode2);
    var a2 = getType(rnode1);
    if(a2>a1)return -1;
    if(a2<a1)return 1;
    return 0;
}
function dateCmp(rnode1, rnode2) {
    return getDate(rnode2) - getDate(rnode1);
}
