/** vote function **/
function vote(){
	var voter = document.getElementById('voter');
	var vid = 0;
	var opts = [];

	var inputs = voter.getElementsByTagName('INPUT');
	for(var i=inputs.length-1; i>=0; i--){
		if(inputs[i].name == 'vote_opts' && inputs[i].checked) opts.push(inputs[i].value);
		else if(inputs[i].name == 'vid') vid = inputs[i].value;
	}

	if(opts.length == 0)
		alert('请选择你要投票的选项');
	else {
		var url = './vote.php?act=ajaxVote&vid='+vid;
		for(var i=opts.length-1; i>=0; i--)
			url += '&opts[]=' + opts[i];

		ajax(function(){
			if(xml.readyState == 4){
				if(xml.status == 200){
					if(xml.responseText == '0')
						alert('投票系统正忙，请稍候再投票');
					else{
						if(xml.responseText == '1'){
							alert('投票成功');
						} else {
							alert('你的ip已经投过票了，谢谢支持。');
						}
						if(document.getElementById('vote_btn'))
							document.getElementById('vote_btn').style.display = 'none';
						if(document.getElementById('view_vote_btn'))
							document.getElementById('view_vote_btn').style.display = '';
						loadVoteResult(voter,vid);
					}
				}
			} else voter.innerHTML = "正在投票，请稍候...";
		},url);
	}
}

function loadVoteResult(voter,vid){
	var url = './vote.php?act=ajaxVoteResult&vid='+vid;
	if(document.getElementById('vote_btn'))
		document.getElementById('vote_btn').style.display = 'none';

	if(document.getElementById('view_vote_btn'))
		document.getElementById('view_vote_btn').style.display = 'none';

	if(document.getElementById('return_vote_btn'))
		document.getElementById('return_vote_btn').style.display = '';
	ajax(function(){
		if(xml.readyState == 4){
			if(xml.status == 200){
				voter.innerHTML = xml.responseText;
			};
		}
		else
			voter.innerHTML = "正在加载投票结果，请稍候...";
	},url);
}

function ajax(callback, url , config){
    this.xml = new Object;

    this.url = url;
    this.callback = callback;

    this.config = {
        'method': 'GET',
        'timeout': 0,
        'syn': true
    };

	if((config || 0).method) this.config.method = config.method;
	if((config || 0).timeout) this.config.timeout = config.timeout;
	if((config || 0).syn) this.config.syn = config.syn;

    this.init = function(){
        if (window.XMLHttpRequest) {
            this.xml = new XMLHttpRequest();

            if (this.xml.overrideMimeType) {
                this.xml.overrideMimeType("text/xml");
            }
        }
        else {
            if (window.ActiveXObject) {
                try {
                    this.xml = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e) {
                    try {
                        this.xml = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e) {
                    }
                }
            }
        }
    }

    this.send = function(){
        var _this = this;
		this.xml.onreadystatechange = function(){
			_this.callback.call(_this.xml);
		}

        if (this.config.method.toUpperCase() == 'GET') {
            this.xml.open('GET', this.url, this.config.syn);

            try {
                this.xml.send(null);
            }
            catch (e) {
                alert(e.message);
            }
        }
        else {
            var url = this.url.url;
            var param = this.url.param;
            this.xml.open('POST', url, this.config.syn);
            this.xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            this.xml.send(param);
        }
    }

    //constructor
    this.init();
    if (this.url && this.callback) {
        this.send();
    }
}
