<?php
    session_start();
		
    include 'function/classes.php';
	
    if($_POST['prodid']!='')
	{		
		if ($_POST['page_marker'] == 'top_page')
		{								
			if( count($_POST['prodid']) > 7)
			{
				$saveMsg="Please select maximum 7 products";
			}
			else 
			{
				$temp_list = '';
				foreach($_POST['prodid'] as $list) {
					$prod = getData('order_id','tb_top_page','product_id = ' . $list);	
					if ($prod)
					{
						$temp_list[$list] = $prod['order_id'];
					}		
					else
					{
						$temp_list[$list] = 0;
					}
				}
				$del = saveData('tb_top_page','','','deleteall');
				foreach($_POST['prodid'] as $list) 
				{
					$tmp=$tmp+1;
					if($count!=$tmp)
					{
						$new=saveData('tb_top_page',"'".$list."'," . $temp_list[$list],'','new');
						//$new=saveData('tb_top_page',"'".$list."'",'','new');
					}
				}	
				$saveMsg="Data has been updated";
					//$setData = $setData . $_SESSION['SESS_ARR'][2][$_GET['tbtype']][$i] ."='" . $str ."',";
			}
		}
	}
	elseif (isset($_POST['page_marker']) && $_POST['page_marker'] == 'top_page_sorter')
	{
		$i = 0;
		foreach($_POST as $key=>$value)
		{

			$temp = explode('_', $key);
			if ($temp[0] == 'order') 
			{
				$new=saveData('tb_top_page','order_id=' . $value,'product_id = ' . $temp[1],'update');
			}
		}
		$saveMsg="Data has been updated";
    }
	             
	$limit=10; // Limit of result per page 
	if ($_GET['page']!= '') {
	$pages=$_GET['page'];}
	else 
	{ $pages = 1;} //$_GET['page']; //Get the current page 
	if (isset($_POST['btndelete']))
    {
        $id="";
        $cID = $_POST['chkid'];
        if(empty($cID))
        {
            $saveMsg="You didn't select any record.";
        }
        else
        {
			$saveMsg="";
			$N = count($cID);

       
            for($i=0; $i < $N; $i++)
            {
              //echo($cID[$i] . " ");
              if ($i==count($cID)-1){
                  $id=$id."". $cID[$i]."";
              }
              else
              {
                $id=$id."". $cID[$i].",";
              }
            }
            saveData($_SESSION['SESS_ARR'][0][$_GET['tbtype']],'', $_SESSION['SESS_ARR'][2][$_GET['tbtype']][1] . " IN (" . $id . ")",'delete');
            $saveMsg="Deleted";
        }
        
    }
        if(!isset($_SESSION['SESS_MEMBER_ID']) ||
                  (trim($_SESSION['SESS_MEMBER_ID'])==""))
                      {
                          echo "You will need to logon to view this page";
                          exit();
                      }
       else{
            getArr();
            if ($_GET['tbtype']<>'top_page' && $_GET['tbtype']<>'tb_top_page' ){
				if ($_GET['tbtype'] == 'product')
				{
					// sort product by order id
					$tmpTable=getList($_SESSION['SESS_ARR'][1][$_GET['tbtype']],$_SESSION['SESS_ARR'][0][$_GET['tbtype']],"ORDER BY order_id LIMIT ".(($pages-1)*$limit).",". $limit);
				}
				else
				{
            		$tmpTable=getList($_SESSION['SESS_ARR'][1][$_GET['tbtype']],$_SESSION['SESS_ARR'][0][$_GET['tbtype']]," LIMIT ".(($pages-1)*$limit).",". $limit);
				}
				//echo $_SESSION['SESS_ARR'][1][$_GET['tbtype']];
				//echo '<br>' . $_SESSION['SESS_ARR'][0][$_GET['tbtype']] . ' ' . ($pages-1) . ' ' . $limit ;
						//echo mysql_num_rows($tmpTable);
			}
			elseif ($_GET['tbtype'] == 'tb_top_page')
			{
				//$tmpTable=getList('product_id, order_id','tb_top_page','');            	
				$tbtop=getList('tb_top_page.product_id,product_name,tb_top_page.order_id','tb_top_page','JOIN tb_products ON tb_products.product_id = tb_top_page.product_id');
				$chosen=array(); 
				$chosen_names = array();
				$chosen_order_id = array();
				while($row=mysql_fetch_array($tbtop))
					{
						$chosen[]=$row['product_id'];
						$chosen_names[]=$row['product_name'];						
						$chosen_order_id[]=$row['order_id'];
					}
			}
			else{
            	$tmpTable=getList('product_name,product_id','tb_products','');
            	$tbtop=getList('tb_top_page.product_id,product_name','tb_top_page','JOIN tb_products ON tb_products.product_id = tb_top_page.product_id ORDER BY tb_top_page.order_id');
				$chosen=array(); 
				$chosen_names = array();
				while($row=mysql_fetch_array($tbtop))
					{
						$chosen[]=$row['product_id'];
						$chosen_names[]=$row['product_name'];						
					}
            }
       }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Mobivation</title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <link href="css/default.css" rel="stylesheet" type="text/css" />
		<script>
			var minNum=1; var maxNum=7;
			function checkListBoxSize(){
				oSelect=document.getElementById("prodid");
				var count=0;
				for(var i=0;i<oSelect.options.length;i++){
				if(oSelect.options[i].selected)
				count++;
				if(count>maxNum){
					alert("Please select maximum 7 products");
				return false;
				}
				}
				if(count<1){
					alert("Please select at least 1 product");
				return false;
				}
				return true;
			}
		
		</script>    
		</head>


    <body>
        <div id="wrapper">
        <?php include 'include/header.php'; ?>
            <!-- end div#header -->
            <div id="page">
                <div id="content">
                    <div id="welcome">
                        <form name="frmlist" onsubmit="return checkListBoxSize()" action="<?php echo $_SERVER['PHP_SELF'];?>?tbtype=<?php echo $_GET['tbtype'];?>" method="post">
                        <!--body-->
                        <?php
                            echo '<table border="0" cellspacing="1" cellspacing="1">';
                            echo '<tr><td>' . $saveMsg . '</td></tr>';
                            if ($_GET['tbtype']<>'top_page' && $_GET['tbtype']<>'tb_top_page'){
                            echo '<tr><td align="right"><input type="submit" value="delete" name="btndelete"></td></tr>';}
                            else
							{
								echo '<tr><td align="right">&nbsp;</td></tr>';
							}
							echo '</table>';
                        ?>
                        <table cellpadding="6" cellspacing="1" class="list">
                            <?php
                                echo '<tr>';
                                for($i = 0; $i<($_SESSION['SESS_ARR'][3][$_GET['tbtype']]); $i=$i+3){
                                    if($_SESSION['SESS_ARR'][2][$_GET['tbtype']][$i]!=""){
                                       if($i==0){
                                       }
                                       elseif ($i==3){
                                            echo '<td class=listHeader>'.$_SESSION['SESS_ARR'][2][$_GET['tbtype']][$i].'</td>';
                                            echo '<td class=listHeader>Delete</td>';
                                       }
                                       elseif (($_GET['tbtype']=='product') && ($i==6 || $i == 21)){
										   // skip language & description
                                       }
									   elseif ($_GET['tb_type'] == 'tb_top_page')
									   {
									   }
                                       else{
                                       		echo '<td class=listHeader>'. $_SESSION['SESS_ARR'][2][$_GET['tbtype']][$i].'</td>';
                                       }
                                    }
                                }
                                echo '</tr>';
                            ?>
                       <?php 
                       	if (count($tmpTable > 0)){

                       		if ($_GET['tbtype']<>'top_page' && $_GET['tbtype']<>'tb_top_page')
							{
                       			$ctr = 0;
                            	while($row=mysql_fetch_array($tmpTable) ){
									$ctr++;
                                	echo '<tr>';
									if ($ctr % 2 == 0)
									{										
										$bgcolor = '#ffffff';
									}
									else
									{
										$bgcolor = '#e6e6e6';
									}
									//echo "here " . $_SESSION['SESS_ARR'][3][$_GET['tbtype']];       
										for($i = 1; $i < $_SESSION['SESS_ARR'][3][$_GET['tbtype']]; $i=$i+3){
	                                 	
	                                            if ($i==1){
	                                               
	                                               //echo'<td><a href="edit.php?tbtype='.$_GET['tbtype'].'&&id='. $row[$_SESSION['SESS_ARR'][2][$_GET['tbtype']][$i]] .'">'. $row[$_SESSION['SESS_ARR'][2][$_GET['tbtype']][$i]] .'</a></td>';
	                                            }
	                                            elseif ($i==4){

	                                               echo '<td valign="top" bgcolor="' . $bgcolor. '"><a href="edit.php?tbtype='.$_GET['tbtype'].'&id='. $row[$_SESSION['SESS_ARR'][2][$_GET['tbtype']][1]] .'">'. $row[$_SESSION['SESS_ARR'][2][$_GET['tbtype']][$i]] .'</a></td>';
	                                               echo '<td valign="top" bgcolor="' . $bgcolor. '"><input type="checkbox" name="chkid[]" value='. $row[$_SESSION['SESS_ARR'][2][$_GET['tbtype']][1]] .'>';
	
	                                            }
	                                            elseif (($_GET['tbtype']=='product') && ($i==7 || $i == 22)){
													// Skip language, platform & 
	                                           		//echo $_SESSION['SESS_ARR'][2][$_GET['tbtype']][$i];
	                                            }
												elseif (($_GET['tbtype']=='product') && ($i==10))
												{
														// show platform
														$sql = 'SELECT platform FROM tb_supported_platform sp, tb_platform p WHERE sp.product_id = ' . $row[$_SESSION['SESS_ARR'][2][$_GET['tbtype']][1]] . ' AND sp.platform_id = p.platform_id';
														//echo $sql;
														$query = mysql_query($sql);
														if ($query) 
														{
															$result = mysql_fetch_array($query);
															if ($result)
															{
																echo '<td valign="top" bgcolor="' . $bgcolor. '">' . $result['platform'] .'</td>';
															}
															else
															{
																echo '<td valign="top" bgcolor="' . $bgcolor. '">&nbsp;</td>';
															}
														}
														else
														{
															echo '<td valign="top" bgcolor="' . $bgcolor. '">&nbsp;</td>';
														}
												}
	                                            else
	                                            {
	                                               		echo'<td valign="top" bgcolor="' . $bgcolor. '">'. substr(strip_tags($row[$_SESSION['SESS_ARR'][2][$_GET['tbtype']][$i]]),0,70) .'</td>';

	                                            }
												//echo '<br>' . $i . ' - ' . $_GET['tbtype']  . ' - ' . $row[$_SESSION['SESS_ARR'][2][$_GET['tbtype']][11]];
	                                
		                            }
                                }
                                echo'</tr>';
                          	}
							elseif ($_GET['tbtype'] == 'tb_top_page')
							{
								?>
								<td>
								Slider order: 
                                <table>
								<?php
								$ctr = 0;
								for ($i = 0; $i < sizeof($chosen_names) ; $i++)
								//{
								//foreach($chosen_names as $chosen_names_value)
								{
									echo '<tr>';
									echo '<td>';
									echo '<input type="text" size="3" value="' . $chosen_order_id[$i] . '" name="order_' . $chosen[$i] . '"> - ' . $chosen_names[$i];
									echo '</td>';
									echo '</tr>';
								}								
								?>
                                </table>                            
                                <input type="hidden" name="page_marker" value="top_page_sorter" />                                
                                <!--
								<br /><br />
                                <input type="hidden" name="top_page" />
								<select name ='prodid[]' id='prodid' multiple size=7 style="width:100;" onchange="datachecking();">
                                -->
								<?php //getInputType('listboxview','products','',$chosen);?>
								<!-- </select>
								</td>
                                -->
								<?php
                          	}
							elseif ($_GET['tbtype'] == 'top_page')
							{
								?>
								<td>
								Currently displayed on the home page: 
								<?php
								foreach($chosen_names as $chosen_names_value)
								{
									echo "<br />- " . $chosen_names_value;
								}
								?>                            
								<br /><br />
                                <input type="hidden" name="page_marker" value="top_page" />
								<select name ='prodid[]' id='prodid' multiple size=7 style="width:100;" onchange="datachecking();">
								<?php getInputType('listboxview','products','',$chosen);?>
								</select>
								</td>
								<?php
                          	}
                        }
                        if ($_GET['tbtype']=='top_page' || $_GET['tbtype']=='tb_top_page'){
                            echo '<tr><td align="right"><input type="submit" value="submit" name="btnsubmit" ></td></tr>';}
                       	if($_GET['tbtype']<>'top_page' && $_GET['tbtype']!='tb_top_page'){include 'include/paging.php';} ?>
                        </table>
                        <!--body ends-->
                    </form>
                    </div>
                    <!-- end div#welcome -->

                </div>
                <!-- end div#content -->
                <div id="sidebar">
                    <ul>
                    <?php include 'include/nav.php'; ?>
                        <!-- end navigation -->
                        <?php //include 'include/updates.php'; ?>
                        <!-- end updates -->
                    </ul>
                </div>
                <!-- end div#sidebar -->
                <div style="clear: both; height: 1px"></div>
            </div>
            <?php include 'include/footer.php'; ?>
        </div>
        <!-- end div#wrapper -->
    </body>
</html>


