<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com © 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";
include "../includes/arrays.inc.php";
if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}
 if(isset( $_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"]!= ""){
	$adminname = $_SESSION["sess_adminname"];
}else{
	header("Location:login.php");
	exit;
}
if(isset($_GET['id']) and  (!empty($_GET['id'])))
{
	$id=$_GET['id'];
}

else
{
 	header("location:tax.php");
}		

	$sql="select * FROM ".$tableprefix."taxrate where Tid='".$id."' ";
	$rs=mysql_query($sql) or die(mysql_error());
	
	if(mysql_num_rows($rs)>0)
	{ // check number of rows
	
		$rr=mysql_fetch_array($rs);
		$vTaxid=$rr['Tid'];
		$vtaxname=$rr['taxname'];
		$vtaxbase=$rr['vtaxbase'];
		$countryname =$rr['vCountry'];
		$vstate=$rr['vState'];
		$ntaxrate =$rr['ntaxrate'];
		$vtaxaddress =$rr['vtaxaddress'];
		$vrateenable=$rr['rate_enable'];
		$countState=split(',',$vstate);
		
	} // end numrows check 
	
if(trim($countryname)=='')
{
	$countryname='US';
}//end if

$varOnload="updateState('".$countryname."','".$vstate."');";

if(isset($_POST['btn_back']))
{
		header("Location:tax.php");
}
//selecting settings for fedex shipping
$message="";
if(isset($_POST['submit']))
{
	$vtaxname=addslashes($_POST['taxname']);
	$countryname=$_POST['country'];
	$vtaxbase=$_POST['vtaxbase'];
	$ntaxrate=addslashes($_POST['taxrate']);
	$vtaxaddress=addslashes($_POST['taxaddress']);
	$vrateenable=addslashes($_POST['rate_enable']);
	
	if(is_array($_POST['state']))
	{// 
		$vstate=addslashes(join(",",$_POST['state']));
		
	}// end if 	
	else
	{
		$vstate=addslashes($_POST['state']);
	}
	if(is_array($_POST['state']))
	{// 
		$vstate=addslashes(join(",",$_POST['state']));
	}// end if 	
	else
	{
		$vstate=addslashes($_POST['state']);
	}
	if($_POST["bill_state"])
	{
		$vstate=addslashes($_POST['bill_state']);
	}
	
	if(empty($vtaxname))
	{
		$message= ENT_TAXNAME."<br>";
	}
	
	if(empty($countryname))
	{
		$message.=SEL_COUNTRY."<br>";
	}
	if(empty($vstate))
	{
		$message.=STATE_MANDATE."<br>";
	}
	if(empty($ntaxrate))
	{
		$message.=ENT_TRATE."<br>";
	}
	
	if(!empty($ntaxrate) && !is_numeric($ntaxrate))
	{
		$message.=T_NUM."<br>";
	}
	
	if( $message=="")
	{
	
	$sql_update="update " .$tableprefix."taxrate set taxname='".$vtaxname."',vtaxbase='".$vtaxbase."',vCountry='".$countryname."',
				 vState='".$vstate."',ntaxrate='".$ntaxrate."',vtaxaddress='".$vtaxaddress."',rate_enable='".$vrateenable."' 
				 where Tid='".$id."' ";
	$result=mysql_query($sql_update);
	
	$msg_succ=T_SAVED;
		header('location:tax.php?rghtMenu=rghtMenu1&msg=u');
				exit();
	
	}	
	 
	if(mysql_affected_rows()>0)
		{
			//$message = "Settings Updated Successfully !!";
		}		
	
}	
$message=($message!='')?$message:$msg_succ;
include "../includes/htmltop.php";
$regCount=($_POST['state']!='')?$_POST['state']:$countState;
?>
<style type="text/css">
<!--
.style1 {color: #FF0000}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="javascript">
function validatetax()
{
return true;
if(document.taxoptions.taxname.value=="")
{
	alert("<?php echo PLS_TNAME;?>");
	document.taxoptions.taxname.focus();
	return false;
}
if(document.taxoptions.country.value=="")
{
	alert("<?php echo PLS_COUNTRY;?>");
	document.taxoptions.Country.focus();
	return false;
}
if(document.getElementById('stateSelect').value=="")
	{
	alert("<?php echo PLS_SELSTATE;?>");
	return false;
	}
if(document.taxoptions.taxrate.value=="")
{
	alert("<?php echo PLS_ENT_TRATE;?>");
	document.taxoptions.taxrate.focus();
	return false;
}
else if(isNaN(document.taxoptions.taxrate.value))
{
	alert("<?php echo TRATE_INT;?>");
	document.taxoptions.taxrate.focus();
	return false;
}	
else
return true;

}

</script>
<body onLoad="<?php echo $varOnload;?>">
<center>
		
		<div align="left" id="layout">
		
		
				
				
				<!--header area start -->
<?php

	include_once("../includes/topheader_admin.php")

?>
				
				<!--header area end -->
				
				
				
				
				<div align="left" class="main_body">
				<!--main body start -->
				
				
				
<?php
		include_once("../includes/admintopmenu.php")
?>
		
<form action="" method="post" name="taxoptions" onSubmit="return validateFedex();">
		
<!-- The id of the country field(s).  If more than one, seperate with spaces -->
<input type="hidden" value="countrySelect" name="cs_config_country_field" id="cs_config_country_field">
<input type="hidden" value="stateSelect"   name="cs_config_state_field"   id="cs_config_state_field">

<!-- The id of the the fields holding the default values.  If more than one, seperate with spaces -->
<input type="hidden" value="countryDefault" name="cs_config_country_default" id="cs_config_country_default">
<input type="hidden" value="stateDefault"   name="cs_config_state_default"   id="cs_config_state_default">


<!-- The actual default values -->
<input type="hidden" value="<?php echo $countryname;?>" name="countryDefault" id="countryDefault">
<input type="hidden" value="<?php echo $vstate;?>"   name="stateDefault"   id="stateDefault">
<SCRIPT type="text/javascript" SRC="../js/country-state.js"></SCRIPT>
		<div align="left" class="content_area">
				<!--content area start -->
				<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tr><td height="35"><?php include_once("../includes/taxmenu.php")?></td>
			</tr>
				<tr>
				<td>
						<div align="left" class="page_title"><?php echo EDIT_TRATE;?></div>
						<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            			<br>
						<div align="left">
						
								<table width="100%" border="0" cellpadding="3" cellspacing="3" class="manage_table">
								<tr>
								  <td width="40%"  height="40"> <?php echo T_NAME;?><span class="style1">*</span></td>
								  <td width="1%"><LABEL for=taxtype3>
                                  </LABEL>
						          :<BR></td>
								<td width="59%"><input type="text" name="taxname" id="taxname" value="<?php echo $vtaxname;?>"></td>
								</tr>
								<tr>
								  <td  height="40"><?php echo BASED_ON;?> <span class="style1">*</span></td>
								  <td>:</td>
								  <td><select name="vtaxbase" id="vtaxbase">
								  <option value="Subtotal" <?php if($vtaxbase=="Subtotal"){ echo "selected";}?>><?php echo SUBTOTAL;?></option>
								  <option value="Subtotal and Shipping" <?php if($vtaxbase=="Subtotal and Shipping"){ echo "selected";}?>><?php echo SUB_SHPNG;?></option>
							      </select></td>
								  </tr>
								<tr>
								  <td  height="40"><?php echo SHPNG_COUNTRY;?><span class="style1">*</span></td>
								  <td>:</td>
								  <td><select id='countrySelect' name='country' onchange='updateState(this.id)'>
                                  </select></td>
								</tr>
								<tr>
								  <td  height="40" align="left" valign="top"><?php echo STATE;?> <span class="style1">*</span></td>
								  <td align="left" valign="top">:</td>
								  <td><?php
											if(is_array($regCount))
											{
												foreach($regCount as $key=>$val)
												{
													$val=str_replace(' ','_',$val);
													echo '<input type="hidden" id="editCounty'.$key.'" name="editCounty'.$key.'" value='.$val.'">';
												}//end foreach
											}//end if
									?>
									<input type="hidden" id="editCount" name="editCount" value="<?php echo count($regCount);?>">
								  <select name='state[]' size="20" id='stateSelect' multiple> 
								  </select>
								  <SCRIPT  type="text/javascript">initCountry(); </SCRIPT>
								  <?php
								  			if($countryname!='US')
											{
									?>		
												<input type="hidden" name="stateNonUsVal" value="<?php echo $vstate;?>" id="stateNonUsVal">
									<?php
											}//end if
									?></td>
								</tr>
								<tr>
								  <td  height="40"><?php echo TAX_RATE;?><span class="style1">*</span></td>
								  <td>:</td>
								<td><input name="taxrate" type="text" id="taxrate" size="10" maxlength="4" value="<?php echo $ntaxrate;?>">
								%</td>
								</tr>
								<tr>
								  <td  height="40"><?php echo TAXD_ADD;?><span class="style1">*</span></td>
								  <td>:</td>
								  <td><select name="taxaddress">
								  <option value="Billing" <?php if($vtaxaddress=="billing"){ echo "selected"; }?>><?php echo BILL_CHARGE;?></option>
								   <option value="Shipping" <?php if($vtaxaddress=="shipping"){ echo "selected"; }?>><?php echo SHP_CHARGE;?></option>
							      </select></td>
								  </tr>
								<tr>
								  <td  height="40"><?php echo ENABLED;?><!--<span class="style1">*</span>--></td>
								  <td>:</td>
								  <td><LABEL for=taxratestatus>
                                                                          <?php
                                                                          $checked = ($vrateenable == 1)? "checked" : '';
                                                                          ?>
								    <input name="rate_enable"  type="checkbox" value="1"   <?php  echo $checked; ?> />
							      <?php echo YES_TR;?></LABEL></td>
								  </tr>
						  </table>

						</div>
						<div align="center"><a href="tax.php"><input type="submit" name="btn_back" value="<?php echo BACK;?>" class="button"></a>&nbsp;<input type="submit" name="submit" value="<?php echo UPDATE;?>" class="button" onClick="return validatetax();"></div>
						
				<!--content area end-->
		</td></tr></table>		
												
		  </div>
		   <SCRIPT  type="text/javascript">initCountry(); </SCRIPT>
		 				</form>
				
					<div align="left" class="clear_float"><img src="../images/clear.gif" alt="" width="1" height="1" /></div>
				<!--main body end -->
				</div>
				
				
<?php
	include "../includes/footer.php";
?>

<script language="javascript">
function validateFedex()
{
	if(document.fedexShipping.fedexaccno.value=="")
	{
	alert("<?php echo PLS_FAC;?>");
	document.fedexShipping.fedexaccno.focus();
	return false;
	}
	
	if(document.fedexShipping.fedexmeterno.value=="")
	{
	alert("<?php echo PLS_FNO;?>");
	document.fedexShipping.fedexmeterno.focus();
	return false;
	}
	return true;
	
}	


</script>

