function getbyid(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.all) {
		return document.all[id];
	} else if (document.layers) {
		return document.layers[id];
	} else {
		return null;
	}
}

function setTab(area,id) {
	var tabArea=document.getElementById(area);

	var contents=tabArea.childNodes;
	for(i=0; i<contents.length; i++) {
		if(contents[i].className=='tabcontent'){contents[i].style.display='none';}
	}
	document.getElementById(id).style.display='';

	var tabs=document.getElementById(area+'tabs').getElementsByTagName('a');
	for(i=0; i<tabs.length; i++) { tabs[i].className='tab'; }
	document.getElementById(id+'tab').className='tab curtab';
	document.getElementById(id+'tab').blur();
}

/*Cookie操作*/
function CreatCookie(sName,sValue){
	var expires = function(){ //Cookie保留时间
		var mydate = new Date();
		mydate.setTime(mydate.getTime + 3*30*24*60*60*1000);
		return mydate.toGMTString();
	}
	document.cookie = sName + "=" + sValue + ";expires=" + expires;
}
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 GetCookie(sName) {//获得Cookie
	var arg = sName + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return GetCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function DelCookie(sName,sValue){ //删除Cookie
	document.cookie = sName + "=" + escape(sValue) + ";expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

//显示工具条
function hidetoolbar() {
	window.parent.document.getElementById("toolbarframe").style.display="none";
}
function showtoolbar() {
	document.getElementById("toolbarframe").style.display = "block";
}

//复制URL地址
function setCopy(_sTxt){
	if(navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
		clipboardData.setData('Text',_sTxt);
		alert ("网址“"+_sTxt+"”\n已经复制到您的剪贴板中\n您可以使用Ctrl+V快捷键粘贴到需要的地方");
	} else {
		prompt("请复制网站地址:",_sTxt); 
	}
}

//加入收藏
function addBookmark(site, url){
	if(navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
		window.external.addFavorite(url,site)
	} else if (navigator.userAgent.toLowerCase().indexOf('opera') > -1) {
		alert ("请使用Ctrl+T将本页加入收藏夹");
	} else {
		alert ("请使用Ctrl+D将本页加入收藏夹");
	}
}

function display(id) {
	dobj = getbyid(id);
	if(dobj.style.display == 'none' || dobj.style.display == '') {
		dobj.style.display = 'block';
	} else {
		dobj.style.display = 'none';
	}
}



//改变文章字体大小
function doZoom(size) {
	getbyid('articlebody').style.fontSize = size+'px';
}
//打印
function doPrint(){
	var csslink = document.getElementsByTagName('link');
	for (i=0; i<csslink.length; i++) {
		if (csslink[i].rel=='stylesheet') {
			csslink[i].disabled=true;
		}
	}

	printCSS = document.createElement("link");
	printCSS.id = 'printcss';
	printCSS.type = 'text/css';
	printCSS.rel = 'stylesheet';
	printCSS.href = siteUrl+'/css/print.css';
	
	var docHead = document.getElementsByTagName('head')[0];
	var mainCSS = csslink[0];
	docHead.insertBefore(printCSS,mainCSS);
	
	var articleTitle = document.getElementsByTagName('h1')[0];
	var cancelPrint = document.createElement("p");
	cancelPrint.id = 'cancelPrint';
	cancelPrint.style.textAlign = 'right';
	cancelPrint.innerHTML = "<a href='javascript:cancelPrint();' target='_self'>返回</a>&nbsp;&nbsp;<a href='javascript:window.print();' target='_self>打印</a>";
	getbyid('articledetail').insertBefore(cancelPrint,articleTitle);
	
	window.print();
}
function cancelPrint() {
	if (printCSS) {
		document.getElementsByTagName('head')[0].removeChild(printCSS);
	}
	
	var csslink = document.getElementsByTagName('link');
	for (i=0; i<csslink.length; i++) {
		if (csslink[i].rel=='stylesheet') {
			csslink[i].disabled=false;
		}
	}

	if (getbyid('cancelPrint')) {
		getbyid('articledetail').removeChild(getbyid('cancelPrint'));
	} 
}

//添加文章中的图片链接
function addImgLink(divID) {
	var msgarea = getbyid(divID);
	if(msgarea) {
		var imgs = msgarea.getElementsByTagName('img');
		for (i=0; i<imgs.length; i++) {
			if (imgs[i].parentNode.tagName.toLowerCase() != 'a') {
				imgs[i].title = '点击图片可在新窗口打开';
				imgs[i].style.cursor = 'pointer';
				imgs[i].onclick = function() { window.open(this.src); }
			}
		}
	}
}

function ctlent(event,id) {
	var form = getbyid(id);
	if (event.ctrlKey && event.keyCode == 13) {
		form.submit();
	}
}

function insertSmilies(smilieid) {
	var src = getbyid('smilie_' + smilieid).src;
	var code = getbyid('smilie_' + smilieid).alt;
	code += ' ';
	AddText(code);
}
function AddText(txt) {
	obj = getbyid('xspace-commentform').message;
	selection = document.selection;
	if(!obj.hasfocus) {
		obj.focus();
	}	
	if(typeof(obj.selectionStart) != 'undefined') {
		var opn = obj.selectionStart + 0;
		obj.value = obj.value.substr(0, obj.selectionStart) + txt + obj.value.substr(obj.selectionEnd);
	} else if(selection && selection.createRange) {
		var sel = selection.createRange();
		sel.text = txt;
		sel.moveStart('character', -strlen(txt));
	} else {
		obj.value += txt;
	}
}
function strlen(str) {
	return (str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}


//显示管理菜单层
function showmanagemenu() {
	var obj = getbyid('xspace-managemenu');
	if(obj.style.display == 'none') {
		obj.style.display = '';
	} else {
		obj.style.display = 'none';
	}
	return false;
}

/**
 * 全选
 */
function checkall(form, prefix, checkall) {
	var checkall = checkall ? checkall : 'chkall';
	for(var i = 0; i < form.elements.length; i++) {
		var e = form.elements[i];
		if(e.name != checkall && (!prefix || (prefix && e.name.match(prefix)))) {
			e.checked = form.elements[checkall].checked;
		}
	}
}
/**
 * 检测是否装了Flash播放器
 */
function _uFlash() {
	var f="-",n=navigator;
	if (n.plugins && n.plugins.length) {
		for (var ii=0;ii<n.plugins.length;ii++) {
			if (n.plugins[ii].name.indexOf('Shockwave Flash')!=-1) {
				f=n.plugins[ii].description.split('Shockwave Flash ')[1];
				break;
			}
		}
	} else if (window.ActiveXObject) {
		for (var ii=10;ii>=2;ii--) {
			try {
				var fl=eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash."+ii+"');");
				if (fl) { f=ii + '.0'; break; }
			} catch(e) {}
		}
	}
	//return f;
	if(f.indexOf("8")!=0 && f.indexOf("9")!=0) {
		alert("您的系统未安装Flash8版本及其以上的Flash播放器无法正常查看相关内容");
	}
}

/**
 * 获取窗口的高度与宽度
 */
function getWindowSize() {
  var winWidth = 0, winHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    winWidth = window.innerWidth;
    winHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    winWidth = document.documentElement.clientWidth;
    winHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    winWidth = document.body.clientWidth;
    winHeight = document.body.clientHeight;
  }
  return {winWidth:winWidth,winHeight:winHeight}
}

function setdefaultpic() {
	var dev = getbyid("subjectpic").value;
	var picobj = document.getElementsByName("picid");
	var checked = false;
	for(var i=0;i<picobj.length;i++) {
		if(dev=="0") {
			picobj[i].checked = true;
			checked = true;
			break;
		} else if(picobj[i].value == dev) {
			picobj[i].checked = true;
			checked = true;
			break;
		}
	}
	if(!checked && typeof picobj[0] == "object") {
		picobj[0].checked = true;
	}
}

function jie(){
	$("#caozuo").fadeOut();
	$(".editp").fadeOut();
	$("#shang1").fadeOut();
	$("#goumai").fadeIn();
	$("#shang2").fadeIn();
}
function checkshou(){
	if($("#name").val()==''){
		alert("请输入真实姓名！");
		$("#name")[0].focus();
		return false;
	}
	if($("#email").val()==''){
		alert("请输入E-mail！");
		$("#email")[0].focus();
		return false;
	}
	if($("#address").val()==''){
		alert("请输入详细地址！");
		$("#address")[0].focus();
		return false;
	}
	if($("#zipcode").val()==''){
		alert("请输入邮政编码！");
		$("#zipcode")[0].focus();
		return false;
	}
	if($("#mobile").val()==''){
		alert("请输入手机号码！");
		$("#mobile")[0].focus();
		return false;
	}
	return true;
}
function checkpoint(){
	var point=$("#point").val();
	$.post("./ajax/user.php",{action:'checkpoint',point:point},
	function(avr){
		if(avr){
			alert("请不要超过你现有的积分！");
			$("#point").val(avr);
		}
	})
}
function rate_text(num){
	if(num==1){
		$('#rate_text').html("差")
	}else if(num==2){
		$('#rate_text').html("一般")
	}else if(num==3){
		$('#rate_text').html("好")
	}else if(num==4){
		$('#rate_text').html("很好")
	}else if(num==5){
		$('#rate_text').html("非常好")
	}
}
function rating(id,num){
	$.post("./ajax/user.php",{action:'rating',id:id,num:num},
	function(avr){
		if(avr=='yi'){
			sAlert('yi');
		}else if(avr=='false'){
			sAlert('ratingfaile');
		}else if(avr!=''){
			//$('#game_ratings_size').html(avr);
			average_rating(id);
			you_rating(id);
			alert("评价成功，谢谢！")
		}
	});
		
}
function you_rating(id){
	$.post("./ajax/user.php",{action:'you_rating',id:id},
	function(msg){
		if(msg){
		$('#myrate').html(msg);
		}
	})
}
function average_rating(id){
	$.post("./ajax/user.php",{action:'average_rating',"gid":id},
	function(msg){
		$('#average_rating').html(msg);
	})
}
function delcar(pid) {
	$.post("./ajax/car.php",{action:'del',pid:pid},
	function(avr){
		if(avr=='login'){
			alert("请先登陆！");
			window.location="login.html";
			return false;
		}else if(avr=='false'){
			alert("删除失败！");
		}else if(avr=='true'){
			getcatnum();
			window.location='cart.php';
		}
	});
	
}
function clearcar() {
	if (confirm("确定要清空当前购物车吗？"))
   {
      $.post("./ajax/car.php",{action:'clear'},
		function(avr){
			if(avr=='login'){
				alert("请先登陆！");
				window.location="login.html";
				return false;
			}else if(avr=='true'){
				getcatnum();
				window.location='cart.php';
			}
		});
   }else {
      return false;
   }
	
}
function getcatnum(){
	$.getJSON("./ajax/user.php",{action:"getcatnum"},
	function(json){
		$('#num_product').html(json.productnum);
		$('#num_money').html(json.money);
	})
}
function editnum(pid) {
	var shopnum= $('#pnum'+pid).val();
	$.post("./ajax/car.php",{action:'edit',pid:pid,num:shopnum},
	function(avr){
		if(avr=='login'){
			alert("购买产品，请先登陆！");
			window.location="login.html";
			return false;
		}else if(avr=='false'){
			alert("购买失败！");
		}else if(avr=='true'){
			getcatnum();
			window.location='cart.php';
		}
	});
}
function addcart(pid) {
	var shopnum= $('#shopnum').val();
	$.post("./ajax/car.php",{action:'add',pid:pid,num:shopnum},
	function(avr){
		//alert(avr)
		if(avr=='login'){
			alert("购买产品，请先登陆！");
			window.location="login.html";
			return false;
		}else if(avr=='false'){
			alert("购买失败！");
		}else if(avr=='true'){
			getcatnum();
			window.location='cart.html';
		}
	});
}
function addfav(pid){
	$.post("./ajax/user.php",{action:'addfav',pid:pid},
	function(avr){
		if(avr=='login'){
			alert("收藏产品，请先登陆！");
			window.location="login.html";
			return false;
		}else if(avr=='false'){
			alert("收藏失败！");
		}else if(avr=='yi'){
			alert("已经收藏了！");
		}else if(avr=='true'){
			alert("收藏成功！");
		}
	});
}
function searchcheck(){
	if($("#keyword").val()==''){
		alert("请输入关键字！");
		$("#keyword")[0].focus();
		return false;
	}
	return true;
}
