<?php
$org_person_id = $this->session->userdata('org_person_id');
?>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="">
        <a href="<?php echo base_url(); ?>super_admin">Dashboard</a>->>
        <a href="<?php echo base_url(); ?>super_admin/user_list/Organization/4/">Organization List</a>->>
        <a href="<?php echo base_url(); ?>super_admin/view_detail/Organization/<?php echo $org_person_id; ?>">Organization Detail</a>->>
        <a href="<?php echo base_url(); ?>super_admin_organization/organization_resource_person/<?php echo $org_person_id; ?>">Resource Person List</a>->>
        <span>Edit Resource Person</span>
    </div>
    <br>
    <h3 class="page-header"><span>Edit <?php echo $user_info->person_first_name . ' ' . $user_info->person_last_name; ?> User</span></h3>
    <p>
        <?php
        $message = $this->session->userdata('message');
        $this->session->unset_userdata('message');
        if ($message) {
            echo $message;
        }
        ?>
    </p>
    <div class="table-responsive col-sm-12">
        <?php echo validation_errors(); ?>
        <form id="formID2" action="<?php echo base_url(); ?>super_admin_organization/edit_trainer_information/<?php echo $org_info->person_id . '/' . $user_info->person_id; ?>" method="post" name="userForm" enctype="multipart/form-data" >

            <input type="hidden" name="resource_person_id" id="resource_person_id" value="<?php echo $trainer_info->resource_person_id; ?>">
            <input type="hidden" name="organization_id" id="organization_id" value="<?php echo $org_info->organization_id; ?>">
            <input type="hidden" name="organization_code" id="organization_code" value="<?php echo $org_info->organization_code; ?>">

            <div class="form-group col-sm-10">
                <div class="form-group col-sm-4">
                    <label for="person_email">Email:</label><span class="required"> * </span>
                    <input type="hidden" name="person_previous_role" id="person_previous_role" value="<?php echo $user_info->person_role; ?>"/>
                    <input type="hidden" name="email_status" id="email_status_value" value="0"/>
                    <input type="email"
                           data-errormessage-value-missing="Person Email is required!" 
                           class="form-control validate[required, custom[email]] text-input"
                           id="person_email" name="person_email" placeholder="Enter email"  value="<?php
                           if (set_value('person_email')) {
                               echo set_value('person_email');
                           } else {
                               echo $user_info->person_email;
                           }
                           ?>" onblur="checkUserEmail(this.value, '<?php echo $user_info->person_id; ?>', 'email_status')"autofocus >
                    <input type="hidden" name="email_status" id="email_status_value" value="0"/>
                </div>
                <script>
                    function checkUserEmail(given_text, personId, objID)
                    {
//                        alert(personId);
                        if (given_text != '') {
                            serverPage = '<?php echo base_url(); ?>super_admin/check_user_email_by_person_id/' + given_text + '/' + personId;  // passing person_email Field value 
                        } else if (given_text == '') {
                            document.getElementById(objID).innerHTML = '<p style="color:red;">Please Enter your Email</p>';
                        }
                        /*
                         *  After Database Check display message 
                         *  if available the message will be Email Address Available
                         *  if same email address exist in database then message show Email Address Already Exists
                         */
                        xmlhttp.open("GET", serverPage);
                        xmlhttp.onreadystatechange = function ()
                        {
                            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                            {
//                                alert(xmlhttp.responseText);
                                if (xmlhttp.responseText == 1)
                                {
                                    document.getElementById(objID).innerHTML = '<p style="color:red;">Email Address Already Exists</p>';
                                    document.getElementById(objID).value = 1;
                                } else if (xmlhttp.responseText == 2)
                                {
                                    document.getElementById(objID).innerHTML = '<p style="color:green;">Email Address Available</p>';
                                    document.getElementById(objID).value = 2;
                                }
                            }
                        }
                        xmlhttp.send(null);
                    }

                </script>
                <div class="form-group col-sm-8" id=""></div>
                <div class="form-group col-sm-4" id="email_status"><?php echo form_error('person_email'); ?></div>
            </div>

            <div class="form-group col-sm-10">
                <div class="form-group col-sm-4">
                    <label for="person_first_name">First Name</label><span class="required"> * </span>
                    <input type="text" 
                           data-errormessage-value-missing="Person First Name is required!"
                           class="form-control validate[required, custom[personName]] text-input" 
                           id="person_first_name" name="person_first_name" value="<?php
                           if (set_value('person_first_name')) {
                               echo set_value('person_first_name');
                           } else {
                               echo $user_info->person_first_name;
                           }
                           ?>" 
                           onblur="makeProfileID();" 
                           placeholder="Enter First Name" >
                </div>

                <div class="form-group col-sm-4">
                    <label for="person_last_name">Last Name</label>
                    <input type="text"  
                           data-errormessage-value-missing=""
                           class="form-control validate[custom[personName]] text-input" 
                           id="person_last_name" name="person_last_name" placeholder="Enter Last Name" value="<?php
                           if (set_value('person_last_name')) {
                               echo set_value('person_last_name');
                           } else {
                               echo $user_info->person_last_name;
                           }
                           ?>" 
                           onblur="makeProfileID();" 
                           onclick="checkFirstName(person_first_name.value, 'first_name_status');" >
                </div>

                <div class="form-group col-sm-4">

                    <label for="url">Profile ID:</label><span class="required"> * </span>
                    <input type="hidden" name="old_url" id="old_url" value="<?php echo $user_info->url; ?>">
                    <input type="hidden" name="url_id" id="url_id" value="<?php echo $user_info->url_id; ?>">
                    <input data-errormessage-value-missing="Profile URl is required!" type="text" 
                           class="form-control validate[required, custom[urlSpecialCaracter], custom[spaceFunction], custom[urlExistence]]" 
                           id="profile_url" name="profile_url"
                           placeholder="Give a Profile URL"  value="<?php
                           if (set_value('profile_url')) {
                               echo set_value('profile_url');
                           } else {
                               echo $user_info->url;
                           }
                           ?>">

                    <input type="hidden" name="url_exist" id="url_exist" value="">

                    <script>

                        $(document).ready(function () {
                            $("#profile_url").on("keypress keyup keydown blur", function () {
                                var profileURL = $(this).val();
                                checkProfileUrl(profileURL, 'email_status');
                            });
                        });

                        function checkProfileUrl(given_text, objID)
                        {
                            var oldURL = document.getElementById('old_url').value;
//                            alert(oldURL);
                            if (given_text != '') {
                                serverPage = '<?php echo base_url(); ?>super_admin/check_profile_url_existance/' + 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('url_exist').value = '1';

                                        } else if (xmlhttp.responseText == 2)
                                        {
                                            document.getElementById('url_exist').value = '';
                                        }
                                    }
                                }
                                xmlhttp.send(null);
                            } else if (given_text == '') {
                                document.getElementById(objID).innerHTML = '<p style="color:red;"></p>';
                            }
                        }
                    </script>
                </div>

                <div class="form-group col-sm-6" id="first_name_status"></div>
                <div class="form-group col-sm-10"><?php echo form_error('person_last_name'); ?></div>

                <div class="form-group col-sm-4">
                    <label for="person_gender">Gender</label>
                    <input type="radio" 
                           data-errormessage-value-missing="Person Gender is required!"
                           class=" validate[required] text-input" 
                           id="person_gender" name="person_gender" value="1" onblur="checkGenderStatus(this.value, 'gender_status');"> Male
                    <input type="radio" 
                           data-errormessage-value-missing="Person Gender is required!"
                           class=" validate[required] text-input" 
                           id="person_gender" name="person_gender" value="2" > Female
                </div>
                <script>
                    document.forms['userForm'].elements['person_gender'].value = '<?php
                           if (set_value('person_gender')) {
                               echo set_value('person_gender');
                           } else {
                               echo $user_info->person_gender;
                           }
                           ?>';
                    function checkGenderStatus(value, objID) {
                        if (value == '') {
                            document.getElementById(objID).innerHTML = '<p style="color:red;">Please Enter your First Name</p>';
                        } else {
                            document.getElementById(objID).innerHTML = '<p style="color:red;"></p>';
                        }
                    }
                </script>

                <div class="form-group col-sm-4" id="gender_status"> </div>

            </div>

            <div class="form-group col-sm-10">
                <div class="form-group col-sm-4">
                    <label for="person_password">Password</label><span class="required"> * <?php echo form_error('person_password'); ?></span>
                    <input type="hidden" name="person_old_password" id="person_old_password" value="<?php echo $user_info->person_password; ?>">
                    <input type="password"  
                           data-errormessage-value-missing="* Password field is required!"
                           class="form-control validate[custom[password]]  text-input" 
                           id="person_password" 
                           name="person_password" 
                           onblur="noticeField(this.value, 'password_status');" 
                           placeholder="Password">
                    <input type="hidden" name="pass_stat" id="pass_stat" value="3">
                </div>

                <div class="form-group col-sm-4">
                    <label for="confirm_password">Confirm Password</label><span class="required"> * </span>
                    <input type="password"  
                           data-errormessage-value-missing="* Person Confirm Password is required!"
                           class="form-control validate[custom[matchPassword]] text-input"
                           id="confirm_password" 
                           name="confirm_password" 
                           onblur="checkConfirmPassword(person_password.value, this.value, 'password_status');" 
                           placeholder="Password" 
                           minlength="6">
                    <input type="hidden" name="pass_match_stat" id="pass_match_stat" value="0">
                </div>

                <script>
                    function noticeField(password, objID) {
                        if (password == '') {
//                            document.getElementById(objID).innerHTML = '<p style="color:red;">Please Enter Password</p>';
                            if (!document.getElementById('person_gender').checked) {
//                                document.getElementById('gender_status').innerHTML = '<p style="color:red;">Please Select Gender</p>';
                            }
                        } else {
                            if (password < 6) {
//                                document.getElementById(objID).innerHTML = '<p style="color:red;"> Minimum length 6</p>';
                            }
                            document.getElementById(objID).innerHTML = '<p style="color:red; font-size:10px;"></p>';
                        }
                    }

                    function checkConfirmPassword(password, confirmPassword, objID) {
                        if (password == '') {
//                            document.getElementById(objID).innerHTML = '<p style="color:red;">Please Enter Password</p>';
                        } else if (password == '' && confirmPassword == '') {
//                            document.getElementById(objID).innerHTML = '<p style="color:red;">Please Enter Password and Confirm Password</p>';
                        } else if (password && confirmPassword == '') {
//                            document.getElementById(objID).innerHTML = '<p style="color:red;">Please Enter Confirm Password</p>';
                        } else if (password === confirmPassword) {
                            document.getElementById(objID).innerHTML = '<p style="color:green;">Password Matched</p>';
                            document.getElementById('pass_match_stat').value = '1';
                        } else {
//                            document.getElementById(objID).innerHTML = '<p style="color:red;">Password is not Matched</p>';
                            document.getElementById('pass_match_stat').value = '0';
                        }
                        if (password < 6) {
//                            document.getElementById(objID).innerHTML = '<p style="color:red;">Password is not Matched</p>';
                        }
                    }
                </script>

                <div class="form-group col-sm-10" id="password_status"></div>
                <div class="form-group col-sm-10" id="password_score"></div>
                <div class="form-group col-sm-4 danger required"><?php echo form_error('person_password'); ?></div>
                <div class="form-group col-sm-5 danger required"><?php echo form_error('confirm_password'); ?></div>
            </div>

            <div class="form-group col-sm-12">
                
                <div class="form-group col-sm-4">
                    <label class="control-label" for="typeahead">Country<span style="color: red">*</span></label>
                    <div class="controls">
                        <select   
                            data-errormessage-value-missing="Person Country is required!"
                            class="form-control validate[required] text-input" 
                            id="person_country" name ="person_country">
                            <script language="javascript">
                    populateCountries("person_country", "person_city");  // ....for Country with State
                            </script>
                        </select>
                    </div>
                    <div class="form-group col-sm-4" id="country_status"></div>
                </div>
                <div class="form-group col-sm-4">
                    <label for="typeahead">City<span style="color: red">*</span></label>
                    <div class="controls">
                        <select  
                            data-errormessage-value-missing="Person First Name is required!"
                            class="form-control validate[required] text-input" 
                            name ="person_city" id ="person_city">
                        </select>
                    </div>
                    <div class="form-group col-sm-4" id="city_status"></div>
                </div>

                <div class="form-group col-sm-12 danger"><?php echo form_error('person_country'); ?></div>

                <div class="form-group col-sm-6">
                    <label for="address">Address</label>
                    <textarea name="person_address" class="form-control" ><?php
                        if (set_value('person_address')) {
                            echo set_value('person_address');
                        } else {
                            echo $user_info->person_address;
                        }
                        ?></textarea>
                </div>

                <div class="form-group col-sm-2">
                    <label for="zip">Zip/Postal Code</label>
                    <input type="text" 
                           data-errormessage-value-missing="Person Zip/Postal Code is required!"
                           class="form-control validate[required] text-input" 
                           name="person_zip" maxlength="5" class="form-control" value="<?php
                           if (set_value('person_zip')) {
                               echo set_value('person_zip');
                           } else {
                               echo $user_info->person_zip;
                           }
                           ?>" >
                </div>
            </div>

            <div class="form-group col-sm-10">
                <div class="form-group col-sm-4">
                    <label for="person_contact_number"> Mobile Number<span style="color: red">*</span></label>
                    <div class="form-group">
                        <input 
                            data-errormessage-value-missing="Person country Code is required!"
                            class="form-control col-sm-2 validate[required] text-input"
                            name="country_code" id ="country_code" style="width:50px;" minlength="3" maxlength="4" value="<?php
                            if (set_value('country_code')) {
                                echo set_value('country_code');
                            } else {
//                            echo $user_info->country_code;
                                $mobile_number = $user_info->person_contact_number;
                                $country_code = strstr($mobile_number, ' ', TRUE);
                                echo $country_code;
                            }
                            ?>">
                        <div class="form-group col-sm-1" style="padding: 5px;">-</div>
                        <?php
                        $country_code = strstr($mobile_number, ' ', FALSE);
                        ?>
                        <input data-errormessage-value-missing="Person Mobile Number is required!"
                               class="form-control col-sm-4 validate[required] text-input"
                               name="person_contact_number" id ="person_contact_number" style="width:120px;" onblur="checkContaNumber();"  minlength="11" maxlength="11" value="<?php
                               if (set_value('person_contact_number')) {
                                   echo set_value('person_contact_number');
                               } else {
                                   echo $country_code;
                               }
                               ?>">
                    </div>

                    <script>
                        function checkContaNumber() {
                            countryCode = document.getElementById('country_code').value;
                            contactNumber = document.getElementById('person_contact_number').value;
                            if (countryCode != '' && contactNumber == '') {
                                document.getElementById('person_contact_number_status').innerHTML = 'Please Give your Contact Number if any <p>or Remove the Country code<p>';
                            }
                            if (countryCode == '' && contactNumber != '') {
                                document.getElementById('person_contact_number_status').innerHTML = 'Please Give your Country code';
                            } else {
                                document.getElementById('person_contact_number_status').innerHTML = '';
                                submitChangable = true;
                            }
                        }
                    </script>

                </div>
                <div class="form-group col-sm-7 required" id="person_contact_number_status"><?php echo form_error('person_contact_number'); ?></div>
            </div>

            <div class="form-group col-sm-10">
                <div class="form-group col-sm-10">
                    <!--Old Images -->
                    <input type="hidden" id="user_image" name="person_image" value="<?php echo $user_info->person_image; ?>">
                    <img style="float:left;width: 50px; height: 50px;" src="<?php echo base_url() . $user_info->person_image_thumb; ?>" />
                    <input type="hidden" id="user_image_thumb" name="person_image_thumb" value="<?php echo $user_info->person_image_thumb; ?>">

                    <div id="image_field">
                        <label for="person_new_image" style="float:left;">Image</label><br><br>
                        <input style="float:left;" type='file' onchange="readURL(this, 'photo_0');" name="person_new_image" value="<?php echo set_value('person_image'); ?>"/>
                    </div>
                    <div id="image" style="float:left;width: 150px; height: 50px;" > </div>
                    <script>
                        function readURL(input, objID) {
                            var node = document.createElement("img");
                            var parentDIV = document.getElementById("image");
                            parentDIV.appendChild(node);
                            document.getElementById("image").appendChild(node);
                            node.setAttribute("id", objID);
                            node.setAttribute("src", "");

                            var file = input.files[0];
                            if (file.size > 300000) {
                                alert('Your Selected file size exceeded!!! max File Size 300 KB and Resulation ');
                                serverPage = '<?php echo base_url(); ?>super_admin/load_image_field';  // passing person_email Field value 
                                xmlhttp.open("GET", serverPage);
                                xmlhttp.onreadystatechange = function ()
                                {
                                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                                    {
                                        document.getElementById('image').innerHTML = 'Maximum File Size 300 KB and Resulation 1024x1024';
                                        document.getElementById('image_field').innerHTML = xmlhttp.responseText;
                                    }
                                }
                                xmlhttp.send(null);
                            } else {
                                var list = document.getElementById("image");
                                list.removeChild(list.childNodes[0]);
                                if (input.files && input.files[0]) {
                                    var reader = new FileReader();
                                    reader.onload = function (e) {
                                        $('#' + objID)
                                                .attr('src', e.target.result)
                                                .width(50)
                                                .height(50);
                                    };
                                    reader.readAsDataURL(input.files[0]);
                                }
                            }
                        }
                    </script>
                </div>
            </div>

            <div class="form-group col-sm-10">
                <div class="form-group col-sm-4">
                    <label for="person_role">Person Role:</label><span class="required"> * </span>
                    <select data-errormessage-value-missing="Person Role is required!"
                            class="form-control validate[required] text-input"
                            name="person_role" 
                            id="person_role"
                            onchange="checkPersonRole(this.value);">
                        <option value="3">Resource Person</option>
                    </select>
                </div>

                <input type="hidden" name="resource_person_id" id="resource_person_id" value="<?php echo $user_info->resource_person_id; ?>">
                <input type="hidden" name="selected_organization_id" id="selected_organization_id" value="individual">
                <div class="form-group col-sm-4" id="user_role_status"></div>

            </div>

            <div>
                <script language="javascript">
                    document.forms['userForm'].elements['person_role'].value = '<?php
                               if (set_value('person_role')) {
                                   echo set_value('person_role');
                               } else {
                                   echo $user_info->person_role;
                               }
                               ?>';
                </script>
            </div>
            <div class="form-group col-sm-8">

                <div class="form-group col-sm-8">
                    <label for="person_status">Status</label><span class="required"> * <?php echo form_error('status'); ?></span>
                    <input type="radio" 
                           data-errormessage-value-missing="Person Status is required!"
                           class="validate[required] text-input"
                           name="person_status" id="person_status1" value="1"  />Active
                    <input 
                        data-errormessage-value-missing="Person Status is required!"
                        class="validate[required] text-input"
                        type="radio" name="person_status" id="person_status2" value="0" />Inactive
                    <script>
                        document.forms['userForm'].elements['person_status'].value = '<?php
                               if (set_value('person_role')) {
                                   echo set_value('person_role');
                               } else {
                                   echo $user_info->person_status;
                               }
                               ?>';
                    </script>
                </div>

                <div class="checkbox col-sm-6" >
                    <button type="submit" id="submit" class="btn btn-default">Submit</button>
                    <button type="reset" class="btn btn-default">Reset</button>
                </div>
            </div>

        </form>
    </div>
</div>

<script>
    function scorePassword(pass) {
        var score = 0;
        if (!pass)
            return score;

        // award every unique letter until 5 repetitions
        var letters = new Object();
        for (var i = 0; i < pass.length; i++) {
            letters[pass[i]] = (letters[pass[i]] || 0) + 1;
            score += 5.0 / letters[pass[i]];
        }

        // bonus points for mixing it up
        var variations = {
            digits: /\d/.test(pass),
            lower: /[a-z]/.test(pass),
            upper: /[A-Z]/.test(pass),
            nonWords: /\W/.test(pass),
        }

        variationCount = 0;
        for (var check in variations) {
            variationCount += (variations[check] == true) ? 1 : 0;
            (variations[check] == false) ? $("#password_status").text('A-Z, a-z, 0-9, minumum one special charecter') : '';
        }
        score += (variationCount - 1) * 10;

        var digit = (/\d/.test(pass)) ? 1 : 0;
        var small_letter = (/[a-z]/.test(pass)) ? 1 : 0;
        var capital_letter = (/[A-Z]/.test(pass)) ? 1 : 0;
        var special_letter = (/\W/.test(pass)) ? 1 : 0;
        if (digit && small_letter && capital_letter && special_letter) {
            document.getElementById('pass_stat').value = 1;
        } else {
            document.getElementById('pass_stat').value = 0;
        }
        return parseInt(score);
    }

    function checkPassStrength(pass) {
        var score = scorePassword(pass);
        if (score > 70)
            return "";
//            return "strong";
        if (score > 50)
            return "";
//            return "good";
        if (score >= 30)
            return "";
//            return "weak";
        return "";
    }

    $(document).ready(function () {

        $("#person_password").on("keypress keyup keydown onblur", function () {
            var pass = $(this).val();
            if (pass == '') {
                document.getElementById('pass_stat').value = 3;
            }
            $("#password_status").text(checkPassStrength(pass));
            $("#strength_score").text(scorePassword(pass));

        });
    });

    document.forms['userForm'].elements['person_country'].value = '<?php
                               if (set_value('person_country')) {
                                   echo set_value('person_country');
                               } else {
                                   echo $user_info->person_country;
                               }
                               ?>';

    populateStates('person_country', 'person_city');

    document.forms['userForm'].elements['person_city'].value = '<?php
                               if (set_value('person_city')) {
                                   echo set_value('person_city');
                               } else {
                                   echo $user_info->person_city;
                               }
                               ?>';

</script>