function $(id){
	return document.getElementById(id);
}

function stringTrim(v) {
    return v.replace(/(^\s*)|(\s*$)/g, "");
}

function $t(id, tagName){
	return $(id).getElementsByTagName(tagName);
}

function stringTrim(v) {
    return v.replace(/(^\s*)|(\s*$)/g, "");
}

ajax = function(url,postback,method,params,format){
    this.url = url;
    this.postback = postback;
    this.params = params;	
    this.xmlHttpRequest = null;
    this.method = method;
	this.format = format;
    this.getDocument(url);
}

ajax.prototype = {	
	createXMLHttpRequest:function(){
		if (typeof XMLHttpRequest != "undefined"){
			this.xmlHttpRequest = new XMLHttpRequest();			
		} else if (typeof ActiveXObject != "undefined"){			
			var xmlHttpVers =  ["Microsoft.XMLHTTP", "MSXML.XMLHTTP", "Microsoft.XMLHTTP", "Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP"];
			for (var i=0; i<xmlHttpVers.length; i++){
				try{
					this.xmlHttpRequest = new ActiveXObject(xmlHttpVers[i]);
					break;
				} catch (e){}
			}
		} else{
			return this.xmlHttpRequest;
		}
    },

    getDocument:function(url){
		this.createXMLHttpRequest();
        if(this.xmlHttpRequest){
            try {
                this.xmlHttpRequest.open(this.method,url,true);               	
                var o = this;				
                this.xmlHttpRequest.onreadystatechange = function(){
                    o.onReadyState();
                }
				if(this.method == 'post' || this.method == 'POST'){
					this.xmlHttpRequest.setRequestHeader("Content-length",this.params.length);
					this.xmlHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
				}
				this.xmlHttpRequest.send(this.params);			
            }catch(e){
			}
        }
    },

	onReadyState:function(){
    	if(this.xmlHttpRequest.readyState == 4){
        	if(this.xmlHttpRequest.status == 200 || this.xmlHttpRequest.status == 0 ){
				var reuqest = this.xmlHttpRequest.responseText;
				if(this.format == "json"){
					var json = eval('(' + reuqest + ')'); 
					this.postback(json)
				} else{
					this.postback(reuqest);
				}
        	}
		}
	}
}

function ajaxUpdater(domId,url){
    var back = function(source){
        $(domId).innerHTML = source;
    }
    var abc = new ajax(url,back,'get','');
}

function ajaxRequest(url,back,method,parames,format){
    var abc = new ajax(url,back,method,parames,format);
}

function runHtml(v) {
	var textarea = v.parentNode.getElementsByTagName('textarea')[0];
	var code = textarea.value;
	if (code != ''){
		var newwin = window.open('','',''); 
		newwin.opener = null;
		newwin.document.write(code);
		newwin.document.close();
	}
}

function postComment(){	
	var guestName = stringTrim($('guestName').value);
	var guestContent = stringTrim($('guestContent').value);
	var postId = stringTrim($('postId').value);
	if(guestName == ''){
		alert('名字/昵称不能为空！');	
		return false;
	}
	if(guestContent == ''){
		alert('内容不能为空！');	
		return false;
	}
	if(postId == ''){
		alert('未知错误！');	
		return false;
	}
	var url = '/api/saveComment.php?';
	p = 'postId=' + encodeURIComponent(postId) + '&';
	p += 'guestName=' + encodeURIComponent(guestName) + '&';
	p += 'guestContent=' + encodeURIComponent(guestContent) + '&';
	$('postTips').innerHTML = '<div class="load">评论提交中，请稍候……</div>';	
	$('postTips').style.display = 'block';
	
	cbk = function (j){
		if(typeof(j) == 'string'){
			$('postTips').style.display = 'none';
			alert(j);
			return false;
		}
		addComment(j);
		$('postComment').innerHTML = '<div class="ok">评论发布成功！</div>';
		return false;		
	}
	var ajax = ajaxRequest(url,cbk,'post',p,'json');
	return false;
}

function addComment(v){
	var html = '';
	html += '<legend><strong>'+v['userName']+'</strong>于'+v['createTime']+'说</legend>';
	html += v['comment'];
	if(v['isReply'] == 1){
		html += '<br />';
		html += '<div class="reply">'+v['replyUser']+'于'+v['createTime']+'回复说</div>';
		html += v['reply'];
	}
	var fieldset = document.createElement('fieldset'); 
	fieldset.setAttribute('align', 'center:');
	fieldset.innerHTML = html;
	$('comments').appendChild(fieldset);
}

function moreComment(v){
	v.innerHTML = '加载中……';
	var url = '/api/getComment.php?';
	p = 'postId=' + encodeURIComponent($('postId').value) + '&';
	p += 'pagerId=' + encodeURIComponent($('pagerId').value) + '&';
	
	cbk = function (j){
		if(j.length != 0){
			for(var i=0; i<j.length; i++){
				addComment(j[i]);	
			}	
			v.innerHTML = '显示更多评论';
			$('pagerId').value = parseInt($('pagerId').value) + 1;
		}
		if(j.length < 10){
			v.style.display = 'none';
		}
	}
	var ajax = ajaxRequest(url,cbk,'post',p,'json');
	return false;
}


function postLink(){	
	var siteName = stringTrim($('siteName').value);
	var siteLink = stringTrim($('siteLink').value);
	if(siteName == ''){
		alert('网站名不能为空！');	
		return false;
	}
	if(siteLink == ''){
		alert('网址不能为空！');	
		return false;
	} 
	var url = '/api/saveLink.php?';
	p = 'siteName=' + encodeURIComponent(siteName) + '&';
	p += 'siteLink=' + encodeURIComponent(siteLink) + '&';
	$('postTips').innerHTML = '<div class="load">评论提交中，请稍候……</div>';	
	$('postTips').style.display = 'block';
	
	cbk = function (j){
		alert(j);
		$('postTips').innerHTML = '';	
		$('linkForm').innerHTML = '<div class="ok">提交成功！</div>';
		return false;		
	}
	var ajax = ajaxRequest(url,cbk,'post',p,'json');
	return false;
}
