<?php

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


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

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

$querylog="SELECT FA_SourceFunds as loginaccess 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");
	

if($mode=='')
	$mode='insert';

class tabs {
	var $name;
	var $tabs;
	var $active;
	var $current;
function start($name){
		if (empty($this->active)){ $this->active = $name; }
		$this->current = $name;
		ob_start();
	}
	function end(){
		$this->tabs[$this->current] = ob_get_contents();
		ob_end_clean();
	}
	function run(){
	
		if (count($this->tabs) > 0){
			echo "<DIV CLASS='tabs'>\n";
			$jsClear = "";
			foreach($this->tabs as $tabname => $tabcontent){
				$tabid = "tab_".$this->name."_$tabname";
				$contentid = "tabcontent_".$this->name."_$tabname";
				$jsClear .= "\tdocument.getElementById('$tabid').className = 'tab_inactive';\n";
				$jsClear .= "\tdocument.getElementById('$contentid').style.display = 'none';\n";
			}
			echo "<script type=\"text/javascript\">\n";
			echo "function tab_".$this->name."(id,fieldname){\n";
			
			
			
			echo "$jsClear";
			echo "\tdocument.getElementById('tab_".$this->name."_'+id).className = 'tab_active';\n";
			echo "\tdocument.getElementById('tabcontent_".$this->name."_'+id).style.display = '';\n";
			echo"	
				//alert(fieldname);
				if(fieldname!='' && fieldname!=undefined)
					
					document.bank[fieldname].focus();
				";
			echo "}\n";
			echo "</script>\n";
			foreach($this->tabs as $tabname => $tabcontent){
				$tabid = "tab_".$this->name."_$tabname";
				$contentid = "tabcontent_".$this->name."_$tabname";
				echo "<DIV CLASS='";
				if ($this->active == $tabname){ echo "tab_active"; }else{ echo "tab_inactive"; }
				echo "' ID='$tabid' ";
				echo "onClick=\"tab_".$this->name."('$tabname');\">$tabname</DIV>\n";
			}
			echo "<DIV STYLE='float: center; clear:both;'></DIV>\n";
			foreach($this->tabs as $tabname => $tabcontent){
				$contentid = "tabcontent_".$this->name."_$tabname";
				echo "<DIV ID = '$contentid' CLASS='tab_content' STYLE='display: ";
				if ($this->active == $tabname){ echo "block"; }else{ echo "none"; }
				echo ";'>$tabcontent</DIV>\n";
			}
			echo "</DIV>\n";
			echo "<DIV STYLE='clear: both;'></DIV>\n";
		}}
}	
	

print <<<HERE

<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
		<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Source Funds</title>

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

function showlistUsers(index){
	window.open('list_users.php?frm=4&index='+index,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function addFundUser(index, fund_code){
	var newfund_code = document.getElementById('txtfundcode').value;
	var funddesc 	 = document.getElementById('txtfunddesc').value;
	funddesc 		 = encodeURIComponent(funddesc);
	
	var funddt_id = document.getElementById('funddtid'+index).value;
	var usercode  = document.getElementById('usercode'+index).value;
	if (funddt_id == '') 
		var mode = 'insertfund';
	else 
		var mode = 'updatefund';
	
	if (newfund_code == ''){
		alert('Fund Code is Empty!');
		document.getElementById('txtfundcode').focus();
		return 0;
	} else if (usercode == ''){
		alert('User Code is Empty!');
		document.getElementById('usercode'+index).focus();
		return 0;
	} else {
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp5=new XMLHttpRequest();
		}
		else{//code for IE6, IE5
			xmlhttp5=new ActiveXObject("Microsoft.xmlhttp");
		}
		xmlhttp5.onreadystatechange=function()
		{
			if (xmlhttp5.readyState==4 && xmlhttp5.status==200)
			{
				var data = xmlhttp5.responseText.split('[BRK]');
				if (data[1] != '') alert(data[1]);
				if (newfund_code != fund_code){
					document.sourceFund.submit();
				} else {
					document.getElementById('fundGrid').innerHTML = '';
					document.getElementById('fundGrid').innerHTML = data[2];
				}
			}			
		}
		
		xmlhttp5.open("GET","Form_SourceFunds_action.php?mode="+mode+"&fund_code="+fund_code+"&newfund_code="+newfund_code+"&funddesc="+funddesc+"&funddt_id="+funddt_id+"&usercode="+usercode);
		xmlhttp5.send();
	
	}
}

function deleteFundUser(index, fund_code){
	var funddt_id = document.getElementById('funddtid'+index).value;
	if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp5=new XMLHttpRequest();
		}
		else{//code for IE6, IE5
			xmlhttp5=new ActiveXObject("Microsoft.xmlhttp");
		}
		xmlhttp5.onreadystatechange=function()
		{
			if (xmlhttp5.readyState==4 && xmlhttp5.status==200)
			{
				var data = xmlhttp5.responseText.split('[BRK]');
				if (data[1] != '') alert(data[1]);
				document.getElementById('fundGrid').innerHTML = '';
				document.getElementById('fundGrid').innerHTML = data[2];
				
			}			
		}
		
		xmlhttp5.open("GET","Form_SourceFunds_action.php?mode=deleteFundUser&fund_code="+fund_code+"&funddt_id="+funddt_id);
		xmlhttp5.send();
}

function clearFundUser(index, fund_code){
	if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp5=new XMLHttpRequest();
		}
		else{//code for IE6, IE5
			xmlhttp5=new ActiveXObject("Microsoft.xmlhttp");
		}
		xmlhttp5.onreadystatechange=function()
		{
			if (xmlhttp5.readyState==4 && xmlhttp5.status==200)
			{
				var data = xmlhttp5.responseText.split('[BRK]');
				if (data[1] != '') alert(data[1]);
				document.getElementById('fundGrid').innerHTML = '';
				document.getElementById('fundGrid').innerHTML = data[2];
				
			}			
		}
		
		xmlhttp5.open("GET","Form_SourceFunds_action.php?mode=clearFundUser&fund_code="+fund_code);
		xmlhttp5.send();
}

function SubmitRecord(){
	document.sourceFund.action = "Form_SourceFunds_action.php?mode=Post";
	document.sourceFund.submit();	
}
function SetFund(fund_code){
	document.sourceFund.txtfundcode.value = fund_code;
	document.sourceFund.submit();
}
function confirm_delete(fund_code){
	if (confirm("Are you sure You Want to Delete "+fund_code+"?")){
		document.sourceFund.action = "Form_SourceFunds_action.php?mode=deleteRecord&fund_code="+fund_code;
		document.sourceFund.submit();	
	}	
}
function SearchForSup(){
	document.sourceFund.submit();		
}
function ClearKeyword(){
	document.sourceFund.txtKeyword.value = '';	
	document.sourceFund.submit();	
}

function ClearPage(){
	document.sourceFund.txtfundcode.value 	 = '';
	document.sourceFund.txtfundcodeOld.value = '';
	document.sourceFund.txtfunddesc.value 	 = '';
	
	document.sourceFund.action = "Form_SourceFunds.php";
	document.sourceFund.submit();
}

function addPayment(index, payment_code){
	var newpaymentcode = document.getElementById('paymentcode'+index).value;
	var paymentdesc	   = document.getElementById('paymentdesc'+index).value;
	paymentdesc 	   = encodeURIComponent(paymentdesc);
	
	if (payment_code == '')
		var mode = 'insertpayment';
	else 
		var mode = 'updatepayment';
	if (newpaymentcode == '' || paymentdesc == '' ){
		alert("Code and Description Cannot be Empty!");
		return 0;
		
	} else {
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp5=new XMLHttpRequest();
		}
		else{//code for IE6, IE5
			xmlhttp5=new ActiveXObject("Microsoft.xmlhttp");
		}
		xmlhttp5.onreadystatechange=function()
		{
			if (xmlhttp5.readyState==4 && xmlhttp5.status==200)
			{
				var data = xmlhttp5.responseText.split('[BRK]');
				if (data[1] != '') alert(data[1]);
				document.getElementById('paymentGrid').innerHTML = '';
				document.getElementById('paymentGrid').innerHTML = data[2];
				
			}			
		}
		
		xmlhttp5.open("GET","Form_SourceFunds_action.php?mode="+mode+"&payment_code="+payment_code+"&newpaymentcode="+newpaymentcode+"&paymentdesc="+paymentdesc);
		xmlhttp5.send();
	}
}

function deletePayment(index, payment_code){
	
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp5=new XMLHttpRequest();
	}
	else{//code for IE6, IE5
		xmlhttp5=new ActiveXObject("Microsoft.xmlhttp");
	}
	xmlhttp5.onreadystatechange=function()
	{
		if (xmlhttp5.readyState==4 && xmlhttp5.status==200)
		{
			var data = xmlhttp5.responseText.split('[BRK]');
			if (data[1] != '') alert(data[1]);
			document.getElementById('paymentGrid').innerHTML = '';
			document.getElementById('paymentGrid').innerHTML = data[2];
			
		}			
	}
	
	xmlhttp5.open("GET","Form_SourceFunds_action.php?mode=deletePayment&payment_code="+payment_code);
	xmlhttp5.send();
	
}

function clearPayment(index, payment_code){
	
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp5=new XMLHttpRequest();
	}
	else{//code for IE6, IE5
		xmlhttp5=new ActiveXObject("Microsoft.xmlhttp");
	}
	xmlhttp5.onreadystatechange=function()
	{
		if (xmlhttp5.readyState==4 && xmlhttp5.status==200)
		{
			var data = xmlhttp5.responseText.split('[BRK]');
			if (data[1] != '') alert(data[1]);
			document.getElementById('paymentGrid').innerHTML = '';
			document.getElementById('paymentGrid').innerHTML = data[2];
			
		}			
	}
	
	xmlhttp5.open("GET","Form_SourceFunds_action.php?mode=clearPayment&payment_code="+payment_code);
	xmlhttp5.send();
	
}
</script>
<LINK REL="stylesheet" TYPE="text/css" MEDIA="all" HREF="css/tabs.css">

<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.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;
}

.style12 {
	font-size: 9px;
	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>

<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='sourceFund' method='post'>
<input type=hidden name='mode' value='$mode'>

HERE;



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'>Source Funds</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

if ($txtfundcode != ''){
	$query = "select sourceFund_Code, sourceFund_desc	from Source_Funds	where sourceFund_Code = '$txtfundcode' ";	
	$result = mysql_query($query, $connection);
	$row = mysql_fetch_row($result);
	$txtfundcode = $row[0];
	$txtfunddesc = $row[1];
}	

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='38'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px'>";
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='btnPost' name='btnPost' onclick='SubmitRecord(); return false;' title='Save Changes' $Insmode><span><span>Save</span></span></button>";

			
echo"   </td><td align='left' width='250px'>
	<input type='text' name='txtKeyword' value='$txtKeyword' style='width:105px;' onKeyPress='return submitenter(this,event)'>
	<button type='button' id='btnSearch' name='btnSearch' onclick='SearchForSup(); return false;' title='Search Bank'><span><span>Find</span></span></button>
<button type='button' id='btnClear' name='btnClear' onclick='ClearKeyword(); return false;' title='Clear Keyword'><span><span>Clear</span></span></button>
    </td><td align='left'>&nbsp;<span class='style1'><b>Results/page</b></span>
	 <span class='style1'><select name='perpage' id='perpage' value='$perpage' onchange=SearchForSup();>";
	 
	if ($startrange=='')
		$startrange = 0;
	if ($perpage=='')
		$perpage = 20;
		
	for($i=10;$i<=$maxperpage;$i=$i+10){
		if($i==$perpage)
			echo "<option value='$i'  selected>$i</option>";
		else echo "<option value='$i'>$i</option>";
		
	} 
	 echo"</td></tr>";
echo"	</table>";
echo"</td></tr>";

$bleft = "border-left:1px solid gray";
$bright = "border-right:1px solid gray";
$btop = "border-top:1px solid gray";
$bbottom = "border-bottom:1px solid gray";

echo"<tr><td id='content-body'>";		

$tabs = new tabs("Source");	
$tabs->start("Source Funds");

echo"<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
	<col width=65%>";

echo"   <tr bgcolor='#F7F7F7'  ><td valign='top'>";	 

	  
echo" <table border='0' style='table-layout:fixed' align='left' valign='top' width='1100px'>
	<col width='110px'><col width='200px'><col width='110px'><col width='200px'><col width='30px'><col width='350px'>";

echo"<tr bgcolor='#F7F7F7' height='25'><td valign='top' align='left' ><span class='style1'>Fund Code <span class='style26'>*</span></span></td>";
echo"<td valign='top'><input type='text' name='txtfundcode' id='txtfundcode' value='".htmlspecialchars($txtfundcode, ENT_QUOTES)."' style='font-size:12px; width:150px;' maxlength='20' />
			<input type='hidden' name='txtfundcodeOld' id='txtfundcodeOld' value='".htmlspecialchars($txtfundcode, ENT_QUOTES)."' maxlength='20' />
	</td>";
echo"<td valign='top' align='left' ><span class='style1'>Description </span></td>";
echo"<td valign='top'><input type='text' name='txtfunddesc' id='txtfunddesc' value='".htmlspecialchars($txtfunddesc, ENT_QUOTES)."' style='font-size:12px; width:180px;'  maxlength='120' /></td>";
echo "<td></td><td valign='top'><div id='fundGrid' style='width:300px;height:200px;'>";
addSourceFundUser($txtfundcode, $connection);
echo "</div></td></tr>";

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

echo"<td  valign='top'>";

echo"<table style='width:1050px;' border='0' cellspacing=0><col width=100><col width=150>
	<tr>
	<td id='' style='width:1050px;height:15px;' colspan='4'></td>
	</tr>
	 <tr>
	 <td  valign='top' colspan=3 id='' style='width:1050px;'>
	 <table style='width:1050px; position:relative; left:15px;' border='0' cellspacing=0>
	 <col width=50>
	 <col width=400>
	 <col width=650>
	<tr height='30px' style='background-color:darkgray;'><td>&nbsp;</td>
	<td align='center'><span class='style1'><b>Fund Code</b></td>
	<td align='center'><span class='style1'><b>Description</b></td></tr>";
	 
	echo"<tr><td colspan=4 id=''><div style='overflow-y:scroll;height:250px;width:1050px;border:0px solid #000000;'>
	<table  border='0' width='1050px'>
	<col width=50>
	 <col width=350>
	 <col width=350>
	 <col width=350>";
	
	$querysearch = "select sourceFund_Code, sourceFund_desc from Source_Funds ";
	$querysearch = $querysearch." where (sourceFund_Code like"."('%".$txtKeyword."%') or sourceFund_desc like"."('%".$txtKeyword."%') ) ";

	$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
	$totalrows = mysql_num_rows($resultsearch);
	
	if ($startrange=='')
		$startrange = 0;
	if ($perpage=='')
		$perpage = 20;
	
	$querysearch = $querysearch." limit ".$startrange.",".$perpage;
	

	$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());	
	while($rows=mysql_fetch_array($resultsearch)){
		$ct+=1;
		$stylefont='style15';
		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
				
		if($mode=='edit' && $bank_Id==$rows[0]){
			
		$Color="style='background-color:#1e87a5'";
			$stylefont='style28';
	}	
		
	echo'<tr '.$Color.' height="20px" >';
	
print<<<HERE

<td align=center ><img border='0' src='img/c_delete.png' width='9' height='10' title='Delete Detail' onmouseover="this.style.cursor='pointer'" OnClick="confirm_delete('$rows[0]')" $DelMode></td>

HERE;

	echo"<td onMouseOver=\"style.cursor='hand'\" "; echo " ondblclick=SetFund('$rows[0]');";echo"><span class='$stylefont' >$rows[0]&nbsp;</span></font></td>
		<td onMouseOver=\"style.cursor='hand'\" "; echo " ondblclick=SetFund('$rows[0]');";echo"><font face='Arial' size='2' color='#186194'><span class='$stylefont'>$rows[1]&nbsp;</span></font></td>
		";

	echo"	</tr>";
}		
	
	echo"</table></div></td></tr>";	
echo"<tr ><td >&nbsp;</td><td align='right'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td  align='center' colspan=2>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td></tr>";


		
echo"</table></td></tr><tr>
					<td id='search-bottom' style='width:1050px;height:20px;' colspan='4'></td>
				</tr></table></td></tr></table>";
				
$tabs->end();

$tabs->start('Payment Mode');

echo" <table border='0' class='tablecontent' align='center' valign='top'>
      <col width=500>
	  <col width=500>";
	  
echo "<tr><td><div id='paymentGrid' style='width:420px;height:300px;'>";
addPaymentModeGrid($connection);
echo "</div></td><td></td></tr>";
echo"</table>";
$tabs->end();


$tabs->run();	
				
echo"</td></tr>";

 

echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";
include("footer.php");	
echo" </table>  ";//end of last table menu		

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

HERE;
if($vTabFocus!=''){
	echo'<script type="text/javascript" language="javascript">';
	echo"tab_('$vTabFocus','');";
	echo'</script>';	
}	
?>