/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
function setBankTransNegativeForSc(elem) {
    if (IS_CHANGE_DROPDOWN && isFirstReport){
        $(elem).val(1).trigger('change');
    }
}

function setBankExpNegativeForSc(elem) {
    if (IS_CHANGE_DROPDOWN && isFirstReport){
        $(elem).val(1).trigger('change');
    }
}

function setBankEquifNegativeForSc(elem) {
    if (IS_CHANGE_DROPDOWN && isFirstReport){
        $(elem).val(1).trigger('change');
    }
}

function setSmartCreditPersonalInfoForAnalysis(){
    if (IS_CHANGE_DROPDOWN && isFirstReport) {
        $('#main-html-area select.smart-cerdit-personal-info').val(1).trigger('change');
    }
}


function setSmartCreditPublicRecortForAnalysis(){
    if (IS_CHANGE_DROPDOWN && isFirstReport) {
        $('#main-html-area select.smart-credit-public-dropdown').val(1).trigger('change');
    }
}

function getSmartCreditPublicRecortForAnalysis(){
    var contactInfo = [];
   
    var public_tbl = $("#main-html-area td").filter(function () {
        var result = false;
        var current_text = $(this).clone().children().remove().end().text().replace(/<(?:.|\n)*?>/gm, '')
        current_text = current_text.replace(/\r?\n|\r/g, '');
        current_text = $.trim(current_text);

        if (current_text == "Public Information" && $(this).attr('style')) {

            result = true;
        }
        return result;
    }).parent('tr').first().next('tr').children('td').children('table');

    var finalArray = [];
    var value_types_array = [];
    value_types_array[2] = "TransUnion";
    value_types_array[3] = "Experian";
    value_types_array[4] = "Equifax";
    var table_name = "";
    public_tbl.find('tr').each(function (i, el) {
        var table_name_td = $(this).find('td.crGrantedTradelineHeader');
        var temp_table_name = table_name_td.children('b').clone().children().remove().end().text();
        if (temp_table_name) {
            table_name = temp_table_name;
            return false;
        }
    });

    var Obj = {};
    var values = {
            "trans":3,
            "exp":3,
            "equif":3,
        };
        var objDate = {
            "trans":"",
            "exp":"",
            "equif":"",
        };
        
        var type_value_name = {
                "trans":"",
                "exp":"",
                "equif":"",
            };
        var main_key_name = "";  
        var final_count = 0;
    public_tbl.find('tr').each(function (i, el) {

        var tds = $(this).find('td.crLightTableBackground');
        var obj = {key_name: '', data: '', type: ''};
        var obj2 = {}
        var key_name = ""
        tds.filter(function (key_index, value) {
            var key_text = $(value).children('b').clone().children().remove().end().text().replace(/<(?:.|\n)*?>/gm, '');
            if (key_text) {
                key_text.replace(':', '')
                key_name = $.trim(key_text);
                return false;
            }
        });
        
        
        
        tds.filter(function (index, value) {
            var current_text = "";
            current_text = $(this).clone().children().remove().end().text().replace(/<(?:.|\n)*?>/gm, '');
            var newText = ""
            var type = 3;
            if ($(value).children('select').val() > 0) {
                type = $(value).children('select').val();
            }

            if (current_text) {
                obj['type'] = type;
                obj['data'] = current_text;
                obj['key_name'] = key_name;
                obj['table_name'] = table_name;           
                
                if (value_types_array[index]) {

                    if(key_name.toLowerCase() == toLowerCase("Date Filed/Reported:") || key_name.toLowerCase() == toLowerCase("Type:")){

                        if(value_types_array[index] == "TransUnion"){
                            values['trans'] = type;
                            if(key_name.toLowerCase() == toLowerCase("Date Filed/Reported:")){
                                if(current_text)
                                objDate['trans'] = current_text;
                            }else if(key_name.toLowerCase() == toLowerCase("Type:")){
                                if(current_text)
                                type_value_name['trans'] = current_text;
                            }
                        }

                        if(value_types_array[index] == "Experian"){
                            values['exp'] = type;
                            if(key_name.toLowerCase() == toLowerCase("Date Filed/Reported:")){
                                if(current_text)
                                objDate['exp'] = current_text;
                            }else if(key_name.toLowerCase() == toLowerCase("Type:")){
                                if(current_text)
                                type_value_name['exp'] = current_text;
                            }
                        }

                        if(value_types_array[index] == "Equifax"){
                            values['equif'] = type;
                            if(key_name.toLowerCase() == toLowerCase("Date Filed/Reported:")){
                                if(current_text)
                                objDate['equif'] = current_text;
                            }else if(key_name.toLowerCase() == toLowerCase("Type:")){
                                if(current_text)
                                type_value_name['equif'] = current_text;
                            }
                        }
                        
                        if(key_name && final_count == 0){
                            final_count = 1;
                            main_key_name = key_name.replace(':','');
                        }
                        //console.log('key_name',values,objDate,type_value_name);
                    }
                    
                }

            }
        })

        Obj['key_name'] = main_key_name;    
        Obj['values'] = values;    
        Obj['objDate'] = objDate;    
        Obj['type_value_name'] = type_value_name;    
       
       //return false;
        //console.log('dddd',Obj);
    });

    PUBLIC_RECORD_INFO_CREDIT_ANALYSIS = Obj;
    //console.log('PUBLIC_RECORD_INFO_CREDIT_ANALYSIS',PUBLIC_RECORD_INFO_CREDIT_ANALYSIS)
   return Obj;
}

function getSmartCreditInquiriesForAnalysis(){
    var dataObj = [];
    var contactInfo = [];
    
    var inquries_tbl = $("#main-html-area td").filter(function () {
        var current_text = $(this).clone().children().remove().end().text().replace(/<(?:.|\n)*?>/gm, '')
        current_text = $.trim(current_text);
        return current_text == "Inquiries" && $(this).attr('style');
    }).parents('table[border = 1]').first().next('table').next('table');

    inquries_tbl.find('tr').each(function (i, el) {
        var key_tds = $(this).find('td').children('table').each(function () {
            var tds = $(this).find('tr td');
            var obj = {};
            var type = 3;
            var bank_name = getTextValueFromTdByColumn(tds, 1);
            var date_of_inq = "";
            var creditor_bereau = "";
            if (getTextValueFromTdByColumn(tds, 2)) {
                date_of_inq = getTextValueFromTdByColumn(tds, 2);
            }

            if (getTextValueFromTdByColumn(tds, 3)) {
                creditor_bereau = getTextValueFromTdByColumn(tds, 3);
            }

            if (tds.eq(3).children('select').val()) {
                type = tds.eq(3).children('select').val();
            }

            if (bank_name) {
                obj = {
                    'bank_name': bank_name ,
                    'date':date_of_inq,
                    'selected_value': type,
                    'type': creditor_bereau
                }
                contactInfo.push(obj);
            }
        });
    });
    
    INQURIES_RECORD_INFO_CREDIT_ANALYSIS = contactInfo;
}

function setSmartCreaditAccountHistoryForAnalysis() {
    var appent_option = '<option value=\"6\">Repaired Accounts/Now Positive</option>';
    appent_option += '<option value=\"5\">Late Pays / 2 Or Less</option>';
    var set_all_appent_option = '<option value=\"6\">Set all items Repaired Accounts/Now Positive</option>';
    set_all_appent_option += '<option value=\"5\">Set all items Late Pays / 2 Or Less</option>';
    
    var final_Obj = {};
    
    
    var account_history_tables = $('#main-html-area #AccountHistory:contains(Account History)').next('td').html(allSelectSmartCreditDropDown('smart-cerdit-Account-history',set_all_appent_option));
    var all_crditors_contacts_smart_credit = creditorContactsSmartCredit()
    var all_crditors_contacts_smart_credit_addresses = [];
    if(all_crditors_contacts_smart_credit){
        $.each(all_crditors_contacts_smart_credit,function(j, jval){
            var tbank_name = jval.address;
            tbank_name = tbank_name.replace(/\r?\n|\r/g, '');
            tbank_name = tbank_name.replace('_', '');
            tbank_name = tbank_name.replace('-', '');
            tbank_name = tbank_name.replace(/\s+/g, " ");
            if(jval.bank_name){
                all_crditors_contacts_smart_credit_addresses.push($.trim(tbank_name));
            }
        })
    }
    var db_account_history = null;
    var temp_array = [];
    var bankList = [];
   $('#main-html-area .crWhiteTradelineHeader b').each(function (k, elk) {
        var bank = $.trim($(this)
                .clone()    //clone the element
                .children() //select all the children
                .remove()   //remove all the children
                .end()  //again go back to selected element
                .text().replace(/<(?:.|\n)*?>/gm, ''));
        bankList.push(bank);
    });
    
    var parsed_bank = [];
    
    $('#main-html-area .crWhiteTradelineHeader b').each(function (i, el1) {
        var bank = $.trim($(this)
                .clone()    //clone the element
                .children() //select all the children
                .remove()   //remove all the children
                .end()  //again go back to selected element
                .text().replace(/<(?:.|\n)*?>/gm, ''));
        
        var selected_values = {};   
        var Obj = {};
        var date_opened = {};
        var day_count = {};
        var derogetory = {};
        var comments = {};
        var paymentStatus = {};
        var credit_limit = {};
        var balance = {};
        var accountType = {};
        var accountStatus = {};
        var accountno = {};
        if (bank){
            
            var current_table = $(this).parent().parents('table').first().next('table').find('table.crLightTableBackground').first();
            current_table.find('tr').each(function (j, el2) {

                var tds = $(this).find('td');
                var title = $.trim(tds.eq(0)
                        .clone()    //clone the element
                        .children() //select all the children
                        .remove()   //remove all the children
                        .end()  //again go back to selected element
                        .text());
                var experian = getTextValueFromTdByColumn(tds, 2);
                var equifax = getTextValueFromTdByColumn(tds, 3);
                var trans = getTextValueFromTdByColumn(tds, 1);
                var types = getSmartCreditAccountHistoryTypeByKeyName(bank,db_account_history);
                
                
                if(!($.inArray(bank, parsed_bank) !== -1)) {
                    
                    parsed_bank.push(bank);
                    
                    var is_trans_select = false;
                    var is_exp_select = false;
                    var is_equif_select = false;
                
                    $(this).parents('table').first().find('tr').each(function(){
                            if($(this).attr('class')){

                                var title = $.trim($(this).find('td b').text());
                                var trans_val = $.trim($(this).find('td').eq(1).text()).replace('--','');
                                var exp_val = $.trim($(this).find('td').eq(2).text()).replace('--','');
                                var equif_val = $.trim($(this).find('td').eq(3).text()).replace('--','');

//                                if(bank == "M & T BANK"){
//                                    console.log('trans_val',title,trans_val,trans_val.toLowerCase().includes(toLowerCase("late")));
//                                    console.log('exp_val',title,exp_val);
//                                    console.log('equif_val',title,equif_val);
//                                }

                                if(title.toLowerCase() == toLowerCase("Account #:")){
                                    accountno['trans'] = trans_val;
                                    accountno['exp'] = exp_val;
                                    accountno['equif'] = equif_val;

                                }else if(title.toLowerCase() == toLowerCase("Payment Status:")){

                                    if (trans_val)
                                        if (toLowerCase(trans_val) == toLowerCase("Collection/Chargeoff") || toLowerCase(trans_val) == toLowerCase("Late Payment") || trans_val.toLowerCase().includes(toLowerCase("late"))) {
                                            is_trans_select = true;
                                            paymentStatus['trans'] = trans_val;
                                        }

                                    if (exp_val)
                                        if (toLowerCase(exp_val) == toLowerCase("Collection/ChargeOff") || toLowerCase(exp_val) == toLowerCase("Late Payment") || exp_val.toLowerCase().includes(toLowerCase("late"))) {
                                            is_exp_select = true;
                                            paymentStatus['exp'] = exp_val;
                                        }

                                    if (equif_val)
                                        if (toLowerCase(equif_val) == toLowerCase("Collection/ChargeOff") || toLowerCase(equif_val) == toLowerCase("Late Payment") || equif_val.toLowerCase().includes(toLowerCase("late"))) {
                                            is_equif_select = true;
                                            paymentStatus['equif'] = equif_val;
                                        }

                                }else if(title.toLowerCase() == toLowerCase("Account Status:")){
                                    if (trans_val)
                                        if (trans_val == toLowerCase("Derogatory")) {
                                            derogetory['trans'] = "Derogatory";
                                            is_trans_select = true;
                                        } else {
                                            accountStatus['trans'] = trans_val;
                                        }
                                    if (exp_val)
                                        if (exp_val == toLowerCase("Derogatory")) {
                                            is_exp_select = true;
                                            derogetory['exp'] = "Derogatory";
                                        } else {
                                            accountStatus['exp'] = exp_val;
                                        }
                                    if (equif_val)
                                        if (equif_val == toLowerCase("Derogatory")) {
                                            derogetory['equif'] = "Derogatory";
                                            is_equif_select = true;
                                        } else {
                                            accountStatus['equif'] = equif_val;
                                        }
                                }else if(title.toLowerCase() == toLowerCase("Creditor Remarks:")){
                                    //if(bank == "")
                                    if (trans_val)
                                        //if(trans_val.toLowerCase().includes(toLowerCase("Charged Off")) || trans_val.toLowerCase().includes(toLowerCase("Late")) || trans_val.toLowerCase().includes(toLowerCase("Bankruptcy")))
                                        comments['trans'] = trans_val.replace( /[\r\n\t]+/gm, "" ).replace(/\s+/g,' ').trim();
                                    
                                    if (exp_val)
                                        //if(exp_val.toLowerCase().includes(toLowerCase("Charged Off")) || exp_val.toLowerCase().includes(toLowerCase("Late")) || exp_val.toLowerCase().includes(toLowerCase("Bankruptcy")))
                                        comments['exp'] = exp_val.replace( /[\r\n\t]+/gm, "" ).replace(/\s+/g,' ').trim();;
                                    
                                    if (equif_val)
                                        //if(equif_val.toLowerCase().includes(toLowerCase("Charged Off")) || equif_val.toLowerCase().includes(toLowerCase("Late")) || equif_val.toLowerCase().includes(toLowerCase("Bankruptcy")))
                                         comments['equif'] = equif_val.replace( /[\r\n\t]+/gm, "" ).replace(/\s+/g,' ').trim();
                                }else if(title.toLowerCase() == toLowerCase("Creditor Remarks:")){
                                    //if(bank == "")
                                    if (trans_val)
                                        //if(trans_val.toLowerCase().includes(toLowerCase("Charged Off")) || trans_val.toLowerCase().includes(toLowerCase("Late")) || trans_val.toLowerCase().includes(toLowerCase("Bankruptcy")))
                                        comments['trans'] = trans_val.replace( /[\r\n\t]+/gm, "" ).replace(/\s+/g,' ').trim();
                                    
                                    if (exp_val)
                                        //if(exp_val.toLowerCase().includes(toLowerCase("Charged Off")) || exp_val.toLowerCase().includes(toLowerCase("Late")) || exp_val.toLowerCase().includes(toLowerCase("Bankruptcy")))
                                        comments['exp'] = exp_val.replace( /[\r\n\t]+/gm, "" ).replace(/\s+/g,' ').trim();;
                                    
                                    if (equif_val)
                                        //if(equif_val.toLowerCase().includes(toLowerCase("Charged Off")) || equif_val.toLowerCase().includes(toLowerCase("Late")) || equif_val.toLowerCase().includes(toLowerCase("Bankruptcy")))
                                         comments['equif'] = equif_val.replace( /[\r\n\t]+/gm, "" ).replace(/\s+/g,' ').trim();;
                                }else if (title.toLowerCase() == toLowerCase("DATE OPENED:")) {
                                        if (trans_val) {
                                            date_opened['trans'] = trans_val;
                                        }

                                        if (exp_val) {
                                            date_opened['exp'] = exp_val;
                                        }

                                        if (equif_val) {
                                            date_opened['equif'] = equif_val;
                                        }
                                } else if (title.toLowerCase() == toLowerCase("CREDIT LIMIT:")) {
                                        if (trans_val) {
                                            credit_limit['trans'] = trans_val;
                                        }

                                        if (exp_val) {
                                            credit_limit['exp'] = exp_val;
                                        }

                                        if (equif_val) {
                                            credit_limit['equif'] = equif_val;
                                        }
                                } else if (title.toLowerCase() == toLowerCase("Balance Owed:")) {
                                        if (trans_val) {
                                            balance['trans'] = trans_val;
                                        }

                                        if (exp_val) {
                                            balance['exp'] = exp_val;
                                        }

                                        if (equif_val) {
                                            balance['equif'] = equif_val;
                                        }
                                } else if (title.toLowerCase() == toLowerCase("Account Type:")) {
                                        if (trans_val) {
                                            accountType['trans'] = trans_val;
                                        }

                                        if (exp_val) {
                                            accountType['exp'] = exp_val;
                                        }

                                        if (equif_val) {
                                            accountType['equif'] = equif_val;
                                        }
                                }
                            }


                    });
                    

                    if (experian && experian == 'Experian') {
                        if(is_exp_select){
                            setBankExpNegativeForSc(tds.eq(2).children('select'));
                        }
                        selected_values['trans'] = tds.eq(2).children('select').val();            
                    }

                    if (equifax && equifax == 'Equifax') {
                        if(is_equif_select){
                            setBankEquifNegativeForSc(tds.eq(3).children('select'));
                        }
                        selected_values['equif'] = tds.eq(3).children('select').val()
                    }

                    if (trans && trans == 'TransUnion') {
                        if(is_trans_select){
                            setBankTransNegativeForSc(tds.eq(1).children('select'));
                        }
                        selected_values['exp'] = tds.eq(1).children('select').val();
                    }


                    Obj['paymentStatus'] = paymentStatus;
                    Obj['date_opened'] = date_opened;
                    //Obj['day_count'] = day_count;
                    Obj['derogetory'] = derogetory;
                    Obj['comments'] = comments;
                    Obj['balance'] = balance;
                    Obj['credit_limit'] = credit_limit;
                    Obj['accountType'] = accountType;
                    Obj['accountStatus'] = accountStatus;
                    Obj['accountno'] = accountno;
                    Obj['selected_values'] = selected_values;
                    
                    
                } 
            })
            
            final_Obj[bank] = Obj;
            
            
            
        }
    });
    
    
    NEGATIVE_ITEM_SELECTED_LIST_CREDIT_ANALYSIS = final_Obj;
    //console.log('fffff',NEGATIVE_ITEM_SELECTED_LIST_CREDIT_ANALYSIS)
   
}

function getSCSummaryForAnalysis(){
    var result = {};
    //var account_history_tables = $('table tr td#Summary').parents("table:first").next('table tr:first td table');
    var account_history_tables = $('#main-html-area td.crLightTableBackground span b:contains(TOTAL ACCOUNTS:)').parents("table:first");
     
    var negative_items_values = {};
    var inquiries_values = {};
    var public_records_values = {};
    var DELINQUENT_VALUES = {};
    var DEROGATORY_VALUES = {};
    var COLLECTION_VALUES = {};
    var OPEN_ACCOUNTS_VALUES = {};
    var CLOSE_ACCOUNTS_VALUES = {};
    var TOTAL_ACCOUNTS_VALUES = {};
    var BALANCES_VALUES = {};
    var PAYMENTS_VALUES = {};
    
    var trans_val = 0;
    var exp_val = 0;
    var equif_val = 0;
    account_history_tables.find('tr').each(function (i,el) {
        var tds = $(this).find('td');
        var label_name_td = $(this).find('td span b');
        var other_td = $(this).find('td');
         var label_name = $.trim(label_name_td.eq(0)
                                            .clone()    //clone the element
                                            .children() //select all the children
                                            .remove()   //remove all the children
                                            .end()  //again go back to selected element
                                            .text().replace(/<(?:.|\n)*?>/gm, '').replace(/\r?\n|\r/g, '').replace(/\s+/g, " ")).replace(':','');
         var td_text_1 = $.trim(other_td.eq(1).find('span')
                                            .clone()    //clone the element
                                            .children() //select all the children
                                            .remove()   //remove all the children
                                            .end()  //again go back to selected element
                                            .text());
         td_text_1 = parseInt(td_text_1);                        
         var td_text_2 = $.trim(other_td.eq(2).find('span')
                                            .clone()    //clone the element
                                            .children() //select all the children
                                            .remove()   //remove all the children
                                            .end()  //again go back to selected element
                                            .text());
        td_text_2 = parseInt(td_text_2);                                         
         var td_text_3 = $.trim(other_td.eq(3).find('span')
                                            .clone()    //clone the element
                                            .children() //select all the children
                                            .remove()   //remove all the children
                                            .end()  //again go back to selected element
                                            .text());
        td_text_3 = parseInt(td_text_3);                                        
        label_name = label_name.toUpperCase();
       // console.log('label_name',label_name,td_text_1,td_text_2,td_text_3);
        if(!$.isNumeric(td_text_1)){
                td_text_1 = 0;
            }
        if(!$.isNumeric(td_text_2)){
                td_text_2 = 0;
            }
       if(!$.isNumeric(td_text_3)){
                td_text_3 =0 ;
            }
        
        if("TOTAL ACCOUNTS" == label_name){
            TOTAL_ACCOUNTS_VALUES['trans'] = td_text_1;
            TOTAL_ACCOUNTS_VALUES['exp'] = td_text_2;
            TOTAL_ACCOUNTS_VALUES['equif'] = td_text_3;
        }
        
        if("OPEN ACCOUNTS" == label_name){
            OPEN_ACCOUNTS_VALUES['trans'] = td_text_1;
            OPEN_ACCOUNTS_VALUES['exp'] = td_text_2;
            OPEN_ACCOUNTS_VALUES['equif'] = td_text_3;
        }
        
        if("CLOSED ACCOUNTS" == label_name){
            CLOSE_ACCOUNTS_VALUES['trans'] = td_text_1;
            CLOSE_ACCOUNTS_VALUES['exp'] = td_text_2;
            CLOSE_ACCOUNTS_VALUES['equif'] = td_text_3;
        }
        
        if("DELINQUENT" == label_name){
            DELINQUENT_VALUES['trans'] = td_text_1;
            DELINQUENT_VALUES['exp'] = td_text_2;
            DELINQUENT_VALUES['equif'] = td_text_3;
        }
        
        if("DEROGATORY" == label_name){
            DEROGATORY_VALUES['trans'] = td_text_1;
            DEROGATORY_VALUES['exp'] = td_text_2;
            DEROGATORY_VALUES['equif'] = td_text_3;
        }
        
        if("BALANCES" == label_name){
            BALANCES_VALUES['trans'] = td_text_1;
            BALANCES_VALUES['exp'] = td_text_2;
            BALANCES_VALUES['equif'] = td_text_3;
        }
        
        if("PAYMENTS" == label_name){
            PAYMENTS_VALUES['trans'] = td_text_1;
            PAYMENTS_VALUES['exp'] = td_text_2;
            PAYMENTS_VALUES['equif'] = td_text_3;
        }
        
        if(SUMMARY_PUBLIC_RECORD == label_name){         
            public_records_values['trans'] = td_text_1;
            public_records_values['exp'] = td_text_2;
            public_records_values['equif'] = td_text_3;
        }
        
        if('INQUIRIES' == label_name){
            inquiries_values['trans'] = td_text_1;
            inquiries_values['exp'] = td_text_2;
            inquiries_values['equif'] = td_text_3;
        }
               
    })
    
    negative_items_values['trans'] = trans_val;
    negative_items_values['exp'] = exp_val;
    negative_items_values['equif'] = equif_val;
    
   result['negative_item_summary'] = negative_items_values;
   result['DELINQUENT_VALUES'] = DELINQUENT_VALUES;
   result['DEROGATORY_VALUES'] = DEROGATORY_VALUES;
   result['COLLECTION_VALUES'] = COLLECTION_VALUES;
   result['OPEN_ACCOUNTS_VALUES'] = OPEN_ACCOUNTS_VALUES;
   result['CLOSE_ACCOUNTS_VALUES'] = CLOSE_ACCOUNTS_VALUES;
   result['TOTAL_ACCOUNTS_VALUES'] = TOTAL_ACCOUNTS_VALUES;
   result['BALANCES_VALUES'] = BALANCES_VALUES;
   result['PAYMENTS_VALUES'] = PAYMENTS_VALUES;
   result['public_record_summary'] = public_records_values;
   result['inquiry_summary'] = inquiries_values;
   
   return result;
}