RECENT_VISIT_GOODS = "addTheGoodsForYou";


/** 当显示商品图片时，找不到图片，则去掉图片上的链接 */
function imageNotFound(id, srcImage ,url) {
    
    srcImage.onerror=null;
    
    srcImage.src='/images/notfound.jpg';
    
	var image = $("image_" + id);
//	var zoom_image = $("zoom_image_" + id);
	
//	image.href="javascript:void(0);";
//	image.href="/goods_info.action?goodsid="+id;
	image.href = url;
	image.target="";
	
//	zoom_image.hide();
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/**
 * 当访问商品时，记录访问情况
 */			
function addTheGoodsForYou(id, name,memberPrice) {
	
	var days = 1;
	
	var s = readCookie( RECENT_VISIT_GOODS );
	
	if ( s == null ) 
		createCookie(RECENT_VISIT_GOODS, encodeURIComponent(id+"|"+name+"|"+memberPrice), days);
	else {
		var recents = decodeURIComponent(s);
		var notfound = true;
		var g = recents.split("!");
		for (var k=0; k<g.length; k++) {
			if ( g[k] != null ) {
				var value = g[k].split("|");
				var existId = value[0];
				var existname = value[1];
				var existPrice = value[2];
				if ( existId == id ) { 
					var cou = g.length-1-k ;
					if(cou<10){notfound = false;}
				}
			}
		}
		
		if ( notfound )
			createCookie(RECENT_VISIT_GOODS, s + "!" + encodeURIComponent(id+"|"+name+"|"+memberPrice), days);
	}
}

/**
 * 清除最近访问商品的列表
 */
function cleanRecentVisit(){
	eraseCookie( RECENT_VISIT_GOODS );
	var rvg = $("recent_visit_goods");
	if ( rvg ) {
	   rvg.innerHTML = "";
	   if ( rvg.parentNode ) {
	       rvg.parentNode.removeChild( rvg );
	   }
	}
}

/**
 * 显示最近访问商品的列表
 */
function displayRecentVisitGoods() {
	
	var s = readCookie( RECENT_VISIT_GOODS );
	if ( s != null ) {
		document.writeln("<div class=\"app01\" id=\"recent_visit_goods\"></div>");
		
		var h1 = document.createElement("h1");
//		h1.className="tit02";

//您最近浏览过的商品 [<a href="#">清空</a>]

		h1.innerHTML="<strong>浏览记录</strong><em>Recent History</em>";
		
		s = decodeURIComponent(s);
		
		
		var ul = document.createElement("ul");
		
		var g = s.split("!");
		var i = g.length - 10;
		
		for ( var k =0; k < g.length-i ; k++) {
			if ( g[k] != null ) {
				var cou = g.length-1-k ;
				var idname = g[ cou ].split("|");
				var p = idname[2];
				var plist = p.split(".");
				if ( idname != null && idname.length == 3) {
					
					var li = document.createElement("li");
					li.innerHTML="<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"p_rh\"><tr><td rowspan=\"2\">" +
					"<a id=\"image_"+ idname[0] + "\" href=\"/goods_info.action?goodsid="+ idname[0] + "\"><img src=\"/goodsimage/"+idname[0]+"_s4.gif\"  width=\"70\" height=\"70\" id=\"image_"+idname[0]+"\" onerror=\"imageNotFound(\'"+idname[0]+"\', this,\'/goods_info.action?goodsid="+ idname[0] + "\')\"/></td><th valign=\"top\"></a>" 
									+"<a href=\"/goods_info.action?goodsid="+ idname[0]
							+ "\">" + idname[1] + "</a></th> </tr><tr><td>￥<span class=\"jg\">"+plist[0]+"</span></td></tr></table>";
					ul.appendChild( li );
				}
			}
		}
		var clear_div = document.createElement("div");
		clear_div.className = "clear";
		
		$("recent_visit_goods").appendChild( h1 );
		$("recent_visit_goods").appendChild( ul );
		$("recent_visit_goods").appendChild( clear_div );
		
		document.writeln("<div class=\"clear\"></div>");
	}
}


function gourl( prefix ) {
	var k = document.getElementById("p_keyword");
	var price_l = document.getElementById("price_l").value;
	
	if ( price_l != "" && isNaN( parseFloat( price_l ) )) {
		alert("价格范围输入错误！");
		document.getElementById("price_l").focus();
		return;
	}
	var price_u = document.getElementById("price_u").value;
	if ( price_u != "" && isNaN( parseFloat( price_u ) )) {
		alert("价格范围输入错误！");
		document.getElementById("price_u").focus();
		return;
	}
	
	var url = prefix + "&keyword=" + k.value+"&price_l="+price_l+"&price_u="+price_u;
	this.location.href = url;
}

function login_validate(){
	
   if($('username_login').value == null || $('username_login').value == ''){
	   
//		alert('请输入会员帐号！');
	   $('usernamevalidatelogin').show();
		$('username_login').focus();
		return false;
	}
	if($('password_login').value == null || $('password_login').value == ''){
//		alert('请输入密码！');
		$('passwordvalidatelogin').show();
		$('password_login').focus();
		return false;
	}
	if($('logininputCode').value == null || $('logininputCode').value == ''){
//		alert('请输入密码！');
		$('logininputCode').show();
		$('logininputCode').focus();
		return false;
	}
		
}


function compareChecked(targetURL){
      
       this.location = targetURL ;
      
}

function ckeckedOrNot(cid,nid){
	var goodsId = $(cid).value;
	var checked = $(cid).checked;
	new Ajax.Request('goods_check.action', 
	{
	method: 'get',
	parameters: {goodsId:goodsId,checked:checked},
	onSuccess: function(transport) { try{ onRefreshGoodsSuccess(transport,  $('input_goods') ); } catch(e){ alert('加载待比较列表错误')};  $("ajax_loader").hide();},
	onLoading: function() { $("ajax_loader").show(); },
	onFailure: function(){ alert('取得待比较列表时错误');  $("ajax_loader").hide(); }
	});
}

function addToCompareBox(gid){

if ( $("ul_compare") ) {
	var lis = $("ul_compare").getElementsByTagName("li");
	var len = lis.length;
	if ( len >= 6 ) {
		alert("对比栏中的商品不能超过6个!");
		return false;
	}
}

new Ajax.Request('add_compareBox.action',
{
	method: 'get',
	parameters: {gid:gid},
	onSuccess: function(transport) { try{ onRefreshGoodsSuccess(transport,  $('input_goods') ); } catch(e){ alert('加载待比较列表错误')};  $("ajax_loader").hide();},
	onLoading: function() { $("ajax_loader").show(); },
	onFailure: function(){ alert('取得待比较列表时错误');  $("ajax_loader").hide(); }
});
}


function onRefreshGoodsSuccess(transport,dest){
	if ( $('input_goods') ) {
		$('input_goods').innerHTML = transport.responseText;
	}
}

function delFromCompareBox(goodsId){
new Ajax.Request('delete_compareBox.action',
{
	method:'get',
	parameters:{gid:goodsId},
	onSuccess: function(transport) { try{ onRefreshGoodsSuccess(transport,  $('input_goods') ); } catch(e){ alert('加载待比较列表错误')};},
	
	onFailure: function(){ alert('取得待比较列表时错误');  $("ajax_loader").hide(); }
	});
}