﻿function decorateAjaxGridWithCellBuilder(){
    AjaxGrid.prototype.addElementsToCell=function(that,cell,elementName,element,position){
        var newElem=null;
        if(typeof(element)!='object'){
            that.addLabelToCell(that,cell,elementName,element,position);
        }else{
            switch(element.type){
                case 'label':
                    newElem=that.addLabelToCell(that,cell,elementName,element.text,position);
                break;
                case 'anchor':
                    newElem=that.addAnchorToCell(that,cell,elementName,element.text,element.url,element.onClick,position);
                break;
                case 'img':
                    newElem=that.addImgToCell(that,cell,elementName,element.src,element.alt,element.width,element.height,position);
                break;
                case 'imgLnk':
                    newElem=that.addImgLinkToCell(that,cell,elementName,element.src,element.alt,element.width,element.height,element.onClick,position);
                break;
                case 'imgMouseEvents':
                    newElem=that.addImgMouseEventsToCell(that,cell,elementName,element.src,element.alt,element.width,element.height,element.onClick,element.onMouseDown,element.onMouseUp,position);
                break;
                case 'lineBr':
                    that.addBrToCell(that,cell,position);
                break;
                case 'div':
                    newElem=that.addDivCellToRow(that,cell,elementName,element.children,position);
                break;
                default:
                  alert('Undefined element:'+element.type);
            }
            if(newElem&&element.cssClass){
                newElem.className=element.cssClass;
            }
        }
    };
    AjaxGrid.prototype.addLabelToCell=function(that,cell,name,value,position,tableIndex){
        var label=document.createElement('LABEL');
        label.id=that.controlId+'_DataGrid_'+tableIndex+'_lbl'+name;
        label.innerHTML=value;
        cell.appendChild(label);
        return label;
    };
    AjaxGrid.prototype.addAnchorToCell=function(that,cell,name,text,url,onClick,tableIndex){
        var anchor=document.createElement('A');
        anchor.id=that.controlId+'_DataGrid_'+tableIndex+'_a'+name;
        anchor.innerHTML=text;
        anchor.href=url;
        eval('anchor.onclick=function(){'+onClick+'};');
        cell.appendChild(anchor);   
        return anchor;
    };
    AjaxGrid.prototype.addImgToCell=function(that,cell,name,src,alt,width,height,tableIndex){
        var img=document.createElement('img');
        img.id=that.controlId+'_DataGrid_'+tableIndex+'_img'+name;
        img.src=src;
        img.alt=alt;
        if(width>0){
            img.style.width=width+'px';
        }
        if(height>0){
            img.style.height=height+'px';
        }
        cell.appendChild(img);
        return img; 
    };
    AjaxGrid.prototype.addImgLinkToCell=function(that,cell,name,src,alt,width,height,onClick,tableIndex){
        var img=document.createElement('img');
        img.id=that.controlId+'_DataGrid_'+tableIndex+'_img'+name;
        img.src=src;
        img.alt=alt;
        eval('img.onclick=function(){'+onClick+'};');
        if(width>0){
            img.style.width=width+'px';
        }
        if(height>0){
            img.style.height=height+'px';
        }
        cell.appendChild(img);
        return img;
    };
    AjaxGrid.prototype.addImgMouseEventsToCell=function(that,cell,name,src,alt,width,height,onClick,onMouseDown,onMouseUp,tableIndex){
        var img=document.createElement('img');
        img.id=that.controlId+'_DataGrid_'+tableIndex+'_img'+name;
        img.src=src;
        img.alt=alt;
        eval('img.onclick=function(){'+onClick+'};');
        eval('img.onmousedown=function(){'+onMouseDown+'};');
        eval('img.onmouseup=function(){'+onMouseUp+'};');
        if(width>0){
            img.style.width=width+'px';
        }
        if(height>0){
            img.style.height=height+'px';
        }
        cell.appendChild(img);
        return img;
    };
    AjaxGrid.prototype.addDivCellToRow=function(that,cell,name,children,tableIndex){
        var div=document.createElement('div');
        div.id=that.controlId+'_DataGrid_'+tableIndex+'_div'+name;
        for(var idx=0;idx<children.length;idx++){
            that.addElementsToCell(that,div,name+'_'+idx,children[idx],tableIndex);
        }
        cell.appendChild(div);
        return div; 
    };
    AjaxGrid.prototype.addBrToCell=function(that,cell,tableIndex){
        var br=document.createElement('br');
        cell.appendChild(br);
    };
}
