<!doctype html>
<html class="no-js" lang="zxx">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Sudila Printers - Kurunegala Sri Lanka</title>
<meta name="description" content="Buddist books online; best online Buddist book store in Sri Lanka. Our collection include Sri Lankan Buddhist books, Buddhist Information and Online Books from Sri Lanka.">
<meta name="keywords" content="Sudila Printers">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Favicons -->
<link rel="shortcut icon" href="images/favicon.ico">

<!-- Google font (font-family: 'Roboto', sans-serif; Poppins ; Satisfy) -->
<link type="text/css" href="css/font_opensans.css" rel="stylesheet">
<link type="text/css" href="css/font_poppins.css" rel="stylesheet">
<link type="text/css" href="css/font_roboto.css" rel="stylesheet">
<!-- Stylesheets -->
<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="css/plugins.css">
<link type="text/css" rel="stylesheet" href="css/style.css">
<!-- Cusom css -->
<link type="text/css" rel="stylesheet" href="css/custom.css">
<!-- Modernizer js -->
<script type="text/javascript" src="js/vendor/modernizr-3.5.0.min.js"></script>
</head>
<body>
<!-- Main wrapper -->
<div class="wrapper" id="wrapper">
  <div id="pre_loader"></div>
  <!-- Header -->
  <?php include 'common/header.php';
	if (isset($_SESSION['temp_user_bb']) && isset($_SESSION['guest_user_bb'])) {
		$user_id = htmlspecialchars(strip_tags(trim($_SESSION['temp_user_bb'])), ENT_QUOTES, 'UTF-8');
		$whr = 'temp_id';

		if(empty($user_id)) {
			$redirectUrl = "index";
			echo "<script type=\"text/javascript\">";
			echo "window.location.href = '$redirectUrl'";
			echo "</script>";
		
		} else {
			$sqlt = mysqli_query($conn, "SELECT * FROM live_cart WHERE temp_id=$user_id"); //  AND User_ID='tmp_".$user_id."'
			if(mysqli_num_rows($sqlt) > 0) {
				$row_inv = mysqli_fetch_assoc($sqlt);
				$g_inv = $row_inv['inv_no'];
				$sql_up = mysqli_query($conn, "UPDATE live_cart SET User_ID='G$g_inv', inv_no='$g_inv' WHERE temp_id=$user_id");
			}
		}

	} else {
		if (!isset($_SESSION['normal_user_bb']) || empty($_SESSION['normal_user_bb'])) {
			$redirectUrl = "sign-in";
			echo "<script type=\"text/javascript\">";
			echo "window.location.href = '$redirectUrl'";
			echo "</script>";
		
		} else {
			$user_id = htmlspecialchars(strip_tags(trim($_SESSION['normal_user_bb'])), ENT_QUOTES, 'UTF-8');
			$whr = 'User_ID';
		}
	}

	$sql = mysqli_query($conn, "SELECT id FROM live_cart WHERE $whr=$user_id");
	if(mysqli_num_rows($sql) == 0) {
		$redirectUrl = "index";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";	

	}
  ?>
  <!-- //Header --> 
  <!-- Start Bradcaump area -->
  <div class="ht__bradcaump__area bg-image--4">
    <div class="container">
      <div class="row">
        <div class="col-lg-12">
          <div class="bradcaump__inner text-center">
            <h2 class="bradcaump-title">Checkout</h2>
            <nav class="bradcaump-content"> <a class="breadcrumb_item" href="index">Home</a> <span class="brd-separetor">/</span> <a class="breadcrumb_item" href="my-cart">My Cart</a> <span class="brd-separetor">/</span> <span class="breadcrumb_item active">Checkout</span> </nav>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- End Bradcaump area --> 
        <!-- Start Checkout Area -->
        <section class="wn__checkout__area pt--30 pb--30 bg__white">
        	<div class="container">
				<div class="row">
        			<div class="col-lg-12 col-12">
					<form id="chk_form" method="post" action="javascript:void(0);">
        				<div class="customer_details account__form">
        					<h3>Recipient Information</h3>
        					<div class="customar__field mt--30">
								<div class="row">
	        						<div class="input_box col-md-6">
	        							<label>First Name<span>*</span></label>
	        							<input class="clr" type="text" id="fname" name="fname" autocomplete="off" placeholder="" required>
										<span class="help-block"></span>
	        						</div>
	        						<div class="input_box col-md-6">
	        							<label>Last Name<span>*</span></label>
	        							<input class="clr" type="text" id="lname" name="lname" autocomplete="off" placeholder="" required>
										<span class="help-block"></span>
	        						</div>
        						</div>
								<div class="row">
									<div class="input_box col-md-6">
										<label>Contact No<span>*</span></label>
										<input class="clr" type="text" id="tel_no" name="tel_no" maxlength=10 autocomplete="off" placeholder="" required>
										<span class="help-block"></span>
									</div>
									<div class="input_box col-md-6">
										<label>Email Address<span>*</span></label>
										<input class="clr" type="email" id="email" name="email" autocomplete="off" placeholder="" required>
										<span class="help-block"></span>
									</div>
								</div>
								<div class="row">
	        						<div class="input_box col-md-6">
	        							<label>Address Line1<span>*</span></label>
	        							<input class="clr" type="text" id="address_1" name="address_1" autocomplete="off" placeholder="" required>
										<span class="help-block"></span>
	        						</div>
	        						<div class="input_box col-md-6">
	        							<label>Address Line2</label>
	        							<input class="clr" type="text" id="address_2" name="address_2" autocomplete="off" placeholder="">
										<span class="help-block"></span>
	        						</div>
        						</div>
								<div class="row">
	        						<div class="input_box col-md-6">
	        							<label>City<span>*</span></label>
	        							<input class="clr" type="text" id="city" name="city" autocomplete="off" placeholder="" required>
										<span class="help-block"></span>
	        						</div>
	        						<div class="input_box col-md-6">
	        							<label>Postcode / ZIP</label>
	        							<input class="clr" type="text" id="postal_code" name="postal_code" autocomplete="off" placeholder="">
										<span class="help-block"></span>
	        						</div>
        						</div>
								<div class="row">
	        						<div class="input_box col-md-6">
										<label>Country<span>*</span></label>
										<select class="select__option clr" id="country" name="country" required>
											<option>Sri Lanka</option>
											<?php $sql_ct = mysqli_query($conn, "SELECT country FROM ems_charges WHERE max_kg>0 ORDER BY country");while($row_ct = mysqli_fetch_array($sql_ct)) { ?>
												<option><?php echo $row_ct['country']; ?></option>
											<?php } ?>
										</select>
										<span class="help-block"></span>
	        						</div>
	        						<div class="input_box col-md-6">
										<label>Delivery Method<span>*</span></label>
										<select class="select__option clr" id="del_method" name="del_method" required>
										  <option value="">Select Delivery Method</option>
										  <?php
										  $sqldel = "SELECT * FROM delivery_methods WHERE enable_method = 1 ORDER BY order_id ASC";
										  $sqld = mysqli_query($conn, $sqldel);
										  while($rowd = mysqli_fetch_array($sqld)) {
											  echo '<option id="'.$rowd['method_id'].'" value="'.$rowd['id'].'">'.$rowd['method_name'].'</option>';
										  }
										  /*<!--<option value="1" id="postal_chk">Postal - Deliver within 3-5 days</option>-->
										  <option value="2" id="cour_chk">Courier Service - Deliver within 3-4 days</option>
										  <option value="3" id="ems_chk">Expedited Mail Service (EMS)</option>
										  <!--<option value="4" id="cash_on">Cash On Delivery - Deliver within 3-4 days</option>-->
										  */
										  ?>
										</select>
										<span id="txt_error2" style="font-size: 12px; color: red; display:none">*Maximum Total Weight is 20kg<br></span> 
										<span class="help-block"></span>
	        						</div>
        						</div>
        					</div>
							<div id="accordion" class="checkout_accordion mt--30" role="tablist">
								<div class="payment">
									<div class="che__header" role="tab" id="headingOne">
										<a class="checkout__title" data-toggle="collapse" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
											<span>Special Messages</span>
										</a>
									</div>
									<div id="collapseOne" class="collapse show" role="tabpanel" aria-labelledby="headingOne" data-parent="#accordion">
										<div class="payment-body customar__field">
											<div class="input_box" style="margin-bottom:5px">
											<label class="text-primary">Special Instructions: (Optional)</label>
												<input class="clr" type="text" name="s_msg" id="s_msg" autocomplete="off" placeholder="" maxlength="250">
											</div>
											<i>* Please make your special instructions short and clear.</i>
											<b>Example: 'Call the recipient before the delivery.'</b>
										</div>
									</div>
								</div>
							</div><hr class="mt--20">
							<div id="errorDiv"></div>
							<div class="mt--20 row">
								<div class="col-md-12 d-flex justify-content-between">
									<div class="submite__btn">
										<a href="my-cart">Edit Cart</a>
									</div>
									<div class="product-add-to-cart chkt">
										<input type="submit" value="NEXT" id="btn_chk" name="btn_checkout">
									</div>
								</div>
							</div>
        				</div>
        			</form>
        			</div>
        		</div>
        	</div>
        </section>
        <!-- End Checkout Area -->
  <!-- Footer Area -->
  <?php include 'common/footer.php'; ?>
  <!-- //Footer Area --> 
</div>
<!-- //Main wrapper -->
<!-- JS Files --> 
<script type="text/javascript" src="js/vendor/jquery-3.2.1.min.js"></script> 
<script type="text/javascript" src="js/popper.min.js"></script> 
<script type="text/javascript" src="js/bootstrap.min.js"></script> 
<script type="text/javascript" src="js/plugins.js"></script> 
<script type="text/javascript" src="js/active.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/add-to-cart.js"></script>
<script type="text/javascript">
var del_type = localStorage['delvry_type_b'];
var country = localStorage['country_b'];

$(document).ready(function () {
	// valid email pattern
	var eregex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	$.validator.addMethod("validemail", function (value, element) {
		return this.optional(element) || eregex.test(value);
	});
	
	// contact no validation
	var telregex = /^\d{10}$/;
	$.validator.addMethod("validtel", function (value, element) {
		return this.optional(element) || telregex.test(value);
	});	
	
	// JavaScript Validation For Checkout Form 
	$("#chk_form").validate({
		rules: {
			email: {
				validemail: true
			},
			tel_no: {
				validtel: true,
			}
		},
		messages: {
			email: {
				validemail: "Please Enter Valid Email Address"
			},
			tel_no: {
				validtel: "Invalid Contact Number"
			}
		},
		errorPlacement: function (error, element) {
			$(element).closest('.input_box').find('.help-block').html(error.html());
		},
		highlight: function (element) {
			$(element).closest('.input_box').removeClass('has-success').addClass('has-error');
		},
		unhighlight: function (element, errorClass, validClass) {
			$(element).closest('.input_box').removeClass('has-error');
			$(element).closest('.input_box').find('.help-block').html('');
		},
		submitHandler: submitForm
	});

	function submitForm() {
		$("#pre_loader").show();
		localStorage['country_b'] = $("#country").val();
		localStorage['delvry_type_b'] = $("#del_method").val();
				
		$.ajax({
			type: 'POST',
			async: false,
			url: 'connection/checkout_conn',
			data: $('#chk_form').serialize(),
			dataType: 'json',
			success: function (data) {
				$('input[name=btn_chk]').prop('disabled', true);
				$('input[type=text],input[type=select],input[type=email]').prop('disabled', true);
				setTimeout(function () {
					if (data.status === '1') {
						window.location.href = 'order-summary';
					
					} else if (data.status === '3') {
						alert('Session Expired. Please Try Again!');
						window.location.href = 'sign-in';
					
					} else {
						$("#pre_loader").hide();
						$('#errorDiv').slideDown(200, function () {
							$('#errorDiv').html('<div class="alert alert-danger">' + data.message + '</div>');
							$('#errorDiv').delay(5000).slideUp(100);
							$('input[type=text],input[type=select],input[type=email]').prop('disabled', false);
							$('input[name=btn_chk]').prop('disabled', false);
						});
					}
				}, 1000);
			},
			error: function () {
				$("#pre_loader").hide();
				$('#errorDiv').slideDown(200, function () {
					$('#errorDiv').html('<div class="alert alert-danger">An unknown error occoured, Please try again Later...!</div>');
					$('#errorDiv').delay(5000).slideUp(100);
					$('input[type=text],input[type=select],input[type=email]').prop('disabled', false);
					$('input[name=btn_chk]').prop('disabled', false);
				});
			}
		});
		return false;
	}
	
	var countryd = del_typed = '';
	if(country !== '') {
		if(country === undefined) {
			countryd = '';
		} else {
			countryd = country;
		}
	} else {
		countryd = '';
	}

	if(del_type != '') {
		if(del_type === undefined) {
			del_typed = '';
		} else {
			del_typed = del_type;
		}
	} else {
		del_typed = '';
	}

	var county = del_t = ''; 
	$.post("connection/checkout_conn", {'get_chkout_details': "data"}, function( data ) {
		if(data === '') {
			$(".clr").val('');
		
		} else {
			$("#fname").val(data.f_name);
			$("#lname").val(data.l_name);
			$("#tel_no").val(data.tel_no);
			$("#address_1").val(data.address_1);
			$("#address_2").val(data.address_2);
			$("#city").val(data.city);
			$("#postal_code").val(data.postal_code);
			$("#email").val(data.email);
			$("#s_msg").val(data.special_msg);
			if(data.country != undefined) {
				county = data.country;
			}
			if(data.delivery_type != undefined) {
				del_t = data.delivery_type;
			}
		}
		
		update_country(countryd, county, del_typed, del_t);
		
	}, "json");
	
});

function update_country(local_contry, data_contry, local_delt, data_delt) {
	//console.log(local_contry+', '+data_contry);
	if(local_contry === '' && data_contry != '') {
		$("#country").val(data_contry);
	
	} else if(local_contry != '' && data_contry === '') {
		$("#country").val(local_contry);
	
	} else if(local_contry === '' && data_contry === '') {
		$("#country").val('Sri Lanka');
	
	} else {
		$("#country").val(local_contry);
	}
	
	//console.log(local_delt+', '+data_delt);
	if(local_delt === '' && data_delt != '') {
		$("#del_method").val(data_delt);
	
	} else if(local_delt != '' && data_delt === '') {
		$("#del_method").val(local_delt);
	
	} else if(local_delt === '' && data_delt === '') {
		$("#del_method").val('');
	
	} else {
		$("#del_method").val(local_delt);
	}
	
	if($("#del_method").val() != '3') {
		$("#country").val('Sri Lanka');
	}
}

$("#del_method").change(function() {
	var val = $(this).val();

	if(val === '3') {
		$("#country").val('');

	} else {
		$("#country").val('Sri Lanka');
	}
	
	if(val === '1') {
		$('#txt_error2').fadeIn();
	
	} else {
		$('#txt_error2').fadeOut();
	}
});

$("#country").change(function() {
	var country = $(this).val();
	if(country == 'Sri Lanka') {
		$("#del_method").val('');

	} else {
		$("#del_method").val('3');
	}
});

</script>
</body>
</html>