<?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;
}
$sql = "SELECT * FROM " . $tableprefix . "settings ";
$result = mysql_query($sql);
if (mysql_num_rows($result) != 0) {
    $row = mysql_fetch_array($result);

    $tax_enable = $row['enable_tax'];
} // end result check 
//selecting settings for fedex shipping
$message = "";
//echo '<pre>';
//print_r($_POST);exit;
if (isset($_POST['submit'])) {
    $taxname = addslashes($_POST['taxname']);
    $countryname = $_POST['country'];
    $vtaxbase = $_POST['vtaxbase'];
    $taxrate = addslashes($_POST['taxrate']);
    $taxaddress = addslashes($_POST['taxaddress']);
    $taxenable = addslashes($_POST['rate_enable']);

    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($taxname)) {
        $message = ENT_TAXNAME. "<br>";
    }

    if (empty($countryname)) {
        $message.=SEL_COUNTRY . "<br>";
    }

    if ($countryname == "US") {
        if (empty($vstate)) {
            $message.=SEL_STATE . "<br>";
        }
    }

    if (empty($taxrate)) {
        $message.=ENTER_TAX_RATE. "<br>";
    }

    if (!empty($taxrate) && !is_numeric($taxrate)) {
        $message.=T_NUM . "<br>";
    }

    $select_sql = "select * from " . $tableprefix . "taxrate where taxname='" . addslashes($_POST["taxname"]) . "' and vCountry='" . $countryname . "' and vState='" . $vstate . "' ";
    $select_rs = mysql_query($select_sql) or die(mysql_error());
    if (mysql_num_rows($select_rs))
        $message = TNAME_EXIST;
    //echo $message;exit;

    if (empty($message)) {
        $sql_update = "insert into " . $tableprefix . "taxrate(taxname,vtaxbase,vCountry,vState,ntaxrate,vtaxaddress,rate_enable)
					values('" . $taxname . "','" . $vtaxbase . "','" . $countryname . "','" . $vstate . "','" . $taxrate . "','" . $taxaddress . "','" . $taxenable . "')";
        $result = mysql_query($sql_update);

        if (mysql_affected_rows() > 0) {
            $msg_succ =T_SAVED;
            $taxname = '';
            $countryname = '';
            $vstate = '';
            $vtaxbase = '';
            $taxrate = '';
            $taxaddress = '';
            $taxenable = '';
        }//end if
        header('location:tax.php?rghtMenu=rghtMenu1&msg=a');
        exit();
    }//end if
    else {
        $varOnload = "showStates('Country', 'Regions', '" . $countryname . "');";
    }
} else {
    $varOnload = '';
}//end else
$message = ($message != '') ? $message : $msg_succ;
$adminCurrency	= GetAdminCurrency();

$txtCurrency = $adminCurrency['currency_code'];
$deafultCountry  = $currencyBasedCountries[$txtCurrency];
include "../includes/htmltop.php";
?>
<style type="text/css">
    <!--
    .style1 {color: #FF0000}
    -->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="javascript">
    function showStates(countrylistId, statelistid, selectedState)
    {
        var sName;
        var sTotalCount=document.getElementById('editCount').value;
        var objstate= document.getElementById(statelistid);
        var objcountry = document.getElementById(countrylistId);
        objstate.options.length = 1;
        var statelist = g_statelist;

        var countryId = objcountry[objcountry.selectedIndex].value;
        countryId = countryId.split("_")[0];
        var countryCode = countryId.split("_")[1];

        var counter = 0;
        var states = new Array();



        for ( i=0; i < statelist.length; i++ )
        {
            if ( statelist[i].refID == countryId)
            {
                //copy matching states into an array
                states[counter] = { optText:statelist[i].title, optValue:statelist[i].id };
                counter++;
            }
        }
        // sort the states array
        states.sort(sortAsc);
        var optObjs = document.createElement('option');
        optObjs.text="All";
        optObjs.value=1;
        objstate.options.add(optObjs);
        // copy sorted options from array to states dropdown
        for (var j=0; j<states.length; j++)
        {
            var optObj = document.createElement('option');
            optObj.text = states[j].optText;
            optObj.value = states[j].optText;
            objstate.options.add(optObj);

            for(var ijk=0;ijk<sTotalCount;ijk++)
            {
                sName=document.getElementById('editCounty'+ijk).value;
                sName=sName.substr(0,sName.length-1);
                sName=sName.replace(/_/g, " ");

                if(optObj.text==sName)
                {//alert(optObj.selected);
                    optObj.selected = true;
                }//end if
            }//end for loop

            if(states[j].optValue == selectedState)
                optObj.selected = true;
        }

        if(counter > 0 && countryCode != 'US')
        {
            objstate.style.display='';
            objregionlabel.style.display = '';
        }

        else
        {
            objregionlabel.style.display = 'none';
            objstate.style.display='none';
        }
    }
    function validatetax()
    {        
        if(document.taxoptions.taxname.value=="")
        {
            return confirm("<?php echo PLS_TNAME;?>")
            document.taxoptions.taxname.focus();
            return false;
        }
        if(document.taxoptions.country.value=="")
        {
        	return confirm("<?php echo PLS_COUNTRY;?>")
            document.taxoptions.Country.focus();
            return false;
        }
        if(document.taxoptions.country.value == "US")    {
            if(document.getElementById('stateSelect').value=="" )
            {
            	return confirm("<?php echo PLS_SELSTATE;?>")
                return false;
            }
        }
        if(document.taxoptions.taxrate.value=="")
        {
        	return confirm("<?php echo PLS_ENT_TRATE;?>")
            document.taxoptions.taxrate.focus();
            return false;
        }
        else if(isNaN(document.taxoptions.taxrate.value))
        {
        	return confirm("<?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 $deafultCountry;?>" name="countryDefault" id="countryDefault">
                <input type="hidden" value=""   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 ADD_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%" align="left"><input type="text" name="taxname" class="textarea" id="taxname" value="<?php echo $taxname; ?>"></td>
                                        </tr>
                                        <tr>
                                            <td  height="40"><?php echo BASED_ON;?> <span class="style1">*</span></td>
                                            <td></td>
                                            <td align="left"><select name="vtaxbase" class="textarea" 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 align="left">  <select id='countrySelect' class='textarea2' name='country' onchange='updateState(this.id,"tax")'> </select></td>
                                        </tr>
                                        <tr>
                                            <td  height="40" align="left" valign="top"><?php echo STATE;?></td>
                                            <td align="left" valign="top"></td>
                                            <td align="left" valign="top">
<?php
if (is_array($_POST['state'])) {
    foreach ($_POST['state'] as $key => $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($_POST['state']); ?>">
                                                <select name='state[]' size="20" id='stateSelect' multiple>
                                                </select>
                                                <SCRIPT  type="text/javascript">initCountry(); </SCRIPT>

                                            </td>
                                        </tr>
                                        <tr>
                                            <td  height="40"><?php echo TAX_RATE;?><span class="style1">*</span></td>
                                            <td></td>
                                            <td align="left"><input name="taxrate" class="textarea2" type="text" id="taxrate" size="10" maxlength="5" value="<?php echo $taxrate; ?>">
                                                %</td>
                                        </tr>
                                        <tr>
                                            <td  height="40"><?php echo TAXD_ADD;?><span class="style1">*</span></td>
                                            <td></td>
                                            <td align="left"><select name="taxaddress" class="textarea2">
                                                    <option value="Billing"><?php echo BILL_CHARGE;?></option>
                                                    <option value="Shipping"><?php echo SHP_CHARGE;?></option>
                                                </select></td>
                                        </tr>
                                        <tr>
                                          <td  height="40"><?php echo ENABLED;?><!--<span class="style1">*</span>--></td>
                                            <td></td>
                                            <td align="left">
                                                <LABEL for=taxratestatus>
                                                    <input name="rate_enable" type="checkbox" value="1"  id="rate_enable" checked>
                                                    <?php echo YES_TR;?>
                                                </LABEL>
                                            </td>
                                        </tr>
                                    </table>

                                </div>
                                <div align="center"><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";
?>

