/*
 * Dragon Ecommerce Reserve WordPress Plugin
 * Copyright: Ming's IT Services Ltd.
 * Licensed under Ming's IT Services Ltd.
 * All rights reserved.
 * Disclaimer:  While Ming's IT Services Ltd. makes every effort to deliver high quality products, we do not 
 *				guarantee that our products are free from defects. Our software is provided “as is," and you use the 
 *				software at your own risk.
 *				We make no warranties as to performance, merchantability, fitness for a particular purpose, or any 
 *				other warranties whether expressed or implied.
 *				No oral or written communication from or information provided by Ming's IT Services Ltd. 
 *				shall create a warranty.
 *				Under no circumstances shall Ming's IT Services Ltd. be liable for direct, indirect, special, 
 *				incidental, or consequential damages resulting from the use, misuse, or inability to use this software, 
 *				even if Ming's IT Services Ltd. has been advised of the possibility of such damages.
 * */
function dragonecommercereservezyx987_confirmOrder() {
	jQuery("div.error div.alert-danger").html('').hide();
	jQuery("div.success div.alert-success").html('').hide();
	var dragonecommercereservezyx987_payment_amount = jQuery('form#dragonecommercereservezyx987_submitorderandpayment_form span#total').text();				  
	var dragonecommercereservezyx987_addOrderByBraintree_data = {dragonecommercereservezyx987_hidden:"Y"};
	var dragonecommercereservezyx987_addOrderByBraintree_data_key = 'dragonecommercereservezyx987_hidden_security_addOrderByBraintreeByAjax';				
	dragonecommercereservezyx987_addOrderByBraintree_data[dragonecommercereservezyx987_addOrderByBraintree_data_key] = dragonecommercereservezyx987_addOrderByBraintreeByAjax_nonce;
	dragonecommercereservezyx987_addOrderByBraintree_data.deviceDataFromTheClient = deviceDataFromTheClient;
	var shippingname = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #shippingaddress-name');
	var shippingemail = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #shippingaddress-email');
	var shippingphone = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #shippingaddress-phone');
	var shippingaddress1 = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #shippingaddress-address');
	var shippingaddress2 = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #shippingaddress-address2');
	var shippingcity = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #shippingaddress-city');					
	var shippingstate = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #shippingaddress-state');
	var shippingcountry = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #shippingaddress-country');
	var shippingzip = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #shippingaddress-zip');		
	var shippingstatename = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form input#shippingaddress-statename');
	var shippingcountryname = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form input#shippingaddress-countryname');
	var shippingid = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #shippingid');
	var handlingid = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #handlingid');
	
	var discountfee = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #discountfee');
	var couponfee = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #couponfee');
	
	
	var tax_id = document.querySelector('form#dragonecommercereservezyx987_submitorderandpayment_form #taxid');		
	var subtotal = jQuery('form#dragonecommercereservezyx987_submitorderandpayment_form span#subtotalOriginal').text();
	var discountidsArr = [];
	var discountids = '';
	for(var i=0; i < discountArr.length; i++) {			
		if(discountArr[i]["id"] !== null && discountArr[i]["id"] !== '' && discountArr[i]["id"] !== 0) {
			discountidsArr.push(discountArr[i]["id"]);
		}
	}
	discountids = discountidsArr.join("|");
	var couponidsArr = [];
	var couponids = '';
	for(var i=0; i < couponArr.length; i++) {			
		if(couponArr[i]["id"] !== null && couponArr[i]["id"] !== '' && couponArr[i]["id"] !== 0) {
			couponidsArr.push(couponArr[i]["id"]);
		}
	}
	couponids = couponidsArr.join("|");	
	var shippingfee = jQuery('form#dragonecommercereservezyx987_submitorderandpayment_form span#shipping').text();
	var handlingfee = jQuery('form#dragonecommercereservezyx987_submitorderandpayment_form span#handling').text();
	var tax = jQuery('form#dragonecommercereservezyx987_submitorderandpayment_form span#tax').text();
	var totalfee = jQuery('form#dragonecommercereservezyx987_submitorderandpayment_form span#totalOriginal').text();
	var shoppingcartitems2 = shoppingcartitems.slice();
	for(var i=0; i < shoppingcartitems2.length; i++) {	
        delete shoppingcartitems2[i]["imagefilenames"];
	}	
	var myshoppingcartitems = JSON.stringify(shoppingcartitems2);
	var shippingdescription = jQuery("form#dragonecommercereservezyx987_submitorderandpayment_form select#shipping_dropdown option:selected").text();
	var handlingdescription = jQuery("form#dragonecommercereservezyx987_submitorderandpayment_form select#handling_dropdown option:selected").text();
				
	dragonecommercereservezyx987_addOrderByBraintree_data.action = "dragonecommercereservezyx987_addOrderByBraintreeByAjax";
	dragonecommercereservezyx987_addOrderByBraintree_data.userid = b1n9j5t7R9P2;
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingname = shippingname ? shippingname.value : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingaddress1 = shippingaddress1 ? shippingaddress1.value : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingaddress2 = shippingaddress2 ? shippingaddress2.value : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingcity = shippingcity ? shippingcity.value : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingprovince = shippingstate ? shippingstate.value : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingcountry = shippingcountry ? shippingcountry.value : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingprovincename = shippingstatename ? shippingstatename.value : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingcountryname = shippingcountryname ? shippingcountryname.value : '';		
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingpostalcode = shippingzip ? shippingzip.value : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingphonenumber = shippingphone ? shippingphone.value : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingemail = shippingemail ? shippingemail.value : '';		
	dragonecommercereservezyx987_addOrderByBraintree_data.totalfee = totalfee;	
	dragonecommercereservezyx987_addOrderByBraintree_data.subtotal = subtotal ? subtotal : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.discountids = discountids ? discountids : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.couponids = couponids ? couponids : '';
	
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingfee = shippingfee ? shippingfee : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.handlingfee = handlingfee ? handlingfee : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.tax = tax ? tax : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shoppingcartitems  = myshoppingcartitems ? myshoppingcartitems  : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingdescription = shippingdescription ? shippingdescription : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.handlingdescription = handlingdescription ? handlingdescription : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingid = shippingid ? shippingid.value : '';
	dragonecommercereservezyx987_addOrderByBraintree_data.handlingid = handlingid ? handlingid.value: '';
	dragonecommercereservezyx987_addOrderByBraintree_data.taxid = tax_id ? tax_id.value : '';	
	dragonecommercereservezyx987_addOrderByBraintree_data.pluginurl = pluginurl;
	dragonecommercereservezyx987_addOrderByBraintree_data.shippingchargeenabled = (settings["ENABLE_SHIPPING_CHARGE"]["settingvalue"] === '1' ? '1' : '0');
	dragonecommercereservezyx987_addOrderByBraintree_data.handlingchargeenabled = (settings["ENABLE_HANDLING_CHARGE"]["settingvalue"] === '1' ? '1' : '0');
	
	var other_temp = b1n9j5t7R9P2;
	if(b1n9j5t7R9P1 === null || b1n9j5t7R9P1 === 0 || b1n9j5t7R9P1 < 0) {
		other_temp = 0;
	}
	if(b1n9j5t7R9P2 === null || b1n9j5t7R9P2 === 0 || b1n9j5t7R9P2 < 0) {
		other_temp = 0;
	}
	if(b1n9j5t7R9P1 !== b1n9j5t7R9P2) {
		other_temp = 0;
	}		
	dragonecommercereservezyx987_addOrderByBraintree_data.other = other_temp;
	jQuery('div#loading').show();
	jQuery.ajax({
		type: "POST",
		url: ajaxurl,
		data: dragonecommercereservezyx987_addOrderByBraintree_data
	}).done(function (mydata) {
		alert(mydata);
		jQuery('div#loading').hide();
		if(mydata === "Success. Please check your email for reservation details.") {
			shoppingcartitems = [];	
			dragonecommercereservezyx987_refreshLandingpageLeftTabMenu();
			dragonecommercereservezyx987_showCartIconQuantity();
			return window.location.href = window.location.href;
		}
	}).fail(function( jqXHR, textStatus ) {
		alert("Add user reservation to db, Request failed: " + textStatus);
		jQuery('div#loading').hide();
	});	
}

var form = jQuery('form#dragonecommercereservezyx987_submitorderandpayment_form');
form.submit(function(event) {
    event.preventDefault();				  
    dragonecommercereservezyx987_validateOrderFields();
    var formIsInvalid = false;			  
    var myemail = jQuery.trim(jQuery('input#shippingaddress-email').val());			  
    if(!dragonecommercereservezyx987_validateEmail(myemail)){
	    formIsInvalid = true;
    }
    if (formIsInvalid) {
	    return;
    }			  
    dragonecommercereservezyx987_confirmOrder();
});
			
jQuery(document).ready(function(){
	jQuery("div.error div.alert-danger").html('').hide();
	jQuery("div.success div.alert-success").html('').hide();
	
});