<?php
defined('_JEXEC') or die('Restricted access');
//JToolBarHelper::title(JText::_('NS Pro Newsletters'), 'inbox.png');

$db =& JFactory::getDBO();
$db->setQuery('SELECT `id`, `lname` FROM `#__nspro_lists` WHERE `published` = 1');
$mlists = $db->loadObjectList();

$mattachments = array();
if (!($this->isNew)) {
  $query = 'SELECT * FROM `#__nspro_newsletter_attachments` WHERE `newsletter_id` = ' . $this->newsletter->id . ' ORDER BY `id`';
  $db->setQuery($query);

  $mattachments = $db->loadObjectList();
}

$editor =& JFactory::getEditor();
?>
<script type="text/javascript">
<!--

  function submitbutton(pressbutton) {
    var form = document.adminForm;

    if (pressbutton == 'ncancel') {
      submitform( pressbutton );
      return;
    }

    selRef = document.getElementById('mailing_lists2');
    var mstring = '';
    for (var i=0; i < selRef.options.length;i++) {
      if (selRef.options[i].selected) {
        if (mstring == '') {
          mstring = selRef.options[i].value;
        }
        else  {
          mstring = mstring + ',' + selRef.options[i].value;
        }
      }
    }

    document.getElementById('mailing_lists').value = mstring;

    // do field validation
    var text = <?php echo $editor->getContent( 'message' ); ?>
    if (text == ""){
      alert( "<?php echo JText::_( 'Newsletter must have some text', true ); ?>" );
    } else {
      <?php
      echo $editor->save( 'message' );
      ?>
      submitform( pressbutton );
    }
  }
//-->
</script>
<table>
<tr><td>
<form action="index.php" method="post" name="adminForm" id="adminForm">
<div class="col width-10">
    <fieldset class="adminform">
        <legend><?php echo JText::_( 'Details' ); ?></legend>

        <table class="admintable">
        <tr>
            <td width="100" align="right" class="key">
                <label for="fName">
                    <?php echo JText::_( 'From Name' ); ?>:
                </label>
            </td>
            <td>
                <input class="text_area" type="text" name="fName" id="fname" size="180" maxlength="250"
                value="<?php echo $this->newsletter->fName;?>" />
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="fEmail">
                    <?php echo JText::_( 'From Email' ); ?>:
                </label>
            </td>
            <td>
                <input class="inputbox" type="text" name="fEmail" id="femail" size="180" value="<?php echo $this->newsletter->fEmail; ?>" />
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="datetime">
                    <?php echo JText::_( 'Date and Time' ); ?>:
                </label>
            </td>
            <td>
                <input class="inputbox" type="text" name="datetime" id="datetime" size="180" value="<?php echo $this->newsletter->datetime; ?>" />
            </td>
        </tr>
        <tr>
            <td width="120" class="key">
                <?php echo JText::_( 'Sent' ); ?>:
            </td>
            <td>
                <?php echo JHTML::_( 'select.booleanlist',  'sent', 'class="inputbox"', $this->newsletter->sent ); ?>
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="subject">
                    <?php echo JText::_( 'Subject' ); ?>:
                </label>
            </td>
            <td>
                <input class="inputbox" type="text" name="subject" id="subject" size="180" value="<?php echo $this->newsletter->subject; ?>" />
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="mailing_lists">
                    <?php echo JText::_( 'Mailing Lists' ); ?>:
                </label>
            </td>
            <td>
                <input type="hidden" name="mailing_lists" id="mailing_lists" value="<?php echo $this->sub->mailing_lists; ?>" />
                <select class="inputbox" size="5" style="width: 258px;" multiple="multiple" name="mailing_lists2[]" id="mailing_lists2">
                <?php
                foreach($mlists as $mlist) {
                  print '<option value="' . $mlist->id . '" id="list_' . $mlist->id . '"';
                  print '>' . $mlist->lname . '</option>';
                }
                ?>
                </select>
            </td>
        </tr>
        <tr>
            <td width="110" class="key">
                <label for="message">
                    <?php echo JText::_( 'Message' ); ?>:
                </label>
            </td>
            <td>
            <?php
              echo $editor->display('message', $this->newsletter->message, '728', '400', '180', '20');
            ?>
            </td>
        </tr>
        <tr>
        </table>
    </fieldset>
</div>
<input type="hidden" name="option" value="com_nspro" />
<input type="hidden" name="id" value="<?php echo $this->newsletter->id; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="nspro" />
</form>
<i>Note: When uploading or deleting attachments, all changes above will be lost!</i>
</td></tr>
<?php
  if (!($this->isNew)) {
    if (count($mattachments) > 0) {
      ?>
<tr><td>
<div class="col width">
<fieldset class="adminform">
<legend>Newsletter Attachments</legend>
<form action="index.php" method="post">
<table>
<?php
  foreach ($mattachments as $mat) {
    print '<tr><td><input type="checkbox" name="attachment[]" value="' . $mat->id . '"/></td><td>'. $mat->filename .'</td></tr>';
  }
?>
</table>
<br/>
<input type="submit" name="deleter" value="Delete Selected Attachments"/>
<input type="hidden" name="option" value="com_nspro" />
<input type="hidden" name="newsletter_id" value="<?php echo $this->newsletter->id; ?>" />
<input type="hidden" name="task" value="deleteattachments" />
<input type="hidden" name="controller" value="nspro" />
</form>
</fieldset>
</div>
</td></tr>
      <?php
    }
?>
<tr><td>
<form enctype="multipart/form-data" action="index.php" method="POST" name="fileattachform">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<div class="col width">
<fieldset class="adminform">
<legend>Add Attachments</legend>
<table class="admintable"><tr>
<td width="110" class="key">Select File:</td>
<td><input id="my_file_element" type="file" name="file_1" size="80">
</td></tr><tr>
<td width="110" class="key">Attached Files (up to 5):</td>
<td><div id="attached_files_list" style="border: 1px solid #CCC; padding: 15px; background: rgb(255, 255, 255) none repeat scroll 0% 0%;"></div>
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="attachallfiles" value="<?php print JText::_('Upload and Attach'); ?>"><br/>
</td></tr>
</table>
<script type="text/javascript"><!--
// Thank you Mister Stickman !!!
// Multiple file selector by Stickman -- http://www.the-stickman.com
// with thanks to: [for Safari fixes] Luis Torrefranca -- http://www.law.pitt.edu and Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com [for duplicate name bug] 'neal'
function MultiSelector( list_target, max ){this.list_target = list_target;this.count = 0;this.id = 0;if( max ){this.max = max;} else {this.max = -1;};this.addElement = function( element ){if( element.tagName == 'INPUT' && element.type == 'file' ){element.name = 'file_' + this.id++;element.multi_selector = this;element.onchange = function(){var new_element = document.createElement( 'input' );new_element.type = 'file';new_element.setAttribute('size', '80');this.parentNode.insertBefore( new_element, this );this.multi_selector.addElement( new_element );this.multi_selector.addListRow( this );this.style.position = 'absolute';this.style.left = '-1000px';};if( this.max != -1 && this.count >= this.max ){element.disabled = true;};this.count++;this.current_element = element;} else {alert( 'Error: not a file input element' );};};this.addListRow = function( element ){var new_row = document.createElement( 'div' );new_row.setAttribute('style', 'margin-bottom: 10px;');var new_row_button = document.createElement( 'input' );new_row_button.type = 'button';new_row_button.value = 'Delete';new_row.element = element;new_row_button.onclick= function(){this.parentNode.element.parentNode.removeChild( this.parentNode.element );this.parentNode.parentNode.removeChild( this.parentNode );this.parentNode.element.multi_selector.count--;this.parentNode.element.multi_selector.current_element.disabled = false;return false;};new_row.innerHTML = element.value+' ';new_row.appendChild( new_row_button );this.list_target.appendChild( new_row );};};
--></script>
<script type="text/javascript"><!--
var multi_selector = new MultiSelector( document.getElementById( 'attached_files_list' ), 5 );
multi_selector.addElement( document.getElementById( 'my_file_element' ) );
--></script>
</fieldset>
</div>

<input type="hidden" name="newsletter_id" value="<?php print $this->newsletter->id; ?>" />
<input type="hidden" name="option" value="com_nspro" />
<input type="hidden" name="task" value="attachfile" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="controller" value="nspro" />
</form>
</td></tr>
<?php
  }
  else {
    print '<tr><td><i>Note: Attachments are not available when making a new Newsletter.</i></td></tr>';
  }
?>
</table>
<div class="clr"></div>

<small><i>NS Pro by <a href="http://www.mavrosxristoforos.com/">Mavrosxristoforos.com</a> Copyright 2010</i></small>
<script type="text/javascript"><!--
<?php
$mnLists = explode(',', $this->newsletter->mailing_lists);
foreach ($mnLists as $mnlist) {
  print 'document.getElementById("list_'.$mnlist.'").setAttribute("selected","selected");' . "\n";
}
?>
-->
</script>