

function fadein(id)
{
	var o = document.getElementById(id);
	
	var h = o.style.height;
    var i=0;	
	if( (o) && o.style.display!="block")
	{
	   o.style.filter = "Alpha(Opacity=0)"; //for IE    
		o.style.opacity = 0; //for FF	
		o.style.display="block";
		function change(){
			if(i>=100)
				i=100;
			else
			{
				i+=10;
			   o.style.filter = "Alpha(Opacity=" + i + ")"; //for IE    
				o.style.opacity = i/100; //for FF
			}

		}

		if(i<100)
			window.setInterval(change,2);

	}
}


function scratchin()
{
	var o = document.getElementById('warn_box');
	var h = "24";
    var i=0;
	var j=0;
	if(o!=null)
	{
		function change(){
			if(i>=100)
				i=100;
			else
			{
				i+=5;
			   o.style.filter = "Alpha(Opacity=" + i + ")"; //for IE    
				o.style.opacity = i/100; //for FF
			}
			
			if(j>=h) j=h;
			else
			{
				j+=8;
				o.style.height= j +"px";
			}
		}
		function shutup()
		{
			o.style.display="none";
		}
		if(i<100)
			window.setInterval(change,10);
		 window.setTimeout(shutup,8000);
	}
}


function mail_reply(receiver_id,receiver,title_id,title,replyto_ac_id,replyto_ac,replyto_id,replyto)
{
	document.getElementById(receiver_id).value=receiver;
	document.getElementById(title_id).value=title;
	document.getElementById(replyto_id).value=replyto;
	document.getElementById(replyto_ac_id).value=replyto_ac;
}
function mail_status(stamp,status)
{
	var xmlHttp=GetXmlHttpObject();
	var url="http://www.zircy.com/mail_status.php?stamp="+stamp+"&status="+status+"&rand="+Math.random();
	if(xmlHttp)
	{
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				if(status=='read')
					document.getElementById(stamp).style.font="12px normal";
				else if(status=='hide')
					location.href="http://www.zircy.com/mail/";
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	else
	{
		document.getElementById(stamp).innerHTML="unsupported browser";
	}
}

function getElementsByClass(searchClass,node,tag) {
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                }
        }
        return classElements;
}

function shutup_class(all)
{
	arrElements=getElementsByClass(all,null,null);
	for(var i=0; i<arrElements.length; i++)
	{
		arrElements[i].style.display="none";
	}	
}

function set_height(o, num)
{
	document.getElementById(o).style.height=num;

}

function showdiv(i,j)
{
	document.getElementById(i).innerHTML=document.getElementById(j).innerHTML;
	fadein(i);
}
function dovalue(i,j)
{
	document.getElementById(i).value=j;
}
function autovalue(input,j)
{
	document.getElementById(input).value=j;
}
function merge_stuff(i,j,k)
{
	var name=document.getElementById(j).value;
	if(!name)
	{
		document.getElementById(i).style.display = "none";
	}
	else
		document.getElementById(i).style.display = "block";

	var tag=document.getElementById(k).value;

	document.getElementById(i).innerHTML="<img src='img/lightbulb.gif' /> 您喜欢<span>"+name+"</span>，有什么原因或者故事与大家分享吗？<textarea id=\"input_intro\" name=\"intro\"></textarea>";
}

function intro_float(i)
{
	document.getElementById(i).style.display = "block";
}
function display(i)
{
	document.getElementById(i).style.display="block";
}
function display_select(i,width)
{
	document.getElementById(i).style.width=width;
}

function switch_display(i,j,k,l)
{
	document.getElementById(i).style.display="block";
	document.getElementById(j).style.display="none";
	document.getElementById(k).style.display="none";
	document.getElementById(l).style.display="none";
}

function display_all(i,valuei,j,valuej,k,valuek)
{
	if(document.getElementById(i).style.height=="auto")
	{
		document.getElementById(i).style.height=valuei;
		document.getElementById(j).style.height=valuej;
		document.getElementById(k).style.height=valuek;
	}
	else
	{
		document.getElementById(i).style.height="auto";
		document.getElementById(j).style.height="auto";
		document.getElementById(k).style.height="auto";
	}
}
function display_one_all(i,valuei)
{
	if(document.getElementById(i).style.height=="auto")
	{
		document.getElementById(i).style.height=valuei;
	}
	else
	{
		document.getElementById(i).style.height="auto";
	}
}
function shutup(i)
{
	var o=document.getElementById(i);
	if(o)
		o.style.display="none";
}

function shutup2(i,j)
{
	document.getElementById(i).style.display="none";
	document.getElementById(j).style.display="none";
}

function switch_button(self,i,value,cancel,j,k)
{
	if(document.getElementById(self).value!=cancel)
	{
		document.getElementById(i).style.display="block";
		document.getElementById(self).value=cancel;
	}
	else
	{
		document.getElementById(i).style.display="none";
		document.getElementById(self).value=value;
	}

}
function switch_pwd(self,i,value,cancel,tag)
{
	if(document.getElementById(self).value!=cancel)
	{
		document.getElementById(i).style.display="block";
		document.getElementById(self).value=cancel;
		document.getElementById(tag).value="yes";
	}
	else
	{
		document.getElementById(i).style.display="none";
		document.getElementById(self).value=value;
		document.getElementById(tag).value="no";
	}

}
function show_stock(type,price,num)
{
	document.getElementById("stock").innerHTML="（库存："+num+"本）";
	document.getElementById("cart_type").value=type;
	document.getElementById("cart_price").value=price;
	document.getElementById("cart_stock").value=num;
}
function switchshow(j,i)
{
	var flag=i+"_title";
	document.getElementById(j).innerHTML=document.getElementById(i).innerHTML;
	var title=new array('author_intro_title','directory_title','book_intro_title','info_title','remark_title');
	for (var k=0;k<5;k++ )
	{
			if(title[k]==flag)
				document.getElementById(title[k]).style.background="#CCCCCC";
			else
				document.getElementById(title[k]).style.background="#158E02";
	}
	
}


function check_sign(theform)
{
	if (theform.account.value.length<5 || theform.account.value.indexOf("@")==-1 || theform.account.value.indexOf(".")==-1)
	{
		alert("请确定您的email填写无误");
		theform.account.focus( );
		return (false);
	}
	if (theform.password.value.length<1)
	{
		alert("为了保证您的账户安全，请设置密码");
		theform.password.focus( );
		return(false);
	}

	if(theform.password.value!=theform.password_confirm.value)
	{
		alert("两次密码输入不匹配，请重新输入");
		theform.password.focus( );
		theform.password.value="";
		theform.password_confirm.value="";
		return (false);
	}
}

function check_login(theform)
{
	if (theform.account.value.length<5 || theform.account.value.indexOf("@")==-1 || theform.account.value.indexOf(".")==-1)
	{
		alert("请准确输入您的email帐号");
		theform.account.focus( );
		return (false);
	}
	if (theform.password.value.length<1)
	{
		alert("请输入您的登录密码");
		theform.password.focus( );
		return(false);
	}

}


function check_info(theform)
{
	if (theform.name.value.length<2)
	{
		alert("请输入您的真实姓名，或者选择以后再填");
		theform.name.focus( );
		return(false);
	}
	if (theform.phone.value.length<2 && theform.mobile.value.length<2)
	{
		alert("请输入至少一个固定电话号码或移动电话号码，或者选择以后再填");
		theform.phone.focus( );
		return(false);
	}

	if(theform.address.value.length<4)
	{
		alert("请输入您的详细地址，或者选择以后再填");
		theform.address.focus( );
		return (false);
	}

}

function check_cart(theform)
{

	if(!theform.radio.used.checked)
	{
		alert("请输入准确的数量");
		theform.cart_num.focus( );
		return(false);
	}
	if (theform.cart_num.value.length<1 || theform.cart_num.value.length>3)
	{
		alert("请输入准确的数量");
		theform.cart_num.focus( );
		return(false);
	}
}


function   check_num(NUM)  
{  
    var i,strTemp,flag=true;  
    strTemp="0123456789";  
    if (NUM.length==0)
		flag=false;
	else
	{
		for   (i=0;i<NUM.length;i++)  
		{  
			if(strTemp.indexOf(NUM.charAt(i))==-1)  
			flag=false;   
		} 
		return flag;
	}
}  


function GetXmlHttpObject()
{
	var xmlHttp=null; 
	if (window.XMLHttpRequest)// Moz, FF, NN, Op
	{
	  xmlHttp = new XMLHttpRequest(); 
	}
	
	else if(window.ActiveXObject)//IE
	{
	  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	}
	return xmlHttp;
}


function auto_text(input, hint, input_id)
{

	var me = this;
	this.input = input;
	this.hint = hint;
	this.highlighted = -1;//highlighted是一个寄存变量，保存了当前被用户选中的那个条目编号

	this.getKey = function(ev)//这个函数是用来捕捉键盘的，每个键盘都有对应的数值
	{
		if(ev)			//Mozilla
			return ev.keyCode;
		if(window.event)	//IE
			return window.event.keyCode;
	}
	input.onblur=function(ev)
	{
		window.setTimeout(me.shutup,100);
	}
	this.shutup=function()
	{
		me.hint.style.display="none";
	}

	input.onkeyup = function(ev)  //这个ajax请求服务器并返回数据的函数，大家应该比较熟悉了
	{
		var key = me.getKey(ev);//读取键盘值
		if(me.input.value=="")//如果输入框里没有任何内容，则提示层隐藏，不选中任何条目
		{
			me.hint.style.display="none";
			me.highlighted = -1;
		}
		else if(key!=38 &&  key!=40)//如果键盘不是上下箭头，说明有输入，则发送Ajax请求并取回数据
		{
			
			me.temp=me.input.value;
			var text_part=encodeURIComponent(me.input.value);//对输入框的值进行编码，解决中文问题，便于传送到服务器端
			var xmlHttp=GetXmlHttpObject();
			var url="http://www.zircy.com/auto_text.php?input_id="+input_id+"&text_part="+text_part+"&rand="+Math.random();//服务器请求，包含了text_part这个数据
			if(xmlHttp)
			{
				xmlHttp.onreadystatechange=function()
				{
					if(xmlHttp.readyState==4)//服务器有相应并返回了数据，则显示出提示层，并把数据显示在提示层里面
					{
						if(xmlHttp.responseText)
						{
							me.hint.style.display="block";
							me.hint.innerHTML=xmlHttp.responseText;
						}
						else
							me.hint.style.display="none";
					}
				}
				xmlHttp.open("GET",url,true);
				xmlHttp.send(null);
			}
			else
			{
				me.hint.innerHTML="unsupported browser";
			}
			
		}
		
	}
	input.onkeydown = function(ev)//当用户在输入框里按上下箭头时，调用activate()函数来选中提示层里相应的条目。
	{
		var key = me.getKey(ev);
		var lis = me.hint.getElementsByTagName('li');//获取提示层里的元素长度

		if(key==40) //向下箭头，则选中的条目向下移动
		{
			if(me.highlighted< lis.length)
				me.highlighted++;
			else
				me.highlighted=0;
		}
		else if(key==38) //向上箭头，则选中的条目向上移动
		{
			if(me.highlighted>0)
				me.highlighted--;
			else
				me.highlighted= lis.length
		}
		else
			me.highlighted = -1;
		me.activate();//选中新的条目
	
	}

	this.activate = function()//选中某一个条目并进行操作
	{
		Tree = new Array;
		temp_v=document.getElementById(me.highlighted).innerHTML;
		aa=temp_v.split(" : ");  
		me.input.value=aa[0];//输入框里的内容变为选中的条目内容

		var lis = me.hint.getElementsByTagName('li');
		for (i in lis)
		{
			var li = lis[i];

			if (this.highlighted !=lis[i].id)//把所有未选中的条目的css设置一下
			{
				li.className = "";
			}
			else
				li.className = "auto_selected";//把选中的条目的css属性改变一下，比如字体变粗
		}

	};

}
