//Grid Data View JavaScript Library
//Copyright(C) 1999 NXT Wave Ltd
//http://www.nxt-wave.com
//Developed by Greg Montgomery
//email: greg@nxt-wave.com
//Release: Beta 0.9

function DVGrid(name,DC,DVMode) {
  this.name = name
  this.DVtype = "DVGrid"
  this.DVMode = DVMode
  this.DVPage = ""
  this.cols = new Array()
  this.tableTag = "BORDER=1 WIDTH=\"100%\""
  this.rowTag = ""
  this.DC = DC
  this.state = ""
}

function DVGridColInd(col,DC) {
  if (col.colInd<0) {
    for (var ind=0; ind<DC.colDefn.length; ind++){
      if (col.name == DC.colDefn[ind].name) {
        col.colInd = ind
        break
      }
    }
    if (col.colInd<0) {
      logMessage("E",col.name+" does not exist in "+DC.name)
    }
  }
  return col.colInd
}

function DVGridCol(name,title,format,sortable){
  this.name = name
  this.title = title
  this.format = format
  this.sortable = sortable
  this.tag = ""  //used in <TD [tag]>
  this.colInd = -1
}

function DVGridAddCol(DV, col){
  size = DV.cols.length
  DV.cols[size] = col
  return size
}

function DVGridSort(doc,col,DV){
  logMessage("D","Sort clicked "+col)
  if (DV.state==null || DV.state.length==0) {
    asc=0
  } else {
    lastCol = DV.state.substring(1,DV.state.length)
    asc = DV.state.substring(0,1)
    if (col == lastCol)
    {
      if (asc == "0")
        asc = "1"
      else
        asc = "0"
    } else {
      asc = "0"
    }
  }
  DV.state = asc+""+col
  if (DV.DVMode == "HIST") {
    logMessage("D","Setting state "+DV.state + " mode "+DV.DVMode)
    setCookieState(DV.name,DV.state)
    history.go(0)
  } else {
    logMessage("D","Not history "+DV.DVMode)
    showPage(doc,DV.DVPage,col)
  }
}


function DVGridDraw(doc,dvName,DV) {
  DC = DV.DC
  var newOutline = ""
  doc.write("<TABLE "+DV.tableTag+">");
  doc.write("<TR "+DV.rowTag+">")
  if (DV.DVMode == "HIST") {
    DV.state = getCookieState(DV.name)
    logMessage("D","State"+DV.state)
    preFunct = " "
  } else if (DV.DVMode == "LAYER") {
    if (isIE4) {
      preFunct = ""
    } else {
      preFunct = "parent."
    }
  } else {
    preFunct = "parent."
  }
  var currstate = DV.state
  for (var i = 0; i < DV.cols.length; i++){
   ind = DVGridColInd(DV.cols[i],DC)
   doc.write("<TD "+DV.cols[i].tag+"><A class=col title=SORT HREF=\"\" onClick='"+preFunct+"DVGridSort(document,"+DV.cols[i].colInd+","+preFunct+dvName+");return false;'>"+DC.colDefn[ind].title+"</A></TD>")
  }
  if (DV.state.length==0) {
    curCol=0
    curAsc=0
  } else {
    curCol=parseInt(DV.state.substring(1,DV.state.length))
    curAsc=parseInt(DV.state.substring(0,1))
  }
  DCSortBy(DV.DC,curCol,curAsc)
  for (var row = 0; row < DC.rows.length; row++) {
    newOutline = "<TR "+DV.rowTag+">"
    for (var i = 0; i < DV.cols.length; i++){
      ind = DVGridColInd(DV.cols[i],DC)
      newOutline += "<TD>"+DCGetColVal(DC,row,ind)+"</TD>"
      //newOutline += "<TD>"+DC.rows[row].cols[ind].val+"</TD>"
    }
    newOutline += "</TR>"

    doc.write(newOutline)
    newOutline = ""

    }

  doc.write("</TABLE>");
}
