﻿/****************************************************************************/
///LoanTableBuilder takes no parameters when instantiated
/****************************************************************************/

LoanTableBuilder = function(){
    LoanTableBuilder.Instance  = this;
}


/****************************************************************************/
///This function takes any number of parameters
///The first of which is the id of the Element in which to build the Table
///The second is the desired id of the Table to be created which is needed
///to reference the table after it is created in order to add values to it
///All succeeding are objects of key:value 
///representing [headerCell]:[tableCell] values
/*==========================================================================*/

LoanTableBuilder.prototype.BuildTable = function(){    
    var funcArgsKey = [];
    var funcArgsValue = [];
      for(var i=2; arguments[i]; i++){
        for( value in arguments[i]){
            var ArrayEle = $get(arguments[i][value]);
            funcArgsKey.push(value);
            funcArgsValue.push(ArrayEle);
        }
    }
    var pageEle = arguments[0];
    var tbl = document.createElement('table');
    var tblHead = document.createElement('thead');
    var tblBody = document.createElement('tbody');
    var tblFoot = document.createElement('tfoot');
    var tblRow = document.createElement('tr');
    
    function setAlternatingRows(){
        var rows = pageEle.firstChild.firstChild.nextSibling.childNodes;//get all rows in table
        for (var i=0; i<rows.length; i++){//itterate through rows
            (i%2==0) ? rows[i].className = 'even': //if the row is divisible by 2 it is even
                rows[i].className = '';//otherwise it is odd
        }
    }
    
    function createDeleteButton(tblRow){
        var ele = document.createElement('a');
        var eleText = document.createTextNode('Remove');
        
        var deleteAction =  function(){
            if(this.parentNode.parentNode.parentNode.firstChild.nextSibling == null){
                $get('LoanTable').innerHTML = '';
                CalculatorPage.HideStep(pageEle.parentNode.parentNode);
            }
            else{
                this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
                setAlternatingRows();
                LoanTableBuilder.Instance.CustomEventFunction();
            }
        } 
        
        ele.setAttribute('href','#Table');
        LoanTableBuilder.Instance.AddEvent(ele ,'click',deleteAction);
        ele.appendChild(eleText);
        
        return ele;
    }
    
    function createHead(){
        var tblHeadRow = document.createElement('tr');
        var tblCell = document.createElement('th');
        tblCell.appendChild(document.createTextNode(" "));
        tblHeadRow.appendChild(tblCell);
        for(var i=0; funcArgsKey[i]; i++){
            var tblCell = document.createElement('th');
            tblCell.setAttribute('scope','col');
            tblCell.cIndex = i;
            tblCell.appendChild(document.createTextNode(funcArgsKey[i]));
            tblHeadRow.appendChild(tblCell);
            tblHead.appendChild(tblHeadRow);
        }
    }
    
    function createRow(){
        var deleteButton = createDeleteButton(tblRow);
        var tblCell = document.createElement('td');
        var cellDelete = deleteButton;
        tblCell.appendChild(cellDelete);
        tblRow.appendChild(tblCell)
        
       for(var i=0; funcArgsValue[i]; i++){
            var cellContent = createCellContent(funcArgsValue[i]);
            var tblCell = document.createElement('td');
            tblCell.cIndex = i;
            tblCell.appendChild(cellContent);
            tblRow.appendChild(tblCell);
        }
        setAlternatingRows();
        
        for(var i=0; funcArgsValue[i]; i++){
            funcArgsValue[i].value = ''
        }
        funcArgsValue[0].focus();
    }
    
    function createCellContent(funcArg){
        var val;
        var txt = funcArg.value;
        txt = txt.replace(/[$,%]/g,"");
        switch(funcArg.type){
            case "text" :
                if(funcArg.id.match(/loanBalance/i)!= null){
                    val = document.createTextNode(njs.Converter.StringToMoney(txt));
                }
                else if(funcArg.id.match(/interestRate/i)!= null){
                    val = document.createTextNode(njs.Converter.IntToPercent(txt));;
                }
                else{
                    val = document.createTextNode(txt);
                }
                break;
            case "select-one" :
                    val = document.createTextNode(txt);
                break;
            case "checkbox" : 
                val = document.createElement('input');
                val.setAttribute('type','checkbox');
                LoanTableBuilder.Instance.AddEvent(val, 'click', LoanTableBuilder.Instance.CustomEventFunction);
                if(funcArg.checked == true)
                    (window.ActiveXObject)?
                        val.setAttribute('defaultChecked','checked'):
                        val.setAttribute('checked','checked');
                break;
        }
        return val;
    }
    if(!arguments[0].hasChildNodes()){
        tbl.setAttribute('id',arguments[1])
        tbl.appendChild(tblHead);
        tbl.appendChild(tblBody);
        tbl.appendChild(tblFoot);
        pageEle.appendChild(tbl);
        tblBody.appendChild(tblRow);
        createHead();
        createRow();
    }
    else{
        tbl = $get(arguments[1]);
        tblBody = tbl.getElementsByTagName('tbody');
        tblBody = tblBody[0].appendChild(tblRow);
        createRow();
    }
}
/*==========================================================================*/
/****************************************************************************/

LoanTableBuilder.prototype.AddEvent = function(element, eventType, eventFunction, capture)
{
    if (!capture) 
        capture = false;

    var funcCheck = /"function()"/;
    var passedFunction = (typeof eventFunction == "string" && funcCheck.test(eventFunction)) ?
                            eval(eventFunction) : 
                         (typeof eventFunction == "string" && !funcCheck.test(eventFunction)) ?
                            Function(eventFunction) : eventFunction;

    try {
        if (element.addEventListener) 
            element.addEventListener(eventType, passedFunction, capture);
        else 
            element['on' + eventType] = passedFunction;
    }
    catch (e) {} 
}

LoanTableBuilder.prototype.CustomEventFunction = function(){return;}

LoanTableBuilder.prototype.SetCustomEventFunction = function(customEventFunction){
    this.CustomEventFunction = customEventFunction;
}