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

function _ajax(){
	var req		= null;
	var method	= "get";
	var sync	= true;
	var query	= Array();
	var argu	= [];
	var parser	= null;
	var res = {responseXML:null,responseText:null};
	this.init = function(){
		if(window.ActiveXObject){
			req = new ActiveXObject("Microsoft.XMLHTTP");
			return true;
		}
		else if(window.XMLHttpRequest){
			parser = new DOMParser();
			req = new XMLHttpRequest();
			return true;
		}
		else{
			return false;
		}
	}
	this.setVal = function(n, v){
		if (typeof(n) == "object"){
			query = query.concat(n);
		}
		else{
			query.push(n + "=" + v);
		}
	}
	this.setPost = function(){
		method = "post";
	}
	this.setSync = function(){
		sync = false;
	}
	this.setArgu = function(a){
		argu = a
		if (isNull(a))
			argu = [];
	}
	this.open = function(_url, func){
		req.open(method, _url, sync);
		req.onreadystatechange = function(){
			if (req.readyState == 4 && req.status == 200)
			{
				res.responseText	= req.responseText;
				res.responseXML	= req.responseXML;
				if (!isNull(parser)){
					res.responseXML = parser.parseFromString(req.responseText,"text/xml");
				}
				eval(func).apply(res, argu);
			}
		};
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	}
	this.send = function(){
		if (query.length){
			req.send(query.join("&"));
		}
		else{
			req.send(null);
		}
	}
}

function ajax(_url, func, argu, sync){
	var req = new _ajax();
	req.init();
	if (!isNull(sync) && isNull == false){
		req.setSync();
	}
	req.setArgu(argu);
	req.open(_url, func);
	req.send(null);
}

function topac()
{
    var pid     = 0;
    var maxnum  = 100;
    var toptit  = '顶到头了';
    var downtit = '踩到底了';
    this.init = function(p_id, max_num, top_title,down_title)
    {
        pid     = p_id;
        if(max_num > 0) maxnum = max_num;
        if(top_title != '')toptit = top_title;
        if(down_title != '')downtit = down_title;
    }
    this.add = function(act)
    {
        this.set(act);
    }
    this.set = function(act)
    {
        this.ajax(act);
    }
    this.get = function()
    {
        this.ajax();
    }
    this.ajax = function(act)
    {
        switch(act)
        {
            case 'ding':
                var url = '../tableforum/topac.aspx?pid='+pid+'&act=' +1 +'&'+ new Date().getTime();
                ajax(url,function()
                    {
                        if(this.responseText != '')
                        {
                            eval(this.responseText.trim());
                            if(typeof(angel) == 'undefined') return false;
                            switch(angel.ret)
                            {
                                case 1:
                                    var obj = GetObj('topac_ding_shower');
                                    var num = obj.innerHTML;
                                    if(num != toptit)
                                    {
                                        num = num.replace(/[^0-9]/g,'');
                                        if(num == '' )
                                        {
                                            num = 0;
                                        }
                                        ++num;
                                        if(num > maxnum)
                                        {
                                             obj.innerHTML = toptit;
                                        }
                                        else
                                        {
                                            obj.innerHTML = num + '次';
                                        }
                                    }
                                    alert('顶帖成功！感谢您的参与！');
                                break;
                                default:
                                    alert(angel.desc);
                            }
                        }
                    }
                )
                break;
            case 'cai':
                var url = '../tableforum/topac.aspx?pid='+pid+'&act=' +2 +'&'+ new Date().getTime();
                ajax(url,function()
                    {
                        if(this.responseText != '')
                        {
                            eval(this.responseText.trim());
                            if(typeof(angel) == 'undefined') return false;
                            switch(angel.ret)
                            {
                                case 1:
                                    var obj = GetObj('topac_cai_shower');
                                    var num = obj.innerHTML;
                                    if(num != downtit)
                                    {
                                        num = num.replace(/[^0-9]/g,'');
                                        if(num == '' )
                                        {
                                            num = 0;
                                        }
                                        ++num;
                                        if(num > maxnum)
                                        {
                                             obj.innerHTML = downtit;
                                        }
                                        else
                                        {
                                            obj.innerHTML = num + '次';
                                        }
                                    }
                                    alert('踩帖成功！感谢您的参与！');
                                break;
                                default:
                                    alert(angel.desc);
                            }
                        }
                    }
                )
                break;
            default:
                var url = '../tableforum/topac.aspx?pid='+pid+'&act=' + 0 +'&'+ new Date().getTime();
                ajax(url,function()
                    {
                        if(this.responseText != '')
                        {
                            eval(this.responseText.trim());
                            if(typeof(angel) == 'undefined') return false;
                            switch(angel.ret)
                            {
                                case 1:
                                    GetObj('topac_ding_shower').innerHTML = angel.up >  maxnum  ? toptit : angel.up + '次';
                                    GetObj('topac_cai_shower').innerHTML =  angel.down > maxnum  ? downtit :  angel.down + '次';
                                break;
                                default:
                                    alert(angel.desc);
                            }
                        }
                    }
                )
        }
    }
}
var topac = new topac();
