//AJAX 地址
var ajaxurl = "/member/ajax_do.php";
//网站 地址
var weburl = "";
// 添加商品到购物车 第一步
function addToCart(goodsId)
{
	$.get(ajaxurl, {action: "isLogin"},
	function(data){
		if (data == "" || data == 0)
		{
			//alert("登陆: " + data);
			openTipsLogin();
		}
		else
		{
			addToCart2(goodsId);
		}
	});
}
// 添加商品到购物车 第二步
function addToCart2(goodsId)
{
	//alert("添置到购物车: " + goodsId + "|"+ $("#goodsMatel").val() + "|"+$("#ringSize").val());
	$.post(ajaxurl,{action: "addtocart", goods_number: ""+$("#J_buynum").val()+"",goods_id:""+goodsId+"",goods_matel:""+$("#goodsMatel").val()+"",goods_size:""+$("#ringSize").val()+"",goods_attr:"" }, addToCartResponse);
}
function addToCartResponse(result)
{
	//if (result == "exists" || result == "true")
	if (result != "false")
	{
		//购物车商品已经存在exists
		//购物车商品添加成功 true
		//alert ("购物车商品"+result);
	    openTipsCart(result);
		//跳转到购物车页面
		//window.location.href="/plus/cart.php"; 
	}
	else
	{
		//购物车商品添加失败
		//alert(result);
	}
	//openTipsDiv("loginTips");
}
//ajax弹出层
//打开购物车提示层
function openTipsCart(result)
{
	openTipsDiv("cartTips");
	closebtnEvent("cartTips");
   $(function(){
		$("#tips_btn_yes").click(function(){
			//alert("确定了"+jobs_id);
			//addToBaoming2(jobs_id);							 
		});
		//$("#_money").html("￥"+money+".00");
		$("#tips_content").empty();
		$("#tips_content").append('<p class="pd_l tc f14">'+result+'</p>');
	});
}

//打开登陆弹出层
function openTipsLogin()
{
	openTipsDiv("loginTips");
	$(function(){
		$("#flclose").click(function(e){
 			if(e&&e.preventDefault){
 				e.preventDefault();
 			} else {
 				window.event.returnValue = false;
 			}
 			$("#loginTips").hide();
			$("#loginTips #ajaxusername,#ajaxpassword").val('');//清空所有登录信息
			//$("#loginTips").remove();
			//$("select").css("visibility","visible");
			closeTipsDiv("loginTips");
        })
 		$("#fclosebtn").click(function(e){
 			$("#flclose").click();
 		})
	});
}
//ajax登陆
function ajaxlogin()
{
	var username = document.getElementById("ajaxusername").value;
	var password = document.getElementById("ajaxpassword").value;
	$.post(ajaxurl,{action: "login", userid: ""+ username+"",pwd:""+password+"" }, loginResponse);
}

function loginResponse(result){
	if (result == "ok")
	{
	    closeTipsDiv("loginTips");
		reurl();
	}
	else
	{
	   $("#loginerrdiv").css("display","block");
	   $("#loginerrmsg").empty();
	   $("#loginerrmsg").append(result);
	}
}
////图层类型html
function TipsHtmlToJS(type)
{
	var HTMLJS="";
	switch (type)
	{
		case "loginTips":
		//登陆图层
		HTMLJS = '<div class=\"tips_b\">'+
		'<div class=\"tips_box\">'+
		'   <div class=\"tips_title\">'+
		'      <h2>User Login</h2>'+
		'      <span class=\"close\"><a href=\"javascript:void(0);\" id=\"flclose\">Close</a></span>'+
		'   </div>'+
		'   <div class=\"tips_text\">'+
		'      <div class=\"dl_tips\" id=\"loginerrdiv\" style=\"display:none;\">'+
		'        <b class=\"dl_err\"></b><span id=\"loginerrmsg\"></span>'+
		'      </div>'+
		'      <table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"dl_tbl\" id=\"loginformtable\">'+
		'         <tr><td>User Name:</td><td colspan="2"><input type=\"text\" class=\"tips_txt\" id=\"ajaxusername\"></td></tr>'+
		'         <tr><td>Password:</td><td><input type=\"password\" class=\"tips_txt\" id=\"ajaxpassword\"></td><td class=\"t_ar\"></td></tr>'+
		'         <tr><td></td><td colspan=\"2\"><input type=\"button\" value=\"Sign in\" onclick=\"ajaxlogin()\" class=\"btn_Dora_m\" id=\"floginbtn\" />&nbsp;&nbsp;<a href=\"'+weburl+'/member/index_do.php?fmdo=user&dopost=regnew\" target=\"_blank\" class="public_Lblue" tabindex=\"-1\"><b>Join Free</b></a></td></tr>'+
		'         <tr><td colspan="3" class="t_ar">Pls register or Sign in before shopping</td></tr>'+
		'</table></div></div></div>';
		break;
		case "cartTips":
		HTMLJS = '<div class="tips_b">'+
		      '<div class=\"tips_box\">'+
			     '<div class=\"tips_title\">'+
				    '<h2>A Best Jewelry Tips</h2>'+
					'<span class=\"close\"><a href=\"javascript:void(0)\" id=\"flclose\">Close</a></span>'+
				 '</div>'+
				  '<div class=\"tips_text\" id=\"tips_content\">'+
					 '<p class=\"pd_l tc f14\">This product is already in your shopping cart.<br/>Shopping Cart <span class=\"fI b\">1</span> item Total: <span class=\"fI b\">$ 12.00 </span></p>'+
				  '</div>'+
					'<div class=\"tips_botton\">'+
					'<a href="/plus/cart.php"><img src=\"'+weburl+'/skin/images/bt_cart.jpg\" alt=\"View Cart\"></a>&nbsp;'+
					'<a href=\"javascript:void(0)\" id=\"tips_btn_no\"><img src=\"'+weburl+'/skin/images/bt_buy.jpg\" alt=\"Continue Shopping\"></a>'+
				'</div></div></div>';
		break;
		//普通弹出层
		case "tips_box":
		 HTMLJS = '<div class="tips_b">'+
		  '<div class=\"tips_box\" style="width: 320px;">'+
		    '<div class=\"tips_title\">'+
			  '<h2 style=\"width: 240px;\">温馨提示</h2>'+
			   '<span class=\"close\"><a href=\"javascript:void(0)\" id=\"flclose\">关闭</a></span>'+
			   '</div>'+
			   '<div class=\"tips_text\" style="padding: 24px 5px; text-align: center;" id=\"tips_box_content\">'+
			   '<div class=\"tips_sbt\" id=\"tips_btn\">'+
                       '<input class="btn_Lblue_m" value="确定" id="tips_fclosebtn" type="button">/>'+
				'</div></div></div>';
		break;
		//充值弹出层
		case "fundTip":
		   HTMLJS = '<div class="tips_b">'+
		               '<div class=\"tips_box\">'+
					    '<div class=\"tips_title\">'+
                           '<h2>Please recharge!</h2>'+
						   '<span class=\"close\"><a href=\"javascript:void(0)\" id=\"flclose\">Close</a></span>'+
						 '</div>'+
						'<div class=\"tips_text\" id=\"tips_content\">'+
						'<p class=\"pd_l tc f14\">Your account balance is not enough for payment. <font class=\"red\">Please recharge!</font></p></div>'+
						'<div class=\"tips_sbt\" id=\"tips_btn_box\">'+
                        '<input type=\"button\" value=\"Favorites\" class=\"btn_Lblue_b\" id=\"tips_btn_no\" />&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\"Recharge\" class=\"btn_Dora_b\" id=\"tips_btn_yes\" />'+
				 '</div></div></div>';
		break;
		case "buyTip":
		//-确认报名弹出层
		HTMLJS = '<div class=\"tips_b\">'+
		      '<div class=\"tips_box\">'+
			    '<div class=\"tips_title\">'+
                '<h2>Order Confirm</h2>'+
                '<span class=\"close\"><a href=\"javascript:void(0)\" id=\"flclose\">Close</a></span>'+
                '</div>'+
           '<div class=\"tips_text\" id=\"tips_content\">'+
               '<p class=\"pd_l tc f14\">Charge <span class=\"red\" id="_money">$0.00</span> from your account balance!</p></div>'+
            '<div class=\"tips_sbt\" id=\"tips_btn_box\">'+
               '<input type=\"button\" value=\"Cancel\" class=\"btn_Lora_b\" id=\"tips_btn_no\" /><input type=\"button\" value=\"Pay Now\" class=\"btn_Dora_b\" id=\"tips_btn_yes\" />'+
            '</div></div></div>';
		break;
		case "successTip":
		HTMLJS = '<div class=\"tips_b\">'+
		'<div class=\"tips_box\">'+
		  '<div class=\"tips_title\">'+
		     '<h2>报名成功</h2>'+
		     '<span class=\"close\"><a href=\"javascript:void(0)\" id=\"flclose\">关闭</a></span>'+
		   '</div>'+
		   '<div class=\"tips_info\" id=\"tips_content\">'+
		     '<div class="icon_suc">'+
			   '<div class="txt_suc">您好，恭喜您报名成功!</div>祝您找到好工作！</div>'+
		       '<div class="suc_link">您还可以选择：<a href="javascript:void(0)" id="tips_btn_back">返回购买</a> | <a href="/">返回首页</a><br>查看我的帐户：<a href="/member/" target="_blank">帐户明细</a> | <a href="//member/operation.php" target="_blank">Buying Reminders</a> | <a href="/member/buy.php?do=card" target="_blank">在线充值</a></div>'+
			 '</div></div>'+
			 '<div class=\"tips_sbt\" id=\"tips_btn_box\">'+
			 '<input value="关　闭" class="btn_Lora_b" id="tips_btn_close" type="button">'+
			 '</div></div></div>';
		break;
		default:
		break;
	}
	return HTMLJS;
}
//
//// 关闭mask和弹出图层
function closeTipsDiv(type)
{
	var _id = type;
   //document.body.removeChild(document.getElementById("speDiv"));
	document.body.removeChild(document.getElementById(_id));
	document.body.removeChild(document.getElementById('mask'));
	document.body.removeChild(document.getElementById('iframe'));
	
	var i = 0;
	var sel_obj = document.getElementsByTagName('select');
	while (sel_obj[i])
	{
		sel_obj[i].style.visibility = "";
		i++;
	}
}
// 关闭事件
function closebtnEvent(_target)
{
	//关闭按扭
	$("#flclose").click(function(e){
		if(e&&e.preventDefault){
			e.preventDefault();
		} else {
			window.event.returnValue = false;
		}
		$("#"+_target).hide();
		closeTipsDiv(_target);
		//$("#baomingTip").hide();
		//closeTipsDiv("baomingTip");
	});
	//取消按扭
	$("#tips_btn_no").click(function(e){
		$("#flclose").click();
	})
}
////弹出层
function openTipsDiv(divtype)
{
	//var _id = "speDiv";
	var _id = divtype;
	var m   = "mask";
	var i   = "iframe";
	if (document.getElementById(_id)) document.removeChild(document.getElementById(_id));
	if (document.getElementById(m)) document.removeChild(document.getElementById(m));
	//计算上卷元素值
	var scrollPos;
	if (typeof window.pageYOffset != 'undefined')
	{
		scrollPos = window.pageYOffset;
	}
	else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
	{
		scrollPos = document.documentElement.scrollTop;
	}
	else if (typeof document.body != 'undefined')
	{
		scrollPos = document.body.scrollTop;
	}
	
	var iframe = document.createElement("iframe");
	iframe.id = i;
	//iframe.style.border="1px solid #59B0FF";
	iframe.style.position = "absolute";
	iframe.style.zIndex = "50";
	iframe.style.top = (parseInt(scrollPos + 200)) + "px";
	iframe.style.left = (parseInt(document.body.offsetWidth) - 360) / 2 + "px"; // 屏幕居中
	iframe.style.width = "350px";
	iframe.style.height = "auto";
	document.body.appendChild(iframe);

	// 新激活图层
	var newDiv = document.createElement("div");
	newDiv.id = _id;
	newDiv.style.position = "absolute";
	newDiv.style.zIndex = "10000";
	newDiv.style.width = "360px";
	newDiv.style.height = "auto";
	newDiv.style.top = (parseInt(scrollPos + 200)) + "px";
	newDiv.style.left = (parseInt(document.body.offsetWidth) - 360) / 2 + "px"; // 屏幕居中
	newDiv.style.overflow = "auto";
	newDiv.style.background = "#FFF";
	newDiv.className = "tips_m";
	//newDiv.style.border = "3px solid #59B0FF";
	//newDiv.style.padding = "5px";
//	//生成层内内容
	newDiv.innerHTML = TipsHtmlToJS(divtype);
	//获取弹层内容代码
	//var newDiv = TipsHtmlToJS(divtype);
	//激活弹层图层
	document.body.appendChild(newDiv);
	
	// mask图层
	var newMask = document.createElement("div");
	newMask.id = m;
	newMask.style.position = "absolute";
	newMask.style.zIndex = "9999";
	newMask.style.width = document.body.scrollWidth + "px";
	newMask.style.height = document.body.scrollHeight + "px";
	newMask.style.top = "0px";
	newMask.style.left = "0px";
	newMask.style.background = "#FFF";
	newMask.style.filter = "alpha(opacity=30)";
	newMask.style.opacity = "0.40";
	document.body.appendChild(newMask);
}

//刷新页面
function reurl(){
	//url = location.href; //把当前页面的地址赋给变量 url
	//self.location.replace(url); 
	window.location.reload();
}
//搜索
function checkSearch(){
/*	if(document.formsearch.searchtype.value=="")
	document.formsearch.action="http://www.google.cn/custom"
	else
	document.formsearch.action="/plus/search.php"*/
	if($('#search-keyword').val()=="" || $('#search-keyword').val()=="Enter the Item No.")
	{
		$('#search-keyword').focus();
		alert("Please enter the Item No.");
		return false;
	}
}
//
$(function(){
		//文本框Style
/*		$("#brandSearch").change(function(){
			window.location.href="/plus/searchbrand.php?brand="+$("#brandSearch").val(); 
		});*/
		var tm = $("#topmenuMenu");
		var tb = $("#topmenuBody");
		$("#topDropmenu").hover(function() {
			tm.removeClass();
			tm.addClass("menu_on");
			tb.show();
		},
		function() {
			tm.removeClass();
			tm.addClass("menu_no");
			tb.hide();
		});
		//
		$("#searchIntelligent").mouseover(function(){
		   $("#homePopSearch").show();
        });
		$("#searchIntelligent").mouseout(function(){
		   $("#homePopSearch").hide();	
		   //$("#homePopSearch").css("display","none");
        });
		$("#homePopSearch").mouseover(function(){
		   $("#homePopSearch").show();									   
        });
		$("#homePopSearch").mouseout(function(){
		   $("#homePopSearch").hide();									   
        });
		//
 })

function openkefu(url)
{
	//window.open(url,"_blank","height=473,width=703,top=200,left=200,status=yes,toolbar=no,menubar=no,resizable=yes,scrollbars=no,location=no,titlebar=no");
	window.open(url,"inquiry","height=473,width=703,top=200,left=200,status=yes,toolbar=no,menubar=no,resizable=yes,scrollbars=no,location=no,titlebar=no");
}
function addBookmark()
{
   if (document.all)
   {
      window.external.addFavorite('http://www.abestjewelry.com','A Best Jewelry From China');
   }
   else if (window.sidebar)
   {
      window.sidebar.addPanel('A Best Jewelry From China', 'http://www.abestjewelry.com', "");
   }
} 

/*****************字串处理**********************/
// Trim() , Ltrim() , RTrim()   
String.prototype.Trim = function()   
{   
  return this.replace(/(^\s*)|(\s*$)/g, "");   
}   
String.prototype.LTrim = function()   
{   
  return this.replace(/(^\s*)/g, "");   
}   
String.prototype.RTrim = function()   
{   
  return this.replace(/(\s*$)/g, "");   
} 

//实现小数点后指定精确位数四舍五入
function forDight(Dight,How) {
   Dight = Math.round(Dight*Math.pow(10,How))/Math.pow(10,How);
   return Dight;
}
