<?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 (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($_REQUEST['defaultcurrency'])
    {
        $defaultCurrencyId = $_REQUEST['defaultcurrency'];
        $sql = "SELECT vcurrency_code,vactive FROM ".$tableprefix."currency_master WHERE ncurrency_id='".$defaultCurrencyId."'";
        $res1 = mysql_query($sql);
        $rows = mysql_fetch_array($res1);
        $ccode = $rows['vcurrency_code'];
        $vactive = $rows['vactive'];

        if($vactive=="Y")
            {
                $updateSettings = "UPDATE ".$tableprefix."settings SET admin_currency ='".$ccode."'";
                mysql_query($updateSettings);
                header('Location:currency.php?msg=currency_default');
            }
        else
            {
                $message = MSG_ENABLE_DEFAULT_CURRENCY;
            }

        
    }

/**/

$adminCurrency = GetAdminCurrency();
$defaultCurrency = $adminCurrency['currency_code'];

/* ---------Patch----------- */


if ($_GET['msg'] != "") {
	switch ($_GET['msg']) {
		case 'add':
			$message = MSG_NEW_CURRENCY."!!";
			break;
		case 'update':
			$message = MSG_CURRENCY_DETAILS_UPDATED."!!";
			break;
		case 'currency_disable':
			$message = MSG_ENABLE_ATLEAST_ONE_CURRENCY."!!";
			break;
		case 'currency_delete':
			$message = MSG_CANNOT_DELETE_ALL_CURRENCIES."!!";
			break;
                case 'currency_default':
                        $message = MSG_DEFAULT_CURRENCY."!!";
			break;
		case 'delete':
			$message = MSG_CURRENCY_DELETED."!!";
			break;
	}
}

/* -------------Checking the count of active currencies-------------- */

$select_active_currency = "SELECT ncurrency_id FROM " . $tableprefix . "currency_master WHERE vactive = 'Y'";
$result_active_currency = mysql_query($select_active_currency) or die(mysql_error());

/* -------------Checking the count of active currencies-------------- */

/* -------------Checking the count of currencies-------------- */

$select_currency_count = "SELECT ncurrency_id FROM " . $tableprefix . "currency_master WHERE 1";
$result_currency_count = mysql_query($select_currency_count) or die(mysql_error());

if (mysql_num_rows($result_currency_count) == 1) {
	$update_currency_status = "UPDATE " . $tableprefix . "currency_master SET vactive = 'Y' WHERE 1";
	mysql_query($update_currency_status) or die(mysql_error());
	$update_currency_row = mysql_fetch_assoc($result_currency_count);
	$_SESSION['SESS_currency_type'] = $update_currency_row['ncurrency_id'];
}

/* -------------Checking the count of active currencies-------- */

/* ---------Patch----------- */

if ($_GET['delete_permanent'] == 'N') {

	switch ($_GET['delete_status']) {

		CASE 'N':
			$delete_currency = "UPDATE " . $tableprefix . "currency_master SET vactive = 'Y' WHERE ncurrency_id = " . $_GET['del_id'];
			mysql_query($delete_currency) or die(mysql_error());

			/* ---------Patch----------- */
			$msg = "update";
			header("Location:currency.php?page=" . $_GET['page'] . "&txtSearch=" . $_GET['txtSearch'] . "&msg=" . $msg);
			exit;

			/* ---------Patch----------- */
			break;
		CASE 'Y':

			if (mysql_num_rows($result_active_currency) > 1) {

                                $sql = "SELECT vcurrency_code,vactive FROM ".$tableprefix."currency_master WHERE ncurrency_id='".$_GET['del_id']."'";
                                $res2 = mysql_query($sql);
                                $rows1 = mysql_fetch_array($res2);
                                if($rows1['vcurrency_code']!=$defaultCurrency)
                                    {
                                        $delete_currency = "UPDATE " . $tableprefix . "currency_master SET vactive = 'N' WHERE ncurrency_id = " . $_GET['del_id'];
                                        mysql_query($delete_currency) or die(mysql_error());

                                        /* ---------Patch----------- */
                                        $msg = "update";
                                        header("Location:currency.php?page=" . $_GET['page'] . "&txtSearch=" . $_GET['txtSearch'] . "&msg=" . $msg);
                                }

                                else
                                    {
                                        $message = MSG_CANNOT_DISABLE_DEFAULT_CURRENCY."!!";
                                }

				/* ---------Patch----------- */
			} else {
				header("Location:currency.php?page=" . $_GET['page'] . "&txtSearch=" . $_GET['txtSearch'] . "&msg=currency_disable");
			}
			break;
	}
} else if ($_GET['delete_permanent'] == 'Y') {

	if (mysql_num_rows($result_currency_count) > 1) {
                $sql = "SELECT vcurrency_code,vactive FROM ".$tableprefix."currency_master WHERE ncurrency_id='".$_GET['del_id']."'";
                $res2 = mysql_query($sql);
                $rows1 = mysql_fetch_array($res2);
                if($rows1['vcurrency_code']!=$defaultCurrency)
                 {
                    $delete_currency = "DELETE FROM " . $tableprefix . "currency_master WHERE ncurrency_id = " . $_GET['del_id'];
                    mysql_query($delete_currency) or die(mysql_error());
                    header('Location:currency.php?msg=delete');
                    exit;
                }
                else
                    {
                        $message = MSG_CANNOT_DELETE_DEFAULT_CURRENCY."!!";
                }
		
	} else {
		header('Location:currency.php?msg=currency_delete');
		exit;
	}
}

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

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



$qryopt = "";
if ($txtSearch != "") {

    $qryopt .= "  WHERE vcurrency_name  like '" . addslashes($txtSearch) . "%'";
}
$sql = "SELECT *,CASE WHEN vactive = 'Y' THEN 'Yes' ELSE 'No' END AS vcurrency_deleted
						FROM " . $tableprefix . "currency_master " . $qryopt . " order by ncurrency_id DESC ";

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

$limit = getSettingsValue('npagination_limit');

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


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

$totalrows = mysql_num_rows($result);



/* ----------Pagination patch--------------- */

$page = $_GET['page'];

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


/* if($txtSearch != "")
 {
$limitvalue = 0;
//$page = 1;
}
else
{ */
$limitvalue = ($page - 1) * $limit;
//}

/* ----------Pagination patch--------------- */

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

$sql = $sql . $query_string;

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

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

//echo $sql;

include "../includes/htmltop.php";
?>
<script language="JavaScript" type="text/JavaScript">
    function clickSearch()
    {

        if(document.frmCurrency.txtSearch.value=='')
        {
            return confirm("<?php echo MSG_ENTER_CURRENCY_NAME;?>")
            document.frmCurrency.txtSearch.focus();
            return false;
        }

        else
        {
            document.frmCurrency.submit();	
            //return false;
  
        }
     
    }



    function deleteCurrency()
    {

			
        return confirm("<?php echo MSG_CURRENCY_DELETE_CONFIRM;?>!!")

    }


    function blockCurrency(val)
    {

			
        if(val == 'Y')
        {

            return confirm("<?php echo MSG_CURRENCY_DISABLE_CONFIRM;?>!!")
	
        }
        else if(val == 'N')
        {
	
        	return confirm("<?php echo MSG_CURRENCY_ENABLE_CONFIRM;?>!!")
	
        }	

    }
function validateDefaultCurrency()
{
    var c = confirm("Are you sure to make this as default currency?");
    if(c)
        return true;
    else
        return false;
}
</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 -->

					<table cellpadding="0" cellspacing="0" border="0" width="100%">
						<tr>
							<td height="35">
                                
                                <?php include_once("../includes/currencymenu.php") ?>
                                                            
							</td>
						</tr>
						<tr>
							<td>

								<div align="left" class="page_title">
									<?php echo CURRENCY_SETTINGS;?>
								</div>
								<div align="center" class="text_information grey_tr">
									<?php echo $message ?>
								</div>
								<div align="left">
                                                                    <table cellpadding="0" cellspacing="0" width="100%">
									<form action="" method="get" name="frmCurrency">
                                                                            
										<tr>
											<td  align="left"><span class="required">*<?php echo UPDATE_CURRENCY_SETTINGS;?>.
											</span></td>
										</tr>
										<tr valign="top">
											<td align="right"  valign="top">
												<table width="100%" border="0">
													<tr>
														<td align="right"></td>
                                                                                                                <td width="100%" align="left" valign="middle" class=""><?php echo SEARCH_BY_CURRENCY_NAME;?>:&nbsp;
															&nbsp; <input type="text" name="txtSearch" size="20"
															maxlength="50"
															value="<?php echo(htmlentities($txtSearch)); ?>"
															class="textbox">
															<a onClick="return clickSearch();" class="search_button"><?php echo BTN_SEARCH; ?></a>
														</td>
													</tr>
												</table>
											</td>
										</tr>
                                                                            
									</form>
                                                                     </table>
								</div>

								<div align="left">
                                                                    <table cellpadding="0" cellspacing="0" width="100%">
                                                                        <tr>
											<td valign="top">
									<form action="" method="post" name="frmCurrency1">

										

												<table border="0" width="100%" cellpadding="2" cellspacing="2" class='manage_table'>
													<tr class="">
														<th width="31%" align="center">#</th>
														<th width="18%" align="center"><?php echo CURRENCY_NAME;?>
														</th>
														<th width="15%" align="center"><?php echo CURRENCY_CODE;?>
														</th>
														<th width="20%" align="center"><?php echo EXCHANGE_PRICE;?>
														</th>
														<th width="14%" align="center"><?php echo ACTIVE;?></th>
                                                                                                                <th width="14%" align="center"><?php echo DEEEFAULT;?></th>
														<th width="21%" align="center"><?php echo ACTION;?></th>
													</tr>
													<?php
													if (mysql_num_rows($result) > 0) {

                                                    $i = 1;
                                                    
                                                    while ($row = mysql_fetch_array($rs)) {
                                                        ?>
													<tr>
													<td align="center"><?php echo (($page * $limit) + $i - $limit) ?></td>
													<td align="center"><?php echo $link . htmlentities($row["vcurrency_name"]) . "</a>"; ?>
														</td>
														<td align="center"><?php echo $row["vcurrency_code"] ?></td>
														<td align="center"><?php echo $adminCurrency['currency_symbol']; ?>&nbsp;<?php echo htmlentities($row["nexchange_price"]) ?>
														</td>
														<td align="center"><?php echo htmlentities($row["vcurrency_deleted"]); ?>&nbsp;</td>
                                                                                                                <td align="center">
                                                                                                                    <?php
                                                                                                                    
                                                                                                                    if($defaultCurrency==$row["vcurrency_code"])
                                                                                                                        {
                                                                                                                            $delink1 = "currency.php?defaultcurrency=".$row["ncurrency_id"]."&st=N";
                                                                                                                            $defaultUrl = "Yes";
                                                                                                                        }
                                                                                                                    else
                                                                                                                        {
                                                                                                                            $delink1 = "currency.php?defaultcurrency=".$row["ncurrency_id"]."&st=Y";
                                                                                                                            $defaultUrl = "<a href='".$delink1."' onclick='return validateDefaultCurrency()' style='cursor: pointer'>No</a>";
                                                                                                                        }
                                                                                                                        echo $defaultUrl;
                                                                                                                        ?>
                                                                                                                    &nbsp;</td>
														<td align="center" valign="middle">
														<a
															href="currencyedit.php?edit_id=<?php echo $row["ncurrency_id"] ?>&qstring=<?php echo $_SERVER['QUERY_STRING'] ?>">
																<img title="<?php echo TITLE_EDIT; ?>" src="../images/btn_edit.gif"
																border="0">
														</a>
														<a
															href="currency.php?del_id=<?php echo $row["ncurrency_id"] ?>&delete_status=<?php echo $row['vcurrency_delete_status'] ?>&delete_permanent=Y"
															style="cursor: pointer"
															onClick="return deleteCurrency();"> <img title="<?php echo TITLE_DELETE; ?>" src="../images/btn_del.gif" border="0">
														</a>&nbsp;/&nbsp; <?php
														if ($row['vactive'] == "N") {
                                                                    ?> <a
															href="currency.php?del_id=<?php echo $row["ncurrency_id"] ?>&delete_status=<?php echo $row['vactive'] ?>&delete_permanent=N&page=<?php echo $page ?>&txtSearch=<?php echo $txtSearch ?>"
															style="cursor: pointer"
                                                                                                                        onClick="return blockCurrency('<?php echo $row['vactive'] ?>')" class="enable_button"><?php echo LINK_ENABLE; ?></a> <?php
                                                                } else {
                                                                    ?> <a
															href="currency.php?del_id=<?php echo $row["ncurrency_id"] ?>&delete_status=<?php echo $row['vactive'] ?>&delete_permanent=N&page=<?php echo $page ?>&txtSearch=<?php echo $txtSearch ?>"
															style="cursor: pointer"
                                                                                                                        onClick="return blockCurrency('<?php echo $row['vactive'] ?>')" class="disable_button"><?php echo LINK_DISABLE; ?></a> <?php
                                                                }
                                                                ?> 
														</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'] . "?page=$pageprev&txtSearch=$txtSearch alt='Previous' border='0' class='small_gray_button'>prev</a>";
                                                }


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

                                                    $next = "<a href=" . $_SERVER['PHP_SELF'] . "?page=$pagenext&txtSearch=$txtSearch alt='Next' border='0' class='small_gray_button'>next</a>";
                                                }

                                                if ($page < $numofpages || $page > 1) {
                                                    ?>
                                                                                                        <tr>
                                                                                                            <td valign="top" colspan="7">
                                                                                                                <table cellpadding="0" cellspacing="0" width="100%">
                                                                                                                    <tr>
														<td colspan="6" align="right" valign="middle">
															<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;?>(<?php echo S;?>) <?php echo $page ?>/<?php echo $numofpages ?>&nbsp;&nbsp;
																	<?php echo GO_TO;?>
																</td>
																<td width="8%" align="right">
																	<select name="gotoPage" style="padding:4px 5px; margin-bottom:1px; ">
																		<?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.frmCurrency1.submit();" class="go_button_2">Go</a>	
																</td>
																<td width="41%" align="center"><?php echo $next ?></td>
																</tr>
															</table>
														
														</td>
													</tr>
                                                                                                                </table>
                                                                                                            </td>
                                                                                                        </tr>


													
													<?php
                                                }
                                                ?>
												</table>
									
									</form>
                                                                                        </td></tr>
                                                                    </table>



								</div>

							</td>
						</tr>
					</table>
					<!--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";
			?>