<?php
ob_start();
/* 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_adminname"]) and $_SESSION["sess_adminname"]!= ""){
$adminname = $_SESSION["sess_adminname"];
}else{
header("Location:login.php");
exit;
}



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


if(isset($_POST['add']))
{

$planval =$_POST['chkPeriod'];

$messag='';

$select_cnt = "SELECT * FROM ".$tableprefix."sellerPlans WHERE plan_Period = '".$planval."' ";
$result_cnt = mysql_query($select_cnt) or die(mysql_error());

if(mysql_num_rows($result_cnt)>0)
{
$message = SEL_PLAN_EXIST." !!";
}


/*check whether there is any free plan exists in the database*/	
$noofFreePlans= mysql_num_rows(mysql_query("SELECT * FROM ".$tableprefix."sellerPlans WHERE plan_Period ='F'"));
/*check whether coupon with the same name exists in the database*/	
$select_coupon = "SELECT * FROM ".$tableprefix."sellerPlans WHERE LOWER(plan_Title) = ".GetSQLValueString(strtolower($_POST['txtplanTitle']),"text");
$result_coupon = mysql_query($select_coupon) or die(mysql_error());
if(mysql_num_rows($result_coupon)>1)
{
$message = PLAN_NAME_EXIST."!!";
}
elseif($fromDate>$toDate)
{
$message = ENDDATE_GREATER."!!";
}
elseif($fromDate>$toDate)
{
$message = ENDDATE_GREATER."!!";
}
elseif($noofFreePlans>0 && $_POST['chkPeriod']=='F'){
$message = MULTIPLE_PLANS;
}


if($message=='')
{
if($_POST['chkPeriod']=="F")
    {
    $planAmt = 0;
}
else
    {
    $planAmt = $_POST['txtPlanCharge'];
}
$insert_gift_coupon = "INSERT INTO ".$tableprefix."sellerPlans (plan_Title, plan_Description,plan_SubscriptionCharge, plan_Status,plan_Period)
VALUES("	.GetSQLValueString($_POST['txtplanTitle'],"text").","
.GetSQLValueString($_POST['txtPlanDescription'],"text").","
.GetSQLValueString($planAmt,"text").",'Y','".$_POST['chkPeriod']."')";

mysql_query($insert_gift_coupon) or die(mysql_error());

$insert_id = mysql_insert_id();									
header('Location:sellerPlanes.php?msg='.SELLER_PLAN_ADD_SUCCESS);
}						  


}

$planType = $_REQUEST['chkPeriod'];

if($planType=='')
    {
    $planType ='F';
}

if($planType=='M')
    {
        $checkPlanMonth='checked';
}
else
    {
    $checkPlanMonth='';
}

if($planType=='Y')
    {
        $checkPlanYear='checked';
}
else
    {
    $checkPlanYear='';
}

if($planType=='F')
    {
        $checkPlanFree='checked';
}
else
    {
    $checkPlanFree='';
}

?>
<script>
 $(document).ready(function()
{
     var curPlan= $('input[name=chkPeriod]:checked').val();
     if(curPlan=='F')
           {
               $('#spSubCharge').val("0");
               $('#planamt').hide();
               
           }
           else
               {
                    $('#planamt').show();
           }
});


function checkAddCouponForm()
{

var form_name = new Array('frmAddCoupon');		
var display_name = new Array('Coupon Name');
var count = 2;	

if(check_fields('frmAddCoupon','ccDiscount'))	
{
       var curPlan= $('input[name=chkPeriod]:checked').val();

      
       if(curPlan=='F')
           {
               $('#spSubCharge').val("0");
               $('#planamt').hide();
           }
           else
               {
                    $('#planamt').show();
           }
               

    if(trimSpaces(document.frmAddCoupon.txtplanTitle.value==""))
    {	 
        alert("<?php echo ENTER_PLAN_TITLE;?>");
        document.frmAddCoupon.txtplanTitle.focus();
        return false;		 	 
    }  	 

if(curPlan!='F'){
    if(!check_numeric(trimSpaces(document.frmAddCoupon.txtPlanCharge.value)))
    {
    	alert("<?php echo SUB_CHARGE_NUM;?>");
        document.frmAddCoupon.txtPlanCharge.focus();
        return false;
    }
   
        if(trimSpaces(document.frmAddCoupon.txtPlanCharge.value==""))
        {
            alert('<?php echo ENTER_SUB_CHARGE;?>(<?php echo $adminCurrency['currency_symbol'];?>).');
            document.frmAddCoupon.txtPlanCharge.focus();
            return false;
        }
   }
}
else
{
    return false;
}

}

function validatePlans()
{   
    var curPlan= $('input[name=chkPeriod]:checked').val();
    var dataString = {"planval":curPlan}; 
    $.ajax({
    url	        :"sellerPlans_validate.php",
    type		:"post",
    data		:dataString,
    dataType 	:"json",
    success		:function(data){
        if(data.planCount==1)
        {
            return confirm("<?php echo PLAN_EXIST;?>");
        }
        return false;
    }
    });
}

function showPlanAmount()
{
     var curPlan= $('input[name=chkPeriod]:checked').val();

     if(curPlan=='F')
           {
               $('#spSubCharge').val("0");
               $('#planamt').hide();
           }
           else
               {
                    $('#planamt').show();
           }
}

</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")
?>		


<div align="left" class="content_area">

<!--content area start -->

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

<div align="left">									

<form name="frmAddCoupon" action="#" method="POST" onSubmit="return checkAddCouponForm();">

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

<tr><td colspan="3">&nbsp;</td></tr>

<tr><td colspan="3" align="right"><span class="required">*</span>&nbsp;<?php echo FIELDS_ARE_MANDATORY;?></td></tr>
<tr><td width="25%" align="left" valign=top><?php echo PLAN_TITLE;?>&nbsp;<span class="required">*</span></td><td width="1%" >&nbsp;</td>
<td width="74%" align="left" valign=top><input class="textbox" type="text" name="txtplanTitle" id="txtplanTitle" value="<?php echo $_POST['txtplanTitle'];?>" size="25" maxlength="150"></td>
</tr>						
<tr>
<td align="left" valign=top><?php echo DESCRIPTION;?>&nbsp;</td>
<td >&nbsp;</td>
<td valign=top align="left">
<textarea name="txtPlanDescription" rows="8" cols="60" class="textbox"><?php echo $_POST['txtPlanDescription'];?></textarea>
</td>
</tr>
<tr><td width="26%" align="left" valign=top><?php echo PLAN_PERIOD;?> <span class="required">*</span></td><td width="2%" >&nbsp;</td>
<td width="72%" align="left" valign=top>
    <input type="radio" name="chkPeriod" value="M" checked="<?php echo $checkPlanMonth;?>"  onclick="showPlanAmount();" />

<?php echo MONTHLY;?> <input type="radio" name="chkPeriod" value="Y" checked="<?php echo $checkPlanYear;?>" onclick="showPlanAmount();"  /><?php echo YEARLY;?> <input type="radio" name="chkPeriod" value="F" checked="<?php echo $checkPlanFree;?>"  onclick="showPlanAmount();"/><?php echo FREE_TRIAL;?>
</td>
</tr>						
<tr id="planamt"><td width="25%" align="left" valign=top><?php echo SUBSCRIPTION_CHARGE;?> &nbsp;( <?php echo $adminCurrency['currency_symbol'];?> )<span class="required">*</span></td><td width="1%" >&nbsp;</td>
<td width="74%" align="left" valign=top><input class="textbox" type="text" name="txtPlanCharge" value="<?php echo $_POST['txtPlanCharge'];?>" size="25" maxlength="15"></td>
</tr>															
<tr>
<td colspan="3" align="center">

<input type="button" class="button" name="back" value="&nbsp;<?php echo BACK;?>&nbsp;" onClick="Javascript:history.go(-1)">
<input type="submit" value="<?php echo ADD_TEXT;?>" name="add" class="button" onClick="return validatePlans();">
</td>
</tr>
<tr><td colspan="3">&nbsp;</td></tr>
</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";

ob_end_flush();
?>