/**
* 删除购物车的商品信息
*/

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;
		   }

		});

}

/**
* 菜单的收展
* idName: ID名称
*/
function menu_show_and_dispaly( idName ){
	document.getElementById(idName).style.display = '';
	return true;
}

/**
* 把大图显示在指定的位置上
* bigPic:图片路径
* Sname: 图片名称
*/
function gotoshow( bigPic, Sname ){
	document.getElementById( Sname ).src = bigPic;
	return true;
}