<?php
    session_start();
    include 'config.php';
    // var_dump($_SESSION);die;

    if(!$_SESSION)
    {
        header("Location: index.php");
    }
    else
    {
        $uid = $_SESSION['userid'];
        $uname = $_SESSION['uname'];
        $regime_type = $_SESSION['regime_type'];
        $cargo_channel = $_SESSION['cargo_channel'];
        $company_code = $_SESSION['company_code'];
    }   
    
    if(isset($_POST['countries']) && $_POST['countries'] == 1)
    {
        $sql_stmt = "SELECT code,description FROM country_master ";
        $result = mysqli_query($con,$sql_stmt);
        while ($row = $result->fetch_assoc()) 
        {
            $data[] = $row;
        }
        echo json_encode($data);
        exit();
    }

    if(isset($_POST['manuCountry']) && $_POST['manuCountry'] == 1)
    {
        $sql_stmt = "SELECT description,code FROM country_master ";
        $result = mysqli_query($con,$sql_stmt);
        while ($row = $result->fetch_assoc()) 
        {
            $data[] = $row;
        }
        echo json_encode($data);
        exit();
    }

    if(isset($_POST['currency']) && $_POST['currency'] == 1)
    {
        $sql_stmt = "SELECT code FROM currency_master";
        $result = mysqli_query($con,$sql_stmt);
        while ($row = $result->fetch_assoc()) 
        {
            $data[] = $row;
        }
        echo json_encode($data);
        exit();
    }

    if(isset($_POST['ins']) && $_POST['ins'] == 1)
    {
        $sql_stmt = "SELECT code FROM inco_terms where ins = 'Y' ";
        $result = mysqli_query($con,$sql_stmt);
        while ($row = $result->fetch_assoc()) 
        {
            $data[] = $row;
        }
        echo json_encode($data);
        exit();
    }

    if(isset($_POST['fri']) && $_POST['fri'] == 1)
    {
        $sql_stmt = "SELECT code FROM inco_terms where fri = 'Y' ";
        $result = mysqli_query($con,$sql_stmt);
        while ($row = $result->fetch_assoc()) 
        {
            $data[] = $row;
        }
        echo json_encode($data);
        exit();
    }

    if(isset($_POST['inco']) && $_POST['inco'] == 1)
    {
        $sql_stmt = "SELECT code FROM inco_terms ";
        $result = mysqli_query($con,$sql_stmt);
        while ($row = $result->fetch_assoc()) 
        {
            $data[] = $row;
        }
        echo json_encode($data);
        exit();
    }
?>
<!DOCTYPE html>
<html>
    <head>
	    <link href="css/style-inn.css" rel="stylesheet">
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
        <link href="https://cdn.datatables.net/1.10.22/css/dataTables.bootstrap4.min.css" rel="stylesheet" id="bootstrap-css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.22/js/dataTables.bootstrap4.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
        <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
        <title><?php echo $uname ; ?></title>
        <style>
            body 
            {
                margin: 0;
                padding: 0;
                background-color: #9c9c9c00;
                height: 100vh;
                font-size: x-small;                
            }
            .form-control
            {
                font-size: larger;  
                padding: .075rem .75rem !important;  
                border: 1px solid #bbbbbb;
                border-radius: 0.05rem;				
            }
            .btn.disabled, .btn:disabled 
            {
                opacity: 0.45;
            }
			.form-control:disabled, .form-control[readonly]{background-color: #fbfbfb;}
			.form-group p 
            {
                margin-top: 0;
                margin-bottom: 5px;
            }
            button[disabled]
            {
                border: 1px solid #999999;
                background-color: #cccccc;
                color: #666666;
            }
            .form-group 
            {
                margin-bottom: 0px;
            }
			input:disabled
            {
                background-color: grey;
            }  
            .table td, .table th 
            {
                padding: 3px;
                line-height: 16px;
            }            
            table,th,td
            {
                border: solid 1px #DDD;
                border-collapse: collapse;
                padding: 2px 3px;
                text-align: center;
            }
            
            th 
            {
                font-weight: bold;
            }
            .disstyle
            {
                text-align: initial;
                font-weight: 600;
                display: flex;
            }
            .modal-header .close
            {
                margin-top: -24px;
            }
            .modal-header
            {
                padding-bottom: 20px;
                border-bottom: 1px solid #e5e5e5;
                padding-top: 0px;
            }
            hr
            {
                margin-top: 20px;
                margin-bottom: 20px;
                border: 1px solid #204d74;
                border-top: 1px solid #204d74;
            }
            a
            {     
                display: inline-block;     
                position: relative;    
                z-index: 1;     
                padding: 2em;     
                margin: -2em; 
            }
            .red
            {
                background-color : #fa9393;
            }            
            footer 
            {
                position: fixed;
                left: 0;
                bottom: 0;
                width: 100%;
				z-index: 99;
				padding: 7px;
                background-color: #49525b;
                /* #6c757d   ; */
                color: white;
                text-align: center;
            }
            footer a{color:#fff !important;}
            input[type="number"]::-webkit-outer-spin-button, 
            input[type="number"]::-webkit-inner-spin-button
            {
                -webkit-appearance: none;
                margin: 0;
            }
            input[type="number"] 
            {
                -moz-appearance: textfield;
            }
            .ajax-loader
            {
                visibility: hidden;
                background-color: rgba(255,255,255,0.7);
                position: absolute;
                z-index: +100 !important;
                width: 95%;
                height:100%;
            }                
            .ajax-loader img
            {
                position: relative;
                top:50%;
                left:50%;
            }			
			@media (min-width: 768px)
            {
                .col-md-7 .col-md-4
                {
                    padding-left: 5px;
                    padding-right: 5px;
                }
                .buttn .col-md-2
                {
                    padding-left: 7px;
                    padding-right: 7px;
                }
            }
        </style>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script>    
        <script>
            var json_object = {};
            var col = [];
            var countries = [];
            var currency = [];
            var manuCountry = [];
            var inco = [];
            var ins = [];
            var fri = [];
            var ExcelToJSON = function()
            {
                this.parseExcel = function(file)
                {
                    var reader = new FileReader();
        
                    reader.onload = function(e)
                    {
                        var data = e.target.result;
                        var workbook = XLSX.read(data, 
                        {
                            type: 'binary'
                        });
                        workbook.SheetNames.forEach(function(sheetName)
                        {
                            // Here is your object
                            var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName],{ header: 1});//,{}
                            json_object = XL_row_object;
                            document.getElementById("shipment").value = json_object.length;
                        })
                    };
                    reader.onerror = function(ex)
                    {
                        console.log(ex);
                    };
                    reader.readAsBinaryString(file);
                };
            };
    
            function handleFileSelect(evt) 
            {
                var files = evt.target.files; // FileList object
                var xl2json = new ExcelToJSON();
                xl2json.parseExcel(files[0]);
            }
        
            function CreateTableFromJSON() 
            {
                for (var i = 0; i < json_object.length; i++)
                {
                    for (var key in json_object[i]) 
                    {
                        if (col.indexOf(key) === -1) 
                        {
                            // console.log(key);
                            col.push(key);
                        }
                    }
                }
        
                // CREATE DYNAMIC TABLE.
                var table = document.createElement("table");
                table.className = "table table-striped table-bordered";
                table.style.width = '3250px';
                table.setAttribute('id', 'example');
        
                // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.
                var tr = table.insertRow(-1); // TABLE ROW.
        
                // for (var i = 0; i < col.length; i++)
                // { 
                //     console.log(col[i]);
                //     var th = document.createElement("th"); // TABLE HEADER.
                //     th.innerHTML = col[i];
                //     tr.appendChild(th);
                // }
                var regime = document.getElementById("regime_type").value;

                // ADD JSON DATA TO THE TABLE AS ROWS.
                for (var i = 0; i < json_object.length; i++)
                {
                    tr = table.insertRow(-1);
                    if(i == 0)
                    {
                        for (var j = 0; j < col.length; j++)
                        {                           
                            var th = document.createElement("th"); 
                            th.innerHTML = json_object[i][col[j]];
                            tr.appendChild(th);
                        }
                    }
                    else
                    {
                        for (var j = 0; j < col.length; j++)
                        {
                            tr.className = i;  
                            var tabCell = tr.insertCell(-1);
                            if(j == 0)   
                            {
                                var btn = document.createElement('input');
                                btn.type = "button";
                                btn.className = "btn-modal";
                                btn.value = json_object[i][col[j]];
                                tabCell.appendChild(btn);
                            }                            
                            else
                            {
                                if(json_object[i][col[j]] === undefined)
                                {
                                    if(j == 1)
                                    {
                                        var val  = IsValidCurrencyNumber(json_object[i][col[1]]);
                                        if(val == 0)
                                        {
                                            tabCell.className = "invalid";
                                            tabCell.innerHTML = json_object[i][col[1]]; 
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = json_object[i][col[1]]; 
                                        }
                                    }
                                    if(j == 11)
                                    {
                                        var val  = IsValidCurrencyNumber(json_object[i][col[11]]);
                                        if(val == 0)
                                        {
                                            tabCell.className = "invalid";
                                            tabCell.innerHTML = json_object[i][col[11]]; 
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = json_object[i][col[11]]; 
                                        }
                                    }
                                    if(j == 12)
                                    {
                                        var val  = IsValidCurrencyNumber(json_object[i][col[12]]);
                                        if(val == 0)
                                        {
                                            tabCell.className = "invalid";
                                            tabCell.innerHTML = json_object[i][col[12]]; 
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = json_object[i][col[12]]; 
                                        }
                                    }                                    
                                    if(j == 15)
                                    {
                                        var val  = IsValidCurrencyNumber(json_object[i][col[15]]);
                                        if(val == 0)
                                        {
                                            tabCell.className = "invalid";
                                            tabCell.innerHTML = json_object[i][col[15]]; 
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = json_object[i][col[15]]; 
                                        }
                                    }
                                    if(j == 3)
                                    {
                                        if(json_object[i][col[3]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = removeSpecials(json_object[i][col[3]]); 
                                        }
                                    }
                                    if(j == 4)
                                    {
                                        if(json_object[i][col[4]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = removeSpecials(json_object[i][col[4]]); 
                                        }
                                    }
                                    if( j == 14 )
                                    {
                                        if(json_object[i][col[14]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                    }
                                    if( j == 16 )
                                    {
                                        if(json_object[i][col[16]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                    }
                                    if(j == 5)
                                    {
                                        if(json_object[i][col[5]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = removeSpecials(json_object[i][col[5]]); 
                                        }
                                    }
                                    if(j == 6 )
                                    {
                                        if(json_object[i][col[6]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = removeSpecials(json_object[i][col[6]]); 
                                        }
                                    }
                                    if(j == 10)
                                    {
                                        if(json_object[i][col[10]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = removeSpecials(json_object[i][col[10]]); 
                                        }
                                    }
                                    if( j == 9)
                                    {
                                        tabCell.innerHTML = removeSpecialsForNumber(json_object[i][col[j]]); 
                                    }
                                    if(j == 2 || j == 7 )
                                    {
                                        tabCell.innerHTML = removeSpecials(json_object[i][col[j]]); 
                                    }
                                    if(j == 8)
                                    {
                                        tabCell.innerHTML = formatDate(json_object[i][col[j]]);                                        
                                    }
                                    if( j == 13 )
                                    {
                                        var check = checkManuCountry(removeSpecials(json_object[i][col[13]]));
                                        if(check < 1)
                                        {
                                            tabCell.innerHTML = "";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = capitalize(getCountry(removeSpecials(json_object[i][col[13]]))); 
                                        }
                                    }
                                    if( j == 14 )
                                    {
                                        var check = checkCurrency(removeSpecials(json_object[i][col[14]]));
                                        if(check < 1)
                                        {
                                            tr.style.backgroundColor = "#fa9393";
                                            tabCell.className = "invalid";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = capitalize(removeSpecials(json_object[i][col[14]])); 
                                        }
                                    }
                                    tabCell.innerHTML = ""; 
                                    if(regime == 1 && json_object[i][col[16]] != "")
                                    {
                                        if(j == 17)
                                        {
                                            var ins = incoForFright(removeSpecials(json_object[i][col[16]]));                                            
                                            if( ins == 1 && json_object[i][col[17]] === undefined)
                                            {
                                                tabCell.className = "invalid";
                                                tr.style.backgroundColor = "#fa9393";
                                                // tabCell.innerHTML = "";
                                            }                                        
                                        }
                                        if(j == 18)
                                        {
                                            var ins =  incoForFright(removeSpecials(json_object[i][col[16]]));                                            
                                            if( ins == 1 && json_object[i][col[18]] === undefined)
                                            {
                                                tabCell.className = "invalid";
                                                tr.style.backgroundColor = "#fa9393";
                                                // tabCell.innerHTML = "";
                                            }                                        
                                        }
                                        if(j == 19)
                                        {
                                            if(json_object[i][col[16]] != "")
                                            {
                                                var fri = incoForInsurance(removeSpecials(json_object[i][col[16]]));
                                                if( fri == 1 && json_object[i][col[19]] === undefined )
                                                {
                                                    tabCell.className = "invalid";
                                                    tr.style.backgroundColor = "#fa9393";
                                                    // tabCell.innerHTML = "";
                                                }  
                                            }                                                                                  
                                        }
                                        if(j == 20)
                                        {
                                            if(json_object[i][col[16]] != "")
                                            {
                                                var fri = incoForInsurance(removeSpecials(json_object[i][col[16]]));
                                                if( fri == 1 && json_object[i][col[20]] === undefined )
                                                {
                                                    tabCell.className = "invalid";
                                                    tr.style.backgroundColor = "#fa9393";
                                                    // tabCell.innerHTML = "";
                                                }  

                                            }                                                                                  
                                        }
                                    }
                                    else
                                    {
                                        if( j == 16 )
                                        {
                                            var check = checkInco(removeSpecials(json_object[i][col[16]]));
                                            if(check < 1)
                                            {
                                                // tabCell.innerHTML = "";
                                                tr.style.backgroundColor = "#fa9393";
                                                tabCell.className = "invalid";
                                            }
                                            else
                                            {
                                                tabCell.innerHTML = capitalize(removeSpecials(json_object[i][col[j]])); 
                                            }
                                        }                                        
                                    }
                                }
                                else
                                {                     
                                    //tabCell.innerHTML = removeSpecials(json_object[i][col[j]]);               
                                    if(j == 1)
                                    {
                                        var val  = IsValidCurrencyNumber(json_object[i][col[1]]);
                                        if(val == 0)
                                        {
                                            tabCell.className = "invalid";
                                            tabCell.innerHTML = json_object[i][col[1]]; 
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = json_object[i][col[1]]; 
                                        }
                                    }
                                    if(j == 11)
                                    {
                                        var val  = IsValidCurrencyNumber(json_object[i][col[11]]);
                                        if(val == 0)
                                        {
                                            tabCell.className = "invalid";
                                            tabCell.innerHTML = json_object[i][col[11]]; 
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = json_object[i][col[11]]; 
                                        }
                                    }
                                    if(j == 12)
                                    {
                                        var val  = IsValidCurrencyNumber(json_object[i][col[12]]);
                                        if(val == 0)
                                        {
                                            tabCell.className = "invalid";
                                            tabCell.innerHTML = json_object[i][col[12]]; 
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = json_object[i][col[12]]; 
                                        }
                                    }
                                    if(j == 15)
                                    {
                                        var val  = IsValidCurrencyNumber(json_object[i][col[15]]);
                                        if(val == 0)
                                        {
                                            tabCell.className = "invalid";
                                            tabCell.innerHTML = json_object[i][col[15]]; 
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = json_object[i][col[15]]; 
                                        }
                                    }
                                    if(j == 3)
                                    {
                                        if(json_object[i][col[3]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = removeSpecials(json_object[i][col[3]]); 
                                        }
                                    }
                                    if(j == 4)
                                    {
                                        if(json_object[i][col[4]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = removeSpecials(json_object[i][col[4]]); 
                                        }
                                    }
                                    if(j == 5)
                                    {
                                        if(json_object[i][col[5]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = removeSpecials(json_object[i][col[5]]); 
                                        }
                                    }
                                    if(j == 6 )
                                    {
                                        if(json_object[i][col[6]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = removeSpecials(json_object[i][col[6]]); 
                                        }
                                    }
                                    if(j == 10)
                                    {
                                        if(json_object[i][col[10]] === undefined)
                                        {
                                            tabCell.className = "invalid";
                                            tr.style.backgroundColor = "#fa9393";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = removeSpecials(json_object[i][col[10]]); 
                                        }
                                    }
                                    if( j == 9)
                                    {
                                        tabCell.innerHTML = removeSpecialsForNumber(json_object[i][col[j]]); 
                                    }
                                    if(j == 2 || j == 7 )
                                    {
                                        tabCell.innerHTML = removeSpecials(json_object[i][col[j]]); 
                                    }
                                    if(j == 8)
                                    {
                                        tabCell.innerHTML = formatDate(json_object[i][col[j]]);                                        
                                    }
                                    if( j == 13 )
                                    {
                                        var check = checkManuCountry(removeSpecials(json_object[i][col[13]]));
                                        if(check < 1)
                                        {
                                            tabCell.innerHTML = "";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = capitalize(getCountry(removeSpecials(json_object[i][col[13]]))); 
                                        }
                                    }
                                    if( j == 14 )
                                    {
                                        var check = checkCurrency(removeSpecials(json_object[i][col[14]]));
                                        if(check < 1)
                                        {
                                            tr.style.backgroundColor = "#fa9393";
                                            tabCell.className = "invalid";
                                        }
                                        else
                                        {
                                            tabCell.innerHTML = capitalize(removeSpecials(json_object[i][col[14]])); 
                                        }
                                    }
                                    if(regime == 1)
                                    {
                                        if( j == 16 )
                                        {
                                            var check = checkInco(removeSpecials(json_object[i][col[16]]));
                                            if(check < 1)
                                            {
                                                tr.style.backgroundColor = "#fa9393";
                                                // tabCell.innerHTML = "";
                                                tabCell.className = "invalid";
                                            }
                                            else 
                                            {
                                                // if(json_object[i][col[16]] != "")
                                                // {
                                                //     tabCell.className = "invalid";
                                                // }
                                                // else
                                                // {
                                                    tabCell.innerHTML = capitalize(removeSpecials(json_object[i][col[16]]));                                                 
                                                // }

                                            }
                                        }
                                        if( j == 17)
                                        {
                                            var ins = incoForFright(removeSpecials(json_object[i][col[16]]));
                                            if(ins == 0)
                                            {
                                                // tabCell.className = "invalid";
                                                tabCell.innerHTML = "";
                                                // tr.style.backgroundColor = "#fa9393";
                                            }
                                            else
                                            {
                                                tabCell.innerHTML = capitalize(removeSpecials(json_object[i][col[17]])); 
                                            }
                                        }
                                        if(j == 18)
                                        {
                                            var ins =  incoForFright(removeSpecials(json_object[i][col[16]]));
                                            if(ins == 0)
                                            {
                                                // tabCell.className = "invalid";
                                                tabCell.innerHTML = "";
                                                // tr.style.backgroundColor = "#fa9393";
                                            }
                                            else
                                            {
                                                var val = IsValidCurrencyNumber(json_object[i][col[18]]); 
                                                if(val == 0)
                                                {
                                                    tabCell.className = "invalid";
                                                    tabCell.innerHTML = json_object[i][col[18]]; 
                                                    tr.style.backgroundColor = "#fa9393";
                                                }
                                                else
                                                {
                                                    tabCell.innerHTML = json_object[i][col[18]]; 
                                                }
                                            }
                                        }
                                        if( j == 19)
                                        {      
                                            if(json_object[i][col[16]] != "")
                                            {
                                                var fri = incoForInsurance(removeSpecials(json_object[i][col[16]]));                                
                                                if(fri == 0)
                                                {
                                                    // tabCell.className = "invalid";
                                                    tabCell.innerHTML = "";
                                                    // tr.style.backgroundColor = "#fa9393";
                                                }
                                                else
                                                {
                                                    tabCell.innerHTML = capitalize(removeSpecials(json_object[i][col[19]])); 
                                                }
                                            }                                           
                                        }
                                        if(j == 20)
                                        {      
                                            if(json_object[i][col[16]] != "")
                                            {
                                                var fri = incoForInsurance(removeSpecials(json_object[i][col[16]]));                                
                                                if(fri == 0)
                                                {
                                                    // tabCell.className = "invalid";
                                                    tabCell.innerHTML = "";
                                                    // tr.style.backgroundColor = "#fa9393";
                                                }
                                                else
                                                {
                                                    var val = IsValidCurrencyNumber(json_object[i][col[20]]); 
                                                    if(val == 0)
                                                    {
                                                        tabCell.className = "invalid";
                                                        tabCell.innerHTML = json_object[i][col[20]]; 
                                                        tr.style.backgroundColor = "#fa9393";
                                                    }
                                                    else
                                                    {
                                                        tabCell.innerHTML = json_object[i][col[20]]; 
                                                    }
                                                }
                                            }                                            
                                        }
                                    }
                                    else
                                    {
                                        if( j == 16 )
                                        {
                                            var check = checkInco(removeSpecials(json_object[i][col[16]]));
                                            if(check < 1)
                                            {
                                                tr.style.backgroundColor = "#fa9393";
                                                tabCell.className = "invalid";
                                                // tabCell.innerHTML = "";
                                            }
                                            else
                                            {
                                                tabCell.innerHTML = capitalize(removeSpecials(json_object[i][col[j]])); 
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }                
                }        
                // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
                var divContainer = document.getElementById("showData");
                divContainer.innerHTML = "";
                divContainer.appendChild(table);
                new_table();
            }

            function getAllIndexes(arr, val) 
            { 
                var indexes = [], i = -1 , count = 0;
                while ((i = arr.indexOf(val, i+1)) != -1)
                {
                    count++;
                }
                return count;
            }

            function getindexes(arr,val)
            {
                var first = false;
                var index = 0; var indx = [];
                for(var i = 0; i< arr.length;i++)
                {
                    if(arr[i] == val)
                    {
                        if(first==true)
                        {
                            index = i;
                            indx.push(index);
                        }
                        first = true;
                    }    
                }
                return indx;
            }

            function removeSpecialsForNumber(str)
            {
                if(str != undefined)
                {
                    var res = str.replace(/([^\d]*)(\d*(\.\d{0,2})?)(.*)/, '$2');//( /[^\w.]|_/g, " ")
                }
                return res;
            }

            function IsValidCurrencyNumber(value)
            {
                if(value != null){
                    var reg = new RegExp(/^\d*\.?\d*$/);
                    if(value.match(reg)==null)
                        return 0;
                    else
                        return 1;
                }
                else
                    return 0;
            }

            function removeSpecials(str)
            {
                if(str != undefined)
                {
                    var res = str.replace(/\W+/g, " ");
                }
                return res;
            }

            function capitalize(string)
            {
                if( string != undefined)
                {
                    var upper = string.toUpperCase();
                    return upper;
                }
            }              

            function checkInco(term)
            {
                if( term != undefined)
                {
                    var upper = term.toUpperCase();
                    if(inco.indexOf(upper) !== -1)
                    {
                        return 1;
                    } 
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }

            function incoForInsurance(term)
            {
                if( term != undefined)
                {
                    // alert(ins);
                    var upper = term.toUpperCase();
                    if(ins.indexOf(upper) !== -1)
                    {
                        // alert('here');
                        return 1;
                    } 
                    else
                    {
                        return 0;
                    }
                }
                
            }

            function incoForFright(term)
            {
                // alert(fri);
                if( term != undefined)
                {
                    var upper = term.toUpperCase();
                    if(fri.indexOf(upper) !== -1)
                    {
                        return 1;
                    } 
                    else
                    {
                        return 0;
                    }
                }
            }

            function formatDate(date)
            {
                var d = new Date(date),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear();

                if (month.length < 2) 
                    month = '0' + month;
                if (day.length < 2) 
                    day = '0' + day;

                return [year, month, day].join('-');
            }

            $.ajax
            ({
                type : "post",
                async: false,
                data : { currency : 1 },
                url : "",
                success : function(result)
                {
                    var array = $.parseJSON(result);

                    $.each(array, function(key, value) {
                        currency.push(value.code);
                    });
                }
            });            

            $.ajax
            ({
                type : "post",
                async: false,
                data : { countries : 1 },
                url : "",
                success : function(result)
                {
                    var array = $.parseJSON(result);
                    $.each(array, function(key, value) {
                        countries[value.description] = value.code;
                    });
                }
            });

            $.ajax
            ({
                type : "post",
                async: false,
                data : { manuCountry : 1 },
                url : "",
                success : function(result)
                {
                    var array = $.parseJSON(result);

                    $.each(array, function(key, value) {
                        manuCountry.push(value.code);
                        manuCountry.push(value.description);
                    });
                }
            });

            $.ajax
            ({
                type : "post",
                async: false,
                data : { ins : 1 },
                url : "",
                success : function(result)
                {
                    var array = $.parseJSON(result);
                    $.each(array, function(key, value)
                    {
                        ins.push(value.code);
                    });
                }
            });

            $.ajax
            ({
                type : "post",
                async: false,
                data : { fri : 1 },
                url : "",
                success : function(result)
                {
                    var array = $.parseJSON(result);
                    $.each(array, function(key, value) {
                        fri.push(value.code);
                    });
                }
            });

            $.ajax
            ({
                type : "post",
                async: false,
                data : { inco : 1 },
                url : "",
                success : function(result)
                {
                    var array = $.parseJSON(result);
                    $.each(array, function(key, value) {
                        inco.push(value.code);
                    });
                }
            });

            function getCountry(string)
            {
                if( string != undefined)
                {
                    var upper = string.toUpperCase();
                    if (upper in countries)
                    {
                        return countries[upper];
                    }
                    else
                    {
                        return upper;
                    }
                }
            }   
            
            function checkCurrency(string) 
            {
                if(string != undefined)
                {
                    var upper = string.toUpperCase();
                    if(currency.indexOf(upper) !== -1)
                    {
                        return 1;
                    } 
                    else
                    {
                        return 0;
                    }
                }                
            }
              
            function checkManuCountry(string)
            {
                if( string != undefined)
                {
                    var upper = string.toUpperCase();
                    if(manuCountry.indexOf(upper) !== -1)
                    {
                        return 1;
                    } 
                    else
                    {
                        return 0;
                    }
                }
            }
      
            function new_table()
            {
                var regime = document.getElementById("regime_type").value;
                var cargo_ch = document.getElementById("cargo_ch").value;
                var body = document.getElementsByTagName('body')[0];
                var tbl = document.createElement('table');
                tbl.className = "csv-table";
                tbl.style.width = '100%';
                tbl.setAttribute('border', '1');
                tbl.style.visibility = "hidden"; 
                var tbdy = document.createElement('tbody');
                var arr = [];
                for (var k = 0;  k < json_object.length; k++) 
                {                      
                    arr.push(json_object[k][col[0]]);
                } 
                for (var i = 0; i < json_object.length; i++) 
                { 
                    if(i == 0)
                    {
                        //HDR
                        var tr1 = document.createElement('tr');
                        for (var j = 1; j < 21; j++)
                        {
                            var th = document.createElement("th");  
                            if( j == 1)
                            {
                                th.innerHTML = "HDR";      
                            }
                            if(j == 2)
                            {
                                th.innerHTML = "NEW";  
                            }
                            if(j == 3)
                            {
                                th.innerHTML = "1";  
                            }
                            if( j == 4)
                            {
                                th.innerHTML = document.getElementById("cust_code").value; //"143034647";
                            }
                            if( j == 5)
                            { 
                                th.innerHTML = document.getElementById("business_code").value; //"AE-1049580";                                                                
                            }
                            if( j == 6)
                            {
                                th.innerHTML = document.getElementById("cto").value; //"PR-01518";      
                            }
                            if(j == 7)
                            {
                                th.innerHTML = document.getElementById("shipping_agent").value; 
                            }                            
                            if( j == 8)
                            {
                                
                                th.innerHTML =  document.getElementById("port_of_load").value;   
                                th.className = "p_o_l";                                                             
                            }
                            if( j == 9)
                            { 
                                if(cargo_ch == 7)
                                {
                                    th.innerHTML =  "";  
                                }
                                else
                                {
                                    th.innerHTML =  document.getElementById("port_of_discharge").value;   
                                }
                                th.className = "p_o_d";                                                             
                            }                            
                            if( j == 10)
                            { 
                                th.innerHTML =  json_object.length;                                                                
                            }
                            if(regime == 1)
                            {
                                if( j == 11)
                                {
                                    th.innerHTML = document.getElementById("in_master_doc_no").value;      
                                }
                                if(j == 12)
                                {
                                    th.innerHTML = cargo_ch;                                 
                                }
                                if(cargo_ch == 8)
                                {
                                    if( j == 13)
                                    {
                                        th.innerHTML = document.getElementById("registration_no1").value; 
                                    }                                    
                                }
                                else if(cargo_ch == 7)
                                {
                                    if( j == 14)
                                    { 
                                        th.innerHTML = document.getElementById("carrier_no1").value;                                                                                               
                                    }
                                }
                                if( j == 15)
                                {
                                    th.innerHTML = formatDate(document.getElementById("schedule_date1").value);       
                                }
                            }
                            if( regime == 2)
                            {                                                              
                                if( j == 16)
                                { 
                                    th.innerHTML = document.getElementById("out_master_doc_no").value;                                                                       
                                }
                                if( j == 17)
                                { 
                                    th.innerHTML = cargo_ch;                                                                 
                                }
                                if(cargo_ch == 7)
                                {
                                    if( j == 19)
                                    { 
                                        th.innerHTML = document.getElementById("carrier_no1").value;                                                                                               
                                    }
                                    if( j == 20 )
                                    {
                                        if(document.getElementById("carrier_no1").value != "")
                                        {
                                            th.innerHTML = formatDate(document.getElementById("schedule_date1").value);       
                                        }
                                    }
                                } 
                            } 
                            tr1.appendChild(th);                           
                        }
                        tbdy.appendChild(tr1);
                    }
                    else
                    {
                        var multiline = getindexes(arr,json_object[i][col[0]]);
                        if( multiline.includes(i) === false)
                        {
                            //DLD
                            var tr2 = document.createElement('tr');
                            for (var j = 1; j < 41; j++)
                            {
                                tr2.className = i;
                                var td1 = document.createElement("td");  
                                if( j == 1)
                                {
                                    td1.innerHTML = "DLD";      
                                }
                                if(j == 3)
                                {
                                    if( json_object[i][col[2]] === undefined)
                                    {
                                        td1.innerHTML =  "";                                    
                                    }
                                    else
                                    {
                                        td1.innerHTML = removeSpecials(json_object[i][col[2]]); 
                                    }
                                    td1.className = "dec_ref_no"; 
                                }
                                if( j == 4)
                                { 
                                    td1.innerHTML = regime;                                                                
                                }  
                                if( j == 8)
                                { 
                                    td1.innerHTML = "LV";                                                                
                                }             
                                if(regime == 2)
                                {
                                    if( j == 5)
                                    { 
                                        td1.innerHTML = "214";                                                                
                                    } 
                                    if(j == 16)
                                    {
                                        td1.innerHTML = removeSpecials(json_object[i][col[3]]);  
                                    }
                                    if(j == 17)
                                    {
                                        td1.innerHTML = removeSpecials(json_object[i][col[4]]);  
                                    }
                                    if(j == 19)
                                    {
                                        td1.innerHTML = removeSpecials(json_object[i][col[5]]);  
                                    }
                                    if(j == 20)
                                    {
                                        td1.innerHTML = removeSpecials(json_object[i][col[6]]);  
                                    } 
                                }
                                if(regime == 1)
                                {
                                    if( j == 5)
                                    { 
                                        td1.innerHTML = "114";                                                                
                                    }
                                    if(j == 26)
                                    {
                                        td1.innerHTML = removeSpecials(json_object[i][col[3]]);  
                                    }
                                    if(j == 27)
                                    {
                                        td1.innerHTML = removeSpecials(json_object[i][col[4]]);  
                                    }
                                    if(j == 29)
                                    {
                                        td1.innerHTML = removeSpecials(json_object[i][col[5]]);  
                                    }
                                    if(j == 30)
                                    {
                                        td1.innerHTML = removeSpecials(json_object[i][col[6]]);  
                                    } 
                                }   
                            
                                tr2.appendChild(td1);    
                            }  
                            tbdy.appendChild(tr2);

                            //PYD
                            var tr7 = document.createElement('tr');
                            tr7.className = i;
                            for (var j = 57; j < 60; j++)
                            {
                                var td = document.createElement("td");  
                                if(j == 57 )//( j == 60)
                                {
                                    td.innerHTML = "PYD";
                                }
                                if(j == 58)
                                {
                                    td.innerHTML = "1";
                                }
                                if(j == 59)
                                {
                                    td.innerHTML = document.getElementById("mode_of_payment_1").value ;
                                }
                                tr7.appendChild(td);
                            }
                            tbdy.appendChild(tr7);

                            //TDD
                            var tr11 = document.createElement('tr');
                            tr11.className = i;
                            for (var j = 63; j < 73; j++)
                            {
                                var td = document.createElement("td"); 
                                if(j == 63)
                                {
                                    td.innerHTML = "TDD";
                                } 
                                if(regime == 1)
                                {
                                    if(j == 64)
                                    {
                                        td.innerHTML = removeSpecials(json_object[i][col[0]]);  
                                    } 
                                } 
                                if(regime == 2)
                                {
                                    if(j == 65)
                                    {
                                        td.innerHTML = removeSpecials(json_object[i][col[0]]);  
                                    } 
                                } 
                                if(j == 66)
                                {
                                    td.innerHTML = "1"; //document.getElementById("cargo_type").value;
                                }                            
                                if(j == 67)
                                {
                                    td.innerHTML = "kg";
                                } 
                                if(j == 68)
                                {
                                    td.innerHTML = removeSpecialsForNumber(json_object[i][col[12]]);  
                                    td.className = "total_gross";
                                } 
                                if(j == 69)
                                {
                                    td.innerHTML = "kg";
                                } 
                                if(j == 70)
                                {
                                    td.innerHTML = removeSpecialsForNumber(json_object[i][col[11]]);                                 
                                    td.className = "total_net";
                                } 
                                if(j == 71)
                                {
                                    td.innerHTML = "";
                                } 
                                if(j == 72)
                                {
                                    td.innerHTML = ""; //json_object[i][col[13]]; 
                                }                             
                                tr11.appendChild(td);
                            }
                            tbdy.appendChild(tr11);

                            //PKD
                            var tr12 = document.createElement('tr');
                            tr12.className = i;
                            for (var j = 73; j < 77; j++)
                            {
                                var td = document.createElement("td"); 
                                if(j == 73)
                                {
                                    td.innerHTML = "PKD";
                                }
                                if(j == 74)
                                {
                                    td.innerHTML = document.getElementById("package_type").value;
                                    td.className = "pack_type";
                                }   
                                if(j == 75)
                                {
                                    td.innerHTML = json_object[i][col[1]]; 
                                    td.className = "no_of_pieces";
                                }                                           
                                tr12.appendChild(td);
                            }
                            tbdy.appendChild(tr12);

                            //INV
                            var tr13 = document.createElement('tr');
                            tr13.className = i;
                            for (var j = 77; j < 92; j++)
                            {
                                var td = document.createElement("td"); 
                                if(j == 77)
                                {
                                    td.innerHTML = "INV";
                                }
                                if(j == 78)
                                {
                                    if( json_object[i][col[7]] === undefined )
                                    {
                                        td.innerHTML =  "";
                                    }
                                    else
                                    {
                                        td.innerHTML =  removeSpecials(json_object[i][col[7]]);
                                    }
                                    td.className = "inv_no";
                                }
                                if(j == 79)
                                {
                                    if( json_object[i][col[8]] === undefined )
                                    {
                                        td.innerHTML =  "";
                                    }
                                    else
                                    {
                                        td.innerHTML =  formatDate(json_object[i][col[8]]);   
                                    }
                                    td.className = "inv_date";
                                }
                                if(j == 80)
                                {
                                    if( json_object[i][col[9]] === undefined )
                                    {
                                        td.innerHTML =  "";
                                    }
                                    else
                                    {
                                        td.innerHTML =  removeSpecialsForNumber(json_object[i][col[9]]);   
                                    }
                                    td.className = "no_of_pack";
                                }
                                if(j == 81)
                                {
                                    td.innerHTML = document.getElementById("invoice_type").value;                                
                                    td.className = "inv_type";
                                }
                                if(j == 83)
                                {
                                    td.className = "pay_instru";
                                }
                                if(j == 85)
                                {
                                    var check = checkCurrency(removeSpecials(json_object[i][col[14]]));
                                    if(check < 1)
                                    {
                                        td.innerHTML =  "";
                                    }
                                    else
                                    {
                                        td.innerHTML = capitalize(removeSpecials(json_object[i][col[14]])); 
                                    }
                                    td.className = "inv_curr";                                
                                }
                                if(j == 86)
                                {
                                    td.innerHTML = removeSpecialsForNumber(json_object[i][col[15]]); 
                                    td.className = "inv_val";
                                }
                                if(j == 87)
                                {
                                    td.className = "inco";
                                    if( json_object[i][col[16]] === undefined )
                                    {
                                        td.innerHTML =  "";
                                    }
                                    else
                                    {
                                        td.innerHTML =  capitalize(removeSpecials(json_object[i][col[16]]));
                                    }
                                }
                                if(regime == 1)
                                {
                                    if(j == 88)
                                    {
                                        if(json_object[i][col[16]] == "FOB" || json_object[i][col[16]] == "EXW" || json_object[i][col[16]] == "FAS" || json_object[i][col[16]] == "FCA"  )
                                        {
                                            if( json_object[i][col[17]] === undefined )
                                            {
                                                td.innerHTML =  "";
                                            }
                                            else
                                            {
                                                td.innerHTML =  capitalize(removeSpecials(json_object[i][col[17]]));
                                            }
                                        }
                                        else if(json_object[i][col[16]] == "CIF" || json_object[i][col[16]] == "CIP" || json_object[i][col[16]] == "DAF" || json_object[i][col[16]] == "DDP" || json_object[i][col[16]] == "DDU" || json_object[i][col[16]] == "DEQ" || json_object[i][col[16]] == "DES" || json_object[i][col[16]] == "DAP" || json_object[i][col[16]] == "DAT" )
                                        {
                                            if( json_object[i][col[17]] !== undefined )
                                            {
                                                td.innerHTML =  "";
                                            } 
                                        }
                                        td.className = "freight_curr";
                                    }
                                    if(j == 89)
                                    {
                                        if(json_object[i][col[16]] == "FOB" || json_object[i][col[16]] == "EXW" || json_object[i][col[16]] == "FAS" || json_object[i][col[16]] == "FCA" )
                                        {
                                            if( json_object[i][col[18]] === undefined )
                                            {
                                                td.innerHTML =  "";
                                            }
                                            else
                                            {
                                                td.innerHTML =  removeSpecialsForNumber(json_object[i][col[18]]);
                                            }
                                        }  
                                        else if(json_object[i][col[16]] == "CIF" || json_object[i][col[16]] == "CIP" || json_object[i][col[16]] == "DAF" || json_object[i][col[16]] == "DDP" || json_object[i][col[16]] == "DDU" || json_object[i][col[16]] == "DEQ" || json_object[i][col[16]] == "DES" || json_object[i][col[16]] == "DAP" || json_object[i][col[16]] == "DAT" )
                                        {
                                            if( json_object[i][col[18]] !== undefined )
                                            {
                                                td.innerHTML =  "";
                                            } 
                                        } 
                                        td.className = "freight";                             
                                    }
                                    if(j == 90)
                                    {
                                        if(json_object[i][col[16]] == "FOB" || json_object[i][col[16]] == "EXW" || json_object[i][col[16]] == "FAS" || json_object[i][col[16]] == "FCA" || json_object[i][col[16]] == "CFR" || json_object[i][col[16]] == "CPT")
                                        {
                                            if( json_object[i][col[19]] === undefined )
                                            {
                                                td.innerHTML =  "";
                                            }
                                            else
                                            {
                                                td.innerHTML =  capitalize(removeSpecials(json_object[i][col[19]]));
                                            }
                                        }
                                        else if(json_object[i][col[16]] == "CIF" || json_object[i][col[16]] == "CIP" || json_object[i][col[16]] == "DAF" || json_object[i][col[16]] == "DDP" || json_object[i][col[16]] == "DDU" || json_object[i][col[16]] == "DEQ" || json_object[i][col[16]] == "DES" || json_object[i][col[16]] == "DAP" || json_object[i][col[16]] == "DAT" )
                                        {
                                            if( json_object[i][col[19]] !== undefined )
                                            {
                                                td.innerHTML =  "";
                                            } 
                                        }
                                        td.className = "ins_curr";
                                    }
                                    if(j == 91)
                                    {
                                        if(json_object[i][col[16]] == "FOB" || json_object[i][col[16]] == "EXW" || json_object[i][col[16]] == "FAS" || json_object[i][col[16]] == "FCA" || json_object[i][col[16]] == "CFR" || json_object[i][col[16]] == "CPT")
                                        {
                                            if( json_object[i][col[20]] === undefined )
                                            {
                                                td.innerHTML =  "";
                                            }
                                            else
                                            {
                                                td.innerHTML =  removeSpecialsForNumber(json_object[i][col[20]]);
                                            }
                                        }   
                                        else if(json_object[i][col[16]] == "CIF" || json_object[i][col[16]] == "CIP" || json_object[i][col[16]] == "DAF" || json_object[i][col[16]] == "DDP" || json_object[i][col[16]] == "DDU" || json_object[i][col[16]] == "DEQ" || json_object[i][col[16]] == "DES" || json_object[i][col[16]] == "DAP" || json_object[i][col[16]] == "DAT" )
                                        {
                                            if( json_object[i][col[20]] !== undefined )
                                            {
                                                td.innerHTML =  "";
                                            } 
                                        }
                                        td.className = "insurance";
                                    }  
                                }                                         
                                tr13.appendChild(td);
                            }
                            tbdy.appendChild(tr13);

                            //IID
                            var tr14 = document.createElement('tr');
                            tr14.className = i;
                            for (var j = 92; j < 112; j++)
                            {
                                var td = document.createElement("td"); 
                                if(j == 92)
                                {
                                    td.innerHTML = "IID";
                                }
                                if(j == 93)
                                {
                                    td.innerHTML = "1";
                                }
                                if(j == 95)
                                {
                                    td.innerHTML = removeSpecials(json_object[i][col[10]]);
                                    td.className = "goods_desc";
                                }
                                if(j == 96)
                                {
                                    td.innerHTML = "N";
                                    td.className = "m_goods_cond";
                                }
                                if(j == 97)
                                {
                                    td.innerHTML = "";
                                }
                                if(j == 99)
                                {
                                    td.innerHTML = "kg";
                                } 
                                if(j == 100)
                                {
                                    td.innerHTML = removeSpecialsForNumber(json_object[i][col[11]]);
                                    td.className = "total_net";
                                }                            
                                if(j == 101)
                                {
                                    td.innerHTML = "N";
                                }
                                if(j == 104)
                                {
                                    td.innerHTML = removeSpecialsForNumber(json_object[i][col[15]]);
                                    td.className = "inv_val";
                                }
                                if(j == 105)
                                {
                                    if(json_object[i][col[13]] === undefined)
                                    {
                                        td.innerHTML = " ";
                                    }
                                    else
                                    {
                                        var check = checkManuCountry(removeSpecials(json_object[i][col[13]]));
                                        if(check < 1)
                                        {
                                            td.innerHTML = "";
                                        }
                                        else
                                        {
                                            td.innerHTML = capitalize(getCountry(removeSpecials(json_object[i][col[13]]))); 
                                        }
                                        // td.innerHTML = capitalize(getCountry(removeSpecials(json_object[i][col[13]])));
                                    }
                                    td.className = "m_country";
                                }                               
                                tr14.appendChild(td);
                            }
                            tbdy.appendChild(tr14);
                        }
                        else
                        {   
                            //IID
                            var tr14 = document.createElement('tr');
                            tr14.className = i;
                            for (var j = 92; j < 112; j++)
                            {
                                var td = document.createElement("td"); 
                                if(j == 92)
                                {
                                    td.innerHTML = "IID";
                                }
                                if(j == 93)
                                {
                                    var s = multiline.indexOf(i);
                                    td.innerHTML = s+2;
                                }
                                if(j == 95)
                                {
                                    td.innerHTML = removeSpecials(json_object[i][col[10]]);
                                    td.className = "goods_desc";
                                }
                                if(j == 96)
                                {
                                    td.innerHTML = "N";
                                    td.className = "m_goods_cond";
                                }
                                if(j == 97)
                                {
                                    td.innerHTML = "";
                                }
                                if(j == 99)
                                {
                                    td.innerHTML = "kg";
                                } 
                                if(j == 100)
                                {
                                    td.innerHTML = removeSpecialsForNumber(json_object[i][col[11]]);
                                    td.className = "total_net";
                                }                            
                                if(j == 101)
                                {
                                    td.innerHTML = "N";
                                }
                                if(j == 104)
                                {
                                    td.innerHTML = removeSpecialsForNumber(json_object[i][col[15]]);
                                    td.className = "inv_val";
                                }
                                if(j == 105)
                                {
                                    if(json_object[i][col[13]] === undefined)
                                    {
                                        td.innerHTML = " ";
                                    }
                                    else
                                    {
                                        var check = checkManuCountry(removeSpecials(json_object[i][col[13]]));
                                        if(check < 1)
                                        {
                                            td.innerHTML = "";
                                        }
                                        else
                                        {
                                            td.innerHTML = capitalize(getCountry(removeSpecials(json_object[i][col[13]]))); 
                                        }
                                    }
                                    td.className = "m_country";
                                }                               
                                tr14.appendChild(td);
                            }
                            tbdy.appendChild(tr14); 
                        }
                    } 
                }
                tbl.appendChild(tbdy);
                var divContainer = document.getElementById("showData2");
                divContainer.innerHTML = "";
                divContainer.appendChild(tbl);
            }   
        </script>
    </head>

    <body>
    <div class="main-container  overlay">
    <?php
        $sql = "SELECT c.*,u.* from company_master c join user_master u on c.id = u.company_code where u.id = $uid";
        $res = mysqli_query($con,$sql);
        $r = mysqli_fetch_array($res);
        $company = $r['company_name'];
    ?>
        <div class="container"  style="max-width: 1459px;">
            <div class="ajax-loader">
                <img src="ajax-loader.gif"/>
            </div>
            <div class="col-sm-12">
                <input type="hidden" name="regime_type" id="regime_type" value="<?php echo  $regime_type ;?>" >
                <input type="hidden" name="cargo_ch" id="cargo_ch" value="<?php echo  $cargo_channel ;?>" >
                <input type="hidden" name="shipment" id="shipment" >
                <form enctype="multipart/form-data" onkeypress="return event.keyCode != 13">
                <div class="form-group form-group-lg">
                    <div class="row inn-top">
                        <div class="col-sm-2 " style="text-align-last: center;">
                            <h4><b><?php if( $regime_type == 1){ echo "Module: Import";}else if($regime_type == 2){ echo "Module: Export";}else { echo "Please Select Module Type!!"; } ?> </b> </h4>
                        </div>
                        <div class="col-sm-2" style="text-align-last: center;">
                            <h4><b><?php if( $cargo_channel == 7){ echo "Channel: Courier Land";}else { echo "Channel: Courier Air";}?> </b> </h4>
                        </div>     
                        <div class="col-sm-3" style="text-align-last: center;">
                            <h4><b><?php echo "User Name : ".$uname ; ?> </b> </h4>
                        </div>
                        <div class="col-sm-5" style="text-align-last: end;">
                            <button  class="btn btn-secondary"><a href="change_password.php?id=<?php echo $uid;?>" style="color: azure;">Modify User <i class="fa fa-user"></i></a></button>
                            <button  class="btn btn-secondary ask"><a href="settings.php" style="color: azure;">Change Module <i class="fa fa-wrench"></i></a></button>
                            <button  class="btn btn-danger logout" onclick="return confirm('do you want to logout?');"><a href="logout.php" style="color: azure;">Logout</a></button> 
                        </div>
                    </div>
                    <hr>
					<div class="container" style="    max-width: 99% !important;">  
					<div class="row"> 
                    <div class="col-md-7" style="border: 1px solid #999; padding: 7px 17px;    height: 98px;    max-width: 56%;">   
                    <div class="row">  				
                        <input type="hidden" name="cust_code" id="cust_code" class="form-control freeze" value="<?php echo $r['broker_customer_code']; ?>"> 
                        
                        <input type="hidden" name="business_code" id="business_code" class="form-control freeze" value="<?php echo $r['broker_business_code']; ?>"> 
                        
                        <input type="hidden" name="shipping_agent" id="shipping_agent" class="form-control freeze" value="<?php echo $r['shipping_agent']; ?>"> 
                        
                        <input type="hidden" name="cto" id="cto" class="form-control freeze" value="<?php echo $r['cto']; ?>"> 
                           
                        <?php if($regime_type == 2){ ?>					
						 
                        <div class="col-md-4">
                            <input type="text" name="out_master_doc_no" id="out_master_doc_no" class="form-control freeze" value="<?php if($_SESSION['out_master_doc_no']){ echo $_SESSION['out_master_doc_no'];}?>" > 
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Master document number </p>                               
                        </div> 
                        <?php if($cargo_channel == 7) { ?>
                        <div class="col-md-4">
                            <input type="text" name="carrier_no1" id="carrier_no1" class="form-control" value="<?php if($_SESSION['carrier_no1']){ echo $_SESSION['carrier_no1'];}?>" > 
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Carrier Number(Optional) </p>                               
                        </div>
                        <div class="col-md-4">
                            <input type="date" name="schedule_date1" id="schedule_date1" class="form-control freeze datepicker" value="<?php if($_SESSION['schedule_date1']){ echo $_SESSION['schedule_date1']; }else{ echo date('Y-m-d'); }?>"> 
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Carrier Schedule Date </p>                               
                        </div> 
                        <?php }} ?>
                        <?php if($regime_type == 1){ ?>
                        <div class="col-md-4">
                            <input type="text" name="in_master_doc_no" id="in_master_doc_no" class="form-control freeze" value="<?php if($_SESSION['in_master_doc_no']){ echo $_SESSION['in_master_doc_no'];}?>"> 
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Master document number </p>                               
                        </div> 
                        <?php if($cargo_channel == 8) { ?>
                        <div class="col-md-4">
                            <input type="text" name="registration_no1" id="registration_no1" class="form-control freeze" value="<?php if($_SESSION['registration_no1']){ echo $_SESSION['registration_no1'];}?>"> 
                            <p class="pull-left" style="font-size:10px;font-weight: bold;"> Carrier Registration Number</p>                               
                        </div>
                        <div class="col-md-4">
                            <input type="date" name="schedule_date1" id="schedule_date1" class="form-control freeze datepicker" value="<?php if($_SESSION['schedule_date1']){ echo $_SESSION['schedule_date1']; }else{ echo date('Y-m-d'); } ?>"> 
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Carrier Schedule Date </p>                               
                        </div> 
                        <?php } else if ($cargo_channel == 7) { ?>
                        <div class="col-md-4">
                            <input type="text" name="carrier_no1" id="carrier_no1" class="form-control freeze" value="<?php if($_SESSION['carrier_no1']){ echo $_SESSION['carrier_no1'];}?>"> 
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Carrier Number </p>                               
                        </div>
                        <div class="col-md-4">
                            <input type="date" name="schedule_date1" id="schedule_date1" class="form-control freeze datepicker" value="<?php if($_SESSION['schedule_date1']){ echo $_SESSION['schedule_date1']; }else{ echo date('Y-m-d'); }?>"> 
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Carrier Schedule Date </p>                               
                        </div> 
						
                        <?php }}  ?>
                                        
                        <?php if($cargo_channel == 7){?>
                        <?php if($regime_type == 1) { ?>
                            <div class="col-md-4 ui-widget">
                                <input type="text" name="desti" id="desti" class="form-control freeze port" value="<?php if($_SESSION['desti']){ echo $_SESSION['desti'];}?>">  
                                <p class="pull-left" style="font-size:10px;font-weight: bold;">Origin Country</p>
                                <input type="hidden" name="destination" id="destination" value="<?php if($_SESSION['destination']){ echo $_SESSION['destination'];}?>">
                            </div> 
                            <div class="col-md-4 ui-widget">
                                <input type="text" name="port_of_discharge" id="port_of_discharge" class="form-control freeze" value="<?php if($_SESSION['port_of_discharge']){ echo $_SESSION['port_of_discharge'];}?>">  
                                <p class="pull-left" style="font-size:10px;font-weight: bold;"> Port of Load(Origin Country)</p>
                                <input type="hidden" name="port_of_load" id="port_of_load" value="<?php if($_SESSION['port_of_load']){ echo $_SESSION['port_of_load'];}?>">
                            </div>
                        <?php }elseif($regime_type == 2){ ?> 
                            <div class="col-md-4  ui-widget">
                                <input type="text" name="exit_point" id="exit_point" class="form-control freeze" value="<?php if($_SESSION['exit_point']){ echo $_SESSION['exit_point'];}?>">   
                                <p class="pull-left" style="font-size:10px;font-weight: bold;">Port Of Load(UAE)</p>     
                                <input type="hidden" name="port_of_load" id="port_of_load" value="<?php if($_SESSION['port_of_load']){ echo $_SESSION['port_of_load'];}?>">
                            </div>  
                        <?php }}else{ ?>
                        <?php if($regime_type == 2) { ?>
                        <div class="col-md-4  ui-widget">
                            <input type="text" name="exit_point" id="exit_point" class="form-control freeze port" value="<?php if($_SESSION['exit_point']){ echo $_SESSION['exit_point'];}?>">   
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Exit Point(UAE)</p>     
                        </div>  
                        <div class="col-md-4 ui-widget">
                            <input type="text" name="desti" id="desti" class="form-control freeze port" value="<?php if($_SESSION['desti']){ echo $_SESSION['desti'];}?>">  
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Destination Country</p>
                            <input type="hidden" name="destination" id="destination" value="<?php if($_SESSION['destination']){ echo $_SESSION['destination'];}?>">
                        </div> 
                        <div class="col-md-4"> 
                            <input type="text" name="port_discharge" id="port_discharge" class="form-control freeze" value="<?php if($_SESSION['port_discharge']){ echo $_SESSION['port_discharge'];}?>">  
                            <p class="pull-left" style="font-size:10px;font-weight: bold;"> Port of Discharge (Destination Country)</p>
                            <input type="hidden" name="port_of_discharge" id="port_of_discharge" class="form-control freeze" value="<?php if($_SESSION['port_of_discharge']){ echo $_SESSION['port_of_discharge'];}?>">  
                            <input type="hidden" name="port_of_load" id="port_of_load" value="<?php if($_SESSION['port_of_load']){ echo $_SESSION['port_of_load'];}?>">
                        </div> 
                        <?php } ?>
                        <?php if($regime_type == 1) { ?>
                        <div class="col-md-4 ui-widget">
                            <input type="text" name="desti" id="desti" class="form-control freeze port" value="<?php if($_SESSION['desti']){ echo $_SESSION['desti'];}?>">  
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Origin Country</p>
                            <input type="hidden" name="destination" id="destination" value="<?php if($_SESSION['destination']){ echo $_SESSION['destination'];}?>">
                        </div> 
                        <div class="col-md-4 ui-widget ui-widget">
                            <input type="text" name="port_load" id="port_load" class="form-control freeze" value="<?php if($_SESSION['port_load']){ echo $_SESSION['port_load'];}?>">  
                            <p class="pull-left" style="font-size:10px;font-weight: bold;"> Port of Load(Origin Country)</p>
                            <input type="hidden" name="port_of_load" id="port_of_load" value="<?php if($_SESSION['port_of_load']){ echo $_SESSION['port_of_load'];}?>">
                        </div>
                        <!-- <input type="hidden" name="port_of_load" id="port_of_load" value="<?php if($_SESSION['port_of_load']){ echo $_SESSION['port_of_load'];}?>"> -->
                        <div class="col-md-4 ui-widget">
                            <input type="text" name="port_of_discharge" id="port_discharge1" class="form-control freeze" value="<?php if($_SESSION['port_discharge1']){ echo $_SESSION['port_discharge1'];}?>">   
                            <input type="hidden" name="port_of_discharge" id="port_of_discharge" class="form-control" value="<?php if($_SESSION['port_of_discharge']){ echo $_SESSION['port_of_discharge'];}?>">
                            <p class="pull-left" style="font-size:10px;font-weight: bold;"> Port of Discharge (UAE)</p>
                        </div>
                        <?php } } ?> 						
						</div>
						</div>
						
						<div class="col-md-3" style="border: 1px solid #999;padding: 7px;margin: 0 1% 0 1%;height: 98px;max-width: 17%;"><div class="row">  <div class="col-md-12">
                            <select name="package_type" id="package_type" class="form-control freeze">
                                <?php
                                    $sql_stmt = "SELECT * FROM package_master";
                                    $result = mysqli_query($con,$sql_stmt);
                                    while($row = mysqli_fetch_array($result))
                                    {
                                        if($_SESSION['package_type'] != "" && $_SESSION['package_type'] == $row['code']){
                                ?>
                                <option value="<?php echo $row['code']; ?>" selected><?php echo $row['type']; ?></option>
                                <?php }else { ?>
                                    <option value="<?php echo $row['code']; ?>"><?php echo $row['type']; ?></option>
                                <?php }} ?>
                            </select>
                            <p class="pull-left" style="font-size:10px;font-weight: bold;">Package Type </p>     
                        </div>  
                        <div class="col-md-12">
                            <select name="invoice_type" id="invoice_type" class="form-control freeze">
                                <?php
                                    $sql_stmt = "SELECT * FROM invoice_type";
                                    $result = mysqli_query($con,$sql_stmt);
                                    while($row = mysqli_fetch_array($result))
                                    {
                                        if($_SESSION['invoice_type'] != "" && $_SESSION['invoice_type'] == $row['id']){
                                ?>
                                <option value="<?php echo $row['id']; ?>" selected><?php echo $row['type']; ?></option>
                                <?php }else{ ?>
                                    <option value="<?php echo $row['id']; ?>"><?php echo $row['type']; ?></option>
                                <?php }} ?>
                            </select>
                            <p class="pull-left" style="font-size:10px;font-weight: bold;"> Invoice type  </p>
                        </div> </div></div>
						<div class="col-md-3"><div class="row">
						<table style="width: 100%;">
						<tr><td style="text-align: left;">Broker Customer Code:</td><td style="text-align:  left;"><strong><?php echo $r['broker_customer_code']; ?></strong></td></tr>
						<tr><td style="text-align:  left;">Broker Business Code: </td><td style="text-align:  left;"><strong><?php echo $r['broker_business_code']; ?></strong></td></tr>
						<tr><td style="text-align:  left;">Shipping Agent: </td><td style="text-align:  left;"><strong><?php echo $r['shipping_agent']; ?></strong></td></tr>
						<tr><td style="text-align:  left;">Premises Code: </td><td style="text-align:  left;"><strong><?php echo $r['cto']; ?></strong></td></tr>
						<tr><td style="text-align:  left;"><strong>Credit Account:</strong> </td><td style="text-align:  left;"><strong><input type="text" name="mode_of_payment" id="mode_of_payment_1" class="form-control freeze" style="width: 200px;" value="<?php echo $r['credit_acc']; ?>"> </strong></td></tr>
						
						</table>
						   
						</div>
						</div>
						</div>
						</div>
						</div>
                    </div> 
					<div class="col-md-12 buttn" id="div1">
                        <div class="row" style="margin-top: 10px;">                         
                            <div class="col-md-2">
                                <button id="download" class="btn btn-primary form-control"><a href="download.php" style="color: azure;">Download Manifest Template</a></button>
                            </div>
                            <div class="col-md-2">
                                <input type="button" class="btn btn-primary freeze_btn form-control" name="proceed" value="Upload Manifest" style="margin-bottom:20px;" id="select_file">
                            </div>                            
                            <div class="col-md-4 excel">
                                <input id="upload" type=file name="files[]<?php dateti?>" class="form-control" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
                                <p class="pull-left" style="font-size:10px;"> Select Manifest [.xls] :</p>
                            </div>
                            <div class="col-md-2 excel">
                                <input type="button" class="btn btn-primary form-control" onclick="CreateTableFromJSON()" value="Show" id="show" />
                            </div>
                            <div class="col-md-2 excel">
                                <buttton type="button" class="btn btn-primary form-control" id="clear_grid"><a href="exceltotext.php" style="color: white;">Clear Grid</a></button>
                            </div>
                        </div> 
                    </div>					
                    <div class="excel" id="div2">
                        <div class="col-md-12" style="max-height: 300px;padding: 0;margin-left: 1%;max-width: 98%;overflow: auto;border: 1px solid #dddbff;background: #fff;"> 
                            <div class="clr" id="showData"></div>
                            <div class="clr" id="showData2" style="display:none;"></div>
                        </div>
                    </div>  
                </div>                   
                </form>  
                <div class="row excel" style="margin-top: 15px; text-align-last: center;">
                    <div class="col-md-12">
                        <button id="clear" class="btn btn-warning">New</button> 
                        <button id="export" data-export="export" class="btn btn-primary">Generate Customs File</button> 
                        <button id="exit" class="btn btn-danger">Exit</button> 
                    </div>                       
                </div>                      
            </div>
        </div>
    </div>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script>        
    $(function() {
        $( ".port" ).autocomplete(
        {
            source: 'countries.php',
            minLength: 0
        }).focus(function(){     
            //Use the below line instead of triggering keydown
            $(this).data("uiAutocomplete").search($(this).val());
        });
    });

    $(function() {
        $( "#port_discharge1" ).autocomplete(
        {
            source: 'exit_Point.php',
            minLength: 0
        }).focus(function(){     
            //Use the below line instead of triggering keydown
            $(this).data("uiAutocomplete").search($(this).val());
        });
    });

    $(function() {
        $( "#exit_point" ).autocomplete({
            source: 'exit_Point.php',
            minLength: 0
        }).focus(function(){     
            //Use the below line instead of triggering keydown
            $(this).data("uiAutocomplete").search($(this).val());
        });
    }); 
    $(function() {
        $( "#desti" ).autocomplete({
            source: 'destination.php',
            minLength: 0
        }).focus(function(){     
            //Use the below line instead of triggering keydown
            $(this).data("uiAutocomplete").search($(this).val());
        });
    });
    </script>
        <div class="modal" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" style="padding-right: 484px;">
           <div class="modal-dialog" role="document" style="margin-left: 150px;">
               <div class="modal-content" style="width: 1214px;background: #e7f2ff;padding: 30px;border-radius: 20px;">
                   <div class="modal-header">
                        <div class="col-sm-12">
                            <div class="row">
                                <div class="col-md-11">
                                    <h3 class="modal-title" id="exampleModalLabel" style="text-align:center;">Shipment Details</h3>
                                </div>
                                <div class="col-md-1">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="font-size: -webkit-xxx-large;">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                   <div class="modal-body ans2" style="text-align:center;">                                                    
                   </div>
                   <div class="modal-footer">                       
                 </div>					
               </div>
           </div>			
        </div>

        <footer class="page-footer font-small blue">
            <div class="footer-copyright text-center">© <?php echo date('Y'); ?> Copyright: m2clearance.com . Powered By
                <a href="http://m2clearance.com/" style="color: darkblue;"> m2clearance.com.</a>&nbsp;&nbsp;
                Licensed User : <?php echo $company; ?>
            </div>
        </footer>
        <script> 
            $(function() {   
                $("#port_load").autocomplete({   
                    // autoFocus:true,
                    source: function( request, response ) {        
                        $.ajax({       
                            url: 'get_port.php?country=' + $("#destination").val() + "&term=" + request.term,   
                            dataType: "json",
                            success: function( data ) {           
                                response(data);      
                            }
                        });   
                    },    
                    minLength: 0 
                }).focus(function(){     
                    //Use the below line instead of triggering keydown
                    $(this).data("uiAutocomplete").search($(this).val());
                });
            });

            $(function() {   
                $("#port_discharge").autocomplete({   
                    // autoFocus:true,
                    source: function( request, response ) {        
                        $.ajax({       
                            url: 'get_port.php?country=' + $("#destination").val() + "&term=" + request.term,   
                            dataType: "json",
                            success: function( data ) {           
                                response(data);      
                            }
                        });   
                    },    
                    minLength: 0  
                }).focus(function(){     
                    //Use the below line instead of triggering keydown
                    $(this).data("uiAutocomplete").search($(this).val());
                });
            });

            
            $(function() {   
                $("#port_of_discharge").autocomplete({   
                    // autoFocus:true,
                    source: function( request, response ) {        
                        $.ajax({       
                            url: 'get_port.php?country=' + $("#destination").val() + "&term=" + request.term,   
                            dataType: "json",
                            success: function( data ) {           
                                response(data);      
                            }
                        });   
                    },    
                    minLength: 0   
                }).focus(function(){     
                    //Use the below line instead of triggering keydown
                    $(this).data("uiAutocomplete").search($(this).val());
                });
            });


            $("#port_of_discharge").blur(function(){
                var dest = $(this).val().split('-')[0];
                $.ajax({
                    type : "post",
                    url : "get_con.php",
                    data : {term : dest},
                    beforeSend: function()
                    {
                        $('.ajax-loader').css("visibility", "visible");
                    },
                    success : function(response)
                    {
                        $("#port_of_load").val(response);
                    }, 
                    complete: function(){
                        $('.ajax-loader').css("visibility", "hidden");
                    }                       
                })
            });

            $("#desti").blur(function()
            {
                var dest = $(this).val();
                $.ajax({
                    type : "post",
                    url : "get_dest.php",
                    data : {term : dest},
                    beforeSend: function()
                    {
                        $('.ajax-loader').css("visibility", "visible");
                    },
                    success : function(response)
                    {
                        if(!response)
                        {
                            alert("Invalid Country!!");
                            $("#desti").val("");
                        }
                        else
                        {
                            $("#destination").val(response);
                            // change_port();
                        }
                    }, 
                    complete: function(){
                        $('.ajax-loader').css("visibility", "hidden");
                    }                     
                })
            });

            $("#show").click(function()
            {
                $("#select_file").prop('disabled', true);
                $("#upload").prop('disabled', true);
                $(this).prop('disabled',true);
            });

            $("#port_load").blur(function()
            {
                var dest = $(this).val().split('-')[0];
                $.ajax({
                    type : "post",
                    url : "get_con.php",
                    data : {term : dest},
                    beforeSend: function()
                    {
                        $('.ajax-loader').css("visibility", "visible");
                    },
                    success : function(response)
                    {
                        if(response === "")
                        {
                            alert("invalid port!!");
                            $("#port_load").val("");
                        }
                        else
                        {
                            $("#port_of_load").val(response);
                        }
                    }, 
                    complete: function(){
                        $('.ajax-loader').css("visibility", "hidden");
                    }                    
                })
            });

            $("#port_discharge").blur(function(){
                var dest = $(this).val().split('-')[0];
                $.ajax({
                    type : "post",
                    url : "get_con.php",
                    data : {term : dest},
                    beforeSend: function()
                    {
                        $('.ajax-loader').css("visibility", "visible");
                    },
                    success : function(response)
                    {
                        $("#port_of_discharge").val(response);
                    },
                    complete: function(){
                        $('.ajax-loader').css("visibility", "hidden");
                    }                   
                })
            });

            $("#exit_point").blur(function()
            {
                var dest = $(this).val();
                $.ajax({
                    type : "post",
                    url : "get_uae_ports.php",
                    data : {term : dest},
                    beforeSend: function()
                    {
                        $('.ajax-loader').css("visibility", "visible");
                    },
                    success : function(response)
                    {
                        if(response === "")
                        {
                            alert("invalid port!!");
                            $("#exit_point").val("");
                        }
                        else
                        {
                            $("#port_of_load").val(response);
                        }
                    },
                    complete: function(){
                        $('.ajax-loader').css("visibility", "hidden");
                    }                   
                })
            });

            $("#port_discharge1").blur(function()
            {
                var dest = $(this).val();
                $.ajax({
                    type : "post",
                    url : "get_uae_ports.php",
                    data : {term : dest},
                    beforeSend: function()
                    {
                        $('.ajax-loader').css("visibility", "visible");
                    },
                    success : function(response)
                    {
                        if(response === "")
                        {
                            alert("invalid port!!");
                            $("#port_discharge").val("");
                        }
                        else
                        {
                            $("#port_of_discharge").val(response);
                        }
                    },
                    complete: function(){
                        $('.ajax-loader').css("visibility", "hidden");
                    }                      
                })
            });

            $(".ask").click(function()
            {
                if (confirm('Are you sure ?'))
                {
                    if(!confirm('All data will be erased!!'))
                    {
                        return false;
                    }
                }   
                else
                {
                    return false;
                }
            });

            $("#select_file").click(function()
            {
                set_session();  
            });

            $(document).on('click','.add',function()
            {
                var regime_type = $("#regime_type").val();
                var term = $("#inco").val();
                var id = $(".add").attr("id");
                if(regime_type == 1)
                {
                    if(term == "FOB" || term == "EXW" || term == "FAS" || term == "FCA" || term == "CFR" || term == "CPT" )
                    {
                        if($("#insurance").val() == "" || $("#ins_curr").val() == "")
                        {
                            alert("Please Fill Insurance value & currency");
                            return false;
                        }    
                        else
                        {
                            $("tr."+id).css("background-color", "white");
                        }                      
                    }  
                    if(term == "FOB" || term == "EXW" || term == "FAS" || term == "FCA")
                    {
                        if($("#freight").val() == "" || $("#freight_curr").val() == "")
                        {
                            alert("Please Fill Freight value & currency");
                            return false;
                        }   
                        else
                        {
                            $("tr."+id).css("background-color", "white");
                        }                       
                    }
                }                                  
            });

            $(document).on('click','.btn-modal',function()
            {
                $('.modal'). modal('show');
                var id =  $(this).closest('tr').attr('class');
                var res = sendTypes(id);
                var pack_type = res.pack_type;
                var inv_type =  res.inv_type;
                var pay_instru = res.pay_instru; 
                var regime_type = $('#regime_type').val();
                var hawb = $(this).val();
                var dec_ref_no = $(this).closest('tr').children().eq(2).text();
                var name = $(this).closest('tr').children().eq(3).text();//$("#importer_name").val();
                var address = $(this).closest('tr').children().eq(4).text();//$("#importer_address").val();
                var country = $(this).closest('tr').children().eq(5).text();//$("#importer_country").val();
                var city = $(this).closest('tr').children().eq(6).text();//$("#importer_city").val();
                var inv_no = $(this).closest('tr').children().eq(7).text();
                var date = $(this).closest('tr').children().eq(8).text();
                if(date != "")
                {
                    var inv_date = date;
                }
                var no_of_pack = $(this).closest('tr').children().eq(9).text();
                var goods_desc = $(this).closest('tr').children().eq(10).text();
                var no_of_pieces = $(this).closest('tr').children().eq(1).text();
                var net =  $(this).closest('tr').children().eq(11).text();
                var gross = $(this).closest('tr').children().eq(12).text();
                var m_country = $(this).closest('tr').children().eq(13).text();
                var inv_curr = $(this).closest('tr').children().eq(14).text();
                var inv_val = $(this).closest('tr').children().eq(15).text();
                var inco = $(this).closest('tr').children().eq(16).text();
                var freight =  $(this).closest('tr').children().eq(18).text();
                var freight_curr = $(this).closest('tr').children().eq(17).text();
                var insurance =  $(this).closest('tr').children().eq(20).text();
                var ins_curr = $(this).closest('tr').children().eq(19).text();

                $.ajax
                ({
                    type : 'post',
                    data : { hawb : hawb, name : name, address : address, country : country, city : city , id : id,
                            ref_no :dec_ref_no, net : net, gross : gross, inv_no : inv_no , inv_date : inv_date ,no_of_pack : no_of_pack, 
                            goods_desc : goods_desc,no_of_pieces : no_of_pieces , m_country : m_country , inv_curr : inv_curr , inv_val : inv_val,
                            freight : freight, freight_curr : freight_curr,insurance : insurance ,ins_curr : ins_curr ,inco :inco,
                            pack_type : pack_type ,inv_type : inv_type ,pay_instru : pay_instru
                            },
                    url : 'modal.php',
                    success : function(response)
                    {
                        $('.ans2').html(response);
                    }
                });
            });  

            function sendTypes(id)
            {
                var arr = [];              
                $(".csv-table tbody").find("tr").each(function()
                {
                    if ($(this).attr('class') == id)                    
                    {    
                        var pack_type = $(this).find('td.pack_type').text();
                        var inv_type =  $(this).find('td.inv_type').text();
                        var pay_instru = $(this).find('td.pay_instru').text();   
                        arr.push(pack_type);
                        arr.push(inv_type);
                        arr.push(pay_instru);
                    }                
                });
                return arr;
            }

            $(document).on('click','.add',function()
            {
                var id = $(this).attr('id');
                $(".table").find("tr").each(function()
                {                    
                    if ($(this).attr('class') == id)                    
                    {
                        $(this).find('.invalid').removeClass("invalid");
                       
                        if($('#goods_desc').val() != "") 
                        {
                            $(this).children().eq(10).text($('#goods_desc').val());
                            $(this).children().eq(7).text($('#inv_no').val());
                            $(this).children().eq(13).text($('#m_country').val());
                            $(this).children().eq(2).text($('#dec_ref_no').val());
                            $(this).children().eq(8).text($('#inv_date').val());
                            $(this).children().eq(9).text($('#no_of_pack').val());
                            $(this).children().eq(1).text($('#no_of_pieces').val());
                        }
                        if($('#total_net').val() != "") 
                        {
                            $(this).children().eq(11).text($('#total_net').val());
                        }
                        if($('#total_gross').val() != "") 
                        {
                            $(this).children().eq(12).text($('#total_gross').val());
                        }
                        if($('#inv_curr').val() != "") 
                        {
                            $(this).children().eq(14).text($('#inv_curr').val());
                        }
                        if($('#inv_val').val() != "") 
                        {
                            $(this).children().eq(15).text($('#inv_val').val());
                        }
                        if($('#inco').val() != "") 
                        {
                            $(this).children().eq(16).text($('#inco').val());
                        
                        // if($('#freight').val() != "") 
                        // {
                            $(this).children().eq(18).text($('#freight').val());
                        // }
                        // if($('#freight_curr').val() != "") 
                        // {
                            $(this).children().eq(17).text($('#freight_curr').val());
                        // }
                        // if($('#insurance').val() != "") 
                        // {
                            $(this).children().eq(20).text($('#insurance').val());
                        // }
                        // if($('#ins_curr').val() != "") 
                        // {
                            $(this).children().eq(19).text($('#ins_curr').val());
                        }
                    }
                });
                $(".csv-table tbody").find("tr").each(function()
                {
                    if ($(this).attr('class') == id)                    
                    {               
                        if($('#dec_ref_no').val() != "") 
                        {
                        }                        
                        if($('#total_gross').val() != "")
                        {
                            $(this).find('td.total_gross').text($('#total_gross').val());
                            $(this).find('td.dec_ref_no').text($('#dec_ref_no').val());
                            $(this).find('td.pack_type').text($('#pack_type').val());
                            $(this).find('td.inv_type').text($('#inv_type').val());
                            $(this).find('td.inv_no').text($('#inv_no').val());
                            $(this).find('td.inv_date').text($('#inv_date').val());
                            $(this).find('td.no_of_pack').text($('#no_of_pack').val());
                            $(this).find('td.no_of_pieces').text($('#no_of_pieces').val());
                            $(this).find('td.m_country').text($('#m_country').val());
                        }
                        if($('#total_net').val() != "")
                        {
                            $(this).find('td.total_net').text($('#total_net').val());
                        }             
                        if($('#m_goods_cond').val() != "")
                        {
                            $(this).find('td.m_goods_cond').text($('#m_goods_cond').val());
                        } 
                        if($('#goods_desc').val() != "") 
                        {
                            $(this).find('td.goods_desc').text($('#goods_desc').val());
                        }
                        if($('#inv_curr').val() != "") 
                        {
                            $(this).find('td.inv_curr').text($('#inv_curr').val());
                        }
                        if($('#inv_val').val() != "") 
                        {
                            $(this).find('td.inv_val').text($('#inv_val').val());
                        }
                        if($('#inco').val() != "") 
                        {
                            $(this).find('td.inco').text($('#inco').val());
                        // }
                        // if($('#freight_curr').val() != "") 
                        // {
                            $(this).find('td.freight_curr').text($('#freight_curr').val());
                        // }
                        // if($('#freight').val() != "") 
                        // {
                            $(this).find('td.freight').text($('#freight').val());
                        // }                        
                        // if($('#insurance').val() != "") 
                        // {
                            $(this).find('td.insurance').text($('#insurance').val());
                        // }
                        // if($('#ins_curr').val() != "") 
                        // {
                            $(this).find('td.ins_curr').text($('#ins_curr').val());
                        }
                        if($('#p_o_l').val() != "") 
                        {
                            $(this).find('td.ins_curr').text($('#p_o_l').val());
                        }
                        if($('#p_o_d').val() != "") 
                        {
                            $(this).find('td.ins_curr').text($('#p_o_d').val());
                        }
                    }
                });
                $('.modal').modal('hide');               
            });            
            
            $(".excel").hide();
 
            $(".freeze_btn").click(function()
            {
                var reqlength = $('.freeze').length;
                var value = $('.freeze').filter(function () {
                    return this.value != '';
                });

                if (value.length>=0 && (value.length !== reqlength)) 
                {
                    alert('Please fill out all required fields.');
                } 
                else
                {
                    $(".freeze").attr('readonly', true);
                    $(".excel").show();
                }
            });    

            $("#clear").click(function()
            {
                if (confirm('Are you sure ?'))
                {
                    if(confirm('All data will be erased!!'))
                    {
                        window.location.href = "session_destroy.php";
                        // window.location.reload(false);
                    }
                    else
                    {
                        return false;
                    }
                }   
                else
                {
                    return false;
                }
            }); 

            $("#exit").click(function()
            {
                if (confirm('Are you sure ?'))
                {
                    if (confirm("All data will be erased"))
                    {
                        window.location.href = 'http://apps.m2clearance.com/dubai_custom/logout.php'; 
                    }
                    else
                    {
                        return false;
                    }
                }   
                else
                {
                    return false;
                }
            });  

        </script>
        <script>
            jQuery.fn.tableToCSV = function() 
            {
    
                var clean_text = function(text){
                    text = text.replace(/"/g, '\\"').replace(/'/g, "\\'");
                    return '"'+text+'"';
                };
                
                $(this).each(function()
                {
                    var table = $(this);
                    var port_of_discharge = "";
                    
                    var regime_type = "";
                    var cargo_ch = "";
                    if($("#regime_type").val() == 1) { regime_type = "Import"; }else { regime_type = "Export"; };
                    if($("#cargo_ch").val() == 7) { cargo_ch = "LAND"; }else { cargo_ch = "AIR"; };
                    var title = [];
                    var rows = [];
                    if( $("#cargo_ch").val() == 8 && $("#regime_type").val() == 2 )
                    {
                        port_of_discharge = $('#port_of_discharge').val();
                    }
                    else
                    {
                        port_of_discharge = $('#port_of_load').val();
                    }
                    $(this).find('tr').each(function(){
                        var data = [];
                        $(this).find('th').each(function(){
                            var text = clean_text($(this).text());
                            title.push(text);
                            });
                        $(this).find('td').each(function(){
                            var text = clean_text($(this).text());
                            data.push(text);
                            });
                        data = data.join(",");
                        rows.push(data);
                        });
                    title = title.join(",");
                    rows = rows.join("\n");

                    var csv = title + rows;
                    var uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv);
                    var download_link = document.createElement('a');
                    download_link.href = uri;
                    var ts = new Date().getTime();
                    download_link.download = ts+'-'+regime_type+'-'+port_of_discharge+"-"+cargo_ch+".csv";
                    document.body.appendChild(download_link);
                    download_link.click();
                    document.body.removeChild(download_link);
                });                
            };
            document.getElementById('upload').addEventListener('change', handleFileSelect, false);
            
            $("#export").click(function()
            {     
                var invalidItems = $('.invalid').length;
                if(invalidItems == 0)
                {
                    var count = $("#shipment").val();
                    var csv = 1; 
                    $.ajax({
                        type : "post",
                        url : "insert_activity.php",
                        data : { csv_generated : csv,no_of_shipments : count},
                        success : function(response)
                        {
                            console.log(response);
                        }                    
                    })
                    $(".csv-table").tableToCSV();
                    // set_session();  
                }
                else
                {
                    alert("Manifest Format is incorrect!!");
                }
               
            });  

            function set_session()
            {
                var in_master_doc_no = $("#in_master_doc_no").val();
                var out_master_doc_no = $("#out_master_doc_no").val();
                var carrier_no1 = $("#carrier_no1").val();     
                var registration_no1 = $("#registration_no1").val();     
                var package_type = $("#package_type :selected").val();
                var invoice_type = $("#invoice_type :selected").val(); 
                var exit_point = $("#exit_point").val();
                var port_of_load = $("#port_of_load").val();
                var desti = $("#desti").val();
                var destination = $("#destination").val();
                var port_of_discharge = $("#port_of_discharge").val();
                var port_load = $("#port_load").val();
                var port_discharge1 = $("#port_discharge1").val();
                var port_discharge = $("#port_discharge").val();     
                var schedule_date1 = $("#schedule_date1").val();
                $.ajax
                ({
                    type :  "post",
                    url :   "set_session.php",
                    data :  {   in_master_doc_no : in_master_doc_no,
                                out_master_doc_no : out_master_doc_no,
                                carrier_no1 : carrier_no1,
                                registration_no1 : registration_no1,
                                package_type:package_type,
                                invoice_type:invoice_type,
                                exit_point : exit_point,
                                port_of_load : port_of_load,
                                desti : desti,  
                                destination : destination,
                                port_of_discharge : port_of_discharge,
                                port_load : port_load,
                                port_discharge1 : port_discharge1,
                                port_discharge : port_discharge,
                                schedule_date1 : schedule_date1
                            },
                    success : function(response)
                    {
                        console.log(response);
                        // window.location.reload();
                    }                    
                });
            }                       
        </script>   
        <script>
        document.onkeydown = function(e)
        {
            if(event.keyCode == 123)
            {
                return false;
            }
            if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) 
            {
                return false;
            }
            if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)) 
            {
                return false;
            }
            if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) 
            {
                return false;
            }
            if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0))
            {
                return false;
            }
        }
        </script>
        </div>
    </body>
</html>