function checkSearchForm(form){
	if(form.s.value=='0' && form.w.value==''){
		alert("请选择分类或填写关键词");
		return false;
	}
	return true;
}
function switchPic(data,seconds) {
	this.picData = data;
	this.picIndex = 0;
	this.cnt = this.picData.length;
	this.oImgs = [];
	this.oLink = document.getElementById("ASCLink");
	this.oBtnbox = document.getElementById("ASCBtnBox");
	this.oBtns = {};
	this.interval = seconds;
	this.iID = 0;
	this.loadPic();
}
switchPic.prototype.loadPic = function(){
	var index_i = this.cnt;
	for (var data in this.picData) {
		var newimg = document.createElement("img");
		newimg.src = this.picData[data]['pic'];
		newimg.alt = this.picData[data]['title'];
		newimg.id = "ASCImg";
		var self = this;
		newimg.onmouseover=function(){
			clearInterval(self.iID);
		}
		newimg.onmouseout=function(){
			self.autoCut();
		}
		this.oImgs[this.cnt-index_i] = newimg;		

		var newBtn = document.createElement("div");
		newBtn.className = index_i == 1?"on":"off";

		newBtn.appendChild(document.createTextNode(index_i));

		newBtn.onmouseover=function(){
			num = this.innerHTML;
			self.toPic(num);
		}
		newBtn.onmouseout=function(){
			self.autoCut();
		}
		this.oBtns[index_i-1] =newBtn;
		this.oBtnbox.appendChild(newBtn);
		var theNum = '1';

		index_i--;
	}
}
switchPic.prototype.autoCut = function(){
	var cut = this;
	this.iID = setInterval(function(){cut.nextPic();}, cut.interval*1000);
}
switchPic.prototype.nextPic = function(){
	var theimg = document.getElementById("ASCImg");
	this.oBtns[this.picIndex].className="off";
	this.picIndex = (this.picIndex+1)%this.cnt;
	this.oBtns[this.picIndex].className="on";
	theimg.parentNode.replaceChild(this.oImgs[this.picIndex], theimg);
	this.oLink.href = this.picData[this.picIndex]['link'];
	this.oLink.setAttribute('title',this.picData[this.picIndex]['title']);
}
switchPic.prototype.toPic = function(index){
	var self = this
	clearInterval(self.iID);
	var theimg = document.getElementById("ASCImg");
	this.oBtns[this.picIndex].className="off";
	this.picIndex = (index-1)%this.cnt;
	this.oBtns[this.picIndex].className="on";
	theimg.parentNode.replaceChild(this.oImgs[this.picIndex], theimg);
	this.oLink.href = this.picData[this.picIndex]['link'];
	this.oLink.setAttribute('title',this.picData[this.picIndex]['title']);
}

function showSubclassManu(obj){
	obj.parentNode.className="over";
}
function shutdownSubclassManu(obj){
	obj.parentNode.className="";
}
function subVote(form) {
	var items = form.vote4NewYear;
	var flag = false;
	for(var i=0;i<items.length;i++){
		if (items[i].checked){
			flag = true;
			break;
		}
	}
	if (!flag){
		alert("请先选择投哪一项");
		return false;
	}
	var val = items[i].value

	vote(1, 1, val);
	return false;
}
function vote(voteid, voteobj, voteitem) {
	var x = new Ajax('statusid', 'HTML');
	x.get('/files/votewrite.php?voteid='+voteid+'&voteobj='+voteobj+'&voteitem='+voteitem, function(s) {
		if (s=='repeat') {
			alert("您已经投过票，系统不再重复计票，谢谢您的参与");
		} else if (s=='success') {
			alert("投票成功，谢谢您的参与");
		} else {
			alert("投票失败，请重试");
		}
	});
}
function switchTab(id_num,num){
	for(var i = 0;i <= 9;i++){
		if(GetObj(id_num + i)){GetObj(id_num + i).className = '';}

		if(GetObj(id_num+'_c_'+ i)){GetObj(id_num +'_c_'+ i).style.display = 'none';}
	}
	if(GetObj(id_num + num)){GetObj(id_num + num).className = 'selectd';}
	if(GetObj(id_num +'_c_'+ num)){GetObj(id_num +'_c_'+ num).style.display = 'block';}
}
function GetObj(objName){
	if(document.getElementById){
		return eval('document.getElementById("' + objName + '")');
	}else{
		return eval('document.all.' + objName);
	}
}
function changeStyle(obj) {
	var lis=obj.parentNode.getElementsByTagName('LI');
	for(var key in lis) {
		lis[key].className="";
	}
	obj.className="first";
}
function changeStyleBig(obj) {
	var lis=obj.parentNode.getElementsByTagName('LI');
	for(var i=0;i<lis.length;i++) {
		lis[i].className="";
		var tmpobj = lis[i].getElementsByTagName('DIV');
		tmpobj[0].style.display='none';
		tmpobj = lis[i].getElementsByTagName('A');
		tmpobj[2].style.display='';
		lis[i].style.height='';
	}
	obj.className="first";
	obj.style.height="65px";
	tmpobj = obj.getElementsByTagName("DIV");
	tmpobj[0].style.display='block';
	tmpobj = obj.getElementsByTagName('A');
	tmpobj[2].style.display='none';
}
function changeStyleBig2(obj) {
	var lis=obj.parentNode.getElementsByTagName('LI');
	for(var i=0;i<lis.length;i++) {
		lis[i].className="";
		var tmpobj = lis[i].getElementsByTagName('DIV');
		tmpobj[0].style.display='none';
		tmpobj = lis[i].getElementsByTagName('A');
		tmpobj[2].style.display='';
		lis[i].style.height='';
	}
	obj.className="first";
	obj.style.height="40px";
	tmpobj = obj.getElementsByTagName("DIV");
	tmpobj[0].style.display='block';
	tmpobj = obj.getElementsByTagName('A');
	tmpobj[2].style.display='none';
}
function loadform(form,thedata) {
	this.objs = form.elements;
	this.data = thedata;
	for (var i=0;i<this.objs.length;i++){
		if (this.data[this.objs[i].name] != null) {
			this.setFormValue(this.objs[i]);
		}
	}

}

loadform.prototype.setFormValue = function (obj){
	if (obj.tagName=='SELECT') {
		for (var i=0;i<obj.options.length;i++) {
			if (obj.options[i].value == this.data[obj.name]) obj.options[i].selected=true;
		}
	} else if (obj.tagName=='TEXTAREA') {
		obj.value=this.data[obj.name];
	} else {
		var type=obj.getAttribute('TYPE');

		switch(type) {
			case 'text':
			case 'hidden':
			obj.value=this.data[obj.name];
			break;
			case 'radio':
			case 'checkbox':
			obj.checked=obj.value == this.data[obj.name];
			break;
			default:break;

		}
	}
}
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
function GetCookieVal(offset)
//获得Cookie解码后的值
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function SetCookie(name, value)
//设定Cookie值
{
	var expdate = new Date();
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : "/";
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
	document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
	+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
	+((secure == true) ? "; secure" : "");
}

function GetCookie(name)
//获得Cookie的原始值
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		var xxx = document.cookie.substring(i, j);
		if (xxx == arg)
		return GetCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}
//删除Cookie
function DelCookie(name){
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}
function goSignIn(){
	SetCookie('burl',encodeURIComponent(location.href),24*3600,'/','.esmb.com.cn');
	location.replace("http://shop.esmb.com.cn/files/sign.php");
}

function addCart(pid){
	if (pid) {
		var flag = false;
		var cart = GetCookie('cart');
		if (cart) {
			var goods = cart.split('|');
			var proid;
			var pro = [];

			for(var key in goods){
				proid = parseInt(goods[key].split(':')[0]);
				if (proid == pid) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				SetCookie('cart',cart+'|'+pid+':1',24*3600*30,'/','shop.esmb.com.cn');

			} 
		} else {
			SetCookie('cart',pid+':1',24*3600*30,'/','shop.esmb.com.cn');
		}
	}
	SetCookie('burl',encodeURIComponent(location.href),24*3600,'/','.esmb.com.cn');
	location.href="http://shop.esmb.com.cn/files/shopcart.php";
}
//添加商品+延保服务
function addServeCart(pid,sid){
	if (pid && sid) {
		var flag = false;
		var cart = GetCookie('cart');
		if (cart) {
			var goods = cart.split('|');
			var proid;
			var proidNum = 0;
			var pro = [];
			for(var key in goods){
				proid = parseInt(goods[key].split(':')[0]);
				if (proid == pid) {
					proidNum = parseInt(goods[key].split(':')[1]);
					flag = true;
					break;
				}
			}
			if (!flag) {
				SetCookie('cart',cart+'|'+pid+':1',24*3600*30,'/','shop.esmb.com.cn');
			}
			var sflag = false;
			var scart = GetCookie('scart');
			if (scart) {
				var sgoods = scart.split('|');
				var sproid;
				var spro = [];
				for(var skey in sgoods){
					sproid = parseInt(sgoods[skey].split(':')[0]);  //产品id
					sproid2 = parseInt(sgoods[skey].split(':')[1]);  //延保id
					if (sproid == pid) {
						if (sproid2 == sid) {
							sflag = true;
							break;
						}
					}
				}
				if (!sflag) {
					SetCookie('scart',scart+'|'+pid+':'+sid+':1',24*3600*30,'/','shop.esmb.com.cn');
					var pidNum = proidNum+1;
					var newcart = comma = '';
					var cart = GetCookie('cart');
					var sgoods = cart.split('|');
					for(var proinfo in sgoods){
						pida = parseInt(sgoods[proinfo].split(':')[0]);
						newcart += pid != pida ? comma+sgoods[proinfo] : comma+pid+':'+pidNum;				
						comma = '|';
					}
					SetCookie('cart',newcart,24*3600*30,'/','shop.esmb.com.cn');
				}
			} else {
				SetCookie('scart',pid+':'+sid+':1',24*3600*30,'/','shop.esmb.com.cn');
			}
		} else {
			SetCookie('cart',pid+':1',24*3600*30,'/','shop.esmb.com.cn');
			SetCookie('scart',pid+':'+sid+':1',24*3600*30,'/','shop.esmb.com.cn');
		}
	}
	SetCookie('burl',encodeURIComponent(location.href),24*3600,'/','.esmb.com.cn');
	location.href="http://shop.esmb.com.cn/files/shopcart.php";
}
function checkRadioChecked(formid,radioname){
	var form=document.getElementById(formid);
	var radios = getElementsByName_iefix("INPUT", radioname);
	var flag = false;
	for(var key in radios) {
		if (radios[key].checked) {
			flag = true;
			break;
		}
	}
	return flag;
}
function getElementsByName_iefix(tag, name) {
	var elem = document.getElementsByTagName(tag);
	var arr = new Array();
	for(i = 0,iarr = 0; i < elem.length; i++) {
		att = elem[i].getAttribute("name");
		if(att == name) {
			arr[iarr] = elem[i];
			iarr++;
		}
	}
	return arr;
}
function cartNum(){
	var cart = GetCookie('cart');
	var scart = GetCookie('scart');
	var num = 0;
	if (cart) {
		var goods = cart.split('|');
		for(var key in goods){
			num += parseInt(goods[key].split(':')[1]);
		}
	}
	if (scart) {
		var goods_f = scart.split('|');
		for(var key_f in goods_f){
			num += parseInt(goods_f[key_f].split(':')[2]);
		}
	}
	if (document.getElementById('ProductNum')){
		document.getElementById('ProductNum').innerHTML=num;
	}
}
function goBalance(){
	var cart = GetCookie('cart');
	var num = 0;
	if (cart) {
		location.href="http://shop.esmb.com.cn/files/balance.php";
	} else {
		alert("您的购物车中没有商品");
	}
}
function addFavorite(pid){
	window.open("http://shop.esmb.com.cn/files/myfavorite.php?pid[]="+pid+"&a=add","Favorite");
}
function formatnumber(fnumber,fdivide,fpoint,fround){
	var fnum = fnumber + '';
	var revalue="";

	if(fnum==null){
		for(var i=0;i<fpoint;i++)revalue+="0";
		return "0."+revalue;
	}
	fnum = fnum.replace( /^\s*|\s*$/g, '');
	if(fnum==""){
		for(var i=0;i<fpoint;i++)revalue+="0";
		return "0."+revalue;
	}

	fnum=fnum.replace(/,/g,"");

	if(fround){
		var temp = "0.";
		for(var i=0;i<fpoint;i++)temp+="0";
		temp += "5";

		fnum = Number(fnum) + Number(temp);
		fnum += '';
	}

	var arrayf=fnum.split(".");

	if(fdivide){
		if(arrayf[0].length>3){
			while(arrayf[0].length>3){
				revalue=","+arrayf[0].substring(arrayf[0].length-3,arrayf[0].length)+revalue;
				arrayf[0]=arrayf[0].substring(0,arrayf[0].length-3);
			}
		}
	}
	revalue=arrayf[0]+revalue;

	if(arrayf.length==2&&fpoint!=0){
		arrayf[1]=arrayf[1].substring(0,(arrayf[1].length<=fpoint)?arrayf[1].length:fpoint);

		if(arrayf[1].length<fpoint)
		for(var i=0;i<fpoint-arrayf[1].length;i++)arrayf[1]+="0";
		revalue+="."+arrayf[1];
	}else if(arrayf.length==1&&fpoint!=0){
		revalue+=".";
		for(var i=0;i<fpoint;i++)revalue+="0";
	}

	return revalue;
}

//添加一个历史记录
function addHistory(id, name, url, imgurl){
	var historys = getHistory();
	//先检查是否已经加入
	for(var i=0; i<historys.length; i++){
		if(historys[i].id == id){
			return;
		}
	}
	//如果超过5个,删除最后一个
	if(historys.length > 5){
		historys.pop();
	}
	//加入
	historys.unshift({id:id, name:name, url:url, imgurl:imgurl});
	//保存
	saveHistory(historys);
}
//获取历史记录,以|分隔记录,以,分隔属性,分别为id, name, url, imgurl
function getHistory(){
	var history = [];
	var cookie = GetCookie('history');
	if(cookie){
		var items = cookie.split("|");
		for(var i=0; i<items.length; i++){
			var values = items[i].split(",");
			history.push({id:values[0], name:values[1], url:values[2], imgurl:values[3]});
		}
	}
	return history;
}
function saveHistory(historys){
	var cookie;
	var item, itemcookie = [];
	for(var i=0; i<historys.length; i++){
		item = historys[i];
		itemcookie.push(item.id + ',' + item.name.replace(",", '').replace("|", '') + ',' + item.url + ',' + item.imgurl);
	}
	cookie = itemcookie.join("|");;
	SetCookie('history', cookie, 3600*24*7, '/', '.esmb.com.cn');
}
//显示浏览记录
function displayHistory(){
	var historys = getHistory(), html = '';
	//return;
	for(var i=0; i<historys.length; i++){
		var h = historys[i];
		html += '<li><dl><dt><a href="'+h.url+'" target="_blank"> <img class="Picture50" src="'+h.imgurl+'"/> </a></dt><dd class="p_Name"><a href="'+h.url+'" target="_blank">'+h.name+'</a></dd></dl></li>';
		
	}
	var ul = document.getElementById("historypanel");
	if(ul){
		ul.innerHTML = html;
	}
}
function shw(id){
	if(document.getElementById(id)){
		document.getElementById(id).style.display='';	
	}
}
function hid(id){
	if(document.getElementById(id)){
		document.getElementById(id).style.display='none';	
	}
}
function goBack(){
	var burl = GetCookie('burl');	
	burl = (!burl) ? 'http://shop.esmb.com.cn/' : decodeURIComponent(burl);
	location.href=burl;
}