<?php 
	require "_header.php";
	require "data/combo_semester.php";
		require "data/combo_student.php";
	require "data/sendingsms_studentpresant1.php";
?>
<div id="main-content">
<style type="text/css">
	label.myBold
	{
	   font-weight: bold;
	   color:#15428B;
	   font-size: 10px;
	}
</style>
<script type="text/javascript" src="js/import-employee.js"></script>
<script type="text/javascript" src="js/import-student.js"></script>
<script type="text/javascript" src="js/import-group.js"></script>
<script language="javascript" type="text/javascript">
	function handlekeypress(){
		var msgprifix=Ext.getCmp('msg_prefix').getValue();
		var msgbody=Ext.getCmp('msg_body').getValue();
		var msgsuffix=Ext.getCmp('msg_suffix').getValue(); 
		var total= msgprifix.length + msgbody.length + msgsuffix.length;
		var divid=total/160;
		var modu=Math.round(divid);
		if(divid > modu){
			modu=modu+1;
			Ext.getCmp('msglabel').setText(total +' Characters('+ modu +' SMS)');	
		} else{
			Ext.getCmp('msglabel').setText(total +' Characters('+ modu +' SMS)');
		}
	}
	function phoneDigits(obj,e){
		var s = Ext.getCmp('mobile_number').getValue();
		var l = s.length;
		if(s.length==0){
			Ext.getCmp('per').setText('0 Contacts');	
		} else{
			for(i=0; i<l; i++){
				if(s.charCodeAt(i)==45 || (s.charCodeAt(i) >= 48 && s.charCodeAt(i) <= 57) || s.charCodeAt(i) == 44 ){
					if(s.length > 0){
						varcomma=(s.split(/,/g).length)-1;
						cnt = varcomma + 1;
						Ext.getCmp('per').setText(cnt +' Contacts');	
					} else{
						Ext.getCmp('per').setText('0 Contacts');	
					}
				} else{
					s = s.substring(0,i);
					Ext.getCmp('mobile_number').setValue(s);
				}
			}
		}
	}
	function ValidateMobileNo (strMobileNo) {
		for(i=0;i<strMobileNo.length;i++){
			if(strMobileNo[i]=="") {
				Ext.MessageBox.show({
				   	title: 'Error',
				   	msg: "There is extra comma after moblie number ",
					buttons: Ext.MessageBox.OK,
					icon:Ext.MessageBox.ERROR		   
				});					
				Ext.getCmp('mobile_number').focus(true,500); 
				return false;
			} else if(strMobileNo[i].length!=10){
				Ext.MessageBox.show({
					title: 'Error',
					msg: "Mobile Number must be of 10 digit. Wrong No:"+ strMobileNo[i],
					buttons: Ext.MessageBox.OK,
					icon:Ext.MessageBox.ERROR
				});
				Ext.getCmp('mobile_number').focus(true,500); 
				return false;
			}
		}
		return true;
	}
</script>
<script language="javascript" type="text/javascript">
	var labelArray;
	labelArray = [
		['Mobile Number:<span class="comp">*</span>'],	
		['Message Prifix:'],
		['Message:<span class="comp">*</span>']	,
		['Message Suffix:'],
		['Mobile No:<span class="comp">*</span>']		
	];
	var cnt = 0;
	var msgcnt = 1;
	Ext.onReady(function(){
		var sendsms_container = {
        	xtype:'fieldset',
        	border:false,
        	labelWidth:125,
         	items:[{
				xtype : 'container',
				border : false,
				layout : 'column',
				defaultType : 'field',
				items : [{
					xtype : 'container',
					layout : 'form',
					columnWidth : 1,
					labelWidth  :100,
					width : 170,
					items : [{
						xtype: 'label',
						style: 'padding:0px 0 5px 110px;',
						cls: 'x-form-item myBold',
						text:"Availabel  <?php  echo getBalance($SmsUserName,$SmsPassword,$SMSGetbalance); ?>",
						name: 'avilabel',	
						id:'avilabel'
					},{
						xtype: 'textarea',
						name:'mobile_number',
						growAppend: "",
						id:'mobile_number',
						labelSeparator:'',	
						fieldLabel :labelArray[0],
						width:325,
						height: 100,
						emptyText:'Enter Mobile Number E.g. 9998896396,9898885858,',
						allowBlank:false ,
						enableKeyEvents: true,
						listeners:{
							keyup: phoneDigits
						}
					}]
				},{
					xtype       : 'container',
					layout      : 'form',
					columnWidth : 1,
					style: 'padding:0 0 5px 110px;',
					labelWidth  :40,
					items       : [{
						xtype: 'label',
						text:"0 Contacts",
						cls: 'x-form-item myBold',
						name: 'per',											
						id:'per'
					}]
             	},{
					xtype       : 'container',
					layout      : 'form',
					columnWidth : 1,
					labelWidth  :100,
					items       : [{
						xtype: 'textfield',
						name:'msg_prefix',
						id:'msg_prefix',
						labelSeparator:'',	
						fieldLabel :labelArray[1] ,
						disabled:true,
						width:318,
						allowBlank:true,
						enableKeyEvents: true,
						listeners: {
						  	keyup: handlekeypress
						}
					}]
				},{
					xtype       : 'container',
					layout      : 'form',
					columnWidth : 1,
					labelWidth  :100,
					items       : [{
						xtype: 'textarea',
						name:'msg_body',
						growAppend: "",
						labelWidth  :125,
						id:'msg_body',
						labelSeparator:'',	
						fieldLabel :labelArray[2] ,
						width:318,
						height: 100,
						emptyText:"Enter your Message here",
						labelWidth  :125,
						allowBlank:false ,
						maxLengthText: 'max 250 characters',
						enableKeyEvents: true,
						listeners: {
							keyup: handlekeypress
						}
					}]
              	},{
					xtype       : 'container',
					layout      : 'form',
					columnWidth : 1,
					style: 'padding:0 0 5px 110px;',
					labelWidth  :100,
					items       : [{
						xtype: 'label',
						cls: 'x-form-item myBold',
						text:"0 Characters(0 SMS)",
						name: 'msglabel',	
						id:'msglabel'
					}]
              	},{
					xtype       : 'container',
					layout      : 'form',
					columnWidth : 1,
					height:30,
					labelWidth  : 100,
					items       : [{
						xtype: 'textfield',
						name:'msg_suffix',
						id:'msg_suffix',
						labelSeparator:'',	
						fieldLabel :labelArray[3] ,
						width:318,
						allowBlank:true,
						disabled:true,
						enableKeyEvents: true,
						listeners: {
							keyup: handlekeypress
						}
					}]
              	}]
			}]
		};
		
		var sendsms_form = new Ext.FormPanel({
			id: 'sendsms_form',
			url:'sending-customize1.php',
			method:'post',
			frame:true,
			title: '',
			layout: 'column', 
			items: [{
				columnWidth:1,
				defaults:{border:false},
				autoHeight:true,
				bodyStyle:Ext.isIE ? 'padding:5px 0 5px 5px;' : 'padding:0px 0px;',
				items:[sendsms_container]
			}]
		});
	
		if(!sendsmsWindow){	
			var  sendsmsWindow = new Ext.Window({
				title: 'Customize Message',
				height: 430,
				layout: 'fit',
				labelWidth:125,		
				width: 550,
				modal:true,
				border: false,
				closable: true,
				collapsible:'true',
				bodyStyle:'padding: 5px',
				items: sendsms_form,
				buttonAlign: 'center',
				buttons:[{
						text: 'Student Contacts',
						handler: function () {						 						
							StudentContactWindow.show();
						}
					},{
						text: 'Group Contacts',
						handler: function () {						 						
							GroupContactWindow.show();
						}
					},{
						text: 'Emp. Contacts',
						handler: function () {						 						
							officecontactWindow.show();
						}
					},
					{
						text: 'Send',
						name: 'btnSave',
						id: 'btnSave',
						handler: function(){
							mobilenumber=Ext.getCmp('mobile_number').getValue();
							msgbody=Ext.getCmp('msg_body').getValue();
							Ext.getCmp('msg_prefix').enable(true);	
							Ext.getCmp('msg_suffix').enable(true);
							if(mobilenumber!='' && msgbody!=''){
								spilmobailno=mobilenumber.split(',');								
								if(ValidateMobileNo(spilmobailno)) {
									sendsms_form.getForm().submit({
										timeout: 3000,
										waitMsg: 'Saving...',
										success: function(){
											Ext.MessageBox.show({
												title: 'Message',
												msg: 'Message Send to selected student.',
												buttons: Ext.MessageBox.OK,
												icon:Ext.MessageBox.INFO
											});
											
											sendsms_form.getForm().reset();
											Ext.getCmp('msg_prefix').disable(true);	
											Ext.getCmp('msg_suffix').disable(true);
											Ext.getCmp('per').setText('0 Contacts');	
											var gtotal=Ext.getCmp('msg_prefix').getValue().length+Ext.getCmp('msg_suffix').getValue().length;
											Ext.getCmp('msglabel').setText(gtotal +' Characters(' +'1 SMS)');	
											Ext.getCmp('avilabel').setText("Avilabel  <?php echo getBalance($SmsUserName,$SmsPassword,$SMSGetbalance) ?>");
										},
										failure: function (){
											Ext.MessageBox.show({
												title: 'Error',
												msg: 'Please fill complete form',
												buttons: Ext.MessageBox.OK,
												icon:Ext.MessageBox.ERROR
											});
										}
									});
								}
							} else{
								Ext.MessageBox.show({
									title: 'Error',
									msg: 'Please Fill Complete Form',
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});
							} 
						}
					},
					{
						text: 'Reset',
						handler: function(){
							sendsms_form.getForm().reset();
							Ext.getCmp('per').setText('0 Contacts');	
							Ext.getCmp('msglabel').setText('0 Characters(0 SMS)');
							Ext.getCmp('avilabel').setText("Avilabel Balance is " + bal);	
						}
					},
					{
						text: 'Close',
						handler: function(){
							sendsmsWindow.close();
						}
					}
				]
			})
		}
			
		var varWidth =  (screen.width - sendsmsWindow.width)/2;
		sendsmsWindow.setPosition(varWidth,((screen.height/4)-85));
		
		//alert('<?php //echo $SMSGetbalance; ?>');
		Ext.getCmp('msg_prefix').setValue('Dear Parents, ');
		Ext.getCmp('msg_suffix').setValue(' From Image ETI.');
		var gtotal=Ext.getCmp('msg_prefix').getValue().length+Ext.getCmp('msg_suffix').getValue().length;
		Ext.getCmp('msglabel').setText(gtotal +' Characters(' +'1 SMS)');
		sendsmsWindow.show();
		
		StudentContactWindow.on('hide',function(){
			phoneDigits();	
			Ext.getCmp('mobile_number').focus(true,500);
		});
		
		officecontactWindow.on('hide',function(){
			phoneDigits();	
			Ext.getCmp('mobile_number').focus(true,500);
		});
		
		GroupContactWindow.on('hide',function(){
			phoneDigits();	
			Ext.getCmp('mobile_number').focus(true,500);
		});
	});
</script>
<div id="abcd" align="center"></div>
</div>
<?php require "_footer.php";?>