	<?php
$PageSecurity = 11;
include('includes/Class_location.php');
$title = _('Site Maintenance');
include('includes/SQL_CommonFunctions.inc');
if (isset($_GET['SelectedLocation'])){
	$SelectedLocation = $_GET['SelectedLocation'];
} elseif (isset($_POST['SelectedLocation'])){
	$SelectedLocation = $_POST['SelectedLocation'];
}
 if (isset($_POST['submit'])) 
 {
	//initialise no input errors assumed initially before we test
	$InputError = 0;

	/* actions to take once the user has clicked the submit button
	ie the page has called itself with some user input */
	$date = date($_SESSION['DefaultDateFormat']);
    $_POST['loccode']=strtoupper($_POST['loccode']);
	
	
	if( trim($_POST['prefix']) == '' ) {
		$InputError = 1;
		prnMsg( _('The prefix  may not be empty'), 'error');
	}
	 
	if (isset($SelectedLocation) AND $InputError !=1) {

		/* Set the managed field to 1 if it is checked, otherwise 0 */
		if(isset($_POST['Managed']) and $_POST['Managed'] == 'on'){
			$_POST['Managed'] = 1;
		} else {
			$_POST['Managed'] = 0;
		}
   	 
	//$dataArray['loccode'] = $_REQUEST['loccode'];
	$dataArray['locationname'] = $_REQUEST['locationname'];
	$dataArray['mst_companyid'] = $_REQUEST['CompanyName'];
	$dataArray['prefix'] = $_REQUEST['prefix'];
	$dataArray['deladd1'] = $_REQUEST['deladd1'];
	$dataArray['extension'] = $_REQUEST['extension'];
	$dataArray['deladd2'] = $_REQUEST['deladd2'];
	$dataArray['phone1'] = $_REQUEST['phone1'];
	$dataArray['deladd3'] = $_REQUEST['deladd3'];
	$dataArray['phone2'] = $_REQUEST['phone2'];
	$dataArray['country'] = $_REQUEST['country'];
	$dataArray['mobile'] = $_REQUEST['mobile'];
 	$dataArray['state'] = $_REQUEST['state'];
	$dataArray['fax'] = $_REQUEST['fax'];
	$dataArray['city'] = $_REQUEST['city'];
	$dataArray['email'] = $_REQUEST['email'];	
	$dataArray['pincode'] = $_REQUEST['pincode'];
	$dataArray['tin_no'] = $_REQUEST['tinno'];
	$dataArray['lst_cst'] = $_REQUEST['lst_cst'];
	$dataArray['ce_regn_no'] = $_REQUEST['ce_regn_no'];
	$dataArray['site_range'] = $_REQUEST['site_range'];
	$dataArray['division'] = $_REQUEST['division'];
	$dataArray['regd_off1'] = $_REQUEST['regd_off1'];
	$dataArray['panno'] = $_REQUEST['panno'];
	$dataArray['regd_off2'] = $_REQUEST['regd_off2'];
	$dataArray['commissionerate'] = $_REQUEST['commissionerate'];
	$dataArray['regd_off3'] = $_REQUEST['regd_off3'];
	$dataArray['eccno'] = $_REQUEST['eccno'];
	$dataArray['CompanyId'] =$_REQUEST['CompanyName'];
	if($_REQUEST['exciable']!='Y')
	$dataArray['exciable']='N';
	else
	$dataArray['exciable']=$_REQUEST['exciable'];
	if($_REQUEST['nonexciable']!='Y')
	$dataArray['non_exciable']='N';
	else
	$dataArray['non_exciable']=$_REQUEST['nonexciable'];
    $dataArray['gst_no']=$_REQUEST['gst_no'];
					
	$cond="loccode = '$SelectedLocation'";
	$sql = $s->EditRecord('locations', $dataArray, $cond);
 		$ErrMsg = _('An error occurred updating the') . ' ' . $SelectedLocation . ' ' . _('Site record because');
		$DbgMsg = _('The SQL used to update the Site record was');
		$result = DB_query($sql,$db,$ErrMsg,$DbgMsg);
		
		prnMsg( _('The Site record has been updated'),'success');
		unset($_POST['loccode']);
		unset($_POST['locationname']);
		unset($_POST['prefix']);
		unset($_POST['ProjectType']);
		unset($_POST['SiteCategory']);
		unset($_POST['TenderCode']);
		unset($_POST['ProjectDescription']);
		unset($_POST['ClientCode']);
		unset($_POST['ProjectStatus']);
		unset($_POST['Company']);
		unset($_POST['SiteReference']);
		unset($_POST['deladd1']);
		unset($_POST['deladd2']);
		unset($_POST['deladd3']);
		unset($_POST['DelAdd4']);
		unset($_POST['DelAdd5']);
		unset($_POST['DelAdd6']);
		unset($_POST['extension']);
		unset($_POST['phone1']);
		unset($_POST['phone2']);
		unset($_POST['mobile']);
		unset($_POST['fax']);
		unset($_POST['email']);
		unset($_POST['country']);
		unset($_POST['state']);
		unset($_POST['city']);
		unset($_POST['pincode']);
		unset($_POST['ProjectIncharge']);
		unset($_POST['SiteCoordinator']);
		unset($_POST['EstimatedStartDate']);
		unset($_POST['ActualStartDate']);
		unset($_POST['EstimatedCompletionDate']);
		unset($_POST['ActualCompletionDate']);
		unset($_POST['AgreeDate']);
		unset($_POST['CommencementDate']);
		unset($_POST['TenderValue']);
		unset($_POST['TenderEstimatedValue']);
		unset($_POST['SecurityAdvance']);
		unset($_POST['PaidSecurityAdvance']);
		unset($_POST['PertageofSecurityAdv']);
		unset($_POST['EstimatedBillPeriod']);
		unset($_POST['TenderQtyVariation']);
		unset($_POST['TotalArea']);
		unset($_POST['BuiltUpArea']);
		unset($_POST['BlockFloor']);
		unset($_POST['TaxProvince']);
		unset($_POST['Managed']);		
		unset($SelectedLocation);
		unset($_POST['Contact']);
unset($_POST['tinno']);

	} elseif ($InputError !=1) {

		/* Set the managed field to 1 if it is checked, otherwise 0 */
		if($_POST['Managed'] == 'on') {
			$_POST['Managed'] = 1;
		} else {
			$_POST['Managed'] = 0;
		}

		//SelectedLocation is null cos no item selected on first time round so must be adding a	record must be submitting new entries in the new Location form */
          
		 $sql = "INSERT INTO locations (mst_companyid,
 					loccode,
					locationname,
					prefix,
					projecttype,
					sitecategory,
					tendercode,
					projectdescription,
					clientcoed,
					projectstatus,
					company,
					sitereference,
					deladd1,
					deladd2,
					deladd3,
					deladd4,
					deladd5,
					deladd6,
					extension,
					phone1,
					phone2,
					mobile,
					fax,
					email,
					country,
					state,
					city,
					pincode,
					lst_cst,
					ce_regn_no,
					site_range,
					division,
					regd_off1,
					regd_off2,
					regd_off3,
					panno,
					commissionerate,
					eccno,
					project_incharge,
					site_cordinator,
					estimatedstartdate,
					actualstartdate,
					estimatedcomletiondate,
					actualcompletiondate,
					agreedate,
					commencementdate,
					tendervalue,
					tenderestimatedvalue,
					securityadvance,
					paidsecurityadv,
					persentageofsecurityadv,
					estimated_bill_period,
					tender_qty_variation,
					totalarea,
					built_up_area,
					block_floor,
					contact,
					taxprovinceid,
					exciable,
					non_exciable,
					managed,tin_no,CompanyId,gst_no
					)
			     VALUES (
			    '" . $_POST['CompanyName'] . "',
				'" . $_POST['loccode'] . "',
				'" . $_POST['locationname'] . "',
				'" . $_POST['prefix'] ."',
				'" . $_POST['ProjectType'] ."',
				'" . $_POST['SiteCategory'] . "',
				'" . $_POST['TenderCode'] . "',
				'" . $_POST['ProjectDescription'] . "',
				'" . $_POST['ClientCode'] . "',
				'" . $_POST['ProjectStatus'] . "',
				'" . $_POST['Company'] . "',
				'" . $_POST['SiteReference'] . "',
				'" . $_POST['deladd1'] ."',
				'" . $_POST['deladd2'] ."',
				'" . $_POST['deladd3'] . "',
				'" . $_POST['DelAdd4'] ."',
				'" . $_POST['DelAdd5'] ."',
				'" . $_POST['DelAdd6'] . "',
				'" . $_POST['extension'] . "',
				'" . $_POST['phone1'] . "',
				'" . $_POST['phone2'] . "',
				'" . $_POST['mobile'] . "',
				'" . $_POST['fax'] . "',
				'" . $_POST['email'] . "',
				'" . $_POST['country'] . "',
				'" . $_POST['state'] . "',
				'" . $_POST['city'] . "',
				'" . $_POST['pincode'] . "',
				'" . $_POST['lst_cst'] . "',
				'" . $_POST['ce_regn_no'] . "',
				'" . $_POST['site_range'] . "',
				'" . $_POST['division'] . "',
				'" . $_POST['regd_off1'] . "',
				'" . $_POST['panno'] . "',
				'" . $_POST['regd_off2'] . "',
				'" . $_POST['commissionerate'] . "',
				'" . $_POST['regd_off3'] . "',
				'" . $_POST['eccno'] . "',
				'" . $_POST['ProjectIncharge'] . "',
				'" . $_POST['SiteCoordinator'] . "',
				'" . $_POST['EstimatedStartDate'] . "',
				'" . $_POST['ActualStartDate'] . "',
				'" . $_POST['EstimatedCompletionDate'] . "',
				'" . $_POST['ActualCompletionDate'] . "',
				'" . $_POST['AgreeDate'] ."',
				'" . $_POST['CommencementDate'] ."',
				'" . $_POST['TenderValue'] . "',
				'" . $_POST['TenderEstimatedValue'] . "',
				'" . $_POST['SecurityAdvance'] . "',
				'" . $_POST['PaidSecurityAdvance'] . "',
				'" . $_POST['PertageofSecurityAdv '] . "',
				'" . $_POST['EstimatedBillPeriod'] . "',
				'" . $_POST['TenderQtyVariation'] . "',
				'" . $_POST['TotalArea '] . "',
				'" . $_POST['BuiltUpArea'] . "',
				'" . $_POST['BlockFloor '] . "',
				'" . $_POST['Contact'] . "',
				'" . $_POST['TaxProvince'] . "',
				'".$_POST['exciable']."',
				'".$_POST['nonexciable']."',
				'" . $_POST['Managed'] . "','" . $_POST['tinno'] . "', '" .$_POST['CompanyName']. "','".$_POST['gst_no']."'
			)";
			
			
			

		$ErrMsg =  _('An error occurred inserting the new Site record because');
		$Dbgmsg =  _('The SQL used to insert the Site record was');
		$result = DB_query($sql,$db,$ErrMsg,$DbgMsg);
		prnMsg( _('The new Site record has been added'),'success');

	/* Also need to add LocStock records for all existing stock items */

	   //$sql = "INSERT INTO locstock (
//					loccode,
//					stockid,
//					quantity,
//					reorderlevel)
//			SELECT '" . $_POST['loccode'] . "',
//				stockmaster.stockid,
//				0,
//				0
//			FROM stockmaster";
//
//		$ErrMsg =  _('An error occurred inserting the new Site stock records for all pre-existing parts because');
//		$DbgMsg =  _('The SQL used to insert the new stock Site records was');
//		$result = DB_query($sql,$db,$ErrMsg, $DbgMsg);

		//echo '<br>........ ' . _('and new stock Sites inserted for all existing stock items for the new Site');
		unset($_POST['loccode']);
		unset($_POST['locationname']);
		unset($_POST['prefix']);
		unset($_POST['ProjectType']);
		unset($_POST['SiteCategory']);
		unset($_POST['TenderCode']);
		unset($_POST['ProjectDescription']);
		unset($_POST['ClientCode']);
		unset($_POST['ProjectStatus']);
		unset($_POST['Company']);
		unset($_POST['SiteReference']);
		unset($_POST['deladd1']);
		unset($_POST['deladd2']);
		unset($_POST['deladd3']);
		unset($_POST['DelAdd4']);
		unset($_POST['DelAdd5']);
		unset($_POST['DelAdd6']);
		unset($_POST['extension']);
		unset($_POST['phone1']);
		unset($_POST['phone2']);
		unset($_POST['mobile']);
		unset($_POST['fax']);
		unset($_POST['email']);
		unset($_POST['country']);
		unset($_POST['state']);
		unset($_POST['city']);
		unset($_POST['pincode']);
		unset($_POST['ProjectIncharge']);
		unset($_POST['SiteCoordinator']);
		unset($_POST['EstimatedStartDate']);
		unset($_POST['ActualStartDate']);
		unset($_POST['EstimatedCompletionDate']);
		unset($_POST['ActualCompletionDate']);
		unset($_POST['AgreeDate']);
		unset($_POST['CommencementDate']);
		unset($_POST['TenderValue']);
		unset($_POST['TenderEstimatedValue']);
		unset($_POST['SecurityAdvance']);
		unset($_POST['PaidSecurityAdvance']);
		unset($_POST['PertageofSecurityAdv']);
		unset($_POST['EstimatedBillPeriod']);
		unset($_POST['TenderQtyVariation']);
		unset($_POST['TotalArea']);
		unset($_POST['BuiltUpArea']);
		unset($_POST['BlockFloor']);
		unset($_POST['TaxProvince']);
		unset($_POST['Managed']);		
		unset($SelectedLocation);
		unset($_POST['Contact']);unset($_POST['tinno']);

	}


	/* Go through the tax authorities for all Locations deleting or adding TaxAuthRates records as necessary */

	$result = DB_query('SELECT COUNT(taxid) FROM taxauthorities',$db);
	$NoTaxAuths =DB_fetch_row($result);

	$DispTaxProvincesResult = DB_query('SELECT taxprovinceid FROM locations',$db);
	$TaxCatsResult = DB_query('SELECT taxcatid FROM taxcategories',$db);
	if (DB_num_rows($TaxCatsResult) > 0 ) { // This will only work if there are levels else we get an error on seek.

		while ($myrow=DB_fetch_row($DispTaxProvincesResult)){
			/*Check to see there are TaxAuthRates records set up for this TaxProvince */
			$NoTaxRates = DB_query('SELECT taxauthority FROM taxauthrates WHERE dispatchtaxprovince=' . $myrow[0], $db);

			if (DB_num_rows($NoTaxRates) < $NoTaxAuths[0]){

				/*First off delete any tax authoritylevels already existing */
				$DelTaxAuths = DB_query('DELETE FROM taxauthrates WHERE dispatchtaxprovince=' . $myrow[0],$db);

				/*Now add the new TaxAuthRates required */
				while ($CatRow = DB_fetch_row($TaxCatsResult)){
					$sql = 'INSERT INTO taxauthrates (taxauthority,
										dispatchtaxprovince,
										taxcatid)
							SELECT taxid,
								' . $myrow[0] . ',
								' . $CatRow[0] . '
							FROM taxauthorities';

					$InsTaxAuthRates = DB_query($sql,$db);
				}
				DB_data_seek($TaxCatsResult,0);
			}
		}
	}

echo "<script>location.href='index.php?route=Locations&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
} 
if (isset($_GET['delete'])) {
//the link to delete a selected record was clicked instead of the submit button

	$CancelDelete = 0;

// PREVENT DELETES IF DEPENDENT RECORDS
	$sql= "SELECT COUNT(*) FROM salesorders WHERE fromstkloc='$SelectedLocation'";
	$result = DB_query($sql,$db);
	$myrow = DB_fetch_row($result);
	if ($myrow[0]>0) {
		$CancelDelete = 1;
		prnMsg( _('Cannot delete this location because sales orders have been created delivering from this location'),'warn');
		echo  _('There are') . ' ' . $myrow[0] . ' ' . _('sales orders with this Location code');
	} else {
		$sql= "SELECT COUNT(*) FROM stockmoves WHERE stockmoves.loccode='$SelectedLocation'";
		$result = DB_query($sql,$db);
		$myrow = DB_fetch_row($result);
		if ($myrow[0]>0) {
			$CancelDelete = 1;
			prnMsg( _('Cannot delete this location because stock movements have been created using this location'),'warn');
			echo '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('stock movements with this Location code');

		} else {
			$sql= "SELECT COUNT(*) FROM locstock WHERE locstock.loccode='$SelectedLocation' AND locstock.quantity !=0";
			$result = DB_query($sql,$db);
			$myrow = DB_fetch_row($result);
			if ($myrow[0]>0) {
				$CancelDelete = 1;
				prnMsg(_('Cannot delete this location because location stock records exist that use this location and have a quantity on hand not equal to 0'),'warn');
				echo '<br> ' . _('There are') . ' ' . $myrow[0] . ' ' . _('stock items with stock on hand at this location code');
			} else {
				$sql= "SELECT COUNT(*) FROM www_users WHERE www_users.defaultlocation='$SelectedLocation'";
				$result = DB_query($sql,$db);
				$myrow = DB_fetch_row($result);
				if ($myrow[0]>0) {
					$CancelDelete = 1;
					prnMsg(_('Cannot delete this location because it is the default location for a user') . '. ' . _('The user record must be modified first'),'warn');
					echo '<br> ' . _('There are') . ' ' . $myrow[0] . ' ' . _('users using this location as their default location');
				} else {
					$sql= "SELECT COUNT(*) FROM bom WHERE bom.loccode='$SelectedLocation'";
					$result = DB_query($sql,$db);
					$myrow = DB_fetch_row($result);
					if ($myrow[0]>0) {
						$CancelDelete = 1;
						prnMsg(_('Cannot delete this location because it is the default location for a bill of material') . '. ' . _('The bill of materials must be modified first'),'warn');
						echo '<br> ' . _('There are') . ' ' . $myrow[0] . ' ' . _('bom components using this location');
					} else {
						$sql= "SELECT COUNT(*) FROM workcentres WHERE workcentres.location='$SelectedLocation'";
						$result = DB_query($sql,$db);
						$myrow = DB_fetch_row($result);
						if ($myrow[0]>0) {
							$CancelDelete = 1;
							prnMsg( _('Cannot delete this location because it is used by some work centre records'),'warn');
							echo '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('works centres using this location');
						} else {
							$sql= "SELECT COUNT(*) FROM workorders WHERE workorders.loccode='$SelectedLocation'";
							$result = DB_query($sql,$db);
							$myrow = DB_fetch_row($result);
							if ($myrow[0]>0) {
								$CancelDelete = 1;
								prnMsg( _('Cannot delete this location because it is used by some work order records'),'warn');
								echo '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('work orders using this location');
							}else {
								$sql= "SELECT COUNT(*) FROM custbranch WHERE custbranch.defaultlocation='$SelectedLocation'";
								$result = DB_query($sql,$db);
								$myrow = DB_fetch_row($result);
								if ($myrow[0]>0) {
									$CancelDelete = 1;
									prnMsg(_('Cannot delete this location because it is used by some branch records as the default location to deliver from'),'warn');
									echo '<br> ' . _('There are') . ' ' . $myrow[0] . ' ' . _('branches set up to use this location by default');
								}
							}
						}
					}
				}
			}
		}
	}
	if (!$CancelDelete) {

		/* need to figure out if this location is the only one in the same tax province */
		$result = DB_query("SELECT taxprovinceid FROM locations WHERE loccode='" . $SelectedLocation . "'",$db);
		$TaxProvinceRow = DB_fetch_row($result);
		$result = DB_query("SELECT COUNT(taxprovinceid) FROM locations WHERE taxprovinceid=" .$TaxProvinceRow[0],$db);
		$TaxProvinceCount = DB_fetch_row($result);
		if ($TaxProvinceCount[0]==1){
		/* if its the only location in this tax authority then delete the appropriate records in TaxAuthLevels */
			$result = DB_query('DELETE FROM taxauthrates WHERE dispatchtaxprovince=' . $TaxProvinceRow[0],$db);
		}

		$result= DB_query("DELETE FROM locstock WHERE loccode ='" . $SelectedLocation . "'",$db);
		$result = DB_query("DELETE FROM locations WHERE loccode='" . $SelectedLocation . "'",$db);

		prnMsg( _('Location') . ' ' . $SelectedLocation . ' ' . _('has been deleted') . '!', 'success');
		unset ($SelectedLocation);
	} //end if Delete Location
	unset($SelectedLocation);
	echo "<script>location.href='index.php?route=Locations&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1><?php echo 'Unit Creation'; ?></h1>
<?php if (!isset($SelectedLocation)) 
{
	 	$sql="SELECT  CompanyId,loccode,locationname,mst_companyid FROM locations where mst_companyid='".$_SESSION['ACGCompanyId']."'
		 order by mst_companyid";
		 
		
	$result = DB_query($sql,$db);

	if (DB_num_rows($result)==0){
		prnMsg (_('There are no Sites that match up with a tax province record to display. Check that tax provinces are set up for all dispatch Sites'),'error');
	}
?>



 
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata"><strong>Company</strong></td>
	<td class="tabledata"><strong>Unit Code</strong></td>
	<td class="tabledata"><strong>Unit Name</strong></td>
	<td class="tabledata" colspan="2"><strong>Action</strong></td>
</tr><?php 
$k=0; //row colour counter
while ($myrow = DB_fetch_array($result)) 
{
$Chkzone=DB_query("SELECT * FROM zone_master where Site='".$myrow['loccode']."'",$db);
if(DB_num_rows($Chkzone)>0)
{
	$delstatus='0';
}
else
{
	$delstatus='1';
}


$chkstore=DB_query("SELECT * FROM storetype where Store_Site='".$myrow['loccode']."'",$db);
if(DB_num_rows($chkstore)>0)
{
	$delstatus12='0';
}
else
{
	$delstatus12='1';
}




	if($myrow['managed'] == 1) {
		$myrow['managed'] = _('Yes');
	}  else {
		$myrow['managed'] = _('No');
	}
?>
<tr class="row1">
		<td><?php 
				echo $s->GetFieldValue('companies',"CompanyId='".$myrow['mst_companyid']."'" ,'CompanyName')
		?></td>
	<td><?php echo $myrow['loccode'];?></td>
		<td><?php echo $myrow['locationname'];?></td>
		<td><a href="index.php?route=Locations&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&SelectedLocation=<?php echo $myrow['loccode'];?>"><img src="images/icon_edit.gif"  width="10" height="10"/></a></td>
		<td>
		<?php if($delstatus!='0' || $delstatus12!='0') {?>
		<a href="index.php?route=Locations&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&SelectedLocation=<?php echo $myrow['loccode'];?>&delete=1"><img src="images/Icon_Delete3.gif" width="10" height="10" /></a>
		<?php }else{ ?>
		<img src="images/Icon_Delete3.gif" width="10" alt="NO Permission Because This Branch Has Some Zones Either Some Store Type" title="NO Permission Because This Branch Has Some Zones Either Some Store Type" height="10" />
		<?php } ?>
		</td>
  </tr><?php
	}?>
</table>
<?php
}
?>


<?php
if (isset($SelectedLocation)) {  ?>
<div class="centre"><a href="<?php echo $_SERVER['PHP_SELF'];?>"><?php echo _('REVIEW RECORDS'); ?></a></div>
<?php } ?>
<?php
if (!isset($_GET['delete'])) {
	echo "<form method='post' action=''>";
	if (isset($SelectedLocation)) {
		//editing an existing Location
		$sql = "SELECT *
			FROM locations
			WHERE loccode='$SelectedLocation'";
//echo $sql; die;
		$result = DB_query($sql, $db);
		$myrow = DB_fetch_array($result);
  $_POST['city'] 					= $myrow['city'];
  $_POST['country'] 					= $myrow['country'];
  $_POST['state'] 					= $myrow['state'];
  
   		$_POST['tinno'] 					= $myrow['tin_no'];		
		$_POST['loccode'] 					= $myrow['loccode'];
		$CompanyName1                      =  $myrow['mst_companyid'];
		$_POST['prefix'] 					= $myrow['prefix'];
		$_POST['extension'] 				= $myrow['extension'];
		$_POST['locationname']  			= $myrow['locationname'];
		$_POST['deladd1'] 					= $myrow['deladd1'];
		$_POST['deladd2'] 					= $myrow['deladd2'];
		$_POST['deladd3'] 					= $myrow['deladd3'];
		$_POST['phone1'] 					= $myrow['phone2'];
		$_POST['phone2'] 					= $myrow['phone1'];
		$_POST['mobile'] 					= $myrow['mobile'];
		$_POST['pincode'] 					= $myrow['pincode'];
		$_POST['Contact']					= $myrow['contact'];
		$_POST['Tel']     					= $myrow['tel'];
		$_POST['fax']      					= $myrow['fax'];
		$_POST['email']    					= $myrow['email'];
		$_POST['TaxProvince'] 				= $myrow['taxprovinceid'];
		$_POST['Managed'] 					= $myrow['managed'];
        $_POST['project_incharge'] 			= $myrow['project_incharge'];
		$_POST['site_cordinator'] 			= $myrow['site_cordinator'];
 		$_POST['estimatedstartdate'] 		= $myrow['estimatedstartdate'];
		$_POST['actualstartdate'] 			= $myrow['actualstartdate'];
		$_POST['estimatedcomletiondate'] 	= $myrow['estimatedcomletiondate'];
		$_POST['actualcompletiondate'] 		= $myrow['actualcompletiondate'];
		$_POST['agreedate'] 				= $myrow['agreedate'];
		$_POST['commencementdate'] 			= $myrow['commencementdate'];
		$_POST['tendervalue'] 				= $myrow['tendervalue'];
		$_POST['tenderestimatedvalue'] 		= $myrow['tenderestimatedvalue'];
		$_POST['securityadvance'] 			= $myrow['securityadvance'];
		$_POST['paidsecurityadv'] 			= $myrow['paidsecurityadv'];
		$_POST['persentageofsecurityadv'] 	= $myrow['persentageofsecurityadv'];
		$_POST['estimated_bill_period'] 	= $myrow['estimated_bill_period'];
		$_POST['tender_qty_variation'] 		= $myrow['tender_qty_variation'];
		$_POST['totalarea'] 				= $myrow['totalarea'];
		$_POST['lst_cst'] 					= $myrow['lst_cst'];
		$_POST['ce_regn_no'] 				= $myrow['ce_regn_no'];
		$_POST['site_range'] 					= $myrow['site_range'];
		$_POST['division'] 					= $myrow['division'];
		$_POST['regd_off1'] 					= $myrow['regd_off1'];
		$_POST['panno'] 					= $myrow['panno'];
		$_POST['regd_off2'] 					= $myrow['regd_off2'];
		$_POST['commissionerate'] 					= $myrow['commissionerate'];
		$_POST['regd_off3'] 					= $myrow['regd_off3'];
		$_POST['eccno'] 					= $myrow['eccno'];
		$_POST['exciable'] 					= $myrow['exciable'];
		$_POST['nonexciable'] 					= $myrow['non_exciable'];
		
		
		
		echo "<input type=hidden name=SelectedLocation VALUE=" . $SelectedLocation . '>';
		echo "<input type=hidden name=loccode VALUE=" . $_POST['loccode'] . '>';
		//echo '<table> <tr><td>' . _('Site Code') . ':</td><td>';
		//echo $_POST['loccode'] . '</td></tr>';
	} else { //end of if $SelectedLocation only do the else when a new record is being entered
		if (!isset($_POST['loccode'])) {
			$_POST['loccode'] = '';
		}
 	}
 
	?>
	
<br />
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1"><h3>Add New Unit </h3></td>
</tr>
<tr class="row1">
	<td class="tabledata1">
	<table width="100%" border="0" cellpadding="3" cellspacing="0">
	<?php  if($_GET['SelectedLocation']!="") { ?>
	 <tr class="row1">
		<td class="tabledata1"><strong><?php echo _('Unit Code') . ':'; ?></strong></td>
		<td><input type="Text" name="loccode" value="<?php echo $_POST['loccode']; ?>" style="width:150px;" maxlength=50 ></td>
		<td class="tabledata1"><strong>Company</strong></td>
		
		<td><select name="CompanyName" style="width:150px;">
			<option value="0">--Select--</option>
			<?php
			$CompanyName=DB_query("Select * from companies where 1=1",$db);
			while($CompanyRow=DB_fetch_array($CompanyName)){ ?>
			<option value="<?php echo $CompanyRow['CompanyId']?>"<?php echo ($CompanyName1 == $CompanyRow['CompanyId']?'selected':'');?>><?php echo $CompanyRow['CompanyName']?></option>	
		<?php }?>
		</select>	
		</td>
	  </tr>
	<?php }else { ?>
	 <tr class="row1">
		<td class="tabledata1"><strong><?php echo _('Unit Code') . ':'; ?></strong></td>
		<td><input type="Text" name="loccode" value="<?php echo $_POST['loccode']; ?>" style="width:150px;" maxlength=50></td>
		<td class="tabledata1"><strong>Company</strong></td>
		<td><select name="CompanyName" style="width:150px;">
			<option value="0">--Select--</option>
			<?php
			$CompanyName=DB_query("Select * from companies where 1=1",$db);
			while($CompanyRow=DB_fetch_array($CompanyName))
			{ ?>
			<option value="<?php echo $CompanyRow['CompanyId']?>"><?php echo $CompanyRow['CompanyName']?></option>	
				
		<?php	}
			
			   
			?>
		</select>	
		</td>
	  </tr>
	  <?php } ?>
	  <tr class="row1">
		<td class="tabledata1"><strong><?php echo _('Unit Name') . ':'; ?></strong></td>
		<td><input type="Text" name="locationname" value="<?php echo $_POST['locationname']; ?>" style="width:150px;" maxlength=50></td>
		<td class="tabledata1"><strong><?php echo _('Prefix') . ':'; ?></strong></td>
		<td><input type="Text" name="prefix" value="<?php echo $_POST['prefix']; ?>" style="width:150px;" maxlength=50></td>
	  </tr>

     </table>
    </td>
  </tr>
<tr class="row1">
	<td>
	<table width="100%" border="0" cellpadding="3" cellspacing="0">
	 <tr class="row1">
		<td class="tabledata1" rowspan="3" valign="top"><strong><?php echo _('Address') . ':'; ?></strong></td>
		<td><input type="Text" name="deladd1" value="<?php echo $_POST['deladd1']; ?>" style="width:150px;" maxlength=50></td>
		<td class="tabledata1"><strong><?php echo _('Extension') . ':'; ?></strong></td>
		<td><input type="Text" name="extension" value="<?php echo $_POST['extension']; ?>" style="width:150px;" maxlength=50></td>
	  </tr>
	   <tr class="row1">
		<td><input type="Text" name="deladd2" value="<?php echo $_POST['deladd2']; ?>" style="width:150px;" maxlength=50></td>
		<td class="tabledata1"><strong><?php echo _('Phone1') . ':'; ?></strong></td>
		<td><input type="Text" name="phone1" value="<?php echo $_POST['phone1']; ?>" style="width:150px;"  maxlength=50></td>
	  </tr>
	  <tr class="row1">
		<td><input type="Text" name="deladd3" value="<?php echo $_POST['deladd3']; ?>" style="width:150px;" maxlength=50></td>
		<td class="tabledata1"><strong><?php echo _('Phone2') . ':'; ?></strong></td>
		<td><input type="Text" name="phone2" value="<?php echo $_POST['phone2']; ?>" style="width:150px;" maxlength=50></td>
	  </tr>
	  <tr class="row1">
		<td class="tabledata1"><strong><?php echo _('Country') . ':'; ?></strong></td>
		<td><?php 	$location->Country($_POST['country']);?></td>
		<td class="tabledata1"><strong><?php echo _('Mobile') . ':'; ?></strong></td>
		<td><input type="Text" name="mobile" value="<?php echo $_POST['mobile']; ?>" style="width:150px;" maxlength=50></td>
	  </tr>
	   <tr class="row1">
		<td class="tabledata1"><strong><?php echo _('State') . ':'; ?></strong></td>
		<td><div id='txtstate'><?php 	$location->State($_POST['country'],$_POST['state']);?></div> 		</td>
		<td class="tabledata1"><strong><?php echo _('Fax') . ':'; ?></strong></td>
		<td><input type="Text" name="fax" value="<?php echo $_POST['fax']; ?>" style="width:150px;" maxlength=50></td>
	  </tr>
	  <tr class="row1">
		<td class="tabledata1"><strong><?php echo _('City') . ':'; ?></strong></td>
		<td>
		<div id='txtcity'>
		<?php 	$location->City($_POST['country'],$_POST['state'] ,$_POST['city']);?>
		</div>	 </td>
		<td class="tabledata1"><strong><?php echo _('E-mail') . ':'; ?></strong></td>
		<td><input type="Text" name="email" value="<?php echo $_POST['email']; ?>" style="width:150px;" maxlength=50></td>
	  </tr>
	  
	  
	 <tr class="row1">
		<td class="tabledata1"><strong><?php echo _('Pincode') . ':'; ?></strong></td>
		<td><input type="Text" name="pincode" value="<?php echo $_POST['pincode']; ?>" style="width:150px;" maxlength=50></td>
		<td><strong><?php echo _('Tinno.') . ':'; ?></strong></td>
		<td><input type="Text" name="tinno" style="width:150px;" value="<?php echo $_POST['tinno']; ?>"  /></td>
	  </tr>
	  <tr class="row1">
		<td class="tabledata1"><strong><?php echo _('LST/CST') . ':'; ?></strong></td>
		<td><input type="Text" name="lst_cst" value="<?php echo $_POST['lst_cst']; ?>" style="width:150px;" maxlength=50></td>
		<td><strong><?php echo _('C.E. Regn. No.') . ':'; ?></strong></td>
		<td><input type="Text" name="ce_regn_no" style="width:150px;" value="<?php echo $_POST['ce_regn_no']; ?>"  /></td>
	  </tr>
	  <tr class="row1">
		<td class="tabledata1"><strong><?php echo _('Range') . ':'; ?></strong></td>
		<td><input type="Text" name="site_range" value="<?php echo $_POST['site_range']; ?>" style="width:150px;" maxlength=50></td>
		<td class="tabledata1"><strong><?php echo _('Division') . ':'; ?></strong></td>
		<td><input type="Text" name="division" style="width:150px;" value="<?php echo $_POST['division']; ?>"  /></td>
	  </tr>
	  <tr class="row1">
	  	<td class="tabledata1" rowspan="3" valign="top"><strong><?php echo _('Regd. Office') . ':'; ?></strong></td>
		<td><input type="Text" name="regd_off1" value="<?php echo $_POST['regd_off1']; ?>" style="width:150px;" maxlength=50></td>
		<td class="tabledata1"><strong><?php echo _('PAN No') . ':'; ?></strong></td>
		<td><input type="Text" name="panno" value="<?php echo $_POST['panno']; ?>" style="width:150px;" maxlength=50></td>
	  </tr>
	  
	   <tr class="row1">
	  	<td><input type="Text" name="regd_off2" value="<?php echo $_POST['regd_off2']; ?>" style="width:150px;" maxlength=50></td>
		<td class="tabledata1"><strong><?php echo _('Commissionerate') . ':'; ?></strong></td>
		<td><input type="Text" name="commissionerate" value="<?php echo $_POST['commissionerate']; ?>" style="width:150px;" maxlength=50></td>
	  </tr>
	  <tr class="row1">
	  <td><input type="Text" name="regd_off3" value="<?php echo $_POST['regd_off3']; ?>" style="width:150px;" maxlength=50></td>
	  <td class="tabledata1"><strong><?php echo _('ECC No') . ':'; ?></strong></td>
		<td><input type="Text" name="eccno" value="<?php echo $_POST['eccno']; ?>" style="width:150px;" maxlength=50></td>
	  </tr>
	   <tr class="row1">
	  	<td class="tabledata1" colspan="3"  valign="top"><strong><?php echo _('Excisable') . ':'; ?></strong>
		<input type="checkbox" name="exciable" id="exciable1" value="Y" <?php if($_POST['exciable']=='Y') { ?>?  checked="checked" <?php } ?>/>
		<strong><?php echo _('Non Excisable') . ':'; ?></strong><input type="checkbox" name="nonexciable"
		 id="nonexciable" value="Y" <?php if($_POST['nonexciable']=='Y') { ?>?  checked="checked" <?php } ?>/>
		<strong>Both</strong><input type="checkbox" name="" value="Both" id="both"  onclick="showdata();"/>
		</td>
		</tr>
	  <tr>
	  	<td><strong>GST No.</strong></td>
	  	<td><input type="text" name="gst_no" id="gst_no" style="width:150px;" value="<?php echo $myrow['gst_no']; ?>" /></td>
      </tr>	
     </table>
	</td>
  </tr>
</table>
<table width="100%" border="0" cellpadding="3" cellspacing="0">
 <tr class="row1"> 
<td class="tabledata1" style="text-align:center;"><input type="Submit" name="submit" class="search" value="<?php echo _('Save'); ?>"></td>
  </tr>
</table>	
</form>
<?php } //end if record deleted no point displaying form to add record
include('includes/footer.inc');?>
<script type="text/javascript">
function selectTender(value)
{
	if(value=='Internal'){
		document.getElementById("TenderCode").disabled=true;
		document.getElementById("SiteReference").disabled=true;
		//document.frm.sel.disabled = disableIt;
	}
	else if(value=='External')
	{
	     document.getElementById("TenderCode").disabled=false;
	}
	else
	{
	    
	}
	//alert(value);
	return true;
}

function SelectSiteCategory(value)
{
	if(value=='Running'){
		document.getElementById("Company").disabled=true;
		document.getElementById("SiteReference").disabled=true;
		//document.frm.sel.disabled = disableIt;
	}
	else if(value=='Commercial')
	{
	     document.getElementById("SiteReference").disabled=false;
		 document.getElementById("Company").disabled=false;
	}
	else
	{
	}
	//alert(value);
	return true;
}

</script>
<link rel="stylesheet" href="css/jquery.ui.all.css" />
<script src="js/jquery-1.5.1.js"></script>
<script src="ui/jquery.ui.core.js"></script>
<script src="ui/jquery.ui.widget.js"></script>


<script>
$(function() {
$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' ,changeMonth: true,
		changeYear: true,
		yearRange: '-80:+20'});
});

function showdata()
{
	if(document.getElementById("both").checked ==true)
	{
		document.getElementById("exciable1").checked=true;
		document.getElementById("nonexciable").checked=true;
	}
	else
	{
		document.getElementById("exciable1").checked=false;
		document.getElementById("nonexciable").checked=false;
	}
}
</script>