<script>
    $.noConflict();
    function documentID(el) {
        return document.getElementById(el);
    }

    var status = '1';
</script>
<?php
$person_id = $this->session->userdata('person_id');
?>
<div class="col-sm-10 " >
    <div class="col-sm-12 gray_ttc " >
        <div class="col-sm-12 h3 text-center">
            Add Course Category
            <hr>
            <br>
        </div>
        <?php
        echo validation_errors();
        $url = 'super-admin/add-course-category';
        echo form_open($url, 'class="cmxform" id="formID2" name="add_course_category"');
        ?>
        <div class="form-group col-sm-12">
            <div class="form-group col-sm-5">
                <div class="input-group">
                    <input type="text" 
                           data-errormessage-value-missing="Category Name is required!"  placeholder="Please type Course category Name"
                           class="form-control validate[required, custom[categoryNameExists]] text-input"
                           oninput="checkCategoryNameExists(this.value)"
                           data-errormessage-value-missing="Please type Category name"
                           id="course_category_name" 
                           name="course_category_name"
                           value="<?php echo set_value('course_category_name'); ?>" >
                    <span class="input-group-addon" id="basic-addon1" title="Please type Category Name"><i class="fa fa-book"></i></span>
                    <input type="hidden" id="category_name_exists" value="">
                    <input type="hidden" name="person_id" value="<?php echo $person_id; ?>">
                </div>
            </div>

            <script>
                function checkCategoryNameExists(input) {
                    var xmlhttp = false;
                    try {
                        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                        try {
                            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (E) {
                            xmlhttp = false;
                        }
                    }
                    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
                        xmlhttp = new XMLHttpRequest();
                    }

                    var category_name = input.replace(/  +/g, ' ');

                    serverPage = '<?php echo base_url() ?>super-admin/category-name-existance/' + category_name;
                    xmlhttp.open("GET", serverPage);
                    xmlhttp.onreadystatechange = function ()
                    {
                        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                        {
                            documentID('category_name_exists').value = xmlhttp.responseText;
                        }
                    };
                    xmlhttp.send(null);
                    checkUrlValue('', input ,'');
                }

                function makeManuUrl(profileID) {
                    if (profileID == '') {
                        documentID('menu_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();

                        documentID('course_category_url').value = profileID;
                        checkProfileUrl(profileID);
                    }
                }
            </script>

            <div class="form-group col-sm-5">
                <div class="input-group">
                    <input  type="text" 
                            data-errormessage-value-missing="Category URl is required!"
                            class="form-control validate[required], custom[urlSpecialCaracter], custom[spaceFunction], custom[urlExistence], custom[noCapital]] text-input" 
                            id="course_category_url" 
                            name="course_category_url"
                            oninput="checkUrlValue(event,course_category_name.value,this.value)"
                            placeholder="Give a category URL"  
                            value="<?php echo set_value('course_category_url'); ?>" >
                    <input type="hidden" name="url_exist" id="url_exist" value="">
                    <span class="input-group-addon" id="basic-addon1" title="Please type Category URl "><i class="fa fa-link"></i></span>
                </div>
            </div>
            <script>
                function checkUrlValue(event, categoryName, urlValue) {
                    if (event !== '') {
                        status = '2';
                    }
                    if (status === '1') {
                        makeManuUrl(categoryName);
                    } else if (status === '2') {
                        checkProfileUrl(urlValue);
                    }
                }


                function checkProfileUrl(given_text) {
                    var xmlhttp2 = false;
                    try {
                        xmlhttp2 = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                        try {
                            xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (E) {
                            xmlhttp2 = false;
                        }
                    }
                    if (!xmlhttp2 && typeof XMLHttpRequest != 'undefined') {
                        xmlhttp2 = new XMLHttpRequest();
                    }

                    if (given_text != '') {
                        serverPage = '<?php echo base_url(); ?>super_admin/check_profile_id_existance/' + given_text;
                        xmlhttp2.open("GET", serverPage);
                        xmlhttp2.onreadystatechange = function ()
                        {
                            if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200)
                            {
                                if (xmlhttp2.responseText == 1)
                                {
                                    documentID('url_exist').value = '1';
                                } else if (xmlhttp2.responseText == 2)
                                {
                                    documentID('url_exist').value = '';
                                }
                            }
                        }
                        xmlhttp2.send(null);
                    } else if (given_text == '') {
                    }
                }





            </script>
        </div>
        <div class="form-group col-sm-12">
            <div class="form-group col-sm-5">
                <div class="input-group ">
                    <input type="text"
                           data-errormessage-value-missing="Please type Course Category Author" 
                           class="form-control" 
                           name="course_category_author" 
                           id="course_category_author"
                           value="<?php echo set_value('course_category_author'); ?>" 
                           placeholder="Please type Course Category Author"> 
                    <span class="input-group-addon" id="basic-addon1" title="Please type Course Category Author"><i class="fa fa-user"></i></span>
                </div>
            </div>
            <?php
            if ($all_course_category) {
                ?>
                <div class="form-group col-sm-5">
                    <div class="input-group ">
                        <select data-errormessage-value-missing="Course Category is required!"  
                                class="form-control" 
                                name="course_category_id" 
                                id="course_category_id"  >
                            <option value=" ">Select Parent Category</option>
                            <?php
                            foreach ($all_course_category as $category_value) {
                                echo '<option value="' . $category_value->course_category_id . '">' . $category_value->course_category_name . '</option>';
                            }
                            ?>
                        </select>
                        <span class="input-group-addon" id="basic-addon1" title="Please Select "><i class="fa fa-list"></i></span>
                    </div>
                    <script>
                        document.forms['add_course_category'].elements['course_category_id'].value = '<?php echo set_value('course_category_id'); ?>';
                    </script>
                </div>
            <?php } ?>
        </div>
        <div class="form-group col-sm-12">
            <div class="form-group col-sm-10">
                <div class="input-group col-sm-12">
                    <input class="col-sm-12"
                           type="hidden" name="to_address" id="to_address" value="" >
                    <div class="col-sm-12 padding-zero">
                        <select name="course_category_meta_keywords[]"  id="course_category_meta_keywords" 
                                data-errormessage-value-missing="Interest Field is is Required!"
                                class="tokenize-sample validate[]"
                                title="" 
                                multiple="multiple" style="margin: 0px; padding: 0px; border: 0px; display: none;">
                                    <?php
                                    if (set_value('to_address')) {
                                        $keyword_string = set_value('to_address');
                                        $keywords = explode(",", $keyword_string);
                                        for ($i = 0; $i < (count($keywords) - 1); $i++) {
                                            ?>
                                    <option value="<?php echo $keywords[$i]; ?>" selected><?php echo $keywords[$i]; ?></option>
                                    <?php
                                }
                            }
                            ?>
                        </select>
                    </div>
                    <script type="text/javascript">
                        $('select#course_category_meta_keywords').tokenize({placeholder: "Please Type Meta Keyword"});
                    </script>
                </div>
            </div>
        </div>

        <div class="form-group col-sm-12">
            <div class="form-group col-sm-10">
                <div class="input-group text-left" id="screen1280" >
                    <label>Category Meta Description</label>
                    <textarea   class="form-control resoulation1280" 
                                name="course_category_meta_description[]"
                                rows="4"><?php echo set_value('course_category_meta_description'); ?></textarea>
                </div>

                <div class="input-group text-left" id="screen1024" >
                    <label>Category Meta Description</label>
                    <textarea   class="form-control  resoulation1024" 
                                name="course_category_meta_description[]"
                                rows="4"><?php echo set_value('course_category_meta_description'); ?></textarea>
                </div>
                <div class="input-group text-left" id="screenMobile" >
                    <label>Category Meta Description</label>
                    <textarea   class="form-control  resoulationMobile" 
                                name="course_category_meta_description[]"
                                rows="4"><?php echo set_value('course_category_meta_description'); ?></textarea>
                </div>
                <div class="input-group text-left" id="screenMobile_max499" >
                    <label>Category Meta Description</label>
                    <textarea   class="form-control  resoulationMobile_max499" 
                                name="course_category_meta_description[]"
                                rows="4"><?php echo set_value('course_category_meta_description'); ?></textarea>
                </div>
            </div>
        </div>

        <div class="form-group col-sm-12">
            <div class="form-group col-sm-10">
                <div class="input-group text-left" id="screen1280" >
                    <label>Course Category Description</label>
                    <textarea class="form-control  resoulation1280"
                              name="course_category_description[]"
                              rows="4"><?php echo set_value('course_category_description'); ?></textarea>
                </div>
                <div class="input-group text-left" id="screen1024" >
                    <label>Course Category Description</label>
                    <textarea class="form-control resoulation1024"
                              name="course_category_description[]"
                              rows="4"><?php echo set_value('course_category_description'); ?></textarea>
                </div>
                <div class="input-group text-left" id="screenMobile" >
                    <label>Course Category Description</label>
                    <textarea   class="form-control  resoulationMobile "
                                name="course_category_description[]"
                                rows="4"><?php echo set_value('course_category_description'); ?></textarea>
                </div>
                <div class="input-group text-left" id="screenMobile_max499" >
                    <label>Course Category Description</label>
                    <textarea class="form-control  resoulationMobile_max499" 
                              name="course_category_description[]"
                              rows="4"><?php echo set_value('course_category_description'); ?></textarea>
                </div>
            </div>
        </div>

        <div class="form-group col-sm-12">
            <div class="form-group col-sm-5">
                <div class="input-group ">
                    <select data-errormessage-value-missing="Please Select Course Category Access Level" 
                            class="form-control validate[required]" 
                            name="course_category_access_level" 
                            id="course_category_access_level">
                        <option value="">Select Access Level</option>
                        <option value="1">Public</option>
                        <option value="2">TalhaTraining User</option>
                    </select>
                    <span class="input-group-addon" id="basic-addon1" title="Please Select Course Category Access Level"><i class="fa fa-user"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group col-sm-12">
            <div class="form-group col-sm-10">
                <label class="col-sm-1 text-left padding-zero" for="person_status"> Status : &nbsp;</label>
                <div class="radio radio-success col-sm-11 margin-zero" >
                    <input type="radio" 
                           class="validate[required]"
                           name="course_category_status" 
                           id="published" value="1">
                    <label for="published">Active</label>
                    <span style="margin-right: 30px"></span>
                    <input type="radio"
                           class="validate[required]"
                           name="course_category_status" 
                           id="unpublished" value="2">
                    <label for="unpublishded">Inactive</label>
                    <span style="margin-right: 30px"></span>
                    <input type="radio"
                           class="validate[required]"
                           name="course_category_status" 
                           id="archived" value="3">
                    <label for="archived">Archived</label>
                    <span style="margin-right: 30px"></span>
                    <input type="radio" 
                           class="validate[required]"
                           name="course_category_status" 
                           id="pending" value="4">
                    <label for="pending">Pending</label>
                    <span style="margin-right: 30px"></span>
                    <input type="radio" 
                           class="validate[required]"
                           name="course_category_status" 
                           id="approved" value="5">
                    <label for="approved">Approved</label>
                </div>
            </div>
        </div>
        <div class="form-group col-sm-12">
            <div class="form-group col-sm-8">
                <div class="checkbox checkbox-success">
                    <input id="checkbox1" 
                           name="course_category_home_page_view" 
                           class="styled" type="checkbox"
                           value="2">
                    <label for="course_category_home_page_view">View in Home Page ?
                    </label>
                </div>
            </div>
            <div class="form-group col-sm-8">
                <div id="submit_status"></div>
                <div class="checkbox col-sm-12">
                    <center> 
                        <button type="submit" id="submit" class="btn btn-default" >Submit</button>
                    </center>
                </div>
            </div>
        </div>
        <?php echo form_close(); ?>
    </div>
</div>
<script>
    document.forms['add_course_category'].elements['course_category_access_level'].value = '<?php echo set_value('course_category_access_level'); ?>';
    document.forms['add_course_category'].elements['course_category_status'].value = '<?php echo set_value('course_category_status'); ?>';

    '<?php if (set_value('course_category_home_page_view')) { ?>'
        document.forms['add_course_category'].elements['course_category_home_page_view'].checked = true;
        '<?php } ?>'

</script>