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

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

/**
* 添加到收藏夹
*/
function addBookmark( pid,url){
	
	//window.external.AddFavorite( url, title );
	var kdocTitle = document.title;//标题 
	var blnOk = false;
	
	if(kdocTitle == null){ 
		var t_titles = document.getElementByTagName("title") 
		if(t_titles && t_titles.length >0) 
		{ 
		   kdocTitle = t_titles[0]; 
		}else{ 
		   kdocTitle = ""; 
		} 
	}
	

	if( !document.all ){
		window.sidebar.addPanel( kdocTitle, url,""); 
	}else if( document.all ){		
		window.external.AddFavorite( url, kdocTitle );
	}else if( window.opera && window.print ){
		return true;
	}

	  $.ajax({
	   type: "POST",
	   url:"favorite_action.php",
	   data:"pid="+pid,
	   beforeSend:function( msg ){
		//$("<option>loading...</option>").appendTo("#entry_state");
	  },
	   success: function(msg){
			//$("#entry_state").empty();
			//$(msg).appendTo("#entry_state");
		 return true;
	   }

	});

}

function winopen( divname, htmlname ){

	if( $(divname).css("display") == 'none' ){			
		$(divname).slideToggle("fast");
		
		//$(divname).show(1000);
		// if( $(divname).html() == '' ){			
		// 			loadDemo( htmlname, divname);
		// 		}
		
		switch( divname ){
			case "#showform":
				$("#showform1").slideUp("fast");
				$("#showform2").slideUp("fast");
			break;
			case "#showform1":
				$("#showform").slideUp("fast");
				$("#showform2").slideUp("fast");				
			break;
			case "#showform2":
				$("#showform1").slideUp("fast");
				$("#showform").slideUp("fast");
				check_formWithout();
			break;
		}//end switch
		

	}else{
		$(divname).slideToggle("fast")
		//$(divname).hide(1000);
	}
}


function loadDemo(path, divname) {
	//var directory = path.match(/([^\/]+)\/[^\/\.]+\.html$/)[1];
	var data = false;
	
	$.get( "http://www.luxurysomany.com/"+path+".htm", function( html ){
		
		data = html;
		data = data.replace(/<script.*>.*<\/script>/ig,""); // Remove script tags
		data = data.replace(/<\/?link.*>/ig,""); //Remove link tags
		data = data.replace(/<\/?meta.*>/ig,""); //Remove link tags
		data = data.replace(/<\/?html.*>/ig,""); //Remove html tag
		data = data.replace(/<\/?body.*>/ig,""); //Remove body tag
		data = data.replace(/<\/?head.*>/ig,""); //Remove head tag
		data = data.replace(/<\/?!doctype.*>/ig,""); //Remove doctype
		data = data.replace(/<title.*>.*<\/title>/ig,""); // Remove title tags
		//data = data.trim('');
		$(divname).html(data);
	});
	
	return false;
}

/**
* 用户登录
*/
function cart_login(){
	
	var runType = true;
	var strError = '';
	
	if( $("#customer_name").val() == '' ){		
		strError += '<li>Customer name is required!</li>';
		runType = false;
	}
	
	if( $("#password").val() == '' ){
		strError += '<li>Password is required!</li>';
		runType = false;
	}
	if( !runType ){
		strError = '<ul>'+strError+'</ul>';
		$("#showFormError").html(strError);
	}
	
	if( runType ){//判断用户是否存在
		$.ajax({
			type: "POST",
			url: "http://www.brandluxuryshop.com/cart.php?action=login&category=register",
			data: "customer_name="+$("#customer_name").val()+"&password="+$("#password").val()+"&ajax=1",
			success: function(msg){
					o = eval('(' + msg + ')');   
       				if(o.status == 'success'){ 
						window.location = "cart.php?action=checkout";
					}else{
						$("#showFormError").html(o.meno);
					}//end if
			}
		}); 		
	}
	return false;
	return runType;
	
}//end function

