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


$querylog="SELECT FA_FIPAddress as loginaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['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");

//User Access
$querylog=" select ifNull(FA_IPAddressApp,0) As FA_IPAddressApp, ifNull(FA_IPAddressDel,0) As FA_IPAddressDel, ".
          " ifNull(FA_IPAddressMod,0) As FA_IPAddressMod from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

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

//Access mainmode On Insert/Edit/Delete
$Insmode = '';
if ($FA_IPAddressApp == 0)
	$Insmode = ' disabled ';
	

	
	
if($mainmode=='editmode' && $IPAddress_ID!=''){

	$query_edit="select IPAddress_UpLoad, IPAddress_DownLoad, IPAddress_Amount, Bandwidth,Currency_Code,IPAddress_Description,ifnull(IPAddress_Used,0),IPAddress_ID
				 from ipaddress where IPAddress_ID='".$IPAddress_ID."'";

	//echo $query_edit;
	$resultedit = mysql_query($query_edit, $connection);
	while($row=mysql_fetch_array($resultedit)){

		$IPAddress_UpLoad = $row[0];
		$IPAddress_DownLoad=$row[1];
		$IPAddress_Amount=$row[2];
		$Bandwidth=$row[3];
		$Currency_Code=$row[4];
		$IPAddress_Description=$row[5];
		$IPAddress_Used=$row[6];
		$IPAddress_IDPrv=$row[7];
	}
	$mainmode='edit';
}	

if($IPAddress_Used=='')	$IPAddress_Used=1;	
if($mainmode=='')	$mainmode='insert';	
	
print <<<HERE

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 


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

function AddIPAddress () {
    ClearForm();

	document.ipaddress.mainmode.value='insert';
	document.ipaddress.action="Form_IP_Address.php";
	document.ipaddress.submit();
}

function ClearPage(){
	if (confirm ( "Are you sure you want to Clear Page?"))
	{
		ClearForm();
		
		x.actionmode.value='';
		x.mainmode.value='';
		x.message.value='';
		x.action='Form_IP_Address.php';
		x.submit();
	}	
}


function ClearForm(){
    x = document.ipaddress;
	x.IPAddress_UpLoad.value='';
	x.IPAddress_DownLoad.value='';
	x.IPAddress_Amount.value='';
	x.Bandwidth.value='';
	x.Currency_Code.value='';
	x.IPAddress_Description.value='';
	x.IPAddress_ID.value='';
	x.IPAddress_IDPrv.value='';
}

function SetIP(IPId){
	
	document.ipaddress.IPAddress_ID.value=IPId;
	document.ipaddress.message.value='';
	document.ipaddress.actionmode.value='';

	document.ipaddress.mainmode.value='editmode';
	document.ipaddress.submit();
}

function SetNextPage(startrange,perpage){
	document.ipaddress.action='Form_IP_Address.php?startrange='+startrange+'&perpage='+perpage;
	document.ipaddress.submit();
}

function SetPrvPage(startrange,perpage){
	document.ipaddress.action='Form_IP_Address.php?startrange='+startrange+'&perpage='+perpage;
	document.ipaddress.submit();
}

function SearchForIP(){
	x = document.ipaddress;
	document.ipaddress.action='Form_IP_Address.php?startrange=0';
	document.ipaddress.submit();
}

function ClearKeyword(){
	x = document.ipaddress;
	x.txtKeyword.value='';
	document.ipaddress.action='Form_IP_Address.php?startrange=0';
	document.ipaddress.submit();
}

function submitenter(myfield,e){
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   	document.ipaddress.action='Form_IP_Address.php?startrange=0';
	document.ipaddress.submit();
  
   return false;
   }
else
   return true;
}

function confirm_delete(Code) {
	var strReplaceAll = Code;
	var intIndexOfMatch = strReplaceAll.indexOf( "%20" );

while (intIndexOfMatch != -1){
strReplaceAll = strReplaceAll.replace( "%20", " " )
intIndexOfMatch = strReplaceAll.indexOf( "%20" );
}
	Code= strReplaceAll;
	window.event.returnValue = false;
				
	if (confirm ( "Are you sure you want to Delete this Record " + "?")) {
		window.event.returnValue = true;
		document.ipaddress.mainmode.value='delete';	
		
		document.ipaddress.action='Form_IP_Address_Actions.php?IPAddressID='+Code;
		document.ipaddress.submit();
	}
					
}

function set_limit(){

	perpage=document.ipaddress.perpage.value;
	document.ipaddress.action='Form_IP_Address.php?perpage='+perpage+'&startrange=0';
	document.ipaddress.submit();
}	

function ChkUsed(){
	x=document.ipaddress;
	
	if(x.Chk_Used.checked)
		x.IPAddress_Used.value=1;
	else x.IPAddress_Used.value=0;	
}

function SubmitRecord() {

	if (document.ipaddress.IPAddress_ID.value=='' )
			{alert('Unable to save : check missing field ID ');
			document.getElementById("IPAddress_ID").focus();
	}
			
	else {
		document.ipaddress.checkcode.value='1';
		document.ipaddress.action="Form_IP_Address.php";
		document.ipaddress.submit();
	}
}
	
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>IP Address</title>

<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>
<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">
</head>
<body >
<form name='ipaddress' method='post'>

<input type='hidden' name='mainmode' value='$mainmode'>
<input type=hidden name='actionmode' value='$actionmode'>
<input type=hidden name='checkcode' value='$checkcode'>	
<input type=hidden name='IPAddress_Used' value='$IPAddress_Used'>	
<input type=hidden name='message' value='$message'>	
<input type=hidden name='IPAddress_IDPrv' value='$IPAddress_IDPrv'>	

HERE;



if($mainmode==''){
$status='disabled';
$dis='disabled';

}

if($mainmode=='insert')
$status='';

//$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'>IP ADDRESS</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='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='SearchForIP(); return false;' title='Search IP Address'><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'><span class='style1'><b>Results/page</b></span>
	 <span class='style1'><select name='perpage' id='perpage' value='$perpage' onchange=set_limit();>";
	 
	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>";

echo"<tr><td id='content-body'>";		
echo"<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
	<col width=65%>";

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

echo" <table border='0' style='table-layout:fixed' align='left' valign='top'>
      <col width=100><col width=140><col width=120><col width=140><col width=120>";
	  
	echo"<tr bgcolor='#F7F7F7' height='25px'><td valign='center' align='left' ><span class='style1'>ID</span></td><td align='left'>";
	
	//if($mainmode=='edit') {echo "<b>$IPAddress_ID</b>&nbsp;";echo"<input type='hidden' name='IPAddress_ID' value='$IPAddress_ID' maxlength='15' style='font-size:12px; width:100px; height:18px;'>";
	//}
	//else 
	echo"<input type='text' name='IPAddress_ID' value='$IPAddress_ID' maxlength='15' style='font-size:12px; width:100px; height:18px;'>";
	
	echo"&nbsp;</td><td valign='center' align='left' ><input type='checkbox' name='Chk_Used' $dis onclick='ChkUsed();'";
	if($IPAddress_Used==1) echo " checked ";echo"><span class='style1'> Used</span></td>
	
	</tr>
	
	<tr bgcolor='#F7F7F7' height='25px'><td valign='center' align='left' ><span class='style1'>Upload &nbsp;</span>";
	echo"<td align='left'><input type='text' name='IPAddress_UpLoad'  value='".htmlspecialchars($IPAddress_UpLoad, ENT_QUOTES)."' style='font-size:12px; width:100px; height:18px;'  $status maxlength='15' /></td>";
  
  echo"<td valign='center' align='left' ><span class='style1'>Download &nbsp;</span>";
	echo"<td align='left'><input type='text' name='IPAddress_DownLoad'  value='".htmlspecialchars($IPAddress_DownLoad, ENT_QUOTES)."' style='font-size:12px; width:100px; height:18px;'  $status  /></td>
	</tr>
	<tr bgcolor='#F7F7F7' height='25px'><td valign='center' align='left' ><span class='style1'>Amount &nbsp;</span>";
	echo"<td align='left'><input type='text' name='IPAddress_Amount'  value='".htmlspecialchars($IPAddress_Amount, ENT_QUOTES)."' style='font-size:12px; width:100px; height:18px;'  $status  /></td>";
	
	 function get_currency($connection){		
		$query_cr = "select currency_code,currency_name from currency";
		//echo$query_cr;
		$result_cr=mysql_query($query_cr, $connection);
		return $result_cr;
	}
	
	$curr = get_currency($connection);
	
	 echo"<td align=left ><span class='style1'>Currency</span></td><td  bgcolor='#F7F7F7' height='25' align='left'><select  name='Currency_Code'  $dis style='width:100px;'>
				<option value=''></option>";
				while ($cr=mysql_fetch_row($curr))
				{
				if ($cr[0]==$Currency_Code)
				echo"<option selected value='$cr[0]'>$cr[0]</option>";
				else
				echo"<option value='$cr[0]'>$cr[0]</option>";
				}				
		echo"</select></td></tr>";
		
	echo"<tr bgcolor='#F7F7F7' height='25px'><td valign='center' align='left' ><span class='style1'>Bandwidth &nbsp;</span>";
	echo"<td align='left'><input type='text' name='Bandwidth'  value='".htmlspecialchars($Bandwidth, ENT_QUOTES)."' style='font-size:12px; width:100px; height:18px;'  $status  maxlength='20'/></td>
		</tr>";
		
  echo"<tr bgcolor='#F7F7F7' height='25px'><td valign='center' align='left' ><span class='style1'>Description &nbsp;</span>";
	echo"<td colspan='5' align='left'><input type='text' name='IPAddress_Description'  value='".htmlspecialchars($IPAddress_Description, ENT_QUOTES)."' style='font-size:12px; width:370px; height:18px;'  $status  maxlength='150'/></td>
		</tr>";
  
  
	echo"</table></td><td  valign='top'>";

echo"<table style='width:380px;' border='0' cellspacing=0><col width=100><col width=150>
	<tr><td id='search-top' style='width:380px;height:15px;' colspan='4'></td>
	</tr>
	 <tr><td  valign='top' colspan=3 id='search-content' style='width:380px;'><table style='width:380px;' border='0' cellspacing=0><col width=30><col width=80><col width=140><col width=130>
	<tr height='30px'><td>&nbsp;</td>
	<td align='center'><span class='style1'><b>ID</b></td><td align='center'><span class='style1'><b>Upload</b></td><td align='center'><span class='style1'><b>Download</b></td></tr>";
	 
	echo"<tr><td colspan=4 id='search-content'><div style='overflow-y:scroll;height:250px;width:370px;border:0px solid #000000;padding-left:5px;'><table  border='0' width='352px'><col width=30><col width=80><col width=140><col width=112>
	";
	
	
	$querysearch="select IPAddress_ID,IPAddress_UpLoad,IPAddress_DownLoad from ipaddress ";
	$querysearch=$querysearch." where (IPAddress_ID like"."('%".$txtKeyword."%') or IPAddress_UpLoad like"."('%".$txtKeyword."%') or IPAddress_DownLoad 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($mainmode=='edit' && $IPAddress_IDPrv==$rows[0]){
			
			$Color="style='background-color:#1e87a5'";
			$stylefont='style28';
	}	
		
	echo'<tr '.$Color.' height="20px" >';
	
	if($FA_IPAddressDel==1)	
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;
else print<<<HERE

<td align=center >&nbsp;</td>

HERE;

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

	

	echo"	</tr>";
}		
	
echo"</table></div></td></tr>";
echo"<tr><td >&nbsp;</td><td align='center'>";
		
		$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:380px;height:20px;' colspan='4'></td>
				</tr></table></td></tr>";			

/*				
echo"<table border=0 align=center width=100%> <tr bgcolor=#D8D8D8 height='35'> ";
echo"	<td align=center><input type='button'  value='Add IP Address'";  	
			
	if($mainmode=='insert' or $mainmode=='edit')
		echo "disabled ";
	echo"onclick='AddIPAddress()' $style title='Add IP Address' $Insmode>" ;
	
echo"           <input type='button' id='btnClear' name='BtnClear' value='Clear Page' ";

echo"  onclick='ClearPage();' $style title='Clear Page'>";

				
				
		if($mainmode=='view'){
echo"           <input type='button' name='btnEdit' id='btnEdit' value='Edit' ";//Edit
				
				
				echo" onclick='editmainmode();' $style	title='Edit IP Address' />";
	}
else	{
echo"           <input type='button' name='btnPost' id='btnPost' value='Save' ";//post
				if ($mainmode=='')
						echo " disabled ";
				
				echo" onclick='SubmitRecord();' $style	title='Save Changes' />";}

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

print <<< HERE
       </td>
      </tr>
	  </table>
HERE;

//// Checking if code already exists or not
if($checkcode=='1'){

	$IPAddress_ID=trim($IPAddress_ID);
	$IPAddress_IDPrv=trim($IPAddress_IDPrv);
	
	if(($mainmode=='insert' && $IPAddress_ID!=$IPAddress_IDPrv) || ($mainmode=='edit' && $IPAddress_ID!=$IPAddress_IDPrv)){
		$query_ip="select count(*) from ipaddress where IPAddress_ID=".$IPAddress_ID;
		//echo$query_ip;exit;
		$result=mysql_query($query_ip, $connection);
		$row_code=mysql_fetch_row($result);
		$num=$row_code[0];
	
	}
	else $num=0;
	
	

	if($num>0){
		$message="Code Already exists";
		
		echo"<script type='text/javascript'>";
		echo"	document.ipaddress.message.value='Code Already Exists';";
		echo"	document.ipaddress.checkcode.value='0';";
		echo"	document.ipaddress.submit();";	
		echo"</script>";
			
	}

	else{
		echo"<script type='text/javascript'>";
		echo"	document.ipaddress.action='Form_IP_Address_Actions.php';";
		echo"	document.ipaddress.submit();";
		echo"</script>";
	}
}
 echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";
include("footer.php");	

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

HERE;

?>
