<?xml version="1.0" encoding="UTF-8"?>
<!--
 -
 -  $Id$
 -
 -  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 -  project.
 -
 -  Copyright (C) 1998-2018 OpenLink Software
 -
 -  This project is free software; you can redistribute it and/or modify it
 -  under the terms of the GNU General Public License as published by the
 -  Free Software Foundation; only version 2 of the License, dated June 1991.
 -
 -  This program is distributed in the hope that it will be useful, but
 -  WITHOUT ANY WARRANTY; without even the implied warranty of
 -  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 -  General Public License for more details.
 -
 -  You should have received a copy of the GNU General Public License along
 -  with this program; if not, write to the Free Software Foundation, Inc.,
 -  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 -
-->

<v:page name="channels_create" decor="template/template.vspx" style="template/template.xsl" fast-render="1" button-anchors="1" xmlns:v="http://www.openlinksw.com/vspx/" xmlns:vm="http://www.openlinksw.com/vspx/macro" doctype="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <v:method name="check_url" arglist="in url varchar, in url2 varchar, in type integer, in msg any">
    <![CDATA[
      declare exit handler for sqlstate '*' { goto _error; };
      declare retValue any;

      retValue := '';
      self.v_data := null;
      url := trim(url);
      if (is_empty_or_null(url))
      {
        retValue := sprintf('Please enter correct %s', msg);
        goto _error;
      }

      if (exists(select 1 from ENEWS.WA.FEED_DOMAIN, ENEWS.WA.FEED where EFD_DOMAIN_ID = self.domain_id and EFD_FEED_ID = EF_ID and EF_URI = url2))
      {
        retValue := sprintf('There is already subscription for this feed (%s).', url2);
        goto _error;
      }

      if (type = 0)
      {
        self.v_data := ENEWS.WA.channels_uri(url2);
      } else if (type = 1) {
        self.v_data := ENEWS.WA.channels_content(url);
      } else if (type = 2) {
        self.v_data := ENEWS.WA.channels_uri(url2, 2, ENEWS.WA.account_name (self.account_id));
      } else if (type = 3) {
        url2 := file_to_string(url);
        self.v_data := ENEWS.WA.channels_content(url2);
      }
    _error:

      if (retValue = '')
        if (is_empty_or_null(self.v_data))
        {
        retValue := sprintf('Unknown %s.', msg);
      } else if (length(self.v_data) <= 1) {
        retValue := 'Feed(s) source is bad or empty.';
      } else if ((type <> 0) and (self.v_data[0] <> 'OPML') and (self.v_data[0] <> 'OCS')) {
        retValue := 'Feed(s) source type must be OPML or OCS.';
      }

      if (retValue <> '')
      {
        self.vc_error_message := retValue;
        self.vc_is_valid := 0;
        self.v_data := null;
      } else {
        if (self.v_data[0] = 'links')
          self.v_step := '2';
        if (self.v_data[0] = 'channel')
          self.v_step := '3';
        if (self.v_data[0] = 'OPML')
          self.v_step := '3.1';
        if (self.v_data[0] = 'OCS')
          self.v_step := '3.2';
        if (self.v_data[0] = 'Checked')
          self.v_step := '3.3';
        self.v_url := '';
      }
      return retValue;
    ]]>
  </v:method>
  <v:method name="source_label" arglist="in type integer">
    <![CDATA[
      if (type = 0)
        return 'From URL (RSS/Atom/OPML/OCS/SIOC ...)';
      if (type = 1)
        return 'Upload (OPML, OCS)';
      if (type = 2)
        return 'Upload from WebDAV (OPML, OCS)';
      if (type = 3)
        return 'From directory of known (popular) feeds';
      return '';
    ]]>
  </v:method>
  <v:method name="directoryTreeLinks" arglist="in id integer">
    <![CDATA[
      declare name varchar;
      declare retValue any;

      retValue := vector();
      while (not is_empty_or_null(id))
      {
        name := (select ED_NAME from ENEWS.WA.DIRECTORY where ED_ID = id);
        retValue := vector_concat(vector(vector(concat(name, ' /'), id)), retValue);
        id := (select ED_PARENT_ID from ENEWS.WA.DIRECTORY where ED_ID = id);
      }
      retValue := vector_concat(vector(vector('/', 0)), retValue);
      return retValue;
    ]]>
  </v:method>

  <v:method name="pshCheck" arglist="">
    <![CDATA[
      return case when ENEWS.WA.pshCheck () and not isnull (self.v_psh) then 1 else 0 end;
    ]]>
  </v:method>

  <vm:pagetitle>Feed add</vm:pagetitle>
  <vm:pagewrapper>
    <vm:header/>
    <vm:variables>
      <v:variable name="v_url" type="varchar" param-name="URL" default="''"/>
      <v:variable name="v_title" type="varchar" default="''"/>
      <v:variable name="v_folder_id" type="varchar" default="'0'"/>
      <v:variable name="v_folder_name" type="varchar" default="''"/>
      <v:variable name="v_psh" type="varchar" default="null"/>
      <v:variable name="v_psh_enabled" type="integer" default="1"/>
      <v:variable name="v_source" type="integer" default="-1" persist="0"/>
      <v:variable name="v_retrieve" type="integer" default="1" persist="0"/>
      <v:variable name="v_mfolder_id" type="varchar" default="null"/>
      <v:variable name="v_mfolder_name" type="varchar" default="null"/>
      <v:variable name="v_step" type="varchar" default="'0'" persist="0"/>
      <v:variable name="v_data" type="varchar" default="null" persist="temp"/>
      <v:variable name="v_rb" type="integer" param-name="rb" default="0"/>
      <v:variable name="v_directory_id" type="integer" default="0" persist="0"/>
    </vm:variables>
    <vm:pagebody>
      <v:before-render>
        <![CDATA[
          self.F1.uf_action := ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels_create.vspx'));
        ]]>
      </v:before-render>
      <v:before-data-bind>
        <![CDATA[
          declare params any;

          params := self.vc_page.vc_event.ve_params;
          if ((self.v_source = -1) and (self.v_url <> ''))
            self.v_source := 0;
          if (isnull(self.v_data))
            self.v_data := vector('');
          self.v_title := trim(get_keyword ('f_title', params));
          if (isnull(self.v_title) and length(self.v_data) > 1)
            self.v_title := get_keyword('title', self.v_data[1], '');
          self.v_psh := null;
          if (length(self.v_data) > 1)
            self.v_psh := get_keyword ('psh', self.v_data[1]);
          self.v_psh_enabled := atoi (get_keyword ('f_psh_enabled', params, '1'));
          if (not self.pshCheck())
            self.v_psh_enabled := 0;
          self.v_folder_id := trim(get_keyword ('f_folder_id', params, '0'));
          self.v_folder_name := trim(get_keyword ('f_folder_name', params, ''));
          if (self.v_source = 3)
          {
            declare N integer;
            N := cast (get_keyword ('directory', params, '-1') as integer);
            if (N <> -1)
              self.v_directory_id := N;
          }
        ]]>
      </v:before-data-bind>

      <v:template type="simple" enabled="-- case when (self.v_step = '0') then 1 else 0 end">
        <div class="form-header">
          Select feed source
        </div>
        <div class="new-form-body">
          <table cellspacing="0">
            <tr>
              <th>
                Select feed(s) source
              </th>
              <td nowrap="nowrap">
                <v:radio-button name="rb_0" xhtml_id="rb_0" value="0" group-name="choice_source" initial-checked="--either(lte(self.v_rb,0),1,0)" xhtml_title="--self.source_label(0)" />
                <vm:label for="rb_0" value="--self.source_label(0)" />
                <br />
                <v:radio-button name="rb_1" xhtml_id="rb_1" value="1" group-name="choice_source" initial-checked="--either(equ(self.v_rb,1),1,0)" xhtml_title="--self.source_label(1)" />
                <vm:label for="rb_1" value="--self.source_label(1)" />
                <br />
                <v:radio-button name="rb_2" xhtml_id="rb_2" value="2" group-name="choice_source" initial-checked="--either(equ(self.v_rb,2),1,0)" xhtml_title="--self.source_label(2)" />
                <vm:label for="rb_2" value="--self.source_label(2)" />
                <br />
                  <v:radio-button name="rb_3" xhtml_id="rb_3" value="3" group-name="choice_source" initial-checked="--either(equ(self.v_rb,3),1,0)" xhtml_title="--self.source_label(3)" />
                  <vm:label for="rb_3" value="--self.source_label(3)" />
              </td>
            </tr>
          </table>
        </div>
        <div class="form-footer">
          <v:button action="simple" name="Next0" value="Next" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                if (self.rb_0.ufl_selected)
                {
                  self.v_source := 0;
                } else if (self.rb_1.ufl_selected) {
                  self.v_source := 1;
                } else if (self.rb_2.ufl_selected) {
                  self.v_source := 2;
                } else if (self.rb_3.ufl_selected) {
                  self.v_source := 3;
                  self.v_directory_id := 0;
                }
                self.v_step := '1';
                self.vc_data_bind(e);
              ]]>
            </v:on-post>
          </v:button>
          <v:button action="simple" name="Cancel0" value="Cancel" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels.vspx')));
              ]]>
            </v:on-post>
          </v:button>
        </div>
      </v:template>

      <v:template type="simple" enabled="-- case when ((self.v_step = '1') and (self.v_source <> 3)) then 1 else 0 end">
        <div class="form-header">
          <v:label format="%s" value="--'Subscribe feed(s)'" />
        </div>
        <div class="new-form-body">
          <table cellspacing="0">
            <tr>
              <th nowrap="nowrap">
                <v:label value="--concat(self.source_label(self.v_source))" />
              </th>
              <td valign="center" nowrap="nowrap">
                <v:template type="simple" enabled="-- case when (self.v_source = 0) then 1 else 0 end">
                  <input type="text" name="source_0" value="<?V self.v_url ?>" size="70"></input>
                </v:template>
                <v:template type="simple" enabled="-- case when (self.v_source = 1) then 1 else 0 end">
                  <input type="file" name="source_1" size="70"></input>
                </v:template>
                <v:template type="simple" enabled="-- case when (self.v_source = 2) then 1 else 0 end">
                  <v:text name="source_2" xhtml_id="source_2" value="" xhtml_size="70" />
                  <input type="button" id="source_2_button" value="Browse..." onclick="davBrowse ('source_2');" />
                 		  <![CDATA[
                	  <script type="text/javascript">
                      OAT.Loader.load(['dav'], function(){OAT.WebDav.init(davOptions);});
                	  </script>
            			    ]]>
                </v:template>
                <v:button action="simple" value="Subscribe" xhtml_alt="Subscribe feed" enabled="-- case when (self.v_source = 0) then 1 else 0 end">
                  <v:on-post>
                    <![CDATA[
                      declare retValue varchar;

                      self.v_url := trim(get_keyword('source_0', e.ve_params, ''));
                      retValue := self.check_url(self.v_url, self.v_url, 0, 'feed(s) URL');
                      if (retValue <> '')
                      {
                        --self.vc_error_message := retValue;
                        self.vc_is_valid := 0;
                        self.v_data := null;
                      }
                      if (retValue = '')
                        self.vc_data_bind(e);
                    ]]>
                  </v:on-post>
                </v:button>
              </td>
            </tr>
            <v:template type="simple" enabled="-- case when (self.v_source = 1) or (self.v_source = 2) then 1 else 0 end">
              <tr>
                <th>
                  <v:label for="f_mfolder_name" value="Feed folder"/>
                </th>
                <td>
                  <v:text name="f_mfolder_name" null-value="--''" value="--self.v_folder_name" xhtml_class="textbox" xhtml_size="40%"/>
                  <v:data-list name="f_mfolder_id" value="--self.v_folder_id" sql="select 0 as EFO_ID, 'Select folder ...' as EFO_PATH2, '' as EFO_PATH from WS.WS.SYS_DAV_USER where U_NAME = 'dav' union all select EFO_ID, ENEWS.WA.folder_path2(EFO_DOMAIN_ID, EFO_ID) as EFO_PATH2, ENEWS.WA.folder_path(EFO_DOMAIN_ID, EFO_ID) as EFO_PATH from ENEWS.WA.FOLDER where EFO_DOMAIN_ID = self.domain_id order by EFO_PATH" key-column="EFO_ID" value-column="EFO_PATH2" xhtml_class="select"/>
                </td>
              </tr>
            </v:template>
            <tr>
              <th />
              <td>
                 <v:check-box name="f_retrieve" xhtml_id="retrieve" is-boolean="1" true-value="1" false-value="0" value="--self.v_retrieve" />
                 <b><vm:label for="retrieve" value="Retrieve items now"/></b>
              </td>
            </tr>
            <v:template type="simple" enabled="-- case when (self.v_source = 0) then 1 else 0 end">
              <tr>
                <td align="center" valign="center" colspan="2" style="border: solid #935000; border-width: 1px 0px 1px 0px;">
                  <br/>
                  <b>Use shortcuts below to subscribe to Blogspot blogs, LiveJournals, Xangas, Topix news, Google Groups and Yahoo Groups.</b>
                  <br/><br/>
                </td>
              </tr>
              <tr>
                <th>
                  <a href="http://www.blogspot.com"><img border="0" src="image/blogspot.gif"/></a>
                </th>
                <td nowrap="nowrap">
                  <v:text name="blogspot" value="" xhtml_size="70%"/>
                  <v:button action="simple" value="Blogspot User">
                    <v:on-post>
                      <![CDATA[
                        declare retValue varchar;

                        self.v_url := trim(self.blogspot.ufl_value);
                        retValue := self.check_url(self.v_url, sprintf('http://%s.blogspot.com/atom.xml', self.v_url), 0, 'Blogspot User');
                        if (retValue = '')
                          self.vc_data_bind(e);
                      ]]>
                    </v:on-post>
                  </v:button>
                </td>
              </tr>
              <tr>
                <th>
                  <a href="http://www.livejournal.com"><img border="0" src="image/livejournal.gif"/></a>
                </th>
                <td nowrap="nowrap">
                  <v:text name="livejournal" value="" xhtml_size="70%"/>
                  <v:button action="simple" value="LiveJournal User">
                    <v:on-post>
                      <![CDATA[
                        declare retValue varchar;

                        self.v_url := trim(self.livejournal.ufl_value);
                        retValue := self.check_url(self.v_url, sprintf('http://www.livejournal.com/users/%s/data/rss', self.v_url), 0, 'LiveJournal User');
                        if (retValue = '')
                          self.vc_data_bind(e);
                      ]]>
                    </v:on-post>
                  </v:button>
                </td>
              </tr>
              <tr>
                <th>
                  <a href="http://www.xanga.com"><img border="0" src="image/xangalogo.gif"/></a>
                </th>
                <td nowrap="nowrap">
                  <v:text name="xanga" value="" xhtml_size="70%"/>
                  <v:button action="simple" value="Xanga User">
                    <v:on-post>
                      <![CDATA[
                        declare retValue varchar;

                        self.v_url := trim(self.xanga.ufl_value);
                        retValue := self.check_url(self.v_url, sprintf('http://www.xanga.com/rss.aspx?user=%s', self.v_url), 0, 'Xanga User');
                        if (retValue = '')
                          self.vc_data_bind(e);
                      ]]>
                    </v:on-post>
                  </v:button>
                </td>
              </tr>
              <tr>
                <th>
                  <a href="http://www.topix.net/"><img border="0" src="image/topix.gif"/></a>
                </th>
                <td nowrap="nowrap">
                  <v:text name="topix" value="" xhtml_size="70%"/>
                  <v:button action="simple" value="Topix Zipcode">
                    <v:on-post>
                      <![CDATA[
                        declare retValue varchar;

                        self.v_url := trim(self.topix.ufl_value);
                        retValue := self.check_url(self.v_url, sprintf('http://rss.topix.net/search/?q=%s&xml=1', self.v_url), 0, 'Topix Zipcode');
                        if (retValue = '')
                          self.vc_data_bind(e);
                      ]]>
                    </v:on-post>
                  </v:button>
                </td>
              </tr>
              <tr>
                <td class="td_label">
                  <a href="http://groups.yahoo.com/"><img border="0" src="image/ygroups.gif"/></a>
                </td>
                <td nowrap="nowrap">
                  <v:text name="yahoo" value="" xhtml_size="70%"/>
                  <v:button action="simple" value="Yahoo Group">
                    <v:on-post>
                      <![CDATA[
                        declare retValue varchar;

                        self.v_url := trim(self.yahoo.ufl_value);
                        retValue := self.check_url(self.v_url, sprintf('http://rss.groups.yahoo.com/group/%s/rss', self.v_url), 0, 'Yahoo Group');
                        if (retValue = '')
                          self.vc_data_bind(e);
                      ]]>
                    </v:on-post>
                  </v:button>
                </td>
              </tr>
              <tr>
                <th>
                  <a href="http://groups-beta.google.com/"><img border="0" src="image/groups2beta.gif"/></a>
                </th>
                <td nowrap="nowrap">
                  <v:text name="google" value="" xhtml_size="70%"/>
                  <v:button action="simple" value="Google Group">
                    <v:on-post>
                      <![CDATA[
                        declare retValue varchar;

                        self.v_url := trim(self.google.ufl_value);
                        retValue := self.check_url(self.v_url, sprintf('http://groups-beta.google.com/group/%s/feed/msgs.xml', self.v_url), 0, 'Google Group');
                        if (retValue = '')
                          self.vc_data_bind(e);
                      ]]>
                    </v:on-post>
                  </v:button>
                </td>
              </tr>
            </v:template>
          </table>
        </div>
        <div class="form-footer">
          <v:button action="simple" name="Back1" value="Back" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, sprintf ('channels_create.vspx?rb=%d', self.v_source))));
              ]]>
            </v:on-post>
          </v:button>
          <v:button action="simple" name="Next1" value="Next" xhtml_alt="Next" enabled="-- case when (self.v_source = 0) then 0 else 1 end">
            <v:on-post>
              <![CDATA[
                declare retValue varchar;
                declare url2 varchar;

                self.v_retrieve := self.f_retrieve.ufl_selected;
                self.v_mfolder_name := trim(self.f_mfolder_name.ufl_value);
                self.v_mfolder_id := cast (self.f_mfolder_id.ufl_value as integer);
                if (self.v_mfolder_name <> '')
                {
                  if (not ENEWS.WA.folder_check_name(self.v_mfolder_name, 1))
                  {
                    self.vc_error_message := 'Please, enter other folder name. This name contains bad characters.';
                    self.vc_is_valid := 0;
                    return;
                  }
                }
                if (self.v_source = 1)
                {
                  self.v_url := trim(get_keyword('source_1', e.ve_params, ''));
                  retValue := self.check_url(self.v_url, null, 1, 'local file name for OPML or OCS feeds');
                }
                else if (self.v_source = 2)
                {
                  self.v_url := trim(get_keyword('source_2', e.ve_params, ''));
                  url2 := concat(ENEWS.WA.host_url(), self.v_url);
                  retValue := self.check_url(self.v_url, url2, 2, 'WebDAV file name for OPML or OCS feeds');
                }
                if (retValue = '')
                {
                  self.v_url := '';
                  self.vc_data_bind(e);
                }
              ]]>
            </v:on-post>
          </v:button>
          <v:button action="simple" value="Subscribe" xhtml_alt="Subscribe" enabled="-- case when (self.v_source = 0) then 0 else 1 end">
            <v:on-post>
              <![CDATA[
                declare retValue varchar;
                declare url2 varchar;

                self.v_retrieve := self.f_retrieve.ufl_selected;
                self.v_mfolder_name := trim (self.f_mfolder_name.ufl_value);
                self.v_mfolder_id := cast (self.f_mfolder_id.ufl_value as integer);
                if (self.v_mfolder_name <> '')
                {
                  if (not ENEWS.WA.folder_check_name(self.v_mfolder_name, 1))
                  {
                    self.vc_error_message := 'Please, enter other folder name. This name contains bad characters.';
                    self.vc_is_valid := 0;
                    return;
                  }
                }
                if (self.v_source = 1)
                {
                  self.v_url := trim(get_keyword('source_1', e.ve_params, ''));
                  retValue := self.check_url(self.v_url, null, 1, 'local file name for OPML or OCS feeds');
                }
                else if (self.v_source = 2)
                {
                  self.v_url := trim(get_keyword('source_2', e.ve_params, ''));
                  url2 := concat(ENEWS.WA.host_url(), self.v_url);
                  retValue := self.check_url(self.v_url, url2, 2, 'WebDAV file name for OPML or OCS feeds');
                }
                if (retValue = '')
                {
                  self.v_step := '4';
                  self.v_url := '';
                  self.vc_data_bind(e);
                }
              ]]>
            </v:on-post>
          </v:button>
          <v:button action="simple" name="Cancel1" value="Cancel" xhtml_class="form-button">
            <v:on-post><![CDATA[
              self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels.vspx')));
             ]]></v:on-post>
          </v:button>
        </div>
      </v:template>

      <v:template type="simple" enabled="-- case when ((self.v_step = '1') and (self.v_source = 3)) then 1 else 0 end">
        <div class="form-header">
          Directory of known (popular) feeds
        </div>
        <input type="hidden" name="directory" value="-1"/>
        <div class="new-form-body">
          <table cellspacing="0">
            <tr>
              <td class="td_simple">
                Directory:
                <?vsp
                  declare N, L, needHR integer;
                  declare aLinks any;
                  aLinks := self.directoryTreeLinks(self.v_directory_id);
                  L := length(aLinks);
                  for (N := 0; N < L; N := N + 1)
                    http(sprintf('<a href="#" onclick="javascript: myPost(\'F1\', \'directory\', %d); return false" alt="%s" class="nolink3" title="%s"> %s</a>', aLinks[N][1], rtrim(aLinks[N][0], '/'), rtrim(aLinks[N][0], '/'), aLinks[N][0]));
                  http('<hr />');
                  if (self.v_directory_id)
                  {
                    for (select ED_ID, ED_NAME from ENEWS.WA.DIRECTORY where ED_PARENT_ID = self.v_directory_id order by ED_NAME) do
                    {
                      http(sprintf('<input type="image" src="image/folder_16.png" border="0" onclick="javascript: myPost(\'F1\', \'directory\', %d); return false" alt="%s" title="%s"/>&nbsp;', ED_ID, ED_NAME, ED_NAME));
                      http(sprintf('<a href="#" onclick="javascript: myPost(\'F1\', \'directory\', %d); return false" alt="%s" title="%s" class="nolink3"> %s</a>', ED_ID, ED_NAME, ED_NAME, ED_NAME));
                      http('<br />');
                      needHR := 1;
                    }
                  } else {
                    for (select ED_ID, ED_NAME from ENEWS.WA.DIRECTORY where ED_PARENT_ID is null order by ED_NAME) do
                    {
                      http(sprintf('<input type="image" src="image/folder_16.png" border="0" onclick="javascript: myPost(\'F1\', \'directory\', %d); return false" alt="%s" title="%s"/>&nbsp;', ED_ID, ED_NAME, ED_NAME));
                      http(sprintf('<a href="#" onclick="javascript: myPost(\'F1\', \'directory\', %d); return false" alt="%s" class="nolink3" title="%s"> %s</a>', ED_ID, ED_NAME, ED_NAME, ED_NAME));
                      http('<br />');
                      needHR := 1;
                    }
                  }
                  if (exists(select 1 from ENEWS.WA.FEED_DIRECTORY where EFD_DIRECTORY_ID = self.v_directory_id))
                  {
                    if (needHR)
                      http('<hr />');
                ?>
                <table id="channels" class="FM_grid" cellspacing="0">
                  <thead class="sortHeader">
                    <tr>
                      <th style="text-align: center; background-color: #B0CDE4;">
                        <input type="checkbox" name="cbx_all" value="Select All" title="Select All" onClick="selectAllCheckboxes(this.form, this, 'cb_')"/>
                      </th>
                      <th style="text-align: center; background-color: #B0CDE4; width: 100%;">Title</th>
                      <th style="text-align: center; background-color: #B0CDE4;">Feed folder</th>
                    </tr>
                  </thead>
                  <?vsp
                    declare i integer;
                    i := 0;
                    for (select a.EF_URI,
                                a.EF_TITLE,
                                c.EFD_DOMAIN_ID
                           from ENEWS.WA.FEED a
                                  join ENEWS.WA.FEED_DIRECTORY b on b.EFD_FEED_ID = a.EF_ID
                                    left join ENEWS.WA.FEED_DOMAIN c on c.EFD_FEED_ID = a.EF_ID and c.EFD_DOMAIN_ID = self.domain_id
                          where EFD_DIRECTORY_ID = self.v_directory_id
                          order by EF_TITLE) do
                    {
                  ?>
                    <tr>
                      <td align="center">
                        <?vsp
                          declare S varchar;
                          S := '';
                          if (not isnull(EFD_DOMAIN_ID))
                            S := 'disabled="disabled"';
                          http(sprintf('<input type="checkbox" name="cb_item" %s value="%d" title="%V" />', S, i, EF_TITLE));
                        ?>
                      </td>
                      <td>
                        <?V EF_TITLE ?>
                      </td>
                      <td nowrap="nowrap">
                        <input type="hidden" name="rss_<?=i?>" value="<?V EF_URI ?>"/>
                        <input type="text" name="folder_name_<?=i?>"/>
                        <select class="select" name="folder_id_<?=i?>">
                          <option class="option" value="0"> Select folder ...</option>
                          <?vsp
                            for select EFO_ID, ENEWS.WA.folder_path2(EFO_DOMAIN_ID, EFO_ID) as EFO_PATH2, ENEWS.WA.folder_path(EFO_DOMAIN_ID, EFO_ID) as EFO_PATH from ENEWS.WA.FOLDER where EFO_DOMAIN_ID = self.domain_id order by EFO_PATH do  {
                          ?>
                          <option class="option" value="<?V EFO_ID ?>"><?V EFO_PATH2?></option>
                          <?vsp
                            }
                          ?>
                        </select>
                      </td>
                    </tr>
                  <?vsp
                        i := i + 1;
                      }
                    }
                  ?>
                </table>
                <script type="text/javascript">
                  <![CDATA[
                    coloriseTable('channels');
                  ]]>
                </script>
              </td>
            </tr>
          </table>
        </div>
        <div class="form-footer">
          <v:button action="simple" name="Back2" value="Back" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels_create.vspx?rb=4')));
              ]]>
            </v:on-post>
          </v:button>
          <v:button action="simple" name="Subscribe2" value="Subscribe" xhtml_class="form-button">
            <v:before-render>
              <![CDATA[
                control.vc_enabled := coalesce((select 1 from ENEWS.WA.FEED_DIRECTORY where EFD_DIRECTORY_ID = self.v_directory_id), 0);
              ]]>
            </v:before-render>
            <v:on-post>
              <![CDATA[
                declare i, N, L integer;
                declare feed_id, folder_id integer;
                declare feed_uri, folder_name varchar;
                declare v_data, params any;

                params := e.ve_params;
                L := length(params);
                for (i := 0; i < L; i := i + 2)
                {
                  if (params[i] = 'cb_item')
                  {
                    N := atoi(params[i+1]);
                    feed_uri := get_keyword(sprintf ('rss_%d', N), self.vc_event.ve_params, '');
                    v_data := ENEWS.WA.channel_select(feed_uri);
                    if (length(v_data))
                    {
                      feed_id := ENEWS.WA.channel_create(v_data);

                      folder_name := trim(get_keyword(sprintf ('folder_name_%d', N), self.vc_event.ve_params, ''));
                      if (folder_name <> '')
                        if (not ENEWS.WA.folder_check_name(folder_name, 1))
                        {
                          self.vc_error_message := 'Please, enter other category name. This name contains bad characters.';
                           self.vc_is_valid := 0;
                          return;
                        }

                      folder_id := get_keyword(sprintf ('folder_id_%d', N), self.vc_event.ve_params, '');
                      ENEWS.WA.channel_domain (-1, self.domain_id, feed_id, get_keyword('title', v_data, ''), null, folder_name, folder_id);
                      {
                        declare exit handler for sqlstate '*' { goto _next; };
                        if (ENEWS.WA.channel_feeds(feed_id) = 0)
                          ENEWS.WA.feed_refresh(feed_id);
                        ENEWS.WA.tag_refresh(self.domain_id, self.account_id, 0);
                      }
                    }
                  _next:;
                  }
                }
                self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels.vspx')));
              ]]>
            </v:on-post>
          </v:button>
          <v:button action="simple" name="Cancel2" value="Cancel" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels.vspx')));
              ]]>
            </v:on-post>
          </v:button>
        </div>
      </v:template>

      <v:template type="simple" enabled="-- case when (self.v_step = '2') then 1 else 0 end">
        <div class="form-header">
          Autodiscovered Feeds
        </div>
        <div class="new-form-body">
          <?vsp
            if (length(self.v_data) > 18)
              http('<div style="height: 490px; overflow: auto;">');
          ?>
            <table id="feeds" cellspacing="0">
              <tr>
                <td style="text-align: center; background-color: #B0CDE4;">
                  <input type="checkbox" name="cbx_all" value="Select All" title="Select All" onClick="selectAllCheckboxes(this.form, this, 'cb_')"/>
                </td>
                <th style="text-align: center; background-color: #B0CDE4;">Type</th>
                <th style="text-align: center; background-color: #B0CDE4; width: 100%;">Title</th>
                <th style="text-align: center; background-color: #B0CDE4;">URL</th>
              </tr>
              <?vsp
                declare i, j, n, k integer;
                declare data any;

                n := length(self.v_data);
                for (i := 1; i < n; i := i + 1)
                {
                  data := self.v_data[i];
              ?>
                <tr>
                  <td align="center">
                    <?vsp
                      declare S varchar;
                      S := coalesce((select 'disabled="disabled"' from ENEWS.WA.FEED, ENEWS.WA.FEED_DOMAIN where EF_URI = get_keyword('rss', data, '') and EFD_FEED_ID = EF_ID and EFD_DOMAIN_ID = self.domain_id), '');
                      http(sprintf('<input type="checkbox" name="cb_item" %s value="%d" title="%V" />', S, i, get_keyword('title', data, '')));
                      k := length(data);
                      for (j := 0; j < k; j := j + 2)
                        http(sprintf('<input type="hidden" name="$_%s_%d" value="%s" />', data[j], i, coalesce(data[j+1], '')));
                    ?>
                  </td>
                  <td>
                    <?V get_keyword('format', data, '') ?>
                   </td>
                  <td>
                    <?V get_keyword('title', data, '') ?>
                   </td>
                  <td>
                    <?V get_keyword('rss', data, '') ?>
                   </td>
               </tr>
               <?vsp
                 }
               ?>
            </table>
          <?vsp
            if (length(self.v_data) > 18)
              http('</div>');
          ?>
        </div>
        <div class="form-footer">
          <v:button action="simple" value="Back" xhtml_onclick="javascript:history.back();return false;" xhtml_class="form-button"/>
          <v:button action="simple" value="Check" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                declare i, j, N, L integer;
                declare url, channel, channels, params any;

                channels := vector('Checked');
                params := e.ve_params;
                L := length(params);
                for (i := 0; i < L; i := i + 2)
                {
                  if (params[i] = 'cb_item')
                  {
                    N := atoi(params[i+1]);
                    url := get_keyword(sprintf('$_rss_%d', N), params, '');
                    if (url <> '')
                    {
                      channel := ENEWS.WA.channels_uri(url);
                      if ((length(channel) > 0) and (channel[0] <> 'links'))
                        for (j := 1; j < length(channel); j := j + 1)
                          channels := vector_concat(channels, vector(channel[j]));
                    }
                  }
                }
                if (length(channels) = 1)
                {
                  self.vc_error_message := 'Empty feed\'s list. Please select another links!';
                  self.vc_is_valid := 0;
                  return;
                }
                self.v_step := '3.3';
                self.v_data := channels;
                self.vc_data_bind(e);
              ]]>
            </v:on-post>
          </v:button>
          <v:button action="simple" name="Cancel3" value="Cancel" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels.vspx')));
              ]]>
            </v:on-post>
          </v:button>
        </div>
      </v:template>

      <v:template type="simple" enabled="-- case when (self.v_step = '3') then 1 else 0 end">
        <div class="form-header">
          Feed
        </div>
        <div class="new-form-body">
          <?vsp
            declare i, j, n, l integer;

            l := length(self.v_data);
            for (i := 1; i < l; i := i + 1)
            {
              n := length(self.v_data[i]);
              for (j := 0; j < n; j := j + 2)
                if (isstring(self.v_data[i][j+1]))
                  http(sprintf('<input type="hidden" name="$_%s_%d" value="%s"/>', self.v_data[i][j], i, coalesce(self.v_data[i][j+1], '')));
            }
          ?>
          <table cellspacing="0">
            <tr>
              <th>
                <v:label for="f_title" value="Title"/>
              </th>
              <td>
                <v:text name="f_title" null-value="--''" value="--ENEWS.WA.utf2wide(self.v_title)" xhtml_class="textbox" xhtml_size="70%"/>
              </td>
            </tr>
            <tr>
              <th>
                <v:label for="f_folder_name" value="Feed folder"/>
              </th>
              <td>
                <v:text name="f_folder_name" null-value="--''" value="--self.v_folder_name" xhtml_class="textbox" xhtml_size="40%"/>
                <v:data-list name="f_folder_id" value="--self.v_folder_id" sql="select 0 as EFO_ID, 'Select folder ...' as EFO_PATH2, '' as EFO_PATH from WS.WS.SYS_DAV_USER where U_NAME = 'dav' union all select EFO_ID, ENEWS.WA.folder_path2(EFO_DOMAIN_ID, EFO_ID) as EFO_PATH2, ENEWS.WA.folder_path(EFO_DOMAIN_ID, EFO_ID) as EFO_PATH from ENEWS.WA.FOLDER where EFO_DOMAIN_ID = self.domain_id order by EFO_PATH" key-column="EFO_ID" value-column="EFO_PATH2" xhtml_class="select"/>
              </td>
            </tr>
            <v:template type="simple" enabled="--self.pshCheck()">
              <tr>
                <th>
                  PubSubHub Server
                </th>
                <td>
                  <v:label value="--self.v_psh"/>
                </td>
              </tr>
              <tr>
                <th>
                  <v:label for="f_psh_enabled" value="PubSubHub Enable"/>
                </th>
                <td>
                  <v:check-box name="f_psh_enabled" is-boolean="1" true-value="1" false-value="0" value="--self.v_psh_enabled">
                 	  <v:after-data-bind>
                 		  <![CDATA[
                 		    if (not ENEWS.WA.check_feeds_grants (self.domain_id, self.account_id))
                          control.vc_add_attribute ('disabled', 'disabled');
            			    ]]>
            			  </v:after-data-bind>
                  </v:check-box>
                </td>
              </tr>
            </v:template>
          </table>
        </div>
        <div class="form-footer">
          <v:button action="simple" value="Subscribe" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                self.f_title.ufl_value := trim(self.f_title.ufl_value);
                if (is_empty_or_null(self.f_title.ufl_value))
                {
                  self.vc_error_message := 'Please, enter correct title name.';
                   self.vc_is_valid := 0;
                  return;
                }
                declare i, l integer;
                declare feed_id varchar;
                declare S, data, params any;

                data := vector();
                params := e.ve_params;
                l := length(params);
                for (i := 0; i < l; i := i + 2)
                  if (params[i] like '$_%_1')
                  {
                    S := replace(replace(params[i], '$_', ''), '_1', '');
                    data := vector_concat(data, vector(S, params[i+1]));
                  }

                self.v_psh_enabled := atoi (get_keyword ('f_psh_enabled', params, '0'));
                data := vector_concat (data, vector ('psh_enabled', self.v_psh_enabled));
                feed_id := ENEWS.WA.channel_create(data);

                self.f_folder_name.ufl_value := trim(self.f_folder_name.ufl_value);
                if (self.f_folder_name.ufl_value <> '')
                {
                  if (not ENEWS.WA.folder_check_name(self.f_folder_name.ufl_value, 1))
                  {
                    self.vc_error_message := 'Please, enter other folder name. This name contains bad characters.';
                     self.vc_is_valid := 0;
                    return;
                  }
                }
                ENEWS.WA.channel_domain (-1, self.domain_id, feed_id, self.f_title.ufl_value, null, self.v_folder_name, trim(self.f_folder_id.ufl_value));
                {
                  declare exit handler for sqlstate '*' { goto _next; };
                  if (ENEWS.WA.channel_feeds(feed_id) = 0)
                    ENEWS.WA.feed_refresh(feed_id);
                  ENEWS.WA.tags_refresh(self.domain_id, self.account_id, 0);
                }
              _next:
                self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels.vspx')));
               ]]>
             </v:on-post>
          </v:button>
          <v:button action="simple" name="Cancel4" value="Cancel" xhtml_class="form-button">
            <v:on-post><![CDATA[
              self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels.vspx')));
             ]]></v:on-post>
          </v:button>
        </div>
      </v:template>

      <v:template type="simple" enabled="-- case when (self.v_step like '3.%') then 1 else 0 end">
        <div class="form-header">
          <?vsp http(self.v_data[0]); ?> Feeds
        </div>
        <div class="new-form-body">
          <?vsp
            http(sprintf('<input type="hidden" name="$_retrieve" value="%d"/>', self.v_retrieve));

            if (length(self.v_data) > 18)
              http('<div id="feedsDiv" style="height: 400px; overflow: auto;">');
          ?>
            <table id="feeds" cellspacing="0">
              <tr>
                <td style="text-align: center; background-color: #B0CDE4;">
                  <input type="checkbox" name="cbx_all" value="Select All" title="Select All" onClick="selectAllCheckboxes(this.form, this, 'cb_')"/>
                </td>
                <th style="text-align: center; background-color: #B0CDE4; width: 100%;">Title</th>
                <th style="text-align: center; background-color: #B0CDE4;">Feed folder</th>
              </tr>
              <?vsp
                declare i, n integer;
                declare S, allData, data, selectFolder any;

                selectFolder := '<select class="select" name="$_fid_<folder_id>">';
                selectFolder := selectFolder || '<option class="option" value="0"> Select folder ...</option>';
                for select EFO_ID, ENEWS.WA.folder_path2(EFO_DOMAIN_ID, EFO_ID) as EFO_PATH2, ENEWS.WA.folder_path(EFO_DOMAIN_ID, EFO_ID) as EFO_PATH from ENEWS.WA.FOLDER where EFO_DOMAIN_ID = self.domain_id order by EFO_PATH do
                {
                  S := '';
                  if (not is_empty_or_null (self.v_mfolder_id) and (EFO_ID = self.v_mfolder_id))
                    S := ' selected="selected"';
                  selectFolder := selectFolder || sprintf('<option class="option" value="%d"%s>%V</option>', EFO_ID, S, EFO_PATH2);
                }
                selectFolder := selectFolder || '</select>';

                allData := self.v_data;
                n := length(allData);
                for (i := 1; i < n; i := i + 1)
                {
                  data := allData[i];
              ?>
                <tr>
                  <td align="center">
                    <?vsp
                      declare S varchar;

                      S := coalesce((select 'disabled="disabled"' from ENEWS.WA.FEED, ENEWS.WA.FEED_DOMAIN where EF_URI = get_keyword('rss', data, '') and EFD_FEED_ID = EF_ID and EFD_DOMAIN_ID = self.domain_id), '');
                      http(sprintf('<input type="checkbox" name="cb_item" %s value="%d" title="%V" />', S, i, get_keyword('title', data, '')));
                      http (sprintf ('<input type="hidden" name="$_data_%d" value="%s"/>', i, encode_base64(serialize(data))));
                    ?>
                  </td>
                  <td>
                    <?V get_keyword('title', data, '') ?>
                  </td>
                  <td nowrap="nowrap">
                    <input type="text" name="$_fname_<?=i?>" value="<?V case when is_empty_or_null (self.v_mfolder_name) and is_empty_or_null (self.v_mfolder_id) then get_keyword('folder', data, '') else self.v_mfolder_name end ?>"/>
                    <?vsp http(replace(selectFolder, '<folder_id>', cast(i as varchar))); ?>
                  </td>
                </tr>
              <?vsp
                }
              ?>
            </table>
            <span id="progressMax" style="display: none;"><?V length(self.v_data)-1 ?></span>
            <table id="progressTable" cellspacing="0">
              <tr>
                <td>
                  <div id="progressText"></div>
                </td>
              </tr>
              <tr>
                <td>
                  <div id="progress"></div>
                </td>
              </tr>
            </table>
          <?vsp
            if (length(self.v_data) > 18)
              http('</div>');
          ?>
        </div>
        <div class="form-footer">
          <v:button action="simple" xhtml_id="btn_Back" value="Back" xhtml_onclick="javascript:history.back();return false;" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                self.vc_data_bind(e);
              ]]>
            </v:on-post>
          </v:button>
          <input type="button" id="btn_Subscribe" onclick="javascript: progressText('Fetching Feeds for selected Channels'); initState()" value="Subscribe"/>
          <v:button action="simple" name="btn_Background" xhtml_id="btn_Background" value="Background" xhtml_class="form-button" xhtml_style="display: none;">
            <v:on-post>
              <![CDATA[
                self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels.vspx')));
               ]]>
             </v:on-post>
          </v:button>
          <v:button action="simple" xhtml_id="btn_Stop" name="Cancel5" value="Cancel" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels.vspx')));
               ]]>
             </v:on-post>
          </v:button>
        </div>
      </v:template>

      <v:template type="simple" enabled="-- case when (self.v_step = '4') then 1 else 0 end">
        <div class="feedsData">
        <?vsp
            declare i, n integer;
            declare allData, data any;

          http(sprintf('<input type="hidden" name="$_retrieve" value="%d"/>', self.v_retrieve));
            http(sprintf('<input type="hidden" name="$_mfolder_name" value="%s"/>', self.v_mfolder_name));
            http(sprintf('<input type="hidden" name="$_mfolder_id" value="%d"/>', self.v_mfolder_id));

            allData := self.v_data;
            n := length(allData);
            for (i := 1; i < n; i := i + 1)
            {
              data := allData[i];
              if (not exists(select 1 from ENEWS.WA.FEED, ENEWS.WA.FEED_DOMAIN where EF_URI = get_keyword('rss', data, '') and EFD_FEED_ID = EF_ID and EFD_DOMAIN_ID = self.domain_id))
              {
              http(sprintf('<input type="hidden" name="cb_item_%d" value="%d"/>', i, i));
                http (sprintf ('<input type="hidden" name="$_data_%d" value="%s"/>', i, encode_base64(serialize(data))));
            }
          }
        ?>
        </div>
        <div class="form-header">
          <?vsp http(self.v_data[0]); ?> Feeds
        </div>
        <div class="new-form-body">
          <span id="progressMax" style="display: none;"><?V length(self.v_data)-1 ?></span>
          <table id="progressTable" cellspacing="0">
            <tr>
              <td>
                <div id="progressText">Subscribing to <?V length(self.v_data)-1 ?> Channels and Fetching all associated Feeds</div>
              </td>
            </tr>
            <tr>
              <td>
                <div id="progress"></div>
              </td>
            </tr>
          </table>
        </div>
        <div class="form-footer">
          <v:button action="simple" xhtml_id="btn_Background" value="Background" xhtml_class="form-button">
            <v:on-post>
              <![CDATA[
                self.vc_redirect (ENEWS.WA.utf2wide (ENEWS.WA.page_url (self.domain_id, 'channels.vspx')));
               ]]>
             </v:on-post>
          </v:button>
          <input type="button" id="btn_Stop" onclick="javascript: stopState(); return true;" xhtml_class="form-button" />
        </div>
        <script type="text/javascript">
          <![CDATA[
            OAT.MSG.attach(OAT, "PAGE_LOADED", initState);
          ]]>
        </script>
      </v:template>

      <script type="text/javascript">
        <![CDATA[
          coloriseTable('feeds');
        ]]>
      </script>
    </vm:pagebody>
  </vm:pagewrapper>
</v:page>
