<?php
/**
 * HTML Change Password Form
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    17-08-2020
 * @edited_by
 * @last_edit_date
 * @todo           Nothing
 */
ob_start();
session_start();
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
include("validateLogin.php");
include("includeInformation.php");
include("header.php");
$queryAccount = "SELECT * ";
$queryAccount.= " FROM $userLogin";
$queryAccount.= " WHERE id=$_SESSION[userId]";
$resultAccount = mysqli_query($conn, $queryAccount);
$rowAccount = mysqli_fetch_array($resultAccount);
$msgUniqueMd5 = md5(time());
$_SESSION["msgUnique"] = $msgUniqueMd5;

?>
<link rel="stylesheet" type="text/css" href="JScalendar/jquery-ui-timepicker-addon.css" />
<link rel="stylesheet" media="all" href="JScalendar/jquery-ui.css" />
<?php if($_SESSION["language"]==1){
    ?>
    <script type="text/javascript" src="JScalendar/jquery-ui.min.Ar.js"></script>
    <script type="text/javascript" src="JScalendar/jquery-ui-timepicker-addon-Ar.js"></script>
    <?php
}else{
    ?>
    <script type="text/javascript" src="JScalendar/jquery-ui.min.js"></script>
    <script type="text/javascript" src="JScalendar/jquery-ui-timepicker-addon.js"></script>
    <?php
}
?>
<script type="text/javascript">
var agt=navigator.userAgent.toLowerCase();
function sz(t) {
a = t.value.split('\n');

b=1;
for (x=0;x < a.length; x++) {
 if (a[x].length >= t.cols) b+= Math.floor(a[x].length/t.cols);
 }
b+= a.length;
//if (b > t.rows && agt.indexOf('opera') == -1)
if(b<5)
 t.rows = b;
}
</script>
<script language="javascript" type="text/javascript">
function checkIntergetType(val)
{
	var string = "1234567890";	
	for(var i=0;i<val.length;i++)
	{
		if(string.indexOf(val.charAt(i)) == -1)
		return false;
	}
	return true;
}

function validate()
{   
	if(document.formMsg.userSender.value == "")
  	{
		//أدخل إسم المرسل
  		alert('<?php echo $sendMsgLBL["insertSenderName"];?>');
  		document.formMsg.userSender.focus();
  		return false;
  	} 	
	var sender = document.formMsg.userSender.value;
    if(checkIntergetType(sender))
    {
	    if(sender.substring(0,2)=="00")
		sender=sender.substring(2);
		if(sender.substring(0,1)=="0")
	    sender=sender.substring(1);
		if (sender.length > 12)
		{
			alert("<?php echo $sendMsgLBL["senderNameLength"]?>");
			return false;
		}
	}
	else
	{
		if(unicode(document.formMsg.userSender.value)==1)
		{
			//اسم المرسل بالغة الانجليزية
			alert('<?php echo $sendMsgLBL["senderNameEnglish"]?>')
			return false;
		}
		if (sender.length > 11)
		{
			alert("<?php echo $sendMsgLBL["senderNameLength"]?>");
			return false;
		}
	}
	if(document.formMsg.msg.value == "")
	{
		//يجب عليك إدخال نص الرسالة
		alert('<?php echo $sendMsgLBL["insetTextMsg"];?>')
		document.formMsg.msg.focus();
		return false;
	}
	msgTemp = "";
	for(i=0; i<document.formMsg.msg.value.length; i++)
	{
		if(document.formMsg.msg.value.charCodeAt(i)=="1632")
			msgTemp += "0";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1633")
			msgTemp += "1";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1634")
			msgTemp += "2";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1635")
			msgTemp += "3";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1636")
			msgTemp += "4";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1637")
			msgTemp += "5";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1638")
			msgTemp += "6";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1639")
			msgTemp += "7";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1640")
			msgTemp += "8";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1641")
			msgTemp += "9";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1785")
			msgTemp += "9";
		else if(document.formMsg.msg.value.charCodeAt(i)=="1642")
			msgTemp += "%";			
		else
			msgTemp +=document.formMsg.msg.value.charAt(i);
	}
	document.formMsg.msg.value =msgTemp;
	for(i=0; i<document.formMsg.msg.value.length; i++)
	{
		if(document.formMsg.msg.value.charAt(i) =='ـ')
		{
			if(document.formMsg.confirmMsg.value == '0')
			{
				//الحرف التالي (شفت و ت) قد لا يصل بشكل صحيح على جميع أنواع الجوالات
				alert('<?php echo $sendMsgLBL["lettersNotSend"];?>');
				document.formMsg.confirmMsg.value = '1';
				document.formMsg.msg.focus();
				return false;
			}
		}
	}
	/*
	if(textFilter(document.formMsg.msg.value)==1)
	{
		document.formMsg.msg.focus();
		return false;
	}
	*/
	return true;
}
function unicode(strText)
{
	str = "دجحخهعغفقثصضطكمنتالبيسشظزوةىلارؤءئإلإألأآلآ،؛؟ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيـًٌٍَُِّْ";
 	for (i=0; i<strText.length; i++)
		if (str.indexOf(strText.charAt(i)) != -1)
			return 1;
 	return 0;
}

function charPlus()
{
	strLen = 0;
	strText = document.formMsg.msg.value;
	for(i=0; i<strText.length; i++)
	{
		if(strText.charCodeAt(i)!=13)
			strLen++;
	}
	document.formMsg.numChars.value = strLen;
	numChar = strLen;	
	if (unicode(document.formMsg.msg.value))
	{
		document.formMsg.unicode1.value = 1;
		if(numChar <= 70)
			document.formMsg.numMsgs.value = 1;
		else
			document.formMsg.numMsgs.value = Math.ceil(numChar/67);
	}
	else
	{
		document.formMsg.unicode1.value = 0;
		if(numChar <= 160)
			document.formMsg.numMsgs.value = 1;
		else
			document.formMsg.numMsgs.value = Math.ceil(numChar/153);
	}
}
function addVar(){
	document.getElementById("msg").value+="<<Name>>";
	document.getElementById("varNum").value++;
	formMsg.msg.focus();
}

$(document).ready(function(){
	$('#calendar').datetimepicker();
	
	$("#process").click(function(){
		if (!validate())
		{
			return false;
		}
		var userSender = $('#userSender').val();
		var msg = $('#msg').val();
		if (userSender !="" && msg !="")
		{
			$("#reportResult").html('');							 
			senderName = $('#userSender').val();
			var values = new Array();
			if($("input:checkbox[id='categoryId[]']").length) {	
				$.each($("input:checkbox[id='categoryId[]']:checked"), function(){
					values.push($(this).val());
				});	
			}
			var catSelected = values;
			var numbersCat = "";
			if($("#numbersCatId").length)
				numbersCat = $('#numbersCatId').val();
			var numbersRank = "";	
			if($("#numbersRankId").length)
				var numbersRank = $('#numbersRankId').val();
			var calendarlater = $('#calendar').val();
			var delNum = 'off';
			if($("#deleteNum").is(':checked'))
				delNum='on';

			$.post("reportSendFromMyList.php", { 
				msg: $("#msg").val(),
				numbers: $("#numbers").val(),
				userSender: senderName,							
				category: ""+catSelected,
				numberscat: numbersCat,
				numbersrank: numbersRank,
				calendarlater: calendarlater,
				deleteNum: delNum,
			},
			function(data){
				$("#reportResult").html(data);	
				$('#reportSendModal').modal('show');
			});
		}
		else
		{
			//يجب ادخال اسم المرسل و نص الرسالة المرسلة
			alert("<?php echo $sendMsgLBL["enterSenderAndMsg"];?>");
			return false;
		} 
	});
	
});
function viewOrHideValData(varObjID2)
{
	if(varObjID2 == 1)
	{
		$('#timeSelectTR').hide();
		$('#repeatSendTR').hide();
	}
	else
	{
		$('#timeSelectTR').show();
		$('#repeatSendTR').show();
	}
}

function viewOrHideRepeatTimes(status)
{
	if(status == 1)
		$('#repeatTimesTR').hide();
	else
		$('#repeatTimesTR').show();
}
function clock()
{
	$.post("timeDateServer.php",function(res){																																																											
		$("#TimeOnTheServer").text(res);
	})
	setTimeout('clock()',1000); 
}
clock();

function fetchDateCategory(id)
{
	$.post("categoryNumberFetch.php",{id:id,rand:Math.random() } ,function(res){
		$("#reportResultNumber").html(res);
	});
	$('#catgoriesNumbersModal').modal('show');

}
function checkSubCats(mainCatId)
{
	if($('#mainCategory'+mainCatId).is(':checked'))
	{
		$("#subCatgoriesTR"+mainCatId+" [id^=subCategory"+mainCatId+"-]").prop('checked', 'checked');
	}
	else
	{
		$("#subCatgoriesTR"+mainCatId+" [id^=subCategory"+mainCatId+"-]").prop('checked', '');
	}
} 

</script>
<!--  catgoriesNumbersModal -->
<form  name="formSearch" action="chargeDone.php" method="post" style="margin:0px">
<div class="modal fade modal-fullscreen " style="z-index:10000;padding-<?php echo $align;?>:0px" id="catgoriesNumbersModal" tabindex="-1" role="dialog" aria-labelledby="searchModalTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
     <div class="modal-content text-right">
        <div class="modal-header modal-header-primary">
           <h5 class="modal-title" id="searchModalTitle"><?php echo $sendMsgLBL["catsNumbers"];?></h5>
           <button type="button" class="close slidedown" data-dismiss="modal" aria-label="Close">
           <span aria-hidden="true">&times;</span>
           </button>
        </div>
        <div class="modal-body" style="text-align: <?php echo $align;?>;">
            <span id="reportResultNumber"></span>
        </div>
        <div class="modal-footer">
           <button type="button" class="btn bg-danger btn-flat text-white m-1" data-dismiss="modal"><?php echo $button["close"];?></button>
        </div>
     </div>
  </div>
</div>

</form>
<!--  catgoriesNumbersModal -->

<!-- Start Dashboard page -->
    <!-- start main-content -->
    <div class="main-content">
        <!-- Start Page-content -->
        <div class="page-content">
        <!-- Page-Title -->
        <div class="page-title-box">
            <div class="container-fluid">
                <div class="row align-items-center">
                    <?php echo breadcrumbs(basename($_SERVER['PHP_SELF']));?>
                </div>
            </div>
        </div>
        <?php
		
		$color = "seagreen";
		//@list($elem1, $elme2) = explode("/", $result);
		$balance = sprintf($sendMsgLBL["balanceHeader3"], $rowAccount['account'], $rowAccount['accountOriginal']);
		if($rowAccount[0] <= 0)
			$color = "red";
		?>
        <!-- end page title end breadcrumb -->
        <div class="page-content-wrapper">
            <div class="container-fluid">
                <div class="row justify-content-center align-items-center">
                    <div class="col-sm-12 col-lg-6 ">
                        <div class="card">
                            <div class="card-body table-responsive">
                            <?php
								if(!empty($_GET["res"]))
									echo result($_GET["res"]);
								?>
                                <?php
								unset($_SESSION["catId"]);
								unset($_SESSION["sendTimeMsg"]);
								?>
                                <span class="text-center balanceTitle" style="color:<?php echo $color;?>"><center class="mb-3"><b><?php echo $balance;?></b></center></span>
								<form name=formMsg onsubmit="return validate()" action='sendMsgReportsList.php' method=post>
								<table border="0" width="100%" cellpadding="3" cellspacing="2" style="margin:0px; border-collapse:collapse">
                                   <tbody>
                                      <!-- START (Sender Managment) --->            
                                      <tr>
                                         <td class="myModalLabel"><?php echo $sendMsgLBL["sender"];?>: </td>
                                         <td>
                                            <?php
											$readOnlyAttr = "";
											if(empty($rowAccount['allowChangeSender']))
												$readOnlyAttr = "readOnly";
											$senderInputValue = "";
											if(!empty($rowAccount['sender']))
												$senderInputValue = $rowAccount['sender'];
											else
												$senderInputValue = $row["userSender"];
											?>
                                            <input name="userSender" id="userSender" maxlength="12" <?php echo $readOnlyAttr;?> class="form-control" value='<?php echo $senderInputValue;?>' title="<?php echo $sendMsgLBL["sender"];?>">
                                      </tr>
                                      <!-- END (Sender Managment) --->
                                      <!-- START (Numbers Managment) --->
										<?php
                                            $queryCatsAndNums = "SELECT COUNT(`user_number`.`id`) AS counter, `user_number`.`categoryId`, `user_category`.`name` ";
                                        	$queryCatsAndNums.= "FROM `user_number`, `user_category` ";
                                        	$queryCatsAndNums.= "WHERE `user_number`.`userId`=$_SESSION[userId] AND `user_category`.`id` = `user_number`.`categoryId` ";
                                        	$queryCatsAndNums.= "GROUP BY `user_number`.`categoryId` ";
                                        	$queryCatsAndNums.= "ORDER BY BINARY `user_category`.`name` ASC";
                                        	$resultCatsAndNums = mysqli_query($conn, $queryCatsAndNums);
                                        	$numCatsAndNums = mysqli_num_rows($resultCatsAndNums);
                                            if(!empty($numCatsAndNums))
                                            {
                                                ?>
                                <!-- START (Categories Managment) --->
                                            <tr>
                                                <td	class="myModalLabel" valign="top"><?php echo $sendMsgLBL["catgories"];?>: </td>
                                                <td> 
                                                    <table border="0" cellpadding="5" cellspacing="0" style="margin:0px; border-collapse:collapse"  dir="<?php echo $dir;?>">
                                                    <?php
                                                    $catCounter = 0;
                                                    while($rowCatsAndNums = mysqli_fetch_assoc($resultCatsAndNums))
                                                    {
                                                        if($catCounter == 0)
                                                            echo "<tr>";
                                                        ?>
                                                        <td style="white-space:nowrap; min-width:150px; text-align:<?php echo $align;?>"><input type="checkbox" id="categoryId[]" name="categoryId[]" value="<?php echo $rowCatsAndNums["categoryId"];?>"> <?php echo $rowCatsAndNums["name"];?> <b dir="<?php echo $dir;?>">[<?php echo $rowCatsAndNums["counter"]?>]</b></td>
                                                        <?php
                                                        ++$catCounter;
                                                        if(fmod($catCounter, 3) == 0)
                                                        {
                                                            echo "</tr>";
                                                            $catCounter = 0;
                                                        }						
                                                    }
                                                   if($catCounter != 0)
                                                    {
                                                        while($catCounter != 3)
                                                        {
                                                            echo "<td>&nbsp;</td>";
                                                            ++$catCounter;
                                                        }
                                                        echo "</tr>";
                                                    }?>
                                                    </table>
                                               </td>
                                            </tr>
                                <!-- END (Categories Managment) --->
                                               <?php
                                           }?>            
                                <tr>
                                    <td class=myModalLabel  valign=top><?php echo $sendFromMyListLBL["namesAndNumbers"] ?></td>
                                    <td class=label  colspan=3><textarea name='numbers' id='numbers' rows="2" class="form-control"></TEXTAREA></td>
                                </tr>
                                <tr>
                                    <td class="myModalLabel" valign="top"></td>
                                    <td class="myModalLabel" colspan="2"><?php echo $sendFromMyListLBL["namesAndNumbersExample"]?></td>
                                </tr>
                                 <tr>
                                      <!-- END (Free Numbers Managment) --->
                                      <!-- END (Numbers Managment) --->
                                      <!-- START (Message Managment) --->    
                                      <tr>
                                        <td class=myModalLabel  valign=top><?php echo $sendFromMyListLBL["vars"]?></td>
                                        <td class=myModalLabel  colspan=3><?php echo $sendFromMyListLBL["name"]?> : <input type="button" class="btn btn-success waves-effect waves-light"  value="<?php echo $sendFromMyListLBL["addVar"]?>"    onclick="addVar()" /></td>
                                    </tr>
                                      <tr>
                                         <td class="myModalLabel" valign="top"><?php echo $sendMsgLBL["message"];?>: </td>
                                         <td>
                                            <table border="0" width="100%" cellpadding="2" cellspacing="0" style="margin:0px; border-collapse:collapse">
                                               <tbody>
                                                  <tr>
                                                     <td style="width:500px"><textarea class="form-control" name="msg" id="msg" rows="4"></textarea></td>
                                                  </tr>
                                               </tbody>
                                            </table>
                                         </td>
                                      </tr>
                                      <!-- END (Message Managment) --->
                                      <tr>
                                         <td></td>
                                         <td valign="top" style="white-space: nowrap;"><input type="checkbox" id="deleteNum" name="deleteNum" checked="checked" dir="<?php echo $dir;?>"> <b><?php echo $sendMsgLBL["delNum"];?></b></td>
                                      </tr>
                                      <tr>
                                         <td align="left" colspan="2">
                                            <hr>
                                            <input class="btn btn-success waves-effect waves-light" type="button" value="<?php echo $sendMsgLBL["send"];?>" name="send" id="process">
                                            <input class="btn btn-info  waves-effect waves-light" type="reset" value="<?php echo $sendMsgLBL["clear"];?>">
                                         </td>
                                      </tr>
                                   </tbody>
                                </table>
                                <input type=hidden name=accountUser value='<?php echo $accountUser?>'>
                				<INPUT type=hidden value=0  name=unicode1>
               					 <INPUT type=hidden value='<?php echo $userId?>'  name=userId2>
                                 <input type="hidden" name="varNum" id="varNum" value=0>
                                
                                
                                
                                <!--  reportSendModal -->
                                <div class="modal fade modal-fullscreen " style="z-index:10000;padding-<?php echo $align;?>:0px" id="reportSendModal" tabindex="-1" role="dialog" aria-labelledby="searchModalTitle" aria-hidden="true">
                                  <div class="modal-dialog modal-dialog-centered" role="document">
                                     <div class="modal-content text-right">
                                        <div class="modal-header modal-header-primary">
                                           <h5 class="modal-title" id="searchModalTitle"><?php echo $sendMsgLBL["reportSendMsg"];?></h5>
                                           <button type="button" class="close slidedown" data-dismiss="modal" aria-label="Close">
                                           <span aria-hidden="true">&times;</span>
                                           </button>
                                        </div>
                                        <div class="modal-body" style="text-align: <?php echo $align;?>;">
                                            <span id="reportResult"></span>
                                        </div>
                                        
                                     </div>
                                  </div>
                                </div>
                                <!--  reportSendModal -->
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- end row -->
            </div>
            <!-- end container-fluid -->
        </div>
        <!-- end page-content-wrapper -->
    </div>
        <!-- End Page-content -->
</div>
    <!-- end main content -->
<!-- End Dashboard page -->
<script type="text/javascript">
function openSubCatsTR(trId)
{
	if($('#subCatgoriesTRStatus'+trId).val() == '0')
	{
		$('#subCatgoriesTR'+trId).show('fast');
		$('#subCatgoriesTRStatus'+trId).val(1);
		$('#aCategoryId'+trId).css('color', '#900');
	}
	else
	{
		$('#subCatgoriesTR'+trId).hide('fast');
		$('#subCatgoriesTRStatus'+trId).val(0);
		$('#aCategoryId'+trId).css('color', '#000');
	}
}
function checkSubCats(mainCatId)
{
	if($('#mainCategory'+mainCatId).is(':checked'))
	{
		$("#subCatgoriesTR"+mainCatId+" [id^=subCategory"+mainCatId+"-]").prop('checked', 'checked');
	}
	else
	{
		$("#subCatgoriesTR"+mainCatId+" [id^=subCategory"+mainCatId+"-]").prop('checked', '');
	}
} 
</script>
<?php
include("footer.php");
ob_end_flush();
?>
