<?php

include("../includes/site_config.php");
//include($CONST_INCLUDE_ROOT.'/session_handler.inc');
//include($CONST_INCLUDE_ROOT.'/functions.php');
include("../session_handler.inc");
include("../includes/functions.php");
$mode=$_GET['mode'];

$Sess_UserType= $_SESSION['Sess_UserType'];
if ($Sess_UserType != "A") {
        exit;
}
//include($CONST_INCLUDE_ROOT.'/pop_lists.inc');
//include($CONST_INCLUDE_ROOT.'/error.php');
//include($CONST_INCLUDE_ROOT.'/message.php');
include("../pop_lists.inc");
include("../error.php");
include("../message.php");

//$mode=form_get('mode');

$mode=$_REQUEST['mode'];

//$recid=form_get('recid');

$recid=$_REQUEST['recid'];

//$lstTypes=form_get('lstTypes');

$lstTypes=$_REQUEST['lstTypes'];

//print "$lstTypes";

//$txtBase=form_get('txtBase');

$txtBase=$_REQUEST['txtBase'];

$txtName=$_REQUEST['txtName'];

# retrieve the template
$area = 'member';

$language=new language;

if (!isset($lstTypes) || empty($lstTypes)) {
        $lstTypes='SKG';
}
$arr_languages = $language->GetActiveList();

function split_by_lang($from, $field_name) {
        global $arr_languages;
        $res = array();
        foreach ($arr_languages as $l) {
                $field = $field_name . '_' . $l->LangID;
                if (isset($from[$field]))
                        $res[$l->LangID] = $from[$field];
        }
        return $res;
}
switch ($mode) {
        case 'edit':
                restrict_demo();
				$query = "SELECT * FROM vlistbox WHERE lst_recid = '$recid'";
//                          echo $query."<br>";
                $result = mysql_query($query,$link) or die(mysql_error());
                $opt_row = mysql_fetch_object($result);
//                print_r($opt_row);
/*
                $query = "SELECT vl.lst_recid, vl.lst_base, vl.lst_adult, vl.lst_order, COALESCE(vlv.lst_value, vl.lst_value) lst_value, l.lang_id, l.lang_name,vl.lst_value opt_base
                          FROM vlistbox vl, langfile l LEFT OUTER JOIN vlistbox_values vlv ON vlv.lang_id = l.lang_id AND vl.lst_recid = vlv.lst_recid
                          WHERE vl.lst_recid = '$recid' AND lang_active =1";
*/
$query = "
		SELECT vl.lst_recid, 
		vl.lst_base, 
		vl.lst_adult, 
		vl.lst_order, 
		COALESCE(vlv.lst_value, vl.lst_value) lst_value, 
		l.lang_id, 
		l.lang_name,
		vl.lst_value opt_base 
		FROM vlistbox vl, 
		langfile l 
		LEFT OUTER JOIN vlistbox_values vlv 
		ON vlv.lang_id = l.lang_id  
		WHERE vl.lst_recid = '$$recid'
		";
//echo "<br>$query<br>";
                $result = mysql_query($query,$link) or die(mysql_error());
                break;

        case 'save':
                restrict_demo();
                $arr_txtAddvalue = split_by_lang($_POST, 'txtAddvalue');
                $txtOrder = $_POST['txtOrder'];
                $txtRecid = $_POST['txtRecid'];
                $txtBase=$_POST['txtBase'];
                foreach ($arr_txtAddvalue as $lang => $txtAddvalue) {
#                        $txtAddvalue = preg_replace("/'/","&#039;",$txtAddvalue,-1);
                        $query = "REPLACE vlistbox_values SET lst_value='$txtAddvalue', lst_recid = '$txtRecid',  lang_id = '$lang'";
                        mysql_query($query,$link) or die(mysql_error());
                }
                $txtAddvalue = $arr_txtAddvalue['EN'];
#                $txtAddvalue = preg_replace("/'/","&#039;",$txtAddvalue,-1);
                $query = "UPDATE vlistbox SET lst_value='$txtBase', lst_order=$txtOrder WHERE lst_recid = '$txtRecid'";
                $result = mysql_query($query,$link) or die(mysql_error());
                break;
        case 'add':
                restrict_demo();
                $arr_txtAddvalue = split_by_lang($_POST, 'txtAddvalue');
                $txtOrder=$_POST['txtOrder'];

                $txtAddvalue = $txtBase;
                $query = "INSERT INTO vlistbox (lst_value,lst_type,lst_order,lst_base,lst_adult, name) VALUES ('$txtAddvalue','$lstTypes','$txtOrder','Y','Y','$txtName')";
                mysql_query($query,$link) or die(mysql_error());
				$txtRecid = mysql_insert_id($link);
				$lowelcase = strtolower($txtName);
				$newphrase9 = str_replace(" ","", $lowelcase);
				$tablefield = "adv_$newphrase9";
				$res=mysql_query("SELECT * FROM adverts");
				$fields = mysql_num_fields($res);
				for ($j = 0; $j < $fields; $j++) 
				{
				$fname=mysql_field_name($res, $j);
				if($fname!=$tablefield)
				{
				//$query = "ALTER TABLE `adverts` ADD `adv_$newphrase9` VARCHAR( 1000 ) NOT NULL AFTER `bio_approved`";
				$query = "CREATE TABLE `test` (`adv_$newphrase9` VARCHAR( 1000 ) NOT NULL)";
				mysql_query($query,$link) or die(mysql_error());
				} 
				else
				{
				echo "this field dropdown already exist";
				}
				}
				
                
                foreach ($arr_txtAddvalue as $lang => $txtAddvalue) {
                       echo $query = "INSERT INTO vlistbox_values (lst_recid, lang_id, lst_value,name) VALUES  ('$txtRecid', '$lang', '$txtAddvalue', '$txtName')";
                        mysql_query($query,$link) or die(mysql_error());
                }
                break;
        case 'delete':
                restrict_demo();
                $query="DELETE FROM vlistbox_values WHERE lst_recid='$recid'";
                mysql_query($query,$link) or die(mysql_error());
                $query="DELETE FROM vlistbox WHERE lst_recid='$recid'";
                mysql_query($query,$link) or die(mysql_error());
                break;
}
$query="SELECT vl.lst_recid, vl.lst_type,vl.name, vl.lst_base, vl.lst_adult, vl.lst_order, COALESCE(vlv.lst_value, vl.lst_value) lst_value,vl.lst_value opt_base
        FROM vlistbox vl LEFT OUTER JOIN vlistbox_values vlv ON vl.lst_recid = vlv.lst_recid AND vlv.lang_id = '" . $_SESSION['lang_id'] . "'
        WHERE vl.lst_type='$lstTypes' ORDER BY lst_order ASC";
$values_arr = mysql_query($query,$link) or die(mysql_error());
//mysql_close( $link );

$sel=mysql_query("SELECT DISTINCT lst_type,name from vlistbox");

?>
<?php include("include/header.php"); ?>
<script type="text/javascript">

function validate()
{
		// require at least 3 characters be entered
		if (document.FrmListbox1.lstTypes.value.length < 3)
		{
		alert("Please enter at least 3 characters in the \"Type\" field.");
		document.FrmListbox1.lstTypes.focus();
		return (false);
		}

		// allow ONLY alphanumeric keys, no symbols or punctuation
		// this can be altered for any "checkOK" string you desire
		var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var checkStr = FrmListbox1.lstTypes.value;
		var allValid = true;
		for (i = 0;  i < checkStr.length;  i++)
		{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length)
		{
		allValid = false;
		break;
		}
		}
		if (!allValid)
		{
		alert("Please enter only Capetal letter characters in the \"Type\" field.");
		document.FrmListbox1.lstTypes.focus();
		return (false);
		}
			
		if(document.FrmListbox1.txtBase.value=="")
		{
			alert(" Please Enter The Question Base Value .");
			document.FrmListbox1.txtBase.focus();
			return (false);
		}	
		if(document.FrmListbox1.txtName.value=="")
		{
			alert(" Please Enter The Question Name.");
			document.FrmListbox1.txtName.focus();
			return (false);
		}
		//return (false);	
}
</script>
<style type="text/css">
<!--
#apDiv1 {
	position:absolute;
	left:700px;
	top:188px;
	width:566px;
	height:114px;
	z-index:1;
}
.style1 {color: #333333}
.style2 {color: #094e89}
.style3 {color: #FF0000}
-->
</style>


<div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;Profile Dropdown Questions</div></div>
<div class="clear"></div>
<div class="admin-page">
  <table width="650" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
    <!--DWLayoutTable-->
    <tr><td width="650" align="left">
        <table width="80%"  border="0" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>">
        <!--<form enctype='multipart/form-data' method='post' action='<//?php echo $CONST_ADMIN_LINK_ROOT?>/admin_dropdowns.php<//?php print("?PHPSESSID=".session_id()); ?>' name="FrmListbox">-->
		<form enctype='multipart/form-data' method='post' action='admin_dropdowns.php<?php print("?PHPSESSID=".session_id()); ?>' name="FrmListbox">
          <tr>
            <td align="left" valign="top" class="tdhead" colspan="4">&nbsp;</td>
          </tr>
          <tr class="tdodd">
            <td colspan="4" align="left"> <select class="input"  size="1" name="lstTypes" onChange="FrmListbox.mode.value='view'; FrmListbox.submit(); return true;">
			<? while($type=mysql_fetch_object($sel)){?>
			<option <?php if ($lstTypes==$type->lst_type) print("selected"); ?> value="<?=$type->lst_type?>"><?=$type->name?></option>
			
			<? }?>
                <!--<option <?php if ($lstTypes=="SKG") print("selected"); ?> value="SKG"><?php echo OPTION_SEEKING?></option>
                <option <?php if ($lstTypes=="ETH") print("selected"); ?> value="ETH"><?php echo OPTION_ETHNICITY?></option>
                <option <?php if ($lstTypes=="MRT") print("selected"); ?> value="MRT"><?php echo OPTION_MARITAL?></option>
                <option <?php if ($lstTypes=="BDY") print("selected"); ?> value="BDY"><?php echo OPTION_BODY_TYPE?></option>
                <option <?php if ($lstTypes=="RLG") print("selected"); ?> value="RLG"><?php echo OPTION_RELIGION?></option>
                <option <?php if ($lstTypes=="EDU") print("selected"); ?> value="EDU"><?php echo OPTION_EDUCATION?></option>
                <option <?php if ($lstTypes=="SMK") print("selected"); ?> value="SMK"><?php echo OPTION_SMOKER?></option>
                <option <?php if ($lstTypes=="CHL") print("selected"); ?> value="CHL"><?php echo OPTION_CHILDREN?></option>
                <option <?php if ($lstTypes=="EMP") print("selected"); ?> value="EMP"><?php echo OPTION_EMPLOYMENT?></option>
                <option <?php if ($lstTypes=="INC") print("selected"); ?> value="INC"><?php echo OPTION_INCOME?></option>
                <option <?php if ($lstTypes=="PST") print("selected"); ?> value="PST"><?php echo OPTION_PERSONALITY?></option>
                <option <?php if ($lstTypes=="PHI") print("selected"); ?> value="PHI"><?php echo OPTION_PHILOSOPHIES?></option>
                <option <?php if ($lstTypes=="SOG") print("selected"); ?> value="SOG"><?php echo OPTION_SOCIAL_GROUP?></option>
                <option <?php if ($lstTypes=="GLS") print("selected"); ?> value="GLS"><?php echo OPTION_GOALS?></option>
                <option <?php if ($lstTypes=="HBS") print("selected"); ?> value="HBS"><?php echo OPTION_HOBBIES?></option>
                <option <?php if ($lstTypes=="SPT") print("selected"); ?> value="SPT"><?php echo OPTION_SPORTS?></option>
                <option <?php if ($lstTypes=="MSC") print("selected"); ?> value="MSC"><?php echo OPTION_MUSIC?></option>
                <option <?php if ($lstTypes=="FDT") print("selected"); ?> value="FDT"><?php echo OPTION_FOOD_TASTE?></option>
                <option <?php if ($lstTypes=="EYE") print("selected"); ?> value="EYE"><?php echo OPTION_EYE_COLOR?></option>
                <option <?php if ($lstTypes=="HAR") print("selected"); ?> value="HAR"><?php echo OPTION_HAIR_COLOR?></option>
                <option <?php if ($lstTypes=="DNK") print("selected"); ?> value="DNK"><?php echo OPTION_DRINK?></option>
				<option <?php if ($lstTypes=="ABC") print("selected"); ?> value="ABC"><?php echo "ABC";?></option>-->
              </select> <input type='hidden' name='mode'> </td>
          </tr>
		  <tr><td height="10px" colspan="2"></td></tr>
          <?php
 if ($mode!='edit') {

                //print(" <tr class='tdtoprow'>
                 //        <td>&nbsp;</td>
                  //       <td>&nbsp;</td>
                   //      <td><b>Value</b></td>
                   //      <td><b>Order</b></td>
                   //   </tr>");
                while ($sql_array = mysql_fetch_object($values_arr)) {
                    print("<tr  class='normal'>
                        <!--<td  align='center' valign='middle'><a href='$CONST_ADMIN_LINK_ROOT/admin_dropdowns.php?$sess_id&mode=edit&recid=$sql_array->lst_recid&lstTypes=$sql_array->lst_type'>".GENERAL_EDIT."</a></td>-->
						<td  align='center' valign='middle'><a href='admin_dropdowns.php?$sess_id&mode=edit&recid=$sql_array->lst_recid&lstTypes=$sql_array->lst_type&name=$sql_array->name'>".GENERAL_EDIT."</a></td>
                        <!--<td  align='center' valign='middle'><a href='$CONST_ADMIN_LINK_ROOT/admin_dropdowns.php?$sess_id&mode=delete&recid=$sql_array->lst_recid&lstTypes=$sql_array->lst_type'>".GENERAL_DELETE."</a></td>-->
						<td  align='center' valign='middle'><a href='admin_dropdowns.php?$sess_id&mode=delete&recid=$sql_array->lst_recid&lstTypes=$sql_array->lst_type&name=$sql_array->name'>".GENERAL_DELETE."</a></td>
                        <td  valign='middle'>$sql_array->lst_value</td>
                        <td  valign='middle'>$sql_array->lst_order</td>
                              ");
                }
                print("</tr>");
}
?>

          <?php
if ($mode!='edit') {
?>
		  <tr><td height="20px" colspan="2"></td></tr>
		  <tr>
		    <td  class="admin-head" colspan="2">Add New Dropdown to this section</td>
		  </tr>
		 
          <tr  class="tdodd">
            <td align="left" width="100px"><?= LISTBOX_BASE?></td>
            <td valign='top'   colspan='3'> <input type='text' name='txtBase' value="<?= $lst_base?>" class="input"></td>
          </tr>
		
          <?php
                foreach ($arr_languages as $lang) {
?>
          <!--<tr  class="normal">
            <td align="left">
              <?= LISTBOX_NEW ?>
              &nbsp;(
              <?= $lang->Name ?>
              )</td>
            <td  valign='top' colspan='3'> <input type='text' name='txtAddvalue_<?= $lang->LangID ?>' class="input"></td>
          </tr>-->
          <?
        }
?>
          <tr  class="tdodd">
            <td align="left" >
              <?= LISTBOX_ORDER ?>   	     </td>
            <td align="left" colspan="3"><input type='hidden' name='txtRecid' value='<?= $recid ?>'>
              <input type='text' name='txtOrder' size='5' value='<?= $lst_order ?>' class="input">
			    <? $query = mysql_query("SELECT DISTINCT lst_type,name from vlistbox where lst_type ='".$_REQUEST['lstTypes']."'");
			  	while($type_value=mysql_fetch_object($query)) { ?>
			   <input type='hidden' name='txtName' size='5' value='<?=$type_value->name;?>' class="input">
			  <? } ?>
              <input type='submit' class="normal" onClick="FrmListbox.mode.value='add';" value='<?= GENERAL_SAVE ?>'></td>
          </tr>
          <?
}else {
?>
          <tr  class="normal">
            <td align="left">
              <?= LISTBOX_BASE?>            </td>
            <td valign='top'  colspan="3"> <input type='text' name='txtBase' value="<?= $opt_row->lst_value?>" class="input"></td>
          </tr>
          <?
            while ($item_arr = mysql_fetch_object($result)) {
          ?>
          <tr  class="tdodd">
            <td align="left"  >
              <?= LISTBOX_EDIT ?>
              &nbsp;(
              <?= $item_arr->lang_name ?>
              )</td>
            <td align='left' valign='top'  colspan='3'> <input type='text' name='txtAddvalue_<?= $item_arr->lang_id ?>' value="<?= $item_arr->lst_value ?>" class="input"></td>
          </tr>
          <?
       }
?>
          <tr  class="normal">
            <td align="left">
              <?= LISTBOX_ORDER ?>            </td>
            <td align="left"  colspan="3"><input type='hidden' name='txtRecid' value='<?= $recid ?>'>
              <input type='text' name='txtOrder' size='5' value="<?= $opt_row->lst_order ?>" class="input">
				<input type='hidden' name='txtName' size='5' value="<?=$opt_row->name?>" class="input">
              <input name="submit" type='submit' class="normal" onClick="FrmListbox.mode.value='save';" value='<?= GENERAL_SAVE ?>'></td>
          </tr>

          <?
}
?>  <tr  >
            <td valign="top" align="right"  colspan="4"  class="tdfoot">&nbsp;</td>
          </tr>
        </form>
		<form action="../../../../../../../../DOCUME~1/Admin/LOCALS~1/Temp/scp44072/httpdocs/dating_demo/admin/<?=$CONST_LINK_ROOT?>/admin/admin_dropdowns.php<?php print("?PHPSESSID=".session_id()); ?>" method="post" name="FrmListbox1" onSubmit="return validate();">
		<tr class="normal">
        <tr>
          <td  class="admin-head" colspan="2">Add New Profile Question</td>
        </tr>
          <td class="normal">SystemName</td>
            <td><input type="text" name="lstTypes" />
            UPPERCASE Only</td>
		</tr>
		<tr class="normal"><td>Default Option</td><td><input type="text" name="txtBase" /></td>
		</tr>
		<tr class="normal"><td>Question Name</td><td><input type="text" name="txtName" />
		<input type='hidden' name='mode'>
		<input type="submit" name="submit" value="Add Question" class="normal" onClick="FrmListbox1.mode.value='add';"/>
		</td></tr>
		</form><tr><td></td></tr>
      </table></td>
    </tr>
    <tr>
      <td class="normal" valign="top"><p class="style1"><em><strong><br />
              </strong></em><strong><span class="style3">IMPORTANT NOTE:</span></strong></p>
        <p class="style1"><span class="style2">When adding new profile questions, please follow these instructions:</span></p>
        <p class="style2"><em><strong>SystemName</strong></em> - This is the way the system will look at the new profile question. This must be in UPPERCASE. This will not be seen by the user.</p>
        <p class="style2"><em><strong>Default Option</strong></em> - This will be the first dropdown option in the profile question. You can add more dropdown options after you create the new profile question.  You may add more dropdown options by selecting the profile question after it's created.</p>
        <p class="style2"><em><strong>Question Name</strong></em> - This is the profile question that will show up. This will be what the user sees for the profile dropdown question.</p>
        <p class="style2">Also note that when you delete all the dropdown options for a profile question then that question will be deleted and you will have to re-add the  profile question and it's dropdown options.</p></td>
    </tr>
</table>
</div><div class="admin-page-bottom"></div></div>
<?php include("include/footer.php"); ?>