var PERSONAL_INFO = [];
var SELECTED_ASSOC = "";
var PRIVACY_GUARD_RESULTS = [];
$(document).ready(function () {
    $('button[name=checkConsole], button[name=credit_btn]').click(function(){
        var client_id = document.getElementById('client_id').value;
        var source_type = $('select[name=source_type]').val();
        var btn_name = $(this).attr('name');
        //alert(btn_name);
        if(btn_name=='credit_btn'){
            $('#credit-loader').show();
        }else{
            $('.save-loader').show();
        }
        var formData = {};
        if(client_id==""){
            alert('Please select Client');
            $('select[name=client_id]').parent().addClass('has-error');
        }else {
            var html_content = "";
            var client_id = document.getElementById('client_id').value;
            // var tables = document.getElementsByClassName("rpt_content_table");

            // var delete_all_input = $('input[name=delete_all]');
            // var delete_all = 0;
            // if(delete_all_input.prop('checked')){
            //     delete_all = 1;
            // }
            $('.ng-hide').remove();
            //html_content = $('textarea[name=html_content]').val();//$('.view-html').html();
            html_content = $('.view-html').html();
            html_content = html_content.replace(/\n\s+|\n/g, "");
            html_content = html_content.replace(/<\!--.*?-->/g, "");
            html_content = html_content.replace(/<script[^>]*>(?:(?!<\/script>)[^])*<\/script>/g, "");// remove all script tags from report
            //$.parseHTML(html_content);  
            //console.log('html');
            //console.log(html_content);   
            if(source_type == 1){
                var pInfo = getPersonalInfoWithDropDownData();
                var report_date = $.trim($( "td:contains('Credit Report Date:')" ).next().text());
                
                // var bankInfo = getBankTableInfo();
                // var inq = getSelectedInquiries();
                // var publicInfo = getSelectedPublicInfo();
                formData = {
                    '_token': $('#token').val(),
                    'client_id': client_id,
                    'source_type': source_type,
                    'html_content':html_content,
                    'report_date' :report_date,
                    // 'delete_all':delete_all,
                    //'PERSONAL_INFO_DATA':pInfo,
                    // 'inquiries':inq,
                    // 'publicInfo':publicInfo,
                    // 'outputBankAcc': bankInfo,
                    // 'outputBankInfo': bankInfo,
                };
  
            }else if(source_type == 2){
                var privacyGuard_info = getPrivacyGuardBankTableInfo();
                var personl_info = getDropDownPrivacyGuardPersonalInfo()
                var inq = getDropDownInquiryPrivacyGuard();
                var publicInfo = getDropDownPublicRecordPrivacyGuard()
                formData = {
                    '_token': $('#token').val(),
                    'client_id': client_id,
                    'delete_all':delete_all,
                    'source_type': source_type,
                    'inquiries':inq,
                    'publicInfo':publicInfo,
                    'html_content':html_content,
                    'privacyGuardBank':privacyGuard_info,
                    'PERSONAL_INFO_DATA':personl_info
                };
            }
        }
           //console.log(formData);
           //return false;
                //alert(saveIdentityID);
                $.ajax({
                    type: "POST",
                    data: formData,
                    url: saveIdentityID,
                    success: function (data) {
                         $('.save-loader').hide();
                         //$('#credit-loader').hide();
                        //console.log(data);// will alert "ok"
                        if(data=="success"){
                             if(btn_name=='credit_btn'){
                                var name = $('select[name=client_id]').children('option:selected').data('name');
                                $('input[name=f_l_name]').val(name);
                                var current_date = new Date();
                                var formatedDate = moment(current_date).format('YYYY-MM-DD');
                                $('input[name=date_audit]').val(formatedDate)
                                $('input[name=credit_client_id]').val(client_id);
                                $('input[name=credit_source_type]').val(source_type);
                                var source_name = "";
                                if(source_type == 1){
                                    source_name = "Identitiy IQ";
                                }else if(source_type == 2){
                                    source_name = "Privacy Guard";
                                }
                                
                                $('input[name=credit_report_and_date]').val(source_name+' report dated '+formatedDate+' per '+source_name);
                                $('#credit-analaysis').css({display:'block',overflow:'auto'}).addClass('in');
                            }else{
                                alert("Report has been processed successfully!");
                            }
                            
                        }else{
                            alert("Failed. Try again later.");
                        }
                  
                    },
                    error: function () {
                         alert("Failed. Try again later.111");
                    }
                });
            
    });
    
    function getBankAdditionalInfo(bankName)
    {
       var table = bankName;
       ////console.log('table');
       ////console.log(table);
       
    }
    
    
    
    function setDropDownforPersonalInfo(data)
    {
        var assocDB = [];
        
        var table = $('table.rpt_content_table:eq(1)');
        ////console.log('selectedAssoc');
        ////console.log(data);
       
        var personalInfo = [];
         table.find('tr').each(function (i, el) {
            var $tds = $(this).find('td');
            var key = $tds.eq(0).text();
            var key = $tds.eq(0).text();
                if(key != ""){
                    var text1 = $tds.eq(1).text().replace(/<(?:.|\n)*?>/gm, '');
                     text1 = text1.replace(/\r?\n|\r/g, '');
                     text1 = text1.replace('_', '');
                     text1 = text1.replace('-', '');
                     text1 = text1.replace(/\s+/g, " ");
                     text1 = text1.trim();
                     text1 = text1.replace('updatenegativepositive', '');
                    var text2 = $tds.eq(2).text().replace(/<(?:.|\n)*?>/gm, '');
                     text2 = text2.replace(/\r?\n|\r/g, '');
                     text2 = text2.replace('_', '');
                     text2 = text2.replace('-', '');
                     text2 = text2.replace(/\s+/g, " ");
                     text2 = text2.trim();
                     text2 = text2.replace('updatenegativepositive', '');
                    var text3 = $tds.eq(3).text().replace(/<(?:.|\n)*?>/gm, '');
                     text3 = text3.replace(/\r?\n|\r/g, '');
                     text3 = text3.replace('_', '');
                     text3 = text3.replace('-', '');
                     text3 = text3.replace(/\s+/g, " ");
                     text3 = text3.trim();
                     text3 = text3.replace('updatenegativepositive', '');
                     var key_name = key.replace(/<(?:.|\n)*?>/gm, '');
                     key_name = key.replace(/\r?\n|\r/g, '');
                     key_name = key.replace(':', " ");
                     key_name = key.replace(/\s+/g, " ");
                     key_name = key.trim();
                     var status = getStatusByDataKey(key_name.replace(':',''),data);
                     if(text1!=""){
                        $tds.eq(1).append(pinfoHtmlstring());
                        ////console.log('info');
                        ////console.log(status);
                        var status3 = 3;
                        if(status!=""){
                            status3 = status.status3
                        }
                        $tds.eq(1).children('select').val(status3).trigger('change');
                        $tds.eq(1).children('select').attr('class','all-select review_type');
                    }
                     if(text2!=""){
                        $tds.eq(2).append(pinfoHtmlstring());
                        var status1 = 3;
                        if(status!=""){
                            status1 = status.status1
                        }
                        $tds.eq(2).children('select').val(status1).trigger('change');
                        $tds.eq(2).children('select').attr('class','all-select review_type');
                    }
                     if(text3!=""){
                        $tds.eq(3).append(pinfoHtmlstring());
                        var status2 = 3;
                        if(status!=""){
                            status2 = status.status2
                        }
                        $tds.eq(3).children('select').val(status2).trigger('change');
                        $tds.eq(3).children('select').attr('class','all-select review_type');
                    }
                
                    
                    
                 }
        });

       
    }
    
    
    function getStatusByDataKey(key,data)
    {
        var info = "";
        
        
        $.each(data.DB,function(index,value){
            ////console.log('daaaaattta');
            ////console.log(value);
             if(value.data_key == key){
               info =  value;
               return false;
            }
        
        });
        
        return info;
    }

    function getSelectedInfo()
    {
        
       SELECTED_ASSOC = ""; 
       var client_id = $('select[name=client_id]').val();
       var source_type = $('select[name=source_type]').val();
       if(client_id == "" || client_id <= 0){
           return false;
       }

       $.ajax({
                type: "GET",
                data: {'client_id': client_id,'source_type':source_type},
                url: select_url,
                async:false,
                success: function (data) {
                    
                    var info = data;
                    var json = info.datadocument;
                    SELECTED_ASSOC = json;
                    //console.log('all DPR,DIN,DD');
                    //console.log(info);
                    var html = bankhtmlstring();
                    getInquiriesAndPublicSelectedInfo(html,info);
                    if(source_type == 1){
                        
                        setDropDownforPersonalInfo(json);
                        //$('.sub_header').append(html);
                        $('.sub_header').next('table').find('th.headerTUC').append(html);
                        $('.sub_header').next('table').find('th.headerEXP').append(html);
                        $('.sub_header').next('table').find('th.headerEQF').append(html);
                        $('.sub_header').each(function(){
                            var rf = $(this).text();
                            rf = rf.replace('updatenegativepositive', '');
                            rf = rf.replace('Please selectnegativepositive', '');
                            rf = $.trim(rf);
                            if(rf == 'Risk Factors'){
                              $(this).next('table').find('th').children('select').remove();
                            }
                            
                            if(rf == 'Bankruptcy'){
                              $(this).next('table').find('th').children('select').remove();
                            }
                        })
                        setSelectedBankDropDown(json);
                    } else if(source_type==2) {
                        var inq = setDropDownInquiryPrivacyGuard(html,info);
                        var bankNameList = [];
                        var bankList = [];
                        var temp_array = [];
                        setDropDownPrivacyGuardPersonalInfo(html,data);
                        $('.FtgCrDarkTradeHead').each(function(i,value){
                            var bank_name =  $(this).text();
                            bank_name = $.trim(bank_name);
                            bankNameList.push(bank_name);
                            $(this).parent().next('tr').children('th.FtgCrXPNHead').append(html);
                            $(this).parent().next('tr').children('th.FtgCrEFXHead').append(html);
                            $(this).parent().next('tr').children('th.FtgCrTUCHead').append(html);
                            $(this).parent().next('tr').children('th.FtgCrXPNHead').children('select').attr('class','banks review_type').attr('id','select_pg_exp_'+i);
                            $(this).parent().next('tr').children('th.FtgCrEFXHead').children('select').attr('class','banks review_type').attr('id','select_pg_eqf_'+i);
                            $(this).parent().next('tr').children('th.FtgCrTUCHead').children('select').attr('class','banks review_type').attr('id','select_pg_tuc_'+i);
                        })
                        
                        $('.FtgCrDarkTradeHead').each(function(i,value){
                            var bank_name =  $(this).text();
                            bank_name = $.trim(bank_name);
                            temp_array.push(bank_name);
                            var obj = {'index':i,'bank_name':bank_name+getKeyIndex(bank_name,temp_array,bankNameList)};
                            bankList.push(obj);
                        });
                       
                        //console.log(info.datadocument.DI);
                       $.each(info.datadocument.DI,function(i,value){
                          var index = getMatchIndex(value.data_key, bankList);
                          $('#select_pg_exp_'+index).val(value.type2).trigger('change');
                          $('#select_pg_eqf_'+index).val(value.type3).trigger('change');
                          $('#select_pg_tuc_'+index).val(value.type).trigger('change');
                       });  
                    }
                    setAllGlobalDropdown();
                },
                error: function () {
                     alert("Failed. Try again later.");
                }
            });
            
            
       return SELECTED_ASSOC;     
    }

    function getPersonalInfoWithDropDownData()
    {
        PERSONAL_INFO = [];
        var personal_info = [];
        ////console.log('personalTable');
        var table = $('table.rpt_content_table:eq(1)');
        ////console.log('table');
        ////console.log(table[0]);
       var personalInfo = [];
         table.find('tr').each(function (i, el) {
                
            var $tds = $(this).find('td');
            var key = $tds.eq(0).text();
            var obj = {};    
                
                if(key != ""){
                    var text1 = $tds.eq(1).text().replace(/<(?:.|\n)*?>/gm, '');
                     text1 = text1.replace(/\r?\n|\r/g, '');
                     text1 = text1.replace('_', '');
                     text1 = text1.replace('-', '');
                     text1 = text1.replace(/\s+/g, " ");
                     text1 = text1.trim();
                     text1 = text1.replace('updatenegativepositive', '');
                     text1 = text1.replace('Send To Both EVcrm for Attacks And Audit', '');
                     text1 = text1.replace('Send To Only EVcrm Attack', '');
                     text1 = text1.replace('Send To Only EVcrm Audit', '');
                    var text2 = $tds.eq(2).text().replace(/<(?:.|\n)*?>/gm, '');
                     text2 = text2.replace(/\r?\n|\r/g, '');
                     text2 = text2.replace('_', '');
                     text2 = text2.replace('-', '');
                     text2 = text2.replace(/\s+/g, " ");
                     text2 = text2.trim();
                     text2 = text2.replace('updatenegativepositive', '');
                     text2 = text1.replace('Send To Both EVcrm for Attacks And Audit', '');
                     text2 = text1.replace('Send To Only EVcrm Attack', '');
                     text2 = text1.replace('Send To Only EVcrm Audit', '');
                    var text3 = $tds.eq(3).text().replace(/<(?:.|\n)*?>/gm, '');
                     text3 = text3.replace(/\r?\n|\r/g, '');
                     text3 = text3.replace('_', '');
                     text3 = text3.replace('-', '');
                     text3 = text3.replace(/\s+/g, " ");
                     text3 = text3.trim();
                     text3 = text3.replace('updatenegativepositive', '');
                     text3 = text1.replace('Send To Both EVcrm for Attacks And Audit', '');
                     text3 = text1.replace('Send To Only EVcrm Attack', '');
                     text3 = text1.replace('Send To Only EVcrm Audit', '');
                   
                  var key_name = key.replace(/<(?:.|\n)*?>/gm, '');
                     key_name = key.replace(/\r?\n|\r/g, '');
                     key_name = key.replace(':', " ");
                     key_name = key.replace(/\s+/g, " ");
                     key_name = key.trim();
                     
                    obj = {
                        'TransUnion':{data:text1,'type':$tds.eq(1).children('select').val(),'key_name':key_name.replace(':','')},
                        'Experian':{data:text2,'type':$tds.eq(2).children('select').val(),'key_name':key_name.replace(':','')},
                        'Equifax':{data:text3,'type':$tds.eq(3).children('select').val(),'key_name':key_name.replace(':','')},
                    }
                    
                    PERSONAL_INFO.push(obj);
                    personal_info.push(obj);
                    
                }
        });

        return personal_info;
    }
    
    
    // html dropdown for personal info section.
    function pinfoHtmlstring() {
        var string= "<select style=\"width: 120px;\n" +"    padding: 0px;color: black;\n" +"    height: 19px;\n" +"    margin: 0px;\n" +"    float: right;\" name=\"source_type\"  class=\"review_type\"><option value=\"3\">Send To Both EVcrm for Attacks And Audit</option><option value=\"1\">Send To Only EVcrm Attack</option><option value=\"2\">Send To Only EVcrm Audit</option></select>";
        return string;
    }
    
    function htmlstring() {
        var string= "<select style=\"width: 120px;\n" +"    padding: 0px;color: black;\n" +"    height: 19px;\n" +"    margin: 0px;\n" +"    float: right;\" name=\"source_type\"  class=\"review_type\"><option value=\"3\">Please select</option><option value=\"1\">negative</option><option value=\"2\">positive</option></select>";
        return string;
    }
    function bankhtmlstring() {
        var string= "<select style=\"width: 120px;\n" +"    padding: 0px;color: black;\n" +"    height: 19px;\n" +"    margin: 0px;\n" +"    float: right;\" name=\"source_type\"  class=\"banks\"><option value=\"3\">Please select</option><option value=\"1\">negative</option><option value=\"2\">positive</option></select>";
        return string;
    }
    function htmlDropdownString(len) {
        var string= "<select id='dropdown_"+len+"' style=\"width: 120px;\n" +"    padding: 0px;color: black;\n" +"    height: 19px;\n" +"    margin: 0px;\n" +"    float: right;\" name=\"source_type\"  class=\"review_type\"><option value=\"3\">Please select</option><option value=\"1\">negative</option><option value=\"2\">positive</option></select>";
        return string;
    }
    
    
    
    
    
    function bankCode()
    {
        var bankCodes = new Map([
            ['Capital One', 'C1'],
            ['Capital One Ltd', 'C1'],
            ['T-MOBILE','TMobile'],
            ['SPRINT', 'SPRINT'],
        ]);
        return bankCodes;
    }
    
    $('button[name=process_html]').click(function(){
        var content = $('textarea[name=html_content]').val();
        var search = document.getElementById('search').value;
        var client_id = $('select[name=client_id]').val();
        $('button[name=credit_btn]').hide();
        if(search==""){
            $('select[name=source_type]').parent().addClass('has-error');
            //alert('Please select source type')
        }else if(client_id == 0){
            $('select[name=client_id]').parent().addClass('has-error');
            alert('Please select client')
        } else {
            if (content.length > 0) {
                var source_type = $('select[name=source_type]').val();
               //var html = htmlstring();
                $('select[name=client_id]').parent().removeClass('has-error');
                $('select[name=source_type]').parent().removeClass('has-error');
                $('textarea[name=html_content]').parent().removeClass('has-error');
                $('textarea[name=html_content]').parent().hide();
                $(this).hide();
                $('button[name=checkConsole]').show();
                $('button[name=generateReport]').show();
                $('#velocity_link').show();
                $('button[name=credit_btn]').show();
                $('#goto-report').show();
                if(content !=""){
                    content = content.replace('document.write',"wwwdocument.write");  
                }
                try{
                   var source_type = $('select[name=source_type]').val();
                   if(source_type == 2){
                    content = content.replace('href="resources','href="https://www.privacyguard.com/BCA/Generic/ECD/resources/');
                    //content = content.replace('src="resources','src="https://www.privacyguard.com/BCA/Generic/ECD/resources/');
                    content = replaceAll(content,'ScoreAnalysis','ScoreAnalysis content-div-hide');
                    }
                   
                    $('.view-html').html(content);
//                        console.log($(this));
//                    })
                }catch(e){

                }
                 //getSelectedInfo();

                $('select[name=client_id]').attr('disabled',true);
                
            } else {
                $('textarea[name=html_content]').parent().addClass('has-error');
            }
        }
    });
    $('button[name=paste_html]').click(function(){
        $('.view-html').html('');
        $('textarea[name=html_content]').val('')
        $('textarea[name=html_content]').parent().show();
        $('button[name=process_html]').show();

    });
    
    
    
    $('button[name=import_html]').click(function(){
        var personal_info_table_td_class = $('.info');
        
        ////console.log(personal_info_table_td_class[5].text());
        //$('#mainform').submit();
        
    });
});
$('button[name=credit_btn1]').click(function(){
        var client_id = $('select[name=client_id]').val();
        var source_type = $('select[name=source_type]').val();
        $('#credit-loader').show();
        if(client_id > 0 ){
        $.ajax({
            type: "GET",
            data: {'action':'clientInfo','client_id':client_id,'source_type':source_type},
            url: credit_analysis_url,
            success:function (data) {
                //$('button[name=checkConsole]').click();
                $('#credit-loader').hide();
                var jsonParseData = JSON.parse(data);
                //console.log(jsonParseData);
                $('input[name=f_l_name]').val(jsonParseData.clients.last_name+", "+jsonParseData.clients.first_name);
                //current_date
                $('input[name=date_audit]').val(jsonParseData.current_date)
                $('input[name=credit_client_id]').val(client_id);
                $('input[name=credit_source_type]').val(source_type);
                //Identity IQ  report dated 11/2/2017 per Identity IQ
                //$('#credit-analaysis').css({display:'block',overflow:'auto'}).addClass('in');
            },
            error: function () {
               $('#credit-loader').hide();
            }
        });
    }
});

function replaceAll(str, find, replace) {
    return str.replace(new RegExp(find, 'g'), replace);
}
$('.modal-close-btn').click(function(){
     $('#credit-analaysis').css({display:'none'}).removeClass('in');
})    
function getMatchIndex(bank_name,bankList){
    var result = 3;
    $.each(bankList,function(i,value){
        bank_name = $.trim(bank_name);
        var current_bank = $.trim(value.bank_name);
            if(bank_name == current_bank){
//                //console.log('match')
//                //console.log(bank_name+"===="+current_bank+"====="+value.index);
               result = value.index;
               return false;
            }else{
//               //console.log('Not match')
//                //console.log(bank_name+"===="+current_bank); 
            }
    });

    return result;
}

function count_array_values(my_array, match) 
    { 
        var count = 0; 

        $.each(my_array,function(i,value){
            if (value == match) 
            { 
                count++; 
            } 
        }) 
        

        return count; 
    } 

function getKeyIndex(value, tmp_array, total_array){
        
        var result = "";
        var exist_in_temp = 0;
        var total_exist = count_array_values(total_array,value);
        
        if(tmp_array == "" || total_array == "" || total_exist < 2){
            return  "";
        } else {
           
           exist_in_temp = count_array_values(tmp_array,value); 
        }
        
        return "-"+exist_in_temp;
        
    }


function getInquiriesAndPublicSelectedInfo(html, data)
{
   var client_id = $('select[name=client_id]').val();
   if(client_id == "" || client_id <= 0){
       return false;
   }
    setInquiriesDropDown(html,data);
    setPublicInfoDropDown(html,data);
}


function getHtmlInfo(elem)
{
    var html_text = $('textarea[name=html_content]').val();
    var source_type = $('select[name=source_type]').val();
    if(html_text != ""){
        return false;
    }
    $('#client-loader').show();
    var client_id = $(elem).val();
    var _token= $('#token').val();
        $.ajax({
            type: "POST",
            
            data: {'action':'htmlinfo','client_id':client_id,'source_type':source_type,'_token':_token},
            url: saveIdentityID,
            success:function (data) {
                ////console.log(data);
                var json = JSON.parse(data);
                $('textarea[name=html_content]').val(json);
                $('#client-loader').hide();
            },
            error: function () {
               $('#client-loader').hide();
            }
        });
}




function setSelectedBankDropDown(data)
{   
    var tables = $(".crPrint");
    var bankAdditional = getBankAdditionalInfo();
    var banObj = [];
    var bankArray = [];
    var temp_array = [];
    var bankList = []; 
    tables.find('tr').each(function (i, el) {
        var $tds = $(this).find('td');
        var bank_name ="";
        var firstTd = $tds.eq(0).children('ng-include').children('.sub_header');
        bank_name =  firstTd.clone().children().remove().end().text().replace(/<(?:.|\n)*?>/gm, '');
        bank_name = bank_name.replace(/\r?\n|\r/g, '');
        bank_name = bank_name.replace('_', '');
        bank_name = bank_name.replace('-', '');
        bank_name = bank_name.replace(/\s+/g, " ");
        bank_name = bank_name.trim();
        bank_name = bank_name.replace('updatenegativepositive', '');
        bank_name = $.trim(bank_name);
        if(bank_name != ""){
            bankList.push(bank_name);
        } 
    }); 
    
    $('.sub_header').each(function (i, el) {
            var $tds = $(this).find('td');
            var bank_name ="";
           
            //var firstTd = $tds.eq(0).children('ng-include').children('.sub_header');
            bank_name =  $(this).clone().children().remove().end().text().replace(/<(?:.|\n)*?>/gm, '');
            
            bank_name = bank_name.replace(/\r?\n|\r/g, '');
            bank_name = bank_name.replace('_', '');
            bank_name = bank_name.replace('-', '');
            bank_name = bank_name.replace(/\s+/g, " ");
            bank_name = bank_name.trim();
            bank_name = bank_name.replace('updatenegativepositive', '');
            bank_name = $.trim(bank_name);
            if(bank_name != ""){
                temp_array.push(bank_name);
                var obj = {'index':i,'bank_name':bank_name+getKeyIndex(bank_name,temp_array,bankList)};
                $(this).nextAll('table').first().find('th.headerTUC').children('select').attr('id','selectiq_tuc_'+i);
                $(this).nextAll('table').first().find('th.headerEXP').children('select').attr('id','selectiq_exp_'+i);
                $(this).nextAll('table').first().find('th.headerEQF').children('select').attr('id','selectiq_eqf_'+i);
                $(this).children('select').attr('class','banks review_type');
                bankArray.push(obj);
            }     
    });
   
   console.log('data')
   console.log(data.DI);
   $.each(data.DI,function(i,value){
       var index = getMatchIndex(value.data_key,bankArray);
       console.log(index);
       $('#selectiq_tuc_'+index).val(value.type).trigger('change');
       $('#selectiq_exp_'+index).val(value.type2).trigger('change');
       $('#selectiq_eqf_'+index).val(value.type3).trigger('change');
   })
    
}

function pushPrivacyGuardValues(array, key){
    PRIVACY_GUARD_RESULTS.push({'bank_name':key,'bank_info':array}) ;
}
function getPrivacyGuardBankTableInfo()
{
    var tables = $("#FtgCrTabContent5 .FtgCrContentRowTable");
    var banObj = [];
    
    
    var selectval = {};
    var objArray = [];
    
    tables.find('tr').each(function (i, el) {
            var obj = {};
            var obj1 = [];
            var $tds = $(this).find('td');
            var th =  $(this).find('th');
            var expAccType = "";
            var equiFaxAccType = "";
            var transAccType = "";
            var key_name = "";
            var firstTh = th.eq(0).clone().children().remove().end().text();
            var bank_name ="";
            if(firstTh != "" && th.hasClass('FtgCrDarkTradeHead')){
                bank_name = firstTh;
                var tr = th.parent();
                var trIQExp = th.parent().next('tr').children('th.FtgCrXPNHead');
                var trIQEqufx = th.parent().next('tr').children('th.FtgCrEFXHead');
                var trIQTransu = th.parent().next('tr').children('th.FtgCrTUCHead');
                var ExpType = 3;
                var EqufxType = 3;
                var TransuType = 3;
                if(trIQExp.children().has('select')){
                    ExpType = trIQExp.children('select').val();
                }
                
                if(trIQEqufx.children().has('select')){
                    EqufxType = trIQEqufx.children('select').val();
                }
                
                if(trIQTransu.children().has('select')){
                    TransuType = trIQTransu.children('select').val();
                }
                
                var trPGExp = th.parent().next('tr').children('th.FtgCrXPNHead');
                var trPGEqufx = th.parent().next('tr').children('th.FtgCrEFXHead');
                var trPGTransu = th.parent().next('tr').children('th.FtgCrTUCHead');
                
                if(trPGExp.children().has('select')){
                    ExpType = trPGExp.children('select').val();
                }
                
                if(trPGEqufx.children().has('select')){
                    EqufxType = trPGEqufx.children('select').val();
                }
                
                if(trPGTransu.children().has('select')){
                    TransuType = trPGTransu.children('select').val();
                }
                
                selectval = {'Experian':ExpType,'Equifax':EqufxType,'TransUnion':TransuType};
                
                tr.next('tr').each(function(){
                    
                   var tr = $(this);
                   var key = tr.children('th').eq(0).text();
                   var text1 = $.trim(tr.children('td').eq(0).clone().children().remove().end().text());
                   var text2 = $.trim(tr.children('td').eq(1).clone().children().remove().end().text());
                   var text3 = $.trim(tr.children('td').eq(2).clone().children().remove().end().text());
                   expAccType =  text1;
                   equiFaxAccType = text2;
                   transAccType =  text3;
                   if(key != 'Date:' && key != ""){
                    obj1.push({
                         'Experian':{'account':expAccType,'key_name':key},
                         'Equifax':{'account':equiFaxAccType,'key_name':key},
                         'TransUnion':{'account':transAccType,'key_name':key},
                         });
                    }
                });
                
                if(obj1 != ""){
                    obj = {
                        'bank_name':$.trim(bank_name),
                        'address':"",
                        'phone':"",
                        'bank_info':obj1,
                        'type':selectval
                    }


                    objArray.push(obj);
                }
            }

    });

        return objArray;
}

function getStatusByDataKey1(key,data)
    {
        var info = "";
        
        //console.log('test111')
        ////console.log(data.datadocument)
        $.each(data.DB,function(index,value){
//            //console.log('daaaaattta');
//            //console.log(value);
             if(value.data_key == key){
               info =  value;
               return false;
            }
        
        });
        
        return info;
    }


function setDropDownPrivacyGuardPersonalInfo(html , data){
    var tables = $('.FtgCrContentRowTable').first();
    tables.find('tr').each(function (i, el) {
        var $ths = $(this).find('th');
        var firstTh = $ths.eq(0);
        var title = firstTh.text();
        var secondTd = firstTh.parent().children('td');
        title = title.replace(/\r?\n|\r/g, '');
        title = title.replace('_', '');
        title = title.replace('-', '');
        title = title.replace(/\s+/g, " ");
        title = title.replace(":", "");
        title = $.trim(title);
//      //console.log('abc');
//      //console.log(title+"==="+secondTd.eq(0).text()+"==="+secondTd.eq(1).text()+"===="+secondTd.eq(2).text());
        var bank_name = "";
        bank_name =  secondTd.eq(0).text().replace(/<(?:.|\n)*?>/gm, '');
        bank_name = bank_name.replace(/\r?\n|\r/g, '');
        bank_name = bank_name.replace('_', '');
        bank_name = bank_name.replace('-', '');
        bank_name = bank_name.replace(/\s+/g, " ");
        bank_name = bank_name.replace('updatenegativepositive', '');
        bank_name = bank_name.trim();
        var status = getStatusByDataKey1(title,data);
        
        
        if(bank_name != ""){
           secondTd.eq(0).append(html);
            var status3 = 3;
            if(status!=""){
                status3 = status.status3
            }
            secondTd.eq(0).children('select').val(status3).trigger('change');
            secondTd.eq(0).children('select').attr('class','all-select review_type');
        }
        
        bank_name =  secondTd.eq(1).text().replace(/<(?:.|\n)*?>/gm, '');
        bank_name = bank_name.replace(/\r?\n|\r/g, '');
        bank_name = bank_name.replace('_', '');
        bank_name = bank_name.replace('-', '');
        bank_name = bank_name.replace(/\s+/g, " ");
        bank_name = bank_name.replace('updatenegativepositive', '');
        bank_name = bank_name.trim();
        
        if(bank_name != ""){
           secondTd.eq(1).append(html);
           var status1 = 3;
            if(status!=""){
                status1 = status.status1
            }
            secondTd.eq(1).children('select').val(status1).trigger('change');
            secondTd.eq(1).children('select').attr('class','all-select review_type');
        }
        
        bank_name =  secondTd.eq(2).text().replace(/<(?:.|\n)*?>/gm, '');
        bank_name = bank_name.replace(/\r?\n|\r/g, '');
        bank_name = bank_name.replace('_', '');
        bank_name = bank_name.replace('-', '');
        bank_name = bank_name.replace(/\s+/g, " ");
        bank_name = bank_name.replace('updatenegativepositive', '');
        bank_name = bank_name.trim();
        
        if(bank_name != ""){
           secondTd.eq(2).append(html);
           var status2 = 3;
            if(status!=""){
                status2 = status.status2
            }
            
            secondTd.eq(2).children('select').val(status2).trigger('change');
            secondTd.eq(2).children('select').attr('class','all-select review_type');
        }
        
    });
}
function getDropDownPrivacyGuardPersonalInfo(){
    var tables = $('.FtgCrContentRowTable').first();
    var mainObj = [];
    tables.find('tr').each(function (i, el) {
        var $ths = $(this).find('th');
        var firstTh = $ths.eq(0);
        var title = firstTh.text();
        var experian = "";
        var experianType = 3;
        var equifax = "";
        var equifaxType = 3;
        var transunion = "";
        var transunionType = 3;
        var everyObj = {};
        var secondTd = firstTh.parent().children('td');
        title = title.replace(/\r?\n|\r/g, '');
        title = title.replace('_', '');
        title = title.replace('-', '');
        title = title.replace(/\s+/g, " ");
        title = title.replace(":", "");
        title = $.trim(title);
//      //console.log('abc');
//      //console.log(title);
        var bank_name = "";
        bank_name =  secondTd.eq(0).clone().children().remove().end().text().replace(/<(?:.|\n)*?>/gm, '');
        bank_name = bank_name.replace(/\r?\n|\r/g, '');
        bank_name = bank_name.replace('_', '');
        bank_name = bank_name.replace('-', '');
        bank_name = bank_name.replace(/\s+/g, " ");
        bank_name = $.trim(bank_name);
        bank_name = bank_name.replace('updatenegativepositive', '');
        if(bank_name != ""){
           experian = $.trim(bank_name);
           experianType = secondTd.eq(0).children('select').val();
        }
        
        bank_name =  secondTd.eq(1).clone().children().remove().end().text().replace(/<(?:.|\n)*?>/gm, '');
        bank_name = bank_name.replace(/\r?\n|\r/g, '');
        bank_name = bank_name.replace('_', '');
        bank_name = bank_name.replace('-', '');
        bank_name = bank_name.replace(/\s+/g, " ");
        bank_name = bank_name.trim();
        bank_name = bank_name.replace('updatenegativepositive', '');
        
        if(bank_name != ""){
           equifax = $.trim(bank_name);
           equifaxType = secondTd.eq(1).children('select').val();
        }
        
        bank_name =  secondTd.eq(2).clone().children().remove().end().text().replace(/<(?:.|\n)*?>/gm, '');
        bank_name = bank_name.replace(/\r?\n|\r/g, '');
        bank_name = bank_name.replace('_', '');
        bank_name = bank_name.replace('-', '');
        bank_name = bank_name.replace(/\s+/g, " ");
        bank_name = bank_name.trim();
        bank_name = bank_name.replace('updatenegativepositive', '');
        
        if(bank_name != ""){
           transunion = $.trim(bank_name);
           transunionType = secondTd.eq(2).children('select').val();
        }
        
        if(title != ""){
           everyObj = {
               'Experian':{
                   'data':experian,
                   'key_name':title,
                   'type':experianType
               },
               'Equifax':{
                   'data':equifax,
                   'key_name':title,
                   'type':equifaxType
               },
               'TransUnion':{
                   'data':transunion,
                   'key_name':title,
                   'type':transunionType
               }
           };
           mainObj.push(everyObj);
        }
        
    });
    
    return mainObj;
}




function getBankTableInfo()
{
    var tables = $(".crPrint");
    ////console.log('bankAdditional')
    var bankAdditional = getBankAdditionalInfo();
    ////console.log(bankAdditional)
    var banObj = [];
    tables.find('tr').each(function (i, el) {
            var $tds = $(this).find('td');
            var bank_name ="";
            var firstTd = $tds.eq(0).children('ng-include').children('.sub_header');
            
            var transunionType = firstTd.next('table').find('th.headerTUC').children('select').val();
            var experianType = firstTd.next('table').find('th.headerEXP').children('select').val();
            var equifaxType = firstTd.next('table').find('th.headerEQF').children('select').val();
            
            bank_name =  firstTd.clone().children().remove().end().text().replace(/<(?:.|\n)*?>/gm, '');
            bank_name = bank_name.replace(/\r?\n|\r/g, '');
            bank_name = bank_name.replace('_', '');
            bank_name = bank_name.replace('-', '');
            bank_name = bank_name.replace(/\s+/g, " ");
            bank_name = bank_name.trim();
            bank_name = bank_name.replace('updatenegativepositive', '');
            var subTable = $tds.eq(0).children('ng-include').children('.rpt_content_table');
            if(bank_name != ""){
                
                var banObj1 = [];
                var obj1 = {};
                
                subTable.find('tr').each(function (i, el) {
                   var obj = {}; 
                   var $subtds = $(this).find('td');
                   var key = $subtds.eq(0).text();
                   var key_name = key.replace(/<(?:.|\n)*?>/gm, '');
                    key_name = key.replace(/\r?\n|\r/g, '');
                    key_name = key.replace(':', " ");
                    key_name = key.replace(/\s+/g, " ");
                    key_name = key.replace(" ", "");
                    key_name = key.trim();
                   if(key_name != ""){
                       
                     var text1 = $subtds.eq(1).text().replace(/<(?:.|\n)*?>/gm, '');
                     text1 = text1.replace(/\r?\n|\r/g, '');
                     text1 = text1.replace('_', '');
                     text1 = text1.replace('-', '');
                     text1 = text1.replace(/\s+/g, " ");
                     text1 = text1.trim();
                     text1 = text1.replace('updatenegativepositive', '');
                     
                    var text2 = $subtds.eq(2).text().replace(/<(?:.|\n)*?>/gm, '');
                     text2 = text2.replace(/\r?\n|\r/g, '');
                     text2 = text2.replace('_', '');
                     text2 = text2.replace('-', '');
                     text2 = text2.replace(/\s+/g, " ");
                     text2 = text2.trim();
                     text2 = text2.replace('updatenegativepositive', '');
                     
                    var text3 = $subtds.eq(3).text().replace(/<(?:.|\n)*?>/gm, '');
                     text3 = text3.replace(/\r?\n|\r/g, '');
                     text3 = text3.replace('_', '');
                     text3 = text3.replace('-', '');
                     text3 = text3.replace(/\s+/g, " ");
                     text3 = text3.trim();
                     text3 = text3.replace('updatenegativepositive', '');
                    
                    obj = {
                        'TransUnion':{account:text1,'key_name':$.trim(key_name.replace(':',''))},
                        'Experian':{account:text2,'key_name':$.trim(key_name.replace(':',''))},
                        'Equifax':{account:text3,'key_name':$.trim(key_name.replace(':',''))},
                    }
                    
                    banObj1.push(obj);
                   }
                   
               });
               
               ////console.log('sssssss')
               ////console.log(banObj1)
               var bankInfo = getInfoByKey(bank_name,bankAdditional);
               var name = "";
               var address = "";
               var phone = "";
               if(bankInfo.bank_name != null ){
                   name = bankInfo.bank_name
               }
               
               if(bankInfo.address != null ){
                   address = bankInfo.address
               }
               
               if(bankInfo.phone != null ){
                   phone = bankInfo.phone
               }
               banObj.push({'bank_name':bank_name,'address':address,'phone':phone,
                   'type':{
                       'TransUnion':transunionType,
                       'Experian':experianType,
                       'Equifax':equifaxType
                   },'bank_info':banObj1});
            }
        });
        ////console.log('banObj1111');
        //console.log(banObj);
        var bankMergedData = [];
        $.each(banObj,function(index,value){
            var currentObj = {};
            var isExist = isInfoExist($.trim(value.bank_name),bankAdditional,banObj);
            ////console.log('isexist');
            //console.log(value);
            var dropDownType = 3;
            var current_address = "";
            var current_phone = "";
            var bank_info = "";
            if(value.address != ""){
                current_address = value.address;
            }
            
            if(value.phone != ""){
                current_phone = value.phone;
            }
            
            if(!isEmpty(value.type)){
               dropDownType = value.type;
            }
            
            if(value.bank_info != null){
                bank_info = value.bank_info;
            }
            currentObj = {
                    'bank_name':value.bank_name,
                    'address':current_address,
                    'phone':current_phone,
                    'bank_info':bank_info,
                    'type':dropDownType
                }
            if(isExist.status == 1){
                if(isExist.bankInfo.type > 0){
                    dropDownType = isExist.bankInfo.type;
                }
                
                if(isExist.bankInfo.address != ""){
                    current_address = isExist.bankInfo.address;
                }

                if(isExist.bankInfo.phone != ""){
                    current_phone = isExist.bankInfo.phone;
                }
                
                
                currentObj = {
                    'bank_name':isExist.bankInfo.bank_name,
                    'address':current_address,
                    'phone':current_phone,
                    'bank_info':bank_info,
                    'type':dropDownType
                }
            }
            bankMergedData.push(currentObj);
        })
        
        
        return bankMergedData;
}
function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}
function getInfoByKey(key_name,arrayObj)
{
    var bankInfo = {}
    var sanitizeKeyName = $.trim(key_name);
    
    if(arrayObj != null){
        $.each(arrayObj,function(index, value){
            if(sanitizeKeyName.indexOf($.trim(value.bank_name)) != -1){
               bankInfo =  value;
               return false;
            }
        });
    }
    return bankInfo;
}

function isInfoExist(key_name,arrayObj,mainArray)
{
    var result = 0;
    var bankInfo = {}
    var sanitizeKeyName = $.trim(key_name);
   //alert(sanitizeKeyName);
    
    if(arrayObj != null){
         
        $.each(arrayObj,function(index, value){
            var add_bank_name = $.trim(value.bank_name);
            if(sanitizeKeyName.indexOf(add_bank_name) != -1){
               result = 1;
               bankInfo = getBankArrayInfoBy(sanitizeKeyName,mainArray)//value;
               return false;
            }
        });
    }
    var obj = {'status':result,'bankInfo':bankInfo};
    return obj;
}

function getBankArrayInfoBy(key_name,mainArray){
    var info = {};
    $.each(mainArray,function(index,value){
       var bank_name = $.trim(value.bank_name); 
        if(bank_name == key_name){
            info = value;
            return false;
        }
    });
    
    return info;
}

function getBankAdditionalInfo()
{
    var table = $('table.rpt_content_table:last');
     var bankObj = [];
     var obj = {};    
     table.find('tr').each(function (i, el) {
        
         var $tds = $(this).find('td');
            var bank_name ="";
            bank_name = $tds.eq(0).text().replace(/<(?:.|\n)*?>/gm, '');
            bank_name = bank_name.replace(/\r?\n|\r/g, '');
            bank_name = bank_name.replace('_', '');
            bank_name = bank_name.replace('-', '');
            bank_name = bank_name.replace(/\s+/g, " ");
            bank_name = bank_name.trim();
            //alert(bank_name);return false;
            if(bank_name != ""){
            var $tds = $(this).find('td');
            var key = $tds.eq(0).text();
            
            var text1 = $tds.eq(1).text().replace(/<(?:.|\n)*?>/gm, '');
             text1 = text1.replace(/\r?\n|\r/g, '');
             text1 = text1.replace('_', '');
             text1 = text1.replace('-', '');
             text1 = text1.replace(/\s+/g, " ");
             text1 = text1.trim();
             text1 = text1.replace('updatenegativepositive', '');
            var text2 = $tds.eq(2).text().replace(/<(?:.|\n)*?>/gm, '');
             text2 = text2.replace(/\r?\n|\r/g, '');
             text2 = text2.replace('_', '');
             text2 = text2.replace('-', '');
             text2 = text2.replace(/\s+/g, " ");
             text2 = text2.trim();
             text2 = text2.replace('updatenegativepositive', '');
            var text3 = $tds.eq(3).text().replace(/<(?:.|\n)*?>/gm, '');
             text3 = text3.replace(/\r?\n|\r/g, '');
             text3 = text3.replace('_', '');
             text3 = text3.replace('-', '');
             text3 = text3.replace(/\s+/g, " ");
             text3 = text3.trim();
             text3 = text3.replace('updatenegativepositive', '');
             
            
             obj = {
                 'bank_name':bank_name,
                 'address':text1,
                 'phone':text2
             }
            
        
            bankObj.push(obj);
            }
     
     });
     
        ////console.log('required bankObj data');
        ////console.log(obj);
        return bankObj;
}

function getTypeByDataKey(key,data)
{
        var info = "";
        $.each(data.DI,function(index,value){
            var newKey = getBankRealName(value.data_key);
             if($.trim(key).indexOf(newKey)!= -1){
               info =  value;
               return false;
            }
        
        });
        
        return info;
}


function getBankRealName(bank_name)
{
    var realName = "";
    var bankNameArray = bank_name.split('-');
    if(bankNameArray == null){
        realName = bank_name;
    }
    $.each(bankNameArray,function(i,value){
        var index = value;
        if(index != null &&  index >=1 && index <= 100){
         realName = bank_name.replace("-"+index,"");
         return false;
        }
    });
    return realName;
}

function setInquiriesDropDown(html,data){
    var dataObj = [];
    
    //var html = "<select style=\"width: 120px;\n" +"padding: 0px;color: black;\n" +"height: 19px;\n" +"    margin: 0px;\n" +"    float: right;\" name=\"source_type\"  class=\"review_type\"><option value=\"3\">update</option><option value=\"1\">negative</option><option value=\"2\">positive</option></select>";
        ////console.log('inqData');
        ////console.log(data);
    $('.rpt_fullReport_header').each(function(){
        var inq = $(this).text();
        inq = $.trim(inq);
        if(inq == 'Inquiries'){
            $(this).children('select').remove();
            $(this).append(allSelectDropDown('inquiries'));
           var table =  $(this).nextAll('table.rpt_content_table').first();
           table.find('tr').each(function (i, el) {
               var tds = $(this).find('td');
               var creditor_name = $.trim(tds.eq(0).text());
               var date_of_inq = $.trim(tds.eq(2).text());
               var creditor_bereau = $.trim(tds.eq(3).text());
               
               
               if(creditor_bereau != ""){
                 var data_key = creditor_name+"_"+date_of_inq;
               var type = getInquiryTypeByDataKey(data_key,data);  
                 tds.eq(3).append(html);
                 if(type > 0){
                 tds.eq(3).children('select').val(type).trigger('change');
                 tds.eq(3).children('select').attr('class','inquiries review_type');
                }
               }
           });
        }
    });
    
}


function getInquiryTypeByDataKey(data_key,data)
{
    ////console.log('getInq');
    ////console.log(data);
    var inquiry_type = 3;
    $.each(data.inquiry, function(index,value){
        var key = value.inquiry_key;
        if(key == data_key){
          inquiry_type = value.inquiry_type;
          //alert(inquiry_type);
          return false;
        }
    });
    
    return inquiry_type;
}

function getPublicTypeByDataKey(data_key,data)
{
    ////console.log('getPublic');
    ////console.log(data);
    var public_types = {
        'status1':3,
        'status2':3,
        'status3':3
    };
    $.each(data.publicrecords, function(index,value){
        var key = value.public_key;
        if(key == data_key){
          public_types = {
                      'status1':value.public_status1,
                      'status2':value.public_status2,
                      'status3':value.public_status3
                    };          
          return false;
        }
    });
    
    return public_types;
}

function setPublicInfoDropDown(html,data){
    var dataObj = [];    
    
    $('.sub_header').each(function(){
        var pb = $(this).text();
        pb = $.trim(pb);
        console.log(pb);
        if(pb == 'Bankruptcy'){
            //$(this).children('select').remove();
            //
           var table =  $(this).nextAll('table.rpt_content_table').first();
           table.find('tr').each(function (i, el) {
               var tds = $(this).find('td');
               var key_name = $.trim(tds.eq(0).text()).replace('-','');
               var creditor_name = $.trim(tds.eq(1).text()).replace('-','');
               var date_of_inq = $.trim(tds.eq(2).text()).replace('-','');;
               var creditor_bereau = $.trim(tds.eq(3).text()).replace('-','');
               key_name = key_name.replace(':','');
               if(key_name != ""){
                  var types = getPublicTypeByDataKey(key_name,data);
               }
               ////console.log('ssssssssss');
               ////console.log(types);
               if(creditor_name != ""){
                 tds.eq(1).append(html);
                  tds.eq(1).children('select').val(types.status3).trigger('change');
                  tds.eq(1).children('select').attr('class','publicrecord review_type');
               }
               if(date_of_inq != ""){
                 tds.eq(2).append(html);
                 tds.eq(2).children('select').val(types.status1).trigger('change');
                 tds.eq(2).children('select').attr('class','publicrecord review_type');
               }
               
               if(creditor_bereau != ""){
                 tds.eq(3).append(html);
                 tds.eq(3).children('select').val(types.status2).trigger('change');
                 tds.eq(3).children('select').attr('class','publicrecord review_type');
               }
           });
        }
    });
    
}

function getSelectedPublicInfo(){
    var dataObj = [];
    var html = "<select style=\"width: 120px;\n" +"padding: 0px;color: black;\n" +"height: 19px;\n" +"    margin: 0px;\n" +"    float: right;\" name=\"source_type\"  class=\"review_type\"><option value=\"3\">update</option><option value=\"1\">negative</option><option value=\"2\">positive</option></select>";
        
     $('.sub_header').each(function(){
        var pb = $(this).text();
        pb = $.trim(pb);
        if(pb == 'Bankruptcy'){
           var table =  $(this).nextAll('table.rpt_content_table').first();
           table.find('tr').each(function (i, el) {
               var tds = $(this).find('td');
               var text0 = $.trim(tds.eq(0).text());
               var text1 = $.trim(tds.eq(1).text().replace('updatenegativepositive',''));
               var text1 = $.trim(tds.eq(1).text().replace('updatenegativepositive',''));
               var text1 = $.trim(tds.eq(1).text().replace('Please selectnegativepositive',''));
               var text2 = $.trim(tds.eq(2).text().replace('updatenegativepositive',''));
               var text2 = $.trim(tds.eq(2).text().replace('Please selectnegativepositive',''));
               var text3 = $.trim(tds.eq(3).text().replace('updatenegativepositive',''));
               var text3 = $.trim(tds.eq(3).text().replace('Please selectnegativepositive',''));
               var type1 =""
               var type2 =""
               var type3 =""
              
               //alert(text1+"==="+text2+"===="+text3);
               if(tds.eq(1).children('select').val() != null){
                   var type1 = tds.eq(1).children('select').val();
               }
               
               if(tds.eq(2).children('select').val() != null){
                   var type2 = tds.eq(2).children('select').val();
               }
               
               if(tds.eq(3).children('select').val() != null){
                   var type3 = tds.eq(3).children('select').val();
               }
              var  obj = {
                        'TransUnion':{data:text1,'type':type1,'key_name':text0.replace(':','')},
                        'Experian':{data:text2,'type':type2,'key_name':text0.replace(':','')},
                        'Equifax':{data:text3,'type':type3,'key_name':text0.replace(':','')}
                    }
                   
                 dataObj.push(obj);
           });
           
           return false;
        }
        
    });
//    console.log('dataObj Public')
//    console.log(dataObj);
    return dataObj;
}

function getSelectedInquiries(){
    var dataObj = [];
    var html = "<select style=\"width: 120px;\n" +"padding: 0px;color: black;\n" +"height: 19px;\n" +"    margin: 0px;\n" +"    float: right;\" name=\"source_type\"  class=\"review_type\"><option value=\"3\">update</option><option value=\"1\">negative</option><option value=\"2\">positive</option></select>";
        
    $('.rpt_fullReport_header').each(function(){
        var inq = $(this).clone()    //clone the element
                        .children() //select all the children
                        .remove()   //remove all the children
                        .end()  //again go back to selected element
                        .text();
	
        inq = $.trim(inq);
	
        if(inq == 'Inquiries'){
           var table =  $(this).nextAll('table.rpt_content_table').first();
           table.find('tr').each(function (i, el) {

               var tds = $(this).find('td');
               var creditor_name = $.trim(tds.eq(0).text());
               var date_of_inq = $.trim(tds.eq(2).text());
               var creditor_bereau = tds.eq(3).text();
               creditor_bereau = creditor_bereau.replace('updatenegativepositive','');
               creditor_bereau = $.trim(creditor_bereau);
               if(creditor_name != "" || date_of_inq != "" || creditor_bereau != ""){
                   var type = tds.eq(3).children('select').val();
                   if(type == ""){
                       type = 3;
                   }
                 var obj = {
                     'key_name':creditor_name+"_"+date_of_inq,'creditor_bereau':creditor_bereau,'type':type
                 };
                 dataObj.push(obj);
               }
           });
           
           return false;
        }
        
    });
    
    return dataObj;
}

function setDropDownPublicRecordPrivacyGuard(html,data){

var table = $("div:contains('Public Records')").nextAll('table.FtgCrContentRowTable').first();
table.find('tr').each(function (i, el) {
    var tds = $(this).find('td');
    var innerTable = tds.eq(0).children('table');
    
    tds.each(function(j,ele){
        $(this).children('select').remove();
        
         if(!$(this).is(':empty')){
             console.log($(this));
            $(this).append(html.replace('float: right;',''));
            $(this).children('select').attr('data-category',j);
            $(this).children('select').attr('class','public-records review_type');
        }
    });
    
});
}


function getDropDownPublicRecordPrivacyGuard(){
var dataObj = [];
var table = $("div:contains('Public Records')").nextAll('table.FtgCrContentRowTable').first();
table.find('tr').each(function (i, el) {
    if(i > 2){
    var tds = $(this).find('td');
    var th = $(this).find('th');
    var key_name =  $.trim(th.text());
    var text1 = "";
    var text2 = "";
    var text3 = "";
     var type1 = "";
     var type2 = "";
     var type3 = "";
    if(!tds.eq(0).is(':empty')){
         text1 = tds.eq(0).clone().children().remove().end().text();
         type1 = tds.eq(0).children('select').val();
    }
    if(!tds.eq(1).is(':empty')){
         text2 = tds.eq(1).clone().children().remove().end().text();
         type2 = tds.eq(1).children('select').val();
    }
     if(!tds.eq(2).is(':empty')){
         text3 = tds.eq(2).clone().children().remove().end().text();
         type3 = tds.eq(2).children('select').val();
     }
    
    var  obj = {
            'Experian':{data:text1,'type':type1,'key_name':key_name.replace(':','')},
            'Equifax':{data:text2,'type':type2,'key_name':key_name.replace(':','')},
            'TransUnion':{data:text3,'type':type3,'key_name':key_name.replace(':','')}
        }
    dataObj.push(obj);    
    }   
    
    
});

console.log(dataObj);
return dataObj;

}


function setDropDownInquiryPrivacyGuard(html,data){

var table = $("div:contains('Credit Inquiries')").nextAll('table.FtgCrContentRowTable').first();
table.find('tr').each(function (i, el) {
    var tds = $(this).find('td');
    var innerTable = tds.eq(0).children('table');
    
    tds.next('td.BlueLargeCheck').each(function(j,ele){
        $(this).children('div').children('select').remove();
         if(!$(this).is(':empty')){
            $(this).children('div').append(html.replace('float: right;',''));
            $(this).children('div').children('select').attr('data-category',j);
            $(this).children('div').children('select').attr('class','inquries review_type');
        }
    });
    
});
setDropDownPublicRecordPrivacyGuard(html,data);
}


function getDropDownInquiryPrivacyGuard(){

var table = $("div:contains('Credit Inquiries')").nextAll('table.FtgCrContentRowTable').first();
var mainObjArray = [];
table.find('tr').each(function (i, el) {
    var tds = $(this).find('td');
    //var innerTable = tds.eq(0).children('table');
    var experian = 3
    var equifax = 3;
    var transunion = 3;

    if(i > 0){
        
   var innerTable = $(this).children('td').has('table');
   var dataTable = $(this).children('td').children('table');
   var innerTrs = innerTable.find('tr');
   var innerThs =innerTrs.eq(0).children('th');
        var innerObjTempArray = [];
        innerTable.find('tr').each(function (i, el) {
            var innerThs = $(this).find('th');
            var innerTds = $(this).find('td');
            var key_name = $.trim(innerThs.text());
            var innerTrs = innerThs.parent().next('tr').next('tr');
            var innerObjArray = [];
            innerTrs.each(function(i,el){
                var everyTds = $(this).children('td');
                 if(key_name!=""){
                   var parentTr = dataTable.parent('td').parent('tr');
                   var typeInfo = "";
                   var typeName = "";
                   parentTr.find('td.BlueLargeCheck').each(function(){

                        if($(this).children('div').has('select').length){
                            typeInfo = $(this).children('div').children('select').attr('id','pinq_'+i);
                            return false;
                        }
                   });

                   if(typeInfo.data('category') == 0){
                       typeName = "Experian";
                   }
                   else if(typeInfo.data('category') == 1){
                       typeName = "Equifax";
                   }
                   else if(typeInfo.data('category') == 2){
                       typeName = "TransUnion";
                   }
                   
                   mainObjArray.push({'key_name':key_name+"_"+$.trim(everyTds.eq(0).text()),'creditor_bereau':typeName,'type':typeInfo.val()});
               }
            })
        
     })

    }
});

return mainObjArray;
}

function selectAllPersonalinfoDropdown(elem){
        var value = $(elem).val();
        var className = $(elem).data('key');
        var r = confirm("Are you  sure to change all items?");
        if(r){
          $('.'+className).val(value).trigger('change');
        }
}


function allSelectDropDown(key){
        var string= "<select data-key='"+key+"' onchange=\"selectAllPersonalinfoDropdown(this)\" style=\"width: auto;\n" +"    padding: 0px;color: black;\n" +"    height: 19px;\n" +"    margin: 0px;\n" +"    float: right;\" name=\"source_type\"  class=\"review_type\"><option value=\"3\">Reset all items</option><option value=\"1\">Set all items negative</option><option value=\"2\">Set all items positive</option></select>";
        return string;
}
// create personal info top right dropdown.
function pinfoDropDown(key){
        var string= "<select data-key='"+key+"' onchange=\"selectAllPersonalinfoDropdown(this)\" style=\"width: auto;\n" +"    padding: 0px;color: black;\n" +"    height: 19px;\n" +"    margin: 0px;\n" +"    float: right;\" name=\"source_type\"  class=\"review_type\"><option value=\"3\">Send Personal Information to Both EVcrm Attack and Audit Report</option><option value=\"1\">Send Personal Information to Only EVcrm Attack Report</option><option value=\"2\">Send Personal Information to Only EVcrm Audit Report</option></select>";
        return string;
}


function setAllGlobalDropdown(){
    $('.rpt_fullReport_header').each(function(){
        var currentText = $(this).text();
        currentText = $.trim(currentText);
        if(currentText == 'Personal Information'){
            $(this).append(pinfoDropDown('all-select'))
        }

        if(currentText == 'Public Information'){
        $(this).append(allSelectDropDown('publicrecord'));
        //return false;
        }
        
        if(currentText == 'Account History'){
            $(this).append(allSelectDropDown('banks'));
        //return false;
        }
    });
    
    $('.FtgFullReportHeader').each(function(){
        var currentText = $.trim($(this).text());
        if(currentText == 'Personal Profile'){
            $(this).append(allSelectDropDown('all-select'))
        }
        
        if(currentText == 'Credit Inquiries'){
            $(this).append(allSelectDropDown('inquries'))
        }
        
        if(currentText == 'Public Records'){
            $(this).append(allSelectDropDown('public-records'))
        }
        
        if(currentText == 'Account History'){
            $(this).append(allSelectDropDown('banks'))
        }
    });
    
    
}

