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

if ($_POST['postback1'] == "CS") {//for changing status
    $id = $_POST['uid'];
    $changeto = $_POST['changeto'];
    $sqlcs = "UPDATE " . $tableprefix . "affiliates  SET deleted = '$changeto' where affiliate_id =$id  ";
    //echo $sqlcs;
    mysql_query($sqlcs);
}



if (isset($_GET["txtSearch"]) and $_GET["txtSearch"] != "") {
    $txtSearch = $_GET["txtSearch"];
    $cmbSearchType = $_GET["cmbSearchType"];
} else if (isset($_POST["txtSearch"]) and $_POST["txtSearch"] != "") {
    $txtSearch = $_POST["txtSearch"];
    $cmbSearchType = $_POST["cmbSearchType"];
}

if (isset($_GET["msg"]) and $_GET["msg"] != "") {
    $msg = $_GET["msg"];
    switch ($msg) {
        case "deleted": $message = MSG_USER_DELETED;
            break;
        default:
            ;
    } // switch
}

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

    header("Location:" . $_SERVER['PHP_SELF'] . "?page=" . $_POST['gotoPage']);
}

$qryopt = "";
if ($txtSearch != "" && $txtSearch != "all") {
    if ($cmbSearchType == "affiliatename") {
        $txtkwd = str_replace(" ", "", $txtSearch);
        $qryopt .= "  WHERE  s.first_name like '%" . addslashes($txtkwd) . "%' OR s.last_name like 
			'%" . addslashes($txtkwd) . "%' OR CONCAT(s.first_name,s.last_name) like '%" . addslashes($txtkwd) . "%'";
    } else if ($cmbSearchType == "dateofregn") {
        $dt = $txtSearch;
        $arrdt = explode("/", $dt);
        $day = $arrdt[1];
        $month = $arrdt[0];
        $year = $arrdt[2];
        $dt = $year . "-" . $month . "-" . $day;
        $qryopt .= "  WHERE  s.date_registered like '" . addslashes($dt) . "%'";
    } else if ($cmbSearchType == "email") {
        $qryopt .= "  WHERE  s.email like '" . addslashes($txtSearch) . "%'";
    }
}

$sql = " SELECT s.*,CASE WHEN deleted = 'Y' THEN 'No' ELSE 'Yes' END AS delete_status,
	         CASE WHEN vapproved = 'Y' THEN 'Approved' ELSE 'Not Approved' END AS afiliates_approved
		     FROM " . $tableprefix . "affiliates s " . $qryopt . "  order by date_registered DESC ";



if (!(isset($pagenum))) {
    $pagenum = 1;
}

$limit = getSettingsValue('npagination_limit');

$page = isset($_GET['page']) ? trim($_GET['page']) : 1;


$page = $_GET['page'];

if (empty($page)) {
    $page = 1;
}

$result = mysql_query($sql) or die(mysql_error());

$totalrows = mysql_num_rows($result);

$limitvalue = ($page - 1) * $limit;

$query_string = " LIMIT $limitvalue, $limit";

$sql = $sql . $query_string;

$rs = mysql_query($sql) or die(mysql_error());

$numofpages = ceil($totalrows / $limit);

include "../includes/htmltop.php";
?>
<link rel="stylesheet" href="<?php echo SITE_URL; ?>/style/jquery-ui.css">
<script src="<?php echo SITE_URL; ?>/js/jquery-ui.js"></script>
<script language="JavaScript" type="text/JavaScript">
     $(document).ready(function () {
        $("#datepicker").datepicker({
                dateFormat: 'mm/dd/yy',
                maxDate: '0'
       
         });
      
    $("#cmbSearchType").on("change",function(){
        if(this.value=='dateofregn'){
             $("#textSelector").hide();
             $("#dateSelector").show();
             $("#txtSearch").attr("disabled","disabled");
             $("#datepicker").removeAttr("disabled");
            
        }else{
             $("#textSelector").show();
             $("#dateSelector").hide();
             $("#datepicker").attr("disabled","disabled");
             $("#txtSearch").removeAttr("disabled");
             
        }
        
        
    });
});
    function clickSearch()
    {
        document.frmaffiliates.submit();
    }
    function ChangeStatus(id,status){

        var frm = document.frmAffiliates;
        if(status == "Y"){
            changeto = "<?php echo DISABLE; ?>";
        }else{
            changeto = "<?php echo ENABLE; ?>";
        }
        if(confirm("<?php echo ARE_YOU_SURE_YOU_WANT_TO;?> "+ changeto +" <?php echo THIS_AFFILIATE;?>")){
            frm.postback1.value="CS";
            frm.uid.value=id;
            frm.changeto.value=status;
            frm.submit();
        }

    }
</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 MANAGE_AFFILIATES;?></div>
                <div align="center" class="text_information grey_tr"><?php echo $message ?></div>
                <br>

                <div align="left">

                    <form action="<?php echo  $_SERVER["PHP_SELF"] ?>" method="get" name="frmaffiliates">
                        <?php
                                $dateblock = "none";
                                if($cmbSearchType!= "dateofregn"){
                                           $txtBlock = "display";
                                           $dateblock = "none";
                                           $txtSearch1 = $txtSearch; 
                                           $disabled1   = "";
                                           $disabled2   = "disabled=disabled";
                                 } else{
                                           $txtBlock = "none";
                                           $dateblock = "display";
                                           $txtSearch2 = $txtSearch;
                                           $disabled1   = "disabled=disabled";
                                           $disabled2   = "";
                                }
                                                           
                         ?>

                        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_top">
                            <tr>
                                <td width="55%" align="left" valign="middle" class="">
                                    <select class="textarea" id="cmbSearchType" name="cmbSearchType">
                                        <option value="all"><?php echo ALL_AFFILIATES;?></option>
                                        <option value="affiliatename"  <?php if ($cmbSearchType == "affiliatename" || $cmbSearchType == "") {
                echo("selected");
            } ?>><?php echo AFFILIATE_NAME;?></option>
                                        <option value="email"  <?php if ($cmbSearchType == "email") {
                echo("selected");
            } ?>><?php echo EMAIL;?></option>
                                        <option value="dateofregn"  <?php if ($cmbSearchType == "dateofregn") {
                echo("selected");
            } ?>><?php echo DATE_OF_REGN;?> <!--(<?php echo MM."/".DD."/".YYYY;?>)--></option>
                                    </select>
                                    <span id="textSelector" style="display:<?php echo $txtBlock; ?>"  >
                                        <input type="text" <?php echo $disabled1; ?> name="txtSearch" id="txtSearch" size="20" maxlength="50" value="<?php echo(htmlentities(stripslashes($txtSearch1))); ?>"  onKeyPress="if(window.event.keyCode == '13'){ return false; }"  class="textarea">
                                   </span>
                                    <span id="dateSelector" style="display:<?php echo $dateblock; ?>" >
                                        <input type="text" <?php echo $disabled2; ?> name="txtSearch" id="datepicker" size="20" maxlength="50" value="<?php echo(htmlentities($txtSearch2)); ?>"   class="textarea">
                                    </span>
                                     <a onClick="javascript:clickSearch();" class="search_button"><?php echo BTN_SEARCH; ?></a></td>

                                </td>

                            </tr>
                        </table>
                    </form>	
                </div>

                <div align="left">

                    <form action="" method="post" name="frmAffiliates">

                        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_table">
                            <tr class="form_header">
                                <th width="6%" align="center" valign="middle">#</th>
                                <th width="16%" align="left" valign="middle"><?php echo LOGIN_NAME;?></th>
                                <th width="20%" align="left" valign="middle"><?php echo NAME;?></th>
                                <th width="17%" align="left" valign="middle"><?php echo EMAIL;?></th>
                                <th width="23%" align="center" valign="middle"><?php echo REGISTERED;?>(<?php echo MM."/".DD."/".YYYY;?>)</th>
                                <th width="9%" align="center" valign="middle"><?php echo ACTIVE;?></th>
                                <th width="9%" align="center" valign="middle"><?php echo STATUS;?></th>
                            </tr>

                            <input name="postback1" type="hidden" id="postback1">
                            <input name="uid" type="hidden" id="uid">
                            <input name="changeto" type="hidden" id="changeto">

<?php
$i = 1;

if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_array($rs)) {
        $link = "<a class=links href='editaffiliate.php?affiliateid=" . $row["affiliate_id"] . "' class=\"smalllink\">";
        if ($row["deleted"] == "N") {
            //$statusimg = "cross.gif";
            $changestatuslink = "ChangeStatus(" . $row["affiliate_id"] . ",'Y');";
        } else {
            //$statusimg = "tick.gif";
            $changestatuslink = "ChangeStatus(" . $row["affiliate_id"] . ",'N');";
        }
        $link1 = "<a class=links href=\"#\" class='smalllink' onclick=\"$changestatuslink\">";
        switch ($row["afiliates_approved"]){
        	case 'Approved':$st=APPROVED;
        	break;
        	case 'Not Approved' :$st=NOT_APPROVED;
        	break;
        }
        switch ($row["delete_status"]){
        	case 'Yes':$st1=YES;
        	break;
        	case 'No' :$st1=NO1;
        	break;
        }
        
        ?>






                                    <tr>
                                        <td align="center" valign="middle"><?php echo (($page * $limit) + $i - $limit) ?></td>
                                        <td align="left" valign="middle"><?php echo $link . stripslashes($row["affiliate_name"]) . "</a>"; ?></td>
                                        <td align="left" valign="middle"><?php echo $link . stripslashes($row["first_name"]) . " " . stripslashes($row["last_name"]) . "</a>"; ?></td>
                                        <td align="left" valign="middle"><?php echo $link . htmlentities($row["email"]) . "</a>"; ?></td>
                                        <td align="center" valign="middle"><?php echo $link . dateFormat($row["date_registered"], "Y-m-d", "m/d/Y") . "</a>"; ?></td>
                                        <td align="center" valign="middle"><?php echo $link1 . $st1 . "</a>"; ?></td>
                                        <td align="center" valign="middle"><?php echo $st; ?></td>
                                    </tr>
        <?php
        $i++;
    }
} else {
    ?>										

                                <tr>
                                    <td align="center" valign="middle" colspan="7" class="text_information"><?php echo NO_RECORDS_FOUND;?> !!</td>
                                </tr>




                                <?php
                            }

                            if ($page > 1) {
                                $pageprev = $page - 1;

                                $previous_page = "<a href=" . $_SERVER['PHP_SELF'] . "?rghtMenu=" . $_GET["rghtMenu"] . "&page=$pageprev alt='Previous' border='0' class='small_gray_button'>".TEXT_PREV."</a>";
                            }


                            if ($page < $numofpages) {
                                $pagenext = ($page + 1);

                                $next = "<a href=" . $_SERVER['PHP_SELF'] . "?rghtMenu=" . $_GET["rghtMenu"] . "&page=$pagenext alt='Next' border='0' class='small_gray_button'>Next</a>";
                            }
                            ?>
                            <tr>
                                <td class="page_nos" colspan="5" align="center">&nbsp;</td>
                                <td align="right" valign="middle" class="page_nos">&nbsp;</td>
                                <td align="left" class="btn_gotbl" valign="middle">&nbsp;</td>
                            </tr>							  <?php
                            if ($page < $numofpages || $page > 1) {
                                ?>
                                <tr>
                                    <td colspan="7" align="center">
										<table width="100%" border="0" cellspacing="10" cellpadding="0">
											<tr align="left" valign="middle">
												<td width="21%" align="right"><?php echo $previous_page ?></td>
												<td width="23%" align="right">
													<?php echo PAGE."(".S.")";?>
													<?php echo $page ?>/<?php echo $numofpages ?>&nbsp;&nbsp;<?php echo GO_TO;?>
												</td>
												<td width="8%" align="right">
													<select class="textarea2" name="gotoPage" style="margin-bottom:1px; padding:4px 5px; ">
														<?php
														for ($i = 1; $i <= $numofpages; $i++) {
														if ($i == $_GET['page']) {
														$page_selected = "selected";
														} else {
														$page_selected = "";
														}
														?>									
														<option value="<?php echo $i ?>" <?php echo $page_selected ?>><?php echo $i ?></option>
														
														<?php
														}
														?>									
													</select>	
												</td>
												<td width="7%">
													<a onClick="Javascript:document.frmAffiliates.submit();" class="go_button_2"><?php echo GO; ?></a>   
												</td>
												<td width="41%" align="center"><?php echo $next ?></td>
											</tr>
										</table>
									</td>
								</tr>
							<?php } ?>
                        </table>

                    </form>											

                </div>


                <!--content area end-->

            </div>

            </form>

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