<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", "");

        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]);
        }
    }

    //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 ("You are using Microsoft Internet Explorer.");
    } catch (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) {
            //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 checkUserEmail(given_text, objID)
    {
        if (given_text != '') {
            serverPage = '<?php echo base_url(); ?>super_admin/check_user_email/' + given_text;
        } else if (given_text == '') {
            document.getElementById(objID).innerHTML = '<p style="color:red;">Please Enter your Email</p>';
        }
        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(objID).innerHTML = '<p style="color:red;">Email Address Already Exists</p>';
                    document.getElementById('email_status_value').value = 1;
                    document.getElementById('submit').disabled = true;
                    document.getElementById('agree').checked = false;
                } else if (xmlhttp.responseText == 2)
                {
                    document.getElementById(objID).innerHTML = '<p style="color:green;">Email Address Available</p>';
                    document.getElementById('email_status_value').value = 2;
                    document.getElementById('agree').checked = false;
                }
            }
        }
        xmlhttp.send(null);
    }

    function noticeField(password, objID) {
        if (password == '') {
            document.getElementById(objID).innerHTML = '<p style="color:red;">Please Enter Password</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 Reenter Password</p>';
        } else if (password === confirmPassword) {
            document.getElementById(objID).innerHTML = '<p style="color:green;">Password Matched</p>';
        } else {
            document.getElementById(objID).innerHTML = '<p style="color:red;">password is not matched</p>';
        }
    }

    function manageSubmitButton() {

        var agree = document.getElementById('agree').checked;
        var email = document.getElementById('email_status_value').value;
        if (agree && email == 2) {
            document.getElementById('submit').disabled = false;
        } else {
            document.getElementById('submit').disabled = true;
        }
    }


</script>

<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>->>
        <span>Email To User <?php // echo $user_role;                         ?> User</span>
    </div>
    <!--<h1 class="page-header">Add User Form</h1>-->
    <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 action="<?php echo base_url(); ?>super_admin/send_mail_to_user" method="post" name="user" enctype="multipart/form-data" >
            <div class="form-group col-sm-12">
                <div class="form-group col-sm-12">.</div>
                <div class="form-group col-sm-10">
                    <label for="to_address">TO:</label><span class="required"> * </span>
                    <?php ?>
                    <textarea type="text" class="form-control" id="to_address" name="to_address"  placeholder="Enter email"  
                              value="" 
                              onblur="checkUserEmail(this.value, 'email_status')"><?php
                                  for ($i = 0; $i < count($user_email_address); $i++) {
                                      echo $user_email_address[$i]->user_email.', ';
                                  }
                                  ?>
                    </textarea>
                </div>
                
                <div class="form-group col-sm-4" id="email_status"></div>
                <div class="form-group col-sm-4"><?php echo form_error('to_address'); ?></div>                
            </div>

            <div class="form-group col-sm-12">
                <div class="form-group col-sm-8">
                    <label for="subject">Subject: </label><span class="required"> * </span>
                    <input type="text" class="form-control" id="subject" name="subject" placeholder="Enter Subject"  value="<?php echo set_value('subject'); ?>" >
                </div>
                <!--<div class="form-group col-sm-8" id=""></div>-->
                <div class="form-group col-sm-4" id="email_status"></div>
                <div class="form-group col-sm-4"><?php echo form_error('subject'); ?></div>                
            </div>

            <div class="form-group col-sm-12">
                <div class="form-group col-sm-12">
                    <label for="message"></label>
                    <textarea name="message" class="form-control" rows="10" cols="20" ><?php echo set_value('address'); ?></textarea>
                </div>
            </div>

            <div class="form-group col-sm-10">
                <div class="form-group col-sm-8">
                    <label for="attachment" style="float:left;">Attach File: </label><span class="required"> * <?php echo form_error('attachment'); ?></span>
                    <input style="float:left;" type='file' onchange="readURL(this, 'attachment');" name="attachment" value="<?php echo set_value('attachment'); ?>"/>
                    <div id="image" style="float:left;width: 50px; height: 50px;" ></div>
                </div>
                <button type="submit" id="submit" class="btn btn-default">Send Mail</button>
                <button type="reset" class="btn btn-default">Reset</button>
            </div>

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


<script type="text/javascript">
    document.forms['userForm'].elements['country'].value = '<?php echo set_value('country'); ?>';
    document.forms['userForm'].elements['city'].value = '<?php echo set_value('city'); ?>';
    document.forms['userForm'].elements['user_role_id'].value = '<?php echo set_value('user_role_id'); ?>';
    document.forms['userForm'].elements['status'].value = '<?php echo set_value('status'); ?>';
</script>