<?php
include("postget.php");
include_once("common.php");
checklevel(4,$_REQUEST['clientid']);


if ($_REQUEST[click]==""._EDIT."" OR $_REQUEST[click]==""._ADD_STATE."") {
	$errors = '';
	if ($_POST['newstate']=='') {$errors.= "<br>".ucfirst(_STATE)." "._NOT_SPECIFIED."";}
	if ($_POST['country']=='') {$errors.= "<br>".ucfirst(_COUNTRY)." "._NOT_SPECIFIED."";}
}



if($errors=='' AND $_REQUEST[click]==""._EDIT."") {
	$query="UPDATE hd_countries_states SET
	state='$newstate', 
	country='$country' 
	WHERE id ='$id'";

	if(!mysql_query($query,$db_link)){
		messagetop("ERROR","<b>DEBUG:</b> ".$query."<br><b>MYSQL ERROR:</b> ".mysql_error(),'countries.php',0); 
	}else{
		go2('countries.php');
	}




}else if($errors=='' AND $_REQUEST[click]==""._ADD_STATE."") {

	$query="INSERT INTO hd_countries_states (state, country)
	VALUES ('$newstate','$country')";



	if(!mysql_query($query,$db_link)){
		messagetop("ERROR","DEBUG: ".$query."<br>MYSQL ERROR: ".mysql_error(),'countries.php',0); 
	}else{
		go2('countries.php');
	}




}else{



	if($_REQUEST[id]) 	{
		$query="SELECT * FROM hd_countries_states WHERE id='$_REQUEST[id]'";
		$output=mysql_query($query,$db_link);
		$row=mysql_fetch_array($output);
	}
	include("head_template.php");
	echo '
<div class="container">
	<div class="row">


<table width="98%" class="table table-bordered">
		<tr>
		<td align="center" width="100%">
';

	print(ucfirst(_COUNTRIES)." > ".ucfirst(_STATES));

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

';

	print errors($errors); $errors = '';

	print("<form method='POST'><table>");

	print("<tr>
	<td align='right'><b>".ucfirst(_STATE).":</b></td>
	<td><input name='newstate' size='60'");
	if($_REQUEST[id]){print(" value='$row[state]'");}else{print(" value='$_POST[newstate]'");}
	print("></td></tr>");

	print("<tr>
	<td align='right'><b>".ucfirst(_COUNTRY).":</b></td>
	<td>
	<select name='country'>");
	$query="SELECT id, country FROM hd_countries ORDER BY country";
	$cats=mysql_query($query,$db_link);
	print ("<option value=''>** ".ucfirst(_SELECT_AN_OPTION)." **</option>");
	while($categ=mysql_fetch_array($cats)) {
		if ($_POST['country']) {
			print ("<option value='".$categ[id]."' ".sele($_POST['country'],$categ[id]).">".$categ[country]."</option>");
		}else{
			print ("<option value='".$categ[id]."' ".sele($row[country],$categ[id]).">".$categ[country]."</option>");
		}
	};
	print("</select></td></tr>");
	
	print("</table><br><input type='hidden' name='id' value='$id'>");
	if($id){
		print("<input type='submit' name='click' value='"._EDIT."'><br>");
	}else{
		print("<input type='submit' name='click' value='"._ADD_STATE."'><br>");
	}
	print("</form>");
echo '
		</td>
		</tr>
		</table>
</div>
</div>
';
	include("foot_template.php");
}
?>