/**
* 2007-2020 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@buy-addons.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author    Buy-addons    <contact@buy-addons.com>
* @copyright 2007-2020 Buy-addons
* @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/
"use strict";
function addproduct_newquote_convertToNumber(ccurrency) {
	if (ccurrency === '' || ccurrency === null || ccurrency === undefined) {
        return 0.00;
    }
	return parseFloat(ccurrency.replace(/[^0-9\.]+/g,""));
}
function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
function priceformatNumber(price) {
	if (isNaN(price)) {
		price = 0;
	}
    price = parseFloat(price);
    var p = price.toFixed(3).split(".");
	return  p[0].split("").reverse().reduce(function(acc, price, i, orig) {
		return  price=="-" ? acc : price + (i && !(i % 3) ? "," : "") + acc;
	}, "") + "." + p[1];   
}
function convertCurrencytoNumber(ccurrency) {
	if (isNaN(ccurrency) || ccurrency == '' || ccurrency === null || ccurrency === undefined) {
		return 0.00;
	}
	console.log(ccurrency);
    return parseFloat(ccurrency.toString().replace(/[^0-9\.]+/g,""));
}
function validateEmail($email) {
  var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  return emailReg.test( $email );
}
$(document).ready(function(){
    $("#td_add_new_pro_newquote").click(function(){
            window.parent.$("#quote_err").css("display", "none");
            var id_product_e = document.getElementById('td_id_product').value;
            var combi_product_e = document.getElementById('id_pro_attribute').value;
            if (document.getElementById('qty_product') != null) {
                var qty_product_e = (parseInt)(document.getElementById('qty_product').value);
            } else {
                var qty_product_e = "1";
            }
            if (document.getElementById('td_check_qty_add_pro') != null) {
                var td_check_qty_add_pro = (parseInt)(document.getElementById('td_check_qty_add_pro').value);
            }
                window.parent.$(".alert_max_qty").css("display", "none");
                window.parent.$(".notice_max_qty").css("display", "none");
                var total_priceee = document.getElementById('td_totalprice_newquote').value;
				total_priceee = convertCurrencytoNumber(total_priceee);
                var id_customer = document.getElementById('td_id_customerr').value;
                var td_pri_pro_havetax_max_fixed = jQuery('.td_pri_pro_havetax_max_fixed').val();
				td_pri_pro_havetax_max_fixed = convertCurrencytoNumber(td_pri_pro_havetax_max_fixed);
                var td_pri_pro_havetax_min_fixed = jQuery('.td_pri_pro_havetax_min_fixed').val();
				td_pri_pro_havetax_min_fixed = convertCurrencytoNumber(td_pri_pro_havetax_min_fixed);
                jQuery.ajax({
                    type: 'POST',//phương thức gửi
                    jscrossDomain: window.location.hostname,
                    crossDomain: true,
                    dataType: "json",
                    url: '../index.php?controller=addproductnewquote&fc=module&module=askforaquotemodul', // lấy link của controller mới
                    data: '&td_pri_pro_havetax_max_fixed=' + td_pri_pro_havetax_max_fixed + '&td_pri_pro_havetax_min_fixed=' + td_pri_pro_havetax_min_fixed + '&jsshopid=' + jsshopid + '&batokencus=' + batokencus + '&id_product_e=' + id_product_e + '&combi_product_e=' + combi_product_e + '&qty_product_e=' + qty_product_e + '&total_priceee=' + total_priceee + '&id_customer=' + id_customer + '&baan=0' + '&quote_token=' + token_askaquotemodule + '&ajax=1',  // lấy dữ liệu từ id product ở trên định nghĩa
                    beforeSend: function() {
						$('.loadicon_addp').css('display', 'inline-block');
					},
					success: function(jsonData,textStatus,jqXHR)
                    {
						$('.loadicon_addp').css('display', 'none');
                        newtable(jsonData);
                    }        
                });       
    });
	$("#td_add_new_pro_newquote_an").click(function(){
            var id_product_e = document.getElementById('td_id_product').value;
            var combi_product_e = document.getElementById('id_pro_attribute').value;
            var td_id_quote = $('.td_id_quote').val();
            if (document.getElementById('qty_product') != null) {
                var qty_product_e = (parseInt)(document.getElementById('qty_product').value);
            } else {
                var qty_product_e = "1";
            }
            if (document.getElementById('td_check_qty_add_pro') != null) {
                var td_check_qty_add_pro = (parseInt)(document.getElementById('td_check_qty_add_pro').value);
            }
			window.parent.$(".alert_max_qty").css("display", "none");
			window.parent.$(".notice_max_qty").css("display", "none");
			var total_priceee = document.getElementById('td_totalprice_newquote').value;
			total_priceee = convertCurrencytoNumber(total_priceee);
			var id_customer = jQuery('.td_id_customer').val();
			var td_pri_pro_havetax_max_fixed = jQuery('.td_pri_pro_havetax_max_fixed').val();
			td_pri_pro_havetax_max_fixed = convertCurrencytoNumber(td_pri_pro_havetax_max_fixed);
			var td_pri_pro_havetax_min_fixed = jQuery('.td_pri_pro_havetax_min_fixed').val();
			td_pri_pro_havetax_min_fixed = convertCurrencytoNumber(td_pri_pro_havetax_min_fixed);
			jQuery.ajax({
				type: 'POST',//phương thức gửi
				jscrossDomain: window.location.hostname,
				crossDomain: true,
				dataType: "json",
				url: '../index.php?controller=addproductnewquote&fc=module&module=askforaquotemodul',
				data: '&td_pri_pro_havetax_max_fixed=' + td_pri_pro_havetax_max_fixed + '&td_pri_pro_havetax_min_fixed=' + td_pri_pro_havetax_min_fixed + '&jsshopid=' + jsshopid + '&batokencus=' + batokencus + '&id_product_e=' + id_product_e + '&combi_product_e=' + combi_product_e + '&qty_product_e=' + qty_product_e + '&total_priceee=' + total_priceee + '&id_customer=' + id_customer + '&td_id_quote=' + td_id_quote + '&baan=1' + '&quote_token=' + token_askaquotemodule + '&ajax=1',
				beforeSend: function() {
					$('.loadicon_addp').css('display', 'inline-block');
				},
				success: function(jsonData,textStatus,jqXHR)
				{
					$('.loadicon_addp').css('display', 'none');
					newtable(jsonData);
				}                       
			});  
    });
	baquote_sortProductsQuoteList(); // since 1.0.42
});
function total_price(){
	var total_price = 0;
	$("tr.td_check_exist_pro").each(function(){
		var price_pro = $(this).find(".price_pro").val();
		price_pro = addproduct_newquote_convertToNumber(price_pro);
		var qty_pro = addproduct_newquote_convertToNumber($(this).find(".qty_pro").val());
		var total_price_pro = price_pro * qty_pro;
		total_price += total_price_pro;
	});
	var td_shipping_price = jQuery('.td_shipping_price').val();
	var td_tax_price = jQuery('.td_tax_price').val();
	td_shipping_price = addproduct_newquote_convertToNumber(td_shipping_price);
	td_tax_price = addproduct_newquote_convertToNumber(td_tax_price);
	total_price = total_price + td_shipping_price + td_tax_price;
	return total_price;
}
function baquote_keyPressPrice(me, event) {
	return (event.charCode >= 48 && event.charCode <= 57) || event.charCode == 46;
}
function newtable(jsonData){
	if ($('tr').hasClass(jsonData.id_product +'-'+ jsonData.id_attrbute_pro)) {
		var qty_pro = parseInt(jQuery('.edit_quantity_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro).val());
		var qty_new = qty_pro + parseInt(jsonData.qty_product);
		var quantity_max = parseInt(jQuery('.quantity_max_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro).val());
		var edit_price = jQuery('.edit_price_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro).val();
		if(edit_price.length === 0){
			$('.edit_price_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro).val("0");
			edit_price = '0';
		}
		edit_price = addproduct_newquote_convertToNumber(edit_price);
		
		var new_total_price_pro = edit_price * qty_new;
		var td_pri_pro_havetax_min_fixed = parseFloat(jQuery('.td_pri_pro_havetax_min_fixed_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'').val());
		var td_pri_pro_havetax_max_fixed = parseFloat(jQuery('.td_pri_pro_havetax_max_fixed_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'').val());
		var html = '';
		html += '<span>';
		html += '<input type="hidden" class="quantity_max_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'" value="'+ jsonData.quantity_max +'">';
		html += '<input type="text" class="form-control qty_pro edit_quantity edit_quantity_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +' " style="width:70%;" data-special-product="" name="baproduct['+ jsonData.id_product +'_'+ jsonData.id_attrbute_pro +'][quantity]" value = "'+ qty_new +'" onkeypress="return event.charCode >= 48 && event.charCode <= 57" onpaste="return false;" onkeyup="baquote_EditQty('+jsonData.id_product+', '+jsonData.id_attrbute_pro+', this)">';
		html += '<input type="hidden" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][qty_product]" value="' + qty_new + '">';
		html += '<input type = "hidden" value="' + td_pri_pro_havetax_max_fixed + '" class="tax_max td_pri_pro_havetax_max_fixed_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'">';
		html += '<input type = "hidden" value="' + td_pri_pro_havetax_min_fixed + '" class="tax_min td_pri_pro_havetax_min_fixed_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'">';
		html += '</span>';
		window.parent.$('.qty_pro_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'').html(html);
		var new_total_price_all = total_price();
		var price_have_shippingcost = jQuery('.td_price_have_shippingcost').val();
		var price_have_tax = jQuery('.td_price_have_tax').val();
		var html_new_totalpri = '';
		html_new_totalpri += '<input type = "hidden" value="' + new_total_price_all + '" class="td_fixed_total_price">';
		html_new_totalpri += '<input name="baproduct[total_price_quote]" type = "hidden" value="'+ new_total_price_all +'" class="batotalproducts">';
		html_new_totalpri += '<input name="infor_quote[total_price_all]" type = "hidden" value="'+ new_total_price_all +'" id="td_totalprice_newquote" class="td_total_pri_newquote"><span>'+ priceformatNumber(new_total_price_all) + jsonData.sign_def +'</span>';
		html_new_totalpri += '<input type = "hidden" value="' + price_have_tax + '" class="td_price_have_tax">';
		html_new_totalpri += '<input type = "hidden" value="' + price_have_shippingcost + '" class="td_price_have_shippingcost">';
		window.parent.$(".total_price_quote_dbboo").html(html_new_totalpri);
		var html_new_totalpri_pro = '';
		html_new_totalpri_pro += '<input type="hidden" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][total_price_product]" value="' + new_total_price_pro + '">';
		html_new_totalpri_pro += '<input type="text" class="form-control edit_total_price td_total_pri_pro_newquote_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'" style="width:85%;" data-count-dbboo="dbboo" name="baproduct['+ jsonData.id_product +'_'+ jsonData.id_attrbute_pro +'][total_price_product]" data-special-product="" value = "'+ new_total_price_pro +'" onkeypress="return baquote_keyPressPrice(this, event);" onkeyup="baquote_EditTotal('+jsonData.id_product+', '+jsonData.id_attrbute_pro+', this)">';
		html_new_totalpri_pro += '</input>';
		window.parent.$('.td_total_pri_product_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'').html(html_new_totalpri_pro);
	} else {
		var html_new = '';
		html_new += '<tr height="52" class="td_check_exist_pro '+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'"><input id="td_id_customerr" type="hidden" name="infor_quote[id_customer]" value="' + jsonData.id_customer + '">';
		html_new += '<input type="hidden" class="row_product" value="'+ jsonData.id_product +'" data-id-product="'+ jsonData.id_product +'" data-id-combination="'+ jsonData.id_attrbute_pro +'">';
		html_new += '<td><input name="baproduct['+ jsonData.id_product +'_'+ jsonData.id_attrbute_pro +'][position]" class="quote-position form-control" value = "1"/></td>';
		html_new += '<td><input type="hidden" class="rate_tax_min" value="'+jsonData.rate_tax_min+'"><input type="hidden" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][link_images]" value="' + jsonData.link_images + '"><img src="' + jsonData.link_images + '" style="width: 60px;border: 1px solid #cacaca;border-radius: 6px;" alt="" class="imgm img-thumbnail" style = "width: 60px;"></td>';
		html_new += '<td><input type="hidden" class="rate_tax_max" value="'+jsonData.rate_tax_max+'"><a href="'+ jsonData.link_pro +'" target="_blank">';
		html_new += '<span><input type="hidden" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][name_product]" value="'+ jsonData.name_product['name'] +'">'+ jsonData.name_product['name'] +'</span><input type="hidden" id="td_tax_pro" class="td_tax_pro_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'" value="' + jsonData.tax_of_pro + '">';
		html_new += '</a><input type="hidden" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][id_combi_pro]" value="'+ jsonData.id_attrbute_pro +'"><input type="hidden" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][id_product]" value="'+ jsonData.id_product +'">';
		if (jsonData.cache_is_pack == 1) {
			var pack_text = '<ul><li>';
			pack_text += jsonData.combination.replace(new RegExp("\n", 'g'), "</li><li>");
			pack_text += '</li></ul>';
			html_new += '<div><input type="hidden" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][combination]" value="' + jsonData.combination + '">'+ pack_text +'</div>';
		} else {
			html_new += '<div><input type="hidden" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][combination]" value="' + jsonData.combination + '">'+ jsonData.combination +'</div>';
		}
		html_new += '</td><td>';
		html_new += '<span><input type="hidden" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][reference]" value="' + jsonData.sku_product['reference'] + '">'+ jsonData.sku_product['reference'] +'</span>';
		html_new += '</td>';
		html_new += '<td style="width:15%;" class = "">';
		html_new += '<span style="float: left;padding-top: 5px;" class="signstyle">'+ jsonData.sign_def +'</span>';
		html_new += '<span style="display: table-cell" class="td_pri_pro_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'"><input type="hidden" class="pri_pro_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][pri_pro]" value="' + jsonData.pri_pro + '"><input type="text" class="form-control price_pro edit_price edit_price_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +' " data-special-product="" name="baproduct['+ jsonData.id_product +'_'+ jsonData.id_attrbute_pro +'][price]" autocomplete="off" style="width:85%;" value = "'+ priceformatNumber(jsonData.pri_pro) +'" onkeypress="return baquote_keyPressPrice(this, event);" onkeyup="baquote_EditUnitPrice('+jsonData.id_product+', '+jsonData.id_attrbute_pro+', this)"></input>';
		html_new += '</td><td style="width:9%;" class="qty_pro_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'">';
		html_new += '<span><input type = "hidden" value="' + jsonData.pri_pro_havetax_max + '" class="tax_max td_pri_pro_havetax_max_fixed_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'"><input type = "hidden" value="' + jsonData.pri_pro_havetax_min + '" class="tax_min td_pri_pro_havetax_min_fixed_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'"><input type="hidden" class="quantity_max_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'" value="'+ jsonData.quantity_max +'"><input type="text" class="form-control qty_pro edit_quantity edit_quantity_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +' " style="width:70%;" data-special-product="" name="baproduct['+ jsonData.id_product +'_'+ jsonData.id_attrbute_pro +'][quantity]" value = "'+ jsonData.qty_product +'" onkeypress="return event.charCode >= 48 && event.charCode <= 57" onpaste="return false;" onkeyup="baquote_EditQty('+jsonData.id_product+', '+jsonData.id_attrbute_pro+', this)"><input type="hidden" class="qty_proooo_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][qty_product]" value="' + jsonData.qty_product + '"></span>';
		html_new += '</td><td style="width:15%;"><span style="float: left;padding-top: 5px;" class="signstyle">';
		html_new += ''+ jsonData.sign_def +'</span><span style="display: table-cell" class="td_total_pri_product_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'">';
		html_new += '<input type="text" name="baproduct['+ jsonData.id_product +'_'+ jsonData.id_attrbute_pro +'][total_price_product]" class="form-control edit_total_price td_total_pri_pro_newquote_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'" style="width:85%;" data-count-dbboo="dbboo" name="" data-special-product="" value = "'+ priceformatNumber(jsonData.total_price) +'" onkeypress="return baquote_keyPressPrice(this, event);" onpaste="return false;" onkeyup="baquote_EditTotal('+jsonData.id_product+', '+jsonData.id_attrbute_pro+', this)"><input type="hidden" name="infor_quote['+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'][total_price_product]" value="' + jsonData.total_price + '"></span>';
		html_new += '</td> <td colspan="2">&nbsp;</td><td class="text-right"><div class="btn-group">';
		html_new += '<a value="" onclick="del_product_view('+parseInt(jsonData.id_attrbute_pro)+','+jsonData.id_quote+','+jsonData.id_product+',this)" class="btn btn-default del_product_view del_pro_newquote_'+ jsonData.id_product +'-'+ jsonData.id_attrbute_pro +'">';
		html_new += '<i class="icon-trash"></i> Delete</a></div></td>';
		html_new += '</tr>';
		window.parent.$(".product_table_newquote").append(html_new);
		
		var html_new_totalpri = '';
		html_new_totalpri += '<input name="baproduct[total_price_quote]" type = "hidden" value="'+ jsonData.total_price_new +'" class="batotalproducts">';
		html_new_totalpri += '<input name="infor_quote[total_price_all]" type = "hidden" value="'+ jsonData.total_price_new +'" id="td_totalprice_newquote" class="td_total_pri_newquote"><span>'+ priceformatNumber(jsonData.total_price_new) + jsonData.sign_def +'</span>';
		html_new_totalpri += '<input type = "hidden" value="' + jsonData.total_price_new + '" class="td_fixed_total_price">';
		html_new_totalpri += '<input type = "hidden" value="' + jsonData.pri_pro_havetax_max + '" class="td_pri_pro_havetax_max">';
		html_new_totalpri += '<input type = "hidden" value="' + jsonData.pri_pro_havetax_min + '" class="td_pri_pro_havetax_min">';
		window.parent.$(".total_price_quote_dbboo").html(html_new_totalpri);
	}
	baquote_sortProductsQuoteList(); // since 1.0.42
}
function baquote_EditUnitPrice(id_product, id_attrbute_pro, ob) {
	var edit_price = jQuery('.edit_price_'+ id_product +'-'+ id_attrbute_pro).val();
	edit_price = addproduct_newquote_convertToNumber(edit_price);
	//////////
	if(edit_price.length === 0){
		$('.edit_price_'+ id_product +'-'+ id_attrbute_pro).val("0");
		edit_price = '0';
	}
	var edit_quantity = jQuery('.edit_quantity_'+ id_product +'-'+ id_attrbute_pro).val();
	var old_total_price_pro = jQuery('.td_total_pri_pro_newquote_'+ id_product +'-'+ id_attrbute_pro).val();
	var old_total_price_all = jQuery('.td_total_pri_newquote').val();
	var edit_quantity = addproduct_newquote_convertToNumber(edit_quantity);
	var old_total_price_pro = addproduct_newquote_convertToNumber(old_total_price_pro);
	var old_total_price_all = addproduct_newquote_convertToNumber(old_total_price_all);
	var new_total_price_pro = edit_price * edit_quantity;
	var mid_price_pro = old_total_price_all - old_total_price_pro;
	var new_total_price_all = total_price();
	var price_have_shippingcost = jQuery('.td_price_have_shippingcost').val();
	var price_have_tax = jQuery('.td_price_have_tax').val();
	$('.pri_pro_'+ id_product +'-'+ id_attrbute_pro).val(edit_price);
	var html_new_pri_pro = '';
	html_new_pri_pro += '<input type="hidden" name="infor_quote['+ id_product +'-'+ id_attrbute_pro +'][total_price_product]" value="' + edit_price + '">';
	html_new_pri_pro += '<input type="text" class="form-control edit_total_price td_total_pri_pro_newquote_'+ id_product +'-'+ id_attrbute_pro +'" style="width:85%;" data-count-dbboo="dbboo" name="" data-special-product="" value = "'+ edit_price +'" onkeypress="return event.charCode >= 48 && event.charCode <= 57 || event.charCode == 46" onpaste="return false;">';
	html_new_pri_pro += '</input>';
	window.parent.$('.td_pri_pro'+ id_product +'-'+ id_attrbute_pro +'').html(html_new_pri_pro);
	
	window.parent.$('.td_total_pri_pro_newquote_'+ id_product +'-'+ id_attrbute_pro +'').val(new_total_price_pro);
	window.parent.$('input[name="infor_quote['+ id_product +'-'+ id_attrbute_pro +'][total_price_product]"]').val(new_total_price_pro);
	///// tong tien 
	var html_new_totalpri = '';
	html_new_totalpri += '<input type = "hidden" value="' + new_total_price_all + '" class="td_fixed_total_price">';
	html_new_totalpri += '<input name="baproduct[total_price_quote]" type = "hidden" value="'+ new_total_price_all +'" class="batotalproducts">';
	html_new_totalpri += '<input type = "hidden" name="infor_quote[total_price_all]" value="'+ new_total_price_all +'" id="td_totalprice_newquote" class="td_total_pri_newquote"><span>'+ priceformatNumber(new_total_price_all) + sign_current_quote +'</span>';
	html_new_totalpri += '<input type = "hidden" value="' + price_have_tax + '" class="td_price_have_tax">';
	html_new_totalpri += '<input type = "hidden" value="' + price_have_shippingcost + '" class="td_price_have_shippingcost">';
	window.parent.$(".total_price_quote_dbboo").html(html_new_totalpri);
}
function baquote_EditTotal(id_product, id_attrbute_pro, ob) {
	var edit_price = jQuery('.edit_price_'+ id_product +'-'+ id_attrbute_pro).val();
	edit_price = addproduct_newquote_convertToNumber(edit_price);
	var edit_quantity = jQuery('.edit_quantity_'+ id_product +'-'+ id_attrbute_pro).val();
	var total_pri_pro_input = jQuery('.td_total_pri_pro_newquote_'+ id_product +'-'+ id_attrbute_pro).val();
	if(total_pri_pro_input.length === 0){
		$('.td_total_pri_pro_newquote_'+ id_product +'-'+ id_attrbute_pro).val("0.00");
		var total_pri_pro_input = '0';
	}
	var edit_quantity = edit_quantity.replace(",", "");
	var total_pri_pro_input = total_pri_pro_input.replace(",", "");
	var old_total_pri_pro = edit_price * edit_quantity;
	var old_total_price_all = jQuery('.td_total_pri_newquote').val();
	var mid_price_pro = old_total_price_all - old_total_pri_pro;
	var new_edit_price = total_pri_pro_input / edit_quantity;
	jQuery('.edit_price_'+ id_product +'-'+ id_attrbute_pro).val(new_edit_price);
	var price_have_shippingcost = jQuery('.td_price_have_shippingcost').val();
	var price_have_tax = jQuery('.td_price_have_tax').val();
	var html_new_pri_pro = '';
	html_new_pri_pro += '<input type="hidden" name="infor_quote['+ id_product +'-'+ id_attrbute_pro +'][total_price_product]" value="' + new_edit_price + '">';
	html_new_pri_pro += '<input type="text" class="form-control edit_total_price td_total_pri_pro_newquote_'+ id_product +'-'+ id_attrbute_pro +'" style="width:85%;" data-count-dbboo="dbboo" name="" data-special-product="" value = "'+ priceformatNumber(new_edit_price) +'" onkeypress="return baquote_keyPressPrice(this, event);" onpaste="return false;">';
	html_new_pri_pro += '</input>';
	window.parent.$('.td_pri_pro'+ id_product +'-'+ id_attrbute_pro +'').html(html_new_pri_pro);
	///// tong tien 
	var new_total_price_all = total_price();
	
	var html_new_totalpri = '';
	html_new_totalpri += '<input type = "hidden" value="' + price_have_tax + '" class="td_price_have_tax"><input type = "hidden" value="' + price_have_shippingcost + '" class="td_price_have_shippingcost">';
	html_new_totalpri += '<input name="baproduct[total_price_quote]" type = "hidden" value="'+ new_total_price_all +'" class="batotalproducts">';
	html_new_totalpri += '<input type = "hidden" value="' + new_total_price_all + '" class="td_fixed_total_price"><input name="infor_quote[total_price_all]" type = "hidden" value="'+ new_total_price_all +'" id="td_totalprice_newquote" class="td_total_pri_newquote"><span>'+ priceformatNumber(new_total_price_all) + sign_current_quote +'</span>';
	
	window.parent.$(".total_price_quote_dbboo").html(html_new_totalpri);
}
function baquote_EditQty(id_product, id_attrbute_pro, ob) {
	// jsonData
	var quantity_max = parseInt(jQuery('.quantity_max_'+ id_product +'-'+ id_attrbute_pro).val());
	var edit_quantity = jQuery('.edit_quantity_'+ id_product +'-'+ id_attrbute_pro).val();
	edit_quantity = addproduct_newquote_convertToNumber(edit_quantity);
	if(edit_quantity.length === 0){
		$('.edit_quantity_'+ id_product +'-'+ id_attrbute_pro).val("1");
		var edit_quantity = '1';
	}
	var edit_quantity = parseInt(edit_quantity);
	var edit_price = jQuery('.edit_price_'+ id_product +'-'+ id_attrbute_pro).val();
	edit_price = addproduct_newquote_convertToNumber(edit_price);
	if(edit_quantity == 0){
		$('.edit_quantity_'+ id_product +'-'+ id_attrbute_pro).val("1");
		var edit_quantity = '1';
	}
	var old_total_price_pro = jQuery('.td_total_pri_pro_newquote_'+ id_product +'-'+ id_attrbute_pro).val();
	var old_total_price_all = jQuery('.td_total_pri_newquote').val();
	var mid_price_pro = old_total_price_all - old_total_price_pro;
	var new_total_price_pro = edit_price * edit_quantity;
	var new_total_price_all = total_price();

	var price_have_shippingcost = jQuery('.td_price_have_shippingcost').val();
	var price_have_tax = jQuery('.td_price_have_tax').val();
	var price_have_tax_new = jQuery('.td_price_have_tax').val();
	$('.qty_proooo_'+ id_product +'-'+ id_attrbute_pro).val(edit_quantity);
	window.parent.$('.td_total_pri_pro_newquote_'+ id_product +'-'+ id_attrbute_pro +'').val(new_total_price_pro);
	window.parent.$('input[name="infor_quote['+ id_product +'-'+ id_attrbute_pro +'][total_price_product]"]').val(new_total_price_pro);
	///// tong tien 
	var html_new_totalpri = '';
	html_new_totalpri += '<input name="baproduct[total_price_quote]" type = "hidden" value="'+ new_total_price_all +'" class="batotalproducts">';
	html_new_totalpri += '<input type = "hidden" value="' + new_total_price_all + '" class="td_fixed_total_price"><input type = "hidden" name="infor_quote[total_price_all]" value="'+ new_total_price_all +'" id="td_totalprice_newquote" class="td_total_pri_newquote"><span>'+ priceformatNumber(new_total_price_all) + sign_current_quote +'</span>';
	html_new_totalpri += '<input type = "hidden" value="' + price_have_tax + '" class="td_price_have_tax"><input type = "hidden" value="' + price_have_shippingcost + '" class="td_price_have_shippingcost">';   
	window.parent.$(".total_price_quote_dbboo").html(html_new_totalpri);
}
function baquote_EditShippingCost(ob) {
	var shipping_cost = jQuery('.td_shipping_price').val();
	var tax_cost = jQuery('.td_tax_price').val();
	shipping_cost =  addproduct_newquote_convertToNumber(shipping_cost);
	// calc Shipping Tax rate
	var shipping_cost_tax_rate = parseFloat(jQuery('.shipping_cost_tax_plain').attr('data-tax-rate'));
	var shipping_cost_tax = shipping_cost_tax_rate * shipping_cost;
	jQuery('.shipping_cost_tax_plain').val(shipping_cost_tax);
	
	tax_cost =  addproduct_newquote_convertToNumber(tax_cost);
	var new_total_price_all = total_price();
	var price_have_shippingcost = new_total_price_all;
	var html_new_totalpri = '';
	html_new_totalpri += '<input name="baproduct[total_price_quote]" type = "hidden" value="'+ new_total_price_all +'" class="batotalproducts">';
	html_new_totalpri += '<input type = "hidden" value="' + new_total_price_all + '" class="td_fixed_total_price"><input name="infor_quote[total_price_all]" type = "hidden" value="'+ new_total_price_all +'" id="td_totalprice_newquote" class="td_total_pri_newquote"><span>'+ priceformatNumber(new_total_price_all) + sign_current_quote +'</span>';
	html_new_totalpri += '<input type = "hidden" value="' + shipping_cost + '" name="infor_quote[shipping_cost]" class="td_price_have_shippingcost"><input type = "hidden" value="' + tax_cost + '" name="infor_quote[tax_cost]" class="td_price_have_tax">';
	window.parent.$(".total_price_quote_dbboo").html(html_new_totalpri);
}
function baquote_EditTaxCost(ob) {
	var tax_cost = jQuery('.td_tax_price').val();
	var shipping_cost = jQuery('.td_shipping_price').val();
	tax_cost = addproduct_newquote_convertToNumber(tax_cost);
	var new_total_price_all = total_price();
	var html_new_totalpri = '';
	html_new_totalpri += '<input name="baproduct[total_price_quote]" type = "hidden" value="'+ new_total_price_all +'" class="batotalproducts">';
	html_new_totalpri += '<input type = "hidden" value="' + new_total_price_all + '" class="td_fixed_total_price"><input name="infor_quote[total_price_all]" type = "hidden" value="'+ new_total_price_all +'" id="td_totalprice_newquote" class="td_total_pri_newquote"><span>'+ priceformatNumber(new_total_price_all) + sign_current_quote +'</span>';
	html_new_totalpri += '<input type = "hidden" value="' + tax_cost + '" name="infor_quote[tax_cost]" class="td_price_have_tax"><input type = "hidden" value="' + shipping_cost + '" name="infor_quote[shipping_cost]" class="td_price_have_shippingcost">';
	window.parent.$(".total_price_quote_dbboo").html(html_new_totalpri);
}
function baquote_displayEstimateShippingCost(data){
		var html_new_infor = '';
		if(data.free_shipping == 1){
			html_new_infor += '<p class="help-block">Free shipping</p>';
		} else {
			html_new_infor += '<p class="help-block">Estimate ' + data.shipping_cost_min + ' - ' + data.shipping_cost_max + '</p>';
		}
		window.parent.$(".infor_shipping").html(html_new_infor);
}
function baquote_calcShippingCost() {
	var shipping_option;
	var id_quote = $('.td_id_quote').val();
	shipping_option = $('input[name=radio_shipping]:checked').val();
	if(shipping_option == '' || shipping_option == 0 || shipping_option == undefined) {
		window.parent.$(".ba_newsletter_popup_background").css("display", "none");
		window.parent.$(".td_shipping_popup ").css("display", "none");
		return true;
	}
	var products = baquote_getProducts2();
	var id_customer = $('input.td_id_customer').val();
	$(".td_choose_shipping_loading").show();
	jQuery.ajax({
		type: 'POST',//phương thức gửi
		jscrossDomain: window.location.hostname,
		crossDomain: true,
		dataType: "json",
		url: '../index.php?controller=shippingcost&fc=module&module=askforaquotemodul', // lấy link của controller mới
		data: 'shipping_option=' + shipping_option
			+ '&id_currency=' + td_id_currency 
			+ '&id_quote=' + id_quote 
			+ '&quote_token=' + token_askaquotemodule
			+ '&jsshopid=' + jsshopid
			+ '&id_customer=' + id_customer
			+ '&products=' + products
			+ '&ajax=1',
		success: function(jsonData,textStatus,jqXHR)
		{
			$(".td_choose_shipping_loading").hide();
			baquote_displayEstimateShippingCost(jsonData);
			// since 1.0.39+
			if (typeof(jsonData.shipping_cost_excl_plain) != "undefined") {
				jQuery('.td_shipping_price').val(jsonData.shipping_cost_excl_plain);
				var shipping_cost_tax_rate = parseFloat(jsonData.shipping_cost_tax_rate);
				jQuery('.shipping_cost_tax_plain').val(jsonData.shipping_cost_tax_plain).attr('data-tax-rate', shipping_cost_tax_rate);
				jQuery('.td_tax_price').val(jsonData.total_tax);
				baquote_EditTaxCost();
			}
			baquote_EditShippingCost();
			window.parent.$(".ba_newsletter_popup_background").css("display", "none");
			window.parent.$(".td_shipping_popup").css("display", "none");
		}
		
	});
}
function baquote_getProducts2() {
	var row_product = '';
	var edit_price_format, edit_total_price_format;
	$('#orderProducts tbody tr').each(function(){
		row_product += '|' + $(this).find('.row_product').attr('data-id-product') + '-' + $(this).find('.row_product').attr('data-id-combination');
		edit_price_format = $(this).find('.edit_price').val();
		edit_price_format = addproduct_newquote_convertToNumber(edit_price_format);
		row_product += '-' + edit_price_format;
		row_product += '-' + $(this).find('.edit_quantity').val();
		edit_total_price_format = $(this).find('.edit_total_price').val();
		edit_total_price_format = addproduct_newquote_convertToNumber(edit_total_price_format);
		row_product += '-' + edit_total_price_format;
	});
	return row_product;
}
function baquote_calcTaxCost() {
	var products = baquote_getProducts2();
	var pri_pro_havetax_min = 0;
	var pri_pro_havetax_max = 0;
	var total_tax_cost = 0;
	var id_quote = $('.td_id_quote').val();
	var id_customer = $('input.td_id_customer').val();
	var shipping_option = $('input[name=radio_shipping]:checked').val();
	if(shipping_option == '' || shipping_option == 0 || shipping_option == undefined) {
		shipping_option = 0;
	}
	jQuery(".estimate-block .loadicon_tax").show();
	jQuery.ajax({
	type: 'POST',//phương thức gửi
	jscrossDomain: window.location.hostname,
	crossDomain: true,
	dataType: "json",
	url: '../index.php?controller=taxcost&fc=module&module=askforaquotemodul', // lấy link của controller mới
		data: 'products=' + products
			+ '&shipping_option=' + shipping_option 
			+ '&id_currency=' + td_id_currency 
			+ '&id_quote=' + id_quote 
			+ '&jsshopid=' + jsshopid 
			+ '&id_customer=' + id_customer 
			+ '&quote_token=' + token_askaquotemodule 
			+ '&ajax=1',
		success: function(jsonData,textStatus,jqXHR)
		{
			pri_pro_havetax_min = jsonData.total_tax_cost_min;
			pri_pro_havetax_max = jsonData.total_tax_cost_max;
			total_tax_cost = jsonData.total_tax_cost_max_plain;
			var html_new_infor = '';
			html_new_infor += '<p class="help-block">Estimate Products Tax '+ pri_pro_havetax_min + ' - '+ pri_pro_havetax_max +'</p>';
			// since 1.0.39+
			var shipping_cost_tax = parseFloat(jQuery('.shipping_cost_tax_plain').val());
			total_tax_cost += shipping_cost_tax;
			jQuery('.td_tax_price').val(total_tax_cost);
			baquote_EditTaxCost();
	
			window.parent.$(".infor_tax").html(html_new_infor);
			jQuery(".estimate-block .loadicon_tax").hide();
		}
		
	});
}
function baquote_checkCouponCode() {
	var coupon_code = $('.td_coupon_code').val();
	var id_quote = jQuery('.td_id_quote').val();
	var id_customer = jQuery('.td_id_customer').val();
	var total_price = 0;
	$("tr.td_check_exist_pro").each(function(){
		var price_pro = $(this).find(".price_pro").val();
		price_pro = addproduct_newquote_convertToNumber(price_pro);
		var qty_pro = addproduct_newquote_convertToNumber($(this).find(".qty_pro").val());
		var total_price_pro = price_pro * qty_pro;
		total_price += total_price_pro;
	});
	jQuery.ajax({
		type: 'POST',//phương thức gửi
		jscrossDomain: window.location.hostname,
		crossDomain: true,
		dataType: "json",
		url: '../index.php?controller=couponcode&fc=module&module=askforaquotemodul',
		data: 	'couponcode=' + coupon_code
				+'&id_quote=' + id_quote
				+'&id_customer=' + id_customer
				+'&total_price=' + total_price
				+'&quote_token=' + token_askaquotemodule
				+'&ajax=1',
		success: function(jsonData1,textStatus,jqXHR)
		{
			infor_coupon(jsonData1);
			window.parent.$(".ba_newsletter_popup_background").css("display", "none");
		}	
	});
	
}
function infor_coupon(jsonData1){
	var sign = jQuery('.qty_product_dbboo').attr('data-sign-dbboo');
	var html_coupon_infor = '';
	if(jsonData1.check == '1'){
		if(jsonData1.reduction_product == '0'){
			if(jsonData1.type_coupon == '1'){
				if(jsonData1.free_shipping == '1'){
					html_coupon_infor += '<p class="help-block"> -' + jsonData1.num_coupon + '% - '+free_shipping+'</p>';
				} else {
					html_coupon_infor += '<p class="help-block"> -' + jsonData1.num_coupon + '%</p>';
				}
			} else {
				if(jsonData1.free_shipping == '1'){
					html_coupon_infor += '<p class="help-block"> -' + jsonData1.num_coupon + ''+sign+' - '+free_shipping+'</p>';
				} else {
					html_coupon_infor += '<p class="help-block"> -' + jsonData1.num_coupon + ''+sign+'</p>';
				}
			}
		} else if(jsonData1.reduction_product == '-1'){
			if(jsonData1.type_coupon == '1'){
				if(jsonData1.free_shipping == '1'){
					html_coupon_infor += '<p class="help-block">'+cheapest_product+' -' + jsonData1.num_coupon + '% - '+free_shipping+'</p>';
				} else {
					html_coupon_infor += '<p class="help-block">'+cheapest_product+' -' + jsonData1.num_coupon + '%</p>';
				}
			} else {
				if(jsonData1.free_shipping == '1'){
					html_coupon_infor += '<p class="help-block">'+cheapest_product+' -' + jsonData1.num_coupon + ''+sign+' '+free_shipping+'</p>';
				} else {
					html_coupon_infor += '<p class="help-block">'+cheapest_product+' -' + jsonData1.num_coupon + ''+sign+'</p>';
				}
			}
		} else {
			if(jsonData1.type_coupon == '1'){
				if(jsonData1.free_shipping == '1'){
					html_coupon_infor += '<p class="help-block">'+productttt+' '+jsonData1.infor_product_select_coupon[0]['product_name']+' - ' + jsonData1.num_coupon + '% '+free_shipping+'</p>';
				} else {
					html_coupon_infor += '<p class="help-block">'+productttt+' '+jsonData1.infor_product_select_coupon[0]['product_name']+' - ' + jsonData1.num_coupon + '%</p>';
				}
			} else {
				if(jsonData1.free_shipping == '1'){
					html_coupon_infor += '<p class="help-block">'+productttt+' '+jsonData1.infor_product_select_coupon[0]['product_name']+' - ' + jsonData1.num_coupon + ''+sign+' '+free_shipping+'</p>';
				} else {
					html_coupon_infor += '<p class="help-block">'+productttt+' '+jsonData1.infor_product_select_coupon[0]['product_name']+' - ' + jsonData1.num_coupon + ''+sign+'</p>';
				}
			}
		}
		window.parent.$(".infor_coupon").html(html_coupon_infor);
		var shipping_cost = jQuery('.td_shipping_price').val();
		var tax_cost = jQuery('.td_tax_price').val();
		shipping_cost = addproduct_newquote_convertToNumber(shipping_cost);
		tax_cost = addproduct_newquote_convertToNumber(tax_cost);
		
		jsonData1.total_pri_new_notax = parseFloat(jsonData1.total_pri_new) + parseFloat(shipping_cost);
		var html_new_totalpri_order = ''+ priceformatNumber(jsonData1.total_pri_new_notax)+''+sign +'';
		html_new_totalpri_order += '';
		window.parent.$(".td_total_price_notax_order").html(html_new_totalpri_order);
		jsonData1.total_pri_new = parseFloat(jsonData1.total_pri_new) + parseFloat(shipping_cost) + parseFloat(tax_cost);
		var html_new_totalpri = '';
		html_new_totalpri += '<input type = "hidden" value="' + jsonData1.total_pri_new + '" class="td_fixed_total_price">';
		html_new_totalpri += '<input name="baproduct[total_price_quote]" class="batotalproducts" type = "hidden" value="'+ jsonData1.total_pri_new +'" id="td_totalprice_newquote" class="td_total_pri_newquote"><span>'+ sign +''+ priceformatNumber(jsonData1.total_pri_new) +'</span>';
		window.parent.$(".total_price_quote_dbboo").html(html_new_totalpri);
		var html_new_totalpri_coupon = ''+ priceformatNumber(jsonData1.pri_coupon)+''+sign +'';
		html_new_totalpri_coupon += '';
		window.parent.$(".td_total_coupon_order").html(html_new_totalpri_coupon);
		var html_new_totalpri_order = ''+ priceformatNumber(jsonData1.total_pri_new)+''+sign +'';
		html_new_totalpri_order += '';
		window.parent.$(".td_total_price_tax_order").html(html_new_totalpri_order);
	} else {
		html_coupon_infor += '<p class="help-block"><i class="icon-warning-sign"></i> '+error_js+'</p>';
		window.parent.$(".infor_coupon").html(html_coupon_infor);
		var shipping_cost = jQuery('.td_shipping_price').val();
		var tax_cost = jQuery('.td_tax_price').val();
		
		tax_cost = convertCurrencytoNumber(tax_cost);
		shipping_cost = convertCurrencytoNumber(shipping_cost);
		jsonData1.total_pri_new = convertCurrencytoNumber(jsonData1.total_pri_new);
		
		jsonData1.total_pri_new = parseFloat(jsonData1.total_pri_new) + parseFloat(shipping_cost) + parseFloat(tax_cost);
		window.parent.$('.td_total_pri_newquote').val(jsonData1.total_pri_new);
		window.parent.$('.td_fixed_total_price').val(jsonData1.total_pri_new);
		window.parent.$('input[name="baproduct[total_price_quote]').val(jsonData1.total_pri_new);
		window.parent.$('.total_price_quote_dbboo span').text(priceformatNumber(jsonData1.total_pri_new) + sign);
	}
	var coupon_code = $('.td_coupon_code').val();
	if(coupon_code.length == 0){
		$(".infor_coupon .help-block").text(enter_coupon);
	}
}
$(document).ready(function(){
	$("#td_auto_shipping").on("click", function(){
		window.parent.$(".td_shipping_popup").css("display", "block");
		window.parent.$(".ba_newsletter_popup_background").css("display", "block");
	});
	$(".td_choose_shipping").on("click", function(){
		baquote_calcShippingCost();
	});
	/////////////tax cost
	$("#td_auto_tax").on("click", function(){
		baquote_calcTaxCost();
	});
	$("#td_auto_coupon").on("click", function(){
		baquote_checkCouponCode();
	}); 
	////////////	
    $("#td_save_new_quote").click(function(){
        $("#error_email").scrollTop();
        var email = jQuery('#input_email').val();
        if( !validateEmail(email)) {
            window.parent.$("#error_email").css("display", "block");
            return false; 
        }
        if ($('.product_table_newquote').children('tr').hasClass("td_check_exist_pro")){
            window.parent.$("#quote_err").css("display", "none");
            $(window).scrollTop(0,0);
            return true;
        } else {
            window.parent.$("#quote_err").css("display", "block");
            return false; 
        }
    });
    $(".exit_popup").click(function(){
        window.parent.$(".td_shipping_popup").css("display", "none");
        window.parent.$(".ba_newsletter_popup_background").css("display", "none");
    });
});

// since 1.0.42
function baquote_sortProductsQuoteList(){
	$("table#orderProducts tbody").sortable({
		opacity: 0.6,
		cursor: "move",
		update: function() {
			var position = new Array();
			$("table#orderProducts tbody input.quote-position").each(function(i){
				$(this).val(i+1);
			});
		}
	});
}