// Initialization
$oPage = new cPage();
$oRGroups = new RecipientGroupCollection();
$oRGroupMembers = new RecipientGroupMemberCollection();
$oRGroup = new RecipientGroup();
$aFields = array();
$aFields["name"] = array("field" => "name", "caption" => i18n("Name", $plugin_name), "type" => "base,sort,search");
$aFields["email"] = array("field" => "email", "caption" => i18n("E-Mail", $plugin_name), "type" => "base,sort,search");
$aFields["confirmed"] = array("field" => "confirmed", "caption" => i18n("Confirmed", $plugin_name), "type" => "base");
$aFields["deactivated"] = array("field" => "deactivated", "caption" => i18n("Deactivated", $plugin_name), "type" => "base");
if ($action == "recipientgroup_create" && $perm->have_perm_area_action($area, $action)) {
    $oRGroup = $oRGroups->create(" " . i18n("-- new group --", $plugin_name));
    $_REQUEST["idrecipientgroup"] = $oRGroup->get("idnewsgroup");
    $oPage->setReload();
    $sRefreshLeftTopScript = '<script type="text/javascript">top.content.left.left_top.refreshGroupOption(\'' . $_REQUEST["idrecipientgroup"] . '\', \'add\')</script>';
    $oPage->addScript('refreshlefttop', $sRefreshLeftTopScript);
} elseif ($action == "recipientgroup_delete" && $perm->have_perm_area_action($area, $action)) {
    $oRGroups->delete($_REQUEST["idrecipientgroup"]);
    $sRefreshLeftTopScript = '<script type="text/javascript">top.content.left.left_top.refreshGroupOption(\'' . $_REQUEST["idrecipientgroup"] . '\', \'remove\')</script>';
    $oPage->addScript('refreshlefttop', $sRefreshLeftTopScript);
    $_REQUEST["idrecipientgroup"] = 0;
    $oRGroup = new RecipientGroup();
    $oPage->setReload();
} else {
    $oRGroup->loadByPrimaryKey($_REQUEST["idrecipientgroup"]);
}
if ($oRGroup->virgin == false && $oRGroup->get("idclient") == $client && $oRGroup->get("idlang") == $lang) {
    if ($action == "recipientgroup_save_group" && $perm->have_perm_area_action($area, $action)) {
        // Saving changes
        $aMessages = array();
        $bReload = false;
			url += "&frame=4";
			url += "&idnewsjob=" + idnewsjob;
			url += "&contenido=" + sid;
			url += get_registered_parameters();
			url += "&selAuthor=" + oForm.selAuthor.value;
			url += "&sortby=" + oForm.sortby.value;
			url += "&sortorder=" + oForm.sortorder.value;
			url += "&filter=" + oForm.filter.value;
			url += "&elemperpage=" + oForm.elemperpage.value;

			parent.parent.right.right_bottom.location.href = url;
		}
		</script>';
$oPage->setMargin(0);
// Messagebox JS has to be included before ExecScript!
$oPage->addScript('messagebox', '<script type="text/javascript" src="scripts/messageBox.js.php?contenido=' . $sess->id . '"></script>');
$oPage->addScript('exec', $sExecScript);
$oPage->addScript('parameterCollector.js', '<script language="JavaScript" src="scripts/parameterCollector.js"></script>');
//generate current content for Object Pager
$sPagerId = '0ed6d632-6adf-4f09-a0c6-1e38ab60e303';
$oPagerLink = new cHTMLLink();
$oPagerLink->setLink("main.php");
$oPagerLink->setTargetFrame('left_bottom');
$oPagerLink->setCustom("selAuthor", $_REQUEST["selAuthor"]);
$oPagerLink->setCustom("elemperpage", $_REQUEST["elemperpage"]);
$oPagerLink->setCustom("filter", $_REQUEST["filter"]);
$oPagerLink->setCustom("restrictgroup", $_REQUEST["restrictgroup"]);
$oPagerLink->setCustom("sortby", $_REQUEST["sortby"]);
$oPagerLink->setCustom("sortorder", $_REQUEST["sortorder"]);
$oPagerLink->setCustom("searchin", $_REQUEST["searchin"]);
$oPagerLink->setCustom("frame", $frame);
    $result = $notification->returnNotification("warning", i18n('There is no Content Allocation tree.', 'content_allocation'));
} else {
    if (!is_object($tpl)) {
        $tpl = new Template();
    }
    $hiddenfields = '<input type="hidden" name="action" value="storeallocation">
		<input type="hidden" name="idart" value="' . $idart . '">
		<input type="hidden" name="contenido" value="' . $sess->id . '">
		<input type="hidden" name="area" value="' . $area . '">
		<input type="hidden" name="frame" value="' . $frame . '">
		<input type="hidden" name="idcat" value="' . $idcat . '">';
    $tpl->set('s', 'HIDDENFIELDS', $hiddenfields);
    if (sizeof($loadedAllocations) > 0) {
        $tpl->set('s', 'ARRAY_CHECKED_BOXES', 'var checkedBoxes = [' . implode(',', $loadedAllocations) . '];');
    } else {
        $tpl->set('s', 'ARRAY_CHECKED_BOXES', 'var checkedBoxes = [];');
    }
    $oDiv = new cHTMLDiv();
    $oDiv->updateAttributes(array('style' => 'text-align: right; padding: 5px; width: 730px; border: 1px #B3B3B3 solid; background-color: #FFFFFF;'));
    $oDiv->setContent('<input type="image" src="images/but_ok.gif" />');
    $tpl->set('s', 'DIV', '<br>' . $oDiv->render());
    $tpl->set('s', 'TREE', $result);
    $tpl->set('s', 'REMOVE_ALL', i18n("Remove all", 'content_allocation'));
    $tpl->set('s', 'REMOVE', i18n("Remove", 'content_allocation'));
    $result = $tpl->generate($cfg['pica']['treetemplate_complexlist'], true);
    $script = '<link rel="stylesheet" type="text/css" href="' . $cfg['pica']['style_complexlist'] . '"/>
	<script language="javascript" src="' . $cfg['pica']['script_complexlist'] . '"></script>';
    $oPage->addScript('style', $script);
}
$oPage->setContent($sLocationString . $result . markSubMenuItem(5, true));
$oPage->render();
        }
        if ($oJob->get("dispatch_delay") == 0) {
            // Send manually
            $oForm = new UI_Table_Form("properties", $sPathNext);
            $oForm->addHeader(i18n("Report:", $plugin_name));
            $oForm->add("", "");
            $oForm->add("", sprintf(i18n("Sending newsletter ... (chunk %s of %s, recipients: %s, sent: %s)", $plugin_name), $iChunk, $iChunks, $oJob->get("rcpcount"), $oJob->get("sendcount")));
            $oForm->setActionButton("cancel", $cfg['path']['contenido_fullhtml'] . "images/but_cancel.gif", i18n("Stop sending", $plugin_name), "c");
            $oForm->setActionButton("submit", $cfg['path']['contenido_fullhtml'] . "images/but_ok.gif", i18n("Send next chunk", $plugin_name), "s", "news_job_run");
        } else {
            // Send automatically
            $oForm = new UI_Table_Form("properties");
            $oForm->addHeader(i18n("Report:", $plugin_name));
            $oForm->add("", "");
            $oForm->add("", sprintf(i18n("Sending newsletter ... (chunk %s of %s, recipients: %s, sent: %s)", $plugin_name), $iChunk, $iChunks, $oJob->get("rcpcount"), $oJob->get("sendcount")));
            $oPage->addScript("Refresh", '<meta http-equiv="refresh" content="' . $oJob->get("dispatch_delay") . '; URL=' . $sPathNext . '">');
            $oForm->unsetActionButton("submit");
            $oForm->setActionButton("cancel", $cfg['path']['contenido_fullhtml'] . "images/but_cancel.gif", i18n("Stop sending", $plugin_name), "c");
        }
    } else {
        // All newsletters should have been sent
        $oForm = new UI_Table_Form("properties");
        $oForm->addHeader(i18n("Report:", $plugin_name));
        $oForm->add("", "");
        $oForm->add("", sprintf(i18n("The newsletter has been sent to %s recipients", $plugin_name), $oJob->get("sendcount")));
        $oPage->setReload();
    }
    $oPage->setContent($notis . $oForm->render(true));
} else {
    if ($action == "news_job_delete" && $perm->have_perm_area_action($area, $action) && is_numeric($_REQUEST["idnewsjob"])) {
        $oJobs = new cNewsletterJobCollection();
			controller = document.getElementById("itemname");
			controller.focus();
			function fieldCheck() {
				if (controller.value == "") {
					alert("' . i18n("Please enter a category name.", 'content_allocation') . '");
					controller.focus();
					return false;
				}
                return true;
			}
		</script>';
    $oDiv->setContent($form);
    $sTemp = $oDiv->render();
} else {
    $newTree = '<a href="main.php?action=' . $action . '&step=createRoot&frame=' . $frame . '&area=' . $area . '&contenido=' . $sess->id . '"><img  src="images/folder_new.gif" border="0" style="vertical-align: middle; margin-right: 5px;">' . i18n("Create new tree", 'content_allocation') . '</a><div style="height:10px"></div>';
}
$result = $oTree->renderTree(true);
if ($result === false) {
    $result = '&nbsp;';
}
$js = '
<script language="javascript">
/* Function for deleting categories*/
function deleteCategory(idpica_alloc) {
    var url = "main.php?area=' . $area . '&action=' . $action . '&step=deleteItem&idpica_alloc=" + idpica_alloc + "&frame=' . $frame . '&contenido=' . $sess->id . '";
    window.location.href = url;
}
</script>';
$oPage->addScript('deleteCategory', $js);
$oPage->setContent($pNotify . $newTree . $sTemp . '<br/>' . $result);
$oPage->render();
		// Create messageBox instance
		box = new messageBox("", "", "", 0, 0);

		// If html newsletter template selection has changed, ask user
		// if he/she may like to save this change (e.g. to get an html
		// newsletter immediately)
		function askSubmitOnTplChange(oSelectObject) {
			iOriginalTplIDArt = ' . $iTplIDArt . ';

			if (iOriginalTplIDArt != oSelectObject.options[oSelectObject.selectedIndex].value) {
				if (iOriginalTplIDArt == 0) {
					// Everything fine: Just selecting a template for the first time
					submitForm();
				} else {
					// You may loose information, warn!
					box.confirm("' . i18n("HTML newsletter template changed", $plugin_name) . '", "' . i18n("HTML template has been changed. Do you like to save now to apply changes?<br /><br /><b>Note, that existing HTML newsletter content will get lost!</b>", $plugin_name) . '", "submitForm()");
				}
			}
		}

		function submitForm() {
			document.frmNewsletterMsg.submit();
		}
		</script>';
    $oPage->addScript('messagebox', '<script type="text/javascript" src="scripts/messageBox.js.php?contenido=' . $sess->id . '"></script>');
    $oPage->addScript('execscript', $sExecScript);
    $oPage->setContent($notis . $oForm->render(true));
} else {
    $oPage->setContent($notis . "");
}
$oPage->render();
$oForm->setVar("frame", $frame);
$oForm->setVar("area", $area);
$oForm->setVar("action", "recipients_import_exec");
$oForm->addHeader(i18n("Import recipients", $plugin_name));
$oSelDelimiter = new cHTMLSelectElement("selDelimiter");
$aItems = array();
$aItems[] = array("semicolon", i18n("Semicolon", $plugin_name));
$aItems[] = array("tab", i18n("Tab", $plugin_name));
$oSelDelimiter->autoFill($aItems);
$oSelDelimiter->setDefault($_REQUEST["selDelimiter"]);
$oForm->add(i18n("Delimiter", $plugin_name), $oSelDelimiter->render());
$oAreaData = new cHTMLTextarea("txtData", $_REQUEST["txtData"], 80, 20);
$sInfo = '<a href="javascript:fncShowHide(\'idInfoText\');"><strong>' . i18n("Import information", $plugin_name) . '</strong></a>' . '<div id="idInfoText" style="display: none">' . '<br /><br /><strong>' . i18n("Specify colum types:", $plugin_name) . '</strong>' . i18n("<br />The first line must contain the column names; this specifies the column order.<br />&lt;column name&gt;[delimiter]&lt;column name&gt;...", $plugin_name) . '<br /><br /><strong>' . i18n("Data structure:", $plugin_name) . '</strong><br />' . i18n("The recipients have to be entered using the following format:<br />&lt;data&gt;[Delimiter]&lt;data&gt;... - each recipient in a new line.", $plugin_name) . '<br /><br /><strong>' . i18n("Example:", $plugin_name) . '</strong>' . i18n("<br />name;mail;confirmed<br />Smith;jon.smith@example.org;1", $plugin_name) . '<br /><br /><strong>' . i18n("The following column names will be recognized:", $plugin_name) . '</strong><br />' . implode("<br />\n", $aFields) . '</div>';
$oForm->add(i18n("Recipients", $plugin_name), $oAreaData->render() . "<br />" . $sInfo);
unset($sInfo);
$sExecScript = '
<script type="text/javascript">
    /* Enabled/Disable group box */
    function fncShowHide(strItemID) {
        objItem = document.getElementById(strItemID);

        if (objItem.style.display == "none") {
           objItem.style.display = "inline";
        } else {
           objItem.style.display = "none";
        }
    }
	</script>';
$oPage->addScript('execscript', $sExecScript);
$oPage->setContent($sMessage . $oForm->render(true));
$oPage->render();