<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */
  if (!defined("_VALID_PHP"))
      die('Direct access to this location is not allowed.');

  require_once(ZAZAVILITE . "lib/class_forms.php");
  $form = new Forms();
?>
<?php switch($core->action): case "edit": ?>
<?php $row = $core->getRowById("forms", $form->formid);?>

<h1><?php echo _FORM_MANAGE;?></h1>
<p class="info"><?php echo _FORM_UPDATE;?> <?php echo _REQ1;?> <?php echo required();?> <?php echo _REQ2;?></p>
<h2><?php echo _FORM_EDIT;?> &rsaquo; <?php echo $row['name'];?></h2>
<form action="" method="post" id="admin_form" name="admin_form">
  <table cellspacing="0" cellpadding="0" class="formtable">
    <tr>
      <td width="150"><?php echo _FORM_NAME;?>: <?php echo required();?></td>
      <td><input name="name" type="text" class="inputbox required" value="<?php echo $row['name'];?>" size="55" title="<?php echo _FORM_PLZNAME;?>"/></td>
    </tr>
    <tr>
      <td><?php echo _FORM_SUBJECT;?>: <?php echo required();?></td>
      <td><input name="subject" type="text" class="inputbox required" value="<?php echo $row['subject'];?>" size="55" title="<?php echo _FORM_PLZSUB;?>"/></td>
    </tr>
    <tr>
      <td><?php echo _FORM_MESSAGE;?>: <?php echo required();?></td>
      <td><textarea name="thankyou" cols="50" rows="5" class="inputbox required" title="<?php echo _FORM_ENTER;?>"><?php echo $row['thankyou'];?></textarea>
        <?php echo tooltip(_FORM_TTS);?></td>
    </tr>
    <tr>
      <td><?php echo _FORM_MAILTO;?>: <?php echo required();?></td>
      <td><input name="mailto" type="text" class="inputbox required" value="<?php echo $row['mailto'];?>" size="55" title="<?php echo _FORM_PLZMAIL;?>"/>
        <?php echo tooltip(_FORM_TTMAIL);?></td>
    </tr>
    <tr>
      <td><?php echo _FORM_SUBMIT;?>: <?php echo required();?></td>
      <td><input name="submit_lbl" type="text" class="inputbox required" value="<?php echo $row['submit'];?>" size="55" title="<?php echo _FORM_PLZLABEL;?>"/>
        <?php echo tooltip(_FORM_TTSUB);?></td>
    </tr>
    <tr>
      <td><?php echo _FORM_PUB;?>:</td>
      <td><span class="input-out">
        <label for="active-1">Yes</label>
        <input name="active" type="radio" id="active-1" value="1" <?php getChecked($row['active'], 1); ?> />
        <label for="active-2">No</label>
        <input name="active" type="radio" id="active-2" value="0" <?php getChecked($row['active'], 0); ?> />
        </span></td>
    </tr>
    <tr>
      <td><?php echo _FORM_CAPTCHA;?>:</td>
      <td><span class="input-out">
        <label for="captcha-1"><?php echo _YES;?></label>
        <input name="captcha" type="radio" id="captcha-1" value="1" <?php getChecked($row['captcha'], 1); ?> />
        <label for="captcha-2"><?php echo _NO;?></label>
        <input name="captcha" type="radio" id="captcha-2" value="0" <?php getChecked($row['captcha'], 0); ?> />
        </span></td>
    </tr>
    <tr>
      <td><input type="submit" name="submit" class="button" value="<?php echo _FORM_UPDATE2;?>" /></td>
      <td><a href="loader.php?do=forms" class="button-alt"><?php echo _CANCEL;?></a></td>
    </tr>
  </table>
  <input name="formid" type="hidden" value="<?php echo $form->formid;?>" />
</form>
<?php echo $core->doForm("processForm");?>
<?php break;?>
<?php case"add": ?>
<h1><?php echo _FORM_ADD;?></h1>
<p class="info"><?php echo _FORM_ADD2;?> <?php echo _REQ1;?> <?php echo required();?> <?php echo _REQ2;?></p>
<h2><?php echo _FORM_ADDC;?></h2>
<form action="" method="post" id="admin_form" name="admin_form">
  <table cellspacing="0" cellpadding="0" class="formtable">
    <tr>
      <td width="150"><?php echo _FORM_NAME;?>: <?php echo required();?></td>
      <td><input name="name" type="text" class="inputbox required" value="<?php echo $row['name'];?>" size="55" title="<?php echo _FORM_PLZNAME;?>"/></td>
    </tr>
    <tr>
      <td><?php echo _FORM_SUBJECT;?>: <?php echo required();?></td>
      <td><input name="subject" type="text" class="inputbox required" value="<?php echo $row['subject'];?>" size="55" title="<?php echo _FORM_PLZSUB;?>"/></td>
    </tr>
    <tr>
      <td><?php echo _FORM_MESSAGE;?>: <?php echo required();?></td>
      <td><textarea name="thankyou" cols="50" rows="5" class="inputbox required" title="<?php echo _FORM_ENTER;?>"><?php echo $row['thankyou'];?></textarea>
        <?php echo tooltip(_FORM_TTS);?></td>
    </tr>
    <tr>
      <td><?php echo _FORM_MAILTO;?>: <?php echo required();?></td>
      <td><input name="mailto" type="text" class="inputbox required" value="<?php echo $row['mailto'];?>" size="55" title="<?php echo _FORM_PLZMAIL;?>"/>
        <?php echo tooltip(_FORM_TTMAIL);?></td>
    </tr>
    <tr>
      <td><?php echo _FORM_SUBMIT;?>: <?php echo required();?></td>
      <td><input name="submit_lbl" type="text" class="inputbox required" value="<?php echo $row['submit'];?>" size="55" title="<?php echo _FORM_PLZLABEL;?>"/>
        <?php echo tooltip(_FORM_TTSUB);?></td>
    </tr>
    <tr>
      <td><?php echo _FORM_PUB;?>:</td>
      <td><span class="input-out">
        <label for="active-1"><?php echo _YES;?></label>
        <input name="active" type="radio" id="active-1" value="1" checked="checked" />
        <label for="active-2"><?php echo _NO;?></label>
        <input name="active" type="radio" id="active-2" value="0" />
        </span></td>
    </tr>
    <tr>
      <td><?php echo _FORM_CAPTCHA;?>:</td>
      <td><span class="input-out">
        <label for="captcha-1"><?php echo _YES;?></label>
        <input name="captcha" type="radio" id="captcha-1" value="1" checked="checked" />
        <label for="captcha-2"><?php echo _NO;?></label>
        <input name="captcha" type="radio" id="captcha-2" value="0" />
        </span></td>
    </tr>
    <tr>
      <td><input type="submit" name="submit" class="button" value="<?php echo _FORM_ADDFORM;?>" /></td>
      <td><a href="loader.php?do=forms" class="button-alt"><?php echo _CANCEL;?></a></td>
    </tr>
  </table>
</form>
<?php echo $core->doForm("processForm");?>
<?php break;?>
<?php default: ?>
<h1><?php echo _FORM_MANAGE2;?></h1>
<p class="info"><?php echo _FORM_CUST;?><br /><strong><?php echo _FORM_NOTE;?></strong></p>
<h2><span><a href="loader.php?do=forms&amp;action=add" class="button-sml"><?php echo _FORM_ADD3;?></a></span><?php echo _FORM_VIEW;?></h2>
<table cellpadding="0" cellspacing="0" class="display">
  <thead>
    <tr>
      <th width="20" class="left">#</th>
      <th class="left"><?php echo _FORM_NAME;?></th>
      <th><?php echo _PUBLISHED;?></th>
      <th><?php echo _FORM_EDITF;?></th>
      <th><?php echo _FORM_VIEW2;?></th>
      <th><?php echo _FORM_FIELDS;?></th>
      <th><?php echo _DELETE;?></th>
    </tr>
  </thead>
  <tbody>
    <?php if($form->getForms() == 0):?>
    <tr>
      <td colspan="7"><?php echo $core->msgAlert(_FORM_ALERT,false);?></td>
    </tr>
    <?php else:?>
    <?php foreach ($form->getForms() as $row):?>
    <tr>
      <td><?php echo $row['id'];?>.</td>
      <td><?php echo $row['name'];?></td>
      <td align="center"><?php echo isActive($row['active']);?></td>
      <td align="center"><a href="loader.php?do=forms&amp;action=edit&amp;formid=<?php echo $row['id'];?>"><img src="images/edit.png" class="tooltip"  alt="" title="<?php echo _FORM_EDITF;?>"/></a></td>
      <td align="center"><div id="viewform-<?php echo $row['id'];?>" class="viewform" title="<?php echo $row['name'];?>">
          <table cellspacing="2" cellpadding="2" class="formtable">
            <thead>
              <tr>
                <th colspan="2"><?php echo $row['name'];?></th>
              </tr>
            </thead>
            <?php $form->drawForm($row['id']);?>
            <tr>
              <td colspan="2"><input type="button" name="button-demo" class="button" value="<?php echo $row['submit'];?>" /></td>
            </tr>
          </table>
        </div>
        <a href="javascript:void(0);" onclick="$('#viewform-<?php echo $row['id'];?>').dialog('open'); return false"><img src="images/view.png" class="tooltip"  alt="" title="<?php echo _FORM_VIEW2;?>"/></a></td>
      <td align="center"><a href="loader.php?do=fields&amp;formid=<?php echo $row['id'];?>"><img src="images/edit_field.png" class="tooltip"  alt="" title="<?php echo _FORM_FIELDS2;?>"/></a></td>
      <td align="center"><a href="javascript:void(0);" class="delete" rel="<?php echo $row['name'];?>" id="item_<?php echo $row['id'];?>"><img src="images/delete.png" class="tooltip"  alt="" title="<?php echo _FORM_DELETE;?>"/></a></td>
    </tr>
    <?php endforeach;?>
    <?php unset($row);?>
    <?php endif;?>
  </tbody>
</table>
<div id="dialog-confirm" style="display:none;" title="<?php echo _DELETE.' '._PAGE;?>">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span><?php echo _DEL_CONFIRM;?></p>
</div>
<script type="text/javascript"> 
// <![CDATA[
$(document).ready(function () {
    $(".viewform").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 600,
        height: "auto",
        zindex: 9998,
        modal: false
    });
	
    $('a.delete').live('click', function () {
        var id = $(this).attr('id').replace('item_', '')
        var parent = $(this).parent().parent();
		var title = $(this).attr('rel');
        $("#dialog-confirm").data({
            'delid': id,
            'parent': parent,
			'title': title
        }).dialog('open');
        return false;
    });

    $("#dialog-confirm").dialog({
        resizable: false,
        bgiframe: true,
        autoOpen: false,
        width: 400,
        height: "auto",
        zindex: 9998,
        modal: false,
        buttons: {
            '<?php echo _DELETE;?>': function () {
                var parent = $(this).data('parent');
                var id = $(this).data('delid');
				var title = $(this).data('title');

                $.ajax({
                    type: 'post',
                    url: "ajax.php",
                    data: 'deleteForm=' + id + '&formtitle=' + title,
                    beforeSend: function () {
                        parent.animate({
                            'backgroundColor': '#FFBFBF'
                        }, 400);
                    },
                    success: function (msg) {
                        parent.fadeOut(400, function () {
                            parent.remove();
                        });
						$("html, body").animate({scrollTop:0}, 600);
						$("#msgholder").html(msg);
                    }
                });

                $(this).dialog('close');
            },
            '<?php echo _CANCEL;?>': function () {
                $(this).dialog('close');
            }
        }
    });
});
// ]]>
</script>
<?php break;?>
<?php endswitch;?>