jQuery(document).ready(function($){

	function CBreplaceAll(str, find, replace) {

    	return str.split(find).join(replace);

	}

	

	var emoji = $("#message_before, #message_after").emojioneArea({

        saveEmojisAs      : "unicode",

        imageType         : "svg",

        pickerPosition: 'bottom',

        //buttonTitle: __("Use the TAB key to insert emoji faster")

    });

	

    // Messenger live preview

    emoji[0].emojioneArea.on("paste keyup input emojibtn.click", function() {

        var caption = $(".cb_ahachat_before_message").find(".emojionearea-editor");

        var str_show =caption.html();

        //str_show = str_show.replace("[firstName]", "John");  // only correct if str have one

        //str_show = str_show.replace("[orderNumber]", "#1234");// only correct if str have one

       	str_show = CBreplaceAll(str_show,"[firstName]", "John");

       	str_show = CBreplaceAll(str_show,"[orderNumber]", "#1234");

        $(".ahachat_text_preiew_width_cb").html(str_show);

        

    });

    emoji[1].emojioneArea.on("paste keyup input emojibtn.click", function() {

     	var convert_unique_coupon = $("#coupon_code").val();

     	var amount_coupon = $("#amount").val();

     	if(convert_unique_coupon==""){

     		convert_unique_coupon=="THANKYOU234";

     	}else{

     		convert_unique_coupon = convert_unique_coupon+"234";

     	}

     	if(amount_coupon==""){

     		amount_coupon=10;

     	}

     	var coupon_type = $("#coupon_type").val();

   		if(coupon_type == "fixed_cart"){

   			amount_coupon = "$"+amount_coupon;

   		}else if(coupon_type == "percent_product"){

   			amount_coupon = "$"+amount_coupon+"/product";

   		}else{

   			amount_coupon = amount_coupon+"%";

   		}

   		var expiry_date = $("#expiry_date").val();

      if(expiry_date > 0){

        expiry_date_use = expiry_date+" days";

      }else{

        expiry_date_use = " days";

      }

      var usage_limit = $("#usage_limit").val();  

        var caption = $(".cb_ahachat_after_message").find(".emojionearea-editor");

      	var str_show =caption.html();

      	/*str_show = str_show.replace("[amount_coupon]", amount_coupon);

      	str_show = str_show.replace("[expiry_date]", expiry_date+" days");

      	str_show = str_show.replace("[unique_coupon]", convert_unique_coupon);

        */

        str_show = CBreplaceAll(str_show,"[amount_coupon]", amount_coupon);

        str_show = CBreplaceAll(str_show,"[expiry_date]", expiry_date_use);

        str_show = CBreplaceAll(str_show,"[unique_coupon]", convert_unique_coupon);

        str_show = CBreplaceAll(str_show,"[usage_limit]", usage_limit);

        $(".fb-text__text").html(str_show);

       

    });



    $("#cartback_call_action").on("input",function(e){

    	var str_show = $(this).val();

    	$(".fb-button").text(str_show);

    });



    $("#messenger_receipt_action").change(function(e){

    	if($(this).is(":checked")){

    		$(".messenger_receipt_show_on").show();

    	}else{

    		$(".messenger_receipt_show_on").hide();

    	}

    });

    $("#messenger_receipt_action").trigger("change");

    //

   	$("#amount").on("input",function(event){

   		var amount_coupon = $(this).val();

   		var coupon_type = $("#coupon_type").val();

   		if(coupon_type == "fixed_cart"){

   			amount_coupon = "$"+amount_coupon;

   		}else if(coupon_type == "percent_product"){

   			amount_coupon = "$"+amount_coupon+"/product";

   		}else{

   			amount_coupon = amount_coupon+"%";

   		}

   		var convert_unique_coupon = $("#coupon_code").val();



   		if(convert_unique_coupon==""){

     		convert_unique_coupon=="THANKYOU234";

     	}else{

     		convert_unique_coupon = convert_unique_coupon+"234";

     	}

      var expiry_date = $("#expiry_date").val();

     	if(expiry_date > 0){

        expiry_date_use = expiry_date+" days";

      }else{

        expiry_date_use = " days";

      }

      var usage_limit = $("#usage_limit").val();  

   		var str_show = $("#message_after").val();

   		

        str_show = CBreplaceAll(str_show,"[amount_coupon]", amount_coupon);

        str_show = CBreplaceAll(str_show,"[expiry_date]", expiry_date_use);

        str_show = CBreplaceAll(str_show,"[unique_coupon]", convert_unique_coupon);

        str_show = CBreplaceAll(str_show,"[usage_limit]", usage_limit);

        $(".fb-text__text").text(str_show);

   	});



   	$("#coupon_code").on("input",function(event){

   		var convert_unique_coupon = $(this).val();

     	var amount_coupon = $("#amount").val();

     	var coupon_type = $("#coupon_type").val();

   		if(coupon_type == "fixed_cart"){

   			amount_coupon = "$"+amount_coupon;

   		}else if(coupon_type == "percent_product"){

   			amount_coupon = "$"+amount_coupon+"/product";

   		}else{

   			amount_coupon = amount_coupon+"%";

   		}

     	if(convert_unique_coupon==""){

     		convert_unique_coupon=="THANKYOU234";

     	}else{

     		convert_unique_coupon = convert_unique_coupon+"234";

     	}

     	var expiry_date = $("#expiry_date").val();

      if(expiry_date > 0){

        expiry_date_use = expiry_date+" days";

      }else{

        expiry_date_use = " days";

      }

      var usage_limit = $("#usage_limit").val();

   		var str_show = $("#message_after").val();

   		/*str_show = str_show.replace("[amount_coupon]", amount_coupon);

   		str_show = str_show.replace("[expiry_date]", expiry_date+" days");

      	str_show = str_show.replace("[unique_coupon]", convert_unique_coupon);

        */

        str_show = CBreplaceAll(str_show,"[amount_coupon]", amount_coupon);

        str_show = CBreplaceAll(str_show,"[expiry_date]", expiry_date_use);

        str_show = CBreplaceAll(str_show,"[unique_coupon]", convert_unique_coupon);

        str_show = CBreplaceAll(str_show,"[usage_limit]", usage_limit);

        $(".fb-text__text").text(str_show);

   	});



   	$("#coupon_type").change(function(e){

   		var coupon_type = $(this).val();

   		var amount_coupon = $("#amount").val();

   		var convert_unique_coupon = $("#coupon_code").val();

   		if(coupon_type == "fixed_cart"){

   			amount_coupon = "$"+amount_coupon;

   		}else if(coupon_type == "percent_product"){

   			amount_coupon = "$"+amount_coupon+"/product";

   		}else{

   			amount_coupon = amount_coupon+"%";

   		}

     	if(convert_unique_coupon==""){

     		convert_unique_coupon=="THANKYOU234";

     	}else{

     		convert_unique_coupon = convert_unique_coupon+"234";

     	}

     	var expiry_date = $("#expiry_date").val();

      if(expiry_date > 0){

        expiry_date_use = expiry_date+" days";

      }else{

        expiry_date_use = " days";

      }

      var usage_limit = $("#usage_limit").val();

   		var str_show = $("#message_after").val();

   		/*

   		str_show = str_show.replace("[amount_coupon]", amount_coupon);

   		str_show = str_show.replace("[expiry_date]", expiry_date+" days");

      	str_show = str_show.replace("[unique_coupon]", convert_unique_coupon);

        */

        str_show = CBreplaceAll(str_show,"[amount_coupon]", amount_coupon);

        str_show = CBreplaceAll(str_show,"[expiry_date]", expiry_date_use);

        str_show = CBreplaceAll(str_show,"[unique_coupon]", convert_unique_coupon);

        str_show = CBreplaceAll(str_show,"[usage_limit]", usage_limit);

        $(".fb-text__text").text(str_show);

   	});

   	//

   	$("#expiry_date").on("input",function(event){

   		var expiry_date = $(this).val();

   		var convert_unique_coupon = $("#coupon_code").val();

     	var amount_coupon = $("#amount").val();

     	var coupon_type = $("#coupon_type").val();

   		if(coupon_type == "fixed_cart"){

   			amount_coupon = "$"+amount_coupon;

   		}else if(coupon_type == "percent_product"){

   			amount_coupon = "$"+amount_coupon+"/product";

   		}else{

   			amount_coupon = amount_coupon+"%";

   		}

     	if(convert_unique_coupon==""){

     		convert_unique_coupon=="THANKYOU234";

     	}else{

     		convert_unique_coupon = convert_unique_coupon+"234";

     	}

     	if(expiry_date > 0){

        expiry_date_use = expiry_date+" days";

      }else{

        expiry_date_use = " days";

      }

      var usage_limit = $("#usage_limit").val();

   		var str_show = $("#message_after").val();

        str_show = CBreplaceAll(str_show,"[amount_coupon]", amount_coupon);

        str_show = CBreplaceAll(str_show,"[expiry_date]", expiry_date_use);

        str_show = CBreplaceAll(str_show,"[unique_coupon]", convert_unique_coupon);

        str_show = CBreplaceAll(str_show,"[usage_limit]", usage_limit);

        $(".fb-text__text").text(str_show);

   	});

    //

    //

    $("#usage_limit").on("input",function(event){

      var usage_limit = $(this).val();

      var convert_unique_coupon = $("#coupon_code").val();

      var amount_coupon = $("#amount").val();

      var coupon_type = $("#coupon_type").val();

      if(coupon_type == "fixed_cart"){

        amount_coupon = "$"+amount_coupon;

      }else if(coupon_type == "percent_product"){

        amount_coupon = "$"+amount_coupon+"/product";

      }else{

        amount_coupon = amount_coupon+"%";

      }

      if(convert_unique_coupon==""){

        convert_unique_coupon=="THANKYOU234";

      }else{

        convert_unique_coupon = convert_unique_coupon+"234";

      }

      var expiry_date = $("#expiry_date").val();

      if(expiry_date > 0){

        expiry_date_use = expiry_date+" days";

      }else{

        expiry_date_use = " days";

      }

      var str_show = $("#message_after").val();

        str_show = CBreplaceAll(str_show,"[amount_coupon]", amount_coupon);

        str_show = CBreplaceAll(str_show,"[expiry_date]", expiry_date_use);

        str_show = CBreplaceAll(str_show,"[unique_coupon]", convert_unique_coupon);

        str_show = CBreplaceAll(str_show,"[usage_limit]", usage_limit);

        $(".fb-text__text").text(str_show);

    });

});