
/** validate comment form input */
function validateCommentForm(){
	var form = document.forms.commentform;
	var author = form.author.value.trim();
	var email = form.email.value.trim();
	var url = form.url.value.trim();
	var content = tinyMCE.getContent();
	
	if(author.length==0){
		alert("“姓名”不能为空！");return false;
	}
	if(email.length==0){
		alert("“Email”不能为空！");return false;
	}
	if(!checkEmail(email)){
		alert("“Email”格式错误！");return false;
	}
	if(!checkUrl(url)){
		alert("“链接”格式错误");return false;
	}
	if(content.length==0){
		alert("“内容”不能为空！");return false;
	}
	return true;
}

/** validate search input */
function validateSearchForm(){
	if(document.forms.search.s.value.trim().length==0){
		alert("搜索关键字不能为空！");
		return false;
	}
	return true;
}

/** validate message form input */
function validateMessageForm(){
	var form = document.forms.messageform;
	var author = form.author.value.trim();
	var email = form.email.value.trim();
	var url = form.url.value.trim();
	var content = tinyMCE.getContent();
	if(author.length==0){
		alert("“姓名”不能为空！");return false;
	}
	if(email.length==0){
		alert("“Email”不能为空！");return false;
	}
	if(!checkEmail(email)){
		alert("“Email”格式错误！");return false;
	}
	if(!checkUrl(url)){
		alert("“链接”格式错误");return false;
	}
	if(content.length==0){
		alert("“内容”不能为空！");return false;
	}
	return true;
}

String.prototype.trim = function(){
	return this.replace(/^\s*|\s*$/g,"");
}

/** check email */
function checkEmail(emailStr) {
	if (emailStr.length == 0) {
		return true;
	}
    // TLD checking turned off by default
    var checkTLD=0;
    var knownDomsPat=/^(com|cn|us|ca|uk|jp|cc|de|se|be|nl|sc|dk|at|ch|sr|vc|fr|nu|ws|it|md|hn|bz|no|nz|tw|jobs|am|fm|gs|ms|tc|tk|vg|la|tv|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {
            return false;
        }
    }
    for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) {
            return false;
        }
    }
    if (user.match(userPat)==null) {
        return false;
    }
    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
        for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                return false;
            }
        }
        return true;
    }
    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;
    for (i=0;i<len;i++) {
        if (domArr[i].search(atomPat)==-1) {
            return false;
        }
    }
    if (checkTLD && domArr[domArr.length-1].length!=2 && 
        domArr[domArr.length-1].search(knownDomsPat)==-1) {
        return false;
    }
    if (len<2) {
        return false;
    }
    return true;
}
    
/** check url */
function checkUrl(urlStr){
	if (urlStr.length == 0) {
		return true;
	}
	var exp = /^(((ht|f)tp(s?))\:\/\/)?(www.|[a-zA-Z0-9].)[a-zA-Z0-9\-\.]+\.(com|cn|us|ca|uk|jp|cc|de|se|be|nl|sc|dk|at|ch|sr|vc|fr|nu|ws|it|md|hn|bz|no|nz|tw|jobs|am|fm|gs|ms|tc|tk|vg|la|tv|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$/;
	var p = new RegExp(exp);
 	return p.test(urlStr);
}
