var votesvr="http://dynamic.vip.xunlei.com/vote/gvote/voteid/1/option/";
var resultsvr="http://dynamic.vip.xunlei.com/vote/query/voteid/1/";
var MAX_SELECTED = 3;
var voteid=1;
function initVote() {
    initCheckbox();
    $('voteBtn').onclick = vote_submitForm;
    $('viewBtn').onclick = viewResult;
    
    voteid = $('voteid').value;
    MAX_SELECTED = $('max_selected').value;
    votesvr = "http://dynamic.vip.xunlei.com/vote/gvote/voteid/" + voteid + "/option/";
    resultsvr = getnocacheurl("http://dynamic.vip.xunlei.com/vote/query/voteid/" + voteid);
    //initLogininfo();
}

function vote_submitForm() {
    var count = getSelectedNum();
    if(count > MAX_SELECTED) {
        alert("您选择了太多的选项！");
        return false;
    }
    if(count < 1) {
        alert("请选择您的看法！");
        return false;
    }
    
    var votes = getSelectedValue();
    var newUrl = getnocacheurl(votesvr + votes);
    var win = newWindow(newUrl,'opwd','600','450','no','center');
    win.focus();
    return false;
}

function viewResult() {
    var win = newWindow(resultsvr,'opwd','600','450','no','center');
    win.focus();
}

function initCheckbox(){
    if(!$('ballot_ul'))  return 0;
	var arCheckBox = $('ballot_ul').getElementsByTagName('input');
	if (arCheckBox && arCheckBox.length > 0) {
	    for(var i=0; i< arCheckBox.length; i++) {
	        arCheckBox[i].onclick = oncheck;
	    }
	}    
}

function oncheck(){    
    var count = getSelectedNum();
    if(count > MAX_SELECTED) {
        //alert("太多了");
        if(window.event) {
            window.event.srcElement.checked = false;
        }
    }
}

function getSelectedNum() {
    var count = 0;
    if(!$('ballot_ul'))  return count;
	var arCheckBox = $('ballot_ul').getElementsByTagName('input');
	if (arCheckBox && arCheckBox.length > 0) {
	    for(var i=0; i< arCheckBox.length; i++) {
	        if(arCheckBox[i].checked) {
	            count++;
	        }
	    }
	}
	return count;
	
}    

function getSelectedValue() {
    var values = '';
    if(!$('ballot_ul'))  return count;
	var arCheckBox = $('ballot_ul').getElementsByTagName('input');
	if (arCheckBox && arCheckBox.length > 0) {
	    for(var i=0; i< arCheckBox.length; i++) {
	        if(arCheckBox[i].checked) {
	            values += "_"+arCheckBox[i].value;
	        }
	    }
	}
	return values;
	
}

function newWindow(mypage,myname,w,h,scroll){
    LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
    TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
    settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
    win = window.open(mypage,myname,settings)
    return win;
}