// Grid.js
// version 0.0.2
// author:      Michel Carroll
// modified:    2005-10-07


// grid
function scrollGrid(bdy) {
    lsOffset=0
    var top=getPrevElSib(bdy)
    var lft=getPrevElSib(top)
    if(lft){
        lsOffset=lft.offsetWidth
        lft.style.top=top.offsetHeight-bdy.scrollTop+"px"
    }
    top.style.left=lsOffset-bdy.scrollLeft+"px"
}

function scrollFullGrid(bdy) {
    lsOffset=0
    var top=getNextElSib(bdy)
    var lft=getNextElSib(top)
    if(lft){
        lsOffset=lft.offsetWidth
        lft.style.top=top.offsetHeight-bdy.scrollTop+"px"
    }
    top.style.left=lsOffset-bdy.scrollLeft+"px"
}

function setWidth(el1,el2){
    if(el1.offsetWidth>el2.offsetWidth){
        el2.style.width=el1.offsetWidth+"px"
        el1.style.width=el1.offsetWidth+"px"
    }else{
        el1.style.width=el2.offsetWidth+"px"
        el2.style.width=el2.offsetWidth+"px"
    }
}

function fixCols(ctr){
    ctr=deriveEl(ctr)
    //if(typeof(ctr[0])=="string")ctr[0]=document.getElementById(ctr[0])
    //dvs=ctr.getElementsByTagName("div")
    //for(var i=0;i<dvs.length;i++){
        //if(dvs[i].className=="grdVP"){
            var top=get1stElKid(ctr)
            var bdy=getNextElSib(top)
            if(bdy!=null){
                if(bdy.getElementsByTagName("tr").length>0){
                    t=top.getElementsByTagName("tr")[0]
                    b=bdy.getElementsByTagName("tr")[0]
                    for(j=0;j<t.cells.length;j++){
                        setWidth(t.cells[j].firstChild.firstChild,b.cells[j].firstChild.firstChild)
                    }
                }
            }
        //}
        //break
    //}
}

// table transform
//need
function trimTable(tbl){
    tbl.border="0"
    tbl.cellSpacing="0"
    tbl.cellPadding="0"
    return tbl
}

//need
function TableShell(bRow){
    var tbl = document.createElement("table")
    trimTable(tbl)
    var tbd = document.createElement("tbody")
    tbl.appendChild(tbd)

    this.shell = tbl
    this.body = tbd
    if(bRow){
        var tr=document.createElement("tr")
        tbd.appendChild(tr)
        this.row = tr
    }
}

//need
function rowCell(el){
    tr = document.createElement("tr")
    tr.appendChild(el)
    return tr
}

//need
function makeTL(tbdy){
    var ts = new TableShell(false)
    ts.shell.className="grdTL"
    ts.shell.style.backgroundColor="fffbcc"
    //could include loop/s for number of frozen cols/rows
    ts.body.appendChild(rowCell(tbdy.rows[0].cells[0].cloneNode(true)))
    tbdy.rows[0].deleteCell(0)
    return ts.shell
}

//need
function makeSide(tbdy){
    var ts = new TableShell(false)
    ts.shell.className="grdLft"
    ts.shell.style.backgroundColor="fffbcc"
    for(i=1;i<tbdy.rows.length;i++){
        ts.body.appendChild(rowCell(tbdy.rows[i].cells[0].cloneNode(true)))
        tbdy.rows[i].deleteCell(0)
    }
    return ts.shell
}

//need
function makeHead(tbdy){
    var ts = new TableShell(false)
    ts.shell.className="grdTop"
    ts.shell.style.backgroundColor="#fffbcc"
    ts.body.appendChild(tbdy.rows[0].cloneNode(true))
    tbdy.deleteRow(0)
    return ts.shell
}

//need
function KmsGrid(t,l,h,w,lhs){
    var lsOffset=0
    var dv=document.createElement("div")
    dv.style.position="relative"

    this.box=dv.cloneNode(true)
    elStyle(this.box,t,l,h,w)
    dv.style.position="absolute"
    this.box.style.overflow="hidden"
    this.box.style.clip="rect(0px "+w+"px auto 0px)"

    this.top=dv.cloneNode(true)
    elStyle(this.top,0,0,30,w)
    // pane dependent
    //this.top.style.zIndex="20"

    this.data=dv.cloneNode(true)
    elStyle(this.data,30,lsOffset,h-30,w-lsOffset)
    this.data.style.overflow="auto"
    //this.data.style.zIndex="40"
    this.data.onscroll=function(){scrollGrid(this)}

    this.box.appendChild(this.data)
	this.box.appendChild(this.top)
	if(lhs){
		this.tl=dv.cloneNode(true)
		elStyle(this.tl,0,0,30,100)
		// pane dependent
		//this.tl.style.zIndex="40"

		this.left=dv.cloneNode(true)
		elStyle(this.left,30,0,h-30,100)
		// pane dependent
		//this.left.style.zIndex="20"
		lsOffset=100

		this.box.appendChild(this.left)
		this.box.appendChild(this.tl)
	}

}

//need
function elStyle(el,t,l,h,w){
    el.style.top=t+"px"
    el.style.left=l+"px"
    el.style.height=h+"px"
    el.style.width=w+"px"
}

function makeGrid(tbl,id,t,l,h,w,lhs){
    kg = new KmsGrid(t,l,h,w,lhs)
    tblNew=tbl.cloneNode(true)
    tblNew.className="grdMain"
    if(lhs){
		tl=makeTL(tblNew.tBodies[0])
		lhs=makeSide(tblNew.tBodies[0])
		kg.tl.id=id+"grdTL"
		kg.left.id=id+"grdLft"
    }
    
    hd=makeHead(tblNew.tBodies[0])
    kg.data.appendChild(tblNew)
    kg.top.appendChild(hd)
    if(lhs){
	    kg.left.appendChild(lhs)
	    kg.tl.appendChild(tl)
    }

    tbl.parentNode.insertBefore(kg.box,tbl)
    tbl.parentNode.removeChild(tbl)
    kg.box.id=id+"dvGrid"
    kg.top.id=id+"grdTop"
    kg.data.id=id+"grdBdy"
}

function primeCells(gd){
    tbs=gd.getElementsByTagName("table")
    for(i=0;i<tbs.length;i++){
        tds=tbs[i].getElementsByTagName("td")
        for(j=0;j<tds.length;j++){
            primeCell(tds[j])
        }
    }
}

function primeCell(td){
    dv=document.createElement("div")
    nb=document.createElement("nobr")
    if(td.firstChild){
        dv.appendChild(td.firstChild.cloneNode(true))
        td.removeChild(td.firstChild)
    }
    nb.appendChild(dv)
    td.appendChild(nb)
}

function setColHeads(lhs,id){
    if(typeof(lhs)=="object"){
        id = lhs[1]
        lhs = lhs[0]
    }
    lsOffset=0
    bdyLft=0
    var grd=document.getElementById(id+"dvGrid")
    var top=document.getElementById(id+"grdTop")
    var bdy=document.getElementById(id+"grdBdy")

    topHt=top.getElementsByTagName("table")[0].offsetHeight
    top.style.height=topHt+"px"
    if(lhs){
		var tl=document.getElementById(id+"grdTL")
		var lft=document.getElementById(id+"grdLft")
		lftWd=lft.getElementsByTagName("table")[0].offsetWidth
		tl.style.height=topHt+"px"
		l=lft.getElementsByTagName("tr")[0]
		rtl=tl.getElementsByTagName("tr")[0]
		lft.style.width=lftWd+"px"
		tl.style.width=lftWd+"px"
		bdyLft=lft.offsetLeft+lft.offsetWidth
		lsOffset=lft.offsetWidth
    }
    t=top.getElementsByTagName("tr")[top.getElementsByTagName("tr").length-1]
    b=bdy.getElementsByTagName("tr")[0]

    bdyTop=top.offsetHeight+top.offsetTop

    if(lhs){
//        setWidth(rtl.cells[0].firstChild.firstChild,l.cells[0].firstChild.firstChild)no good when colspan in top
        setWidth(tl,l)
    }
    setWidth(top.getElementsByTagName("table")[0],bdy.getElementsByTagName("table")[0])
    
    for(i=0;i<t.cells.length;i++){
        setWidth(t.cells[i].firstChild.firstChild,b.cells[i].firstChild.firstChild)
    }

    bdy.style.left=bdyLft+"px"
    bdy.style.width=grd.offsetWidth-lsOffset+"px"
    bdy.style.height=grd.offsetHeight-top.offsetHeight+"px"
    bdy.style.top=bdyTop+"px"

    if(lhs)lft.style.top=bdyTop+"px"
    top.style.left=bdyLft+"px"

}

// sorting

//var sortImgs=new SortPoint("../App_Themes/theme1/sortup.gif","../App_Themes/theme1/sortdn.gif","../App_Themes/theme1/sortno.gif")

//function SortPoint(upIm,dnIm,offIm){
//	this.up=new Image()
//	this.up.src=upIm
//	this.dn=new Image()
//	this.dn.src=dnIm
//	this.off=new Image()
//	this.off.src=offIm
//}


function sortTable(th,trgId){
	//if(tblRes.lang!="true")preTbl()
    var table, thead, tbody, colnum, mode, desc, tbody_rows, i, tr;
    trg = document.getElementById(trgId)
    thead=th.parentNode.parentNode
    tbody=trg.tBodies[0];

    // determine which column number they clicked
    colnum=getCellIndex(th);

    //determine mode to sort with
    //mode=th.sortvar
    mode=th.getAttribute("sortvar")

    //should we sort DESC OR ASC?
    desc=th.desc?true:false;
    th.desc=!desc;
	clearPointers(thead);
	th.getElementsByTagName("img")[0].src=(desc?pgImages["sortUp"].src:pgImages["sortDn"].src)

    for(i=0; i<tbody.rows.length; i++){
        tr=tbody.rows.item(i);
        //tr.sortVal=getCellData(tr, colnum, sortRel)
        tr.sortVal=innerTxt(tr.cells[colnum])
    }

    sortChildren(tbody, 'sortVal', mode, desc);
    tbody_rows=tbody.getElementsByTagName('tr');
    for(i=0; i<tbody_rows.length; i++){
        tbody_rows.item(i).className=(i%2==0)?"odd":"";
    }
}
//main function

function clearPointers(th){
	var pointers = th.getElementsByTagName("img");
	for(i=0;i<pointers.length;i++){
		pointers[i].src=pgImages["sortNo"].src
	}
}

function sortChildren(parentElement, sortProperty, mode, desc){
    var i=0, el=get1stElKid(parentElement), children=[];
    el = parentElement.firstChild;while(el&&el.nodeType!=1){el=el.nextSibling;};
    if(el){
        do{
            children[i]=new String(el[sortProperty]);
            children[i++].object = el;
            el=getNextElSib(el);
        }while(el);
        children.sort(compfunc[mode]);
        if(desc)children.reverse();
        for(i=0; i<children.length; i++){
            parentElement.appendChild(children[i].object);
        }
    }
}

//helper functions
//compare things
var compfunc=[];
compfunc['alph']=function(a, b) {
    if(a>b)return 1;
    if(a<b)return -1;
    return 0;
}
compfunc['num']=function(a, b) {
    return parseInt(a)-parseInt(b);
}
compfunc['date']=function(a, b) {
    return new Date(Date.parse(a)).getTime()-new Date(Date.parse(b)).getTime();
}
compfunc['dateUK']=function(a, b) {
    return dateUk(a)-dateUk(b);
}
compfunc['dateUS']=function(a, b) {
    return dateUs(a)-dateUs(b);
}

function dateUs(d){
    a = d.split(/[\/ -]/)
    return parseInt(a[2]+a[0]+a[1],10)
}

function dateUk(d){
    a = d.split(/[\/ -]/)
    return parseInt(a[2]+a[1]+a[0],10)
}

//function firstChildELEMENT_NODE(parent) {
//    var child = parent.firstChild;
//    while(child && child.nodeType != 1)child = child.nextSibling;
//    return child;
//}
//function nextSiblingELEMENT_NODE(el) {
//    do{el = el.nextSibling;
//    }while (el && el.nodeType != 1);
//    return el;
//}
//function previousSiblingELEMENT_NODE(el) {
//    do{el = el.previousSibling;
//    }while (el && el.nodeType != 1);
//    return el;
//}

function getCellIndex(cell){
    var retval = cell.cellIndex;
    if(retval==0 || !retval){
        while(cell=getPrevElSib(cell)){
            retval++;
        }
    }
    return retval;
}

function getRowIndex(row){
    var retval = row.rowIndex;
    if(retval==0 || !retval){
        while(cell=getPrevElSib(row)){
            retval++;
        }
    }
    return retval;
}

function getCellData(row, cellIndex){
    return row.cells[cellIndex].innerText
}

function trh_mouseOverHandler() {
	this.style.backgroundColor = trHighlightColour
}
function trh_mouseOutHandler()  {
	this.style.backgroundColor = 'f4f4e1'
}

function innerTxt(e){
    return e.innerHTML.replace(/<[^>]+>/g,"").replace(/&nbsp;/," ").replace(/[\s]+$/,"").replace(/^[\s]+/,"")
}


