var xmlHttp;
var newstr;
function createXMLHttpRequest() {
    if(window.XMLHttpRequest) { 
            xmlHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { 
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}          
            }
    }
}

function go(votecounts) {
    createXMLHttpRequest();
    var url = "/VoteAction.do?dispatch=submitVoteByAjax&counts="+votecounts;
    xmlHttp.onreadystatechange = goCallback;
    xmlHttp.open("GET", url, true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    xmlHttp.send(null);
}

function goCallback() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
        		//成功返回之后的操作 
					alert("感谢你的投票，谢谢！");
					//window.location.href="sitevote.htm";              
        } else { //页面不正常
	      	alert("您所请求的页面有异常。");
	 	}
    }
}

function successvote(){
		//获得用户选择的问题选项
		var myarray = new Array();
		var j = 0;
		var votecounts = document.getElementsByName("votecount");
		for(i=0;i<votecounts.length;i++){
			if(votecounts[i] != null && votecounts[i].checked == true){
				myarray[j] = votecounts[i].value;
				j++;
			}
		}
		
		for(i=0;i<votecounts.length;i++){
			votecounts[i].checked = false;
		}
		if(myarray==""){
			alert("请选择你要投票的选项！");
		}else{
			//感谢用户的选择
			/*var objdiv = document.getElementById('indexvote');
			if(objdiv != null){
			objdiv.style.visibility='visible';
			objdiv.innerHTML = '<table width=100% border=0 cellspacing=3 cellpadding=0><tr><td align=center valign=top><table width=93% height=40 border=0 align=center cellpadding=0 cellspacing=0><tbody><tr><td height=40 align=left class=STYLE100>感谢您对本次网上调查的支持！如果您要查看投票结果，请单击查看按钮，谢谢！</td></tr><tr><td align="center"><a href=sitevote.htm><img height=20 src=/images/chaxu2.jpg width="40" border="0" /></a></td></tr></tbody></table>';
			}*/
			//调用异步通讯方式，将投票参数传入后台
			go(myarray);
		}			
	}

// 取出选项
function inititem(){
	createXMLHttpRequest();
    var url = "/VoteAction.do?dispatch=InitItemByAjax";
    xmlHttp.onreadystatechange = inititemCallback;
    xmlHttp.open("GET", url, true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    xmlHttp.send(null);
}

function inititemCallback() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            updateitem();
        } else { //页面不正常
	      	alert("您所请求的页面有异常。");
	 	}
    }
}

function updateitem() {
	var totalcount=0;
	var total = document.getElementById("totalcount")
	var items = xmlHttp.responseXML.getElementsByTagName("item");
	var count = 0;
	for(i=0; i<items.length; i++){
    	count= parseInt(items[i].getAttribute("id"));
    	totalcount = totalcount + count;
    }
	//获得每个选项内容
	var itemstr = "";    
    for(i=0; i<items.length; i++){
        var j = i+1;
        var itemtext = items[i].firstChild.nodeValue;
        var itemcount = items[i].getAttribute("id");
        var tmp = 0;
        if(totalcount != 0){
        	tmp = (itemcount/totalcount)*100;
        }
        var tmp1 = 100 - tmp;
        var temp = new String(tmp);
        var temp1 = new String(tmp1);
        temp = temp.substring(0,5);
        itemstr = itemstr + '<table class="main_tdbg_760" cellspacing="0" cellpadding="3" align="center" width="91%"  border="0"><tr><td><table style="WIDTH: 539px; WORD-BREAK: break-all" cellspacing="0" cellpadding="0" width="539" align="left" border="0"><tbody><tr><td height="23">选项'+j+'：<strong>'+itemtext+'</strong></td></tr></tbody></table></td></tr><tr><td><table width="100%" height="41" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td width="9%" height="22" valign="top">得票率：</td><td width="91%" valign="top"><table border="0" cellspacing="0" cellpadding="0"><tr><td width="6" height="14" valign="top"><img src="images/tiao1.gif" width="6" height="14" /></td><td width="'+temp+'%" background="images/tiao4.gif"></td><td width="7" valign="top"><img src="images/tiao3.gif" width="7" height="14" /></td><td width="'+temp1+'%" background="images/tiao.gif"></td><td width="6" valign="top"><img src="images/tiao2.gif" width="6" height="14" /></td></tr></table></td></tr><tr><td height="19" colspan="2" align="center">占：'+temp+' [得：<font color="#ff0000">'+itemcount+'</font>票]</td></tr></tbody></table></td></tr><tr></table>';
    }
    var itemdiv = document.getElementById("itemdiv");
    itemdiv.innerHTML = itemstr;
	total.innerText = totalcount;
	initvote();
}


var issingle;
// 取出问题
function initvote(){
	createXMLHttpRequest();
    var url = "/VoteAction.do?dispatch=InitVoteByAjax";
    xmlHttp.onreadystatechange = initCallback;
    xmlHttp.open("GET", url, true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    xmlHttp.send(null);
}

function initCallback() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            updatesubject();
        } else { //页面不正常
	      	alert("您所请求的页面有异常。");
	 	}
    }
}

function updatesubject() {
	//try{
		//取得suject，并放到页面中
		var subjectdiv = document.getElementById("subjectdiv");
		var subjecttd = document.getElementById("subjecttd");
		var votes = xmlHttp.responseXML.getElementsByTagName("vote");
		//获得每个选项内容
		issingle = votes[0].getAttribute("id");//获得是否单选的标记
        subjectdiv.innerText = "\""+votes[0].firstChild.nodeValue+"\"";
		subjecttd.innerText = votes[0].firstChild.nodeValue;
}