<?php
session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("Functions_Trans.php");
//include("Forms_DataGrid.php");
include("Forms_Invoicing_DataGrid.php");
include("function.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$invoicing_id = $_GET['invoicing_id'];

print <<<HERE
<html>
<head>
<title>Payments Details</title>
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<link type="text/css" rel="stylesheet" href="js/calendar/dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="js/calendar/dhtmlgoodies_calendar.js?random=20060118"></script>
<link type="text/css" href="js/calendar/jquery.datepick.css" rel="stylesheet">
<script type="text/javascript" src="js/calendar/jquery.datepick.js"></script>
<script>
function insertInvoicingDt(){
	var InvoicingDt_Amount  = document.getElementById('InvoicingDt_Amount').value;
	var InvoicingDt_DueDate = document.getElementById('InvoicingDt_DueDate').value; 
	var invoicing_id        = document.getElementById('invoicing_id').value;
	if(InvoicingDt_Amount == ""){
		alert("Amount is required!");
	}
	else
		if(InvoicingDt_DueDate == ""){
			alert("Due Date is required!");
		}
		else{
			var str = "List_InvoicingDt_Actions.php?actionmode=insertdt&InvoicingDt_Amount="+InvoicingDt_Amount+"&InvoicingDt_DueDate="+InvoicingDt_DueDate+"&invoicing_id="+invoicing_id;
			var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
			 var data = xhttp.responseText.split('[BRK]');
			 if(data[0] == 0){
				 alert("Total Payment Amounts is not equal to the main amount!");
			 }
			 else{
				 document.getElementById('list_invoicingdt').innerHTML = data[1];
			 }
			 }
        };
       xhttp.open("GET", str, true);
       xhttp.send();
		}
}
function deleteInvoicingDt(invoicingdt_id){
	if(confirm("Are you sure?")){
		var invoicing_id = document.getElementById('invoicing_id').value;
		var str = "List_InvoicingDt_Actions.php?actionmode=deletedt&invoicingdt_id="+invoicingdt_id+"&invoicing_id="+invoicing_id;
		var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
			 var data = xhttp.responseText.split('[BRK]');
			 document.getElementById('list_invoicingdt').innerHTML = data[0];
		 }
        };
       xhttp.open("GET", str, true);
       xhttp.send();
	}
}
function editInvoicingDt(invoicingdt_id){
	var InvoicingDt_Amount  = document.getElementById('InvoicingDt_Amount_'+invoicingdt_id).value;
	var InvoicingDt_DueDate = document.getElementById('InvoicingDt_DueDate_'+invoicingdt_id).value; 
	var invoicing_id        = document.getElementById('invoicing_id').value;
	if(InvoicingDt_Amount == ""){
		alert("Amount is required!");
	}
	else
		if(InvoicingDt_DueDate == ""){
			alert("Due Date is required!");
		}
		else{
			var str = "List_InvoicingDt_Actions.php?actionmode=editdt&InvoicingDt_Amount="+InvoicingDt_Amount+"&InvoicingDt_DueDate="+InvoicingDt_DueDate+"&invoicing_id="+invoicing_id+"&invoicingdt_id="+invoicingdt_id;
			var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
			 var data = xhttp.responseText.split('[BRK]');
			 if(data[0] == 0){
				 alert("Total Payment Amounts is not equal to the main amount!");
			 }
			 else{
				 document.getElementById('list_invoicingdt').innerHTML = data[1];
			 }
			 }
        };
       xhttp.open("GET", str, true);
       xhttp.send();
		}
}
function saveClose(){
	var invoicing_id = document.getElementById('invoicing_id').value;
	var str = "List_InvoicingDt_Actions.php?actionmode=chktotalamounts&invoicing_id="+invoicing_id;
	var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (xhttp.readyState == 4 && xhttp.status == 200) {
	    var data = xhttp.responseText.split('[BRK]');
		if(data[0] == 0){
			alert("Total Payment Amounts is not equal to the main amount!");
		}
		else{
			window.close();
		}
		}
        };
       xhttp.open("GET", str, true);
       xhttp.send();
}
</script>
</head>
<body>
HERE;
echo "<div style='width: 800px; margin: auto; font-family: Arial; font-size: 20px; text-align: center; padding: 5px; background-color: #acacac;'>
Payments Details
</div>";
echo "<div style='height: 15px'></div>";
echo "<div style='width: 800px; margin: auto; text-align: center'><button onclick=\"saveClose()\"><span><span>Save & Close</span></span></button></div>";
echo "<div style='height: 15px'></div>";
echo "<input type='hidden' name='invoicing_id' id='invoicing_id' value='$invoicing_id'>";
echo "<div id='list_invoicingdt'>";
drawInvoicingDt($connection, $invoicing_id);
echo"</div>";
print <<<HERE
</body>
</html>
HERE;
?>