<?php
session_start();
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj = new my_class();

extract($_REQUEST) ;
$eng_sms_max_part = 0;
$ban_sms_max_part = 0;
foreach($obj->View_colmn_By_Cond("_sms_part_allocation_setup","_sms_type,_sms_part_no","_status='A'") as $sms_part_setup_info){
	if($sms_part_setup_info['_sms_type']=='E'){
		$eng_sms_max_part = $sms_part_setup_info['_sms_part_no'];
	}
	else{
		$ban_sms_max_part = $sms_part_setup_info['_sms_part_no'];
	}
}
?>

<script src="module/general_notice/js/general_notice_phonebook.js" type="text/javascript"></script>
<script src="asset/theme/js/fuelux/fuelux.tree.min.js"></script>
<!--sms counter js-->

<script src="module/general_notice/js/sms_counter_js/sms_counter.min.js"></script>

<style>
    select{
        width:90%;
    }
    input[type="checkbox"], input[type="radio"] {height: 31px;}	
    .hr-18, .hr18 {
        margin: 6px 0px;
    }
    h3 {
        font-size: 16px;
    }
    .mr10{margin-top:10px;}
    input[type="checkbox"] + .lbl::before, input[type="radio"] + .lbl::before {

        height: 16px !important;
    }
    label{
        font-size:14px !important ;
    }
	.tree .tree-folder {
		height:auto;
		overflow:hidden;
		}
	#tree1 .tree-folder .tree-folder-content {
			display:none;
		}	
	#tree1 .tree-folder:first-child .tree-folder-content {
			display:block;
		}
	#tree1 .tree-folder:first-child .tree-folder .tree-folder-content{
			display:none;
		}	
	#tree1 .tree-folder:first-child .tree-folder:first-child .tree-folder-content{
			display:block;
		}			
</style>          

 
<div class="row-fluid" style="background-color:#f5f5f5;-webkit-box-shadow: 0 8px 6px -6px black; -moz-box-shadow: 0 8px 6px -6px black; box-shadow: 0 8px 6px -6px black;">
	<form class="form-vertical group-form" id="phnbk">
        <input type="hidden" id="branchid" name="branchid" value="<?php echo $_SESSION['LOGIN_BRANCH'];  ?>"/>
        <input type="hidden" id="module_id" name="module_id" value="<?php echo $mod_id;  ?>"/>
        <input type="hidden" id="eng_part_info_holder" name="eng_part_info_holder" value="<?php echo $eng_sms_max_part;  ?>"/>
        <input type="hidden" id="ban_part_info_holder" name="ban_part_info_holder" value="<?php echo $ban_sms_max_part;  ?>"/>
		<legend>
			<div class="widget-header header-color-green2">
				<h4 class="lighter smaller" style="font-weight:bold;">General Notice (Phone book)</h4>
			</div>
		</legend>
		
        <div class="row-fluid">
			<div class="span6"><!--onkeypress="msg_length_calculator()"-->
				<div class="control-group" >
					<label for="pb_sms_body">Message:  <font color="#FF0000">*</font></label>
					<textarea style="width:100%;" class="message_default" id="pb_sms_body" rows="6" placeholder="Message" onkeypress="msg_length_calculator()" oncontextmenu="return false;"></textarea>
					<div class="controls topics-bottom" style="width:100%;">  
						<div class="topics-count label label-large label-light" style="width:100%;">
							<ul id="sms-counterr">
								<li>Length: <span id= "length" class="length"></span></li>
								<li>Messages: <span id="messages" class="messages"></span></li>
								<li>Per Message: <span id ="per_message" class="per_message"></span></li>
								<li><span id="rem_lbl">Remaining:</span> <span id="remaining" class="remaining"></span></li>
							</ul>
						</div>
					</div>
				</div>
			</div>
			<div class="span6" id="rule_holder"></div>
        </div>
		
        <div class="row-fluid" style="margin-top: 16px;">
			<div class="row-fluid">
				<div class="span12 students" > 
					<h3 style="line-height:25px;"><span class="lbl">Groups in the Phone book</span> </h3>
					<div class="hr hr-double hr-dotted hr18"></div>  
					<div class="all_pb">
						<?php
							$table_name = "_pb_group_info";
							$column = "_id,_name";
							$where_cond = "_status='A'";
							$pb_grp_info = $obj->View_all_data_row_column_By_Cond($table_name, $column, $where_cond);
						?>
						<div class="span12">
							<div class="widget-box">
								<div class="widget-header widget-header-flat">
									<h4 class="smaller">
										<input class="check_all_pb" title="pb_grp" id="all_pb" type="checkbox"><span class="lbl"></span><?php echo "All Groups"; ?> </span> 
									</h4>
								</div>
								<div class="widget-body" style="padding:8px;">
							<?php  	foreach ($pb_grp_info as $child) { 
										echo '<input class="check_all_pb_grp" title="contact_info" id="'.$child['_id'].'" type="checkbox"/><span class="lbl"></span> '.$child['_name'].' </span> <br>';
									}
							?>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div> 
		</div>
    </form>	
</div>


<!-- end form -->

<div id="dummy_id" style="display:none"></div>

<?php if($_SESSION['add']=='Y'){ ?> 
<div class="row-fluid">
    <div class="span2">
		<div class="control-group">
			<label class="control-label" for="form-field-1">&nbsp;</label>
			<div class="controls">
				<button class="btn btn-success" type="button" id="btn_send_pb_sms">
					<i class="icon-ok bigger-100"></i>
					Send
				</button>
			</div>
		</div>
	</div>
</div>
<?php } ?>

<div class="processing_msg"> </div>
<script>
	$("#rule_holder").load("module/general_notice/get_sms_rules.php");
	$('#pb_sms_body').countSms('#sms-counterr');
	var eng_sms_part = $("#eng_part_info_holder" ).val();
	var ban_sms_part = $("#ban_part_info_holder" ).val();
	
	var ctrlDown = false,
        ctrlKey = 17,
        cmdKey = 91,
        vKey = 86,
        cKey = 67;

    $(document).keydown(function(e) {
        if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = true;
    }).keyup(function(e) {
        if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = false;
    });
    
    // Document Ctrl + C/V 
    $(document).keydown(function(e) {
      //  if (ctrlDown && (e.keyCode == cKey)) alert("Document catch Ctrl+C");
        if (ctrlDown && (e.keyCode == vKey)){
			msg_length_calculator();
		};
    });
	
	var per_msg;
	var msg_count;
	var alert_msg = "  eng: "+eng_sms_part+"  ban: "+ban_sms_part;
	var max = 0;
	var prev_msg = 0;
	
	function msg_length_calculator(){
		var msg_content = $("#pb_sms_body").val();
		var msg_length = msg_content.length;
		per_msg = document.getElementById("per_message").innerHTML;
		if(max==0 || prev_msg!=per_msg){
			//alert('inside max setter');
			if(per_msg==160){
				if(eng_sms_part > 1){
					max = (160*eng_sms_part) - (7*eng_sms_part);
				}
				else{
					max = 160;
				}
			}
		
			if(per_msg<=70){
				if(ban_sms_part > 1){
					max = (70*ban_sms_part) - (3*ban_sms_part);
				}
				else{
					max = 70;
				}
			}
			$('#pb_sms_body').attr('maxlength', max);
		}
		if (msg_length == max) {
			document.getElementById("rem_lbl").style.color = "red";
			document.getElementById("remaining").style.color = "red";
        } else if (msg_length >= max) {
           msg_content = msg_content.substr(0, max);
			$("#pb_sms_body").val(msg_content);
        }
		else{
			document.getElementById("rem_lbl").style.color = "grey";
			document.getElementById("remaining").style.color = "grey";
		}
		prev_msg = per_msg;
	}	
</script>