<html lang="en"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="icon" href="images/favicon.ico">
    <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(factorid,marketid,targetid) {
        var rnd = Math.random();
        try{
            http.open('POST', 'controller/showoptions.php');
            http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            http.onreadystatechange = refreshOpts;
            http.send('ddata='+factorid+':'+marketid+':'+targetid);
        }
        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?first_call=2">
              <table width="100%" border="0" cellspacing="0" cellpadding="5">
                  <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['targ_vars'] as $tar_id=>$vars_array) {
                        foreach($vars_array as $var_id=>$opts_array) {   
            				
							if(count($opts_array) >0) {
								$resql = 'select distinct mst_factor.factorid,mst_factor.factordes,
											 mst_socio.opt_id, mst_socio.opt_desc
											from mst_socio
											left join mst_factor on mst_socio.srv_mrkt = mst_factor.srv_mrkt and mst_socio.factorid = mst_factor.factorid
									where mst_socio.srv_mrkt ='. $_SESSION['sel_market'] .
									' and mst_socio.factorid ='. $var_id .
									' and mst_socio.opt_id in ('. implode(",",$opts_array) . ') 
									 order by 2';
	
								$rerslt = mysql_query($resql) or die($resql);
								while($rw = mysql_fetch_assoc($rerslt)) {		
									?>
									<tr>
										<td class="tar_frmv"><a class="tar_frmv" href="add_newvar?first_call=2&rmv_id=<?php echo $rw['factorid'].":".$rw['opt_id'];  ?>">[-]</a></td>
										<td class="tbl_cell_hdr"><?php echo $rw['factordes']; ?></td>
										<td class="tbl_cell"><?php echo $rw['opt_desc'] ?></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 $_SESSION['sel_market']; ?>,<?php echo $_SESSION['sel_target']; ?>)">
                    <option value="0" selected></option>
                    <?php
                    $varsql = "select distinct mst_factor.factorid,mst_factor.factordes 
										from mst_factor
										left join mst_socio on mst_factor.srv_mrkt = mst_socio.srv_mrkt and  mst_factor.factorid = mst_socio.factorid
								where mst_factor.srv_mrkt =". $_SESSION['sel_market']." order by 2";
                    $varrslt = mysql_query($varsql);
                    while($varopts=mysql_fetch_assoc($varrslt)) {
                        echo '<option value="'. $varopts['factorid'] .'">' . $varopts['factordes'] . "</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">
                  &nbsp;
                  </td>
                </tr>
              </table>
            </form>
</body>
</html>


