<?php
$_SESSION['upload_dir'] = '/source/' . $person_info->url . '/';
$_SESSION['current_path'] = '../source/' . $person_info->url . '/';
?>
<div class="col-sm-8">
    <div class="row gray_ttc">
        <div class="col-sm-12 ">
            <div class="col-sm-12">
                <h3>Create Quiz Form</h3>  
                <hr>
            </div>
            <div class="col-sm-12"><br></div>
            <div class="col-sm-12" id="max499">
                <?php
                // echo validation_errors(); //id="formID2" 
                $url = 'create-quiz';
                echo form_open_multipart($url, 'class="formID2" name="add_quiz_form"');
                ?>
<!--                <input type="hidden" name="quiz_public_id" id="quiz_public_id" value="" >-->
                <div class="form-group col-sm-12">
                    <div class="input-group">
                        <input type="text" 
                               data-errormessage-value-missing="Quiz Name is required!" 
                               class="form-control"
                               id="quiz_name" name="quiz_name" placeholder="quiz name"  
                               value="<?php echo set_value('quiz_name'); ?>" 
                               placeholder="Please enter your  Quiz Name.."
                               oninput="checkUrlValue(this.value, urlValue)">
                        <span class="input-group-addon"  title="Please enter your  Quiz Name"><i class="fa fa-book"></i></span>
                    </div>
                    <div id="quiz_name_message" style="color:red">
                        <?php echo form_error('quiz_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="invitation code is required!"
                               class="form-control" 
                               name="quiz_public_id" id="quiz_public_id"
                               value="<?php echo set_value('quiz_public_id'); ?>" 
                               placeholder="Please enter Quiz Invitation Code.."  
                               oninput="check_quiz_public_id(this.value);">
                        <span class="input-group-addon" title="Please enter Quiz Invitation Code" ><i class="fa fa-question-circle"></i></span>
<!--                        <input type="hidden" id="incode" name='incode' />-->
                    </div>
                    <div id="quiz_public_id_message" style="color:red">
                        <?php echo form_error('quiz_public_id'); ?>
                    </div>
                </div>

                <div class="form-group col-sm-6">
                    <div class="input-group">
                        <input type="text" data-errormessage-value-missing="Group url is required!"
                               class="form-control"  
                               name="quiz_url" id="quiz_url"
                               value="<?php echo set_value('quiz_url') ?>"
                               oninput="checkUrlValue(quiz_name.value, this.value)"
                               placeholder="Please enter Quiz URl.."   >
                        <span class="input-group-addon"  title="Please enter Quiz 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="quiz_url_message" style="color:red">
                        <?php echo form_error('quiz_url'); ?>
                    </div>
                </div>

                <div class=" col-sm-12"><br></div>



                <div class="form-group col-sm-12 padding-zero">
                    <div class="form-group col-sm-6">
                        <div class="input-group">
                            <select data-errormessage-value-missing="Parent Topic is required!"  
                                    class="form-control" 
                                    name="parent_topic_id" 
                                    id="parent_topic_id"
                                    onchange="getParentTopic(this.value, '<?php echo base_url() . 'user/view-child-topic' ?>', '')">
                                <option value="">Select Parent Topic</option>
                                <?php
                                foreach ($quiz_topics as $topic_value) {
                                    echo '<option value="' . $topic_value->topic_id . '">' . $topic_value->topic_name . '</option>';
                                }
                                ?>
                            </select>
                            <span class="input-group-addon" id="basic-addon1" title="Please Select Parent Topic"><i class="fa fa-book"></i></span>
                        </div>
                        <span style="color: red;" id="parent_topic_id_error"><?php echo form_error('parent_topic_id'); ?></span>
                        <script>
                            documentID('parent_topic_id').value = '<?php echo set_value('parent_topic_id'); ?>';
                        </script>
                    </div>




                    <div class="form-group col-sm-6">
                        <div class="input-group">
                            <select data-errormessage-value-missing="Topic is required!"  
                                    class="form-control" 
                                    name="quiz_topics_specification" 
                                    id="quiz_topics_specification">
                                <option value="">Select Topic</option>

                            </select>
                            <span class="input-group-addon" id="basic-addon1" title="Please Select Topic"><i class="fa fa-book"></i></span>
                        </div>
                        <span style="color: red;" id="topic_id_error"><?php echo form_error('quiz_topics_specification'); ?></span>
                    </div>
                </div>


                <div class="col-sm-12"><br></div>

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

                <div class="col-sm-12"><br></div>

                <div class="form-group col-sm-6">
                    <select data-errormessage-value-missing="Please select a status!"
                            class="form-control"
                            id="quiz_random_static"  name="quiz_random_static"
                            onchange="randomStatus(this.value)" >
                        <option value=""> -Select Quiz Status- </option>
                        <!--                            <option value="1">Random</option>-->
                        <option value="2">Static</option>
                    </select>
                    <div id="quiz_random_static_message" style="color:red">
                        <?php echo form_error('quiz_random_static'); ?>
                    </div>
                </div>

                <div id="randomNumber" style="<?php
                if (set_value('quiz_random_static') == '1') {
                    echo 'display: block;';
                } else {
                    echo 'display: none;';
                }
                ?>">
                    <div class="form-group col-sm-6">
                        <div class="input-group">
                            <input type="text" 
                                   data-errormessage-value-missing="Number Of Questions is required!" 
                                   class="form-control"
                                   id="total_number_of_question" name="total_number_of_question" 
                                   value="<?php echo set_value('total_number_of_question'); ?>"  
                                   placeholder="Please Type Number of Question"   >
                            <span class="input-group-addon" title="Please Type Number of Question" ><i class="fa fa-question-circle"></i></span>
                        </div>
                        <div id="total_number_of_question_message" style="color:red">
                            <?php echo form_error('total_number_of_question'); ?>
                        </div>
                    </div>

                    <div class="form-group col-sm-6">
                        <input type="text" 
                               data-errormessage-value-missing="Mark is required!" 
                               class="form-control"  
                               id="points_per_answer" name="points_per_answer" value="<?php echo set_value('points_per_answer'); ?>"
                               placeholder="Type  question Marks" 
                               title="Type  question Marks">
                        <div id="points_per_answer_message" style="color:red">
                            <?php echo form_error('points_per_answer'); ?>
                        </div>
                    </div>

                    <div class="form-group col-sm-6">
                        <input type="text" 
                               data-errormessage-value-missing="Time limit required!"
                               class="form-control"  
                               id="time_limit" name="time_limit" value="<?php echo set_value('time_limit'); ?>"
                               placeholder="Type Time Limit (In minute)" 
                               title="Type Time Limit (In minute)">
                        <div id="time_limit_message" style="color:red">
                            <?php echo form_error('time_limit'); ?>
                        </div>
                    </div>
                </div>

                <div class="col-md-12"><br></div>
                <div class="form-group col-md-6">
                    <select data-errormessage-value-missing="Permission is required!"
                            class="form-control"
                            id="quiz_pulication_status"  name="quiz_pulication_status" 
                            onchange="accessLevel(this.value);" >
                        <option value=""> -Select Permission Status- </option>
                        <option value="1">Public</option>
                        <option value="2">TT User</option>
                        <!--<option value="3">My Link</option>-->
                        <!--<option value="4">Only Group</option>-->
<!--                        <option value="5">Course</option>
                        <option value="6">Video Course</option>-->
                    </select>
                    <div id="quiz_pulication_status_message" style="color:red">
                        <?php echo form_error('quiz_pulication_status'); ?>
                    </div>
                </div>

<!--                <div class="form-group col-sm-12" id="quiz_access_level" ></div>
                <div class="col-sm-6">
                    <div id="quiz_pulication_status_message" style="color:red">
                        <?php //echo form_error('quiz_pulication_status'); ?>
                    </div>
                </div>-->

                <div class="col-sm-12"><br></div>

                <div class="form-group col-sm-6">
                    <div class="text-left padding-zero col-sm-4">  <label >Status  :  &nbsp;&nbsp;</label></div>
                    <div class="radio radio-success col-sm-8" style="margin-top: -3px" >
                        <input  data-errormessage-value-missing="Status is required!"
                                class=""
                                type="radio" name="quiz_status" id="Active" value="1" onchange="check_quiz_status();" >
                        <label for="Active">Active</label>
                        <span style="margin-right: 20px"></span>
                        <input   data-errormessage-value-missing="Status is required!"
                                 class=""
                                 type="radio" name="quiz_status" id="Inactive" value="0" onchange="check_quiz_status();" >
                        <label for="Inactive">Inactive</label>
                    </div>
                    <div id="quiz_status_message" style="color:red">
                        <?php echo form_error('quiz_status'); ?>
                    </div>
                </div>

                <div class="col-sm-12"><br></div>
                <div class="col-sm-5"></div>
                <div class="form-group col-sm-2">

                    <input type="submit" class="green-btn btn btn-default" value="Create"><br/><br/>
                </div>
                <div class="col-sm-5"></div>

                <?php
                echo form_close();
                ?>
            </div>

        </div><!-- /.row gray -->


    </div>

</div>

<script>
    /*
     * @Function            : getParentTopic 
     * @Used IN             : @function-'admin/course/add-course-from.php'
     * @Description         : get parent Topic for view child topic
     * @Developed By        : MD Jaherul Islam
     * @Start DateTime      : 21-04-2016 05:47 PM 
     * @Parameter           : topicID[INTEGER], url[STRING],childTopicID[INTEGRE]
     * @Return              : NULL      
     * @Status              : available      
     */
    var getParentTopic = function (topicID, url, childTopicID, personID1) {
        var personID = (personID1 === '') ? '' : personID1;
        var output = '<option value="">Select Topic</option>';

        if (topicID !== '') {
            var serverUrl = url + '/' + topicID + '/' + personID;
            $.get(serverUrl, function (data, status) {
                if (status === 'success') {
                    var topicInfo = JSON.parse(data);

                    for (var i = 0; i < topicInfo.length; i++) {
                        output += '<option value="' + topicInfo[i].topic_id + '">' + topicInfo[i].topic_name + '</option>';
                    }
                    $('#quiz_topics_specification').html(output);

                    if (childTopicID !== '') {
                        $('#quiz_topics_specification').val(childTopicID);
                    }
                }
            });
        } else {
            $('#quiz_topics_specification').html(output);
        }
    };


    function check_quiz_public_id(given_text)
    {
        if (given_text == '') {
            _("incode").setAttribute("value", 0);
        } else {
            if (given_text) {
                var xmlhttp = create_xmlhttp();
                var serverPage = base_url + 'check_quiz_public_id/' + given_text;
                xmlhttp.open("GET", serverPage);
                xmlhttp.onreadystatechange = function ()
                {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                    {
                        _("quiz_public_id_message").innerHTML = '';
                        _("incode").setAttribute("value", xmlhttp.responseText);
                    }
                };
                xmlhttp.send(null);
            }
        }
    }

    //START quiz urlprocessing
    var urlValue = '';
    function checkUrlValue(courseName, urlInput) {
        urlValue = urlInput;
        if (urlValue === '') {
            makeUrl(courseName);
        } else {
            checkUrl(urlInput);
        }
    }

    function makeUrl(profileID) {
        if (profileID == '') {
            _('quiz_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();
            _('quiz_url').value = profileID;
            checkUrl(profileID);
        }
    }

    function checkUrl(given_text) {
        var xmlhttp2 = create_xmlhttp();
        if (given_text != '') {
            var serverPage = base_url + 'group_con/check_group_url/' + given_text;
            xmlhttp2.open("GET", serverPage);
            xmlhttp2.onreadystatechange = function ()
            {
                if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200)
                {
                    _('quiz_url_message').innerHTML = '';
                    _('url_exist').value = xmlhttp2.responseText;
                }
            };
            xmlhttp2.send(null);
        }
    }
    //END quiz url processing



    // JQuery and PHP validation message manage    

    document.forms['add_quiz_form'].elements['quiz_topics_specification'].value = '<?php echo set_value('quiz_topics_specification'); ?>';
    document.forms['add_quiz_form'].elements['quiz_pulication_status'].value = '<?php echo set_value('quiz_pulication_status'); ?>';
    document.forms['add_quiz_form'].elements['quiz_random_static'].value = '<?php echo set_value('quiz_random_static'); ?>';
    document.forms['add_quiz_form'].elements['quiz_status'].value = '<?php echo set_value('quiz_status'); ?>';
</script>