<?php include("parent/callMe.php"); ?>
<?php  include("sessioncheck.php"); ?>
<?php

if(isset($_POST['action'])){


  $class=$_POST['class'];   
  $numbers=$_POST['numbers'];   
  $message=$_POST['text_fld'];    
  $branch=$_SESSION['branch'];
  $limit=$_POST['limit'];
  $brand_limit=$_POST['brand_limit'];
  $msgcount=intVal($_POST['msgcount1']);

  $numbers2=explode(",",$numbers);
  $cell_no="";

  foreach($numbers2 as $numb){
    
  // adding prefix country code


      if(substr($numb,0,1)=="3"){
      $number1 = "92".$numb;
      }else if(substr($numb,0,1)=="0"){
      $number1 =  "92".substr($numb,1);
      }
      else if(substr($numb,0,2)=="92"){
      $sendmsg1 =  $numb;
      }
      else{
       $number1 = "00";
      }

      $cell_no.=$number1.",";
  }


  $cell_no=rtrim($cell_no,",");

  $username = SMSAPI_USER;
  $password = SMSAPI_PASS;
  $to = $cell_no;



  if($_POST['from']!="" || $_POST['from']!=NULL ){
    $from = $_POST['from'];   
  }else {      
    $from=$_SESSION['brand'];  
  }
   
  $url = "http://lifetimesms.com/json";

$parameters = array("api_token" => SMSAPI_TOKEN,
        "api_secret" => SMSAPI_CODE,
        "to" => $to,
        "from" => $from,
        "type" => "unicode",
        "message" => $_POST['text_fld']
      );

  $ch = curl_init();
  $timeout  =  30;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  $response = curl_exec($ch);
  curl_close($ch);

  //print_r($response);

  // inserting log detials into db

  $sms=0;
  foreach($numbers2 as $numb){
    $br=$_SESSION['branch'];
    $query="insert into edu_messages_details(msg_sender,msg_from,msg_details,msg_status,branch_id,other_msg,msg_count) values('$from','$numb','$message','$status','$br','1','$msgcount')";

    echo "<br/><br/><br/>".$query;

    $result = $m->query($query);
    $sms++;
  }


  // updating credit limit of sms

  $val=intVal($brid[0]['sms_limit'])-($sms*$msgcount);
  $data = array('sms_limit' => $val);
  $m->where("branch_id", $_SESSION['branch']);
  $result =  $m->update ('edu_branch_config', $data);


  $_SESSION['sent_msg']=1;
  
  //echo "<script>window.location='sent_message.php'</script>";
}

?>



<!DOCTYPE html>
<html lang="en">
  <head>
    
    <title>New Urdu SMS | EduPortal</title>
    <?php include('RootMe/css_links.php');?>
   

<!-- form validation -->

<script type = "text/javascript" src="js/jquery.js"> </script>
<script type = "text/javascript" src="js/validate.min.js"> </script>


<script src="js/urdutextbox.js"></script>
<script>

window.onload = myOnload;

function myOnload(evt){

  MakeTextBoxUrduEnabled(message);//enable Urdu in html text area
}

</script>


<style>
.fborder{border: 1px solid #cccccc;}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
   color: #b7b7b7;
}

::-ms-input-placeholder { /* Microsoft Edge */
   color: #b7b7b7;
}
.styletext{line-height: 1.5em; font-family: Arial, Helvetica, sans-serif; font-size: 14px;}


</style>



<!-- Validation files -->

<script type = "text/javascript" src="http://technology-sol.com/cms/js/jquery.js"> </script>
<script type = "text/javascript" src="http://technology-sol.com/cms/js/validate.min.js"> </script>


  </head>
  
<script>
$(document).ready(function(){
    $("#button").click(function(){
      //alert("786");
        // $("#group").slideToggle();
    });
});
</script>

<!-- load counter functions -->
<script type="text/javascript" src="js/counter_funcs.js"></script>
<!-- set the maximum character length value for the field -->

<script type="text/javascript">

  var MAX = 20;
  function check_char_length(elem_id, max_len)
  {
      if(check_content_length(elem_id, max_len)) {
    return true;
      } else {
    // too many chars
    alert("Maximum input length is " + max_len + " characters");
    return false;
      }
  }
    /*]]>*/
    </script>



<script>




function getEmployees(b)
{

  var ajx;  // The variable that makes Ajax possible!
  
  if(window.XMLHttpRequest)
  ajx=new XMLHttpRequest();
  ajx.onreadystatechange = function()
  {

    if(ajx.readyState == 4)
    {
          // alert(ajx.responseText);  
          
          if(ajx.responseText!=null){
            var total=(ajx.responseText).split(",");
            total=total.length;
            document.getElementById("recipient").innerHTML=total;
          }
         // alert(ajx.responseText);
          document.getElementById("numbers").value= ajx.responseText.trim();
    }
  }
  ajx.open("GET", "get_emp_numbers.php?b="+b);
  ajx.send(); 

}

function getOldEmployees(b)
{

  var ajx;  // The variable that makes Ajax possible!
  
  if(window.XMLHttpRequest)
  ajx=new XMLHttpRequest();
  ajx.onreadystatechange = function()
  {

    if(ajx.readyState == 4)
    {
          // alert(ajx.responseText);  
          
          if(ajx.responseText!=null){
            var total=(ajx.responseText).split(",");
            total=total.length;
            document.getElementById("recipient").innerHTML=total;
          }
         // alert(ajx.responseText);
          document.getElementById("numbers").value= ajx.responseText.trim();
    }
  }
  ajx.open("GET", "get_oldemp_numbers.php?b="+b);
  ajx.send(); 

}

function getOldStudents(b)
{

  var ajx;  // The variable that makes Ajax possible!
  
  if(window.XMLHttpRequest)
  ajx=new XMLHttpRequest();
  ajx.onreadystatechange = function()
  {

    if(ajx.readyState == 4)
    {
          // alert(ajx.responseText);  
          
          if(ajx.responseText!=null){
            var total=(ajx.responseText).split(",");
            total=total.length;
            document.getElementById("recipient").innerHTML=total;
          }
         // alert(ajx.responseText);
          document.getElementById("numbers").value= ajx.responseText.trim();
    }
  }
  ajx.open("GET", "get_oldstd_numbers.php?b="+b);
  ajx.send(); 

}

function getContacts(b)
{

  var ajx;  // The variable that makes Ajax possible!
  
  if(window.XMLHttpRequest)
  ajx=new XMLHttpRequest();
  ajx.onreadystatechange = function()
  {

    if(ajx.readyState == 4)
    {
          // alert(ajx.responseText);  
          
          if(ajx.responseText!=null){
            var total=(ajx.responseText).split(",");
            total=total.length;
            document.getElementById("recipient").innerHTML=total;
          }
          // alert(ajx.responseText);
          document.getElementById("numbers").value= ajx.responseText.trim();
    }
  }
  ajx.open("GET", "get_contacts.php?b="+b);
  ajx.send(); 

}


function getCategory(b)
{

  var ajx;  // The variable that makes Ajax possible!
  
  if(window.XMLHttpRequest)
  ajx=new XMLHttpRequest();
  ajx.onreadystatechange = function()
  {

    if(ajx.readyState == 4)
    {
          // alert(ajx.responseText);  
          
          if(ajx.responseText!=null){
            var total=(ajx.responseText).split(",");
            total=total.length;
            document.getElementById("recipient").innerHTML=total;
          }
          // alert(ajx.responseText);
          document.getElementById("numbers").value= ajx.responseText.trim();
    }
  }
  ajx.open("GET", "get_cat_contacts.php?cat_id="+b);
  ajx.send(); 

}


function getFeeDefaulters(b)
{

  var ajx;  // The variable that makes Ajax possible!
  
  if(window.XMLHttpRequest)
  ajx=new XMLHttpRequest();
  ajx.onreadystatechange = function()
  {

    if(ajx.readyState == 4)
    {
          // alert(ajx.responseText);  
          
          if(ajx.responseText!=null){
            var total=(ajx.responseText).split(",");
            total=total.length;
            document.getElementById("recipient").innerHTML=total;
          }
          // alert(ajx.responseText);
          document.getElementById("numbers").value= ajx.responseText.trim();
    }
  }
  ajx.open("GET", "get_feedefaulters.php?b="+b);
  ajx.send(); 

}

function getGroup(str,b)
{


  if(str=="Contacts"){
    //alert("Contacts");
    document.getElementById("classes").style.display="none";
    getContacts(b);
    document.getElementById("category").style.display="block";
  }
  else if(str=="Employees"){
    //alert("Emp");
    document.getElementById("classes").style.display="none";
    document.getElementById("category").style.display="none";
    getEmployees(b);
  }

   else if(str=="OldEmployees"){
    //alert("Emp");
    getOldEmployees(b);
  }
   else if(str=="OldStudents"){
    //alert("Emp");
    getOldStudents(b);
  }

  else if(str=="Fee"){
    //alert("Emp");
    document.getElementById("classes").style.display="none";
    document.getElementById("category").style.display="none";
    getFeeDefaulters(b);
  }

  else {

  document.getElementById("classes").style.display="block";
  var ajx;  // The variable that makes Ajax possible!
  
  if(window.XMLHttpRequest)
  ajx=new XMLHttpRequest();
  ajx.onreadystatechange = function()
  {

    if(ajx.readyState == 4)
    {
          // alert(ajx.responseText);  
          
          if(ajx.responseText!=null){
            var total=(ajx.responseText).split(",");
            total=total.length;
            document.getElementById("recipient").innerHTML=total;
          }

          document.getElementById("numbers").value= ajx.responseText.trim();
    }
  }
  ajx.open("GET", "get_numbers.php?class_id="+str+"&b="+b);
  ajx.send(); 

} // ending else

}

function myFunction() {
    var x = document.getElementById("myDIV");
    if (x.style.display === "none") {
        x.style.display = "block";
    } else {
        x.style.display = "none";
    }
}


function apply(str) {
    
    if (str != null || str != "" ) {
      var x = document.getElementById("text").value=str;
    } else {
      var x = document.getElementById("text").value="";
    }
}



</script>



  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <div class="col-md-3 left_col">
     
            <?php include('RootMe/sidebar.php');?>

              <?php include('RootMe/header.php');?>
     
        <!-- page content -->
          <div class="right_col" role="main">
          <div class="">
                 <h3>Send New Urdu SMS</h3>
              
            <div class="clearfix"></div>

  <div class="row">
              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                 
                  <div class="x_content">
           
    <form class="form-style-7" id="add_student" action="urdu.php" method="post" onSubmit="return msg_check();" enctype="multipart/form-data">


  <input type="hidden" name="action"  value="SendMessagesUrdu">
  <input type="hidden" value="" class="form-control"  id="validate_status" />
  <input type="hidden" name="limit"  value="<?php echo $brid[0]['sms_limit']; ?>" >
 <!--  <input type="text" name="brand_limit"  value="<?php echo $brid[0]['brand_sms_usage']; ?>" > -->


     <div class="col-md-2" > 

   
     </div>

     
       <div class="col-md-8"> 


          

        <div class="row">

                        
        <?php if($sms_credit<=0){?>
          
           <div class="col-md-12" >
              <div class="alert alert-danger alert-dismissible fade in">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                <strong>Sorry !</strong> Your SMS Limit is exceeded, Please contact to administrator !!!
              </div>

          </div>
        <?php } ?>  


          
          <?php  if(isset($_SESSION['sent_msg'])){?>


            <div class="alert alert-success" id="msg">
             <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
              <strong>Success!</strong> Message is sent successfully...
            </div>


          <?php  unset($_SESSION['sent_msg']); }?>


                       
                      <div class="col-md-12 col-xs-12" >
                          <div class="form-group" >


                            
                              <div style="float:left;">  <label > Choose Group to Send SMS  </label> 

                              </div>

                              <div style="float:right;">  
                              <input type="button" onClick="myFunction()" id="button" class="btn btn-primary" style="padding:4px 10px 4px 10px;" value="Add Template" />   

                              </div>

                              <br/> <br/>

                          
                                <select name="class" id="group" class="select_add" style="height:40px;width: 100%;" onChange="getGroup(this.value,<?php echo $_SESSION['branch'];?>)"  >
                                <option value="">Select Group </option>

                                <option value="All"> All Classes  </option>
                                <option value="Employees"> All Employees   </option>
                                <option value="Fee"> Fee Defaulters   </option>
                                <option value="Contacts"> Contacts   </option>
                                <option value="OldStudents"> All Old Students </option>
                                <option value="OldEmployees"> All Old Employees </option>

                                
                            </select>
                          </div>
                      </div>


                  


                       
                      <div class="col-md-12 col-xs-12" >
                          <div class="form-group" >



                          
                                <select name="class" id="classes" class="select_add" style="height:40px;display:none;width: 100%;" onChange="getGroup(this.value,<?php echo $_SESSION['branch'];?>)"  >
                                <option value=""> Select Class  </option>
                                <option value="All"> All Classes  </option>

                                <?php
                                $m->where("branch_id", $_SESSION['branch']);
                                $WorkAssignList = $m->get("edu_class_config");
                                foreach($WorkAssignList as $WorkAssignListVal)
                                { ?>

                                   <option <?php if($student[0]['class_id']==$WorkAssignListVal['class_id']){echo "selected";} ?> value="<?php echo $WorkAssignListVal['class_id']; ?>"><?php echo $WorkAssignListVal['Class_Name']; ?></option>


                              <?php  } ?>

                            </select>
                          </div>
                      </div>

             <!--           <div class="col-md-12 col-xs-12" >
                          <div class="form-group" >
                            
                            <label>  Select Gardian Number : * </label>
                              
                              <select name="gardian" id="gardian" class="select_add" style="height:40px;width: 100%;">
                                <option value="NO"> NO   </option>
                                <option value="YES"> Send SMS To Gardian  </option>
                          </select>
                          </div>
                     </div> -->

            
                      <div class="col-md-12 col-xs-12" >
                          <div class="form-group" >

                         
                                <select name="category" id="category" class="select_add" style="height:55px;display:none;width: 100%;" onChange="getCategory(this.value,<?php echo $_SESSION['branch'];?>)">
                                <option value="category"> Select Categories </option>
                              

                               <?php
                                $m->where("branch_id", $_SESSION['branch']);
                                 $query="select * from eduportal_contacts_category";
                                 $WorkAssignList = $m->query($query);
                                foreach($WorkAssignList as $WorkAssignListVal)
                               { ?>

                                   <option <?php if($contact[0]['cat_id']==$WorkAssignListVal['cat_id']){echo "selected";} ?>  value="<?php echo $WorkAssignListVal['cat_id']; ?>"><?php echo $WorkAssignListVal['cat_name']; ?></option>


                              <?php  } ?>

                            </select>
                          </div>
                      </div>

      
                      <div class="col-md-12 col-xs-12" >
                          <div class="form-group" >
                            
                            <label>  Cell Numbers : * </label>
                              
                              <textarea placeholder="03016138728,03016138662..." class="form-control message1 form-textarea" rows="7" id="numbers" name="numbers" style="line-height: 1.5em; font-family: Arial, Helvetica, sans-serif; font-size: 14px;"><?php echo ltrim($_GET['students'], 'undefined,');?></textarea>
                          </div>
                      </div>


                    <div class="col-md-12 col-xs-12" style="display:none;" id="myDIV" >

                          <div class="form-group" >


                            
                              <div style="float:left;">  <label >  Select Template </label> 

                              </div>

                           
                                <select name="class" id="template" class="select_add" onChange="apply(this.value)"  >
                                <option value="">Choose Template </option>

                                <?php
                                $m->where("branch_id", $_SESSION['branch']);
                                $m->where("status", "0");
                                $WorkAssignList = $m->get("eduportal_templates_details");
                                foreach($WorkAssignList as $WorkAssignListVal)
                                { ?>

                                   <option <?php if($student[0]['template_title']==$WorkAssignListVal['template_title']){echo "selected";} ?> value="<?php echo $WorkAssignListVal['template_detail']; ?>"><?php echo $WorkAssignListVal['template_title']; ?></option>


                              <?php  } ?>

                            </select>
                          </div>
                      </div>


    
                       
                      <div class="col-md-12 col-xs-12" >
                          <div class="form-group" >


                             <label> From  </label> 

                               <select name="from" id="from" class="select_add" style="height:40px;width: 100%;"  >
                                
                                 <?php
                                $m->where("branch_id", $_SESSION['branch']);
                                $m->where("mask_status", "0");
                                $WorkAssignList = $m->get("eduportal_school_masks");
                                foreach($WorkAssignList as $WorkAssignListVal)
                                { ?>

                   <option value="<?php echo $WorkAssignListVal['mask_name']; ?>"><?php echo $WorkAssignListVal['mask_name']; ?></option>


                              <?php  } ?>

                                <option value="EduPortal">EduPortal</option>

                            </select>
                          </div>
                      </div>


                            

                      <div class="col-md-12 col-xs-12" >

                         
                          <div class="form-group" >
                              
                              <label>  Message : * </label>
                    
       
                                  <textarea maxlength="50" class="form-control message1 form-textarea styletext"  rows="7" id="message" name="text_fld"  dir="rtl"><?php echo $_GET['msg'];?></textarea>                           

                            </div>
                   </div>   


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


                          <div class="msg-count">

                            <div class="col-md-4 col-sm-4" >
                              <strong><span>Total:<span id="char">0</span></span></strong>

                            </div>

                           <div class="col-md-4 col-sm-4" >
                              <strong>Contacts:<span id="recipient">0</span></strong>
                            </div>
                            
                            
                            <div class="col-md-4 col-sm-4" style="text-align:right;">
                              <strong class="text-right" ><span>Left:<span id="leavechar">70</span></span></strong>
                            
                            </div>
                            
                            
                            <div class="col-md-4 col-sm-4" style="text-align:right;">
                              <strong class="text-right" ><span class="text-right">MAX:<span id="maxcount">203</span></span></strong>
                            
                            </div>
                            
                            <div class="col-md-4 col-sm-4" style="text-align:right;">
                              <strong class="text-right" ><span class="text-right">Count:<span onChange="msg_count();" id="msgcount">0 
                              </span></span></strong>
                             <input type="hidden" name="msgcount1" id="msgcount1" value="0" />
                            </div>


                              <input type="hidden" name="unicode" id="unicode" value="unicode">
                              <input type="hidden" name="id" id="id" value="">
                         
                          </div>



                   </div>   


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

                        <br> <br>
                        
                        
                        <?php if($sms_credit<=0){?>

                          <div class="alert alert-danger alert-dismissible fade in">
                            <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                            <strong>Sorry !</strong> Your SMS Limit is exceeded, Please contact administrator !!!
                          </div>
                             <input disabled type="submit" value=" Send Message " class="pull-right btn btn-primary" >
                       

                        <?php } else { ?>  

                           <input type="submit" value=" Send Message " class="pull-right btn btn-primary" >    
                       
                        <?php } ?>



                     </div>                    


    
                   </div>  <!-- ending row-->


    </div>   <!--  ending 10 column-->



     <div class="col-md-2" > 

    

     </div>


    </form>        
    

           
          </div>
        </div>

            </div>
          </div>
        </div>
      </div>
      </div>
      </div>  
        <!-- /page content -->
  
<?php include('RootMe/js_linksscript.php');?>
  
<script type="text/javascript">






$(document).ready(function() {

    $(document).on('change', 'input[name="messagetype"]', function() {
        var gettxt = $(this).val();
        if (gettxt == 'text') {
            document.getElementById("text").style.display= 'block';
            document.getElementById("message").style.display= 'none';
            document.getElementById("text").value = '';
            $("#text").removeAttr('dir');
            $('.unicode-show').hide();
            $("#text").attr('maxlength', '612');
            document.getElementById('char').innerHTML = "0";
            document.getElementById('leavechar').innerHTML = "160";
            document.getElementById('maxcount').innerHTML = "612";
            document.getElementById('msgcount').innerHTML = "0";
            
        } else if (gettxt == 'unicode') {

            document.getElementById("text").style.display= 'none';
            document.getElementById("message").style.display= 'block';
            document.getElementById("message").value = '';
            $('.unicode-show').show();
            $("#message").attr('maxlength', '203');
            document.getElementById('char').innerHTML = "0";
            document.getElementById('leavechar').innerHTML = "70";
            document.getElementById('maxcount').innerHTML = "203";
            document.getElementById('msgcount').innerHTML = "0";

        }
    });




    function show(ch, leave, messagecount, total, maxLen) {
        $('#char').html(ch);
        $('#leavechar').html(leave);
        $('#maxcount').html(maxLen);
        $('#msgcount').html(messagecount);
        $("#message").attr('maxlength', maxhtml);
    };




$("[name='text_fld']").keyup(function() {
        var len = this.value.length;
        


      var chars = 1;
            var msgs = 1;
            var maxLen = 203;
            var level1 = 70;
            var level2 = 67;
            var level3 = 67;
            var level4 = 67;
            var level5 = 67;


            maxhtml = 203;
            var sectionToCheck = $('#message').val();
            var allFoundCharacters = sectionToCheck.match(/[{}€~^\\[\]\\|]/g);
           if (allFoundCharacters !== null && allFoundCharacters.length > 0) {               
                 maxhtml = maxLen - allFoundCharacters.length;
                var count = parseInt(len);
                len = count + parseInt(allFoundCharacters.length);
            }



     

            if (len == 0) {
                show(0, level1, 0, 0, maxLen);
            } else if (len > 0 && len <= level1) {
                var tot;
                tot = "";
                show(len, level1 - len, 1, len, maxLen - len);
                document.getElementById("msgcount1").value=1;
            } else if (len > level1 && len <= (level2 + level1)) {
                var tot;
                tot = len - level1;
                show(tot, level2 - tot, 2, len, maxLen - len);
                document.getElementById("msgcount1").value=2;
            } else if (len > (level2 + level1) && len <= (level3 + level1 + level2)) {
                var tot;
                tot = len - (level1 + level2);
                show(tot, level3 - tot, 3, len, maxLen - len);
                document.getElementById("msgcount1").value=3;
            } else if (len > (level3 + level2 + level1) && len <= (level4 + level3 + level1 + level2)) {
                var tot;
                tot = len - (level1 + level2 + level3);
                show(tot, level4 - tot, 4, len, maxLen - len);
                document.getElementById("msgcount1").value=4;
            } else if (len > (level4 + level1 + level2 + level3) < len <= maxLen) {
                var tot;
                tot = len - (level1 + level2 + level3 + level4);
                show(tot, level5 - tot, 5, len, maxLen - len);
                document.getElementById("msgcount1").value=5;
            } else {}
        
        
          
    });

});



</script>







    <script type="text/javascript">
    

$(function(){
  
  $("#add_student").validate({
    rules: {
      text_fld: {
        
        required: true,
        minlength: 2,
        maxlength: 203,
      },

      numbers: {
        required: true,
        minlength: 11,
               
      },





    },
    messages: {
      
      text_fld: {
        required: 'This field is required.',
        number:'Number not allowed'
      },
      
      numbers: {
        required: 'This field is required.',
        minlength:'Minimum length 3',
        maxlength:'Maximum length 6',       
      },
      
      


    },
      
      
    
  });
  });



</script>

  </body>
</html>
