<?php

session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("Functions_Purchase.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");


//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1 as Base1, base2 as Base2 from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


$PTransId    		= $_POST['PTransId']; if ($PTransId == ""){$PTransId = $_GET["PTransId"];}
$actionmode    		= $_POST['actionmode']; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}
$message  			= $_POST['message']; if ($message == ""){$message = $_GET["message"];}
$txtTransRate    	= $_POST['txtTransRate']; if ($txtTransRate == ""){$txtTransRate = $_GET["txtTransRate"];}
$txtTransRateB2  	= $_POST['txtTransRateB2']; if ($txtTransRateB2 == ""){$txtTransRateB2 = $_GET["txtTransRateB2"];}


print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="css/style.css" type="text/css" />
<script type="text/javascript" language="javascript">


function SubmitRecord() {
	x = document.purchaserates;
	
	var result = CheckFields();
	var PurId  = x.PPurId.value;
	   
	if (result == 1) {
		x.action="Form_Purchase_Actions.php?mode=changerates&SPurId="+PurId;
		x.submit();
		
	}	
}

function ChangeLocation() {
	x = document.purchaserates;
	var vMode = x.actionmode.value;
	var vMsg  = x.message.value;
	

	if (vMsg == '' && vMode == 'rateschanged') {
		opener.document.purchase.txtPurId.value = x.PPurId.value;
		opener.document.purchase.submit();
		
		self.close();
	}

	if (document.getElementById('txtPurRate').type == 'text') {
		x.txtPurRate.focus();
		x.txtPurRate.select();
	}	
}


function CheckFields(){
  
    x = document.purchaserates;
	
	var vPurRate   = x.txtPurRate.value;
	var vPurRateB2 = x.txtPurRateB2.value;
	
	vPurRate   = parseFloat(vPurRate);
	vPurRateB2 = parseFloat(vPurRateB2);
	
	if (vPurRate == '')   vPurRate = 0;
	if (vPurRateB2 == '') vPurRateB2 = 0;
	
	if ((vPurRate != '' && vPurRate*1 != vPurRate/1) ) {
		alert('Rate should be a NUMERIC value.');
		x.txtPurRate.focus();
		return 0;
	}
	else if ((vPurRateB2 != '' && vPurRateB2*1 != vPurRateB2/1) ) {
		alert('Rate Base2 should be a NUMERIC value.');
		x.txtPurRateB2.focus();
		return 0;
	}
		
	
	else
	return 1 ;	
}



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

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Purchase Rates</title>


<script type="text/javascript" src="dropdown.js"></script>
<link rel="stylesheet" href="default.css" type="text/css" />
<link rel="stylesheet" href="short.css" type="text/css" />
<link rel="stylesheet" href="dropdown.css" type="text/css" />
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 10px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>

</head>
<body onLoad='ChangeLocation();' >
<form name='transrates' method='post'>
<input type='hidden' name='message' value='$message'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='PTransId' value='$PTransId'>

HERE;

	
//Edit Purchase Detail Mode
if($PTransId != ''){
	$query = " Select trans_ID, Currency_Code ".
	         " From Temp_trans As trans ".
	         " Where trans_ID = ".$PPurId;
	//echo $query;
	$result=mysql_query($query,$connection) or die(mysql_error());
    $rows=mysql_fetch_array($result);
	
	$txtCurCode = $rows[1];
}



$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";
		  
echo"   <table border='0' style='table-layout:fixed' align='center' valign='top'>
			<col width=120>
			<col width=100>
			<tr bgcolor='#F7F7F7' height='25'>
				<th colspan='2'><span class='style1'>Rate / Parity </span></th></tr>
			<tr bgcolor='#F7F7F7' height='25'>	
				<td colspan='2' align='center'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td></tr>			 
			 
			<tr bgcolor='#F7F7F7' height='25'>
				<td valign='center' align='left' ><span class='style12'>Currency</span></td>
				<td><b>$txtCurCode</b></td>
			</tr> ";
		
		
echo"		<tr bgcolor='#F7F7F7' height='25'>
				<td valign='center' align='left' ><span class='style12'>Rate Cur / Base1</span></td>
				<td align='center'><input type='text' name='txtPurRate' value='$txtPurRate' maxlength='20' style='text-align:right; font-size:12px; width:90px;' $vRateReadOnly /></td>
			</tr>	 
			<tr bgcolor='#F7F7F7' height='25'>
				<td valign='center' align='left' ><span class='style12'>Rate Base2 / Base1</span></td>
				<td align='center'><input type='text' name='txtPurRateB2' value='$txtPurRateB2' maxlength='20' style='text-align:right; font-size:12px; width:90px;' $vRateB2ReadOnly /></td>
			</tr>";
echo"   	<tr bgcolor='#F7F7F7' height='35'>
				<td colspan='2' align='center' bgcolor=#D8D8D8>
				 <button type='button' id='btnSubmitRecord' name='btnSubmitRecord' onclick='SubmitRecord(); return false;' title='Submit'><span><span>Submit</span></span></button>
			</td>
			</tr>
        </table>";

	

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

HERE;

?>