<?php
session_start();
include_once("config.php");
$_SESSION['PCD_Login_Error']="";

include_once("data_definitions.php");

if($_SESSION["seas_brd_language"]==2) {
	$font_increase_value=3;
}
else {
	$font_increase_value=0;
}


$selected_target=0;
if(isset($_REQUEST['tarid'])) {
	$selected_target = $_REQUEST['tarid'];
}

if(isset($_REQUEST['btn_new_target'])) {
	if(isset($_REQUEST['new_target'])) {
		$new_target_desc = trim($_REQUEST['new_target']);
		
		if(strlen($new_target_desc) >0) {
			$insdata = 'insert into seas_brd_target (tar_desc) values ("' . $new_target_desc . '")';
			mysql_query($insdata);
			$selected_target = mysql_insert_id();
			echo "<script>";
			echo "refreshparent();";
			echo "</script>";
		}
	}		
}

if(isset($_REQUEST['remove_tarid'])) {
	$rmvSql  = 'delete from seas_brd_target_dat where tar_id='. $_REQUEST['remove_tarid'];
	mysql_query($rmvSql);		
	$rmvSql  = 'delete from seas_brd_target where tar_id='. $_REQUEST['remove_tarid'];
	mysql_query($rmvSql);		
}

if(isset($_REQUEST['rmv'])) {
	$rmvdata = explode(":",$_REQUEST['rmv']);
	$rmvSql  = 'delete from seas_brd_target_dat where 
				tar_id='. $rmvdata[0] . ' and dfields="' . trim($rmvdata[1]) . '" and opts = '.$rmvdata[2] ;
	mysql_query($rmvSql);
}

if(!isset($_SESSION['seas_user_id'])) $_SESSION['seas_user_id']=0;
if($_SESSION['seas_user_id'] <=0) {
	$_SESSION['PCD_page_ref']="report.php";
	header("location:login.php");
}
// Total Samples
$t_sql = "select sum(weight) from seas_brd_dat";
$t_rslt= mysql_query($t_sql);
$t_array = mysql_fetch_array($t_rslt);
$tot_sample = round($t_array[0],0);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html <? if($_SESSION["seas_brd_language"]==2) echo 'dir="rtl"'; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Manage Targets</title>
<style type="text/css">
body {
	background-color: #0099FF;
}
h1 {
	font-family:Verdana, Geneva, sans-serif;
	font-size:<?php echo 16+$font_increase_value; ?>px;
	font-weight:bolder;
	color:#fff;
}
h2 {
	font-family:Verdana, Geneva, sans-serif;
	font-size:<?php echo 12+$font_increase_value; ?>px;
	font-weight:bold;
	color:#ccc;
}
</style>
<style type="text/css">
.tbl_cell {
	font-family:Verdana, Geneva, sans-serif;
	font-size:<?php echo 11+$font_increase_value; ?>px;
	font-weight:100;
	color:#000;
	text-decoration: none;
}
.tbl_cell_hdr {
	font-family:Verdana, Geneva, sans-serif;
	font-size:<?php echo 11+$font_increase_value; ?>px;
	color:#000;
	font-weight:bold;
}
.tbl_cell_err {
	font-family:Verdana, Geneva, sans-serif;
	font-size:<?php echo 10+$font_increase_value; ?>px;
	color: #F00;
}
div.scrollable {
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: auto;
}
.targetmg {
	text-decoration:none;
	font-size:<?php echo 12+$font_increase_value; ?>px;
}
.targetmg a:visited { color: blue}
.targetmg a:link { color: blue }
.targetmg a:hover { color: black }
.targetmg a:active { color: blue }

.tar_fdesc {
	font-family:Verdana, Geneva, sans-serif;
	font-size:<?php echo 10+$font_increase_value; ?>px;
	font-weight:bold;
	font-style:italic;
	color:#666;
}
.tar_fopt {
	font-family:Verdana, Geneva, sans-serif;
	font-size:<?php echo 10+$font_increase_value; ?>px;	
	font-weight: normal;
	font-style:italic;
	color:#666;
}
.tar_frmv {
	font-family:Verdana, Geneva, sans-serif;
	font-size:<?php echo 12+$font_increase_value; ?>px;	
	font-weight: bolder;
	color:#F00;
	text-decoration:none;
}
.tar_fadd {
	font-family:Verdana, Geneva, sans-serif;
	font-size:<?php echo 10+$font_increase_value; ?>px;	
	font-weight: bolder;
	color: #00F;
	text-decoration:none;
}	
.tar_fadd a:hover{
	color:#003;
	font-size:<?php echo 11+$font_increase_value; ?>px;	
}
</style>
<script>
	function mouseovr(obj) {
		obj.style.background='DarkSalmon';
	}
	function mouseout(obj) {
		obj.style.background='white';
	}
	function showdesc(tr) {
		if(tr.style.display=="none") {
			tr.style.display="";
		}
		else {
			tr.style.display="none";	
		}
	}

function edit_target(TargID,TargDesc) {
		var w = 200;
		var h = 130;
		var left = (window.screen.width / 2) - ((w / 2) + 10);
		var top = (window.screen.height / 2) - ((h / 2) + 50);
		
		myWindow2 = window.open('edit_target.php?tarid='+TargID+'&tardesc='+TargDesc, 'tinyWindow3','toolbar=false,width='+w+',height='+h+',left='+left+',top='+top+',resizable=no,scrollbars=yes');	
}

function Add_Newvar(TargID) {
		var w = 600;
		var h = 400;
		var left = (window.screen.width / 2) - ((w / 2) + 10);
		var top = (window.screen.height / 2) - ((h / 2) + 50);
		
		myWindow2 = window.open('add_newvar.php?fisrtcall=1&tarid='+TargID, 'tinyWindow2','toolbar=false,width='+w+',height='+h+',left='+left+',top='+top+',resizable=no,scrollbars=yes');	
}
function removevar(tarid,dflds,opts) {
	if(tarid>0 && opts >0) {
		if(confirm("Do you wish to remove this variable definition from the selected target?")) {
			window.location.href = "managetarget.php?tarid=" + tarid + "&rmv="+ tarid+":"+dflds+":"+opts;
		}
	}
}
function remove_target(tarid) {
	if(tarid>0) {
		if(confirm("Do you wish to remove this target?")) {
			window.location.href = "managetarget.php?remove_tarid=" + tarid;
			refreshparent();
		}
	}	
}

function refreshparent() {
	//opener.location.reload();
	window.opener.refreshtarget();
}
</script>
</head>
<body onunload="refreshparent()">

<h1><?php  echo $language_array['manage_target']; ?></h1>
<h2>Total Sample Size: <?php echo $tot_sample; ?></h2>

<p>
	<table border="1" width="100%" style="background-color:#FFF; border-collapse:collapse; border: thin #000">
    	<tr>
        	<td colspan="3">
            	 <table width="100%" cellpadding="2" cellspacing="0">
                    <tr>
                      <td height="30px" colspan="2" align="left" bgcolor="#CCCCCC"class="tbl_cell_hdr"><form id="form1" name="form1" method="post" action="">
                        <label for="new_target"></label>
                        <input type="text" name="new_target" id="new_target" style="width:150px" />
                        <input type="submit" name="btn_new_target" id="btn_new_target" value="<?php  echo $language_array['add_new_target']; ?>" />
                        <input type="button" name="update_btn" id="update_btn" value="<?php  echo $language_array['update_targets']; ?>" onclick="refreshparent()" />
                      </form></td>
                      <td bgcolor="#CCCCCC"class="tbl_cell_hdr" align="<? if($_SESSION["seas_brd_language"]==2) echo "left"; else echo "right";?>"><?php  echo $language_array['sample_size']; ?></td>
                      <td bgcolor="#CCCCCC" class="tbl_cell_hdr" align="<? if($_SESSION["seas_brd_language"]==2) echo "left"; else echo "right";?>"><?php  echo $language_array['perc_to_total_sample']; ?></td>
                    </tr>                 
					<?php
					if($_SESSION["seas_brd_language"]==2) {
						$sql = "select tar_id,case when length(tar_desc_ar)>0 then tar_desc_ar else tar_desc end as tar_desc  from seas_brd_target order by tar_desc";	
					}
					else {
                    	$sql = "select tar_id,tar_desc  from seas_brd_target order by tar_desc";
					}
                    $rslt = mysql_query($sql);
                    while($rw=mysql_fetch_assoc($rslt)) {
						// Sample Size
					
						$tr_condition = "";
						$tr_sql  = "select distinct dfields from seas_brd_target_dat where tar_id=" . $rw['tar_id'];
						$tr_rslt = mysql_query($tr_sql);
						while($tr_rw=mysql_fetch_assoc($tr_rslt)) {
							$tr_opts="";
							$tr_sql2  = "select distinct opts from seas_brd_target_dat where tar_id=" . $rw['tar_id'] . ' and dfields = "'. $tr_rw['dfields'].'"';
							$tr_rslt2 = mysql_query($tr_sql2);
							while($tr_rw2=mysql_fetch_assoc($tr_rslt2)) {
								if(strlen($tr_opts)>0) $tr_opts .= ",";
								$tr_opts .= $tr_rw2['opts'];
							}
							mysql_free_result($tr_rslt2);
							if(strlen($tr_condition)>0) $tr_condition .=  " AND ";
							$tr_condition .= " (" . $tr_rw['dfields'] ." in (" . $tr_opts . ")) ";
						}
						mysql_free_result($tr_rslt);
						if(strlen($tr_condition)>0) $tr_condition = " (" . $tr_condition . ") ";
						// Target Base
						if(strlen($tr_condition) >0) {
							$sqlcond = $tr_condition;
						}
						else {
							$sqlcond = "1=1";
						}
						$t_sql = "select sum(weight) from seas_brd_dat where " . $sqlcond;
						$t_rslt= mysql_query($t_sql);
						$t_array = mysql_fetch_array($t_rslt);
						$trg_sample = round($t_array[0],0);	
						mysql_free_result($t_rslt);					
						
                    ?>

                    <tr  onmouseover="mouseovr(this)" onmouseout="mouseout(this)">
                        <td class="tbl_cell" align="center" nowrap="nowrap">
                        <img src="images/trash.png" title="<?php  echo $language_array['remove_this_target']; ?>" onclick="remove_target(<?php echo $rw['tar_id']; ?>);return 0;" />
                        <img src="images/edit.gif" title="<?php  echo $language_array['edit_description_of_this_target']; ?>" onclick='edit_target(<?php echo $rw['tar_id'].',"'.trim($rw['tar_desc']) . '"'; ?>);return 0;' />
                        </td>
                        <td class="tbl_cell targetmg" align="<? if($_SESSION["seas_brd_language"]==2) echo "right"; else echo "left";?>" width="60%"><a href="#" class="targetmg" onclick="showdesc(<?php echo "tr_". $rw['tar_id']; ?>)"><?php echo $rw['tar_desc']; ?></a></td>
                        <td class="tbl_cell" align="<? if($_SESSION["seas_brd_language"]==2) echo "left"; else echo "right";?>" width="20%"><?php echo $trg_sample; ?></td>
                        <td class="tbl_cell" align="<? if($_SESSION["seas_brd_language"]==2) echo "left"; else echo "right";?>" width="20%"><?php echo round((($trg_sample/$tot_sample)*100),2); ?></td>                    </tr>
                    
                    <?php
						$rsql = "select seas_brd_target_dat.dfields,seas_brd_target_dat.opts,dmap.fdesc,dmap.opts as opts_desc from seas_brd_target_dat left join seas_brd_dmap dmap on seas_brd_target_dat.dfields=dmap.dfields where tar_id=". $rw['tar_id'] . " order by 1,2" ;
						$rrslt = mysql_query($rsql);
						//if(mysql_num_rows($rrslt) >0) {
					?>
                    <tr id="tr_<?php echo $rw['tar_id']; ?>" style="display:<?php if($selected_target!=$rw['tar_id']) echo "none"; ?>">
                        <td class="tbl_cell" align="<? if($_SESSION["seas_brd_language"]==2) echo "right"; else echo "left";?>" width="60%" colspan="3" style="padding:10px">
                      		<table cellpadding="1" cellspacing="0">
                            	<?php
								while($rrw=mysql_fetch_assoc($rrslt)) {
									$opt_array_tmp = explode(";",$rrw['opts_desc']);
									$opt_array=array();
									foreach($opt_array_tmp as $opts) {
										$opts_split = explode(":",$opts);
										$opt_array[$opts_split[0]]=$opts_split[1];
									}
								?>
                            	<tr>
                                	<td> 
                                    <span class="tar_frmv" ><a href="#" onclick='removevar(<?php echo $rw['tar_id'].',"'.trim($rrw['dfields']).'",'.$rrw['opts'];  ?>)' title="<?php  echo $language_array['click_to_remove_this_variable_definition']; ?>" class="tar_frmv">[-]</a> </span>
                                    <span class="tar_fdesc"><?php echo $rrw['fdesc']; ?> = </span>
                                    <span class="tar_fopt"><?php echo $opt_array[$rrw['opts']]; ?></span>
                                    </td>
                                </tr>
                                <?php
								}
								?>
                            	<tr>
                                	<td> 
                                    <span class="tar_fadd" ><a href="#" title="<?php  echo $language_array['click_to_add_new_variable_definition']; ?>" class="tar_fadd">[+]</a> </span>
                                    <span class="tar_fadd"><a href="#" onclick="Add_Newvar(<?php echo $rw['tar_id']; ?>)" title="<?php  echo $language_array['click_to_add_new_variable_definition']; ?>" class="tar_fadd" onclick="Add_Newvar(<?php echo $rw['tar_id']; ?>)"><?php  echo $language_array['add_new_variable']; ?></a></span>
                                    <span class="tar_fopt">&nbsp;</span>
                                    </td>
                                </tr> 
                            </table>
                        </td>
                    </tr>
                    <?php
						}
						mysql_free_result($rrslt);
					?>
                    <?php
                    //}
					mysql_free_result($rslt);
                    ?>
                 </table>
            </td>
    	</tr>
    </table>
  </form>
  </p>
</body>
</html>
