<?php
defined( 'ABSPATH' ) or exit;   // show a blank page if try to access this file directly

require_once( EWZ_PLUGIN_DIR . 'classes/ewz-base.php' );
require_once( EWZ_PLUGIN_DIR . 'classes/ewz-exception.php' );
require_once( EWZ_PLUGIN_DIR . 'classes/ewz-field.php' );
require_once( EWZ_PLUGIN_DIR . 'classes/ewz-layout.php' );
require_once( EWZ_PLUGIN_DIR . 'classes/validation/ewz-layout-input.php' );
require_once( EWZ_PLUGIN_DIR . 'includes/ewz-common.php' );
require_once( EWZ_CUSTOM_DIR . 'ewz-custom-data.php' );



/* * ************************** Main Layout Function ********************************
 *
 * Generate the Layouts page
 *
 * If getting post input, save the data.
 * Display the Layouts page
 *
 * @return void
 */
function ewz_get_layout_controls($ewzG ){
?>
   <div style="float:right">
      <img alt="Help icon" class="ewz_ihelp" src="<?php print  $ewzG['helpIcon']; ?>" onClick="ewz_help('lcompressed')"> &nbsp;
         Show Compressed View <input type="checkbox" onClick="layouts_compress_lines(this)">
   </div>

   <br><br>

   <?php  if ($ewzG['can_do']) { ?>
      <div class="ewz_numc"><?php print $ewzG['nonce_string']; ?></div>
         <img  class="ewz_ihelp" src="<?php print $ewzG['helpIcon']; ?>" onClick="ewz_help('lsave')">&nbsp;
      <button  type="button" class="button-secondary" id="add_layout" onClick="add_layout_copy()">
         Add a New Layout
      </button> &nbsp;
         with options copied from:  <select id="ewz_addlayout"><?php print $ewzG['layouts_options']; ?></select>
      <br><br>
   <?php } ?>
      <div class='ewz_order' id='lorder'>
         <img  class="ewz_ihelp" src="<?php print $ewzG['helpIcon']; ?>" onClick="ewz_help('lsort')">&nbsp;
         Layouts may be dragged to rearrange.<br><br>
         <button  type="button"  id="layouts_sort" onClick="sort_layouts_alpha()">Sort Layouts Alphabetically</button>
        <br><br>
          <button  type="button"  id="layouts_save1_" onClick="save_layout_order()">Save Order of Layouts</button>
      </div>

<?php }

function ewz_layout_menu()
{
    // check the permissions
    if ( !Ewz_Permission::can_see_layout_page() ) {
        wp_die( __("Sorry, you do not have permission to view this page", 'entrywizard' ) );
    }
    // grab any error message from processing the POST data and display it in an alert
    $message = '';

    // get the data from the database and display it
    // errors encountered here stop the processing
    try {
        /**********************************************************/
        /*   Get all the layouts -- including the attached fields */
        /**********************************************************/
        // get editable layouts sorted by layout_id
        $editable_layouts = array_values( Ewz_Layout::get_all_layouts( 'can_edit_layout' ) );

        $nonce_string = wp_nonce_field( 'ewzadmin', 'ewznonce', true, false );

        $layouts = ewz_html_esc( $editable_layouts );

        /************************************/
        /* Set the ewzG Javascript variable */
        /************************************/
        $ewzG = array( 'layouts' => $layouts );
        $ewzG['can_do'] = Ewz_Permission::can_edit_all_layouts();

        // get option list of editable layouts sorted by layout_id to match the order of the layout objects
        $ewzG['layouts_options'] = ewz_option_list( Ewz_Layout::get_layout_opt_array( 'can_edit_layout'  ) );
        $ewzG['nonce_string'] = $nonce_string;
        $ewzG['message'] =  wp_strip_all_tags( $message );   // alerts cant show html tags
        $ewzG['load_gif'] = plugins_url( 'images/loading.gif', dirname( __FILE__ ) );
        $ewzG['empty_img'] = array( "max_img_w" => EWZ_DEFAULT_DIM,
                                    "max_img_h" => EWZ_DEFAULT_DIM,
                                    "canrotate" => false,
                                    "mustfill" => false,
                                    "max_img_size" => EWZ_MAX_SIZE_MB,
                                    "min_longest_dim" => EWZ_DEFAULT_MIN_LONGEST,
                                    "allowed_image_types" => array( "image/jpeg" ) );
        $ewzG['empty_str'] = array( "fieldwidth" => EWZ_MAX_FIELD_WIDTH,
                                    "maxstringchars" => EWZ_MAX_STRING_LEN,
                                    "ss_col_fmt" => "-1" );
        $ewzG['empty_opt'] = array( "value" => "", "label" => "", "maxnum" => 0, "selecttext" => "" );
        $ewzG['empty_rad'] = array( "radlabel" => '', "xradlabel" => '' );
        $ewzG['empty_chk'] = array( "maxnum" => 0 ,"chklabel" => '',"xchklabel" => '');
        $ewzG['empty_lab'] = array('label' => '', 'showlabel' => false );
        $ewzG['helpIcon'] = plugins_url( 'images/help.png', dirname( __FILE__ ) );
        $ewzG['maxUploadMb'] = EWZ_MAX_SIZE_MB;
        $ewzG['maxNumitems'] = EWZ_MAX_NUMITEMS;
        $ewzG['maxTotalMb'] = EWZ_MAX_TOTAL_MB;
        $ewzG['display'] = ewz_html_esc( Ewz_Layout::get_all_display_headers() );
        $ewzG['jsvalid'] = Ewz_Base::validate_using_javascript();     // this is set to false for testing server validation
        $ewzG['do_warn'] = Ewz_Base::warn_on_leaving();               // this is set to false for testing since selenium cant handle it

        // a start on internationalization, not completed
        $ewzG['errmsg'] = array(
            'onlylayout' => esc_html__('You cannot delete the only layout.', 'entrywizard') ,
            'deletehasitems' => esc_html__('There are items uploaded to webforms using this layout. Please delete them first.', 'entrywizard'),
            'deletehaswebforms' => esc_html__('This action will also delete all the webforms using this layout.', 'entrywizard') ,
            'deleteconfirm' => esc_html__('Really delete the entire layout ', 'entrywizard') ,
            'layoutname' => esc_html__('Please enter a name for the layout.', 'entrywizard') ,
            'label' => esc_html__('Labels for checkboxes and radiobuttons must consist of letters, digits, dashes, periods, underscores and spaces only.' , 'entrywizard'),
            'maxnumitems' => esc_html__('Please enter the maximum number of items for the layout.', 'entrywizard') ,
            'nofields' => esc_html__('A layout must have at least one field.', 'entrywizard') ,
            'colhead' => esc_html__('Each field in a layout must have a column header.', 'entrywizard') ,
            'ident' => esc_html__('Each field must have an identifier that starts with a letter and consists only of letters, digits, dashes and underscores.', 'entrywizard'),
            'followimg' => esc_html__('A followup field (i.e. one with the identifier "followupQ") may not have type "Image File"', 'entrywizard'),
            'maximgw' => esc_html__('The max image width should consist of digits only.' , 'entrywizard'),
            'maximgh' => esc_html__('The max image height should consist of digits only.' , 'entrywizard'),
            'minlongestdim' => esc_html__('The minimum longest dimension should consist of digits only.' , 'entrywizard'),
            'nomaximgsz' => esc_html__('Each image field must have a max image size.' , 'entrywizard'),
            'maximgsz' => esc_html__('The max image size should consist of digits only, ' .
                'representing the number of Megabytes allowed.' , 'entrywizard'),
            'sysmaxsz' => sprintf( esc_html__('Your php settings limit the size of uploaded files to %d M or less', 'entrywizard'), $ewzG['maxUploadMb'] ) ,
            'sysmaxup' => sprintf( esc_html__('There is a maximum total upload of %d' .
                'M allowed by your php settings.  If a user submits all allowed images at ' .
                'maximum allowed size, their upload may fail without any message. Continue anyway?' , 'entrywizard') , $ewzG['maxTotalMb'] ),
            'optlabel' => esc_html__('Each option in an option list must have a label for the web page.' , 'entrywizard'),
            'optvalue' => esc_html__('Each option in an option list must have a value.' , 'entrywizard'),
            'option' => esc_html__('Options may consist only of letters, digits, dashes, periods and underscores.' , 'entrywizard'),
            'optioncount' => esc_html__('A drop-down selection must contain at least one option' , 'entrywizard'),
            'selecttext' => esc_html__('Text for blank selection may consist only of letters, digits, dashes, periods and underscores.' , 'entrywizard'),
            'restrmsg' => esc_html__('A restriction must have a message to show the user.' , 'entrywizard'),
            'maxnumchar' => esc_html__('Each text entry must have a maximum number of characters.' , 'entrywizard'),
            'chkboxchkmax' => esc_html__('A required checkbox may not set a maximumn number that may be checked', 'entrywizard'),
            'imgtypes' => esc_html__('Each image file must have a set of allowed image types.' , 'entrywizard'),
            'all_any' => esc_html__('Warning: if all the items in a restriction allow `Any` value, ' .
                'the restriction has no effect. It will, however, slow down processing. Go ahead anyway?' , 'entrywizard'),
            'one_any' => esc_html__('Warning: if only one item in a restriction is different from `Any`, ' .
                'the restriction has the same effect as removing the forbidden item from the selection list, ' .
                'but is much slower. Go ahead anyway?', 'entrywizard') ,
            'useless_label' => esc_html__('Warning: A fixed-text label that is not displayed to the user and not shown in the spreadsheet will have no effect at all. ' .
                'Go ahead anyway?', 'entrywizard'),
            'no_append_all_items' => esc_html__('An "All-Items" field may not be appended to another field, and no other field may be appended to it.', 'entrywizard'),
            'no_img_all_items' => esc_html__('An "All-Items" field may not be an image field.', 'entrywizard')
        );

        // use of $ewzG1.var = ewzG  is a hack to get around the fact that wp_localize_script
        // runs html_entity_decode on scalar values.  Our data is already processed where it needs to be,
        // and some of it contains html entities which should not be decoded.

        wp_localize_script( 'ewz-admin-layouts',  'ewzG1',  array( 'gvar'=>$ewzG ) );
                              // script name set above in ewz_layout_scripts

        /* **************************************************************** */
        /* The "ewz_layouts" div is where the actual content goes,          */
        /* generated by javascript from the ewzG object to save bandwidth   */
        /*                                                                  */
        /* Within this is a div containing all the help text.  The help     */
        /* divs actually display as popups when the help icons are clicked  */
        /* **************************************************************** */
?>

    <div class="wrap">
            <h2>EntryWizard Layout Management</h2>
            <!-- HELP SECTION Layout Management Page  -->
            <div class="ewz_inotes">
                <b>Notes:</b>
                <ul><li>All items are required unless marked as optional. Restrictions cannot be created until any changes to fields have been saved.</li>
                <li>Deletions that ask for a confirmation are saved immediately.
                Other changes to a layout are saved when you click it's "Save Changes" button.<br />
                Edit one layout at a time - <u>when one layout is saved, unsaved changes to others  will be lost.</u></li>
                <li>Further information may be obtained by clicking the help icons:
                <img alt="Help icon" class="ewz_ihelp" src="<?php print $ewzG['helpIcon']; ?>"> </li>
               </ul>
            </div>

   <div id="top-controls">
      <?php ewz_get_layout_controls($ewzG); ?>
  </div>

        <div id="ewz_layouts" style="clear:both;">
           <br>
        </div>



        <div id="help-text" style="display:none">
            <!-- HELP POPUP Layouts  -->
            <div id="layout_help" class="wp-dialog" >
                <p>The "name" will appear on the menu of layouts on the "Webforms" page.
                </p>

                <p>The shortcode "ewz_show_webform" generates a webform with N rows, where
                    N is the maximum number of items, and a column for each field
                </p>
                <p>Users may alter the data they enter so long as the webform is open for uploads.
                   Text, Dropdown, Radio and Checkbox data may be changed, but the only way to alter uploaded
                   images is to delete the entire item and re-submit.  <i>Multiple image fields for an item must all be
                   uploaded at the same time.</i></p>

                <p>If some values in the "Maximum number of items" selection list are disabled, it it because
                   you have set a larger maximum for one of the options in a drop-down field.</p>

                <p>Note that your environment or your web hosting company puts three limits on
                    uploaded items.  These have been detected to be:</p>
                    <ol><li>No individual item may be bigger than <?php print $ewzG['maxUploadMb']; ?>M</li>

                        <li>The total size of the whole upload including all items may not be bigger than
                            <?php print $ewzG['maxTotalMb']; ?>M</li>

                        <li>The maximum number of items that may be uploaded simultaneously is
                            <?php print $ewzG['maxNumitems']; ?></li>
                    </ol>
                <p>( For the technically-minded, these limits are set in <i>php.ini</i>, and more
                    details may be obtained from the php documentation website
                    http://php.net/manual/en/ini.core.php )
                </p>

                <p>In addition, Wordpress currently is able to handle only the following image types:
                    <ol><li>.jpg/.jpeg</li>
                        <li>.png</li>
                        <li>.gif</li>
                    </ol>
                </p>
            </div>

            <!-- HELP POPUP Layouts ~ Name For Layout -->
            <div id="name_help" class="wp-dialog" >
                 <p>The name you set for the layout will appear as an option in the "Layouts" drop-down selection on the WebForms page.</p>
            </div>

            <!-- HELP POPUP Layouts ~ Maximum Number of Items -->
            <div id="maxnum_help" class="wp-dialog" >
                 <p>The "Maximum number of items" controls the number of rows appearing in the upload form.</p>
                 <p>If you check "Overrideable by webforms", then any webform using this layout may change this number.
                 <p>If a webform overrides the "Maximum number of items", any maximum numbers set for individual drop-down selection
                     fields are <b>not</b> affected.  So if you set such maxima on individual fields, think carefully before checking
                     "Overrideable by webforms".</p>
                 <p>If some values in the "Maximum number of items" selection list are disabled, it it because
                    you have set a larger maximum for one of the options in a drop-down field.</p>
            </div>

            <!-- HELP POPUP Layouts ~ Notes -->
            <div id="notes_help" class="wp-dialog" >
                 <p>You may ignore this input, or use it for notes as you like.  It has no effect, but is simply displayed here.</p>
                 <p>The input area normally shows only one line, but may be resized as you like.</p>
            </div>

            <!-- HELP POPUP Layouts ~ All Items -->
            <div id="all_items_help" class="wp-dialog" >
                 <p>( Since version 1.3.0 ) If this option is checked, the input is displayed only once, at the top of the webform,
                    and the same value is assigned to each item in the webform.</p>
                 <p>This may be useful in cases where a set of images is to be uploaded, and some overall title/option/category is to be
                    given to the set.</p>
                 <p>This option may not be used for image files.</p>
            </div>
            <!-- HELP POPUP Layouts ~ Fields -->
            <div id="field_help" class="wp-dialog" >
                <p>The <b>shortcode</b> "ewz_show_webform" generates a webform.  The form appears as a table with
                     the number of rows equal to the "maximum number of items" set above, and with
                     (normally -- see "Append to Previous Column in Webform" for an exceptional case )
                     a column for each field you create. <br>
                     Each field may be a text input, an image upload, a drop-down selection, a checkbox or
                     a radio button.
                </p>
                <ul>
                   <li> Optionally, other fields may appear above the main table. These are for input that applies to <u>all</u> the items.</li>
                   <li>
                     <p>The <b>order</b> of the fields in the webform may be changed by dragging the "postboxes", which
                     are the long bars containing the field titles, up or down.
                    (To move a postbox to the top you may have to drag the top one down, instead)
                   </li>
                   <li><b>Too many fields, or fields that are too wide</b>, may make a form that is too wide
                    to display on the viewer's monitor.  In that case, the form will display with a
                    scrollbar at the bottom, which is not very user-friendly. <br><br>
                    The width available for the form depends on the Wordpress theme. Some themes make it
                    easy to change the width, others may require some custom coding to do it.<br><br>
                    If your form is too wide, you may wish to compress the layout by using the
                    "Append to Previous Column in Webform" option on some fields.<br><br>
                    When designing your layout, keep testing the view with different window widths and font sizes.
                    If possible, view it on different monitors with different resolutions.<br><br>
                </li>
                <li> If a <b>restriction</b> has been created that forbids a certain combination of field
                    values, fields affected may no longer be deleted, and their type may no longer be changed.
                    If a restriction involves one or more options in a drop-down field, those options
                    may not be deleted, and their values may not be changed.<br><br>
                    Items that may no longer be changed for this reason are outlined in red.
                </li>
               </ul>
            </div>

      <!-- HELP POPUP Layouts ~ Compressed View -->
       <div id="lcompressed_help" class="wp-dialog ewz-help" >
           <p>If you have a large number of layouts, you may prefer a more
            condensed view when rearranging them, so you can see more at once.</p>
            Selecting this will remove some of the vertical spacing.
           <p> Note: this option is not
            saved, so you will need to re-select every time you open the page.
          </p>
       </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Column Header for Web Page -->
            <div id="fhead_help" class="wp-dialog" >
                <p>This will appear as a header for the field column on the page the user sees.</p>
                <p>If you have many fields, you may need to keep the total width of your form in
                    mind when setting this value.</p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Data Type -->
            <div id="ftype_help" class="wp-dialog" >
                <p>Each field contains one of the following types of user input:
                    <ol>
                        <li><b>A text input:</b> The user may input any ( single-line ) piece of text,
                            subject only to the length restrictions you set</li>
                        <li><b>An image file:</b> The user will be asked to select a file from their
                            local machine.  You may restrict various image parameters, such a size
                            and dimensions.</li>
                        <li><b>A drop-down option list:</b> You choose the values that appear.
                            The user must select one of these values.
                            Optionally, you may set a maximum on the number of items that may have a
                            particular value selected </li>
                        <li><b>A checkbox:</b>
                            The user may either check the box or leave it blank.
                            <p>Note that "Requiring" a checkbox means requiring it to be checked.
                            In that case, the checkbox may not have a "Maximum number that may be checked", and that item will be disabled.</p>
                            <p>( It may sometimes be more useful to use a drop-down with options "Yes" and "No" instead of a checkbox. )</p>
                        </li>
                        <li><b>A radio button:</b>
                            The user may "check" the button for <b>just one item</b>, no more.
                            Clicking a "checked" button unchecks it.</li>
                        <li><b>A fixed-text label:</b>
                            This is not actually a user input, and you may never have a need for it. <br>
                            If you choose to display it to the user, it will show the same value in every row.<br>
                            It's main purpose is to generate a fixed custom column in the downloaded
                            spreadsheet, without the user having to enter anything. The column will contain, in every row,
                            the text entered for "Label".<br>
                            </li>
                    </ol>
                </p>

                <p>Users may alter the data they enter so long as the webform is open for uploads.
                   Text, Dropdown, Radio and Checkbox data may be changed, but the only way to alter uploaded
                   images is to delete the entire item and re-submit.  <i>Multiple image fields for an item must all be
                   uploaded at the same time.</i></p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Field Identifier -->
            <div id="sshead_help" class="wp-dialog" >
                <p>This identifier is used for the header that appears at the top of the column
                    if this field is included in your spreadsheet.
                </p>
                <p>If the field is an image file, you may also (optionally) choose to include it
                    as part of the filename when you download the image.
                </p>
                <p>It must start with a letter and consist only of letters, digits, dashes or
                    underscores - no spaces or special characters. No more than 15 characters.
                    This makes sure that a filename containing it should be accepted by most
                    systems.
                </p>
                <hr />
                <b><i> For users of the ewz_followup shortcode only</i></b>
                <p> The ewz_followup shortcode is designed to give a user an overview of all the items they have  uploaded
                    to a set of webforms, together with one optional additional field ( textbox, checkbox, etc ).<br />
                    <br /><br />
                   There are certain special values for the identifier that will cause a change in EntryWizard's behaviour.
                   <ul>
                     <li> If the identifier has the special value 'followupQ', the field is treated as a
                     "followup" field, which is not displayed by the 'ewz_show_webform' shortcode.
                     It is the <u>only input</u> field shown by the 'ewz_followup' shortcode.</li>
                     <li> If the field contains 'XFQ' as part of it's identifier, the field will <b>not</b> be displayed by
                     the followup shortcode.</li>
                   </ul>
                     There is further information on using the 'ewz_followup' shortcode at the  top of the Webforms page.
                </p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Spreadsheet Column -->
            <div id="sscol_help" class="wp-dialog" >
                <p>If you wish the data in this column to appear in the spreadsheet, specify here
                     the column in which it is to appear. If you select 'None', the item will not appear
                     in the spreadsheet at all.
                </p>
                <p>EntryWizard will not allow you to select a column you have already chosen for
                    another field
                </p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Required -->
            <div id="req_help" class="wp-dialog" >
                <p>Check this box if you wish to insist that the field is filled out by everyone.
                   Leave it unchecked if the field is optional.<br>
                    ( The box is disabled for radiobutton and checkbox fields, since there is only
                      one possible way to fill them out. )
                </p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Append To Previous Column -->
            <div id="append_help" class="wp-dialog" >
              <p>If your theme does not allow a sufficiently wide page width, a webform ( after clicking "Add, Change or Delete" ) may
              display only with a horizontal scrollbar.</p>
              <p>To avoid this, you may wish to display more than one field in a single table cell.
                 Checking "Append to Previous Column in Webform" will accomplish this.
                 Each field so checked will be displayed in a extra line in the previous column, instead of in a column of its own.</p>
              <p>For the first column, this option is ignored.</p>
              <p>NOTE: the "append" option may <u>not</u> be applied to fields that have "Applies To All Items" checked.</p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Image Field ~ Image Dimensions -->
            <div id="imgdim_help" class="wp-dialog" >
                 <p>A user attempting to upload an image with width or height greater than the maxima set here will receive an error message.</p>
                 <p>See also the help items on Maximum image size and Minimum longest dimension</p>
            </div>


            <!-- HELP POPUP Layouts ~ Fields ~ Image Field ~ Can Rotate To Fit -->
            <div id="rot_help" class="wp-dialog" >
                <p>If this is checked, and you have set a maximum width of 1280 and a maximum
                    height of 1024, an image with height 1280 and width 1024 will also be accepted.
                </p>
                <p>May be useful if the image is to be printed, or displayed on a rotatable monitor.
                </p>
            </div>


            <!-- HELP POPUP Layouts ~ Fields ~ Image Field ~ Must Be As Large As Possible -->
            <div id="mustfill_help" class="wp-dialog" >
                <p>If this is checked, images are required to have <u>either</u> a width equal to your maximum allowed width
                   <u>or</u> a height equal to your maximum allowed height.
                </p>
                <p>Note that the checking done here allows for the "margin of error" in your Settings.
                If checking this option, you may wish to set this margin to 1 or 2. We have found some software in use that does not always
                resize an image to the exact width or height requested.
                </p>
            </div>


            <!-- HELP POPUP Layouts ~ Fields ~ Image Field ~ Maximum Image Size -->
            <div id="imgsize_help" class="wp-dialog" >
                <p>Your web hosting company also puts three limits on uploaded items:</p>
                    <ol><li>No individual item may be bigger than <?php print $ewzG['maxUploadMb']; ?></li>
                        <li>The total size of the whole upload including all items may not be bigger than
                            <?php print $ewzG['maxTotalMb']; ?></li>
                        <li>The maximum number of items that may be uploaded simultaneously is
                            <?php print $ewzG['maxNumitems']; ?></li>
                    </ol>
                <p>For the technically-minded, these limits are set in <i>php.ini</i>, and more
                    details may be obtained from the php documentation website
                    http://php.net/manual/en/ini.core.php
                </p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Image Field ~ Minimum Longest Dimension -->
            <div id="longestdim_help" class="wp-dialog" >
                <p>The purpose of this setting is to disallow very small images when they could be enlarged within the maximum width and height limitations.</p>
                <p>(An alternative method of doing this would be to check "Image must be as large as possible" and then set the "Pixels of leeway" in your settings to a fairly large value.)</p>
                <p>If not set, this value will default to <?php print EWZ_DEFAULT_MIN_LONGEST; ?></p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Image Field ~ Allowed Image Types -->
            <div id="imgtype_help" class="wp-dialog" >
                <p>Wordpress currently is able to handle only the following image types:
                    <ol><li>.jpg/.jpeg</li>
                        <li>.png</li>
                        <li>.gif</li>
                    </ol>
                </p>
                <p>To aid in identifying files, EntryWizard saves images with filenames derived from the user's
                    original filenames in the following way:
                    <ol><li> All characters except letters, digits, underscores, dashes,
                             and all periods except the last,  are replaced by '_' (or the alternative  you select in your settings).
                             Note that <u>spaces are replaced</u>.
                             This ensures the filename is acceptable on most hosting systems. </li>
                        <li> If the base filename then ends with a digit, another '_' is appended.
                            This makes extra digits added by Wordpress in the case of duplicate
                            filenames more legible. It also ensures that no filename is totally
                            numeric - Wordpress handles files with completely numeric names
                            differently in some cases.</li>
                    </ol>
                </p>
            </div>


            <!-- HELP POPUP Layouts ~ Fields ~ Drop-Down Selection ~ Select Options -->
            <div id="opt_help" class="wp-dialog" >
                <p>For each option you create, you need to select:
                    <ul><li><b>Label for Web Page:</b> the label that is displayed in the drop-down
                            list the user selects from.<br>
                            If you have many fields, you may need to keep the total width of your
                            form in mind when setting this value.<br>
                            May contain only letters, digits, dashes, underscores</li>
                        <li><b>Value for Spreadsheet:</b> what you see in the corresponding
                            spreadsheet column when the user selects this item.<br>
                            May contain only letters, digits, dashes, underscores<br>
                            (Behind the scenes, this is also the value stored in the database)<br>
                     <i>Hint:</i> When you set the Label, if the Value has not already been set it defaults to that of the Label.
                     If you want different values for Label and Value, try setting the Value first.
                            </li>
                        <li><b>Maximum Number Allowed:</b> If you enter a number N here,
                            no more than N items uploaded by this user may have the matching value.<br>
                            This allows for such rules as "6 images may be submitted, no more than
                            4 may be prints."<br>
                            You cannot set a maximum here that is greater than the "Maximum number
                            of items" set in the "General Information" section. </li>
                    </ul>
                    Use the 'X' button to delete an option.
                </p>
                <p>To create a list of numeric options, like "1,2,3,4,5"  or "5.0, 5.5, 6.0 ...", you may use the "Add Series" button:
                   <ul><li>Start, Step and End must all be numeric</li>
                       <li>Step cannot be zero.  If Step is positive, End must be greater than Start.
                           If Step is negative, End must be less than Start. </li>
                       <li>If any of Start, Step or End contains a decimal point, all values will be shown with the
                           highest number of decimal places in any of the inputs.</li>
                       <li>Stored value will always be the same as Label</li>
                       <li>If the max is set, it will be the same for all options</li>
                   </ul>

            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Text Field ~ Maximum Number of Characters -->
            <div id="maxchar_help" class="wp-dialog" >
                <p>The user will not be able to type more than this many characters in the text box.
                </p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Text Field ~ Maximum Number of Visible Characters -->
            <div id="maxvis_help" class="wp-dialog" >
                <p>For a regular single-line text box, this is the number of characters that will be visible
                   at any one time - ie the width of the text box.
                </p>
                <p>If you need many more characters
                than the width of your form allows, try setting the "Number of Lines" to something
                bigger than 1. That creates a "textarea" instead of a normal text input. In that case,
                this value represents the initial width in characters of the (resizeable) text area.
                </p>
                <p>If you have many fields, you may need to keep the total width of your form in mind
                    when setting this value.
                </p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Text Field ~ Number of Lines -->
            <div id="textrows_help" class="wp-dialog" >
                <p>A normal html text input is a single line only. Setting this value to something
                greater than 1 creates a "textarea" instead, with the assigned number of rows, and
                number of columns equal to the "Number of Characters Visible in Input". </p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Text Field ~ Formatted Text -->
            <div id="frmtstr_help" class="wp-dialog" >
                <p>FOR SINGLE_LINE TEXT INPUTS ONLY, if a column is selected, that column in the spreadsheet
                    will contain this item re-formatted so that each word starts with a capital letter.
                </p>
                <p>Useful for titles used in displays.
                </p>
                <p>( The text is shown unchanged if it already contains both upper and
                    lower-case characters )
                </p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Checkbox Field ~ Maximum Number That May Be Checked -->
            <div id="chkmax_help" class="wp-dialog" >
                <p>The maximum number of items that may have this checkbox checked.
                </p>
            </div>
            <!-- HELP POPUP Layouts ~ Fields ~ Checkbox Field ~ Label To Display When Checked -->
            <div id="chklabel_help" class="wp-dialog" >
                <p>When the data is displayed to the user, the default is to
                   display "checked" if the checkbox is checked, and otherwise "-".  You may enter any other text to display instead.
                </p>
            </div>

            <!-- HELP POPUP Layouts ~ Fields ~ Radiobutton Field ~ Label To Display When Checked -->
            <div id="radlabel_help" class="wp-dialog" >
                <p>When the data is displayed to the user, the default is to
                   display "checked" if the radio button is checked, and otherwise "-".  You may enter any other text to display instead.
                </p>
            </div>
            <!-- HELP POPUP Layouts ~ Fields ~ Label Field ~ Label -->
            <div id="textlabel_help" class="wp-dialog" >
                <p>The value to display for the field.
                </p>
            </div>
            <!-- HELP POPUP Layouts ~ Fields ~ Label Field ~ Show Label -->
            <div id="showlabel_help" class="wp-dialog" >
                   <p>Show the label as a fixed text field in the webform.
                      If this is not checked, the field is hidden from the user, but appears in the downloaded spreadsheet
                      if the Spreadsheet Column is set.
                  </p>
                  <p>Hint: to avoid showing a blank table-cell for a field that is hidden from the user, append the field to the previous one. </p>
            </div>

            <!-- HELP POPUP Layouts ~ Saving -->
            <div id="lsave_help" class="wp-dialog" >
                      <p>Add a New Layout creates a new Layout copied from the one you select ( except for any restrictions ), and adds it to
                      the top of the list.
                  You then need to at least give it a name  before saving it.
                  ( Of course, there would be little point in doing this unless you were going to make other changes, too )</p>
            </div>

            <!-- HELP POPUP Layouts ~ Sorting -->
            <div id="lsort_help" class="wp-dialog" >
               <p>Layouts may be dragged to rearrange them.
                  Clicking "Save Order of Layouts" will save the order, and
                  the new order will subsequently be used for this page, and for the dropdown menus of layouts in the webforms page</p>
            </div>

            <!-- HELP POPUP Layouts ~ Restrictions -->
            <div id="restr_help" class="wp-dialog" >
                <p>Normally, any combination of allowed field values is allowed.
                    There may, however, be occasions where you wish to disallow some particular combination.
                </p>
                <p>For instance, you may have a field <i>Type</i> with values <i>Digital</i>,
                    <i>Print</i> or <i>Slide</i>, and a field for an uploaded image <i>File</i>.
                    In this case, you may need to require that a <i>Digital</i> type must have a
                    <i>File</i>, although it may be optional for <i>Print</i> or <i>Slide</i>
                </p>
                <p>To do this, you may create a restriction forbidding the combination
                    <i>Type=Digital</i> and <i>File=Blank</i>, with the message
                    "A Digital type must have an uploaded image file".
                </p>
                <p>If a user clicks "Save Changes" when any item has one of these forbidden combinations,
                    your message will pop up and the upload will not work.
                </p>
                <p><br><b>Set up all your fields and click "Save Changes" first before adding
                        any restrictions</b>.  Once a field has had a restriction placed on it,
                        some items within the field may no longer be edited. These fields are
                        indicated by a red outline. If you need to change them, you must first
                        delete the restriction.
                </p>
                <p>NOTE: restrictions will <b>not</b> be enforced on "followup" fields ( fields with the
                   special identifier "followupQ" ).  For further information about followup fields
                   see the help button at the top of the webforms page.
                </p>
            </div>

            <!-- HELP POPUP Layouts ~ Restrictions ~ Restriction Message -->
            <div id="rmsg_help" class="wp-dialog" >
                <p>If a user attempts to submit a webform with the forbidden combination of
                    field values, the upload will fail and this message will be shown.</p>
            </div>


            <!-- HELP POPUP Layouts ~ Extra Information -->
            <div id="xtra_help" class="wp-dialog" >
                <p>In addition to the information entered by members, you may include several
                    other items of information in the spreadsheet that you download.
                </p>
                <p>The source of the data is shown in parentheses.
                     <ul><li><b>"WP User data"</b> is information you control via the Wordpress Users menu.
                          <u>Note that this data is only visible to admins with the right permission</u>.</li>
                      <li><b>"EWZ Item data"</b> is information about an item stored by this plugin but not
                          actually set by the user.<br>
                          "Attached To" contains a record of the page title the image was attached to via the entrywizard interface. If further
                              changes were made via WordPress, they are <b>not</b> reflected here.<br>

                          The "EWZ Item data" may include data uploaded by the administrator via a .csv file
                          ( See the Data Management area on the WebForms page ).<br />
                           <i>NB for version 1.2.21: to make the terminology match that of the new WP gallery editor, the term "excerpt"
                            has been replaced by "caption", and "content" by "description".</i><br />
                          The "WP Item ID" is a numeric identifier created by Wordpress.  You will need this
                          if you wish to upload such a .csv file.
                                     </li>
                     <li><b>"EWZ Webform data"</b> is mainly set by the administrator on the WebForms page.
                          The WP Webform ID is a numeric identifier created by Wordpress.</li>
                     <li><b>"Custom data"</b> is optional. Wordpress stores some information about a
                         user, but there are plugins, like CIMI User Extra Fields or S2Member,  that allow you
                         to add more information.<br>
                         If you have such a plugin, and if it provides a function to access the
                         information, you may tell EntryWizard about it -- see the "ewz-extra.txt"
                         file.</li>
                  </ul>
               </p>
            </div>

        </div>
    </div> <!-- wrap -->



    <?php
    } catch( Exception $e ){
            wp_die( $e->getMessage() );
    }
}