<?php

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


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");


$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 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_ProjectParent,0) As Module_ProjectParent, ifNull(Module_CostCenterParent,0) As Module_CostCenterParent ".
			 " From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

		
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<title>Search Divisions</title>
<script type="text/javascript" language="javascript">

function SelectRecord(value1,value2,value3){

	vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	vType  = document.search.divtype.value;

	if(vFrm=='1'){
		opener.document.production.txtDivisionID.value   = value1;
		opener.document.production.txtDivisionCode.value = value2;
		opener.document.production.txtDivisionCode.focus();
		//opener.document.production.submit();
	}
	else if(vFrm=='2'){
		opener.document.adjustment.txtDivisionID.value   = value1;
		opener.document.adjustment.txtDivisionCode.value = value2;
		opener.document.adjustment.txtDivisionCode.focus();
		//opener.document.adjustment.submit();
	}
	else if(vFrm=='3'){
		opener.document.production.txtDivisionID.value   = value1;
		opener.document.production.txtDivisionCode.value = value2;
		opener.document.production.txtDivisionCode.focus();
		//opener.document.production.submit();
	}
	else if(vFrm=='4'){
		if (vItem == 1) {
			opener.document.search.PProjectCode.value = value2;
			opener.document.search.PProjectCode.focus();
		} else 	if (vItem == 2) {
			opener.document.search.PCostCentCode.value = value2;
			opener.document.search.PCostCentCode.focus();
		}
		//opener.document.search.submit();
	}
	else if(vFrm=='5'){
		opener.document.adjustmentdt.txtDivisionID.value   = value1;
		opener.document.adjustmentdt.txtDivisionCode.value = value2;
		opener.document.adjustmentdt.txtDivisionCode.focus();
		//opener.document.adjustmentdt.submit();
	}
	

	self.close();
}


function ClearForm()
{
	vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	vType  = document.search.divtype.value;
	
	document.search.PSearch.value='';
	document.search.action='List_FrmDivisions.php?frm='+vFrm+'&item='+vItem+'&divtype='+vType;
	document.search.submit();

}

function RefreshPage()
{
	document.search.submit();

}

function LoadForm() 
{
	x=document.search;
	x.PSearch.focus();

}

function SetNextPage(startrange,perpage){
	vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	vType  = document.search.divtype.value;
	
	document.search.action='List_FrmDivisions.php?PSearch=&frm='+vFrm+'&item='+vItem+'&divtype='+vType+'&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
    vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	vType  = document.search.divtype.value;
	
	document.search.action='List_FrmDivisions.php?PSearch=&frm='+vFrm+'&item='+vItem+'&divtype='+vType+'&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}




</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}

a:visited {
	text-decoration: none;
	color: #0000FF;
}


.style1 {color: #999999; font-weight: bold; font-size: 11px;}
.style2 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style3 {color: #999999; font-weight: bold; font-size: 11px; }
.style4 {color: #3333FF; }

</style>

</head>
<body onLoad='LoadForm();'>
<form name='search' method='post' action=''>
<input type='hidden' name='item'  value='$item'>
<input type='hidden' name='frm' value='$frm'>
<input type='hidden' name='divtype' value='$divtype'>

HERE;

echo"<table border='0' cellspacing='1' style='table-layout:fixed' align='center'>

	  <col width=150>
	  <col width=350>
	  <col width=150>
	
		     
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='3'><span class='style2'>List of Divisions ($divtype)</span></td></tr>
	  <tr bgcolor='#DDDDDD' height=30px>
	    <td align='left'><span class='style1'>Search by Code/Name</span></td>
		<td align='left'><span class='style1'><input type='text' name='PSearch' id='PSearch' value='$PSearch' style='width:150px;' maxlength='100'/></span>
		    <input type='submit' name='btnSubmit' value='Find' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnCancel' value='Cancel' onclick='ClearForm();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
		</td>
		<td align='left' valign='center'><span class='style1'>Per Page </span><span class='style1'><select name='perpage' id='perpage' value='$perpage' onchange='RefreshPage();'>";
              

        if ($perpage==20)
			$s20 = 'selected';
		else if ($perpage==30)
			$s30 = 'selected';
		else if ($perpage==40)
			$s40 = 'selected';
		else if ($perpage==50)
			$s50 = 'selected';
			
				
        echo'<option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s40.' value="40">40</option>
			 <option '.$s50.' value="50">50</option>
			 </select></span></td>';
echo" </tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='3'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
	  
	
		<td  height='25' align='left' ><span class='style3'>Code </span></td>
		<td  height='25' align='center' colspan='2'><span class='style3'>Name </span></td>
		
	  </tr>";
		
$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on (($Module_ProjectParent=1 and project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%')) or ($Module_ProjectParent=0 and project.project_code = loginstk.project_code )) ".
			  " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);	


$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.costcent_code )) ".
			   " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);	


$querysearch = " Select Divisions.Divisions_ID, Divisions.Divisions_Code, Divisions.Divisions_Name ".
			   " From Divisions ".
			   " Inner Join DivisionTypes on Divisions.DivisionTypes_Id = DivisionTypes.DivisionTypes_Id ";
			 
if ($projectexist >	0 && strtoupper($divtype) == 'PROJECT') {
	$querysearch = $querysearch." Inner join ( select distinct project_code ".
								" 			   From ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
								" 			   Where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
								" 			   And (project_code is not null and project_code != '') ".
								"			 ) QPrj on (($Module_ProjectParent=1 and Divisions.Divisions_Code like concat(QPrj.project_code,'%')) or ($Module_ProjectParent=0 and Divisions.Divisions_Code = QPrj.project_code )) ";
}							 
if ($costcentexist > 0 && strtoupper($divtype) == 'COSTCENTER') {
	$querysearch = $querysearch." Inner join ( Select distinct costcent_code ".
								" 			   From ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
							    "			   Where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
							    "              And (costcent_code is not null and costcent_code != '') ".
								"			 ) QCst on (($Module_ProjectParent=1 and Divisions.Divisions_Code like concat(QCst.costcent_code,'%')) or ($Module_ProjectParent=0 and Divisions.Divisions_Code = QCst.costcent_code )) ";
}			 
 
$querysearch = $querysearch." Where (Divisions.Divisions_Code Like "."('%".$PSearch."%') or Divisions.Divisions_Name Like "."('%".$PSearch."%')) ".
							" And Upper(DivisionTypes_Name) = Upper('".$divtype."') ".
							" Order By Divisions.Divisions_Code ";

//echo $querysearch;
$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)){
		
	extract($rows);
	$ct+=1;

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


	$DivisionsName=ereg_replace("'","%27",$Divisions_Name);
	$DivisionsName=ereg_replace("&","%26",$Divisions_Name);
	$DivisionsName=ereg_replace("#","%26",$Divisions_Name);

	echo"<tr $Color>";
	echo"  <td align='left'><a onclick=\"SelectRecord('$Divisions_ID','$Divisions_Code','$DivisionsName');\" href='#'><span class='style4'>$Divisions_Code</span></a></td>";
	echo"  <td align='left' colspan='2'><span class='style4'>$Divisions_Name</span></td>";
    echo"</tr>";
	
}
							
	
		
echo"   <tr><td colspan='3'>&nbsp;</td></tr>
		<tr><td colspan='3'><table width='100%' border=0><tr><td width='200'>&nbsp;</td><td width='125' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='125' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='200'>&nbsp;</td></tr>";
		echo"</table>";

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


          

		  

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

HERE;

?>