<?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_DataGrid1.php");


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

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$querylog="SELECT FA_PurchaseJV as loginaccess,FA_PurchaseJVVAT from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");


$querylog=" select ifNull(FA_PurchaseJVDel,0) As FA_PurchaseJVDel, ifNull(FA_PurchaseJVMod,0) As FA_PurchaseJVMod, ".
          " ifNull(FA_PurchaseJVApp,0) As FA_PurchaseJVApp, ifNull(ModifyVAT,0) As ModifyVAT, ".
		  " ifNull(Login_ModifyDiscount,0) As Login_ModifyDiscount, ifNull(Login_FastPrint,0) As Login_FastPrint , ifNull(Login_ModifySellingPrice,0) As Login_ModifySellingPrice ".
		  " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}
$querycostcent=" select costcent_applyvat ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(loginstk.costcent_code,'%') ".
 			   " where user_account='".$_SESSION['useraccount']."' ".
 			   "";
			   
$costcentres = mysql_query($querycostcent,$connection) or die(mysql_error());
$rows=mysql_fetch_array($costcentres);
$costcent_applyvat = $rows[0];
if($costcent_applyvat=='')	$costcent_applyvat = 1;


	
//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ifNull(Supplier_ValidateProject,0) As Supplier_ValidateProject, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, ".
			 " ifNull(Module_ProjectDt,0) As Module_ProjectDt, ifNull(Module_CostCenterDt,0) As Module_CostCenterDt, ".
			 " ifNull(Module_RestrictProjCost,0) As Module_RestrictProjCost, ifNull(Module_RestrictCost,0) As Module_RestrictCost, ".
             " ifNull(Module_PostToJournal,0) As Module_PostToJournal, ifNull(Module_PurchasesJvWHT,0) As Module_PurchasesJvWHT,  ".
			 " ifNull(Module_PurchaseOrderJV,0) As Module_PurchaseOrderJV ".
             " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

//Read From PurchaseSetup Table
$queryPurchaseSetup="Select SupplierCur, VATAccountNonStock, ifnull(ShowInvoiceDate,0) as ShowInvoiceDate  From PurchaseSetup";
$resultPurchaseSetup=mysql_query($queryPurchaseSetup,$connection) or die(mysql_error());
while($rowPurchaseSetup=mysql_fetch_array($resultPurchaseSetup)){
	extract($rowPurchaseSetup);
}


$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());



$actionmode = $_POST["actionmode"]; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}   
$mainaction = $_POST["mainaction"]; if ($mainaction == ""){$mainaction = $_GET["mainaction"];}   

//Access Mode On Insert/Edit/Delete
$DiscountMode = '';
if ($Login_ModifyDiscount == 0)	$DiscountMode = ' readonly ';


if (empty($mainaction))	  $mainaction = 'insert';
if (empty($txtVatPrefix)) $txtVatPrefix = $VATAccountNonStock;

$message = ereg_replace("'"," ",$message);



print <<<HERE
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 

<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>


<link type="text/css" href="js/calendar/jquery.datepick.css" rel="stylesheet">
<script type="text/javascript" src="js/calendar/jquery.datepick.js"></script>

<link type="text/css" href="js/grid/scripts/jquery-ui.css" rel="stylesheet">
<script type="text/javascript" src="js/grid/scripts/jquery-ui.min.js"></script>



<script type="text/javascript" src="dropdown.js"></script>
<script type="text/javascript" language="javascript">
	
	
	$(document).ready(function() { 
		
	if (document.getElementById('txtLedgerNo[1]'))
		document.getElementById('txtLedgerNo[1]').focus();
		
	
		$(document).on('focusin', '.table-focus-g1', function() {
			AutoSaveRecordGrid(this.id,'SelectedRec','ModRec_G1','imgSubmitPurchasesJvDt');
			selRecordColor(this,'SelectedRec','SelectedRecColor');
		});
		
		//Set Scroll Positions on Page Load
		scrollToPage();
		
		$(window).scroll(function(){
			scrollPage();
		});
		
	
	});
	
	
function submitPurchasesJvDt(TrDtId,TrId,vrec,vmode,ModuleRestProj, ModuleRestCost, CheckSalesman,filepath){

var vDTId = TrDtId;

	if (vmode=='i')
			{		 
				var vactionmode = 'submitdt';
				vmode = 'submitrecord';
			}	
	else 	{
				var vactionmode = 'editdt';	
				vmode = 'editdt';
			}

var txtLedgerNo 		= 'txtLedgerNo['+vrec+']';
var txtDescription1 	= 'txtDescription1['+vrec+']';
var txtDescription2 	= 'txtDescription2['+vrec+']';
var txtDtCurr 			= 'txtDtCurr['+vrec+']';
var txtDtQty 			= 'txtDtQty['+vrec+']';
var txtDtPrice 			= 'txtDtPrice['+vrec+']';
var txtDtDiscPer 		= 'txtDtDiscPer['+vrec+']';
var txtDtTotal 			= 'txtDtTotal['+vrec+']';
var txtDtVat 			= 'txtDtVat['+vrec+']';
var txtDtManualNo 		= 'txtDtManualNo['+vrec+']';
var txtProjectCodeDt 	= 'projectcodedt['+vrec+']';
var txtCostCentCodeDt 	= 'costcentcodedt['+vrec+']';
var txtAssetPer 		= 'txtAssetPer['+vrec+']';
	
var value1 	= document.getElementById(txtLedgerNo).value;
var value2 	= document.getElementById(txtDescription1).value;
var value3 	= document.getElementById(txtDescription2).value;
var value4 	= document.getElementById(txtDtCurr).value;
var value5 	= document.getElementById(txtDtQty).value;
var value6 	= document.getElementById(txtDtPrice).value;
var value7 	= document.getElementById(txtDtDiscPer).value;
var value8 	= document.getElementById(txtDtTotal).value;
var value9 	= document.getElementById(txtDtVat).value;
var value10 = document.getElementById(txtDtManualNo).value;
var value11 = document.getElementById(txtProjectCodeDt).value;
var value12 = document.getElementById(txtCostCentCodeDt).value;
var value13 = document.getElementById(txtAssetPer).value;

//if (PreqId=='')
	var x =  document.purchasesjv;
	var result = CheckFields(ModuleRestProj, ModuleRestCost, CheckSalesman);
if (result != 1) 
return;	
	
	var vId           = x.txtPurchasesJVId.value;
	var vDate         = x.txtDate.value;
	var vMaturityDate = x.txtMaturityDate.value;
	var vInvoiceDate  = x.txtInvoiceDate.value;
		
	var vSalesman     = x.txtSupplierCode.value;
	var vRemark       = x.txtRemark.value;
	var vManualNo     = x.txtManualNo.value;
	var vVatPrefix    = x.txtVatPrefix.value;
	var vCurr         = x.txtCurr.value;
	var vProjectCode  = x.txtProjectCode.value;
	var vCostCentCode = x.txtCostCentCode.value;
	var vDiscountPer  = x.txtDiscountPer.value;
	var vDiscountAmt  = x.txtDiscountAmt.value;
	var vVatChkbx     = x.txtVatChk.value;
	
	vRemark   	= vRemark.replace('&','%26');
	value3   	= value3.replace('&','%26');
	vRemark   	= vRemark.replace('#','%23');
	vManualNo 	= vManualNo.replace('&','%26');
	vManualNo 	= vManualNo.replace('#','%23');
	value3 		= value3.replace('#','%23');
	
	
	var vMainMode = x.mainaction.value;
		

	var SubBtnID = 'imgSubmitPurchasesJvDt['+vrec+']';
	var SubBtnObj = document.getElementById(SubBtnID);
	$(SubBtnObj).unbind('click');
	$(SubBtnObj).removeAttr("onclick");	

	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  
	var trid = document.getElementById('txtPurchasesJVId').value;

	$("#Loader").addClass("ajaxsaving");  
	xmlhttp.onreadystatechange=function()
  {  
    
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
				{ 		
					if (vmode=='submitrecord')
						{  
					
							var data = xmlhttp.responseText.split("[BRK]");
							
							document.getElementById('txtPurchasesJVId').value = data[1];
							//document.getElementById('testdatagrid').innerHTML = data[0];
							
							
					
						//document.transferrequest.mainaction.value='edit';
						document.purchasesjv.actionmode.value='edit';
					
						if (trid==''){				
								document.purchasesjv.action='Form_PurchasesJV.php';				
								document.purchasesjv.submit();
								
										}
										else {
								
										ReloadGrid(filepath,'JP',trid,'datagrid','','','ADD','','','','',0,'',function() {
											var focusedvrec = parseInt(vrec) + 1;
											var focused = 'txtLedgerNo['+focusedvrec+']';
											document.getElementById(focused).focus();
										});
										ReloadGridDt(filepath,'PurchasesJv-TOT','TOTALS','totals',trid,-1);
										}
						}	
					else {
							//document.getElementById('testdatagrid').innerHTML =  xmlhttp.responseText;
							 	
							ReloadGrid(filepath,'JP',trid,'datagrid','','','ADD','','','','',0,'',function() {
											var focusedvrec = parseInt(vrec) + 1;
											var focused = 'txtLedgerNo['+focusedvrec+']';
											document.getElementById(focused).focus();
										});
							ReloadGridDt(filepath,'PurchasesJv-TOT','TOTALS','totals',trid,-1);
						
						}
				}
		}
		
		
			
  }


	xmlhttp.open("GET","Form_PurchasesJV_Actions.php?mainaction="+vMainMode+"&actionmode=&SId="+vId+"&SDtId="+vDTId+"&PDate="+vDate+"&PMaturityDate="+vMaturityDate+
									"&PSalesmanCode="+vSalesman+"&PCurr="+vCurr+"&PProjectCode="+vProjectCode+"&PCostCentCode="+vCostCentCode+"&PDiscountPer="+vDiscountPer+"&PDiscountAmt="+vDiscountAmt+
									"&PInvoiceDate="+vInvoiceDate+
									"&PVatChkbx="+vVatChkbx+"&PManualNo="+vManualNo+"&PVatPrefix="+vVatPrefix+"&PRemark="+vRemark+"&mode="+vmode+
									"&value1="+value1+"&value2="+value2+"&value3="+value3+"&value4="+value4+"&value5="+value5+
									"&value6="+value6+"&value7="+value7+"&value8="+value8+"&value9="+value9+"&value10="+value10+
									"&value11="+value11+"&value12="+value12+"&value13="+value13
									,true);
	xmlhttp.send();   
}
 	
	
</script>


<script type="text/javascript" language="javascript">

function SetVatDt(indx) {
	x = document.purchasesjv;
	var vmode = x.actionmode.value;
	var txtDtVat = 'txtDtVat[' + indx + ']';
	var chkDtVat = 'chkDtVat[' + indx + ']';
	
	document.getElementById(txtDtVat).value = 0;
	
	if (document.getElementById(chkDtVat).checked) document.getElementById(txtDtVat).value = 1;
	   
}

function dokeypurchasesjvdt(e,obj,rowindx,columnslist,vbutname){





	//if (e.ctrlKey == false)
	//return;
	var vindx = rowindx;
	if (!e) { var e = window.Events;}
    var intKey = (window.Events) ? e.which : e.keyCode;


	var columnsarray=new Array();
	columnsarray = columnslist.split(",");
	var firstcell = columnsarray[0];
	var lastcell  = columnsarray[columnsarray.length-1];
	var arrayname = obj.id.split("[");

	if (( ((intKey == 37) || (intKey == 39)) && e.ctrlKey )  ||  (intKey == 13))
	{
		var cindx = columnsarray.indexOf(arrayname[0]);
		
		if (intKey == 37) { //left key
			var nextindx = Number(cindx) - 1;
		}
		if (intKey == 13 || intKey == 39) { //right key
			var nextindx = Number(cindx) + 1;
		}


		var nextitem = columnsarray[nextindx] + '[' + rowindx + ']' ;

		
		if (document.getElementById(nextitem)) {
			var nextobj = document.getElementById(nextitem);
			nextobj.focus();
			if(nextobj.type!='select-one')
			nextobj.select();
		}
		else { 
			var nextrow  = rowindx + 1;
			var nextcell = firstcell + '[' +  nextrow + ']';
				
			var imgname = 'imgSubmit' + vbutname + '[' +  rowindx + ']';
			
			document.getElementById(imgname).click();
			
			if (document.getElementById(nextcell) ) {
				document.getElementById(nextcell).focus();
				if(document.getElementById(nextcell).type!='select-one')
				document.getElementById(nextcell).select();
			}	
					
		}	
	}
	else if ( ((intKey == 38) || (intKey == 40)) && e.ctrlKey) {
		
		if (intKey == 38) 
			rowindx = rowindx - 1;
		if (intKey == 40) 
			rowindx = rowindx + 1;
			
		var nextitem =  arrayname[0] + '[' + rowindx + ']';
		if (document.getElementById(nextitem)) {
			var nextobj = document.getElementById(nextitem);
			nextobj.focus();
			if(nextobj.type!='select-one')
			nextobj.select();
		}	
	}

}



function showsearchpage(vtype,vtext1,vtext2,vtext3,vtext4,vtext5,vwidth,vheight){
	var text1value = document.getElementById(vtext1).value;

	win = window.open('List_AccountNo.php?PLedger_Number='+text1value+'&item=2&vtype='+vtype+'&vtext1='+vtext1+'&vtext2='+vtext2+'&vtext3='+vtext3+'&vtext4='+vtext4+'&vtext5='+vtext5+'&text1value='+text1value, 'Search', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width='+vwidth+',height='+vheight+',top=100,left=100');		
	win.creator = self;
	win.focus();	
	}


function showsearchpage1(vtype,vtext1,vtext2,vtext3,vtext4,vtext5,vtext6,vwidth,vheight){

	
	
	
		var text2value = document.getElementById(vtext2).value;
		
		
	win = window.open('List_AccountNo.php?PLedger_Name='+text2value+'&item=2&vtype='+vtype+'&vtext1='+vtext1+'&vtext2='+vtext2+'&text1value='+text2value, 'Search', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width='+vwidth+',height='+vheight+',top=100,left=100');		
	win.creator = self;
	win.focus();
	}
	
	
function setaccountno(val,vfield1,vfield2,vgridname,btnname,btnname1,codedesc){

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
				{ 	
					var data = xmlhttp.responseText.split("[BRK]");
					var resp = data[1];
					if (resp==0)
						{
							if (codedesc==1)
								document.getElementById(btnname).click();
							else	
								document.getElementById(btnname1).click();
						}
						else	{							
								if (codedesc==1)
									document.getElementById(vfield2).value = resp;
								else	
									document.getElementById(vfield1).value = resp;
								}
				}
		}
  }


  if (codedesc==1)
	xmlhttp.open("GET","Form_PurchasesJV_Actions.php?vaccountno="+val+"&codedesc="+codedesc+"&actionmode=checkaccountno",true);
else	
  if (codedesc==2)
	xmlhttp.open("GET","Form_PurchasesJV_Actions.php?vaccountname="+val+"&codedesc="+codedesc+"&actionmode=checkaccountno",true);  
  

xmlhttp.send();

}	

function showlistManualNo(str,indx) {
	x = document.purchasesjvdt;

	var txtLedgerNo = 'txtLedgerNo[' + indx + ']';
	var vTransType = 'JS';
	var vLedgerNo  = document.getElementById(txtLedgerNo).value;
	
	window.open('Form_Trans_ListManualNo.php?index='+indx+'&str='+str+'&PTransType='+vTransType+'&PLedgerNo='+vLedgerNo,'Form_Trans_ListManualNo','width=740,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function CalcDtTotal(option, dec ,indx) {
	x = document.purchasesjvdt;
	
	
	
	if (!e) { var e = window.event;}
    var intKey = (window.Event) ? e.which : e.keyCode;
	
	
	var txtDtQty = 'txtDtQty[' + indx + ']';
	var txtDtPrice = 'txtDtPrice[' + indx + ']';
	var txtDtDiscPer = 'txtDtDiscPer[' + indx + ']';
	var txtDtTotal = 'txtDtTotal[' + indx + ']';
	
	
	var vDtQty     = document.getElementById(txtDtQty).value;
	var vDtPrice   = document.getElementById(txtDtPrice).value;
	var vDtDiscPer = document.getElementById(txtDtDiscPer).value;
	var vDtTotal   = document.getElementById(txtDtTotal).value;
	
	if (vDtPrice == '')   vDtPrice = 0;
	if (vDtQty == '')     vDtQty = 0;
	if (vDtDiscPer == '') vDtDiscPer = 0;
	if (vDtTotal == '')   vDtTotal = 0;
	
	vDtQty     = parseFloat(vDtQty);	
	vDtPrice   = parseFloat(vDtPrice);	
	vDtDiscPer = parseFloat(vDtDiscPer);	
	vDtTotal   = parseFloat(vDtTotal);	
	
	if (option == 1 && vDtQty > 0 && vDtPrice >= 0)
		document.getElementById(txtDtTotal).value = Math.round(((vDtQty*vDtPrice) - (vDtQty*vDtPrice*vDtDiscPer/100))*Math.pow(10,dec))/Math.pow(10,dec);
	else if (option == 2 && intKey != 9 && vDtQty > 0 && vDtTotal >= 0)
		document.getElementById(txtDtPrice).value = Math.round((vDtTotal/vDtQty)*Math.pow(10,dec))/Math.pow(10,dec);

}
function CheckFields(ModuleRestProj, ModuleRestCost){
  
    x = document.purchasesjv;
	
	var vDate = x.txtDate.value;
	var vMaturityDate = x.txtMaturityDate.value;
	var vSupplier = x.txtSupplierCode.value;
	var vProject = x.txtProjectCode.value;
	var vCostCenter = x.txtCostCentCode.value;
	var vCurr = x.txtCurr.value;
	
	var vmode  = x.mainaction.value;
	var vmode1 = x.actionmode.value;
	
	if (vDate=='' )
	{
		alert("Date is mandatory!");
		x.txtDate.focus();
		return 0;
	}
	else if (vMaturityDate=='')
	{
		alert("MaturityDate is mandatory!");
		x.txtMaturityDate.focus();
		return 0;
	}
	else if (vSupplier=='' )
	{
		alert("Supplier is mandatory!");
		x.txtSupplierCode.focus();
		return 0;
	}
	else if (vProject=='' && ModuleRestProj==1)
	{
		alert("Project is mandatory!");
		x.txtProjectCode.focus();
		return 0;
	}
	else if (vCostCenter=='' && ModuleRestCost==1)
	{
		alert("CostCenter is mandatory!");
		x.txtCostCentCode.focus();
		return 0;
	}
	else if (vCurr=='' )
	{
		alert("Currency is mandatory!");
		x.txtCurr.focus();
		return 0;
	}
	else if (document.getElementById('txtWHTPer')) { 
		var vWHTPer = x.txtWHTPer.value;
		var vWHTAmt = x.txtWHTAmt.value;
		
		if (vWHTPer != "" && (vWHTPer*1 != vWHTPer/1 || vWHTPer < 0) ) {
			alert('WHT Percentage should be a NUMERIC and POSITIVE value.');
			x.txtWHTPer.focus();
			return 0;
		}
		else if (vWHTAmt != "" && (vWHTAmt*1 != vWHTAmt/1 || vWHTAmt < 0) ) {
			alert('WHT Amount should be a NUMERIC and POSITIVE value.');
			x.txtWHTAmt.focus();
			return 0;
		}
		
		return 1 ;
	}
	
			
	
	else
	return 1 ;	
}




function ClearPage(){
	if (confirm ( "Are you sure you want to Clear Page?"))
	{
		ClearForm('All');
		x = document.purchasesjv;
		x.mainaction.value='insert';
		x.actionmode.value='';
		x.action='Form_PurchasesJV.php';
		x.submit();
	}	
}

function ClearPage1(){
	x = document.purchasesjv;
	var msg = x.message.value;
	
	ClearForm('All');
	
	x.mainaction.value='insert';
	x.actionmode.value='';
	x.message.value=msg;
	x.action='Form_PurchasesJV.php?message='+x.message.value;
	x.submit();
}

function CancelRecord(){
	x = document.purchasesjv;
	var vmode  = x.mainaction.value;
	var vmode1 = x.actionmode.value;
	var vId    = x.txtPurchasesJVId.value;
	
	if (confirm ( "Are you sure you want to Delete current pending PurchasesJV permenantly?"))
	{
		ClearForm('All');
		x.txtPurchasesJVId.value = vId;
		x.mainaction.value='insert';
		x.actionmode.value='cancel';
		x.action='Form_PurchasesJV.php';
		x.submit();
	}
    
}



function PostChanges(ModuleRestProj, ModuleRestCost) {
	x = document.purchasesjv;
	var vId       = x.txtPurchasesJVId.value;
	var vMainMode = x.mainaction.value;
	var vVatChkbx = x.txtVatChk.value;
	var result = CheckFields(ModuleRestProj, ModuleRestCost);
	
	
	if (x.txtCountDetail.value == 0 || x.txtCountDetail.value == '') {
		alert('Save not allowed. Please add detail(s).');
	}
	else {
	
		if (result == 1) {
			if (confirm ( "Are you sure you want to Save current PurchasesJV?"))
			{
				x.btnPost.disabled = true;
				x.btnCancel.disabled = true;
				x.btnClear.disabled = true;
				
				if (document.getElementById('btnAddDetail'))
					x.btnAddDetail.disabled = true;
				
				x.actionmode.value='post';
				x.action="Form_PurchasesJV_Actions.php?mode=post&mainmode="+vMainMode+"&SId="+vId+"&SVatChkbx="+vVatChkbx;
				x.submit();
			}
		}
	}	
}

	
function DeleteDetail(PDtId) {
	x = document.purchasesjv;
	var vId       = x.txtPurchasesJVId.value;
	var vMainMode = x.mainaction.value;
	var vVatChkbx = x.txtVatChk.value;
	
	var vDelMsg = '';
	vDelMsg = "Are you sure you want to Delete this record ?";
	   
	if (confirm ( vDelMsg ))
	{
		x.action="Form_PurchasesJV_Actions.php?mode=deletedt&mainmode="+vMainMode+"&SId="+vId+"&SDtId="+PDtId+"&SVatChkbx="+vVatChkbx;
		x.submit();
	}

}

function UpdateDetail(PDtId) {
    x = document.purchasesjv; 
	var vId       = x.txtPurchasesJVId.value;
	var vDate     = x.txtDate.value;
	var vVatChkbx = x.txtVatChk.value;
	
	var vMainMode = x.mainaction.value;
	
	win = window.open('Form_PurchasesJVDetail.php?mainaction='+vMainMode+'&actionmode=&PId='+vId+'&PDate='+vDate+'&PDtId='+PDtId+'&PVatChkbx='+vVatChkbx, 'Form_PurchasesJVDetail', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=600,height=300,top=100');		
	win.creator = self;
	win.focus();		

}




//All= All form; D= Detail Part
function ClearForm(Option){
    x = document.purchasesjv;
	var vmode = x.actionmode.value;
	
	if (Option == 'All') {
		
		x.txtPurchasesJVId.value='';
		x.txtRef.value='';
		x.txtJvRef.value='';
		x.txtJvFolio.value='';
		x.txtDate.value='';
		x.txtMaturityDate.value='';
		x.txtSupplierCode.value='';
		x.txtSupplierName.value='';
		x.txtSupplierVat.value='';
		x.txtInvoiceNo.value='';
		x.txtManualNo.value='';
		x.txtVatPrefix.value='';
		x.txtRemark.value='';
		x.txtCurr.value='';
		x.txtProjectCode.value='';
		x.txtCostCentCode.value='';
		
		
		x.txtTaxable.value='';
		x.txtNoneTaxable.value='';
		x.txtGrossTotal.value='';
		x.txtDiscountPer.value='';
		x.txtDiscountAmt.value='';
		x.txtVatAmt.value='';
		x.txtVatChk.value='';
		x.txtNetTotal.value='';
		
		x.txtCountDetail.value='';
				
		x.message.value='';
		
		x.actionmode.value = '';
		x.mainaction.value = '';
		
		x.txtPaymentTerms.value = '';
		x.txtInvoiceDate.value = '';
		
		if (document.getElementById('txtWHTPer')) {
			x.txtWHTChk.value='';
			x.txtWHTAcc.value='';
			x.txtWHTPer.value='';
			x.txtWHTAmt.value='';
		}
	} 
}

function AddDetail(ModuleRestProj, ModuleRestCost) {
	x = document.purchasesjv;
	//x.actionmode.value = '';
	var result = CheckFields(ModuleRestProj, ModuleRestCost);

	var vId           = x.txtPurchasesJVId.value;
	var vDate         = x.txtDate.value;
	var vMaturityDate = x.txtMaturityDate.value;
	var vSupplier     = x.txtSupplierCode.value;
	var vInvoiceNo    = x.txtInvoiceNo.value;
	var vManualNo     = x.txtManualNo.value;
	var vRemark       = x.txtRemark.value;
	var vCurr         = x.txtCurr.value;
	var vProjectCode  = x.txtProjectCode.value;
	var vCostCentCode = x.txtCostCentCode.value;
	var vDiscountPer  = x.txtDiscountPer.value;
	var vDiscountAmt  = x.txtDiscountAmt.value;
	var vVatChkbx     = x.txtVatChk.value;
	
	var vWHTChk = '';
	var vWHTAcc = '';
	var vWHTPer = '';
	var vWHTAmt = '';
	
	if (document.getElementById('txtWHTPer')) {
		vWHTChk = x.txtWHTChk.value;
		vWHTAcc = x.txtWHTAcc.value;
		vWHTPer = x.txtWHTPer.value;
		vWHTAmt = x.txtWHTAmt.value;
	}
	
	vRemark    = vRemark.replace('&','%26');
	vRemark    = vRemark.replace('#','%23');
	vInvoiceNo = vInvoiceNo.replace('&','%26');
	vInvoiceNo = vInvoiceNo.replace('#','%23');
	vManualNo  = vManualNo.replace('&','%26');
	vManualNo  = vManualNo.replace('#','%23');
	
	
	var vMainMode = x.mainaction.value;
	
	if (result == 1) {
		win = window.open('Form_PurchasesJVDetail.php?mainaction='+vMainMode+'&actionmode=&PId='+vId+'&PDate='+vDate+'&PMaturityDate='+vMaturityDate+
		                  '&PSupplierCode='+vSupplier+'&PInvoiceNo='+vInvoiceNo+'&PCurr='+vCurr+'&PProjectCode='+vProjectCode+
						  '&PCostCentCode='+vCostCentCode+'&PDiscountPer='+vDiscountPer+'&PDiscountAmt='+vDiscountAmt+'&PVatChkbx='+vVatChkbx+
						  '&PWHTChk='+vWHTChk+'&PWHTAcc='+vWHTAcc+'&PWHTPer='+vWHTPer+'&PWHTAmt='+vWHTAmt+'&PManualNo='+vManualNo+'&PRemark='+vRemark, 
						  'Form_PurchasesJVDetail', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=600,height=300,top=100');		
		win.creator = self;
		win.focus();		
	}	
	
    
}


function SearchForRef(){
    
	//ClearForm('All');
    x = document.purchasesjv;
    win = window.open('Form_PurchasesJVSearch.php', 'Form_PurchasesJVSearch', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=840,height=500,top=100');		
	win.creator = self;
	win.focus();		
			
}

function SearchForPorderJV(){
	x = document.purchasesjv;
    win = window.open('Form_PurchasesOrderJVPost.php', 'Form_PurchasesOrderJVPost', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1080,height=580,top=100');		
	win.creator = self;
	win.focus();
}

function SearchForPending(){
    //ClearForm('All');
    x = document.purchasesjv;
    win = window.open('Form_PurchasesJVSearchPending.php', 'Form_PurchasesJVSearchPending', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1040,height=570,top=100');		
	win.creator = self;
	win.focus();		
			
}


function ChangeDate(sdate){
	var idate = new String;
	idate = sdate;
      
    dt_array = idate.split("/");
      
    if ((dt_array[0] < 10) && (dt_array[0].length == 1))
		dt_array[0] = "0" + dt_array[0];
        
	if ((dt_array[1] < 10) && (dt_array[1].length == 1))
		dt_array[1] = "0" + dt_array[1];
   	
	fdate = dt_array[1] + "-" + dt_array[0] + "-" + dt_array[2];
	
	return fdate;
}



function CalcDiscount(option, dec, vatapplied, vatperc) {
	x = document.purchasesjv;
	var vmode  = x.mainaction.value;
	var vmode1 = x.actionmode.value;
	
	var TaxAmt		 = x.txtTaxable.value;
	var GrossTotal	 = x.txtGrossTotal.value;
	var DisPer		 = x.txtDiscountPer.value;
	var DisAmt		 = x.txtDiscountAmt.value;
	var VatAmt		 = x.txtVatAmt.value;
	
	if ((DisAmt*1 != DisAmt/1 || DisAmt < 0) ) {
		alert('Discount Amount should be a NUMERIC value');
		x.txtDiscountAmt.focus();
		return 0;
	}
	else if ((DisPer*1 != DisPer/1 || DisPer < 0) ) {
		alert('Discount Percentage should be a NUMERIC value');
		x.txtDiscountPer.focus();
		return 0;
	}
	
	
	if (TaxAmt == '') 		TaxAmt = 0;
	if (GrossTotal == '') 	GrossTotal = 0;
	if (DisPer == '') 		DisPer = 0;
	if (DisAmt == '') 		DisAmt = 0;
	if (VatAmt == '') 		VatAmt = 0;
	
	TaxAmt		 = parseFloat(TaxAmt);
	GrossTotal	 = parseFloat(GrossTotal);
	DisPer		 = parseFloat(DisPer);
	DisAmt		 = parseFloat(DisAmt);
	VatAmt		 = parseFloat(VatAmt);
	
	if (GrossTotal > 0 && (DisPer > 0 || DisAmt > 0)) {
		if (option == 1 )  //Option by Percentage Get The Amount
			x.txtDiscountAmt.value = roundNumber((GrossTotal*DisPer/100),dec);
		else if (option == 2 ){  //Option by Amount Get The Per
		    x.txtDiscountPer.value = roundNumber(((DisAmt/GrossTotal)*100),dec);
				
			if (DisAmt > 0)	x.txtDiscountPer.readOnly = true; 
			else  x.txtDiscountPer.readOnly = false;
					
		}

		// Calculation of Vat Amount and Net Total
		DisAmt = x.txtDiscountAmt.value;
		if (DisAmt == '') DisAmt = 0;
		DisAmt = parseFloat(DisAmt);
		
		VatAmt = 0;
		if (vatapplied == 1 && TaxAmt > 0) {
			VatAmt = roundNumber(((TaxAmt - (DisAmt*TaxAmt/GrossTotal))*vatperc/100),dec);
		} 
			
		NetTotal = 	roundNumber((GrossTotal + VatAmt - DisAmt),dec);
		
		VatAmt 	 = parseFloat(VatAmt);
		NetTotal = parseFloat(NetTotal);
		x.txtVatAmt.value   = formatNumber(VatAmt,dec);
		x.txtNetTotal.value = formatNumber(NetTotal,dec);
		
	}   
	
}

function SetVat(dec, vatapplied, vatperc) {
	x = document.purchasesjv;
	var vmode  = x.mainaction.value;
	var vmode1 = x.actionmode.value;
	
	var TaxAmt       = x.txtTaxable.value;
	var GrossTotal   = x.txtGrossTotal.value;
	var VatAmt       = x.txtVatAmt.value;
	var DisAmt       = x.txtDiscountAmt.value;
	var NetTotal     = x.txtNetTotal.value;
	
	if (TaxAmt == '') 		TaxAmt = 0;
	if (GrossTotal == '') 	GrossTotal = 0;
	if (VatAmt == '') 		VatAmt = 0;
	if (DisAmt == '') 		DisAmt = 0;
	if (NetTotal == '') 	NetTotal = 0;
	
	VatAmt		 = parseFloat(VatAmt);
	NetTotal	 = parseFloat(NetTotal);
	DisAmt		 = parseFloat(DisAmt);
	TaxAmt 		 = parseFloat(TaxAmt);
	GrossTotal 	 = parseFloat(GrossTotal);
	
	if (x.chkVat.checked) {
	
		x.txtVatChk.value = 1;
		
		// Calculation of Vat Amount and Net Total
		
		VatAmt = 0;
		if (vatapplied == 1 && TaxAmt > 0) {
			VatAmt = roundNumber(((TaxAmt - (DisAmt*TaxAmt/GrossTotal))*vatperc/100),dec);
		} 
			
		NetTotal = 	roundNumber((GrossTotal + VatAmt - DisAmt),dec);
		
		VatAmt	   = parseFloat(VatAmt);
		TaxAmt 	   = parseFloat(TaxAmt);
		GrossTotal = parseFloat(GrossTotal);
		NetTotal   = parseFloat(NetTotal);
		
		x.txtTaxable.value    = formatNumber(TaxAmt,dec);
		x.txtGrossTotal.value = formatNumber(GrossTotal,dec);
		x.txtVatAmt.value     = formatNumber(VatAmt,dec);
		x.txtNetTotal.value   = formatNumber(NetTotal,dec);
			
		
	}
	else
	{
		x.txtVatChk.value = 0;
		
		x.txtVatAmt.value   = formatNumber(0,dec);
		x.txtNetTotal.value = formatNumber(roundNumber((NetTotal - VatAmt),dec),dec);
		
	}  
	

}

function SetWHT(dec) {
	x = document.purchasesjv;
	
	if (x.chkWHT.checked) {
		x.txtWHTChk.value = 1;

	} else {
		x.txtWHTChk.value = 0;
		x.txtWHTPer.value = '';
		x.txtWHTAmt.value = '';
	}

}
function CalcWHTAmount(dec) {
	x = document.purchasesjv;
	
	var WHTPer       = x.txtWHTPer.value;
	var NetTotal     = x.txtGrossTotal.value;
	
	if (WHTPer == '') 		WHTPer = 0;
	if (NetTotal == '') 	NetTotal = 0;
	
	WHTPer		 = parseFloat(WHTPer);
	NetTotal	 = parseFloat(NetTotal);
	
	if (x.chkWHT.checked) {
		x.txtWHTChk.value = 1;
		
		if(WHTPer > 0 && NetTotal > 0) {
			x.txtWHTAmt.value = formatNumber(roundNumber((NetTotal * WHTPer/100),dec),dec);
		
		}
	}
}

function roundNumber(num, dec) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
  newnumber = parseFloat(newnumber); 
  
  return newnumber;
}

function formatNumber(num, dec) { // Arguments: number to format, number of decimal places
	return num.toFixed(dec);
}


function PrintPurchasesJV(PId, PRef) {
	var str = '';
	
	if (confirm ( "Do you want to Print current Invoice with reference "+PRef+" ?"))
	{
		str = 'Form_PurchasesJVPreview.php?PurchasesJVId='+PId;
		window.open(str);
	}	
	
	
}
function clearpurchasejvdt(vrec){
var txtLedgerNo = 'txtLedgerNo['+vrec+']';
var txtLedgerName = 'txtLedgerName['+vrec+']';
var txtDescription1 = 'txtDescription1['+vrec+']';
var txtDescription2 = 'txtDescription2['+vrec+']';
var txtDtCurr = 'txtDtCurr['+vrec+']';
var txtDtQty = 'txtDtQty['+vrec+']';
var txtDtPrice = 'txtDtPrice['+vrec+']';
var txtDtDiscPer = 'txtDtDiscPer['+vrec+']';
var txtDtTotal = 'txtDtTotal['+vrec+']';
var txtDtVat = 'txtDtVat['+vrec+']';
var txtDtManualNo = 'txtDtManualNo['+vrec+']';

	
 document.getElementById(txtLedgerNo).value = '';
 document.getElementById(txtLedgerName).value = '';
 document.getElementById(txtDescription1).value = '';
 document.getElementById(txtDescription2).value = '';
 document.getElementById(txtDtCurr).value = '';
 document.getElementById(txtDtQty).value = '';
 document.getElementById(txtDtPrice).value = '';
 document.getElementById(txtDtDiscPer).value = '';
 document.getElementById(txtDtTotal).value = '';
 document.getElementById(txtDtVat).value = '';
 document.getElementById(txtDtManualNo).value = '';

}

function showlistSupplier(str){
	var vdate, vcode;
	vdate=document.purchasesjv.txtDate.value;
	vcode=document.purchasesjv.txtSupplierCode.value;
  
	window.open('List_Supplier.php?frm=2&item='+str+'&PLedger_number='+vcode+'&TransDate='+vdate,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistProject(str){

	window.open('List_SorProject.php?frm=12&item='+str,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistCostcenter(str){

	window.open('List_SorCostcenter.php?frm=12&item='+str,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistProjectDt(str,PIndex){
	XPos = (screen.availHeight - 550) / 2;
	YPos = (screen.availWidth - 850) / 2;
	
	vProjectCode = encodeURIComponent(document.getElementById('projectcodedt['+PIndex+']').value);
	
	window.open('List_SorProject.php?frm=25&index='+PIndex+'&item='+str+'&PProject_Code='+vProjectCode,'List_SorProject','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes,left='+YPos+',top='+XPos);
}

function showlistCostcenterDt(str,PIndex){
	XPos = (screen.availHeight - 550) / 2;
	YPos = (screen.availWidth - 850) / 2;
	
	vCostcentCode = encodeURIComponent(document.getElementById('costcentcodedt['+PIndex+']').value);
	
	window.open('List_SorCostcenter.php?frm=25&index='+PIndex+'&item='+str+'&PCostcent_Code='+vCostcentCode,'List_SorCostcenter','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes,left='+YPos+',top='+XPos);
}

function showlistLedgers(str){
	x = document.purchasesjv;
	var vTransType = 'JP';
	
	window.open('Form_Trans_ListLedgers.php?str=7&PTransType='+vTransType+'&PAccountType=WHT','mywindow','width=650,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');

}

function SetRecordGridDefaults(PIndex) {
	x = document.purchasesjv;
	
	if (document.getElementById('projectcodedt['+PIndex+']')){
		if (document.getElementById('projectcodedt['+PIndex+']').value == "")
			document.getElementById('projectcodedt['+PIndex+']').value = x.txtProjectCode.value;
	}	

	if (document.getElementById('costcentcodedt['+PIndex+']')){
		if (document.getElementById('costcentcodedt['+PIndex+']').value == "")
			document.getElementById('costcentcodedt['+PIndex+']').value = x.txtCostCentCode.value;
	}
}


function kd(e)
{ 

	x=document.purchasesjv;
    var intKey = (window.Event) ? e.which : e.keyCode;
    if (intKey == 13) { //enter key
        //x.btnSubmitRecord.click();
		
		return false;
    }
    return true;
}

function ChangeMaturityDate(){
	var x = document.purchasesjv;
	var vPaymentTerms   = document.getElementById('txtPaymentTerms').value;
	var vInvoiceDate   = document.getElementById('txtInvoiceDate').value;
	var xmlhttp = getXMLHttpRequest();	
	
  
	if(xmlhttp) {
		
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var data = xmlhttp.responseText;
				x.txtMaturityDate.value = data;
				
			}
		}
				
		xmlhttp.open("GET","Form_Purchase_Actions.php?mode=changedate&mainmode=&txtPaymentTerms="+vPaymentTerms+"&txtInvoiceDate="+vInvoiceDate);
		xmlhttp.send();
		
	}
}
</script>
<title>PurchasesJV</title>


<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">




<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

</head>
<body>
<form name='purchasesjv' method='post' action=''>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='message' value='$message' size='200'>

HERE;

//$message='';

	
//Initializations

	
//Get Default Project
$querydflproject="select project_code as defaultproject from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and default_project=1 ";
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
	
//Get Default Coscenter
$querydflcostcent="select costcent_code as defaultcostcent from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}


if ((empty($txtProjectCode)) && $Module_Project == 1)
	$txtProjectCode = $defaultproject;
if ((empty($txtCostCentCode)) && $Module_CostCenter == 1)
	$txtCostCentCode = $defaultcostcent;	
if (empty($txtCurr))
{
	if ($SupplierCur != '')
		$txtCurr = $SupplierCur;
	else
		$txtCurr = $base1;
}	
if (empty($txtDate))	
	$txtDate = displayToday();
if (empty($txtMaturityDate))	
	$txtMaturityDate = displayToday();
if (empty($txtInvoiceDate))	
	$txtInvoiceDate = displayToday();	

if($ShowInvoiceDate == 1){
	$ndate = explode('/',$txtInvoiceDate);
	$txtInvoiceDate1 = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	$query = "select date_format(DATE_ADD(date_format('$txtInvoiceDate1','%Y-%m-%d'),INTERVAL $txtPaymentTerms DAY), '%d/%m/%Y')  ; ";
	$res = mysql_query($query,$connection);
	$row = mysql_fetch_row($res);
	$txtMaturityDate = $row[0];
	
}

if ($actionmode != "cancel"  && $txtPurchasesJVId != '') {
	// Read From Temp Tables For Selected Record				 
	$query = " Select PurchaseJV_ID, PurchaseJV_Reference, Date_Format(PurchaseJV_Date,'%d/%m/%Y') As PurchaseJV_Date, ".
	         " Date_Format(JournalDt_ValueDate,'%d/%m/%Y') As MaturityDate, ".
			 " PurchasesJV.Supplier_Code As SupplierCode, Suppliers.Ledger_name As SupplierName, Supplier_VAT,   ".
			 " PurchasesJV.Currency_Code, PurchaseJV_InvoiceNo, Purchase_Remark, PurchasesJV.Project_Code, PurchasesJV.Costcenter_Code,  ".
			 " Round(ifNull(PurchaseJV_Taxable,0), ".$DecimalBase."), Round(ifNull(PurchaseJV_NoneTaxable,0), ".$DecimalBase."), ".
			 " Round(ifNull(PurchaseJV_Total,0), ".$DecimalBase.") As GrossTotal, ".
			 " Round(ifNull(PurchaseJV_DiscountPer,0), ".$DecimalBase."), Round(ifNull(PurchaseJV_DiscAmount,0), ".$DecimalBase."),  ".
			 " Round(ifNull(PurchaseJV_VAT,0), ".$DecimalBase.") As PurchaseJV_VAT, ".
			 " Round((ifNull(PurchaseJV_Total,0) + IfNull(PurchaseJV_VAT,0) - IfNull(PurchaseJV_DiscAmount,0)), ".$DecimalBase.") As PurchaseJV_NetTotal,  ".
			 " ifNull(PurchaseJV_VatCheck,0) As PurchaseJV_VatCheck, Journal.Journal_folio, Journal.Journal_refgovernment, ".
			 " PurchaseJV_WHTChk, PurchaseJV_WHTAccount, Round(ifNull(PurchaseJV_WHTPer,0), ".$DecimalBase."), Round(ifNull(PurchaseJV_WHTAmt,0), ".$DecimalBase."), ".
			 " PurchasesJV.PurchaseJv_ManualNo, PurchasesJV.PurchaseJv_VatPrefix,Supplier_PaymentTerms,  Date_Format(InvoiceDate,'%d/%m/%Y') as InvoiceDate ".
			 " From Temp_PurchasesJV As PurchasesJV ".
			 " Left Join Suppliers on PurchasesJV.Supplier_Code = Suppliers.Ledger_Number ".
			 " Left Join Journal on PurchasesJV.Journal_ID = Journal.Journal_ID ".
	         " Where PurchaseJV_ID = ".$txtPurchasesJVId;
			 //" And Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
			 
			 
			 
	//echo $query;
	$result=mysql_query($query,$connection) or die(mysql_error());
    $rows=mysql_fetch_array($result);
	
	$txtPurchasesJVId  = $rows[0];
	$txtRef            = $rows[1];
	$txtDate           = $rows[2];
	$txtMaturityDate   = $rows[3];
	$txtInvoiceDate	   = $rows[29];
	if (empty($txtSupplierCode)) { 
		$txtSupplierCode   = $rows[4];
		$txtSupplierName   = $rows[5];
		$txtSupplierVat    = $rows[6];
		$txtPaymentTerms    = $rows[28];
	}	
	$txtCurr           = $rows[7];
	$txtInvoiceNo      = $rows[8];
	$txtManualNo 	   = $rows[26];
	$txtVatPrefix 	   = $rows[27];
	$txtRemark         = $rows[9];
	$txtProjectCode    = $rows[10];
	$txtCostCentCode   = $rows[11];
	
	
	$txtTaxable      = $rows[12];
	$txtNoneTaxable  = $rows[13];
	$txtGrossTotal   = $rows[14];
	$txtDiscountPer  = $rows[15];
	$txtDiscountAmt  = $rows[16];
	$txtVatAmt       = $rows[17];
	$txtNetTotal     = $rows[18];
	$txtVatChk       = $rows[19];
	
	$txtJvFolio	     = $rows[20];
	$txtJvRef 	     = $rows[21];
	
	$txtWHTChk 	     = $rows[22];
	$txtWHTAcc 	     = $rows[23];
	$txtWHTPer 	     = $rows[24];
	$txtWHTAmt 	     = $rows[25];
	
	
	$query = " Select Count(*) FROM Temp_PurchasesJVDt ".
			 " WHERE PurchaseJV_ID = ".$txtPurchasesJVId;
	$result = selectData($connection, $query);
	$txtCountDetail = $result[0];
	if($txtCountDetail == '') $txtCountDetail = 0;
	
}



//Search/Cancel Mode	
if ($actionmode == "cancel" && $txtPurchasesJVId != '') {
	
	// Delete From Temp Tables Already Filled By Search
	$query = " DELETE FROM Temp_PurchasesJVDt ".
			 " WHERE PurchaseJV_ID = ".$txtPurchasesJVId;
			 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
	if (!updateData($connection, $query, true))
      $message  = mysql_error(); 
	  
	
	  
	$query = " DELETE FROM Temp_PurchasesJV ".
			 " WHERE PurchaseJV_ID = ".$txtPurchasesJVId;
			 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
	if (!updateData($connection, $query, true))
      $message  = mysql_error();   

	$txtPurchasesJVId = '';
}






//$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>PURCHASES JV</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>";
	
	/*if ($FA_PurchaseJVApp == 1)
		echo"<button type='button' id='btnAddDetail' name='btnAddDetail' onclick='AddDetail($Module_RestrictProjCost,$Module_RestrictCost);return false' title='Add Detail''><span><span>Add Detail</span></span></button>";
	*/
echo"<button type='button' id='btnClear' name='btnClear' onclick='ClearPage();return false;' title='Clear Page'><span><span>Clear Page</span></span></button>";		

echo"<button type='button' id='btnCancel' name='btnCancel' ";
if ($actionmode=='cancel' || ($actionmode=='' && $mainaction!='edit'))
			echo " disabled ";
			
echo" onclick='CancelRecord();return false;' title='Cancel Changes'><span><span>Cancel</span></span></button>";				


echo"<button type='button' id='btnPost' name='btnPost' ";
	if (($actionmode=='' || $actionmode=='cancel') && $mainaction!='edit')
		echo " disabled ";
			
echo" onclick='PostChanges($Module_RestrictProjCost,$Module_RestrictCost);return false;' title='Save Changes'><span><span>Save</span></span></button>";	
				

echo"<button type='button' id='btnSearch' name='btnSearch' ";
	if (($actionmode!='' && $actionmode!='cancel') || $mainaction=='edit') 
		    echo " disabled ";
	echo" onclick='SearchForRef();return false;' title='Search By Reference'><span><span>Search</span></span></button>";	

echo"<button type='button' id='btnPending' name='btnPending' ";	

        if (($actionmode!='' && $actionmode!='cancel') || $mainaction=='edit')
		    echo " disabled ";
	echo" onclick='SearchForPending();return false;' title='Search Pending'><span><span>Pending</span></span></button>";	

if($Module_PurchaseOrderJV == 1 ){	
echo"<button type='button' id='btnPorderJv' name='btnPorderJv' ";	
    if (($actionmode!='' && $actionmode!='cancel') || $mainaction=='edit')
		    echo " disabled ";
echo" onclick='SearchForPorderJV();return false;' title='Search Purchase Order JV'><span><span>Purchase Order JV</span></span></button>";	
}
	
echo"	</td></tr></table>";
echo"</td></tr>";
				
echo"<tr><td id='content-body'>
<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
   <tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>";	 

print <<<HERE


		<table border="0" style='table-layout:fixed'>
		  <col width=90>
		  <col width=100>
		  <col width=60>
		  <col width=90>
		  <col width=60>
		  <col width=100>
		  <col width=80>
		  <col width=170>
		  <col width=240>
		<tr bgcolor="#F7F7F7" height='25'> 
          <td valign="center" align='left' ><span class="style1">Reference&nbsp;<span class="style16">*</span></span></td>
		  <td><font face='Verdana' size='2'><b>&nbsp;$txtRef</b></font>
			<input type='hidden' name='txtJvFolio' value='$txtJvFolio' >
			<input type='hidden' name='txtJvRef' value='$txtJvRef' >
			<input type='hidden' name='txtCountDetail' value='$txtCountDetail' >
			<input type='hidden' name='txtPurchasesJVId' id='txtPurchasesJVId' value='$txtPurchasesJVId' size='10'>
			<input type="hidden" name="txtRef" value="$txtRef" size='10' >
		  </td>

HERE;
if ($Module_PostToJournal == 1) {
	echo " <td align='left'><span class='style1'>Folio</span></td>	
		   <td><font face='Verdana' size='2'><b>&nbsp;$txtJvFolio</b></font></td>
		   <td align='left'><span class='style1'>JV Ref.</span></td>	
		   <td><font face='Verdana' size='2'><b>&nbsp;$txtJvRef</b></font></td>";

} else {
	echo" <td colspan='4'>&nbsp;</td>";
}	
		  
print <<<HERE
		  <td align="left"><span class="style1">Date&nbsp;<span class="style16">*</span></span></td>
		  <td><input type='text' name='txtDate' value='$txtDate' maxlength='10' style='width:90px;' readonly >
		                   <A HREF="#" onClick="displayCalendar(document.purchasesjv.txtDate,'dd/mm/yyyy',this);  return false; " NAME="anchor1" ID="anchor1"><img src='./img/cal.gif' border=0 align=center></A></td>

HERE;


if ($txtSupplierCode != "" ){				
	$query = " SELECT Suppliers.Project_Code FROM Suppliers ".
	         " WHERE Suppliers.Ledger_Number = '".$txtSupplierCode."'";
	$result = selectData($connection, $query);

	if ($txtPurchasesJVId == "" && $Supplier_ValidateProject == 1) {
		$txtProjectCode = $result[0];
	}
	
}

// Check If Vat Applied
$VatApplied = 0;

if (insertDate($txtDate) > $Setup_VatStartDate && $Setup_VatPercentage != 0) {		  
  $VatApplied = 1;
}
$AllowModifyVAT = '';
if ($ModifyVAT == 0) $AllowModifyVAT = ' disabled ';



$Parameters = array();
$Parameters['Login_AllowPrice'] = $Login_AllowPrice;
$Parameters['Login_NegativeQty'] = $Login_NegativeQty;
$Parameters['Login_ModifySellingPrice'] = $Login_ModifySellingPrice;
$Parameters['Login_ModifyDiscount'] = $Login_ModifyDiscount;
$Parameters['Allow_MinimumPrice'] = $Allow_MinimumPrice;
$Parameters['Module_Unit1Method'] = $Module_Unit1Method;
$Parameters['Module_Pack'] = $Module_Pack;
$Parameters['Module_DoubleUnit'] = $Module_DoubleUnit;
$Parameters['Module_ProjectDt'] = $Module_ProjectDt;
$Parameters['Module_CostCenterDt'] = $Module_CostCenterDt;
$Parameters['Module_RestrictProjCost'] = $Module_RestrictProjCost;
$Parameters['Module_RestrictCost'] = $Module_RestrictCost;
$Parameters['ShowCostCentLayout'] = $ShowCostCentLayout;
$Parameters['DecimalBase'] = $DecimalBase;
$Parameters['Setup_VatPercentage'] = $Setup_VatPercentage;
$Parameters['Module_AdditionalTaxes'] = $Module_AdditionalTaxes;
$Parameters['VatApplied'] = $VatApplied;
$Parameters['AllowModifyVAT'] = $AllowModifyVAT;
$Parameters['txtSupplierCode'] = $txtSupplierCode;
$Parameters['costcent_applyvat'] = $costcent_applyvat;
$Parameters['FA_PurchaseJVVAT'] = $FA_PurchaseJVVAT;
$_SESSION['PurJv_parameters']  = $Parameters;


echo"<td rowspan='5' valign='top'>";
/*
<table border='0' style='table-layout:fixed' cellspacing=0 cellpadding=0>";
echo"<tr>
		<td id='total-top' style='width:240px;height:12px;'></td>
		</tr>
		<tr><td id='total-content' style='width:240px;height:12px;'>";
				
		echo"	<table border='0' cellspacing=0 width=100% >
		<col width=100>
		<col width=110>
			
		<tr  height='20'>
			<td valign='center' align='right' ><span class='style10'>Taxable</span>&nbsp;</td> 
			<td align='left'><input type='text' name='txtTaxable' value='$txtTaxable' maxlength='20' readonly style='text-align:right; width:100px; height=18px;'></td>
		</tr>
		<tr  height='25'>
			<td valign='center' align='right' ><span class='style10'>None Taxable</span>&nbsp;</td> 
 			<td align='left'><input type='text' name='txtNoneTaxable' value='$txtNoneTaxable' maxlength='20' readonly style='text-align:right; width:100px; height=18px;'></td>
		</tr>
		<tr  height='20'>
			<td valign='center' align='right' ><span class='style10'>Gross Total</span>&nbsp;</td> 
			<td align='left'><input type='text' name='txtGrossTotal' value='$txtGrossTotal' maxlength='20' readonly style='text-align:right; width:100px; height=18px;'></td>
		</tr>
		<tr  height='20'>
			<td valign='center' align='right' ><span class='style10'>Discount</span>&nbsp;</td> 
			<td align='left'><input type='text' name='txtDiscountPer' value='$txtDiscountPer' maxlength='5'  style='text-align:right; width:40px; height=18px;' onKeyup='CalcDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage);' $DiscountMode ><span class='style10'>%</span>
			                 <input type='text' name='txtDiscountAmt' value='$txtDiscountAmt' maxlength='20' style='text-align:right; width:44px; height=18px;' onKeyup='CalcDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage);' $DiscountMode ></td>
		</tr>";
		  


echo "  <tr  height='20'>
			<td valign='center' align='right' >
				<input type='hidden' name='txtVatChk' id='txtVatChk' value='$txtVatChk' size='1'/>
				<input name='chkVat' type='checkbox' id='chkVat' value='1' onClick='SetVat($DecimalBase,$VatApplied,$Setup_VatPercentage);'   "; 
			  if ($txtVatChk==1 ) echo " checked $AllowModifyVAT />";  else echo " $AllowModifyVAT />";   
	          
echo "   	<span class='style10'>VAT $Setup_VatPercentage%</span></td> 
			<td align='left'>
				<input type='text' name='txtVatAmt' value='$txtVatAmt' maxlength='20' style='text-align:right; width:100px; height=18px;' readonly >
			</td>
		</tr>";

	
echo"   <tr  height='20'>
			<td valign='center' align='right' ><span class='style10'>Net Total</span>&nbsp;</td> 
			<td align='left'><input type='text' name='txtNetTotal' value='$txtNetTotal' maxlength='20' readonly style='text-align:right; width:100px; height=18px;";
			if($txtNetTotal<0) echo " color:#FF0000;";
			echo"'></td>
		</tr>
		  
		</table></td></tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr>";	
		echo"</table>";*/
		
		
echo"	<div id='totals' style='width:240px; margin-top:2px; float:right;border:0px solid #000000;'>";
ShowPurchasesJvTotals($connection,$txtPurchasesJVId,$_SESSION['PurJv_parameters'],$_SESSION['gJv_arrayaccess']);
echo"	</div>"; 

		
		echo"</td></tr>";	


echo"   <tr bgcolor='#F7F7F7' height='25'>
		  <td align='left' ><span class='style1'>Supplier&nbsp;<span class='style16'>*</span></span></td>";
echo"     <td colspan='5' ><span class='style1'>";
echo"     <label>
           <input type='text'   name='txtSupplierCode' id='txtSupplierCode' value='$txtSupplierCode' style='width:95px;' maxlength='10'  />
           <input type='text'   name='txtSupplierName' id='txtSupplierName' value='$txtSupplierName' style='width:285px;'  />
		   <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Supplier ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistSupplier(1); ' /> </span></label>
		   <input type='hidden' name='txtSupplierVat' id='txtSupplierVat' value='$txtSupplierVat' />
          </label>
          </span>
          </td>";

echo"     <td align='left' ><span class='style1'>Maturity&nbsp;<span class='style16'>*</span></span></td>
          <td ><input type='text' name='txtMaturityDate' value='$txtMaturityDate' maxlength='10' style='width:90px;' readonly >
						  <A HREF='#' onClick=\"displayCalendar(document.purchasesjv.txtMaturityDate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><img src='./img/cal.gif' border=0 align=center></A></td>";		  
echo"   </tr>";

if($ShowInvoiceDate == 1) {		
echo"<tr height='20px'>";  
echo"     <td align='left'><span class='style1'>Payment Terms</span></td>";
echo"     <td colspan='2' ><span class='style1'><input type='text' name='txtPaymentTerms' id='txtPaymentTerms' value='$txtPaymentTerms' style='width:100px;' readonly /></span></td>";
echo "<td></td><td></td><td align='right'  colspan='2' ><span class='style1'>Invoice Date&nbsp;<span class='style16'></span></span></td>
	 <td ><input type='text' name='txtInvoiceDate' id='txtInvoiceDate' value='$txtInvoiceDate'   onBlur=checkDateFormat(this,$strt,$end)    onchange='ChangeMaturityDate();' maxlength='10' style='width:80px;' >
	 <A HREF='#' onClick=\"displayCalendar(document.purchasesjv.txtInvoiceDate,'dd/mm/yyyy',this); return false;\" NAME='anchor1' ID='anchor1'><img src='./img/cal.gif' border=0 align=center></A></td>";
echo"</tr>";	 
} else {
	echo "<input type='hidden' name='txtPaymentTerms' id='txtPaymentTerms' value=''/><input type='hidden' name='txtInvoiceDate' id='txtInvoiceDate' value='' >";
}
echo"   <tr bgcolor='#F7F7F7'  height='25'>
		  <td align='left' valign='center'><span class='style1'>Remark</span></td><td colspan='5' > <span class='style1'>
		  <input type='text' name='txtRemark' value='".htmlspecialchars($txtRemark, ENT_QUOTES)."'  style='width:385px;' maxlength='200' ></span></td>";

echo"     <td align='left' ><span class='style1'>Currency&nbsp;<span class='style16'>*</span></span></td>";
echo"     <td ><span class='style1'><select name='txtCurr' id='txtCurr' style='width:90px;'>";
			echo"<option></option>";
			while($curf=mysql_fetch_row($resultcurrency)){
			echo"<option ";
				if ($txtCurr==$curf[0])
					echo " selected ";
				echo" value='$curf[0]'>$curf[0] </option>";
			}
			echo"</select></span></td>";	
echo"   </tr>";		  


echo"   <tr bgcolor='#F7F7F7' height='25'>";

if ($Module_Project	== 1) {
	echo "<td align='left' ><span class='style1'>Shipment Ref &nbsp;<span class='style16'>*</span></span></td>
	      <td align='left' colspan='2'> <span class='style1'>
		    <input type='text' name='txtProjectCode' id='txtProjectCode' value='$txtProjectCode' style='width:120px;' maxlength='15' readonly /></span>
		    <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(1); '  /></span></label></td>";
} else	{
	echo "<td align='left' colspan='3'><input type='hidden' name='txtProjectCode' id='txtProjectCode' value='$txtProjectCode' /></td>";
}		

if ($Module_CostCenter == 1) {		  
	echo" <td align='left' ><span class='style1'>Costcenter&nbsp;<span class='style16'>*</span></span></td>";		
	echo" <td align='left' colspan='2'><span class='style1'>
		    <input type='text' name='txtCostCentCode' id='txtCostCentCode' value='$txtCostCentCode' style='width:130px;' maxlength='15' readonly /></span>
		    <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Costcenter ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistCostcenter(1); ' /></span></label></td>";
} else {
	echo "<td align='left' colspan='3'><input type='hidden' name='txtCostCentCode' id='txtCostCentCode' value='$txtCostCentCode' /></td>";
}			

echo"     <td align='left' ><span class='style1'>Invoice No.</span></td>";
echo"     <td ><span class='style1'><input type='text' name='txtInvoiceNo' value='".htmlspecialchars($txtInvoiceNo, ENT_QUOTES)."' style='width:90px;' maxlength='26'  /></span></td>";
	
echo"   </tr>";

echo"<tr bgcolor='#F7F7F7' height='25'>";
echo"   <td align='left' valign='center'><span class='style1'>Vat Prefix</span></td>
		<td><input type='text' name='txtVatPrefix' value='".htmlspecialchars($txtVatPrefix, ENT_QUOTES)."'  style='width:90px;' maxlength='10' ></td>";
echo"   <td align='left' colspan='4'></td>";
echo"   <td align='left' valign='center'><span class='style1'>Manual No</span></td>
		<td><input type='text' name='txtManualNo' value='".htmlspecialchars($txtManualNo, ENT_QUOTES)."'  style='width:90px;' maxlength='20' ></td>";
echo"</tr>";

if ($Module_PurchasesJvWHT == 1) {
	echo"<tr bgcolor='#F7F7F7' height='25'>";
	echo"	<td valign='center' align='left' ><span class='style1'>
				<input type='hidden' name='txtWHTChk' id='txtWHTChk' value='$txtWHTChk' size='1'/>
				<input name='chkWHT' type='checkbox' id='chkWHT' value='1' onClick='SetWHT($DecimalBase);'   "; 
				if ($txtWHTChk==1 ) echo " checked ";  echo " />WHT"; 
	echo"	</span></td>";
	echo"   <td colspan='2'><span class='style1'><input type='text' name='txtWHTPer' id='txtWHTPer' value='$txtWHTPer' style='width:90px;' maxlength='5' onkeyup='CalcWHTAmount($DecimalBase);' />(%)</span></td>";
	echo"   <td align='left' ><span class='style1'>WHT Amount</span></td>";
	echo"   <td colspan='2'><span class='style1'><input type='text' name='txtWHTAmt' id='txtWHTAmt' value='$txtWHTAmt' style='width:90px;' maxlength='20'  /></span></td>";
	echo"   <td align='left' ><span class='style1'>WHT Account</span></td>";
	echo"   <td ><span class='style1'><input type='text' name='txtWHTAcc' id='txtWHTAcc' value='$txtWHTAcc' style='width:90px;' maxlength='10'  /></span>
				<label><span class='style1'><img src='img/btnopen.jpg' title=' Select Account ' width='15' height='17' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistLedgers(1); '  /></span></label></td>";
	echo"   <td align='left'>&nbsp;</td>";			
	echo"</tr>";
} 	
/*
echo"   <tr><td colspan='9' valign='top'>";
//<div style='overflow:scroll; height:180px; width:980px;' >
echo"     <table border='0' style='table-layout:fixed'>
		  <col width=20>
		  <col width=20>
		  <col width=40>
		  <col width=100>
		  <col width=180>
		  <col width=180>
		  <col width=80>
		  <col width=70>
		  <col width=100>
		  <col width=60>
		  <col width=110>";
		  
		echo"<tr>";
		echo"  <td bgcolor='#F7F7F7' align='left'><span class='style27'>&nbsp;</span></td>";
		echo"  <td bgcolor='#F7F7F7' align='left'><span class='style27'>&nbsp;</span></td>";
		echo"  <td bgcolor='#F7F7F7' align='center'><span class='style27'>Vat</span></td>";
		echo"  <td bgcolor='#F7F7F7' align='left'><span class='style27'>Ledger Number</span></td>";
		echo"  <td bgcolor='#F7F7F7' align='left'><span class='style27'>Description</span></td>";
		echo"  <td bgcolor='#F7F7F7' align='left'><span class='style27'>Description 2</span></td>";
		echo"  <td bgcolor='#F7F7F7' align='center'><span class='style27'>Manual No</span></td>";
		echo"  <td bgcolor='#F7F7F7' align='center'><span class='style27'>Quantity</span></td>";
		echo"  <td bgcolor='#F7F7F7' align='center'><span class='style27'>Price</span></td>";
		echo"  <td bgcolor='#F7F7F7' align='center'><span class='style27'>Disc(%)</span></td>";
		echo"  <td bgcolor='#F7F7F7' align='center'><span class='style27'>Total</span></td>";
        echo"</tr>";
		
		if ($txtPurchasesJVId != '' ) {
		     
			$querydt = " Select PurchasesJVDt.PurchaseJVDt_Id, PurchasesJVDt.Ledger_Number, PurchaseJVDt_Description, PurchaseJVDt_Description2, ".
					   " ifNull(PurchaseJVDt_Quantity,0), Round(ifNull(PurchaseJVDt_Price,0), ".$DecimalBase."), PurchaseJVDt_PriceBase1, PurchaseJVDt_PriceBase2, ".
					   " Round(ifNull(PurchaseJVDt_DiscPer,0), ".$DecimalBase."), Round(ifNull(PurchaseJVDtCalcTotal,0), ".$DecimalBase.") As Total, ".
					   " if(ifNull(PurchaseJVDt_VAT,0) = 1, 'True', 'False') As VatApplied, PurchaseJvDt_ManualNo ".
					   " From Temp_PurchasesJVDt As PurchasesJVDt ".
					   " Where PurchasesJVDt.PurchaseJV_ID = ".$txtPurchasesJVId.
					   " Order By PurchasesJVDt.PurchaseJVDt_Id Desc ";
			//echo $querydt;
			$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
			while($rowsdt=mysql_fetch_array($resultdt)){
			    
				$ct+=1;

				if($ct%2==0)
					$Color="style='background-color:".$grid_color1."'";
				else			
					$Color="style='background-color:".$grid_color2."'";	
						
								
				echo "<tr ".$Color." >";

				echo "	<td align='center' valign='center'><span class='style15'><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=UpdateDetail('".$rowsdt[0]."') title='Edit Record'  ></span></td>";
				echo "	<td align='center' valign='center'><span class='style15'><img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"DeleteDetail('".$rowsdt[0]."')\" title='Delete Record'  ></span></td>";
				echo "	<td align='left'><span class='style15'>".$rowsdt[10]."</span></td>";
				echo "	<td align='left'><span class='style15'>".$rowsdt[1]."</span></td>";
				echo "	<td align='left'><span class='style15'>".$rowsdt[2]."</span></td>";
				echo "	<td align='left'><span class='style15'>".$rowsdt[3]."</span></td>";
				echo "	<td align='left'><span class='style15'>".$rowsdt[11]."</span></td>";
				echo "	<td align='right'><span class='style15'>".$rowsdt[4]."</span></td>";
				echo "	<td align='right'><span class='style15'>".number_format($rowsdt[5], $DecimalBase)."</span></td>";
				echo "	<td align='right'><span class='style15'>".$rowsdt[8]."</span></td>";
				echo "	<td align='right'><span class='style15'>".number_format($rowsdt[9], $DecimalBase)."</span></td>";
				echo "</tr>";
				
			}
			
		 }
		
		echo "</table>";   //</div>

echo"   </td></tr>";
*/
/*
echo"   <tr bgcolor='#D8D8D8' height='30'><td colspan='9' align='center'>";

if ($FA_PurchaseJVApp == 1)
	echo"<input type='button' id='btnAddDetail' name='btnAddDetail' value='Add Detail'  onclick='AddDetail($Module_RestrictProjCost,$Module_RestrictCost)' $style title='Add Detail' >";
		
echo"   <input type='button' id='btnClear' name='btnClear' value='Clear Page'   onclick='ClearPage();' $style title='Clear Page'>";
				
echo"   <input type='button' name='btnCancel' id='btnCancel' value='Cancel' ";//cancel
		if ($actionmode=='cancel' || ($actionmode=='' && $mainaction!='edit'))
			echo " disabled ";
		echo" onclick='CancelRecord();' $style title='Cancel Changes'>";
				
echo"   <input type='button' name='btnPost' id='btnPost' value='Save' ";//post
		if (($actionmode=='' || $actionmode=='cancel') && $mainaction!='edit')
			echo " disabled ";
		echo" onclick='PostChanges($Module_RestrictProjCost,$Module_RestrictCost);' $style	title='Save Changes' />";

echo"   <input name='btnSearch' type='button' id='btnSearch' value='Search' onclick='SearchForRef();' $style title='Search By Reference' ";
        if (($actionmode!='' && $actionmode!='cancel') || $mainaction=='edit') 
		    echo " disabled ";
		 echo " />";
				
echo"   <input name='btnPending' type='button' id='btnPending' value='Pending' onclick='SearchForPending();' $style title='Search Pending'  ";
        if (($actionmode!='' && $actionmode!='cancel') || $mainaction=='edit')
		    echo " disabled ";
		echo " />";

echo"   </td></tr>";	*/
echo"   </table></td></tr>"; 



echo"   <tr bgcolor='#ffffff'><td colspan='10' valign='top' >";

echo"	<div id='datagrid' class='' style='width:1200px;' >";
$Parameters = array();
$Parameters['txtSupplierCode'] 			= $txtSupplierCode;
$Parameters['Login_ModifySellingPrice'] = $Login_ModifySellingPrice;
$Parameters['Login_ModifyDiscount'] 	= $Login_ModifyDiscount;
$Parameters['DecimalBase'] 				= $DecimalBase;
$Parameters['Module_RestrictProjCost'] 	= $Module_RestrictProjCost;
$Parameters['Module_RestrictCost'] 		= $Module_RestrictCost;
$Parameters['FI_SalesCheckSalesman'] 	= $FI_SalesCheckSalesman;
$Parameters['Module_ProjectDt'] 		= $Module_ProjectDt;
$Parameters['Module_CostCenterDt'] 		= $Module_CostCenterDt;
$_SESSION['purjv_parameters'] 		 	= $Parameters;

//Show Items Grid
ShowPurchasesJvDtGrid($connection,'JS',$txtPurchasesJVId, $_SESSION['purjv_parameters']);
				 
echo"	  </div>"; 
echo"   </td></tr>"; 

	echo"   <tr><td colspan='9' valign='top'>";

echo"	<div id='testdatagrid' class='' style='width:1090px;' >";


echo"	  </div>"; 

echo"   </td></tr>"; 


echo"   </table></td></tr><tr height='30'><td align='center' id='content-bottom' ></td></tr>";  

echo" </table>  ";//end of last table menu
	
if ($actionmode == "post"  && $PrId != '') {
	echo '<script type="text/javascript" language="javascript">';
	
	if ($Login_FastPrint == 1) {
		echo ' PrintPurchasesJV(\''.$PrId.'\', \''.$PrRef.'\'); ';
		
	}		
	echo ' ClearPage1(); ';
	echo '</script>';

}

print <<<HERE
</form>
</body>

HERE;

?>