// JavaScript Document
<!--
$(document).ready(function() {
	$.ajax({type:"POST", url:"../shop/add_to_shopping_cart.php", data:"id=", success:function(msg){
		var msg_array = msg.split(',');
		if(msg_array[0] != ''){
			//alert(msg_array[0]);
		}
		if(msg_array[1] != ''){
			$('.shopping_cart_info').html(msg_array[1]);
		}
		if(msg_array[2] != ''){
			//alert(msg_array[2]);
		}
		//alert('Add Success!'+msg);
		//$('.shopping_cart').html(msg);
	}});
	$('.product_add_to_cart_btn').click(function(){
		//if(confirm('Add to shopping cart?')){
			$.ajax({type:"POST", url:"../shop/add_to_shopping_cart.php", data:"type=product&id=" + $(this).attr('pid'), success:function(msg){
				var msg_array = msg.split(',');
				if(msg_array[0] != ''){
					alert(msg_array[0]);
				}
				if(msg_array[1] != ''){
					$('.shopping_cart_info').html(msg_array[1]);
				}
				if(msg_array[2] != ''){
					alert(msg_array[2]);
				}
				//alert('Add Success!'+msg);
				//$('.shopping_cart').html(msg);
			}});
			//Add_To_Shopping_Cart(<?=$shop_row['id']?>);
		//}
	});
	
	$('.map_shopping_cart_btn').click(function(){
		if(confirm('Add to shopping cart?')){
			$.ajax({type:"POST", url:"../shop/add_to_shopping_cart.php", data:"type=map&id=" + $(this).attr('mid'), success:function(msg){
				var msg_array = msg.split(',');
				if(msg_array[0] != ''){
					alert(msg_array[0]);
				}
				if(msg_array[1] != ''){
					$('.shopping_cart_info').html(msg_array[1]);
				}
				if(msg_array[2] != ''){
					alert(msg_array[2]);
				}
				//alert('Add Success!'+msg);
				//$('.shopping_cart').html(msg);
			}});
			//Add_To_Shopping_Cart(<?=$shop_row['id']?>);
		}
		return false;
	});
	
	$('.input_shop_unit').change(function(){add_to_shopping_cart_unit($(this));});
	$('.input_shop_unit').keyup(function(){add_to_shopping_cart_unit($(this));});
	$('.minus_unit').click(function(){
		if((parseInt($('#unit_' + $(this).attr('pid')).val()) - 1) > -1){
			$('#unit_' + $(this).attr('pid')).val(parseInt($('#unit_' + $(this).attr('pid')).val()) - 1);
			add_to_shopping_cart_unit($('#unit_' + $(this).attr('pid')));
		}
	});
	$('.plus_unit').click(function(){
		$('#unit_' + $(this).attr('pid')).val(parseInt($('#unit_' + $(this).attr('pid')).val()) + 1);
		add_to_shopping_cart_unit($('#unit_' + $(this).attr('pid')));
	});
	
	$('.input_map_shop_unit').change(function(){add_to_shopping_cart_unit($(this));});
	$('.input_map_shop_unit').keyup(function(){add_to_shopping_cart_unit($(this));});
	$('.map_minus_unit').click(function(){
		$('#map_unit_' + $(this).attr('mid')).val(parseInt($('#map_unit_' + $(this).attr('mid')).val()) - 1);
		add_to_shopping_cart_unit($('#map_unit_' + $(this).attr('mid')));
	});
	$('.map_plus_unit').click(function(){
		$('#map_unit_' + $(this).attr('mid')).val(parseInt($('#map_unit_' + $(this).attr('mid')).val()) + 1);
		add_to_shopping_cart_unit($('#map_unit_' + $(this).attr('mid')));
	});
	function add_to_shopping_cart_unit($evt){
		if($evt.attr('class') == 'input_shop_unit'){
			var pid = $evt.attr('pid');
			var punit = $evt.attr('value');
			$.ajax({type:"POST", url:"add_to_shopping_cart_unit.php", data:"type=product&id=" + pid + "&unit=" + punit, success:function(msg){
				if(msg == ''){
					$('#total_price_' + pid).html('$' + Math.round(parseFloat($('#price_' + pid).html().replace('$', '')) * punit * Math.pow(10, 2)) / Math.pow(10, 2));
					var sub_total_price = 0;
					var total_price = 0;
					for(var i = 0; i < $('.total_price').length; i++){
						sub_total_price += Math.round(parseFloat($($('.total_price').get(i)).html().replace('$', '')) * Math.pow(10, 2)) / Math.pow(10, 2);
					}
					$('#subtotal_price').html('$' + Math.round(sub_total_price * Math.pow(10, 2)) / Math.pow(10, 2));
					total_price = sub_total_price + Math.round(parseFloat($('#shipping_handling').html().replace('$', '')) * Math.pow(10, 2)) / Math.pow(10, 2);
					$('#shopping_total_price').html('$' + Math.round(total_price * Math.pow(10, 2)) / Math.pow(10, 2));
					set_total_account('product');
				}
				//alert('Add Success!'+msg);
				//$('.shopping_cart').html(msg);
			}});
		}
		else if($evt.attr('class') == 'input_map_shop_unit'){
			var mid = $evt.attr('mid');
			var munit = $evt.attr('value');
			$.ajax({type:"POST", url:"add_to_shopping_cart_unit.php", data:"type=map&id=" + mid + "&unit=" + munit, success:function(msg){
				if(msg == ''){
					$('#map_total_price_' + mid).html('$' + $('#map_price_' + mid).html().replace('$', '') * munit);
					var sub_total_price = 0;
					var total_price = 0;
					for(var i = 0; i < $('.map_total_price').length; i++){
						sub_total_price += parseFloat($($('.map_total_price').get(i)).html().replace('$', ''));
					}
					$('#map_subtotal_price').html('$' + sub_total_price);
					total_price = sub_total_price + parseFloat($('#map_shipping_handling').html().replace('$', ''));
					$('#map_shopping_total_price').html('$' + total_price);
					set_total_account('map');
				}
				//alert('Add Success!'+msg);
				//$('.shopping_cart').html(msg);
			}});
		}
	}
	
	$('.cancel_order_btn').click(function(){
		var pid = $(this).attr('pid');
		$.ajax({type:"POST", url:"del_item_at_shopping_cart.php", data:"type=product&id=" + pid, success:function(msg){
			if(msg == ''){
				$('#shopping_cart_list_item_' + pid).remove();
				//alert("Cancel Success!");
				location.href = 'shopping_cart.php';
				if($('.shopping_cart_list_item').length == 0){
					$('#shopping_total_price_list').html('<td colspan="7" align="center"><h1 onclick="javascript:history.back();" class="back_shop_btn">Please Select Product First!</h1></td>');
					$('#shopping_cart_action').remove();
					$('#shopping_cart_info_form').remove();
				}
				var sub_total_price = 0;
				var total_price = 0;
				for(var i = 0; i < $('.total_price').length; i++){
					sub_total_price += Math.round(parseFloat($($('.total_price').get(i)).html().replace('$', '')) * Math.pow(10, 2)) / Math.pow(10, 2);
				}
				$('#subtotal_price').html('$' + sub_total_price);
				total_price = sub_total_price + Math.round(parseFloat($('#shipping_handling').html().replace('$', '')) * Math.pow(10, 2)) / Math.pow(10, 2);
				$('#shopping_total_price').html('$' + total_price);
				set_total_account();
			}
		}});
	});
	$('.map_cancel_order_btn').click(function(){
		var mid = $(this).attr('mid');
		$.ajax({type:"POST", url:"del_item_at_shopping_cart.php", data:"type=map&id=" + mid, success:function(msg){
			if(msg == ''){
				$('#map_shopping_cart_list_item_' + mid).remove();
				alert("Cancel Success!");
				if($('.map_shopping_cart_list_item').length == 0){
					$('#map_shopping_total_price_list').html('<td colspan="7" align="center"><h1 onclick="javascript:history.back();" class="back_shop_btn">Please Select Product First!</h1></td>');
					$('#map_shopping_cart_action').remove();
					$('#map_shopping_cart_info_form').remove();
				}
				var sub_total_price = 0;
				var total_price = 0;
				for(var i = 0; i < $('.map_total_price').length; i++){
					sub_total_price += parseInt($($('.map_total_price').get(i)).html().replace('$', ''));
				}
				$('#map_subtotal_price').html('$' + sub_total_price);
				total_price = sub_total_price + parseInt($('#map_shipping_handling').html().replace('$', ''));
				$('#map_shopping_total_price').html('$' + total_price);
				set_total_account();
			}
		}});
	});
	
	function set_total_account($type){
		if($type == 'product'){
			$('#account').val($('#shopping_total_price').html().replace('$', ''));
		}
		else{
			$('#map_account').val($('#map_shopping_total_price').html().replace('$', ''));
		}
		//alert($('#account').val());
	}
	
	$('#checkout').click(function(){
		$('#shopping_cart_user_info').css({"display":"block"});
		if($('.side_menu').height() < $('.side_content').height()){
			$('.side_menu').height($('.side_content').height() + 40);
		}
	});
	
	$('#same_with_billing_info_btn').click(function(){
		if($(this).attr('checked')){
			$('#shipping_f_name').val($('#billing_f_name').val());
			$('#shipping_l_name').val($('#billing_l_name').val());
			$('#shipping_email').val($('#billing_email').val());
			$('#shipping_phone').val($('#billing_phone').val());
			$('#shipping_country')[0].selectedIndex = $('#billing_country')[0].selectedIndex;
			$('#shipping_add').val($('#billing_add').val());
		}
		else{
			$('#shipping_f_name').val('');
			$('#shipping_l_name').val('');
			$('#shipping_email').val('');
			$('#shipping_phone').val('');
			$('#shipping_country')[0].selectedIndex = 0;
			$('#shipping_add').val('');
		}
	});
	
	$('#payment_btn').click(function(){
		var stat_check = true;
		var email_rule = /^[^\s]+@[^\s]+\.[^\s]+$/;
		var alert_msg = '';
		
		//Billing Information
		if ($('#billing_f_name').val() == ''){
			stat_check = false;
			alert_msg += 'Please enter First Name (Billing Info)\r\n';
		}
		if ($('#billing_l_name').val() == ''){
			stat_check = false;
			alert_msg += 'Please enter Last Name (Billing Info)\r\n';
		}
		if ($('#billing_phone').val() == ''){
			stat_check = false;
			alert_msg += 'Please enter Phone (Billing Info)\r\n';
		}
		if (!email_rule.test($('#billing_email').val()) || $('#billing_email').val() == ''){
			stat_check = false;
			alert_msg += 'Please enter Email (Billing Info)\r\n';
		}
		if ($('#billing_country')[0].selectedIndex == 0){
			stat_check = false;
			alert_msg += 'Please select Country (Billing Info)\r\n';
		}
		if ($('#billing_add').val() == ''){
			stat_check = false;
			alert_msg += 'Please enter Address (Billing Info)\r\n';
		}
		
		//Shipping Information
		if ($('#shipping_f_name').val() == ''){
			stat_check = false;
			alert_msg += 'Please enter First Name (Shipping Info)\r\n';
		}
		if ($('#shipping_l_name').val() == ''){
			stat_check = false;
			alert_msg += 'Please enter Last Name (Shipping Info)\r\n';
		}
		if ($('#shipping_phone').val() == ''){
			stat_check = false;
			alert_msg += 'Please enter Phone (Shipping Info)\r\n';
		}
		if (!email_rule.test($('#shipping_email').val()) || $('#shipping_email').val() == ''){
			stat_check = false;
			alert_msg += 'Please enter Email (Shipping Info)\r\n';
		}
		if ($('#shipping_country')[0].selectedIndex == 0){
			stat_check = false;
			alert_msg += 'Please select Country (Shipping Info)\r\n';
		}
		if ($('#shipping_add').val() == ''){
			stat_check = false;
			alert_msg += 'Please enter Address (Shipping Info)\r\n';
		}
		
		//Verification Code
		$.ajax({type:"POST", url:"check_verification_code.php", async:false, data:"code=" + $('#code').val(), success:function(msg){
			if(msg != 'ok'){
				stat_check = false;
				alert_msg += 'Please enter Verification Code\r\n';
			}
		}});
		
		if(stat_check){
			//Save Order & Get Order Number
			$.ajax({
				type:"POST",
				url:"save_order.php",
				async:false,
				data:"billing_f_name=" + $('#billing_f_name').val() +
					"&billing_l_name=" + $('#billing_l_name').val() +
					"&billing_phone=" + $('#billing_phone').val() +
					"&billing_email=" + $('#billing_email').val() +
					"&billing_country=" + $('#billing_country option:selected').val() +
					"&billing_add=" + $('#billing_add').val() +
					"&shipping_f_name=" + $('#shipping_f_name').val() +
					"&shipping_l_name=" + $('#shipping_l_name').val() +
					"&shipping_phone=" + $('#shipping_phone').val() +
					"&shipping_email=" + $('#shipping_email').val() +
					"&shipping_country=" + $('#shipping_country option:selected').val() +
					"&shipping_add=" + $('#shipping_add').val() +
					"&special_comment=" + $('#special_comment').val() +
					"&lid=" + $('#lid').val() +
					"&pay_amount=" + $('#account').val(),
				success:function(msg){
					if(msg.indexOf("order_num=") == -1){
						alert('error' + msg);
						stat_check = false;
						alert_msg += 'Please enter Verification Code\r\n';
					}
					else{
						if(msg.indexOf("order_num=") != -1){
							$('#orderid').val(msg.substring(msg.indexOf("order_num=") + ("order_num=").length, msg.indexOf("form_url=")));
						}
						if(msg.indexOf("form_url=") != -1){
							$('#shopping_cart_form').attr("action", msg.substring(msg.indexOf("form_url=") + ("form_url=").length, msg.indexOf("hidden=")));
						}
						if(msg.indexOf("hidden=") != -1){
							$('.paypal_submit_fields').append(msg.substring(msg.indexOf("hidden=") + ("hidden=").length));
						}
						
						//alert(msg);
						//alert(msg.replace('order_num=', ''));
						//$('#orderid').val(msg.replace('order_num=', ''));
						//alert($('#orderid').val());
						$.ajax({type:"POST", url:"send_shopping.php", async:true, data:"o_id=" + $('#orderid').val(), success:function(msg){alert(msg);}});
						$('#shopping_cart_form').submit();
						//$('#shopping_cart_form').submit();
					}
				},
				error:function(msg){
						alert(msg);
				}
			});
		}
		else{
			alert(alert_msg);
		}
	});
})
$(window).load(function(){
	
});
