<div class="col-sm-8">
    <div class="row gray">


        <div class="col-sm-12 ">
            <div class="col-sm-12">
                <h3>Classroom Course Invite to Learners</h3>
                <hr>
                <nav class="text-justify">
                    <div class="col-sm-12" style="padding: 0">
                        <span class="clo-sm-4" title="Course Details" style="padding: 0">
                            <a href="" style="cursor:default;">
                                <span class="number badge" style="background-color: gray " >1</span>
                                <span class="text-left label" style="color: #000;font-size: 16px;font-weight: normal">Course Profile</span>
                                <span class="nav-arrow" id="cborder" style="margin-right: 8px"><img src="<?php echo base_url(); ?>image/border.jpg"></span>
                            </a>
                        </span>

                        <span class="clo-sm-4" title="Invite Learners" style="padding: 0">
                            <a href="" style="cursor:default;">
                                <span class="number badge" style="background-color: green">2</span>
                                <span class="text-left label" style="color: #000;font-size: 16px;font-weight: normal">Invite Learners</span>
                                <span class="nav-arrow" id="cborder" style="margin-right: 8px"><img src="<?php echo base_url(); ?>image/border.jpg"></span>
                            </a>
                        </span>

                        <span class="clo-sm-4" title="Review And Publish" style="padding: 0">
                            <a href="" style="cursor:default;">
                                <span class="number badge" style="background-color: gray">3</span>
                                <span class="text-left label" style="color: #000;font-size: 16px;font-weight: normal">Review and Publish</span>

                            </a>
                        </span>

                    </div>

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

            <!--<div class="col-sm-12"><br></div>-->
            <!--<div class="col-md-12" style="border: 1px solid green"></div>-->
            <div class="col-sm-12"><?php
                if ($course_info->course_access_level == 1) {
                    $flag = TRUE;
                } else if ($course_info->course_access_level == 2) {
                    $flag = TRUE;
                } else if ($course_info->course_access_level == 3) {
                    $flag = TRUE;
                } else if ($course_info->course_access_level == 4) {
                    if ($group) {
                        echo 'Please Select Your Group ';
                        $flag = TRUE;
                    } else {
                        echo 'You don\'t have any Group';
                        $flag = FALSE;
                    }
                } else if ($course_info->course_access_level == 5) {
                    $flag = TRUE;
                }
                ?></div>

            <div class="col-sm-12">
                <?php
//                echo validation_errors();
                $url = 'class-schedual-invite-to-learner/' . $classroom_info->url;
                echo form_open_multipart($url, 'class="" id="formID2" name="inviteForm" onsubmit="return checkFields();"');
                ?>
                <!--<form class="cmxform" id="formID2" action="<?php // echo base_url();    ?>class-schedual-invite-to-learner/<?php // echo $classroom_info->url;    ?>" method="post" name="courseForm" enctype="multipart/form-data" >-->

                <div class="col-sm-12">
                    <?php if ($flag == TRUE) { ?>
                        <div class="form-group col-sm-12">
                            <?php if ($course_info->course_access_level == 1) { ?>
                                <div class="form-group col-sm-12">
                                    <label for="course_name">Invite Learners: </label><span class="required"> * </span>
                                    <textarea type="text" 
                                              data-errormessage-value-missing="Email address is Required!" 
                                              class="form-control validate[required, custom[multipleEmail]] text-input"
                                              id="to_address" 
                                              name="to_address"
                                              value="<?php echo set_value('invitation_email'); ?>" 
                                              placeholder="Separate multiple email addresses with a comma (,)"></textarea>
                                </div>
                            <?php } else if ($course_info->course_access_level == 2) { ?>
                                <?php
                                $selected_student = array(0);
                                $to_address = '';
                                ?>
                                <input class="col-sm-12"
                                       data-errormessage-value-missing="Email address is Required!" 
                                       class="form-control validate[required, custom[multipleEmail]] text-input"
                                       type="hidden" name="to_address" id="to_address" value="" >

                                <div class="col-sm-1 column"><h4> To:</h4></div>
                                <div class="col-sm-11 column">
                                    <select name="name_email" style="width: 280px;padding: 8px;" id="tokenize_placeholder" 
                                            data-errormessage-value-missing="Email address is Required!"
                                            class="tokenize-sample validate[ custom[multipleEmail]] text-input" 
                                            multiple="multiple" style="margin: 0px; padding: 0px; border: 0px; display: none;">
                                                <?php
                                                foreach ($link_person as $v_person) {
                                                    ?>
                                            <option value="<?php echo $link_person_info[$v_person->person_link_id]->person_id; ?>">
                                                <?php echo $link_person_info[$v_person->person_link_id]->person_first_name . ' (' . $link_person_info[$v_person->person_link_id]->person_email . ')'; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>
                            <?php } else if ($course_info->course_access_level == 3) { ?>
                                <?php
                                $selected_student = array(0);
                                $to_address = '';
                                ?>
                                <input class="col-sm-12"
                                       data-errormessage-value-missing="Email address is Required!" 
                                       class="form-control validate[required, custom[multipleEmail]] text-input"
                                       type="hidden" name="to_address" id="to_address" value="" >

                                <div class="col-sm-1 column"><h4> To:</h4></div>
                                <div class="col-sm-11 column">
                                    <select name="name_email" style="width: 280px;padding: 8px;" id="tokenize_placeholder" 
                                            class="tokenize-sample " 
                                            multiple="multiple" style="margin: 0px; padding: 0px; border: 0px; display: none;">
                                                <?php
                                                foreach ($link_person as $v_person) {
                                                    ?>
                                            <option value="<?php echo $link_person_info[$v_person->person_link_id]->person_id; ?>">
                                                <?php echo $link_person_info[$v_person->person_link_id]->person_first_name . ' (' . $link_person_info[$v_person->person_link_id]->person_email . ')'; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>
                            <?php } else if ($course_info->course_access_level == 4) { ?>

                                <input class="col-sm-12" type="hidden" name="to_address" id="to_address" value="" >
                                <div class="col-sm-1 column"><h4> To:</h4></div>
                                <div class="col-sm-11 column">
                                    <select name="name_email" style="width: 280px;padding: 8px;" id="tokenize_placeholder" 
                                            class="tokenize-sample" multiple="multiple" style="margin: 0px; padding: 0px; border: 0px; display: none;">
                                                <?php foreach ($group as $v_group) { ?>
                                            <option value="<?php echo $v_group->group_id ?>"><?php echo $v_group->group_name . ' (' . $v_group->group_public_id . ')'; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>

                            <?php } else if ($course_info->course_access_level == 5) { ?>

                                <input class="col-sm-12" type="hidden" name="to_address" id="to_address" value="" >
                                <div class="col-sm-1 column"><h4> To:</h4></div>

                                <div class="col-sm-11 column">
                                    <select name="name_email" style="width: 280px;padding: 8px;" id="tokenize_placeholder" 
                                            class="tokenize-sample" multiple="multiple" style="margin: 0px; padding: 0px; border: 0px; display: none;">
                                                <?php
                                                foreach ($link_person as $v_person) {
                                                    ?>
                                            <option value="<?php echo $link_person_info[$v_person->person_link_id]->person_id; ?>">
                                                <?php echo $link_person_info[$v_person->person_link_id]->person_first_name . ' (' . $link_person_info[$v_person->person_link_id]->person_email . ')'; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>

                            <?php } else { ?>
                                <input class="col-sm-12" type="hidden" name="to_address" id="to_address" value="" >
                                <div class="col-sm-1 column"><h4> To:</h4></div>
                                <div class="col-sm-11 column">
                                    <select name="name_email" style="width: 280px;padding: 8px;" id="tokenize_placeholder" 
                                            class="tokenize-sample" multiple="multiple" style="margin: 0px; padding: 0px; border: 0px; display: none;">
                                                <?php foreach ($all_tt_user as $v_person) { ?>
                                            <option value="<?php echo $v_person->person_id ?>"><?php echo $v_person->person_first_name . ' (' . $v_person->person_email . ')'; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            <?php } ?>

                            <script type="text/javascript">
                                $('select#tokenize_placeholder').tokenize({placeholder: "Please Type Person Email or Name"});
                            </script>

                            <div class="form-group col-sm-12 required" id="email_address_validation"><?php echo form_error('to_address'); ?></div>

                            <div class="col-sm-4 required" id=""><?php echo form_error('course_name'); ?></div>
                        </div>

                        <div class="col-sm-12">
                            <div class="input-group">
                                <label for="course_url">Invitation Message:</label><span class="required"> * </span>
                                <textarea data-errormessage-value-missing="Course Level is Required!" 
                                          class="form-control texteditor validate[required] text-input" 
                                          type="text" rows="7"  cols="65" 
                                          name="invitation_message" id="invitation_message" 
                                          placeholder="Hi,I would like you to enroll in my online course on TalhaTraining.Regards, #Teacher Name"  
                                          value=""></textarea>
                            </div>
                        </div>
                    <?php } ?>

                    <div class="col-sm-12 form-group"><br> 
                        <div class="col-sm-3 col-sm-offset-9">
                            <input type="button" class="col-sm-1 btn green-btn btn-small" value="Preview Email" onclick="preview3();" />
                        </div>
                        <br><br> 
                    </div>

                    <div class="col-sm-12" id="email_preview">

                    </div>

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

                    <div class="col-md-4">
                        <a href="<?php echo base_url() . 'edit-scheduled-class/' . $classroom_info->url; ?>" >
                            Previous
                        </a><br/><br/>
                    </div>

                    <?php if ($flag == TRUE) { ?>
                        <div class="col-md-4">
                            <input type="submit" class="green-btn btn btn-default" name="btn" value="Invite Learner"><br/><br/>
                        </div>
                    <?php } else { ?>
                        <div class="col-md-4"></div>
                    <?php } ?>

                    <div class="col-md-4">
                        <a href="<?php echo base_url() . 'schedule-review-and-publish/' . $classroom_info->url; ?>">Skip Invite Learner</a><br/><br/>
                    </div>
                    <div class="col-md-3"></div>


                </div>
                <!--</form>-->
                <?php echo form_close(); ?>

            </div>

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


    </div>
</div>

<script>
    function _(el) {
        return document.getElementById(el);
    }
    function preview3() {

        var invitationMessage = tinyMCE.get('invitation_message').getContent({format: 'raw', no_events: 1});
//        alert(invitationMessage);
        var formdata = new FormData();
        formdata.append("message", invitationMessage);
        xmlhttp.open("POST", "<?php echo base_url() . 'preview_schedule_email/' . $classroom_id; ?>");

        xmlhttp.addEventListener("load", function () {
            _("email_preview").innerHTML = xmlhttp.responseText;
        }, false);

        xmlhttp.send(formdata);
//        document.getElementById('email_preview').innerHTML = xmlhttp.responseText;
    }

    $(document.forms['inviteForm']).submit(function (event) {
        event.preventDefault();
    });

    var courseAccessLevel = '<?php echo $course_info->course_access_level ?>';
    var emailRegularExpression = "/^(([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)(\s*,\s*|\s*;\s*|\s*$))*$/";
    var validateEmailAddress = false;
    var courseAccessLevelMessage = [
        " ",
        "<span style=color:red>To send mail you must have to give valide email address </span>",
        "<span style=color:red>To send mail you must have to give valide email address from your link </span>",
        "<span style=color:red>To send mail you must have to give valide email address from your link </span>",
        "<span style=color:red>To send mail you must have to select only your group </span>",
        "<span style=color:red>To send mail you must have to give valide email address from your link </span>"
    ];

    function checkFields() {

        var xmlhttp1 = false;
        try {
            xmlhttp1 = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp1 = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp1 = false;
            }
        }
        if (!xmlhttp1 && typeof XMLHttpRequest != 'undefined') {
            xmlhttp1 = new XMLHttpRequest();
        }
        var toAddress = document.getElementById('to_address').value;
        var email_address = toAddress.split(',');

        if (toAddress != '') {

            if (courseAccessLevel == 2 || courseAccessLevel == 3 || courseAccessLevel == 4 || courseAccessLevel == 5) {

                var formdata = new FormData();
                formdata.append("toAddress", toAddress);
                formdata.append("courseAccessLevel", courseAccessLevel);
                xmlhttp1.addEventListener("load", function () {
//                    alert(xmlhttp1.responseText);
                    if (xmlhttp1.responseText == 1) {
                        validateEmailAddress = true;
                    } else if (xmlhttp1.responseText == 2) {
                        validateEmailAddress = false;
                    }
//                        alert(validateEmailAddress);
//                        alert(courseAccessLevelMessage[courseAccessLevel]);
                    if (validateEmailAddress == true) {
                        document.getElementById('email_address_validation').innerHTML = '';
                        document.forms["inviteForm"].submit();
                    } else {
                        _('email_address_validation').innerHTML = courseAccessLevelMessage[courseAccessLevel];
                        $('body').scrollTo(100);
                    }
                }, false);
                xmlhttp1.open("POST", "<?php echo base_url() . 'check_email_id'; ?>");
                xmlhttp1.send(formdata);

            } else if (courseAccessLevel == 1) {
                if (toAddress != '') {
                    patt = /^(([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)(\s*,\s*|\s*;\s*|\s*$))*$/;
                    if (patt.test(toAddress) == true) {
                        document.forms["inviteForm"].submit();
                    } else {
                        _('email_address_validation').innerHTML = courseAccessLevelMessage[courseAccessLevel];
                        $('body').scrollTo(100);
                    }
                } else {
                    _('email_address_validation').innerHTML = courseAccessLevelMessage[courseAccessLevel];
                    $('body').scrollTo(100);
                }
            }
        } else {
            document.getElementById('email_address_validation').innerHTML = courseAccessLevelMessage[courseAccessLevel];
//            var elementOffset = $("#tokenize_placeholder").offset().top;
            $('body').scrollTo(100);
            return false;
        }
    }

    $("#c_days").on("keypress keyup keydown blur change mouseup click input", function () {        
        document.getElementById('email_address_validation').innerHTML = '';
    });

    $("#to_address").on("keypress keyup keydown blur change mouseup click input", function () {        
        document.getElementById('email_address_validation').innerHTML = '';
    });
</script>