/* hereitself.com checkout functions */
var jx_d = '/_ajax.php';

// On/Off AddressBook
function sw_addressbook(id) {
	if ( id == '1' ) {
		calc_ship();
		if ( $("#ship_addy1").checked == false )
		{
			$("#ship_addy1").checked = true;
		}
		$("#ship_address_old").attr("class", "memty");
		$("#ship_address_new").attr("class", "hidmemty");
	} else if ( id == '2' ) {
		$("#bill_addy2").checked = true;
		$("#bill_address_new").attr("class", "memty");
		$("#bill_address_old").attr("class", "hidmemty");

	} else if ( id == '3' ) {
		$("#bill_addy3").checked = true;
		$("#bill_address_old").attr("class", "memty");
		$("#bill_address_new").attr("class", "hidmemty");
	} else {
		calc_ship();
		if ( $("#ship_addy0").checked == false )
		{
			$("#ship_addy0").checked = true;
		}
		$("#ship_address_new").attr("class", "memty");
		$("#ship_address_old").attr("class", "hidmemty");
	}
}

// On/Off Billing Address
function sw_billing() {
	if ( $("#same").checked ) {
		$("#bill_div").toggleClass('hidmemty');
	} else {
		$("#bill_div").toggleClass('memty');
	}
}

// calculate shipping
function calc_ship(po_country, po_method) {
	var subtotal = $("#sub_inpt").val();

	if (!po_method && $("input[name='ship_method']"))
	{
		var po_method = $("input[name='ship_method']:checked").val();
	}

	if ($("input[name='ship_addy']") && $("input[name='ship_addy']:checked").val() == 1)
	{
		var ship_c = (po_country) ? po_country + '&abook=1' : $("#ship_addr_list").val() + '&abook=1';
	}
	else
	{
		var ship_c = (po_country) ? po_country : $("#ship_country").val();
	}
	var log_res = $('#shipping_div').empty().addClass('pop-loading3');

	$.ajax({
		type: "POST",
		url: jx_d,
		data: 'do=calc_ship&country=' + ship_c + '&subtotal=' + subtotal + '&meth=' + po_method,
		success: function(html){
				log_res.empty().html(html);
		}
	});
}

// calculate gift
function calc_gift(total) {
	var gifty = ( $('input[name=gift_wrap]').is(':checked') ) ? 2 : 0;
	var g_total = Number(total) + Number(gifty);
	return g_total.toFixed(2);
}

// bag grand total
function bag_g_total(p_sub, p_ship) {
	p_tot = calc_gift(Number(p_sub) + Number(p_ship));

	$("#bag_g_ship").html(p_ship.toFixed(2));
	$("#bag_g_tot").html(p_tot);
}

// yes giftbox
function yes_giftbox() {
	var s_total = $("#bag_g_tot").text();
	var s_total = +s_total;
	var log_res = $("#gift_tot").empty();

	if ( $('input[name=gift_wrap]').is(':checked') ) {
		$("#giftbox_div").attr("class", "memty");
		log_res.html('2.00');
		g_total = s_total + 2;
	} else {
		$("#giftbox_div").attr("class", "hidmemty");
		log_res.html("0.00");
		g_total = s_total - 2;
	}
	$("#bag_g_tot").html(g_total.toFixed(2));
}

// Calculate Discount
function discount_div(id) {
	$("#discount_new_div").attr("class", "hidmemty");
	var org_total = $("#sub_inpt_org").val();
	$("#sub_inpt").val(org_total);
	$("#discount_tot").html('0.00');
	$("#discount_holder").attr("class", "hidmemty");
	calc_ship();

	if ( id == '3' ) {
		$("#f_coupon").attr("class", "hidmemty");
		$("#f_coin").attr("class", "memty");
	} else {
		$("#f_coupon").attr("class", "memty");
		$("#f_coin").attr("class", "hidmemty");
	}
}

function calc_discount() {
	var t_total = $("#sub_inpt").val();
	var dis_new = $("#discount_new_div");
	var dis_tot = $("#discount_tot");
	dis_new.attr("class", "hidmemty");

	for (var i=0; i < document.bagform.f_pay.length; i++)
	{
		if (document.bagform.f_pay[i].checked) {
		  var rad_val = document.bagform.f_pay[i].value;
		}
	}

	if ( ( rad_val == 1 || rad_val == 2 ) && (document.bagform.f_code.value == "" || document.bagform.f_code.value == 0) )
	{
		alert("Please enter Coupon/Gift Certificate Code");
		return false;
	}
	else if ( rad_val == 3 && document.bagform.f_coinage.selectedIndex == "" )
	{
		alert("Please select the coin amount you'd like to use for this order");
		return false;
	}

	if ( rad_val == 3 )
	{
		var discount_coin = Number($("#f_coinage").val());
		var discount_cfix = discount_coin.toFixed(2);
		$("#discount_holder").attr("class", "memty");
		dis_tot.empty().html(discount_cfix);
		g_total = Number(t_total) - discount_coin;
		$("#sub_inpt").val(g_total);
		calc_ship();
	}
	else
	{
		var discount_code = $("#f_code").val();
		var sub_tot = ( rad_val == 2 ) ? Number($("#bag_g_tot").text()) : t_total;
		dis_new.attr("class", "memty");
		dis_new.empty().addClass('pop-loading3');

		$.ajax({
			type: "POST",
			url: jx_d,
			data: 'do=calc_discount&f_pay=' + rad_val + '&f_code=' + discount_code + '&subtotal=' + sub_tot,
			success: function(html){
					dis_new.html(html);
					dis_new.removeClass('pop-loading3');
					var save_today = Number($("#save_today").val());
					var save_tofix = save_today.toFixed(2);

					if ( Number(save_today) > 0 )
					{
						dis_tot.empty();
						$("#discount_holder").attr("class", "memty");
						dis_tot.html(save_tofix);
						g_total = Number(t_total) - save_today;
						$("#sub_inpt").val(g_total);
						calc_ship();
					}
			}
		});
	}
}

// shopping bag plus/minus
function sbag_plu(id, baz_p, islem) {
	var quant = $("#quantity" + id).val();
	var baz_po = Number(baz_p);
	var totti = $("#tot_p").text();
	var total = +totti;

	if ( quant == '0' && islem == 'min') {
			var q = quant;
	} else {
		if ( islem == "plu" ) {
			var q = ++quant;
			var t = total + baz_po;
		} else {
			var q = --quant;
			var t = total - baz_po;
		}

		var b = q * baz_p;
			
		$("#quantity" + id).attr("value", q);
		$("#baz_p" + id).html(b.toFixed(2));
		$("#tot_p").html(t.toFixed(2));
	}
}
