<script>
    var urlValue = '';
    var sat='1';
</script>


<div class="col-sm-8">
    <div class="row gray">
        <div class="col-sm-12 padding-zero">
            <div class="col-sm-12">
                <h3>Update Group </h3>  
                <hr>
            </div>
            
            <div class="col-sm-12"><br></div>
            <!--<div class="col-md-12" style="border: 1px solid green"></div>-->
            <div class="col-sm-12"><br></div>
            <div class="col-sm-12">

                <div class="col-sm-12  ">
                    
                <?php
                 $url =  base_url() . 'group/' . $group_details->url . '/edit-group';;
                 echo form_open_multipart($url, 'class="cmxform" enctype="multipart/form-data" id="formID2" method="post" name="create_group" onsubmit="return submitGroup()"');
                  ?>
                    <?php //echo validation_errors(); //id="formID2" ?>
                    <!--<form id="formID2" action=""  >-->
                        <div class="form-group col-sm-12">
                            <div class="input-group">
<!--                                <input type="text" 
                                       data-errormessage-value-missing="Group Name is required!" 
                                       class="form-control validate[required,custom[special_char]]"   
                                       name="group_name" id="group_name"
                                       value="<?php
                                      // if (set_value('group_name')) {
                                         //  echo set_value('group_name');
                                      // } else {
                                         //  echo $group_details->group_name;
                                      // }
                                       ?>" 
                                       placeholder="Please Type Group Name.." >-->
                                   <input name="group_name" id="group_name" 
                                          type="text" 
                                          class="form-control" 
                                           value="<?php
                                       if (set_value('group_name')) {
                                           echo set_value('group_name');
                                       } else {
                                           echo $group_details->group_name;
                                       }
                                       ?>" 
                                           oninput="checkgroupUrlValue('',this.value,urlValue)"
                                            value="<?php echo set_value('group_name'); ?>"
                                           placeholder="Please Type Group Name.."   >
                                   <span style="color:red;"><?php echo form_error('group_name'); ?></span>
                                <span class="input-group-addon"  title="Please Type Group Name"><i class="fa fa-group"></i></span>
                            </div>
                                <script>
                                        function checkgroupUrlValue(event,videoCourseTitle, urlInput) {
                                            if (event !== '') {
                                             sat = '2';
                                             } 
                                        if (sat === '1') {
                                                makegroupUrl(videoCourseTitle);
                                            } else if(sat === '2') {
                                                checkgroupURLExists(urlInput, '');
                                            }
                                    }
                                    </script>
                                
                            <br>
                        </div>
                    
                        <div class="form-group col-sm-6">
                            <select name="group_type" id="group_type"
                                    data-errormessage-value-missing="Group type is required!"
                                    class="form-control" 
                                    onchange="otherCategory(this.value)">
                                <option value="">Select Type</option>
                                <option value="<?php echo $group_type->group_type_id; ?>"><?php echo $group_type->group_type_name; ?></option>
                                <option value="other">Other</option>
                            </select>
                            <span style="color:red;"><?php echo form_error('group_type'); ?></span>
                            <div id="type_error" class="form-group col-sm-12" style="color: red;">
                             
                         </div>
                            <input type="hidden" name="grp_typ_status" id="grp_typ_status"value="0">

                            <script>
                                document.forms['create_group'].elements['group_type'].value = '<?php
                                if (set_value('group_type')) {
                                    echo set_value('group_type');
                                } else {
                                    echo $group_details->group_type_id;
                                }
                                ?>';
                            </script>
                        </div>

                        <div class="form-group col-sm-6" id="other_group_type" style="display: none">
                            <div class="input-group">
                                <input type="text" class="form-control" 
                                       data-errormessage-value-missing='Group type is required!' 
                                       class='form-control'
                                       name='group_other_type' 
                                       id='group_other_type' 
                                       value='<?php
                                       if (set_value('group_other_type')) {
                                           echo set_value('group_other_type');
                                       }
                                       ?>' 
                                       placeholder='Please Type New Group type'
                                       onblur='checkGroupType(this.value);' >
                                <span class="input-group-addon" title="Please Type New Group type" ><i class="fa fa-retweet"></i></span>
                                <span style="color:red;"><?php echo form_error('group_other_type'); ?></span>
                            </div>
                        </div>
                    

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

                        <script>
                            function otherCategory(givenText) {

                                if (givenText == 'other') {
                                    var Othercategory = document.getElementById('other_group_type');
                                    Othercategory.setAttribute("style", 'display:block; ');
                                } else {
                                    var Othercategory = document.getElementById('other_group_type');
                                    Othercategory.setAttribute("style", 'display:none; ');
                                }
                            }
                        </script>

                        <div class="form-group col-sm-6">
                            <div class="input-group">
                                <input  type="hidden" name="pre_code" id="pre_code" value="<?php echo $group_details->group_public_id; ?>">
                                <input type="text" 
                                       data-errormessage-value-missing="Invitation code is required!"
                                       class="form-control" 
                                       name="group_public_id" 
                                       id="group_public_id" 
                                       value="<?php
                                       if (set_value('group_public_id')) {
                                           echo set_value('group_public_id');
                                       } else {
                                           echo $group_details->group_public_id;
                                       }
                                       ?>"
                                       placeholder="Please Type  Invitation Code.."   
                                       onkeyup="makerequest(this.value);" onblur="makerequest(this.value);">
                                <input type="hidden" id="incode" name='incode' />
                                
                                <span class="input-group-addon"  title="Please Type  Invitation Code"><i class="fa fa-newspaper-o"></i></span>
                            </div>
                            <span style="color:red;"><?php echo form_error('group_public_id'); ?></span>
                        </div>

                        <div class="form-group col-sm-6">
                            <div class="input-group">
                                <input type="hidden" name="old_url" id="old_url" value="<?php echo $group_details->url; ?>">
<!--                                <input type="text" data-errormessage-value-missing="Group url is required!"
                                       class="form-control validate[required, custom[groupurl], custom[spaceFunction], custom[url_group_Existence]] text-input"  
                                       name="group_url" id="group_url"
                                       value="<?php
                                       //if (set_value('group_url')) {
                                          /// echo set_value('group_url');
                                       //} else {
                                         //  echo $group_details->url;
                                       //}
                                       ?>"
                                       placeholder="Please Type Group URL.." 
                                       onkeyup="checkurl(this.value);" onblur="checkurl(this.value);"
                                       >-->
                                 <input name="group_url" id="group_url" type="text" 
                                        value="<?php
                                       if (set_value('group_url')) {
                                           echo set_value('group_url');
                                       } else {
                                           echo $group_details->url;
                                       }
                                       ?>"
                                           class="form-control" 
                                           oninput="checkgroupUrlValue(event,group_name.value,this.value)"
                                          value="<?php echo set_value('group_url'); ?>"
                                           placeholder="Please Type Group URL.." 
                                           onkeyup="checkurl(this.value);" onblur="checkurl(this.value);">
                                <input type="hidden" id="url_exist" value="">
                                <input type="hidden" id="urlcode" name='urlcode' value=''/>
                                <input type="hidden" class="span6 typeahead" id="typeahead"  name="url_id" value="<?php echo $group_details->url_id ?>" >
                                <span class="input-group-addon"  title="Please Type Group URL"><i class="fa fa-link"></i></span>
                            </div>
                            <span style="color:red;"><?php echo form_error('group_url'); ?></span>
                        </div>


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

                        <div class="form-group col-sm-12" >
                            <div class="input-group screen1280" id="" >
                                <center>  
                                    <textarea   name="group_description" id="group_description" class="form-control  resoulation1280 " rows="4"   ><?php
                                        if (set_value('group_description')) {
                                            echo set_value('group_description');
                                        } else {
                                            echo $group_details->group_description;
                                        }
                                        ?></textarea>
                                </center>
                            </div>

                        </div>


                        <div class="col-sm-12"><br> <br></div>
                        <div class="form-group col-sm-6">
                            <?php if($group_details->group_permission==1){ ?>
                            <select data-errormessage-value-missing="Permission is required!"
                                    class="form-control"
                                    id="group_permission"  name="group_permission" >
                                <option value=""> -Select User Permission - </option>
                                <option value="1">Public</option>
                               
                            </select>
                            <?php } else if($group_details->group_permission==2){?>
                            <select data-errormessage-value-missing="Permission is required!"
                                    class="form-control"
                                    id="group_permission"  name="group_permission" >
                                <option value=""> -Select User Permission - </option>
                                <option value="1">Public</option>
                                <option value="2">All Talha Training User</option>
                            </select>
<!--                            <?php// } else if($group_details->group_permission==3){?>
                             <select data-errormessage-value-missing="Permission is required!"
                                    class="form-control validate[required] text-input"
                                    id="group_permission"  name="group_permission" >
                             <option value=""> -Select User Permission - </option>
                                <option value="1">Public</option>
                                <option value="2">All Talha Training User</option>
                                <option value="3">My link</option>
                                <option value="4">Only group Member</option>
                             </select>-->
                            <?php }else if($group_details->group_permission==4){?>
                                 <select data-errormessage-value-missing="Permission is required!"
                                    class="form-control"
                                    id="group_permission"  name="group_permission" >
                             <option value=""> -Select User Permission - </option>
                                <option value="1">Public</option>
                                <option value="2">All Talha Training User</option>
                                <option value="4">Only group Member</option>
                             </select>
                            <?php } ?>
                            <script>
                                document.forms['create_group'].elements['group_permission'].value = '<?php
                                        if (set_value('group_permission')) {
                                            echo set_value('group_permission');
                                        } else {
                                            echo $group_details->group_permission;
                                        }
                                        ?>';
                            </script>
                            <span style="color:red;"><?php echo form_error('group_permission'); ?></span>
                        </div>

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

                        <div class="form-group col-sm-12">
                            <label class="col-sm-3" for="group_status">Group Status:</label>
                            <div class="radio radio-success col-sm-9 radio-margin" >
                                <input type="radio" 
                                       data-errormessage-value-missing="Group Status is Required!" 
                                       class=""  
                                       name="group_status" id="published" value="1" >
                                <label for="published">Published</label>
                                <span style="margin-right: 30px"></span>
                                <input type="radio"
                                       data-errormessage-value-missing="Group Status is Required!" 
                                       class=""
                                       name="group_status" id="unpublished" value="0">
                                <label for="unpublishded">Unpublished</label>
                            </div>
                            <script>
                                document.forms['create_group'].elements['group_status'].value = '<?php
                                        if (set_value('group_status')) {
                                            echo set_value('group_status');
                                        } else {
                                            echo $group_details->group_status;
                                        }
                                        ?>';
                            </script>
                        </div>

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

                        <div class="form-group col-sm-2 col-sm-offset-5">
                            <input type="submit" class="green-btn btn btn-default" value="Update"><br/><br/>
                        </div>
                        <div class="col-sm-3"></div>
                    <?php echo form_close(); ?>
                <!--</form>-->
                </div>

            </div>

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

    </div>
</div>
<script type="text/javascript" src="<?php echo base_url(); ?>front_end_resource/group/group.js">
</script>
<script>
    checkgroupURLExistsURL = '<?php echo base_url() . 'fornt-end-group-url-exists' ?>';
</script>
<script>
      function  submitGroup(){
           var inco_stat= document.getElementById("grp_typ_status").value;
           if(inco_stat=='1'){
            //   alert('no hello');
               return false;
           }else{
               //alert(' hello');
        return true;
     }
    }
</script>
<script type="text/javascript">
<!--
//Create a boolean variable to check for a valid Internet Explorer instance.
    var xmlhttp = false;
//Check if we are using IE.
    try {
//If the Javascript version is greater than 5.
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
//alert(xmlhttp);
//alert ("You are using Microsoft Internet Explorer.");
    } catch (e) {
        //alert(e);

//If not, then use the older active x object.
        try {
//If we are using Internet Explorer.
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//alert ("You are using Microsoft Internet Explorer");
        } catch (E) {
            // alert(E);
//Else we must be using a non-IE browser.
            xmlhttp = false;
        }
    }
//If we are using a non-IE browser, create a javascript instance of the object.
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
//alert ("You are not using Microsoft Internet Explorer");
    }

    function makerequest(given_text)
    {
        var preCode = document.getElementById('pre_code').value;
//        alert(given_text);
        if (given_text == '') {
            document.getElementById("incode").setAttribute("value", 1);
        }

        var patt = new RegExp(" ");
        var res = patt.test(given_text);

        if (res) {

        } else {
            if (given_text) {
                serverPage = '<?php echo base_url(); ?>group/check_public_group_code_for_edit/' + given_text + '/' + preCode;

                xmlhttp.open("GET", serverPage);
                xmlhttp.onreadystatechange = function ()
                {
//	alert(xmlhttp.readyState);
//	alert(xmlhttp.status);
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                    {
//                        alert(xmlhttp.responseText);
                        if (xmlhttp.responseText == 1)
                        {
                            document.getElementById("incode").setAttribute("value", 0);
                        }
                        if (xmlhttp.responseText == 2)
                        {
                            document.getElementById("incode").setAttribute("value", 1);
                        }
                    }
                }
            }
        }
        xmlhttp.send(null);
    }


    function checkurl(given_text)
    {
//        alert(given_text);
        var oldURl = document.getElementById('old_url').value;
        if (given_text == '') {
            document.getElementById("urlcode").setAttribute("value", 1);
        }

        var patt = new RegExp(" ");
        var res = patt.test(given_text);

        if (res) {

        } else {
            if (given_text) {
                serverPage = '<?php echo base_url(); ?>group/check_group_url_for_edit/' + given_text + '/' + oldURl;
                xmlhttp.open("GET", serverPage);
                xmlhttp.onreadystatechange = function ()
                {
//                    alert(xmlhttp.readyState);
//                    alert(xmlhttp.status);
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                    {
//                        alert(xmlhttp.responseText);
                        if (xmlhttp.responseText == 1)
                        {
                            document.getElementById("urlcode").setAttribute("value", 0);
                        }
                        if (xmlhttp.responseText == 2)
                        {
                            document.getElementById("urlcode").setAttribute("value", 1);
                        }
                    }
                }
            }
        }
        xmlhttp.send(null);
    }
      function checkGroupType(value){
          var xmlhttp4 = false;
        try {
            xmlhttp4 = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp4 = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp4 = false;
            }
        }
        if (!xmlhttp4 && typeof XMLHttpRequest != 'undefined') {
            xmlhttp4 = new XMLHttpRequest();
        }
        
       
           
        var url='<?php echo base_url(); ?>' +'check-edit-front-end-group-type';
            
           
            var formdata = new FormData();
            formdata.append("value", value);
           
          
        xmlhttp4.addEventListener("load", function () {
            
            if(xmlhttp4.responseText==1){
                document.getElementById('type_error').innerHTML='This Group type exists.please create another one or select this one from options. ';
                document.getElementById("grp_typ_status").value=1;
        
        }
            else{
                     document.getElementById('type_error').innerHTML="";
                     document.getElementById("grp_typ_status").value=0;    
        }
           
            }, false);
            xmlhttp4.addEventListener("error", function () {
                //alert("not upload");
            }, false);
            xmlhttp4.addEventListener("abort", function () {
               // alert("Aborted");
            }, false);

            xmlhttp4.open("POST", url);
            xmlhttp4.send(formdata);
    }

//-->
</script>

