<?php
$_SESSION['upload_dir'] = '/source/' . $person_info->url . '/';
$_SESSION['current_path'] = '../source/' . $person_info->url . '/';
?>

<div class="col-sm-10">
    <div class="row gray_ttc">
        <div class="col-sm-10 ">
            <div class="col-sm-12">
                <h3>Add Topic</h3>  
                <hr>
            </div>

            <div class="col-sm-12"><br></div>
            <div class="col-sm-12" id="max499">
                <?php
//                echo validation_errors(); //formID2
                $url = 'super-admin/add-topic';
                echo form_open($url, 'class="" id="formID2" name="add_topic_form" onsubmit="return check_form();"');
                ?>
                <div class="form-group col-sm-8">
                    <div class="input-group">
                        <input type="text" 
                               data-errormessage-value-missing="Quiz Topic name is required!" 
                               class="form-control validate[required, custom[topicNameExistence]] text-input"
                               name="topic_name" id="topic_name"
                               value="<?php echo set_value('topic_name'); ?>" 
                               placeholder="Please enter topic name"
                               oninput="checkUrlValue(this.value, urlValue)">
                        <span class="input-group-addon"  title="Please enter your  topic name"><i class="fa fa-book"></i></span>
                        <input type="hidden" name="topic_name_exist" id="topic_name_exist" value="">
                    </div>
                    <div id="topic_name_message" style="color:red">
                        <?php echo form_error('topic_name'); ?>
                    </div>
                </div>
                <div class=" col-sm-12"><br></div>

                <div class="form-group col-sm-6">
                    <div class="input-group">
                        <input type="text" data-errormessage-value-missing="topic url is required!"
                               class="form-control validate[required, custom[ttURL], custom[spaceFunction], custom[ttURLExistence]] text-input"  
                               name="topic_url" id="topic_url"
                               value="<?php echo set_value('topic_url') ?>"
                               oninput="checkUrlValue(topic_name.value, this.value)"
                               placeholder="Please enter topic URL..">
                        <span class="input-group-addon"  title="Please enter topic URL"><i class="fa fa-link"></i></span>
                        <input type="hidden" id="urlcode" name='urlcode' value=''/>
                        <input type="hidden" name="url_exist" id="url_exist" value="">
                    </div>
                    <div id="topic_url_message" style="color:red">
                        <?php echo form_error('topic_url'); ?>
                    </div>
                </div>
                <div class=" col-sm-12"><br></div>

                <div class="form-group col-sm-12">
                    <div class="input-group" id="screen1280" >
                        <textarea class="form-control resoulation1280" name="topic_detail" rows="4"><?php echo set_value('topic_detail'); ?></textarea>
                    </div>
                </div>
                <div class="col-sm-12"><br></div>                

                <div class="form-group col-sm-12">
                    <div class="text-left padding-zero col-sm-1">  <label >Status  :  &nbsp;&nbsp;</label></div>
                    <div class="radio radio-success col-sm-11" style="margin-top: -3px" >

                        <input type="radio"
                               data-errormessage-value-missing="Please Select Course Status" 
                               class="validate[required]" 
                               name="topic_status" 
                               id="published" 
                               value="1"
                               onchange="check_topic_status();">
                        <label for="published">Published</label>
                        <span style="margin-right: 30px"></span>

                        <input type="radio" 
                               data-errormessage-value-missing="Please Select Course Status" 
                               class="validate[required]" 
                               name="topic_status" 
                               id="unpublished" 
                               value="0"
                               onchange="check_topic_status();">
                        <label for="unpublished">unpublished</label>
                        <span style="margin-right: 30px"></span>

                        <input type="radio" 
                               data-errormessage-value-missing="Please Select Course Status" 
                               class="validate[required]" 
                               name="topic_status" 
                               id="pending" 
                               value="3"
                               onchange="check_topic_status();">
                        <label for="pending">Pending</label>
                        <span style="margin-right: 30px"></span>

                        <input type="radio" 
                               data-errormessage-value-missing="Please Select Course Status" 
                               class="validate[required]" 
                               name="topic_status" 
                               id="approved" 
                               value="4"
                               onchange="check_topic_status();">
                        <label for="approved">Approved</label>
                        <span style="margin-right: 30px"></span>

                    </div>
                    <div id="topic_status_message" style="color:red">
                        <?php echo form_error('topic_status'); ?>
                    </div>
                </div>

                <div class="col-sm-12"><br></div>
                <div class="col-sm-3"></div>
                <div class="form-group col-sm-6">
                    <input type="submit" class="green-btn btn btn-default" value="Add Topic"><br/><br/>
                </div>
                <div class="col-sm-3"></div>
                <?php echo form_close(); ?>
            </div>
        </div>
    </div>
    <br>
    <div class="row gray">
        <div class="col-sm-12 ">
            <label><a href="<?php echo base_url() ?>super_admin">Dashboard </a>->> Add topic </label>
        </div>
    </div>
</div>

<script>
    //START url processing
    var urlValue = '';
    function checkUrlValue(givenText, urlInput) {
        urlValue = urlInput;
        if (urlValue === '') {
            makeUrl(givenText);
        } else {
            checkUrl(urlInput);
        }
    }

    function makeUrl(profileID) {
        var name = profileID;
        if (profileID == '') {
            _('topic_url').value = '';
        } else {
            profileID = profileID.replace(/  +/g, '');
            for (i = 0; profileID.length > i; i++) {
                if (profileID.charAt(i) === ' ') {
                    profileID = profileID.replace(/ +/g, '-');
                }
                if (profileID.charAt(i) === '.') {
                    profileID = profileID.replace(/\./g, '');
                }
                if (profileID.charAt(i) === '-') {
                    profileID = profileID.replace(/--/g, '-');
                }
            }

            profileID = profileID.replace(/[&\/\\#@!^_=,+()$~%.'":*?<>{}]/g, '');
            if (profileID.charAt(profileID.length - 1) === '-') {
                profileID = setCharAt(profileID, profileID.length - 1, '');
            }
            profileID = profileID.toLowerCase();
            _('topic_url').value = profileID;
            checkName(name);
            checkUrl(profileID);
        }
    }

    var topicNameStatus;
    function checkName(name) {
        if (name != '') {
            var cn_xmlhttp = create_xmlhttp();
            var formdata = new FormData();
            formdata.append("topic_name", name);
            cn_xmlhttp.addEventListener("load", function (event) {
                topicNameStatus = cn_xmlhttp.responseText;
            }, false);
            cn_xmlhttp.open("POST", base_url + 'super-admin/check-topic-name-existance');
            cn_xmlhttp.send(formdata);
        }
    }

    function checkUrl(given_text) {
        var xmlhttp2 = create_xmlhttp();
        if (given_text != '') {
            var serverPage = base_url + 'super-admin/check-topic-url-existance/' + given_text;
            xmlhttp2.open("GET", serverPage);
            xmlhttp2.onreadystatechange = function ()
            {
                if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200)
                {
                    _('url_exist').value = xmlhttp2.responseText;
                    _('topic_url_message').innerHTML = '';
                }
            };
            xmlhttp2.send(null);
        }
    }
    //END url processing

</script>


<?php if (set_value('topic_status')) { ?>
    <script>
        document.forms['add_topic_form'].elements['topic_status'].value = '<?php echo set_value('topic_status'); ?>';
    </script>
<?php } ?>