<?php
/**
 * Checkout Form
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/checkout/form-checkout.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see 	    https://docs.woocommerce.com/document/template-structure/
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     2.3.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
 
<!--   <table>
  <tr><td width="70%"> <h1 class="entry-title-cart">Checkout</h1> </td>
 <td> <div class="cart-header"><?php if ( is_active_sidebar( 'sidebar-woo' )  ) : ?><?php dynamic_sidebar( 'sidebar-woo' ); ?><?php endif; ?></div> </td></tr>
 </table> -->
<?php
wc_print_notices();

do_action( 'woocommerce_before_checkout_form', $checkout );

// If checkout registration is disabled and not logged in, the user cannot checkout
if ( ! $checkout->is_registration_enabled() && $checkout->is_registration_required() && ! is_user_logged_in() ) {
	echo apply_filters( 'woocommerce_checkout_must_be_logged_in_message', __( 'You must be logged in to checkout.', 'woocommerce' ) );
	return;
}

?>
<style>
.align-justify{
	text-align:justify;
}
#custom_input{
	display:none;
}
</style>
<?php $homeEmergency=false;
$motorCover=false;
if(WC()->cart->get_cart()){
 foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
				$_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
				 
			if($_product->get_name()=="Home Emergency + Boiler Service"){	 
				$homeEmergency=true;
}

 $productArray = explode(" &ndash;",$_product->get_name());

if($productArray&&$productArray[0]&&$productArray[0]=="Motor Break Down Cover"){	 
				$motorCover=true;
}}}

			?>
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data">
<table class="shop_table show-responsive">
	
	<tbody>
							<tr class="cart_item">
						<td class="product-name"  style="background-color: rgb(43, 103, 11); color: #fff; text-align: center;border:0px"  width="45%">

							<div id="mMonthly" class="d"  <?php  if($_POST['billing_cycle']=="Yearly"){echo'style="display: none;"';}?>>
								<b>Monthly</b> <br><span class="price-box"><?php global $woocommerce; echo "£".number_format($woocommerce->cart->subtotal,'2');?></span><br><span style="font-size:11px;"> </span>
								<small style="font-size:9px;display:block">Including Online Discount</small>
							</div>


							<div id="mYearly" class="d"  <?php  if($_POST['billing_cycle']=="Monthly"){echo'style="display: none;"';}if (empty($_POST)){echo'style="display: none;"';}?>>
								    <b>Annualy</b> <br> <span class="price-box"><?php global $woocommerce; 
									$percentage = 12;
									$surcharge =  $woocommerce->cart->subtotal*12  ; 
									echo "£".number_format($surcharge,'2'); ?> </span>
								 	<br> <span style="font-size:11px;"> </span>
								 	<small style="font-size:9px;display:block">Including Online Discount</small>
							 </div>

						</td>
						<td class="moth-year-seperator" width="10%">	</td>
						<td class="product-total month-selection-td" width="45%">	
							<div id="inline_content"> 
								<input name="billing_cycle" id="billing_cycles" value="Monthly" class="select"  type="radio" <?php if($_POST['billing_cycle']=="Monthly"){echo'checked="checked"';}else {echo'checked="checked"';	}?> >Monthly
								<br> 
								<input name="billing_cycle" id="billing_cycles" value="Yearly" class="select" type="radio"  <?php if($_POST['billing_cycle']=="Yearly"){echo'checked="checked"';}?>>Annualy	
							</div>		
						</td>
					</tr>
						</tbody>

</table>

<div class="col2-set" id="customer_details">
			<div class="col-1">			

	<?php if ( $checkout->get_checkout_fields() ) : ?>

		<?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>

		
				<?php do_action( 'woocommerce_checkout_billing' ); ?>
			
			<?php do_action( 'woocommerce_checkout_shipping' ); ?>
			<?php //if($homeEmergency=="true"){?>
			<div>
				<h3 class="form-row form-row-wide" id="heading_field">Your password is a simple word or phrase that Smart Sure can use when we contact you so that you can verify who we are</h3>
				<p class="form-row form-row-wide validate-required validate-required" id="billing_email_policy_field" data-sort=""><label class="checkbox ">
						<input type="checkbox" class="input-checkbox " name="billing_email_policy" id="billing_email_policy" required> <img src="https://smart-sure.com/wp-content/uploads/2017/10/ArrowRight.gif" style="width: 30px;float: left;margin-left: -30px;"> Your policy documentation will be send to you by Email, Please tick this box if you would prefer to receive it by post <abbr class="required" title="required">*</abbr></label></p>
				<input type="hidden" class="termsEmail">
				<?php if(!$motorCover){?>
				<input type="hidden" class="termsBilling">
				<p class="form-row form-row-wide validate-required validate-required" id="billing_confirm_items_field" data-sort=""><label class="checkbox ">
						<input type="checkbox" class="input-checkbox " name="billing_confirm_items" id="billing_confirm_items" required> <img src="https://smart-sure.com/wp-content/uploads/2017/10/ArrowRight.gif" style="width: 30px;float: left;margin-left: -30px;"> I confirm that the covered items are in good working order without any faults and understand that I am responsible for all costs of repairs or replacement are carried out due to pre-existing fault <abbr class="required" title="required">*</abbr></label></p>	<?php }?></div>	
	
		<?php do_action( 'woocommerce_checkout_after_customer_details' ); ?>

	<?php endif; ?>
</div>

<div class="woocommerce-shipping-fields">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>

$(document).ready(function() {
	var billing_first_name_local=localStorage.getItem("billing_first_name");
	var billing_last_name_local=localStorage.getItem("billing_last_name");
	var billing_address_1_local=localStorage.getItem("billing_address_1");
	var billing_city_local=localStorage.getItem("billing_city");
	var billing_postcode_local=localStorage.getItem("billing_postcode");
	var billing_dob_local=localStorage.getItem("billing_dob");
	var billing_phone_local=localStorage.getItem("billing_phone");
	var billing_email_local=localStorage.getItem("billing_email"); 
	var billing_password_local=localStorage.getItem("billing_password"); 
	var shipping_first_name_local=localStorage.getItem("shipping_first_name"); 
	var shipping_last_name_local=localStorage.getItem("shipping_last_name"); 
	var shipping_company_local=localStorage.getItem("shipping_company");
	var shipping_address_1_local=localStorage.getItem("shipping_address_1");
	var shipping_address_2_local=localStorage.getItem("shipping_address_2");
	var shipping_city_local=localStorage.getItem("shipping_city");	
	var shipping_state_local=localStorage.getItem("shipping_state");
	var shipping_postcode_local=localStorage.getItem("shipping_postcode");
	
 
	$("#billing_first_name").val(billing_first_name_local);
	$("#billing_last_name").val(billing_last_name_local);
	$("#billing_address_1").val(billing_address_1_local);
	$("#billing_city").val(billing_city_local);
	$("#billing_postcode").val(billing_postcode_local);
	$("#billing_dob").val(billing_dob_local);
	$("#billing_phone").val(billing_phone_local);
	$("#billing_email").val(billing_email_local);
	$("#billing_password").val(billing_password_local);
	$("#shipping_first_name").val(shipping_first_name_local);
	$("#shipping_last_name").val(shipping_last_name_local);
	$("#shipping_company").val(shipping_company_local);
	$("#shipping_address_1").val(shipping_address_1_local);
	$("#shipping_address_2").val(shipping_address_2_local);
	$("#shipping_city").val(shipping_city_local);
	$("#shipping_state").val(shipping_state_local);
	$("#shipping_postcode").val(shipping_postcode_local);
	
	
	
 
 
	$('body').on('click', '#addItemBtn', function(e) {
		e.preventDefault();
		var billing_first_name = $("#billing_first_name").val();
		var billing_last_name=$("#billing_last_name").val();
		var billing_address_1=$("#billing_address_1").val();
		var billing_city=$("#billing_city").val();
		var billing_postcode=$("#billing_postcode").val();
		var billing_dob=$("#billing_dob").val();
		var billing_phone=$("#billing_phone").val();
		var billing_email=$("#billing_email").val();
		var billing_password=$("#billing_password").val();
		var shipping_first_name=$("#shipping_first_name").val();
		var shipping_last_name=$("#shipping_last_name").val();
		var shipping_company=$("#shipping_company").val();
		var shipping_address_1=$("#shipping_address_1").val();
		var shipping_address_2=$("#shipping_address_2").val();
		var shipping_city=$("#shipping_city").val();
		var shipping_state=$("#shipping_state").val();
		var shipping_postcode=$("#shipping_postcode").val();
		
		
		if(localStorage.getItem("billing_first_name")!=billing_first_name || billing_first_name==''){
	          localStorage.setItem("billing_first_name", billing_first_name);
		}
		if(localStorage.getItem("billing_last_name")!=billing_last_name || billing_last_name==''){
	          localStorage.setItem("billing_last_name", billing_last_name);
		}
		if(localStorage.getItem("billing_address_1")!=billing_address_1 || billing_address_1==''){
	          localStorage.setItem("billing_address_1", billing_address_1);
		}
		if(localStorage.getItem("billing_city")!=billing_city || billing_city==''){
	          localStorage.setItem("billing_city", billing_city);
		}
		if(localStorage.getItem("billing_postcode")!=billing_postcode || billing_postcode==''){
	          localStorage.setItem("billing_postcode", billing_postcode);
		}
		if(localStorage.getItem("billing_dob")!=billing_dob || billing_dob==''){
	          localStorage.setItem("billing_dob", billing_dob);
		}
		if(localStorage.getItem("billing_phone")!=billing_phone || billing_phone==''){
	          localStorage.setItem("billing_phone", billing_phone);
		}
		if(localStorage.getItem("billing_email")!=billing_email || billing_email==''){
	          localStorage.setItem("billing_email", billing_email);
		}
		if(localStorage.getItem("billing_password")!=billing_password || billing_password==''){
	          localStorage.setItem("billing_password", billing_password);
		}
		if(localStorage.getItem("shipping_first_name")!=shipping_first_name || shipping_first_name==''){
	          localStorage.setItem("shipping_first_name", shipping_first_name);
		}
		if(localStorage.getItem("shipping_last_name")!=shipping_last_name || shipping_last_name==''){
	          localStorage.setItem("shipping_last_name", shipping_last_name);
		}
		if(localStorage.getItem("shipping_company")!=shipping_company || shipping_company==''){
	          localStorage.setItem("shipping_company", shipping_company);
		}
		if(localStorage.getItem("shipping_address_1")!=shipping_address_1 || shipping_address_1==''){
	          localStorage.setItem("shipping_address_1", shipping_address_1);
		}
		if(localStorage.getItem("shipping_address_2")!=shipping_address_2 || shipping_address_2==''){
	          localStorage.setItem("shipping_address_2", shipping_address_2);
		}
		if(localStorage.getItem("shipping_city")!=shipping_city || shipping_city==''){
	          localStorage.setItem("shipping_city", shipping_city);
		}
		if(localStorage.getItem("shipping_state")!=shipping_state || shipping_state==''){
	          localStorage.setItem("shipping_state", shipping_state);
		}
		if(localStorage.getItem("shipping_postcode")!=shipping_postcode || shipping_postcode==''){
	          localStorage.setItem("shipping_postcode", shipping_postcode);
		}
		
		var url="https://smart-sure.com/shop/";
		window.location.replace(url);
	});
	
	//$("#billing_first_name").val("<?php echo WC()->session->get( 'billing_first_name' ) ?>");
	
//	$("#billing_last_name").val("<?php echo WC()->session->get( 'billing_last_name' ) ?>");
	
	//$("#billing_address_1").val("<?php echo WC()->session->get( 'billing_address_1' ) ?>");
	
	//$("#billing_city").val("<?php echo WC()->session->get( 'billing_city' )?>");
	
	//$("#billing_postcode").val("<?php echo WC()->session->get( 'billing_postcode' )?>");
	
	//$("#billing_dob").val("<?php echo WC()->session->get( 'billing_dob' )?>");
	
	//$("#billing_phone").val("<?php echo WC()->session->get( 'billing_phone' )?>");
	
	//$("#billing_email").val("<?php echo WC()->session->get( 'billing_email' ) ?>");
	
	//$("#billing_password").val("<?php echo WC()->session->get( 'billing_password' ) ?>");
	
	//$("#shipping_first_name").val("<?php echo WC()->session->get( 'shipping_first_name' ) ?>");
	
	//$("#shipping_last_name").val("<?php echo WC()->session->get( 'shipping_last_name' )?>");
	
	//$("#shipping_company").val("<?php echo WC()->session->get( 'shipping_company' ) ?>");
	
	//$("#shipping_address_1").val("<?php echo WC()->session->get( 'shipping_address_1' )?>");
	
	//$("#shipping_address_2").val("<?php echo WC()->session->get( 'shipping_address_2' ) ?>");
	
	//$("#shipping_city").val("<?php echo WC()->session->get( 'shipping_city' )?>");
	
	//$("#shipping_state").val("<?php echo WC()->session->get( 'shipping_state' )?>");
	
	$(this).checked = false;
	//$( "#place_order" ).prop( "disabled", true );
	//$( "#place_order" ).css('opacity','0.7');
	
	/*$("#place_order").click(function(event){
    event.preventDefault();
	var termsEmail=$(".termsEmail").val();
	var termsBilling=$(".termsBilling").val();
	if(termsEmail=="true" && termsBilling=="true"){
		$(".woocommerce-checkout").submit();
	}else{
		alert("You must agree with all terms and conditions");
	}
    });*/

	$(document).on('change', '#billing_email_policy', function() {
		 if($(this).prop('checked') == true) {
			 var email=$(".termsEmail").val(true);
		 }else{
			 var email=$(".termsEmail").val(false);
		 }
		 // termsAndConditionValidation(); 
	});
	
	$(document).on('change', '#billing_confirm_items', function() {
		 if($(this).prop('checked') == true) {
			 var billing=$(".termsBilling").val(true);
		 }else{
			 var billing=$(".termsBilling").val(false);
		 }
			//termsAndConditionValidation(); 
	});
	
	function termsAndConditionValidation(){
		 var billing=$(".termsBilling").val();
		 var email=$(".termsEmail").val();
		
		if(email=="true" && billing=="true"){
		$( "#place_order" ).prop( "disabled", false );
		$( "#place_order" ).css('opacity','1');
		}else{
		$( "#place_order" ).prop( "disabled", true );
		$( "#place_order" ).css('opacity','0.7');
		}
	}
	
	$("#place_order").hover(function() {
        $(this).css('cursor','pointer').attr('title', 'Please accept our terms and conditions policy.');
    }, function() {
        $(this).css('cursor','auto');
    });
	
	
	$(document).on('change', '#ship-to-different-address-checkbox', function() {
		 if($(this).prop('checked') == true) {
			
		$('.shipping_address').css('display','block');
		
		$('#shipping_first_name').prop('required',true);
		$('#shipping_last_name').prop('required',true);
		$('#shipping_address_1').prop('required',true);
		$('#shipping_city').prop('required',true);
		$('#shipping_postcode').prop('required',true);
		}else{
			
			$('.shipping_address').css('display','none');
			
			$('#shipping_first_name').removeAttr('required');
			$('#shipping_last_name').removeAttr('required');
			$('#shipping_address_1').removeAttr('required');
			$('#shipping_city').removeAttr('required');
			$('#shipping_postcode').removeAttr('required');
			
			
		}
		
	});
});
</script>
  <?php global $woocommerce;
  //  $items = $woocommerce->cart->get_cart();

       //foreach ( $woocommerce->cart->get_cart() as $cart_item ) {
		//$product = $cart_item['data'];
		//var_dump($product[title]);die;
		/*if ( has_term( 'membership', 'product_cat', $product->id ) ) {
		}*/
		
	   //}	

       ?>
      
</div>


			<div class="col-2">

	<table class="shop_table hide-responsive">
	<?php /*if($_POST['billing_cycle']==""){
		$_POST['billing_cycle']="Monthly";
	}*/?>
	<tbody>
							<tr class="cart_item">
						<td class="product-name"  style="background-color: rgb(43, 103, 11); color: #fff; text-align: center;border:0px"  width="45%">

							<div id="mMonthly" class="d"  <?php  if($_POST['billing_cycle']=="Yearly"){echo'style="display: none;"';}?>>
								<b>Monthly</b> <br><span class="price-box"><?php global $woocommerce; echo "£".number_format($woocommerce->cart->subtotal,'2');?></span><br><span style="font-size:11px;"> </span>
								<small style="font-size:9px;display:block">Including Online Discount</small>
							</div>


							<div id="mYearly" class="d"  <?php  if($_POST['billing_cycle']=="Monthly"){echo'style="display: none;"';}if (empty($_POST)){echo'style="display: none;"';}?>>
								    <b>Annualy</b> <br> <span class="price-box"><?php global $woocommerce; 
									$percentage = 12;
									$surcharge =  $woocommerce->cart->subtotal*12  ; 
									echo "£".number_format($surcharge,'2'); ?> </span>
								 	<br> <span style="font-size:11px;"> </span>
								 	<small style="font-size:9px;display:block">Including Online Discount</small>
							 </div>

						</td>
						<td class="moth-year-seperator" width="10%">	</td>
						<td class="product-total month-selection-td" width="45%">	
							<div id="inline_content"> 
								<input name="billing_cycle" id="billing_cycles" value="Monthly" class="select"  type="radio" <?php if($_POST['billing_cycle']=="Monthly"){echo'checked="checked"';}else {echo'checked="checked"';	}?> >Monthly
								<br> 
								<input name="billing_cycle" id="billing_cycles" value="Yearly" class="select" type="radio"  <?php if($_POST['billing_cycle']=="Yearly"){echo'checked="checked"';}?>>Annualy	
							</div>		
						</td>
					</tr>
						</tbody>

</table>
	<h3 id="order_review_heading"><?php _e( 'Your order', 'woocommerce' ); ?></h3>

	<?php do_action( 'woocommerce_checkout_before_order_review' ); ?>



	<div id="order_review" class="woocommerce-checkout-review-order">
		<?php do_action( 'woocommerce_checkout_order_review' ); ?>
	</div>

	<?php do_action( 'woocommerce_checkout_after_order_review' ); ?>

</form>

<?php do_action( 'woocommerce_after_checkout_form', $checkout ); ?>
