<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Santhosh Thomas<santhosh.thomas@armiasystems.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;
}
//selecting settings for fedex shipping
$message="";
if(isset($_POST['submit'])) {
    (isset($_POST['enable_flatrate']))?$txtflatShipping=$_POST['enable_flatrate']:$txtflatShipping='N';
    (isset($_POST['enable_free_shipping']))?$txtfreeShipping=$_POST['enable_free_shipping']:$txtfreeShipping='N';
    $default_flatrate=trim($_POST['rate_fistitem']);
    $flatrate_additional=trim($_POST['rate_additional']);
    
    $freeshipAmt  = trim($_POST['free_shipping_amount']);

    $flatrate_name=trim($_POST['flatrate_name']);
    
    
    mysql_query("UPDATE ".$tableprefix."settings
				    SET  enable_flatrate	= ".GetSQLValueString($txtflatShipping,"text").",
                                         enable_free_shipping    = ".GetSQLValueString($txtfreeShipping,"text").",
                                          min_order_amount        = ".GetSQLValueString($freeshipAmt,"text")."");
   
    if($txtflatShipping=="Y") { // check flat rate shipping enabled or not
    	//print_r($_POST['flatrate_name']);
    	$resSel = mysql_query("SELECT * FROM ".$tableprefix."flat_rate_default");
    	$rowSel = mysql_fetch_object($resSel);
    	if((mysql_num_rows($resSel) == 0 || count($_POST['flatrate_name']) >= 2 || $rowSel->flat_rate_name != $_POST['flatrate_name'][0] || $rowSel->first_Item_charge != $_POST['rate_first_item'][0] || $rowSel->additional_Item_charge  != $_POST['rate_additional_item'][0]) && $_POST['flatrate_name'][0] != ""){
    		$querydel = "DELETE FROM ".$tableprefix."flat_rate_default";
    		mysql_query($querydel);
    		$count_options = count($_POST['flatrate_name']);
    		$saveFlag = 0;
    		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_default
	                                             (flat_rate_name,first_Item_charge,additional_Item_charge)
	                                             VALUES
	                                             ('".$flatrate_name."','".$rate_first_item."','".$rate_additional_item."')";
    		
    				mysql_query($insertQ);
    				$saveFlag = 1;
    		
    			}// end if
    		}
    	}
    	//print_r($_POST['flatrate_name']);
    	if($_POST['flatrate_name'][0] == ""){
    		$message = FLATRATE_MANDATORY;
    	}
        if($saveFlag==1){
            $success_message = CHANGES_SAVED_SUCCESSFULLY;
        }

    } // end flat rate row check
    
}
$message=(!empty($message))?$message:$message_succ;
include "../includes/htmltop.php";


$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"]);
    $txtfreeShipping = $row["enable_free_shipping"];
    $freeshipAmt = $row["min_order_amount"];
}

if($txtEnableShipping == "Y") {
    $shipping_checked = "checked";
}
else {
    $shipping_checked = "";
}
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";
}

if($txtfreeShipping == "Y") {
    $free_checked = "checked";
    $free_block = "";
}
else {
    $free_checked = "";
    $free_block = "none";
}
?>
<style type="text/css">
    <!--
    .style1 {color: #FF0000}
    -->
</style>
<script language="javascript">
    function checkFedexshipping()
    {


        if(document.canadapost.enable_fedexshipping.checked == true)
        {

            document.getElementById("FedexBlock").style.display = "";

        }
        else
        {

            document.getElementById("FedexBlock").style.display = "none";

        }
    }
    function checkFlatrateshipping()
    {
        if(document.canadapost.enable_flatrate.checked == true)
        {
            document.getElementById("FlatBlock").style.display = "";
        }
        else
        {
            document.getElementById("FlatBlock").style.display = "none";
        }
    }
    function checkFreeshipping()
    {
        if(document.canadapost.enable_free_shipping.checked == true)
        {
            document.getElementById("FreeShipping").style.display = "";
        }
        else
        {
            document.getElementById("FreeShipping").style.display = "none";
        }
    }

    $(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);
        });

    });
function validateValues()
  {
      var chks = document.getElementsByName('rate_first_item[]');
       var name = document.getElementsByName('flatrate_name[]');
      var add_item = document.getElementsByName('rate_additional_item[]');
      var shippingAmt = document.canadapost.free_shipping_amount.value;
     
      
       if(document.canadapost.enable_free_shipping.checked == true)
        {
           if(shippingAmt==""){
                alert("Please enter the minimum order amount");
                return false;
            }
            if(isNaN(shippingAmt))
            {
                 alert("Please enter a numeric value for minimum order amount");
                 return false;
            }
             if(shippingAmt=="" || shippingAmt<=0)
            {
                 alert("order amount should be greater than zero");
                 return false;
            }
            
        }
      
       if(document.canadapost.enable_flatrate.checked == true)
        {
            for (var i = 0; i < chks.length; i++)
              {
              /*if (chks[i].value=="" || chks[i].value<=0)
                  {
                  alert("Please enter a valid amount for flat rate");
                  chks[i].focus();
                  return false;
                  }
              }*/
            	if(name[i].value=='' && add_item[i].value!=''  && chks[i].value!=''){

                    alert("Please enter a name for flat rate");
                    name[i].focus();
                    return false;
  
                }
            	else if(name[i].value!='' && add_item[i].value==''  && chks[i].value==''){

                    /* if (chks[i].value=="" || chks[i].value<=0)
                     { */
                     alert("Please enter a valid amount for flat rate");
                     name[i].focus();
                     return false;
                    /* } */
                 }            	
            	else if(name[i].value!='' && add_item[i].value!=''  && chks[i].value==''){

                    /* if (chks[i].value=="" || chks[i].value<=0)
                     { */
                     alert("Please enter a valid amount for flat rate");
                     chks[i].focus();
                     return false;
                    /* } */
                 }
            	else if(name[i].value!='' && add_item[i].value==''  && chks[i].value!=''){

                    /* if (chks[i].value=="" || chks[i].value<=0)
                     { */
                     alert("Please enter a valid amount for flat rate");
                     add_item[i].focus();
                     return false;
                    /* } */
                 }
            	
              if((name[i].value!='' || add_item[i].value!='')  && (chks[i].value=="" || chks[i].value<0) ){

                     /* if (chks[i].value=="" || chks[i].value<=0)
                      { */
                      alert("Please enter a valid amount for flat rate");
                      chks[i].focus();
                      return false;
                     /* } */
                  }else if(chks[i].value!=""){
                        //if(!IsNumeric(chks[i].value))
                        if(isNaN(chks[i].value))
                         {
                              alert("Please enter a numeric value for flat rate");
                               chks[i].focus();
                              return false;
                         }
                  }
              }
        return true;
        }else {
            return true;
        }
  }
</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="canadapost" onSubmit="return validateFedex();">
                    <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_GENERAL_SHIPPING_SETTINGS; ?></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>
                                    <div align="left">

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

                                            <tr>
                                                <td colspan="3" align="center"><span class="required">*</span><?php echo CREATE_ACCOUNTS; ?></td>
                                            </tr>
                                            <tr>
                                                <td align="right" valign="middle">&nbsp;</td>
                                                <td align="center" valign="middle">&nbsp;</td>
                                                <td colspan="2" align="left" valign="middle"><input name="enable_free_shipping" type="checkbox" id="enable_free_shipping" value="Y"  <?php echo $free_checked;?> onClick="checkFreeshipping();">
							        <?php echo SELECT_CHECKBOX_FREE_SHIPPING; ?></td>
                                            </tr>
                                            
                                             <tr id="FreeShipping" style="display:<?php echo $free_block;?>">
                                                <td align="right" valign="middle">&nbsp;</td>
                                                <td align="center" valign="middle">&nbsp;</td>
                                                <td colspan="2" align="left" valign="middle">
                                                    <b><?php echo FREE_SHIIPPING_MINIMUM_AMOUNT; ?></b> &nbsp;
                                                    <?php echo $adminCurrency['currency_symbol'];?> &nbsp;<input name="free_shipping_amount" class="textarea2" type="text" id="free_shipping_amount" value="<?php echo $freeshipAmt; ?>" >
							 <span class="help"><a href="#"
                                                                             title="<?php echo FREESHIPP_TOOLTIP?>"><img
                                                                src="../images/question_mark.jpg" border="0" height="15">    </td>
                                            </tr>

                                            <tr>
                                                <td align="right" valign="middle">&nbsp;</td>
                                                <td align="center" valign="middle">&nbsp;</td>
                                                <td colspan="2" align="left" valign="middle"><input name="enable_flatrate" type="checkbox" id="enable_flatrate" value="Y"  <?php echo $flat_checked;?> onClick="checkFlatrateshipping();">
							        <?php echo SELECT_CHECKBOX; ?></td>
                                            </tr>
                                            <tr>
                                                <td align="right" valign="middle">&nbsp;</td>
                                                <td align="center" valign="middle">&nbsp;</td>
                                                <td colspan="2" align="left" valign="middle">&nbsp;
                                                    <div id="FlatBlock" style="display:<?php echo $flatrate_block;?>">
                                                        <table width="100%" border="0"  class="manage_table" cellpadding="0" cellspacing="0" id="options_window">
                                                            <tr class="form_header">
                                                                <th><?php echo DISPLAY_NAME; ?></th>
                                                                <th><?php echo FIRST_ITEM_RATE; ?></th>
                                                                <th><?php echo ADDITIONAL_ITEM_RATE; ?></th>
                                                            </tr>
                                                            <?php
                                                            $queryFlatRate = "SELECT * FROM ".$tableprefix."flat_rate_default";
                                                            $queryFlatRateQ = mysql_query($queryFlatRate);
                                                            while($queryFlatRateObj = mysql_fetch_object($queryFlatRateQ)) {
                                                                $flatrate_name  = $queryFlatRateObj->flat_rate_name;
                                                                                                                    $default_flatrate =$queryFlatRateObj->first_Item_charge;
                                                                $flatrate_additional =$queryFlatRateObj->additional_Item_charge;
                                                            ?>
                                                            <tr class="option_block">
                                                                <td><input name="flatrate_name[]" type="text"  class="textarea2" size="35" maxlength="50" value="<?php echo stripslashes($flatrate_name);?>"></td>
                                                                <td><?php echo $adminCurrency['currency_symbol'];?> &nbsp;<input name="rate_first_item[]" class="textarea2" type="text" id="rate_firstitem" size="10" maxlength="8" value="<?php echo number_format($default_flatrate, 2, '.', '');?>"></td>
                                                                <td><?php echo $adminCurrency['currency_symbol'];?> &nbsp; <input name="rate_additional_item[]" type="text" class="textarea2" id="rate_additional" size="10" maxlength="8" value="<?php echo number_format($flatrate_additional, 2, '.', ''); ?>"></td>
                                                            </tr>
    <?php
}
?>

                                                        </table>
                                                    </div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="4"><?php echo NOTE;?>:<?php echo EMPTY_THE_ROW_AND_CLICK_UPDATE;?></td>
                                            </tr>
                                            <tr>
                                                <td colspan="4"><input type="button" name="add_more_options" value="<?php echo BTN_ADD_MORE_FLAT_RATE_OPTIONS; ?>" id="add_more_options"/></td>
                                            </tr>
                                            <tr>
                                                <td colspan="4"><div align="center"><input type="submit" name="submit" value="<?php echo BTN_UPDATE; ?>" class="button" onclick="return validateValues()"></div></td>
                                            </tr>
                                        </table>

                                    </div>


                                    <!--content area end-->

                                    <!--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 validateFedex()
                {

                    /*if(document.canadapost.merchatId.value=="")
                    {
                        alert("Please enter canadapost merchantid");
                        document.canadapost.merchatId.focus();
                        return false;
                    }*/


                    return true;

                }

            </script>

