﻿/****************************************************************************/
///This object is not instantiable 
/****************************************************************************/
LoanPackageBuilder = {};


/****************************************************************************/
///Creates a json object as follows:
///LoansPackage{Loans:[{key:value, key:value,..}]}
///This method takes one parameter, an object of key:value pairs
///Parameter should be passed as follows:
///({myKey1:myValue1,myKey2:myValue2,..})
/*==========================================================================*/

LoanPackageBuilder.CreateLoanPackageFromObject = function(obj){
    var LoansPackage = {};
    LoansPackage.Loans = [{}];
    var inputObj = obj;
    for(var n in inputObj){
        var value =  (  typeof inputObj[n] == "string") 
                        ? $get(inputObj[n]).value
                        : inputObj[n];
        switch(n){
            case 'Balance':
                value = (typeof value == "string") ? njs.Converter.MoneyToNumber(value) : value;
            break;
            case 'InterestRate':
                value = njs.Converter.PercentToInt(value);
            break;
        }
        LoansPackage.Loans[0][n] = value;
    }
    return LoansPackage;
}
/*==========================================================================*/
/****************************************************************************/


/****************************************************************************/
///Creates a json object as follows:
///LoansPackage{Loans:[{key:value, key:value,..},{key:value, key:value,..},..]}
///This method takes two parameters the id of the table from which to get
///the values to create the json object and an array of string values that 
///will act as the key names for the values that will be collected this
///array will likely hold strings that are congruent to the names within  
///the <th> elements ie) <th>myKey1</th>
///Parameters should be passed as follows:
///("myTableId",["myKey1","myKey2","myKey3"])
/*==========================================================================*/

LoanPackageBuilder.CreateLoanPackageFromLoanTable = function(tableId, keyNamesArray){
    var LoansPackage = {};
    LoansPackage.Loans = [];
    var tbl = $get(tableId);
    var rows = tbl.firstChild.nextSibling.getElementsByTagName('tr');
    var keyNames = keyNamesArray;
    var j = 0;
        
    for(var i=0; rows[i]; i++){
        LoansPackage.Loans[j] = {};
        
        var focusNode = rows[i].firstChild;
        var hasSibling= true;
        var itterator = 0;
        
        while(hasSibling){
            hasSibling = false;
            
            if(focusNode.nextSibling){
                focusNode = focusNode.nextSibling;
                
                if(focusNode.firstChild.type == "checkbox"){
                    if(focusNode.firstChild.checked){
                        LoansPackage.Loans[j][keyNames[itterator]] = focusNode.firstChild.checked;
                    }
                    else{
                        LoansPackage.Loans[j] = null;
                        j--;
                    }     
                }
                else{ 
                    if(keyNames[itterator] == 'Balance'){
                        LoansPackage.Loans[j][keyNames[itterator]] = (window.ActiveXObject) ?
                            njs.Converter.MoneyToNumber(focusNode.innerText) :
                                njs.Converter.MoneyToNumber(focusNode.textContent);   
                    }
                    else if(keyNames[itterator] == 'InterestRate'){
                        LoansPackage.Loans[j][keyNames[itterator]] = (window.ActiveXObject) ?
                            njs.Converter.PercentToInt(focusNode.innerText) :
                                njs.Converter.PercentToInt(focusNode.textContent);
                    }
                    else{
                        LoansPackage.Loans[j][keyNames[itterator]] = 
                            focusNode.textContent || focusNode.innerText;
                    } 
                }             
                hasSibling = true;
                itterator++;
            }
            else{
                j++;
            }
        }
    }
    if(LoansPackage.Loans[LoansPackage.Loans.length - 1] ==  null){
       LoansPackage.Loans.pop(); 
    }  
    return LoansPackage;
}
/*==========================================================================*/
/****************************************************************************/