<?php session_start();
    if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes') {
        include ('../../../scripts/classes.php');
        if ($_SESSION['lang']=="EN"){$lang="EN"; include ('../../../lang/en.php');}elseif ($_SESSION['lang']=="GR"){$lang="GR";include ('../../../lang/gr.php');}else {$lang="EN";include ('../../../en.php');}
        
        $get_sms_provider = Core::GetSmsProvider();
        if ($get_sms_provider != 'SMSAPI') die('This page is accessible only when SMSAPI is setup as the SMS module provider');
        $userid = Core::GetUserId();
  		$sms_balance = SmsApi::GetSmsApiBalance();
        $sms_settings = SmsApi::GetSmsApiSettings();
          
        $page_title = $lbl_bulk_sms;
    	$mode = _CREATE_AUTOMATED_SMS; 
		
        $sms_mode = Core::GetSmsMode();
        $credit_balance = $sms_balance['credit_balance'];
        
        $max_chars = SmsApi::GetSmsApiMaxChars();
        ($sms_settings['max_chars'] != $max_chars) ? $lbl_multiple_sms_setup = $lbl_multiple_sms_setup_yes : $lbl_multiple_sms_setup = $lbl_multiple_sms_setup_no;
        
?>	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title ?></title>
<link rel="shortcut icon" href="../../../img/favicon.png" />
<link href="../../../cms1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../../scripts/jquery.js"></script>
<script type="text/javascript" src="../../../scripts/charCount.js"></script>
<script type="text/javascript" src="../../../scripts/gen_validatorv4.js"></script>
<script type="text/javascript">
//this is about sending the test message
$(document).ready(function(){
  $("#testButton").click(function(){
        show_div_msg('ftp_1_errorloc', '');
        show_div_msg('info_div', '');
        var testPhone = $("#testPhone").val();
        var sms_text = $("#sms_text").val();
        var sms_length = $("#sms_text").val().length;
        var sender = $("#sender").val();
        var gsm_check = gsm_validator();
            if(sms_text == ''){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>$lbl_error_sms_text</li></ul>"; ?>');
            }else if (gsm_check == false){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>"._SMS_TEXT_HAS_INVALID_CHARS."</li></ul>"; ?>');
            }else if(sms_length > '<?php echo $max_chars; ?>'){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>$lbl_error_sms_length</li></ul>"; ?>');
             }else if(sender == ''){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>$lbl_error_sender</li></ul>"; ?>');  
            }else if(testPhone == ''){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>$lbl_error_phone_req</li></ul>"; ?>');
            }else if(isNaN(testPhone)|| testPhone.indexOf(" ")!=-1 || testPhone.length > 10 || testPhone.length < 10 || testPhone.charAt(0)!="6" || testPhone.charAt(1)!="9"){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>"._PHONE_NUMBER_IS_INVALID."</li></ul>"; ?>');
            }else{
                skm_LockScreen('<div class="LockOnPane"><?php echo _PROCESSING_YOUR_REQUEST; ?></div>');
                $.ajax({
                type: "POST",
                url: "smsapi_send.php",
                data: {'phone' : testPhone, 'sender' : sender,  'sms_text' : sms_text},
            success: function(data){
                skm_UnLockScreen('');
                show_div_msg('info_div', data);
                       $("#balance-container").load("smsapi_prepare.php #balance-msg");
                }
            });
        }
        return false;
  });
  
  /*Bulk messages*/
  $("#saveButton").click(function(){
        show_div_msg('ftp_1_errorloc', '');
        show_div_msg('info_div', '');
        
        var campaign = $("#campaign").val();
        var sms_text = $("#sms_text").val();
        var sender = $("#sender").val();
        var delivery_name = $("#delivery_name").val();
        var exec_time = $("#exec_time").val();
        var is_active = $("#is_active").val();
        var sms_length = $("#sms_text").val().length;
        var gsm_check = gsm_validator();
        
            if(delivery_name == ''){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>"._ERROR_DELIVERY_NAME_MANDATORY."</li></ul>"; ?>');
            }else if(sms_text == ''){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>$lbl_error_sms_text</li></ul>"; ?>');
            }else if (gsm_check == false){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>"._SMS_TEXT_HAS_INVALID_CHARS."</li></ul>"; ?>');
            }else if(sms_length > '<?php echo $max_chars; ?>'){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>$lbl_error_sms_length</li></ul>"; ?>');
             }else if(sender == ''){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>$lbl_error_sender</li></ul>"; ?>');  
            }else if(campaign == ''){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>$lbl_error_campaign</li></ul>"; ?>');
            }else if(exec_time == ''){
                show_div_msg('ftp_1_errorloc', '<?php echo "<ul><li>"._ERROR_EXECUTION_TIME_MANDATORY."</li></ul>"; ?>');                
            }else{
                skm_LockScreen('<div class="LockOnPane"><?php echo _PROCESSING_YOUR_REQUEST; ?></div>');
                $.ajax({
                type: "POST",
                url: "smsapi_save_automated.php",
                data: {'delivery_name' : delivery_name, 'campaign' : campaign, 'sender' : sender,  'sms_text' : sms_text, 'exec_time' : exec_time, 'is_active' : is_active  },
            success: function(data){
                skm_UnLockScreen('');
                show_div_msg('info_div', data);
                document.location = 'smsapi_auto_setup.php';
                }
            });
        }
        return false; 
  });  
});
</script>
<script type="text/javascript">
// get balance with ajax
$(document).ready(function(){
    $("#refresh_balance").click(function(){
       $("#balance-container").load("smsapi_prepare_auto.php #balance-msg");
       return false;
       });
});        
</script>
<script type="text/javascript">
function validate(evt) {
    document.ftp_1.sms_text.value = document.ftp_1.sms_text.value.toUpperCase();
}
</script>
<script type="text/javascript">
//GSM CHARACTERS VALIDATOR
function gsm_validator() {
    var regex = /^[@£$¥èéùìòÇØøÅå_ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩÆæßÉ!“#¤%&‘()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ§¿abcdfeghijklmnopqrstuvwxyzäöñüà\s\n\r\€\f[\]^{|}~\b]+$/g;
    var objValue = document.ftp_1.sms_text.value;
    if (!regex.test(objValue)){
        sfm_show_error_msg('<?php echo _SMS_TEXT_HAS_INVALID_CHARS ?>', document.ftp_1.sms_text);
        return false;
    }else {
        return true;
    }
}
</script>
<script type="text/javascript">
//Control the lock screen feature
  function skm_LockScreen(str)
  {
     var lock = document.getElementById('skm_LockPane');
     if (lock)
        lock.className = 'LockOn';
     lock.innerHTML = str;
  }

  function skm_UnLockScreen(str)
  {
     var lock = document.getElementById('skm_LockPane');
     if (lock)
        lock.className = 'LockOff';
     lock.innerHTML = str;
  }
</script>
</head>
<body>
<script>top.document.title=self.document.title;</script>
<script type="text/javascript">
	$(document).ready(function(){
	$("#sms_text").charCount({
	allowed: <?php echo $max_chars; ?>,		
	warning: 30,
	counterText: '<?php echo $lbl_characters_left; ?> '	
	});
});
</script>
	<div>
		<fieldset>
		<legend><?php echo $mode ?></legend>
                <div id="balance-container">
                <?php if ((int)$credit_balance < 2 && (int)$credit_balance > 1){ $div_class = "warning"; } elseif ($credit_balance <= 1){ $div_class = "critical"; } else { $div_class = "success"; } ?>
                    <div id="balance-msg" class="<?php echo $div_class ?>">
                        <?php 
                            echo '<div id="balance-msg">';
                            echo '<ul><li>'.$lbl_credit_balance.': <strong>'.$credit_balance.'</strong></li></ul>';
                            echo '</div>';
                        ?>
                    </div>    
                </div>
                <div class="padding-5"><img src="../../../img/get.gif" /><a href ='#' id='refresh_balance'><?php echo "&raquo; "._UPDATE_BALANCE ?></a></div>
                <?php
                    if ($credit_balance == 0) echo '<div class="critical"><ul><li>'._ZERO_CREDITS_TEXT.'</li></ul></div>';
                    if ($sms_mode == 'TEST') echo '<div class="warning"><ul><li>'._SMS_TEST_MODE.'</li></ul></div>';
                ?>
                <div class="critical" id="ftp_1_errorloc"></div>
                <div id="info_div"></div>
				<div> 
                    <form id="ftp_1" name="ftp_1">
                    <label class="labels"><?php echo _DELIVERY_NAME ?>*</label><input type="text" value=""  maxlength="255" name="delivery_name" class="addform" id="delivery_name"/>
				   <br />
				    
                    <div class="msg_div">
				    <label class="labels"><?php echo $lbl_message_text ?>*</label><a class="tooltip" href="#">
                    <img src="../../../img/info.gif" /><span class="classic"><?php echo $lbl_multiple_sms_setup." ".$max_chars ?></span>
                    </a><textarea rows="2" id="sms_text" cols="20"  name="sms_text" class="textarea_msg" onkeyup="validate(event);"></textarea><br /></div>
				    
                    <label class="labels"><?php echo $lbl_sender ?></label>
				    <select name="sender" id="sender" class="addform_drop">
				        <option value="<?php echo $sms_settings['sender_id'] ?>"><?php echo $sms_settings['sender_id'] ?></option>
				        <option value="<?php echo $sms_settings['sender_mobile'] ?>"><?php echo $sms_settings['sender_mobile'] ?></option>
				    </select>
                    
                    <br />
                    
				    <label class="labels"><?php echo $lbl_campaign ?></label>
				    <select name="campaign" id="campaign" class="addform_drop">
				        <option value=""><?php echo $lbl_choose_campaign ?></option>
                		<?php 
        				$sql_camps = "SELECT * FROM sms_campaign_header WHERE active='1' ORDER BY created_on DESC";
        				$result_camps  = mysqli_query($mysqli, $sql_camps) or die (mysqli_error($mysqli));
        				    while($row = mysqli_fetch_assoc($result_camps)){ //1
        				        echo '<option value="'.$row['id'].'">'.$row['description'].'</option>'; 
                            } //1
                        mysqli_close($mysqli);
        				?>
        				</select>
				    <br />
                    
				    <label class="labels"><?php echo _TIME_TO_BE_EXECUTED ?></label>
                    <select name="exec_time" id="exec_time" class="addform_drop">
                        <option value="08:00">08:00</option>
                        <option value="09:00">09:00</option>
                        <option value="10:00">10:00</option>
                        <option value="11:00">11:00</option>
                        <option value="12:00">12:00</option>
                        <option value="13:00">13:00</option>
                        <option value="14:00">14:00</option>
                        <option value="15:00">15:00</option>
                        <option value="16:00">16:00</option>
                        <option value="17:00">17:00</option>
                        <option value="18:00">18:00</option>
                        <option value="19:00">19:00</option>
                        <option value="20:00">20:00</option>
                        <option value="21:00">21:00</option>
                        <option value="22:00">22:00</option>
                        <option value="23:00">23:00</option>
                        <option value="00:00">00:00</option>
                        <option value="01:00">01:00</option>
                    </select>
                    <br />
                    <label class="labels"><?php echo _ACTIVE_FEMALE ?></label>
                    <select name="is_active" id="is_active" class="addform_drop">
                        <option value="0"><?php echo _NO ?></option>
                        <option value="1"><?php echo _YES ?></option>
                    </select>
                    <br />
                    <br />
    				<?php if ($credit_balance!=0){ ?>    
                        <input type="button" onclick="toggle_visibility('send_single');" class="button" value="<?php echo _SEND_TEST_SMS; ?>" />
                        <div id="send_single" style="display: none;margin-top:10px">
                        <form id="testSMS" name="testSMS">
                               <label class="labels"><?php echo $lbl_grid_phone ?></label>
                               &nbsp;<?php echo $phones_country_pre ?><input type="text" id="testPhone" name="testPhone" maxlength="10" class="addform"/><br /><br />
                               <input type="button" id="testButton"  class="button" value="<?php echo _SEND_NOUN; ?>" />
                        </form>
                        </div>
                        <br />
                    <?php
                    }
                    ?>	
                    </form>
                        <!-- Save it -->	
                        <form id="CancelForm" name="testSMS" action="smsapi_auto_setup.php">
                            <input type="submit"  class="button" value="<?php echo _CANCEL ?>" />
                            <input type="button"  id="saveButton" name="saveButton" class="button" value="<?php echo _SAVE ?>"  />
                        </form>  

                                                                                      					
			  
			<script type="text/javascript">
			 	 var frmvalidator  = new Validator("ftp_1");
			 		frmvalidator.EnableOnPageErrorDisplaySingleBox();
					frmvalidator.EnableMsgsTogether();
					frmvalidator.addValidation("delivery_name","req","<?php echo _ERROR_DELIVERY_NAME_MANDATORY ?>");
					frmvalidator.addValidation("sms_text","req","<?php echo $lbl_error_sms_text ?>");
					frmvalidator.addValidation("sms_text","maxlen=<?php echo $max_chars ?>", "<?php echo $lbl_error_sms_length ?>");
                    frmvalidator.setAddnlValidationFunction(gsm_validator);						
					frmvalidator.addValidation("sender","req","<?php echo $lbl_error_sender ?>");
					frmvalidator.addValidation("campaign","req","<?php echo $lbl_error_campaign ?>");
                    frmvalidator.addValidation("exec_time","req","<?php echo _ERROR_EXECUTION_TIME_MANDATORY ?>");
				 	
			</script>
            <script type="text/javascript">
            //this is about the send message div
                function toggle_visibility(id) {
                var e = document.getElementById(id);
                if(e.style.display == 'none'){e.style.display = 'block';
                }else{e.style.display = 'none';
    	       }
            }
            </script>
			</div>
		</fieldset>
	</div>
    <div id="skm_LockPane" class="LockOff"></div> 
</body>
</html>
<?php
} else {
    header('Location: login.php');
}
?>
