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

if(isset($_REQUEST['save_btn'])) {
	
	$tar=0; 
	if(isset($_SESSION['PCD_targ_vars'])) {
	   foreach($_SESSION['PCD_targ_vars'] as $tar_id=>$vars_array) {
			foreach($vars_array as $var_id=>$opts_array) {   
				foreach($opts_array as $opts_id=>$sel_opts) {		
					if($tar_id>0 && $opts_id >0) {
						$inssql = 'insert into seas_brd_target_dat (tar_id,dfields,opts) values ('. $tar_id . ',"'. $var_id . '",'. $opts_id . ')';
						mysql_query($inssql);
						
						if($tar==0) $tar = $tar_id;
					}				
				}				
			}
	   }
	}

	$_SESSION['PCD_targ_vars']=array();
	unset($_SESSION['PCD_targ_vars']);

	echo "<script>"	;
	//echo 'opener.location.href = opener.location.href + "?tarid='. $tar .'"';
	echo 'opener.location.href = "managetarget.php?tarid='. $tar .'";';
	//echo 'opener.location.reload();';
	echo "window.close()";
	echo "</script>";
}

if(isset($_REQUEST['rmv_id'])) {
	$rm_code = explode(":",$_REQUEST['rmv_id']);
	$rmv_tar_id=$rm_code[0];
	$rmv_var_id=$rm_code[1];
	$rmv_opt_id=$rm_code[2];
	
	if(isset($_SESSION['PCD_targ_vars'][$rmv_tar_id][$rmv_var_id][$rmv_opt_id])) {
		unset($_SESSION['PCD_targ_vars'][$rmv_tar_id][$rmv_var_id][$rmv_opt_id]);
	}
}
if(isset($_REQUEST['fisrtcall'])){ 
	if($_REQUEST['fisrtcall']==1) {
		if(isset($_SESSION['PCD_targ_vars'])) {
			$_SESSION['PCD_targ_vars']=array();
			unset($_SESSION['PCD_targ_vars']);
		}
	}
}

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");
}


if(isset($_REQUEST['add_btn'])) {
	$vars = trim($_REQUEST['sel_var']);	
	if(isset($_REQUEST['sel_opts'])) {
		foreach($_REQUEST['sel_opts'] as $opts) {
			$tar  = $_REQUEST['trg_id'];		
			$_SESSION['PCD_targ_vars'][$tar][$vars][$opts] = 1;
		}
	}
}

if(isset($_REQUEST['clear_all'])) {
	if(isset($_SESSION['PCD_targ_vars'])) {
		$_SESSION['PCD_targ_vars']=array();
		unset($_SESSION['PCD_targ_vars']);
	}
}

if(!isset($_SESSION['PCD_targ_vars'])) $_SESSION['PCD_targ_vars']=array();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add New Variable</title>
<style type="text/css">
.tbl_cell {
	font-family:Verdana, Geneva, sans-serif;
	font-size:11px;
	font-weight:100;
	color:#000;
	text-decoration: none;
}
.tbl_cell_hdr {
	font-family:Verdana, Geneva, sans-serif;
	font-size:11px;
	color:#000;
	font-weight:bold;
}
.tbl_cell_hdr2 {
	font-family:Verdana, Geneva, sans-serif;
	font-size:11px;
	color:#fff;
	font-weight:bold;
}
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color:#999999;
}
.tar_frmv {
	font-family:Verdana, Geneva, sans-serif;
	font-size:12px;	
	font-weight: bolder;
	color:#F00;
	text-decoration:none;
}
</style>

<script>
var http = createRequestObject();
function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
	try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	catch(f) { xmlhttp=null; }
    }
	if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
		xmlhttp=new XMLHttpRequest();
	}
	return  xmlhttp;
}
function getoptions(dflds,trg) {
	var rnd = Math.random();
	try{
		http.open('POST', 'showoptions.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = refreshOpts;
		http.send('ddata='+dflds+':'+trg);
	}
	catch(e){}
	finally{}
}
function refreshOpts() {
	try{
		if((http.readyState == 4)&& (http.status == 200)){
			var response = http.responseText;
			//document.getElementById("red").innerHTML = response;
			fillcombo(response,'sel_opts');
		}
  	}
	catch(e){}
	finally{}
}
function fillcombo(rsp,cmbo) {
	 var s_menu = document.getElementById(cmbo);
	 s_menu.innerHTML = "";
	 var stateNodes = rsp.split(";");
	 var optionItem;
	 var textValue;
	 for (r=0;r<stateNodes.length; r++)
	 {	
		 var mdata = stateNodes[r].split(":");
		 textValue=mdata[1];
		 if(textValue.length >0) {
			 optionItem = new Option( mdata[1], mdata[0],  false, false);
			 s_menu.options[s_menu.length] = optionItem;
		 }	 
	 }
} 
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="add_newvar.php?first_call=2&tarid=<?php echo  $_REQUEST['tarid']; ?>">
  <table width="100%" border="0" cellspacing="0" cellpadding="5">
    <tr>
      <td height="25" colspan="2" valign="middle" bgcolor="#999999" class="tbl_cell_hdr2">Selected Variable Definitions</td>
    </tr>
    <tr>
      <td height="32" colspan="2" valign="bottom" bgcolor="#FFCC99" class="tbl_cell_hdr">
      <table>
       <?php 
	   $varcount=0;
	      
	   foreach($_SESSION['PCD_targ_vars'] as $tar_id=>$vars_array) {
			foreach($vars_array as $var_id=>$opts_array) {   

				$resql = 'select seas_brd_dmap.fdesc,seas_brd_dmap.opts as opts_desc from seas_brd_dmap where dfields="'. trim($var_id) . '"';
				$rerslt = mysql_query($resql);
				$rearray= mysql_fetch_array($rerslt);
				$opt_array_tmp = explode(";",$rearray[1]);
				$opt_array=array();
				foreach($opt_array_tmp as $opts) {
					$opts_split = explode(":",$opts);
					$opt_array[$opts_split[0]]=$opts_split[1];
				}	
				
				foreach($opts_array as $opts_id=>$sel_opts) {		
			    ?>
				<tr>
					<td class="tar_frmv"><a class="tar_frmv" href="add_newvar.php?first_call=2&tarid=<?php echo  $_REQUEST['tarid']; ?>&rmv_id=<?php echo $tar_id.":".$var_id.":".$opts_id;  ?>">[-]</a></td>
                    <td class="tbl_cell_hdr"><?php echo $rearray[0]; ?></td>
					<td class="tbl_cell"><?php echo $opt_array[$opts_id] ?></td>
				</tr>
				<?php 
					$varcount++;
				}				
			}
	   }
			


		if($varcount<=0) {
			echo "No selection of variables has done";
	   }
		?>
      </table>
      </td>
    </tr>
    <tr>
      <td height="32" valign="bottom" bgcolor="#FFCC99" class="tbl_cell_hdr"><input type="submit" name="save_btn" id="save_btn" value="Save &amp; Close Definition" />
      <input type="submit" name="clear_all" id="clear_all" value="Clear All" /></td>
      <td valign="bottom" bgcolor="#FFCC99" class="tbl_cell_hdr">&nbsp;</td>
    </tr>
    <tr>
      <td width="50%" valign="middle" bgcolor="#CCCCCC" class="tbl_cell_hdr">Select Variable</td>
      <td width="50%" valign="middle" bgcolor="#CCCCCC" class="tbl_cell_hdr">Select Option</td>
    </tr>
    <tr>
      <td height="30" valign="top" bgcolor="#CCCCCC">
        <select name="sel_var" id="sel_var" style="width:100%" onchange="getoptions(this.value,<?php echo $_REQUEST['tarid']; ?>)">
        <option value="0" selected></option>
        <?php
		$varsql = "select dfields,fdesc from seas_brd_dmap where mtype='SNG' order by 2";
		$varrslt = mysql_query($varsql);
		while($varopts=mysql_fetch_assoc($varrslt)) {
			echo '<option value="'. $varopts['dfields'] .'">' . $varopts['fdesc'] . "</option>";
		}
		?>
        </select>
        <?php
		mysql_free_result($varrslt);
		?>
        <br />
        <br />
<input type="submit" name="add_btn" id="add_btn" value="Add Variable" /></td>
      <td valign="top" bgcolor="#CCCCCC">
        <select name="sel_opts[]" size="10" multiple="multiple" id="sel_opts" style="width:100%">
      
        </select>
      </td>
    </tr>
    <tr>
      <td valign="middle" bgcolor="#999999"><div id="red"> </div></td>
      <td bgcolor="#999999">
      <input type="text" name="trg_id" id="trg_id" hidden  value="<?php echo $_REQUEST['tarid'];  ?>" /></td>
    </tr>
  </table>
</form>
</body>
</html>
