<?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";

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_artistid"]) and $_SESSION["sess_artistid"] != "") {
    $artistid = $_SESSION["sess_artistid"];
} else {
    header("Location:login.php");
    exit;
}

//===========================Select Flatrates from Settings Table =========================
$sql2 = "SELECT * FROM " . $tableprefix . "settings ";
$res2 = mysql_query($sql2);
if (mysql_num_rows($res2) != 0) {
    $rw = mysql_fetch_array($res2);
    $chek_flatrate = $rw["enable_flatrate"];
} // end if


if (isset($_POST["btnSaveChanges"])) {


    $message = "";


    $count_options = count($_POST['flatrate_name']);

    for ($i = 0; $i < $count_options; $i++) {
        $flatrate_name = mysql_real_escape_string($_POST['flatrate_name'][$i]);
        $rate_first_item = mysql_real_escape_string($_POST['rate_first_item'][$i]);
        $rate_additional_item = mysql_real_escape_string($_POST['rate_additional_item'][$i]);


        if ($flatrate_name != "" && ( $rate_first_item != "" || $rate_additional_item != "")) {
            $insertQ = "INSERT INTO " . $tableprefix . "flat_rate_seller
                                             (flat_rate_name,first_Item_charge,additional_Item_charge,seller_id)
                                             VALUES
                                             ('" . $flatrate_name . "','" . $rate_first_item . "','" . $rate_additional_item . "','" . $artistid . "')";

            mysql_query($insertQ);
        }// end if
    }


    foreach ($_POST as $key => $val) {
        $ary = array();

        if (strstr($key, '!rate_id')) {

            $ary = explode("!", $key);


            if ($ary[0] > 0) {


                $rateId = $ary[0];

                $flatrate_name = mysql_real_escape_string($_POST[$rateId . '!flatrate_name']);
                $rate_first_item = mysql_real_escape_string($_POST[$rateId . '!rate_first_item']);
                $rate_additional_item = mysql_real_escape_string($_POST[$rateId . '!rate_additional_item']);
                $sellerId = mysql_real_escape_string($_POST[$rateId . '!seller_id']);

                if ($sellerId > 0 && $rate_first_item > 0 && $flatrate_name != '') {

                    $chkQry = "select flat_rate_name from " . $tableprefix . "flat_rate_seller where flat_rate_seller_id='" . $rateId . "'";

                    $res = mysql_query($chkQry);

                    if (mysql_num_rows($res) == 0) {




                        $insertQ = "INSERT INTO " . $tableprefix . "flat_rate_seller
                                             (flat_rate_name,first_Item_charge,additional_Item_charge,seller_id)
                                             VALUES
                                             ('" . $flatrate_name . "','" . $rate_first_item . "','" . $rate_additional_item . "','" . $sellerId . "')";
                    } else {

                        $insertQ = "update  " . $tableprefix . "flat_rate_seller
                                             set flat_rate_name='" . $flatrate_name . "' ,
                                                 first_Item_charge ='" . $rate_first_item . "' ,
                                                 additional_Item_charge='" . $rate_additional_item . "' where
                                                flat_rate_seller_id='" . $rateId . "' and seller_id='" . $sellerId . "'";
                    }
                    mysql_query($insertQ);
                }// end if
                else if ($sellerId > 0 && $flatrate_name == '') {




                    $Q = "delete from  " . $tableprefix . "flat_rate_seller where flat_rate_seller_id='" . $rateId . "' and seller_id='" . $sellerId . "'";
                    mysql_query($Q);
                } else if ($sellerId > 0 && $rate_first_item == 0 && $flatrate_name != '') {

                    $message = VAL_MSG_FILL_FIELD;
                }
            }
        }
    }


    if ($message == '')
        $message = MSG_UPDT_SUC;
}


include "../includes/htmltop.php";
?>
<script language="javascript" type="text/javascript">
    $(document).ready(function(){

        $("#add_more_options").click(function() {

            var block = '<tr class="option_block"><td><input name="flatrate_name[]" type="text"  class="textarea2" size="35" maxlength="40" value=""></td><td><?php echo $adminCurrency['currency_symbol']; ?> &nbsp;<input name="rate_first_item[]" class="textarea2" type="text" id="rate_fistitem" size="10" maxlength="5" value=""></td><td><?php echo $adminCurrency['currency_symbol']; ?> &nbsp; <input name="rate_additional_item[]" type="text" class="textarea2" id="rate_additional" size="10" maxlength="5" value=""></td></tr>';
            $("#options_window").append(block);
        });

    });
</script>
<body>
<center>
    <div align="left" id="layout">
        <!--header area start -->
<?php
include_once("../includes/topheader_seller.php")
?>
        <!--header area end -->
        <div align="left" class="main_body">
            <!--main body start -->
<?php
include_once("../includes/artisttopmenu.php")
?>
            <div align="left" class="content_area">
                <!--content area start -->
                <div align="left" class="page_title"><?php echo TEXT_SHIP_SETT;?> </div>
                <div align="center" class="text_information grey_tr"><?php echo $message ?></div>
                <br>
                <div align="left">
                    <form name="frmProfile" action="<?php echo $_SERVER["PHP_SELF"] ?>" method="POST">
                        <input type="hidden" name="userid" value="<?php echo htmlentities($artistid); ?>">
                        <table cellpadding="0" cellspacing="0" align="center" class="manage_table" width="100%">
                            <tr>
                                <td colspan="3">&nbsp;</td>
                            </tr>
<?php
$defaultFlag = false;
if ($chek_flatrate == "Y") {

    $queryFlatRate = "SELECT * FROM " . $tableprefix . "flat_rate_seller WHERE seller_id ='" . $artistid . "'";
    $queryFlatRateQ = mysql_query($queryFlatRate);
    if (mysql_num_rows($queryFlatRateQ) == 0) {

        $defaultFlag = true;

        $queryFlatRate = "SELECT * FROM " . $tableprefix . "flat_rate_default";
        $queryFlatRateQ = mysql_query($queryFlatRate);
    }
    ?>
                                <tr><td colspan="3" align="right"><span class="required">*</span>&nbsp;<?php echo MSG_FIELD_MANDATORY; ?></td></tr>
                                <tr>
                                    <td colspan="3">

                                        <table width="100%" border="0"  class="manage_table" cellpadding="0" cellspacing="0" id="options_window">


                                            <tr class="form_header">
                                                <th align="left"><?php echo TEXT_DISP_NAME;?></th>
                                                <th align="left"><?php echo TEXT_FIRST_ITEM_RATE;?></th>
                                                <th align="left"><?php echo TEXT_ADD_ITEM_RATE;?></th>
                                            </tr>
    <?php if ($defaultFlag == false) { ?>

    <?php } ?>
    <?php
    while ($queryFlatRateObj = mysql_fetch_object($queryFlatRateQ)) {
        $flatrate_name = $queryFlatRateObj->flat_rate_name;
        $first_Item_charge = $queryFlatRateObj->first_Item_charge;
        $additional_Item_charge = $queryFlatRateObj->additional_Item_charge;
        $rateId = $queryFlatRateObj->flat_rate_seller_id;
        $sellerId = $queryFlatRateObj->seller_id;
        ?>


                                                <tr class="option_block">
                                                    <td><input name="<?php echo $rateId; ?>!flatrate_name" type="text"  class="textarea2" size="35" maxlength="50" value="<?php echo $flatrate_name; ?>"></td>
                                                    <td><?php echo $adminCurrency['currency_symbol']; ?> &nbsp;<input name="<?php echo $rateId; ?>!rate_first_item" class="textarea2" type="text" id="rate_firstitem" size="10" maxlength="8" value="<?php echo number_format($first_Item_charge, 2, '.', ''); ?>"></td>
                                                    <td><?php echo $adminCurrency['currency_symbol']; ?> &nbsp; <input name="<?php echo $rateId; ?>!rate_additional_item" type="text" class="textarea2" id="rate_additional" size="10" maxlength="8" value="<?php echo number_format($additional_Item_charge, 2, '.', ''); ?>"></td>
                                                    <td>&nbsp; <input name="<?php echo $rateId; ?>!seller_id" type="hidden" class="textarea2" value="<?php echo $sellerId; ?>"></td>
                                                    <td>&nbsp; <input name="<?php echo $rateId; ?>!rate_id" type="hidden" class="textarea2" value="<?php echo $rateId; ?>"></td>

                                                </tr>
                                    <?php
                                }
                                ?>

                                        </table>
                                    </td>
                                </tr>

                                <tr>
                                    <td colspan="4" style="font-weight: bold;" ><?php echo MSG_NOTE_SHIP;?> </td>
                                </tr>
                                <tr>
                                    <td colspan="4"><input type="button" name="add_more_options" value="<?php echo BTN_ADD_FLAT_RATE;?>" id="add_more_options"/></td>
                                </tr>

                                <tr><td colspan="3">&nbsp;</td></tr>
                                <tr>
                                    <td colspan="3" align="center">
                                        <input type="submit" value="<?php echo BTN_SAVE_CHANGE;?>" name="btnSaveChanges"  class="button" >
                                    </td>
                                </tr>
                                            <?php
                                        } else {
                                            ?>
                                <tr>
                                    <td height="24" colspan="3" align="left"> <?php echo MSG__FLATRATE;?>.</td>
                                </tr>
                                <tr>
                                    <td height="26" colspan="3" align="left"> <?php echo MSG_FLATRATE_SHIP;?>. </td>
                                </tr>

    <?php
}
?>
                        </table>
                    </form>
                </div>
                <!--content area end-->
            </div>
            <div align="left" class="clear_float"><img src="<?php echo $imagefolder; ?>/clear.gif" alt="" width="1" height="1" /></div>
            <!--main body end -->
        </div>
<?php
include "../includes/footer.php";
?>