﻿// JavaScript Document


function CountStrByte(Message,used,Remain){
	var LastCount = 0;
	var MaxValue  = 1000;
	var ByteCount = 0;
	var StrValue  = Message.value;
	var StrLength = Message.value.length;
	if(LastCount != StrLength) { // 在此判断，减少循环次数
		for (i=0;i<StrLength;i++){
			ByteCount  = (StrValue.charCodeAt(i)<=256) ? ByteCount + 1 : ByteCount + 2;
			if (ByteCount>=MaxValue) {
				Message.value = StrValue.substring(0,i);
				alert("留言内容最多不能超过 " +MaxValue+ " 个字节！\n注意：一个汉字为两字节。");
				ByteCount = MaxValue;
				break;
			}
		}
		Remain.value  = MaxValue - ByteCount;
		used.value    = ByteCount;
		LastCount     = StrLength;
	}
}

function GetXmlHttpObject()
{
	var xmlHttp;
 
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			
		}
	}
	return xmlHttp;
	
}

function checkForm()
{
	
	var f = document.comment_form;
	var article_id = f.article_id.value;
	var comment_table = f.comment_table.value;
	var author = f.author.value;
	var email = f.email.value;
	var validateCode = f.validateCode.value;
	var comments = f.comments.value;
	
	var pattern = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/;
	flag = pattern.test(email);

	xmlHttp=GetXmlHttpObject();
	//alert(xmlHttp);
	if(xmlHttp==null){
		alert ("您的浏览器不支持AJAX！");
		return;
	}
	
	if(email!=""&&!flag){
		alert("您输入的邮件地址有误，请重新输入！");
		f.email.focus();
		return false;	
	}
	if(validateCode!="love"){
		alert("您输入的验证码有误，请重新输入！");
		f.validateCode.focus();
		return;
	}
	if(comments==""){
		alert("评论内容不能为空！");
		f.comments.focus();
		return;
	}

	
	//接收表单的URL地址
	var url="/include/validcomment.php";
	//需要POST的值，把每个变量都通过&来联接
	var postStr = "article_id="+article_id+"&comment_table="+comment_table+"&author="+author+"&email="+email+"&comments="+comments;

	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("POST",url,true);
	
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	
	xmlHttp.send(postStr);
}

function stateChanged()
{
	var newcomment=document.getElementById('newcomment');
	var f = document.comment_form;

	if(xmlHttp.readyState==4)
	{
		if(xmlHttp.status==200)
		{
			newcomment.innerHTML=xmlHttp.responseText+newcomment.innerHTML;
			f.comments.value = '';
			window.location.hash="review";
		
		}else{
			alert('There was a problem with the request.');
		}
	}
}

function checkloginForm()
{
	
	var f = document.login_forma;
	var email = f.email.value;
	var passwd = f.passwd.value;
	var validateCode = f.validateCode.value;

	xmlHttp=GetXmlHttpObject();
	//alert(xmlHttp);
	if(xmlHttp==null){
		alert ("您的浏览器不支持AJAX！");
		return;
	}
	
	if(email==""){
		alert("请输入您注册时使用的邮件地址！");
		f.email.focus();
		return;	
	}
	if(passwd==""){
		alert("请输入密码！");
		f.passwd.focus();
		return;
	}
	if(validateCode!="abc"){
		alert("您输入的验证码有误，请重新输入！");
		f.validateCode.focus();
		return;
	}

	
	//接收表单的URL地址
	var url="/include/validlogin.php";
	//需要POST的值，把每个变量都通过&来联接
	var postStr = "email="+email+"&password="+passwd;

	xmlHttp.onreadystatechange=validlogin;
	
	xmlHttp.open("POST",url,true);
	
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	
	xmlHttp.send(postStr);
}
						
function validlogin()
{
	var logindiv=document.getElementById('logindiv');

	if(xmlHttp.readyState==4)
	{
		if(xmlHttp.status==200)
		{
			//alert(xmlHttp.responseText);
			logindiv.innerHTML=xmlHttp.responseText;
		
		}else{
			alert('There was a problem with the request.');
		}
	}
}