<?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.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
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;
}

$select_shipping_settings = "SELECT * FROM " . $tableprefix . "shipping_settings";
$result_shipping_settings = mysql_query($select_shipping_settings);

if (mysql_num_rows($result_shipping_settings) > 0) {
    $rs_ship = mysql_fetch_array($result_shipping_settings);
    $ups_weight = $rs_ship['nShippingweight'];
    $ups_rate = $rs_ship['ups_rate'];
    $ups_container = $rs_ship['container_type'];
    $ups_type = $rs_ship['address_quote_method'];
} // end if 




include "../includes/htmltop.php";

$message = "";
if (isset($_POST['submit'])) {


    $ups_weight = $_POST['nweight'];
    $ups_rate = $_POST['ups_rate'];
    $ups_container = $_POST['container_type'];
    $ups_type = $_POST['res_type'];
    (isset($_POST['enable_shipping'])) ? $txtEnableShipping = $_POST['enable_shipping'] : $txtEnableShipping = 'N';
    if (empty($ups_weight)) {
    	$message = PLS_MAX;
    }
    if ($message == "") { // check no error
    $update_settings = "UPDATE " . $tableprefix . "shipping_settings SET nShippingweight='" . $ups_weight . "',ups_rate='" . $ups_rate . "',
							container_type='" . $ups_container . "',address_quote_method='" . $ups_type . "' ";
    mysql_query($update_settings) or die(mysql_error());

    mysql_query("UPDATE " . $tableprefix . "settings 
	                SET enable_shipping = " . GetSQLValueString($txtEnableShipping, "text"));

    $success_message = MSG_SETTINGS;
    }
}


$sql = "SELECT * FROM " . $tableprefix . "settings ";
$result = mysql_query($sql);
if (mysql_num_rows($result) != 0) {
    $row = mysql_fetch_array($result);

    $txtEnableShipping = $row["enable_shipping"];
    $txtfedex = $row["enable_fedexshipping"];
    $txtusps = $row["enable_uspsshipping"];
    $chkcanadapost = $row["enable_canadapost"];
    $chkaustraliapost = $row["enable_australiapost"];

    $txtflatShipping = $row["enable_flatrate"];
    $default_flatrate = $row["default_flatrate"];
    $flatrate_additional = $row["additional_flatrate"];
    $flatrate_name = stripslashes($row["flatrate_displayname"]);
}

if ($txtEnableShipping == "Y") {
    $shipping_checked = "checked";
    $ups_shipping_block = "visible";
} else {
    $shipping_checked = "";
    $ups_shipping_block = "none";
}
if ($txtMembership == "Y") {
    $Membership_checked = "checked";
}

if ($chkcanadapost == "Y") {
    $canadapost_checked = "checked";
} else {
    $canadapost_checked = "";
}
if ($chkaustraliapost == "Y") {
    $australiapost_checked = "checked";
} else {
    $australiapost_checked = '';
}

if ($txtusps == "Y") {
    $shipping_usps_checked = "checked";
} else {
    $shipping_usps_checked = "";
}
if ($txtEnableYourPay == "Y") {
    $yourpay = "checked";
    $yourpay_block = "";
} else {
    $yourpay = "";
    $yourpay_block = "none";
}
if ($txtfedex != "N") {
    $fedex_checked = "checked";
    $fedex_shipping_block = "";
} else {
    $fedex_checked = "";
    $fedex_shipping_block = "none";
}


if ($txtflatShipping == "Y") {
    $flat_checked = "checked";
    $flatrate_block = "";
} else {
    $flat_checked = "";
    $flatrate_block = "none";
}
?>
<script language="javascript">
    function checkUpsshipping()
    {
        if(document.frmSettings.enable_shipping.checked == true)
        {
            document.getElementById("upsBlock").style.display = "";
        }
        else
        {
            document.getElementById("upsBlock").style.display = "none";
        }
    }
</script>
<body>
<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="frmSettings" onSubmit="return validate();">

                <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/shippingmenu.php") ?></td>
                        </tr>
                        <tr>
                            <td>

                                <div align="left" class="page_title"><?php echo EDIT_USPSHPNG;?></div>
                                <div align="center" class="text_information grey_tr"><?php echo $message ?></div>
                                <div align="center" class="text_information_success grey_tr">
                                                     <?php echo $success_message; ?>
                                </div>
                                <br>

                                <div align="left">				
                                    <table width="100%" border="0" class="manage_table" cellpadding="3" cellspacing="3">
                                        <tr>
                                            <td width="43%"  align="right" valign="middle"><?php echo ENABLE_SHPN;?></td>
                                            <td width="3%" align="left" valign="middle">:</td>
                                            <td  width="54%" align="left" valign="middle"><input type="checkbox" name="enable_shipping" value="Y" <?php echo $shipping_checked ?>  onClick="checkUpsshipping();"/>
                                                <?php echo ENABLE_USPSS;?>									</td>
                                        </tr>
                                        <tr><td colspan="3" align="left">
                                                <div id="upsBlock" style="display:<?php echo $ups_shipping_block; ?>">
                                                    <table align="center" border="0">						
                                                        <tr>
                                                            <td width="43%" align="right"><?php echo MAX_WEIGHT;?> <span class="required">* </span></td>
                                                            <td width="3%">:</td>
                                                            <td width="54%"><input type='text' name='nweight' id ='nweight' value='<?php echo $ups_weight; ?>' class='textarea' size='21'></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right"><?php echo UPSR;?> <span class="required">* </span></td>
                                                            <td>:</td>
                                                            <td><select class="textarea" name="ups_rate">
                                                                    <?php
                                                                    foreach ($rate_array as $r => $rates) {
                                                                        ?>
                                                                        <option value="<?php echo $r; ?>" <?php
                                                                    if ($ups_rate == $r) {
                                                                        echo "Selected";
                                                                    }
                                                                        ?>><?php echo $rates; ?></option>
                                                                                <?php
                                                                            } // end for each 
                                                                            ?>
                                                                </select></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right"><?php echo CONT_TYP;?><span class="required">* </span></td>
                                                            <td>:</td>
                                                            <td><select class="textarea" name="container_type">
                                                                    <?php
                                                                    foreach ($container_type as $cp => $types) {
                                                                        ?>
                                                                        <option value="<?php echo $cp; ?>" <?php
                                                                    if ($ups_container == $cp) {
                                                                        echo "Selected";
                                                                    }
                                                                        ?>><?php echo $types; ?></option>
                                                                                <?php
                                                                            } // end for each 
                                                                            ?>
                                                                </select></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right"><?php echo ADDRESS_QUOTE;?><span class="required">* </span></td>
                                                            <td>:</td>
                                                            <td><select class="textarea" name="res_type">
                                                                    <?php
                                                                    foreach ($res_type as $re => $res) {
                                                                        ?>
                                                                        <option value="<?php echo $re; ?>" <?php
                                                                    if ($ups_type == $re) {
                                                                        echo "Selected";
                                                                    }
                                                                        ?>><?php echo $res; ?></option>
                                                                                <?php
                                                                            } // end for each 
                                                                            ?>
                                                                </select></td>
                                                        </tr>
                                                    </table>
                                                </div>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td><input type="submit" name="submit" value="<?php echo BTN_UPDATE; ?>" class="button"></td>
                                        </tr>
                                    </table>

                                </div>
                                <div align="center"></div>
                                <!--content area end-->
                            </td></tr>

                    </table>	

                </div>

            </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 validate()
            {
                if(document.frmSettings.nweight.value=="")
                {
                    alert("<?php echo PLS_MAX;?>");
                    document.frmSettings.nweight.focus();
                    return false;
                }
                else if(isNaN(document.frmSettings.nweight.value))
                {
                	alert("<?php echo SHPNG_INTEGER;?>");
                    document.frmSettings.nweight.focus();
                    return false;
                }	
                else
                    return true;

            }
        </script>

