<script type="text/javascript">
 function validate() {
var str=document.form1.contact.value;
       var valid="1234567890";
	   
	   for(i=0;i<str.length;i++){
	   if(valid.indexOf(str.charAt(i))==-1) {
	   alert("Phone Number can only be Numeric!");
	   document.myForm.phone.value=="";
	   document.myForm.phone.focus();
	   return false;
	   }}
	   if(document.myForm.phone.value=="") {
		   alert("enter your phone number");
		   document.form1.contact.value=="";
	       document.form1.contact.focus();
		   return false;
		   
	   }
  
	   if(document.form1.contact.value==""){
	 alert("Enter Phone No");
	document.form1.contact.focus();
	return false;
   }
  var phne = form1.contact.value;
  if(phne.length < 10){
	  alert("Enter A valid Phone Nember!");
	document.form1.contact.focus();
	return false;
	  
  }
	   
   }
</script>

<div class="modal fade" id="myModal" tabindex="-1" role="form" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> Cancel &times;</button>
                <h4 class="modal-title" id="myModalLabel"> Add new Contact </h4>
            </div>
				<div class="modal-body">
			   
					<form name="form1" data-parsley-validate class="form-horizontal form-label-left" method="post" action="" onsubmit="return validate()" >
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-3" for="first-name">Contact Name <span class="required"> </span>
							</label>
								<div class="col-md-6 col-sm-6 col-xs-7">
									<input type="text" id="firstname" name="firstname" required="required" class="form-control col-md-3 col-xs-3">
								</div>
						</div>
					 
					   <div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-3" for="last-name">Mobile Number <span class="required"></span>
								</label>
									<div class="col-md-6 col-sm-6 col-xs-7">
										<input type="text" id="contact" name="contact" required="required" class="form-control col-md-3 col-xs-3"> <b style="color:green;">Number Format, 07XXXXXXXX</b>
									</div>
						</div>
					  
					     <label class="control-label col-md-3 col-sm-3 col-xs-3" for="last-name">Group <span class="required"></span>
								</label>
								<div class="form-group">
											<div class="col-md-4 col-sm-4 col-xs-5">
												<select class="form-control" name="group" reguired>
													<option>--Assign Group--</option>
													<?php 
														$grps = mysql_query("select * from airtime_groups where customer_id='$session_id' order by group_id");
															$numrows = mysql_num_rows($grps);
																if($numrows == 0){
														         echo ' <div class="col-md-offset-3 col-md-9">
																	<button id="btn-signup" type="button" class="btn btn-error"><i class="icon-hand-right"></i>Invalid You Have Not Created Any Group Yet! </button>
                                         
																	</div>';
																}
																else{
																	while($record = mysql_fetch_array($grps)){
		 
																	echo '<option value="'. $record['group_id'] .'">' . $record['name'] . '</option>';
																	}
																}
													?>    
												</select>
											</div>
								</div>
					  
							<div class="form-group">
								<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
									<input type="reset"  class="btn btn-primary" name="cancel" value="Cancel" />
									<input type="submit"  class="btn btn-success" name="savecont" value="Save Contact"/>
								</div>
							</div>
					</form>
				</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
	
			   	<?php 
				if(isset($_POST['savecont'])){
					
					$fname = $_POST['firstname'];
					$phone = $_POST['contact'];
					$grp = $_POST['group'];
					
					if($grp != "--Assign Group--"){
						
						$cn = mysql_query("select * from airtime_contacts where customer_id = '$session_id' and contact = '$phone' and group_id = '$grp' ");
						$numrows = mysql_num_rows($cn);
						
						if($numrows > 0){
							die(mysql_error());
						}
						else{
							 $query = mysql_query("insert into airtime_contacts (customer_id,fname,contact,group_id) values('$session_id','$fname', '$phone','$grp') ");
							 
							 if($query){
							     // get group name
								$qry1 = mysql_query("select * from airtime_groups where group_id = '$grp' ");
								$gdt=mysql_fetch_array($qry1);
								$gname = $gdt['name'];
								
							    $qry = mysql_query("select * from airtime_logs where id = '$session_lid' ");
							    $sdata=mysql_fetch_array($qry);
							    $actions = $sdata['user_actions'];
							    mysql_query("update airtime_logs set user_actions = '$actions Added a new contact - $fname to Group - $gname.' where id = '$session_lid'  ");
							 }
						
						}
						  
					}
					else {
						echo ' <div class="col-md-offset-3 col-md-9">
								<button id="btn-signup" type="button" class="btn btn-error"><i class="icon-hand-right"></i> Choose Group before you save contact! </button>
                                         
							</div>';
					}
				}
				?>
			