<?php

include("includes/useAVclass.php");

require_once "includes/connection.php";
require_once("includes/config.inc.php");

require_once "includes/functions.inc.php";

require_once "includes/functions-data.php";

//require_once "design.php";
$useAVclass = new useAVclass();
$useAVclass->connection(); 


// $_GET['editid'];


@extract($_GET);
@extract($_POST);
@extract($_SESSION);

if(isset($_POST['mydata']))
{

  if($_POST['mydata'] == 'thisdata')
  {
   

$salt =rand(19999, 29999);
$salt1 =rand(31999, 59999);

$hunger_spot = content_desc(check_input($_POST['hunger_spot']));
$address =	content_desc(check_input($_POST['address']));
$phone =	content_desc(check_input($_POST['phone']));
$state =	content_desc(check_input($_POST['state']));
$district =	content_desc(check_input($_POST['district']));
$pincode =	content_desc(check_input($_POST['pincode']));
$latitude =	content_desc(check_input($_POST['latitude']));
$lognitude =	content_desc(check_input($_POST['lognitude']));



	
	$errormsg="";
	
	$data = array('success' => false, 'messages' => array());  

	if($hunger_spot == ''){
		$errormsg .= "hunger_spot,";
	}

	if($address == ''){
		$errormsg .= "address,";
	}

	if($phone == ''  ||  !preg_match('/^[0-9]{10}+$/', $phone)  ){
		$errormsg .= "phone,";
	}

	if($state == ''){
		$errormsg .= "state,";
	}

	if($district == ''){
		$errormsg .= "district,";
	}

	if($pincode == ''  || !preg_match('/^[0-9]{6}+$/', $pincode) ){
		$errormsg .= "pincode";
	}
 

if($errormsg == '')
{

 	$date=date('Y-m-d'); 

	$tableName_send="hunger_spot";
	$tableFieldsName_send=array("hunger_spot","address","phone","state","district","pincode","latitude","lognitude","details");
	$tableFieldsValues_send=array("$hunger_spot","$address","$phone","$state","$district","$pincode","$latitude","$lognitude","$details");
	$useAVclass->insertQuery($tableName_send,$tableFieldsName_send,$tableFieldsValues_send,$pdo);
	$id = $pdo->lastInsertId();

	/*header("location:thanks.php");
	exit;*/
	    $data = array('success' => true, 'messages' => $id);
	    echo json_encode($data);
	    exit();

}
else
{


    $error = explode(',', $errormsg);

  //  $data['messages'] =  $error;
     foreach ( $error  as $key => $value) {

                $data['messages'][$key] = $value;
            } 

            
  }

   echo json_encode($data);

       exit();

	
}

}



?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>IFSA</title>
<!-- Bootstrap Core CSS -->
  <link href="assets/css/bootstrap.min.css" rel="stylesheet">
        <!-- Icon CSS -->
        <link href="assets/css/font-awesome.min.css" rel="stylesheet">
        <link href="assets/css/icofont.css" rel="stylesheet">
        
        <!-- Owl Carousel CSS -->
        <link rel="stylesheet" href="assets/css/owl.carousel.css">
        <!-- Main Style -->
        <link href="assets/css/style.css" rel="stylesheet">
        <!-- Responsive CSS -->
        <link href="assets/css/responsive.css" rel="stylesheet">
        <!-- Favicon -->
        <link rel="shortcut icon" type="image/png" href="assets/image/favicon.png">
<style type="text/css">
	.navbar-area .nav li a {
    font-size: 14px;
    color: #053542;
    padding: 6px 6px!important;
    margin-left: 3px;
    font-weight: bold;
}
</style>

        <style type="text/css">
          select {
            font-size: 12px !important;
          }
        </style>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Menu Access for Tab Key -->
<script src="js/superfish.js"></script>
<script type="text/javascript" src="js/validatejs.js"></script>
<link href="css/modern-ticker.css" type="text/css" rel="stylesheet">
<script src="js/jquery-migrate-1.0.0.js" type="text/javascript"> </script>
<script src="js/jquery.js" type="text/javascript"> </script>
<script src="js/jquery.meanmenu.js"></script>
<script type="text/javascript">
jQuery(document).ready(function () {
//jQuery('#main-nav nav').meanmenu();
});





</script>
<script src="js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" href="css/jquery-ui.css">
<script src="js/jquery-1.12.4.js"></script>
<script src="js/jquery-ui.js"></script>
<script>
		$( function() {
			$( "#example2" ).datepicker({
		dateFormat: "dd-mm-yy",
				minDate: 0
			});
		});
	</script>
<script type="text/javascript" src="<?php echo $HomeURL; ?>/js/jquery.validate.min.js"></script>
<script type="text/javascript">
jQuery.validator.addMethod("alphanumeric", function(value, element) {
return this.optional(element) || /^\w+$/i.test(value);
}, "Letters, numbers, and underscores only please");

jQuery.validator.addMethod("alpha", function(value, element) {
return this.optional(element) || value == value.match(/^[a-zA-Z]+$/);
});


(function($,W,D)
{
//alert("fffff");
var JQUERY4U = {};

JQUERY4U.UTIL =
{
	setupFormValidation: function()
	{
		//form validation rules
		$("#contact_form").validate({
			
			rules: {
			  
				
				txtphone: {
							required: true,
							number: true,
							minlength: 10
					},
				hunger_spot: "required",
				state: "required",	
				district: "required",
				lognitude: "required",
				lognitude: "required",
				phone: "required",
				address: "required",
			},
		   
			
			
			messages: {
			 
			  hunger_spot: "Please Enter Name",
			   state: "Please Select State",
			  district: "Please Enter District",
				pincode: "Please  Enter Pincode",
				phone: "Please Enter Contact Number that should be 10 to 12 digits",
				lognitude: "Please Enter Logintude",
				lognitude: "Please  Enter Lognitude",
				address: "Please Enter Address",
				
				  
			},
			submitHandler: function(form) {
				form.submit();
			}
		});
	}
}

//when the dom has loaded setup form validation rules
$(D).ready(function($) {

	JQUERY4U.UTIL.setupFormValidation();
});

})(jQuery, window, document);
function getlocation(val) {
var address = val;
//alert(val);
$.ajax({
	url: "getcordinate.php",  
	type: "POST", 
	data: {"address":address},	 
	cache: false,
	dataType: "json",
	success: function (msg) {  
	 $("#latitude").val(msg.results[0].geometry.location.lat);
$("#lognitude").val(msg.results[0].geometry.location.lng);
length=msg.results[0].address_components.length-1;
$("#pincode").val(msg.results[0].address_components[length].long_name);
	}	   
});
}

</script>
<script>
var x = document.getElementById("demo");
getLocation();
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
	
        //x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
var lat=position.coords.latitude;
var long=position.coords.longitude;

$("#latitude").val(lat);
$("#lognitude").val(long);



$.ajax({
        url: "auth/webservices/test.php",  
        type: "POST", 
        data: {"lat":lat , "long":long},     
        cache: false,
       dataType: "json",
        success: function (msg) { 
        //alert("hello"); 
         $("#address").val(msg.results[0].formatted_address);
         length=msg.results[0].address_components.length-1;
$("#pincode").val(msg.results[0].address_components[length].long_name);
        }       
    });

}
</script>
<style type="text/css">
  .col-md-6 {
    width: 50%;
    padding: 10px;
}

</style>
</head>
<body class="single-service">
        <!-- start #main-site-wrapper -->
        <main id="main-site-wrapper">
            <!-- start .header-area -->
            
            <script src="w3.js"></script>
            <div w3-include-html="menus.html" corssorigin="false"></div>
            <script>
                 w3.includeHTML();
            </script>
            
            <!-- start section .page-header -->
            <!-- start section .page-header -->
            <section class="page-header valigncenter text-center">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12" style="    margin-bottom: -100px;">
                            <h1>Beneficiary Registration</h1>
                            <!-- <ol class="breadcrumb">
                                <li><a href="index.html">Home</a></li>
                                <li class="active">Beneficiary Registration</li>
                            </ol> --> <!-- end .breadcrumb -->
                        </div> <!-- end .col-** -->
                    </div> <!-- end .row -->
                </div> <!-- end .container -->
            </section> <!-- end .page-header -->
            
            <!-- start section -->
            <section class="section-padding">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12">

                        		<?php 
			if($errormsg != ''):
				echo '<div style="color: red;">'.$errormsg.'</div>';
			endif;
		?>
                             <!-- start .page-inner -->
                            <div class="page-inner">
                                <div class="row">
                                    <div class="col-md-12">
                                       <section class="pb30 pt30">

<!-- Logo Part Start --> 

<!-- Menu Part End --> 

<!-- Menu Part End --> 

<!-- <div class="container-fluid header-img-m home_bg"> 
	<div class="container header-content">
        <h1>We care because<br><span>Eyes are precious!</span></h1>
		<h2>Integrated technology that enable <br> us to stand above the rest...</h2>
			<a href="<?php echo $HomeURL;?>/auth/appointments.php" class="btn btn-lg btn-primary">Book an Appointment</a>
	
      </div>
</div> --> 

<!--Carousel End -->
<div class="container content-area about-us-home-margin">
<div class="col-md-12">
<h2> Beneficiary Registration</h2>
<hr>
</div>

	
	<div class="col-md-12 form-style about-us-content-home">
	<div class="reg_form">
	  <form id="contact_form" method="post" class="" name='form1'>
		
		<fieldset>
		  <legend>Beneficiary Details</legend>
		  
		  <!-- Text input-->
		  <div class="col-md-6">
		  <div class="form-group">
			<label for="hunger_spot" class="control-label">Beneficiary Name: </label>
			  <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
				<input type="text" id="hunger_spot" class="form-control" placeholder="Name" name="hunger_spot" value="<?php if($_POST['hunger_spot']!=""){ echo $_POST['hunger_spot'];} else { echo $rr['hunger_spot'];} ?>" onKeyPress="return LettersWithSpaceOnly(event);">
				<br/>
			  </div>
			</div>
		  </div>
		  <div class="col-md-6">
		  <div class="form-group">
			<label for="state" class="control-label">State : <span class="star">*</span></label>
			  <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span> 
				<!-- <input type="text" id="state" class="form-control" placeholder="State" name="state" required value="<?php if($state!='') { echo $state; } else {};?>"> -->
				 <select name="state" id="state"  placeholder="State" class="form-control" autocomplete="off" onChange="getdistrict(this.value);">
				<option value=""> Select </option>
				<?php 
				$data = $pdo->query("SELECT * from state where c_status='1' order by c_name");
				while($role_module = $data->fetch())
				{

				?>
				<option value="<?php echo $role_module['c_id'];?>" <?php if ($role_module['c_id']==$state) { echo 'selected="selected"';}?>><?php echo $role_module['c_name']; ?></option>
				<?php }
				?>
				</select>
			  </div>
			</div>
		  </div>
		    <div id="dis">
		  <div class="col-md-6">
		  <div class="form-group">
			<label for="district" class="control-label">District : <span class="star">*</span></label>
			  <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
				<select name="district" class="form-control" id="district">
				  <option>Select</option>
				  <?php foreach($districts as $value) { ?>
				  <option value="<?php echo $value;?>"<?php if($rr['district']==$value) { echo "selected='selected'";}?>><?php echo $value;?></option>
				  <?php }?>
				</select>
			  </div>
			</div>
		  </div>
		  </div>
		  <!-- Text area -->
		  <div class="col-md-6">
		  <div class="form-group">
			<label for="name" class="control-label">Address : <span class="star">*</span></label>
			  <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-map-marker"></i></span>
				<input type='text'  id="address" class="form-control" placeholder="Address" name="address" value="<?php if($address!='') { echo $address; } else { echo $rr['address']; }?>" onBlur="return getlocation(this.value)" />
				<?php //echo $rr['address'];?>
			  </div>
			</div>
		  </div>
		  <div class="col-md-6">
		  <div class="form-group">
			<label for="name" class="control-label">Contact No. : <span class="star">*</span></label>
			  <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-phone"></i></span>
				<input type='text'  id="phone" class="form-control" placeholder="phone" name="phone" maxlength="10"  value="<?php if($phone!='') { echo $phone; } else { echo $rr['phone']; }?>" />
				<?php //echo $rr['address'];?>
			  </div>
			</div>
		  </div>
		  <div class="col-md-6">
		  <div class="form-group">
			<label for="details" class="control-label">Details :</label>
			  <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
				<textarea  id="details" class="form-control" placeholder="Details" name="details" value="<?php if($details!='') { echo $details; } else { echo $rr['details']; }?>" />
				<?php echo $rr['address'];?>
				</textarea>
			  </div>
			</div>
		  </div>
		  <div class="col-md-6">
		  <div class="form-group">
			<label for="pincode" class="control-label">Pincode :  <span class="star">*</span></label>
			  <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-map-marker"></i></span>
				<input type="text" id="pincode" class="form-control" placeholder="Pincode" name="pincode"  maxlength="6" value="<?php if($pincode!='') { echo $pincode; } else {};?>" onKeyPress="return isNumberDecimal(event);">
			  </div>
			</div>
		  </div>
		<!-- <div class="col-md-6">
		  <div class="form-group">
			<label for="latitude" class="control-label">latitude :<span class="star">*</span></label>
			  <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-map-marker"></i></span>
				<input type="text" readonly id="latitude" class="form-control" placeholder="Logintude" name="latitude" value="<?php if($latitude!='') { echo $latitude; } else {};?>" required>
			  </div>
			</div>
		  </div>
		  <div class="col-md-6">
		  <div class="form-group">
			<label for="lognitude" class="control-label">Lognitude :<span class="star">*</span></label>
			  <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-map-marker"></i></span>
				<input type="text" id="lognitude" readonly class="form-control"  name="lognitude" value="<?php if($lognitude!='') { echo $lognitude; } else {};?>" required>
			  </div>
			</div>
		  </div> -->			
		  <!-- Button -->
			<div class="col-md-12 text-center">
		  <div class="form-group">
			  <input type="submit" name="cmdsubmit" id="cmdsubmit" value="Submit" title="Submit" class="btn btn-warning" />
					&nbsp;&nbsp;
			  <input type="reset" class="btn btn-warning"  name="cmdreset" title="Reset" value="Reset">
			</div>
		  </div>
		</fieldset>
	  </form>
	</div>
	</div>
  
</div>

<!-- Footer part --> 

<!-- Footer part -->

      </div> <!-- end .row -->
                            </div> <!-- end .page-inner -->
                        </div> <!-- end .col-** -->
                    </div> <!-- end .row -->
                </div> <!-- end .container -->
            </section> <!-- end section -->

            <!-- start section -->
            

             <!-- start section -->
            <section class="section-padding">
                <div class="container">
                    
                </div> <!-- end .container -->
            </section> <!-- end section -->
<!-- Footer part --> 

<!-- Footer part -->

 <!-- start .footer-area -->
            <footer class="footer-area bg-gray">
               <script src="w3.js"></script>
<div corssorigin="false" w3-include-html="footer.html">    <br/><br/></div>
<script>
	 w3.includeHTML();
</script>
            </footer> <!-- end footer -->
        </main> <!-- end #main-site-wrapper -->
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="assets/js/jquery-1.12.4.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <!-- Bootstrap JS -->
        <script src="assets/js/bootstrap.min.js"></script>
        <!-- Owl JS -->
        <script src="assets/js/owl.carousel.min.js"></script>
        <!-- Theme Scripts JS -->
        <script src="assets/js/scripts.js"></script>  
 
<!-- Footer part --> 
<!-- Footer part --> 
<!-- <a href="<?php echo $HomeURL;?>/auth/appointments.php" rel="lightframe" class="lightbox-processed"><img src="<?php echo $HomeURL;?>/images/book-appointment.png" id="ext-link-left1"></a>   --> 
<!--This is for gallery --> 
<!-- Footer part --> 
<!-- Footer part --> 
<!-- <a href="<?php echo $HomeURL;?>/auth/appointments.php" rel="lightframe" class="lightbox-processed"><img src="<?php echo $HomeURL;?>/images/book-appointment.png" id="ext-link-left1"></a>   --> 
<!--This is for gallery --> 

<script>
$(document).ready(function(){
$(".nav-tabs a").click(function(){
	$(this).tab('show');
});
});
</script> 
<script type="text/javascript" src="js/jquery1.js"></script>
<link rel="stylesheet" href="css/jquery.css" type="text/css" media="screen">
<script type="text/javascript">
$(document).ready(function(){

	// enable fancybox if device window is more than 550
	if (screen.width > 550) {
		$('.fancybox').fancybox({
			padding: "7",
			mouseWheel: false,
			fitToView: true
		});
	}
	
	// skip the top nav chrome if on iPhone
	if ((navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPod") != -1)) {
		 window.scrollTo(0, 1);
	}
});



</script> 
<!--<script type="text/javascript" src="js/jsDatePick.js"></script>
<link href="css/jsDatePick.css" rel="stylesheet" type="text/css" />--> 

<script type="text/javascript">
function getPage(id) {
//alert(id);
//generate the parameter for the php script
var data = 'cat=' + id;
//alert(data);
$.ajax({
	url: "category.php",  
	type: "POST", 
	data: data,	 
	cache: false,
	success: function (html) {  
	 
		//hide the progress bar
		$('#loading').hide();   
		 
		//add the content retrieved from ajax and put it in the #content div
		$('#cat').html(html);
		 
		//display the body with fadeIn transition
		$('#content2').fadeIn('slow');	   
	}	   
});
}

function getdistrict(id) {
//alert(id);
    //generate the parameter for the php script
    var data = 'dis=' + id;
	//alert(data);
    $.ajax({
        url: "getcity.php",  
        type: "POST", 
        data: data,     
        cache: false,
        success: function (html) {  
         
            //hide the progress bar
            $('#loading').hide();   
             
            //add the content retrieved from ajax and put it in the #content div
            $('#dis').html(html);
             
            //display the body with fadeIn transition
            $('#content2').fadeIn('slow');       
        }       
    });
}



$("#contact_form").submit(function(event) {     	
    event.preventDefault();
	 mydata =  $(this).serialize();


	$.ajax({

		type:'post',
		url:'beneficiary.php',
		data:'mydata=thisdata&'+mydata,
   		dataType:'json',
		success:function(htm){

       console.log(htm);
			 
       if(htm.success == true ) {

         
         window.location="thanks.php";

       }
       else
       {
           $.each(htm.messages,function(key,value){
                    var element = $('#'+value);
                    var data_name = $('#'+value).attr('placeholder');

                    element.closest('div.input-group')
                    .removeClass('has-error')
                    .addClass(value ? 'has-error':'has-success')
                     .parent('div').find('.text-danger').remove();

                    element.parent('div.input-group').after('<span style="color:red;z-index: 9999999;position: absolute;width: 100%;" class="text-danger">Please fill valid  details for '+data_name+'</span>');

              });

       }

      
				
		},
		error:function()
		{

		}

	});	


});




</script> 
<script type="text/javascript">
$(".closestatus").click(function() {
$("#msgerror").addClass("hide").hide();
});
$(".closestatus").click(function() {
$("#msgclose").addClass("hide").hide();
});

</script>
</body>
</html>