<?php
$PageSecurity = 15;
$title = _('Dispatch Location');
if (isset($_POST['SelectedType'])){
	$SelectedType = strtoupper($_POST['SelectedType']);
} elseif (isset($_GET['SelectedType'])){
	$SelectedType = strtoupper($_GET['SelectedType']);
}

if (isset($Errors)) { unset($Errors); }
$Errors = array();
if (isset($_POST['submit'])) 
{
	$InputError = 0;
	$i=1;
	if (strlen($_POST['TypeAbbrev']) =='') {
		$InputError = 1;
		prnMsg(_('The Store Code not be left '),'error');
		$Errors[$i] = 'Store Type';
		$i++;
	} 
	if (strlen($_POST['Sales_Type']) =='') {
		$InputError = 1;
		prnMsg(_('The Store Name not be left '),'error');
		$Errors[$i] = 'Store Type';
		$i++;
	} 
	

	if (isset($SelectedType) AND $InputError !=1) {

		$sql = "UPDATE locations_to SET StoreName='".$_POST['Sales_Type']."', StoreCode='". $_POST['TypeAbbrev']."', address='". $_POST['address']."', contact_per='". $_POST['contact_per']."', contact_no='". $_POST['contact_no']."' WHERE id = '$SelectedType'";

		$msg = _('The Dispatch Location ') . '  ' .  _('has been updated');
	} elseif ( $InputError !=1 ) {

 
		// First check the type is not being duplicated
 	    	$checkSql = "SELECT count(*) FROM locations_to WHERE StoreCode='".$_POST['TypeAbbrev']."'";
			$checkresult = DB_query($checkSql,$db);
			$checkrow = DB_fetch_row($checkresult);

			if ( $checkrow[0] > 0 ) {
				$InputError = 1;
				prnMsg( _('The Dispatch Location ') . $_POST['TypeAbbrev'] . _(' already exist.'),'error');
			} else {

			// Add new record on submit
 
		 $sql = "INSERT INTO locations_to (id,StoreCode, StoreName, CompanyId, address, contact_per, contact_no)
				VALUES ('','".$_POST['TypeAbbrev']."','". $_POST['Sales_Type']."','".$_REQUEST['CompanyId']."','".$_REQUEST['address']."','".$_REQUEST['contact_per']."','".$_REQUEST['contact_no']."')";
					

			$msg = _('Diapatch Location') . ' ' . $_POST["Sales_Type"] .  ' ' . _('has been created');
			$checkSql = "SELECT count(StoreCode) FROM locations_to";
			$result = DB_query($checkSql, $db);
			$row = DB_fetch_row($result);
		}
	}

	if ( $InputError !=1) {
	//run the SQL from either of the above possibilites
		$result = DB_query($sql,$db);


	
	// Does it exist
		$checkSql = "SELECT count(*) FROM locations_to WHERE StoreCode = '" . $DefaultPriceList . "'";
		$checkresult = DB_query($checkSql,$db);
		$checkrow = DB_fetch_row($checkresult);

	
	      	prnMsg($msg,'success');

		unset($SelectedType);
		unset($_POST['TypeAbbrev']);
		unset($_POST['Sales_Type']);
	}
echo "<script>location.href='index.php?route=Locations_to&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";} 
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<?php 
if (! isset($_GET['delete'])) 
{?>
	<form method="post" action=""><?php
	if($SelectedType !='')
	{?>
	 <h1><?php echo 'Update Dispatch Location'; ?></h1> 
	<?php }
	else
	{?>
	<h1><?php echo 'Dispatch Location'; ?></h1> 
	<?php }
?>
<table width="50%" border="0" cellpadding="3" cellspacing="0">
<?php
if
(isset($SelectedType) AND $SelectedType!='' )
{
$sql = "SELECT * FROM locations_to WHERE id='$SelectedType'";
$result = DB_query($sql, $db);
$myrow = DB_fetch_array($result);
$_POST['id'] = $myrow['id'];
$_POST['TypeAbbrev'] = $myrow['1'];
$_POST['Sales_Type']  = $myrow['2'];
$_POST['short_name']  = $myrow['3'];
$_POST['CompanyId']  = $myrow['4'];
$_POST['address']  = $myrow['5'];
$_POST['contact_per']  = $myrow['6'];
$_POST['contact_no']  = $myrow['7'];

?>
		<input type="hidden" name="SelectedType" VALUE="<?php echo $SelectedType;?>">
		<input type="hidden" name="TypeAbbrev" VALUE="<?php echo $_POST['TypeAbbrev'];?>">
		<input type="hidden" name="store_site" VALUE="<?php echo $_POST['store_site'];?>">
<tr class="row1">
	<td class="tabledata1"><strong>SITE CODE<font color="#FF0000">*</font></strong></td>
	<td><input type="Text" name="TypeAbbrev" id="TypeAbbrev" style="width:150px;" value="<?php echo $_POST['TypeAbbrev'];?>"></td>
</tr>
<?php } else {?>
<tr class="row1">
	<td class="tabledata1"><strong>SITE CODE<font color="#FF0000">*</font></strong></td>
	<td><input type="Text" style="width:150px;" id="TypeAbbrev"  name="TypeAbbrev"></td>
</tr><?php	}
	if (!isset($_POST['Sales_Type'])) 
	{
		$_POST['Sales_Type']='';
	}
?>	
<tr class="row1">
	<td class="tabledata1"><strong>SITE NAME<font color="#FF0000">*</font></strong></td>
	<td><input type="Text" name="Sales_Type" id="Sales_Type" style="width:150px;" value="<?php echo $_POST['Sales_Type'];?>"></td>
</tr>
<!--<tr class="row1">
	<td class="tabledata1"><strong>Company <font color="#FF0000">*</font></strong></td>
	<td>
	<select name="CompanyId" onchange="getSite(this.value)" style="width:150px;">    
    	<option value="0">--Select--</option>
			<?php
			$CompanyName=DB_query("Select * from companies  ",$db);
			while($CompanyRow=DB_fetch_array($CompanyName))
			{ ?>
			<option value="<?php echo $CompanyRow['CompanyId']?>" <?php echo($CompanyId==$CompanyRow['CompanyId']?'selected':'');?>><?php echo $CompanyRow['CompanyName']?></option>	
				
		<?php	}
			
			   
			?>
		</select> </td>
	</tr>-->

<tr class="row1">
	<td class="tabledata1"><strong>Address<font color="#FF0000">*</font></strong></td>
	<td><textarea name="address" id="address"><?php echo $_POST['address'];?></textarea></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Contact Person<font color="#FF0000">*</font></strong></td>
	<td><input type="Text" name="contact_per" id="contact_per" style="width:150px;" value="<?php echo $_POST['contact_per'];?>"></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Contact Number<font color="#FF0000">*</font></strong></td>
	<td><input type="Text" name="contact_no" id="contact_no" style="width:150px;" value="<?php echo $_POST['contact_no'];?>"></td>
</tr>
	<tr class="row1">
		<td class="tabledata1" colspan="2" style="text-align:center;">
		<input type="submit" onclick="return validate();" class="search" name="submit" VALUE="Save">
		<input type="submit" name="Cancel" class="search" VALUE="Cancel">
		</td>
	</tr>
</table>
</form>
<?php } 
if (!isset($SelectedType))
{
   	$sql = "SELECT * FROM locations_to";
	$result = DB_query($sql,$db);
?>
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata"><strong>STORE CODE </strong></td>
	<td class="tabledata"><strong>STORE NAME </strong></td>
	<td class="tabledata"><strong>ADDRESS</strong></td>
	<td class="tabledata"><strong>CONTACT PERSON</strong></td>
	<td class="tabledata"><strong>CONTACT Number</strong></td>
	<td class="tabledata"><strong>EDIT</strong></td>
	<td class="tabledata"><strong>DELETE</strong></td>
</tr>
<?php
	$k=0; //row colour counter
while ($myrow = DB_fetch_row($result))
 {?>
<tr class="row1">
	<td><?php echo $myrow[1];?></td>
	<td><?php echo $myrow[2];?></td>
	<td><?php echo $myrow[5];?></td>
	<td><?php echo $myrow[6];?></td>
	<td><?php echo $myrow[7];?></td>
	<td><a href="index.php?route=Locations_to&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&SelectedType=<?php echo $myrow[0];?>">Edit</a></td>
	<td><a href='index.php?route=Locations_to&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&SelectedType=<?php echo $myrow[0];?>&delete=yes' onclick="return confirm('This store will be deleted. Are you sure?')">Delete</a></td>
</tr><?php }?>
</table>
	<?php }
include('includes/footer.inc');
?>
<script type="text/javascript" language="javascript">
function validate()
{
	if(document.getElementById("TypeAbbrev").value=='')
	{
		alert("Please Fill The Store Code First");
		document.getElementById("TypeAbbrev").focus();
		return false;
	}
	if(document.getElementById("Sales_Type").value=='')
	{
		alert("Please Fill The Store Name First");
		document.getElementById("Sales_Type").focus();
		return false;
	}
	if(document.getElementById("address").value=='')
	{
		alert("Please Fill The Address First");
		document.getElementById("address").focus();
		return false;
	}
	if(document.getElementById("contact_per").value=='')
	{
		alert("Please Fill The Contact Person First");
		document.getElementById("contact_per").focus();
		return false;
	}
	if(document.getElementById("contact_no").value=='')
	{
		alert("Please Fill The Contact Number First");
		document.getElementById("contact_no").focus();
		return false;
	}
}
</script>