	<?php

    $file->foto_upload_exe();
    $main->select_boxes();



		/**********************************************************************
			Version: FreeRichTextEditor.com Version 1.00.
			License: http://creativecommons.org/licenses/by/2.5/
			Description: Example of how to preload content into freeRTE using PHP.
			Author: Copyright (C) 2006  Steven Ewing
		**********************************************************************/

		function freeRTE_Preload($content)
			{
			// Strip newline characters.
			$content = str_replace(chr(10), " ", $content);
			$content = str_replace(chr(13), " ", $content);
			// Replace single quotes.
			$content = str_replace(chr(145), chr(39), $content);
			$content = str_replace(chr(146), chr(39), $content);
			// Return the result.
			return $content;
			}
		// Send the preloaded content to the function.
		if ( isset($_POST['add_article']) && isset($_POST['freeRTE_content']) && ( (trim($_POST['freeRTE_content']) != '') && (trim($_POST['freeRTE_content']) != '<br />') ) && isset($_POST['heading']) && (trim($_POST['heading'])!='') && isset($_POST['category']) && (trim($_POST['category'])!='--') && isset($_POST['subject']) && (trim($_POST['subject'])!='--') && (trim($_POST['subject'])!='') ) {
            echo 'aaa';
			if ( $main->is_logged($_SESSION['id_user']) ) {
				$sql->sql_connect('e_learning');
                $id_cat = $main->get_subject_id($_POST['subject'], substr($_POST['category'],1));
                $next_id = $main->get_next_autoinc_value('articles');
				$sql_insert_article = "INSERT INTO articles (id_root, id_category, id_branch, add_by, remake_of, nadpis, popis, date, show_web, utime)
				VALUES (" . $main->vrat_koren(substr($_POST['category'],1)) . ", " . $id_cat . ", " . substr($_POST['category'],1) . ", " . $_SESSION['id_user'] . ", " . $next_id . " , '" . $_POST['heading'] . "', '" . $_POST['freeRTE_content'] . "', '" . date("Y-m-d G:i:s") . "', 1, " . time() . ")";

				if ( $result_insert_article = mysql_query($sql_insert_article) ) {
                    $i = 0;
                    while ( $_FILES['defaults_' . $i]['name'] != '' ) {
                        if	( isset($_FILES['defaults_' . $i]['name']) && isset($_POST['add_article']) ) {
                            if ( ($_FILES['defaults_' . $i]['error'] == UPLOAD_ERR_OK) ) {
                                    if (is_uploaded_file($_FILES['defaults_' . $i]['tmp_name'])) {
                                        $nazov = $_FILES['defaults_' . $i]['name'];
                                        $nazov = $main->removeDia($nazov);
                                        
                                        if ( !file_exists('attach/' . $next_id) ) {
                                            mkdir('attach/' . $next_id);
                                        }

                                        $location = 'attach/' . $next_id . '/' . $_FILES['defaults_' . $i]['name'];
                                        if ( move_uploaded_file($_FILES['defaults_' . $i]['tmp_name'], $location) ) {
                                            mysql_query('INSERT INTO articles_attach (id_article, path) VALUES (' . $next_id . ',\'' . $location . '\')');
                                        } else {

                                        }
                                    }
                            } else {
                                echo '<p style="color: #FF0000;">zle</p>';
                            }
                        }
                        $i++;
                    }
                }
				echo 'sended<br />';
				$_SESSION['uploadnute'] = array();
				echo '<script type="text/javascript">';
				echo 	'document.location.href=\'' . $main->form_action . '\'';
				echo '</script>';
			} else {
				echo 'error';
			}

		} else  {
            echo $main->return_error_text('Nie všetky položky boli správne vyplnené');
			$content = freeRTE_Preload($_POST['freeRTE_content']);
		}
		?>
        <div class="fileinputs">
		<form id="main_form" name="main_form" enctype="multipart/form-data" method="post" action="<?php echo $main->form_action; ?>">
			<div style="float: left; padding: 10px">
				Názov:<br /><input type="text" value="<?=$_POST['heading']?>" name="heading" style="font-size: 11px;" /><br />
				<input type="text" style="visibility: hidden;" name="main_id" value="<? if ( isset($_POST['main_id']) ) {echo $_POST['main_id'];} else {echo '-1';} ?>" />
				<div>
				<?
				$rs->return_section();
				?>
				</div>
				<script src="./js/richtext.js" type="text/javascript" language="javascript"></script>
				<script src="./js/config.js" type="text/javascript" language="javascript"></script>
				<script type="text/javascript">
					initRTE('<?= $content ?>', 'example.css');
				</script>
				<br />
                prílohy: <input type="file" name="defaults" /><br clear="all" /><br />
				<input type="submit" name="add_article" value="Uloz clanok" />
			</div>
			<div style="float: right; width: 35%; margin-right: 3%; padding: 5px;">
                <input type="file" accept="image/gif,image/jpeg,image/png,image/jpg" name="pic" value="" />
				<input type="submit" style="font-size: 11px" value="Pridaj obrazok" name="add_pic" />
			</div>
		</form>
        </div>
		<?
		echo '<h4>Adresy vami uploadnutych obrazkov: <span style="font-size: 10px;">(budete ju potrebovat pri doplnani obrazkov do clankov)</span></h4>';
		if ( ($_SESSION['uploadnute'] != '') && (sizeof($_SESSION['uploadnute']) > 0) )
			{
			echo '<div style="float: right; width: 380px;">';
			echo substr($_SESSION['uploadnute'],5) . '</div>';
			echo '</div>';
			}
		?>