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


//////////////////////////////////////////////////////////////// si se envi� el formulario, cargar los errores
if ($_REQUEST[click]==""._EDIT."" OR $_REQUEST[click]==""._ADD_COUNTRY."") {
	$errors = '';
	if ($_POST['newcountry']=='') {$errors.= "<br>".ucfirst(_COUNTRY)." "._NOT_SPECIFIED."";}
	if ($_POST['newcountrycode']=='') {$errors.= "<br>".ucfirst(_COUNTRY_CODE)." "._NOT_SPECIFIED."";}
	if ($_POST['newphonecode']=='' OR $_POST['newphonecode']<1 OR $_POST['newphonecode']>999) {$errors.= "<br>".ucfirst(_PHONE_CODE)." "._WRONG."";}
}



if($errors=='' AND $_REQUEST[click]==""._EDIT."") {
	$query="UPDATE hd_countries SET
	country='$newcountry', 
	countrycode='$newcountrycode', 
	phonecode='$newphonecode', 
	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_COUNTRY."") {

	$query="INSERT INTO hd_countries (country, countrycode, phonecode)
	VALUES ('$newcountry','$newcountrycode','$newphonecode')";


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

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

';

	//	viewhelp(48);
	print errors($errors); $errors = '';

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

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

	print("<tr>
	<td align='right'><b>".ucfirst(_COUNTRY_CODE).":</b></td>
	<td><input name='newcountrycode' size='4'");
	if($_REQUEST[id]){print(" value='$row[countrycode]'");}else{print(" value='$_POST[newcountrycode]'");}
	print("> Ex: US, GB, SP</td></tr>");

	print("<tr>
	<td align='right'><b>".ucfirst(_PHONE_CODE).":</b></td>
	<td><input name='newphonecode' size='4'");
	if($_REQUEST[id]){print(" value='$row[phonecode]'");}else{print(" value='$_POST[newphonecode]'");}
	print("></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_COUNTRY."'><br>");
	}
	print("</form>");
echo '
		</td>
		</tr>
		</table>
</div>
</div>
';
	include("foot_template.php");
}
?>