<?php
session_start();

include("config.inc.php");

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


$query1="select grid_color1,grid_color2 from setup";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
extract($rows);

}

if($_GET['lcode']!=""){

    if ($option == 1) {
		$queryproject=" select distinct project.project_code as Code, project_name as Name ".
					  " from project left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on project.project_code like concat(loginstk.project_code,'%') ".
					  " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
					  " and project.project_code = '".$_GET['lcode']."'  ".
					  " order by project.project_code";
					  
		$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
		$projectrows   = mysql_num_rows($projectresult);
		if ($projectrows == 0)
			$queryproject=" select distinct project_code as Code, project_name as Name from project where project.project_code = '".$_GET['lcode']."' order by project_code ";			  
		
        $result=mysql_query($queryproject,$connection) or die(mysql_error());		
	} 
	else if ($option == 2) {
	
		$querycostcent=" select distinct costcent.costcent_code as Code, costcent_name as Name ".
					   " from costcent left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on costcent.costcent_code like concat(loginstk.costcent_code,'%') ".
					   " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
					   " and costcent.costcent_code = '".$_GET['lcode']."'  ".
					   " order by costcent.costcent_code";
					  
		$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
		$costcentrows   = mysql_num_rows($costcentresult);
		if ($costcentrows == 0)
			$querycostcent=" select distinct costcent_code as Code, costcent_name as Name from costcent where costcent.costcent_code = '".$_GET['lcode']."' order by costcent_code ";
		
		$result=mysql_query($querycostcent,$connection) or die(mysql_error());
    }

	while($rows=mysql_fetch_array($result)){
	   extract($rows);
    }

	$post="1";

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>List of Projects/ Costcenters</title>
<script type="text/javascript" language="javascript">


function clearform()
{
  document.form1.PCode.value='';
  document.form1.PName.value=''

}
</script>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
.style1 {
	color: #333333;
	font-weight: bold;
}
.style2 {
	color: #999999;
	font-weight: bold;
}
.style3 {color: #999999}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="800" border="0" align="center">
    <tr>
      <th colspan="5" bgcolor="#DDDDDD"><span class="style1"> PROJECTS/COSTCENTERS </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Project  <br> Code / Name</span></td>
      <td bgcolor="#DDDDDD"><input type="text" name="PCode" value="<?php echo $PCode; ?>"  />
        <input name="code1" type="hidden" id="code1"  value="<?php echo $Code; ?>"></td>
      <td bgcolor="#DDDDDD"><input type="text" name="PName"  value="<?php echo $PName; ?>" size='35'/>
	    <input type="submit" name="Submit" value="Find" style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
		<input type="button" name="cancelbtn" value="Cancel" onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
        <input name="desc1" type="hidden" id="desc1" value="<?php echo $Name; ?>" />
        <input name="item1" type="hidden" id="item1" value="<?php echo $_GET['item']; ?>" />
	    <input name="option" type="hidden" id="option" value="<?php echo $_GET['option']; ?>" /></td>   
    </tr>
<?php if($post=="1") { ?>
<script type="text/javascript" language="javascript">
x=document.form1;
item1=x.item1.value;
option=x.option.value;

if (option == 1) {
	if(item1=="1"){
		opener.document.form1.fromproject.value=document.form1.code1.value;
		opener.document.form1.fromprojectname.value=document.form1.desc1.value;
		self.close();
	}else if(item1=="2"){
		opener.document.form1.toproject.value=document.form1.code1.value;
		opener.document.form1.toprojectname.value=document.form1.desc1.value;
		self.close();
	}
}	
else if (option == 2) {
	if(item1=="1"){
		opener.document.form1.fromcostcent.value=document.form1.code1.value;
		opener.document.form1.fromcostcentname.value=document.form1.desc1.value;
		self.close();
	}else if(item1=="2"){
		opener.document.form1.tocostcent.value=document.form1.code1.value;
		opener.document.form1.tocostcentname.value=document.form1.desc1.value;
		self.close();
	}
}
</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td width="136"><span class="style2">Code </span></td>
      <td width="411" colspan="2"><span class="style2">Name </span></td>
    </tr>
<?php

if ($option == 1) {
	$queryproject=" select distinct project.project_code as Code, project_name as Name ".
				  " from project left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on project.project_code like concat(loginstk.project_code,'%') ".
				  " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
				  " and project.project_code like '%".$PCode."%' ".
				  " and project_name like '%".$PName."%' ".
				  " order by project.project_code";
				  
	$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
	$projectrows   = mysql_num_rows($projectresult);
	if ($projectrows == 0)
		$queryproject=" select distinct project_code as Code, project_name as Name from project ".
					  " where project_code like '%".$PCode."%' ".
				      " and project_name like '%".$PName."%' ".		
					  " order by project_code ";		

       $result=mysql_query($queryproject,$connection) or die(mysql_error());		
} 
else if ($option == 2) {
	
	$querycostcent=" select distinct costcent.costcent_code as Code, costcent_name as Name ".
				   " from costcent left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on costcent.costcent_code like concat(loginstk.costcent_code,'%') ".
				   " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
				   " and costcent.costcent_code like '%".$PCode."%' ".
				   " and costcent_name like '%".$PName."%' ".
				   " order by costcent.costcent_code";
				  
	$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
	$costcentrows   = mysql_num_rows($costcentresult);
	if ($costcentrows == 0)
		$querycostcent=" select distinct costcent_code as Code, costcent_name as Name from costcent ".
                       " where costcent_code like '%".$PCode."%' ".
				       " and costcent_name like '%".$PName."%' ".		
					   " order by costcent_code ";
		
	$result=mysql_query($querycostcent,$connection) or die(mysql_error());
}		
		

while($rows=mysql_fetch_array($result)){
	extract($rows);

	$ct+=1;

	if($ct%2==0)
	{
		$Color="style='color:#0000FF;background-color:".$grid_color1."'";
		
		}else{
			
		$Color="style='color:#0000FF;background-color:".$grid_color2."'";	
	}

	?>
		<tr <?php echo $Color; ?>>
		  <td align="left"><a href="select_projectcostcent.php?lcode=<?php echo $Code; ?>&item=<?php echo $_GET['item']; ?>&option=<?php echo $_GET['option']; ?>"><?php echo $Code; ?></a></td>
		  <td colspan="2"><a href="select_projectcostcent.php?lcode=<?php echo $Code; ?>&item=<?php echo $_GET['item']; ?>&option=<?php echo $_GET['option']; ?>"><?php echo $Name; ?></a></td>
		</tr>
	<?php
}
?>
  </table>
</form>
</body>
</html>
