function sendDelete(ID, Row){

	sendAction(ID, Row, "delete", 0);
}

function countdown()
        {
            var creatorElem = document.getElementById("creatorCount");
                creatorCount = parseInt(creatorElem.innerHTML) - 1;
                creatorElem.innerHTML=creatorCount;
        }

function sendDeleteFolder(ID, Row){

	if(sendAction(ID, Row, "delete", 0)==true){
        countdown();
    }
}

function sendHide(ID, Row){
    var elem = document.getElementById("hide"+ ID );
    if (elem.innerHTML == 'Unhide') {
        ID = 'u' + ID;
    }
	sendAction(ID, Row, "hide", 0);
}

function sendCancel(ID, Row, containerID){
	sendAction(ID, Row, "cancel", containerID);
}

function sendArchive(ID, Row){
    var elem = document.getElementById("archive"+ ID );
    if (elem.innerHTML == 'Unarchive') {
        ID = 'u'+ID;
    }
	sendAction(ID, Row, "archive", 0);
}

function toggleStatus(ID, Row){
	var tmpName = document.getElementById("name" + ID);
    var elem = document.getElementById("status"+ID);
    var statusDescription = elem.name.substring(2);
    var newStatus = elem.name.substring(0,1);
    var oldStatus = elem.name.substring(1,2);
    var agree = confirm("Are you sure you wish to " + statusDescription +
        " " + tmpName.innerHTML + "?");
    if (agree){
      var xmlHttp;
      try
        {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
        }
      catch (e)
        {
        // Internet Explorer
        try
          {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }
        catch (e)
          {
          try
            {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
          catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
          }
        }

        var URL = "toggleStatus.z?ID=" + newStatus + ID;
        xmlHttp.open("GET",URL,true);
        xmlHttp.send(null);
        elem.name = oldStatus + "" + newStatus + elem.innerHTML;
        elem.innerHTML = statusDescription;

        return true;
     }
     else{
         return false;
     }
}

function sendAction(ID, Row, ActionType, containerID){

var LISTINGHIDE ="HIDE";
var UNHIDE = ID.substring(0, 1);
if (UNHIDE != 'u') {
	var tmpName = document.getElementById("name" + ID);
	var UN = "";
            if (ActionType == 'archive') {
                var NewDescription = 'Unarchive';
                LISTINGHIDE=document.vars.a.value;  //either HIDE OR SHOW
                var elem = document.getElementById("archive" + ID);
                var newCount=parseInt(archiveCount)+1;
            }
            else if(ActionType == 'hide') {
                var NewDescription = 'Unhide';
                LISTINGHIDE=document.vars.h.value; //either HIDE OR SHOW

                var elem = document.getElementById("hide" + ID );
                var newCount=parseInt(dismissedCount)+1;
            } 
}
else {
    LISTINGHIDE = "SHOW";
	var tempID= ID.substring(1);
	var tmpName = document.getElementById("name" + tempID);
	var UN = "un";
                if (ActionType == 'archive') {
                var NewDescription = 'Archive';
                var elem = document.getElementById("archive"+tempID);
                var newCount=parseInt(dismissedCount)-1;
            }
            else if(ActionType == 'hide') {
                var NewDescription = 'Dismiss';
                var elem = document.getElementById("hide"+tempID);
                var newCount=parseInt(archiveCount)-1;
            } 
}

var LISTINGHIDEALT="SHOW";
if (LISTINGHIDE == "SHOW") {
    LISTINGHIDEALT="HIDE";
}

var agree = confirm("Are you sure you wish to " + UN + ActionType + " " + tmpName.innerHTML + "?");

    if (agree){
        
    if (ActionType == 'hide') {
        dismissedCount=newCount;       
        var te = document.getElementById('th');
        if (LISTINGHIDE == 'SHOW') {
            var displayToogle = displayDismissedHide;
        }
        else {
             var displayToogle = displayDismissedShow+" ("+dismissedCount+")";
        }

        if (newCount == 0) {
            te.innerHTML = "";
        }
        else {
            te.innerHTML = displayToogle;
            te.href = "listing.z?h="+LISTINGHIDEALT;
        }
        //clear comma
        if (dismissedCount != 0 && archiveCount != 0) {
            document.getElementById("cs").innerHTML = ", ";
        }
        else {
            document.getElementById("cs").innerHTML="";
        }
    }
    if (ActionType == 'archive') {
        var te = document.getElementById('ta');
        archiveCount=newCount;
        if (LISTINGHIDE == 'SHOW') {
            var displayToogle = displayArchiveHide;
        }
        else {
             var displayToogle = displayArchiveShow+" ("+archiveCount+")";
        }
        
        if (newCount == 0) {
            te.innerHTML = "";
        }
        else {
            te.innerHTML = displayToogle;
            te.href = "listing.z?a="+LISTINGHIDEALT;
        }
        //clear comma
        if (dismissedCount != 0 && archiveCount != 0) {
            document.getElementById("cs").innerHTML = ", ";
        }
        else {
            document.getElementById("cs").innerHTML="";
        }
    }
    
      var xmlHttp;
      try
        {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
        }
      catch (e)
        {
        // Internet Explorer
        try
          {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }
        catch (e)
          {
          try
            {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
          catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
          }
        }
		
        var URL = ActionType + ".z?ID=" + ID;

		if (containerID != 0) {
		URL=URL + "&containerID=" + containerID;
		} 
        xmlHttp.open("GET",URL,true);
        xmlHttp.send(null);
        if(elem) {
           elem.innerHTML=NewDescription;
        }
        
        if (LISTINGHIDE=='HIDE') {
            trRM(Row);
        }
        return true;	     
     }
     else{
         return false;
     }
}
     
     /*hide or show columns */
function hidecolumn(Name) {
          var stl = 'none';
          var elems  = document.getElementsByName(Name);
          for (x in elems){
          elems[x].style.display=stl;
          }
          return hiddenColumns;     
     }
function showcolumn(Name) {

          var stl = 'block'
          var elems  = document.getElementsByName(Name);
          for (x in elems){
          elems[x].style.display=stl;     
          }
          return hiddenColumns;
     }


function $(v) { return(document.getElementById(v)); }
function $T(v,i) { return((typeof(i)=='string'?$(i):(i?i:document)).getElementsByTagName(v)); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function inArray(v,r) { for(var i in r) if(r[i]==v) return true; } 
function iSW(w,n) { if($(w)) $(w).id=''; if(n) n.id=w; }

/* TBODY */

function trMAP(a,b,z) { var s=a%2?0:1; for(var i=a?a:0; i<=(b?b:Trl); i++) { Tr[i].className=(s=!s)?'even':'odd'; Tr[i].cells[0].innerHTML=i+1; if(z) trMouse(Tr[i]); } }
function trMV(w,n) { w-=1; n-=1; var b=n<=w?n+1:n-1, z=Tb.replaceChild(Tr[w],Tr[n]); try{Tb.insertBefore(z,Tr[b])}catch(err){Tb.insertBefore(z)}; trMAP(Math.min(w,n),Math.max(w,n)); }
function trRM(o) { var v=o.rowIndex; T.deleteRow(v); Trl-=1; trMAP(v-1); }
// old function trRM(o) { var v=o.rowIndex; T.deleteRow(v); Tr[v-1].id='this'; Trl-=1; trMAP(v-1); }
function trMouse(o) {

	o.onclick=function(){ if(stop) return false; };
	o.onmousedown=function(){ stop=1; iSW('this',this); document.onmouseup=function(){ var v=$('hover'); if(v) { v.id=''; trMV($('this').rowIndex,v.rowIndex); } stop=0; }; };
	o.onmouseover=function(){ iSW('hover',(stop && this.id!='this')?this:''); };

}

/* THEAD */

function thMV(w,n) { var r=T.rows,v='',b='',z='';

	for(var i=0; i<r.length; i++) { v=r[i].cells; SW(v[w],v[n],'innerHTML'); if(i==0) { SW(v[w],v[n],'style.width'); } }

}

function thMouse(r,R) {

	for(var i=0; i<r.length; i++) { if((R && !inArray(i,R)) || !R) {
		r[i].onmousedown=function() { stopTH=1; iSW('thisTH',this); document.onmouseup=function() { var v=$('hoverTH'); if(v) { v.id=''; thMV($('thisTH').cellIndex,v.cellIndex); } stopTH=0; }; };
		r[i].onmouseover=function() { iSW('hoverTH',(stopTH && this.id!='thisTH')?this:''); };

	} }
}


function SW(w,n,v) { function f(a,b) { return(eval("a."+v+(b?"='"+b+"'":'')+";")); }; var z=f(n); f(n,f(w)); f(w,z); }

/* KEY STROKE */

function mkTime(v) { var z=new Date().getTime(v); return(z); }
function kCode(e) { var k=agent('opera')?e.kCode:(agent('msie')?event.keyCode:e.which); return(k); }
function kRun(e,r,fn) { var k=kCode(e); if(inArray(k,r) && !stopK[1]) { stopK=Array('',mkTime()); kStop(k,fn); } }
function kStop(k,fn) { if(!stopK[0]) { if(fn) fn(k); setTimeout("kStop('"+k+"',"+fn+")",120/((mkTime()-stopK[1]>500)?3:1)); } else { stopK[1]=''; } }

document.onkeydown=function(e){ kRun(e,Array(37,38,39,40),function(k) { var v=((k==40||k==39)?1:-1); iSW('this',Tr[Math.min(Trl,Math.max(0,$('this').rowIndex+v-1))]); }); };
document.onkeyup=function(){ stopK[0]=1; };

/* GLOBALS */

var T=$('table'), Tb=T.tBodies[0], Tr=Tb.rows, Trl=Tr.length-1, stop=0, stopTH=0, stopK='', cur=''; trMAP(0,'',1); thMouse($T('th','head'),Array(0,6));
