/**
* 删除购物车的商品信息
*/

function delete_shopping_products( pid ){

	$("#cart_"+pid).hide();
	return true;
	var intCount = $("#cart_"+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;
		   }
		});
}

/**
* 用户登录
*/
function userLogin(){
	var username = $("#username").val();
	var password = $("#password").val(); 
	
	
	if( username == '' || password == ''){
		alert('User name and Password Required!');
		return false;
 
	}

	$.post('user_login.php', {
		username: username,
		password: password
	},function( runData ){
		if( runData == 'fail' ){
			alert('The user does not exist or password error!');
		}else if( runData == 'success' ){
			//alert(runData+'Your data has been saved.');
			window.location = './';
		}
	});

}//end function

/**
* 得到省份
*/
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;
		   }

		});

}

/**
* 添加到收藏夹
*/
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;
	   }

	});

}

/**
* copy 表单信息
*/
function same_as_billing(){
	if( $("#checkbox").attr("checked") ){
		$("#sfirstname").val($("#bfirstname").val());
		$("#slastname").val($("#blastname").val());
		$("#sstreetaddress").val($("#bstreetaddress").val());
		$("#soptional").val($("#boptional").val());
		$("#scity").val($("#bcity").val());
		$("#sstate").val($("#bstate").val());
		$("#szip").val($("#bzip").val());
		$("#scountry").val($("#bcountry").val());
		$("#sphone").val($("#bphone").val());
		$("#semail").val($("#bemail").val());
	}else{
		$("#sfirstname").val('');
		$("#slastname").val('');
		$("#sstreetaddress").val('');
		$("#soptional").val('');
		$("#scity").val('');
		$("#sstate").val('');
		$("#szip").val('');
		$("#scountry").val('');
		$("#sphone").val('');
		$("#semail").val('');
	}
}
/**
* 显示billing的填写表单
*/
function same_as_shipping( thisObj ){
	if( thisObj.checked ){
		$("#billingAddress").hide();	
	}else{
		$("#billingAddress").show();
	}
}
/**
* 表单提交
*/
function submitform( formname ){
	var sf = document.getElementById(formname);
	sf.submit();
	
	return true;
}


var flag=false; 

function DrawImages(ImgD, w, h){ 

 var image=new Image(); 
 image.src=ImgD.src; 
 if(image.width>0 && image.height>0){ 
  flag=true; 
  if(image.width/image.height>= w/h){
   if(image.width > w){
    ImgD.width=w;
    ImgD.height=(image.height*w)/image.width;
   }else{
    ImgD.width=image.width;
    ImgD.height=image.height;
   }
  }
  else{
   if(image.height>h){
    ImgD.height=h;
    ImgD.width=(image.width*h)/image.height;
   }else{
    ImgD.width=image.width;
    ImgD.height=image.height;
   }
  }
 }

}