/**
* ??????????
*/
function delete_shopping_products( pid ){
	
	$("#"+pid).hide();
	
	var intCount = $("#"+pid+" td:eq(4)").html();		
	intCount = parseInt(intCount);	//?????
	
	update_shopping_cart_count();
	ajax_update_shopping_cart( pid, 3 );
	update_product_total( pid, intCount, 2 );
	
	
	//return true;
}

/**
* ????????
* inttype:1?2?
*/
function add_minus_products( pid, inttype ){
	
	var intCount = $("#"+pid+" td:eq(4)").html();	
	var flaPrice = $("#"+pid+" td:eq(2)").html();	
	
	flaPrice = parseFloat(flaPrice);
	intCount = parseInt(intCount);	//?????	
	intCount = inttype == 1 ? intCount+1 : intCount - 1;	
	
	flaPrice=Math.round(flaPrice*100)/100;
	
	intTotalCount = intCount * flaPrice;
	//alert(intTotalCount);
	//alert(intCount);
	if( intCount <= 0 ){
		if( confirm('Real want to delete this Product?') ){
			delete_shopping_products(pid);
			update_shopping_cart_count();
		}else{
			intCount = 1;
		}
	}else{
		
		intTotalCount=Math.round(intTotalCount*100)/100;	
		$("#"+pid+" td:eq(5)").html(intTotalCount)
		$("#"+pid+" td:eq(4)").html(intCount);		
		ajax_update_shopping_cart( pid, inttype );	
		update_product_total( pid, 1, inttype );		
		
	}
	
	//return true;
}

/*
*	????
* count:?????
* inttype:1?2?
*/
function update_product_total( pid, count, inttype ){
	
	var flaPrice = $("#"+pid+" td:eq(2)").html();	//????????	
	var flaTotal = $("#allTotal").html();
	flaTotal = flaTotal.replace('$','');
	flaPrice = parseFloat(flaPrice);	//??????
	flaTotal = parseFloat(flaTotal);	//??????
	
	
	
	flaTotal = inttype == 1 ? flaTotal+flaPrice*count : flaTotal-flaPrice*count;
	
	flaPrice=Math.round(flaPrice*100)/100;	
	flaTotal=Math.round(flaTotal*100)/100;
	
	$("#allTotal").html("$"+flaTotal);
		
	//return true;
}

/**
* ???????
*/
function update_shopping_cart_count(){
	var intCount = $("#shoppingCartCount strong").html();
	intCount = parseInt(intCount);	//?????
	intCount--;
	$("#shoppingCartCount strong").html(intCount);
	//return true;
}

/**
* ajax,?????
* inttype:1?2?3??
*/
function ajax_update_shopping_cart( pid, inttype ){
		
		$.ajax({
		   type: "POST",
		   url: "shopping_cart_update_data.php",
		   data: "pid="+pid+"&qty=1&tye="+inttype,
		   success: function(msg){
		     return true;
		   }
		});
	
}

/**
* ?????
* discountcode:?????
*/
function discountCodeAdd( discountName ){
	
	if( !discountName ){
		return;
	}
	
	var merchandise = $("#merchandise span").html();
	var shippingSub = $("#shippingSub span").html();	
	var Redemption = $("#Redemption span").html() == null ? '$0' : $("#Redemption span").html();	
	var salesTax = $("#salesTax span").html();
	var total = 0;
	var discount = '';
	var couponCode = $("#"+discountName).val();
	
	merchandise = merchandise.replace('$','');
	shippingSub = shippingSub.replace('$','');
	salesTax = salesTax.replace('$','');	
	Redemption = Redemption.replace('$','');
	
	total = shopping_subtotal( merchandise, shippingSub, salesTax, discount, Redemption );
	
	$.ajax({
		   type: "POST",
		   url:"process.php",
		   data:"action=coupon&coupon="+couponCode+"&total="+total,
		   beforeSend:function( msg ){		   	
		   },
		   success: function(msg){
		   	
		   		var msgs = msg.split(';');
		   		
		   		if( msgs[0] == 'false' ){
		   			alert('error');
		   		}else{
		   			
		   			if($('#discountSub').html() == null){
		   				if( msgs[0] == 'G' ){
		   					discount = "$"+msgs[1];
		   				}else if( msgs[0] == 'P' ){
		   					discount = msgs[1]+"%";
		   				}
							$("#salesTax").after("<li id='discountSub'><span>"+discount+"</span>Discount Code:</li>");
						}else{
							$("#discountSub span").html(discount);
						}
						
						total = shopping_subtotal( merchandise, shippingSub, salesTax, discount, Redemption );					
						$("#estimatedTotal span").html("$"+total);
		   		}		   		
		   }
		});
	
	
}

/**
* ??????
*/
function sel_shipping_method( ShipVal ){
	
	//var ShipVal= $('input[@name=shipping][@checked]').val();
	
	if( !ShipVal ){
		return;
	}
	
	var money = $("#moneySub").html();	
	var tax = $("#salesTax").html();
	var shippingSub = $("#shippingSub").html('$'+ShipVal);
	var total;
	
	money = money.replace('$','');	
	tax = tax.replace('$','');	
	
	total = shopping_subtotal( money, ShipVal, tax, 0, 0 );	
	$("#total").html( "$"+total );
	$('#hidshippingSub').val(ShipVal);
	$("#total").slideDown("slow");
	
	
}

/**
* ????
*/
function shopping_subtotal( money, shipping, tax, gif, dis ){
	
	var total;
	
	money =parseFloat(money);	
	money = money == 0 ? 0 : Math.round(money*100)/100;
	
	tax =parseFloat(tax);
	tax = tax == 0 ? 0: Math.round(tax*100)/100;
	
	
	shipping =parseFloat(shipping);	
	shipping = shipping == 0 ? 0 : Math.round(shipping*100)/100;
	
	var blngif = gif.search('%');
	gif = gif.replace( '%', '' );
	gif = gif.replace( '$', '' );
	
	gif = parseFloat(gif);
	gif = gif == 0 ? 0 : Math.round(gif*100)/100;
	
	dis = parseFloat(dis);
	dis = dis == 0 ? 0 : Math.round(dis*100)/100;
	
	if( blngif ){
		total = ( money+shipping+tax-dis ) * gif/100;
	}else{
		total = money+shipping+tax-gif-dis;
	}
	
	total = total == 0 ? 0 : Math.round(total*100)/100;
	
	return total;
}

/**
* ????
*/
function syncCountryState( cuntryId ){
		$("#entry_state").empty();
		
	  $.ajax({
		   type: "POST",
		   url:"process.php",
		   data:"action=address&cid="+cuntryId,
		   beforeSend:function( msg ){
		   	$("<option>loading...</option>").appendTo("#entry_state");
		  },
		   success: function(msg){
		   		$("#entry_state").empty();
		   		$(msg).appendTo("#entry_state");
		     //return true;
		   }
		});
}