<?php 
session_start();
require_once("../../includes/display_internal_user_risk.inc.php");
require_once("../../includes/functions_internal_user_riskmangement.inc.php");
$obj = new data();
$connect = $obj->funConnect();
$display = new display();
$display->logout_iu();

if (@$_REQUEST['action'] == 'submit')
 {
 	$msg = $obj->insertCargoRisk();
	header('Location:./cargorisk.php?msg='.$msg);
 }
$pagename = basename($_SERVER['PHP_SELF']);


// echo '<pre>';


// print_r($_SESSION);


?>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php $display->title(); ?></title>
<?php $display->favicon(); ?>
<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
<?php $display->css(); ?>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="../js/html5shiv.js"></script>
  <script src="../js/respond.min.js"></script>
<![endif]-->
<style>
.animated {
				vertical-align: top; 
				transition: height 0.2s;
				-webkit-transition: height 0.2s; 
				-moz-transition: height 0.2s; 
			}

form.cmxform label.error, label.error {
		/* remove the next line when you have trouble in IE6 with labels in list */
		font-family:Verdana, Arial, Helvetica, sans-serif;
		font-size:10px;
		color: red;
		font-style:normal;
		font-weight:lighter;
		margin:5px;
		vertical-align:top;
		}
</style>
</head>
    <body class="skin-blue fixed">
        <!-- header logo: style can be found in header.less -->
        <?php $display->header_tag(); ?>
		<div class="wrapper row-offcanvas row-offcanvas-left">
            <?php $display->leftmenu(3); ?>
			<aside class="right-side">                
                <!-- Content Header (Page header) -->
                 <section class="content-header">
                    <h1>
					<i class="fa fa-folder-open"></i>&nbsp;Risk Cargo&nbsp;&nbsp;&nbsp;&nbsp;<small><div class="headline text-center" id="time" style="color:#000; font-weight:bold; font-size:12px;"></div></small>
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="../"><i class="fa fa-home"></i> Home</a></li>
                        <li class="active">Cargo&nbsp;&nbsp;&nbsp; >&nbsp;&nbsp;&nbsp;Risk Cargo</li>
                    </ol>
                </section>

                <!-- Main content -->
                <section class="content invoice">
				<!--   content put here..................-->
				<div align="right"><a href="cargorisk.php"><button class="btn btn-info btn-flat">Back</button></a></div>
				<div style="height:10px;">&nbsp;</div>
				<form role="form" name="frm1" id="frm1" enctype="multipart/form-data" action="<?php echo $pagename;?>" method="post">

				<div class="row">
                        <div class="col-xs-12">
                            <h2 class="page-header">
                             ADD RISK CARGO
                            </h2>                            
                        </div><!-- /.col -->
                    </div>


					<div class="box-body">
                    
                       

					<div class="form-group" style="width:49%; float:left;">
							<label for="txtVName" >Folder No</label>

							<input type="text" class="form-control" id="txtfolderNo" name="txtfolderNo"  placeholder="Folder No" autocomplete="off" required>      


						
						</div>
                    
                        <div class="form-group" style="width:49%; float:left;padding-left:1%;">
						<label for="txtVName" >Cp Date</label>
							<input type="text" class="form-control" id="txtCpDate" name="txtCpDate"  placeholder="Date" autocomplete="off" required>                                     
						</div>

                        <div class="form-group" style="width:49%; float:left;">
							<label for="txtVName" >Vessel Name</label>
							<input type="text" class="form-control" id="txtVesselName" name="txtVesselName" placeholder="Vessel Name" autocomplete="off" onKeyUp="setCalculationForffa()" required/>                        
						</div>
                    
                        <div class="form-group" style="width:49%; float:left;padding-left:1%;">
						<label for="txtVName" >Pic Name</label>
							<input type="text" class="form-control" id="txtPicName" name="txtPicName" placeholder="Pic Name" autocomplete="off">                      
						</div>





					    <div class="form-group" style="width:49%; float:left;">
							<label for="txtVName" >Hire
						</label>
							<input type="text" class="form-control" id="txtHire" name="txtHire" placeholder="Hire" autocomplete="off" onKeyUp="getpl()"required>                        </div>
                    
                        <div class="form-group" style="width:49%; float:left;padding-left:1%;">
						<label for="txtVName" >Cargo Name

						</label>
							<input type="text" class="form-control" id="txtCargoName" name="txtCargoName" placeholder="Cargo Name
						" autocomplete="off" >                      
						</div>






						
						<div class="form-group" style="width:49%; float:left;">
							<label for="txtVCode" >Cargo Qty </label>
							<input type="text" class="form-control" id="txtCargoQty" name="txtCargoQty" placeholder="Cargo Qty" autocomplete="off" onKeyUp="getpl()" required>    
							
						 </div>



						 <div style="clear:both;"></div>	

						 <div class="form-group" style="width:49%; float:left;">
						<label for="txtDraft" >
						No Of Shipments
						</label>
							<input type="text" class="form-control" id="txtNoofShipment" name="txtNoofShipment" placeholder="No Of Shipments" autocomplete="off" onkeyup="getpl()" required>                         
						</div>
					

						<!-- <div style="clear:both;"></div>	 -->

						 <div class="form-group" style="width:49%; float:left;padding-left:1%;">
							<label for="txtVCode" >Freight USD / PMT </label>
							<input type="text" class="form-control" id="txtFrtRate" name="txtFrtRate" placeholder="Freight USD / PMT" autocomplete="off" onKeyUp="getpl()" required>    
						
						 </div><br/>


					 		<div style="clear:both;"></div>	
                        
                      
						<div class="form-group" style="width:49%;  float:left;">
						<label for="txtDWT">TCE
							</label>
							


							<input type="text" class="form-control" id="txtTCE" name="txtTCE" placeholder="TCE" autocomplete="off">    
						


						</div>


					
						<div class="form-group" style="width:49%; float:left;padding-left:1%;" id="drycargo7">
							<label for="txtVesselGroup">Ballast Bonus
						</label>
						<input type="text" class="form-control" id="txtBallastBonus" name="txtBallastBonus" placeholder="Ballast Bonus" autocomplete="off" onKeyUp="getpl()">  
						</div>

						<div style="clear:both;"></div>	
                        
                        

						


						<!-- <div style="clear:both;"></div> -->

						
						

                        <div class="form-group" style="width:49%; float:left;">
						<label for="txtDraft" >Voyage duration

						</label>
							<input type="text" class="form-control" id="txtVoyageDuration" name="txtVoyageDuration" placeholder="Voyage duration

							" autocomplete="off" onkeyup="getpl()" required>                         
						</div>
						<!-- <div style="clear:both;"></div>	 -->

						<div class="form-group" style="width:49%;  padding-left:1%;float:left;" id="drycargo1">
							<label for="txtCraneSize">Remarks
						</label>
						<textarea name="txtRemarks" id="txtRemarks" class="form-control" cols="42" rows="4"></textarea>
						</div>
						

						

                        <div style="clear:both;"></div>

		
						<!-- <div class="form-group" style="width:49%; float:left;" id="drycargo8">
						<label for="txtVesselGroup">Remarks</label>
							<textarea name="txtRemarks" id="txtRemarks" class="form-control" cols="42" rows="4"></textarea>
						</div> -->

						
						

						





						
												
						<div style="clear:both;"></div>

						<hr>

						<div>	<label for="txtDraft" >Bunkers Consumed
						</label></div>
                                <table width="100%" class='tablesorter' class="form-group">
								  <tr>
								    <td></td>
									<td>Bunker Grade</td>
									<td>Qty(MT)</td>
									<td>Price(USD)</td>
									<td>Amount(USD)</td>
									</tr>
									<tbody id="ConbunkerTBody">
							  		<tr id="DivConBunkeractual_1">
									<td><a href="#tb1'" onClick="removeConBunkerRows(1);" ><i class="fa fa-times" style="color:red;"></i></a></td>
									<td><select name="selConBunker_1" id="selConBunker_1"  style="width:90px;" class="input-text form-control"><?php $obj->getBunkerList();?></select></td>
									<td><input type="text" name="txtConBunkerQty_1" id="txtConBunkerQty_1" style="width:90px;" class="input-text numeric form-control" onKeyUp="getBunkerCalculation();getpl()" placeholder="0.00" autocomplete="off" value=""/></td>
									<td><input type="text" name="txtConBunkerPrice_1" id="txtConBunkerPrice_1" style="width:90px;" class="input-text numeric form-control" onKeyUp="getBunkerCalculation();getpl();" placeholder="0.00" autocomplete="off" value=""/></td>
									<td><input type="text" name="txtConBunkerAmt_1" id="txtConBunkerAmt_1" style="width:90px;" class="input-text form-control"  placeholder="0.00"  readonly autocomplete="off" value=""/></td>
								  </tr>
								  
								  <tr id="DivConBunkeractual_2">
									<td><a href="#tb2'" onClick="removeConBunkerRows(2);" ><i class="fa fa-times" style="color:red;"></i></a></td>
									<td><select name="selConBunker_2" id="selConBunker_2" style="width:90px;" class="input-text form-control"><?php $obj->getBunkerList();?></select></td>
									<td><input type="text" name="txtConBunkerQty_2" id="txtConBunkerQty_2" style="width:90px;" class="input-text numeric form-control" onKeyUp="getBunkerCalculation();getpl();" placeholder="0.00" autocomplete="off" value=""/></td>
									<td><input type="text" name="txtConBunkerPrice_2" id="txtConBunkerPrice_2" style="width:90px;" class="input-text numeric form-control" onKeyUp="getBunkerCalculation();getpl();" placeholder="0.00" autocomplete="off" value=""/></td>
									<td><input type="text" name="txtConBunkerAmt_2" id="txtConBunkerAmt_2" style="width:90px;" class="input-text form-control"  placeholder="0.00" readonly autocomplete="off" value=""/></td>
								  </tr>
								 
								</tbody>
								  <tr>
									<td colspan="5"><button type="button" onClick="addConBunkerRow()" class="btn btn-primary btn-flat">Add</button><input type="hidden" name="txtConBunkerCount" id="txtConBunkerCount" value="2"/></td>
								  </tr>
								</table>


								<hr>



								<div class="form-group" style="width:49%; float:left;" id="drycargo1">
							<label for="txtCraneSize">P/L
						</label>
							<input type="text" class="form-control" id="txtPl" name="txtPl" placeholder="PL
						" autocomplete="off" value="" readonly>
						</div>
					

                        
						<div class="form-group" style="width:49%;  padding-left:1%;float:left;">
							<label for="txtDraft" >Fxd / Cancelled
						</label>
						<select  name="selFxdCancelled"  class="form-control" id="selFxdCancelled" >
							
							<option value="">Please select a option</option>
							<option value="Fxd">Fixed</option>
							<option value="Cancelled">Cancelled</option>
							</select>                                     
						</div>
						<div style="clear:both;"></div>


					</div><!-- /.box-body -->
				


					
					<div class="box-footer" align="center">
						<input type="hidden" name="txtCRMFILE" id="txtCRMFILE" value="" />
						<input type="hidden" name="txtCRMNAME" id="txtCRMNAME" value="" />


						<!-- <button type="submit" class="btn btn-danger btn-flat" onClick="return getSubmit(9);">Save as draft</button> -->
						<button type="submit" class="btn btn-primary btn-flat" onClick="return getSubmit(0);">Submit</button>
						<input type="hidden" id="action" name="action" value="submit" />
						<input type="hidden" id="txtTotalBunkerCost" name="txtTotalBunkerCost" value="" />
					</div>
				</form>
				
				<!--   content ends here..................-->
                </section><!-- /.content -->
            </aside><!-- /.right-side -->
			
        </div><!-- ./wrapper -->
<?php $display->footer(); ?>
<?php $display->js(); ?>
<link href="../../css/jquery.alerts.css" rel="stylesheet" type="text/css" />
<script src='../../js/jquery.autosize.js'></script>

<link href="../../css/datepicker/datepicker.css" rel="stylesheet" type="text/css" />
<script src="../../js/plugins/datepicker/bootstrap-datepicker.js" type="text/javascript"></script>

<script language="JavaScript" type="text/javascript" src="../../js/jquery.alerts.js"></script>
<script src="../../js/timer.js" type="text/javascript"></script>

<script type="text/javascript">


function getpl(){


	var ttlbunker = parseFloat($("#txtTotalBunkerCost").val());
	if(isNaN(ttlbunker)){ttlbunker = 0.0000;}
	var hire = parseFloat($("#txtHire").val());
	if(isNaN(txtHire)){txtHire = 0.0000;}
	var txtCargoQty = parseFloat($("#txtCargoQty").val());
	if(isNaN(txtCargoQty)){txtCargoQty = 0.0000;}
	var txtFrtRate = parseFloat($("#txtFrtRate").val());
	if(isNaN(txtFrtRate)){txtFrtRate = 0.0000;}
	var txtBallastBonus = parseFloat($("#txtBallastBonus").val());
	if(isNaN(txtBallastBonus)){txtBallastBonus = 0.0000;}

	var txtVoyageDuration = parseFloat($("#txtVoyageDuration").val());
	if(isNaN(txtVoyageDuration)){txtVoyageDuration = 0.0000;}

	var txtNoofShipment = parseFloat($("#txtNoofShipment").val());
	if(isNaN(txtNoofShipment)){txtNoofShipment = 0.0000;}

	

	var expense = ttlbunker+(hire*txtVoyageDuration)+txtBallastBonus;
	var income = txtFrtRate*(txtCargoQty*txtNoofShipment);
	if(isNaN(expense)){expense = 0.0000;}
	if(isNaN(income)){income = 0.0000;}


	$("#txtPl").val((income-expense).toFixed(2))



}
$(document).ready(function(){ 




	$('#txtCpDate').datepicker({

                   
		format: 'dd-mm-yyyy',
	autoclose: true,
	todayBtn: true,
	minuteStep: 1
 }).on('changeDate', function(){  

	setCalculationForffa();
  });



  $('#txtPeriod').datepicker({

                   
format: 'mm-yyyy',
autoclose: true,
todayBtn: true,
minuteStep: 1
}).on('changeDate', function(){  

setCalculationForffa();
});

 

$(".areasize").autosize({append: "\n"});



$("#frm1").validate({
	rules: {
		txtTName:"required",
		selPort:"required",
		selFrequency:"required"

		
		},
	messages: {	
		txtTName:"*",
		selPort:"*",
		selFrequency:"*",
		},
submitHandler: function(form)  {


	if($('#selFrequency').val()!=0){



		jAlert('<b style="font-size:25px;">Please wait . . .</b><br><br><img src="../../img/loading.gif"  />', 'Alert');
		$("#popup_container").css({"background": "rgba(255,255,255,0.1)","border": "10px solid rgba(255,255,255,0.1)","width": "100%"});
		$("#popup_content").css({"background":"none","text-align":"center"});
		$("#popup_ok,#popup_title").hide();  
		form.submit();

	}else{


		
		jAlert('<b style="font-size:25px;">Select frequency', 'Alert');
		
		return false;
		
	}
		
	}
});

});


function getBunkerCalculation()
{
        var totalfoConsumption = totaldoConsumption = 0;
	    var txtSECAConBunkerCount = $("#txtSECAConBunkerCount").val();
		for(var i=1;i<=txtSECAConBunkerCount;i++)
		{
		    var txtSECABunkerQty = $("#txtSECABunkerQty_"+i).val();
			var txtSECABunkerPrice = $("#txtSECABunkerPrice_"+i).val();
		    if($("#ChkSECACalBunker_"+i).is(":checked"))
			{
			    if($("#txtSECAIdentify_"+i).val()=='FO')
			    {
				    totalfoConsumption = parseFloat(totalfoConsumption) + parseFloat(txtSECABunkerQty);
				}
				if($("#txtSECAIdentify_"+i).val()=='DO')
			    {
				    totaldoConsumption = parseFloat(totaldoConsumption) + parseFloat(txtSECABunkerQty);
				}
			}
			var calc = parseFloat(txtSECABunkerQty) * parseFloat(txtSECABunkerPrice);if(isNaN(calc)){calc =0; }
			// $("#txtSECABunkerAmt_"+i).val(calc.toFixed(2));
		}
		// $("#txtTotalSECAConsumption").val($("[id^=txtSECABunkerAmt_]").sum().toFixed(2));
		
		var txtNONSECAConBunkerCount = $("#txtNONSECAConBunkerCount").val();
		for(var i=1;i<=txtNONSECAConBunkerCount;i++)
		{
		    var txtNONSECABunkerQty = $("#txtNONSECABunkerQty_"+i).val();
			var txtNONSECABunkerPrice = $("#txtNONSECABunkerPrice_"+i).val();
			
		    if($("#ChkNONSECACalBunker_"+i).is(":checked"))
			{
			    if($("#txtNONSECAIdentify_"+i).val()=='FO')
			    {
				    totalfoConsumption = parseFloat(totalfoConsumption) + parseFloat(txtNONSECABunkerQty);
				}
				if($("#txtNONSECAIdentify_"+i).val()=='DO')
			    {
				    totaldoConsumption = parseFloat(totaldoConsumption) + parseFloat(txtNONSECABunkerQty);
				}
			}
			var calc = parseFloat(txtNONSECABunkerQty) * parseFloat(txtNONSECABunkerPrice);if(isNaN(calc)){calc =0; }
			// $("#txtNONSECABunkerAmt_"+i).val(calc.toFixed(2));
		}
		// $("#txtTotalNONSECAConsumption").val($("[id^=txtNONSECABunkerAmt_]").sum().toFixed(2));
		
		$("#txtTFUMT").val(totalfoConsumption.toFixed(2));$("#txtTDUMT").val(totaldoConsumption.toFixed(2));
		
		// $("#txtTotalBunkerCost").val($("#txtTotalNONSECAConsumption,#txtTotalSECAConsumption").sum().toFixed(2));
		
		var txtBunkerCount = $("#txtBunkerCount").val();
		for(var i=1;i<=txtBunkerCount;i++)
		{
			var txtBunkerQty = $("#txtBunkerQty_"+i).val();if(isNaN(txtBunkerQty)){txtBunkerQty =0; }
			var txtBunkerPrice = $("#txtBunkerPrice_"+i).val();if(isNaN(txtBunkerPrice)){txtBunkerPrice =0; }
			var calc = parseFloat(txtBunkerQty) * parseFloat(txtBunkerPrice);
			if(isNaN(calc)){calc =0; }
			$("#txtBunkerAmt_"+i).val(calc.toFixed(2));
		}
		
		var txtConBunkerCount = $("#txtConBunkerCount").val();

		var sumCon = 0;
		for(var i=1;i<=txtConBunkerCount;i++)
		{
			var txtConBunkerQty = $("#txtConBunkerQty_"+i).val();if(isNaN(txtConBunkerQty)){txtConBunkerQty =0; }
			var txtConBunkerPrice = $("#txtConBunkerPrice_"+i).val();if(isNaN(txtConBunkerPrice)){txtConBunkerPrice =0; }
			var calc = parseFloat(txtConBunkerQty) * parseFloat(txtConBunkerPrice);
			if(isNaN(calc)){calc =0; }
			$("#txtConBunkerAmt_"+i).val(calc.toFixed(2));

			sumCon=parseFloat(sumCon) + parseFloat(calc.toFixed(2));

		}



		var otheradd = parseFloat(sumCon);

		// var check = $("[id^=txtConBunkerAmt_]").sum();

		console.log(otheradd)
		if(sumCon > 0){$("#txtTotalBunkerCost").val(sumCon)}
	// getFinalCalculation();

	console.log($("#txtTotalBunkerCost").val())
}






function removeConBunkerRows(var1)
{
	jConfirm('Are you sure you want to remove this entry permanently ?', 'Confirmation', function(r) {
		if(r){
				$("#DivConBunkeractual_"+var1).remove();
				getBunkerCalculation();			
			 }
		else{return false;}
		});
}





function addConBunkerRow()
{
	var id = $("#txtConBunkerCount").val();
	if($("#txtConBunkerQty_"+id).val() != "" && $("#selConBunker_"+id).val() != "" && $("#txtConBunkerPrice_"+id).val() != "")
	{
		id = (id - 1) + 2;
		$('<tr id="DivConBunkeractual_'+id+'"><td><a href="#tbb'+id+'" onclick="removeConBunkerRows('+id+');"><i class="fa fa-times" style="color:red;"></i></a></td><td><select name="selConBunker_'+id+'" id="selConBunker_'+id+'" style="width:90px;" class="input-text form-control"><?php $obj->getBunkerList();?></select></td><td><input type="text" name="txtConBunkerQty_'+id+'" id="txtConBunkerQty_'+id+'" style="width:90px;" class="input-text numeric form-control" onkeyup="getBunkerCalculation();getpl();" placeholder="0.00" autocomplete="off" value=""></td><td><input type="text" name="txtConBunkerPrice_'+id+'" id="txtConBunkerPrice_'+id+'" style="width:90px;" class="input-text numeric form-control" onkeyup="getBunkerCalculation();getpl();" placeholder="0.00" autocomplete="off" value=""></td><td><input type="text" name="txtConBunkerAmt_'+id+'" id="txtConBunkerAmt_'+id+'" style="width:90px;" class="input-text form-control" placeholder="0.00" readonly="" autocomplete="off" value=""></td></tr>').appendTo("#ConbunkerTBody");
		$("#txtConBunkerCount").val(id);
		$("#selConBunkerVendor_"+id).html($("#selVendor").html());
		$(".numeric").numeric();	
	}
	else
	{
		jAlert('Please fill previous data', 'Alert');
		return false;
	}
}





</script>

<script>

function changePeriodStatus(elx){

	if($(elx).val()=='quarter'){



		var optionElement = $('<select id="selQuater" name ="selQuater" class="form-control"><option value="0">---Please select a option---</option><option value="1">Quarter 1</option><option value="2">Quarter 2</option><option value="3">Quarter 3</option><option value="4">Quarter 4</option></select>');
   		 $('#txtPeriod').replaceWith(optionElement);


	}if($(elx).val()=='calender'){


		var optionElement = $('<input type="text" class="form-control" id="txtPeriod" name="txtPeriod" placeholder="Period" autocomplete="off" style="pointer-events: none;">');
   		 $('#txtPeriod').replaceWith(optionElement);
   		 $('#selQuater').replaceWith(optionElement);

		$('#txtPeriod').prop('readonly', true);

	}if($(elx).val()=='month'){


		
		var optionElement = $('<input type="text" class="form-control" id="txtPeriod" name="txtPeriod" placeholder="Period" autocomplete="off">');

		$('#txtPeriod').replaceWith(optionElement);


   		 $('#selQuater').replaceWith(optionElement);


		$('#txtPeriod').prop('readonly', false);

		$('#txtPeriod').datepicker({

                   
		format: 'mm-yyyy',
		autoclose: true,
		todayBtn: true,
		minuteStep: 1
		}).on('changeDate', function(){  

		setCalculationForffa();
		});



	}


}



</script>
    </body>
</html>