﻿// JScript 文件
var errInfo = new Array();
errInfo[0]="连接服务器失败,请查看网络连接是否正常！";
errInfo[1]="服务器返回数据格式错误！";



function GetVote(ContentTemplate,ContentBody,channelID,ID)
{
    var response = "";	
	var url="ajax.aspx";
	
	var pars="&A=getVote&channelID=" + channelID +"&ID=" + ID;
	pars +="&r="+Math.random();
	var myAjax= new Ajax.Request(
		url,
		{
			method:"get",
			parameters:pars,
			onSuccess:function(transport){
				xmlDoc=new EXDoc(transport.responseText);
			    try{
			
					var child=xmlDoc.root.contents[0];
					response+=GetVoteRes(ContentTemplate,ContentBody,child.getValue("ID"),child.getValue("ChannelID"),child.getValue("Type"),child.getValue("Title"),child.getValue("Select1"),child.getValue("Select2"),child.getValue("Select3"),child.getValue("Select4"),child.getValue("Select5"),child.getValue("Select6"),child.getValue("Select7"),child.getValue("Select8"));


				    $(ContentBody).innerHTML=response;
				}
				catch(e){
                     ShowTip("服务器返回错误代码或者页面出错",ContentBody);
 				     HideAfter3Seconds(ContentBody);
                        }
			},
			onFailure:function(transport){
				ShowTip(errInfo[0],ContentBody);
				HideAfter3Seconds(ContentBody);
			}
		});
}

function GetArticleVote(ContentTemplate,ContentBody,channelID,ID)
{
    var response = "";	
	var url="ajax.aspx";
	
	var pars="&A=getPostVote&channelID=" + channelID +"&type=1&ID=" + ID;
	pars +="&r="+Math.random();
	var myAjax= new Ajax.Request(
		url,
		{
			method:"get",
			parameters:pars,
			onSuccess:function(transport){
				xmlDoc=new EXDoc(transport.responseText);
			    try{
			
					var child=xmlDoc.root.contents[0];
					response+=GetVoteRes(ContentTemplate,ContentBody,child.getValue("ID"),child.getValue("ChannelID"),child.getValue("Type"),child.getValue("Title"),child.getValue("Select1"),child.getValue("Select2"),child.getValue("Select3"),child.getValue("Select4"),child.getValue("Select5"),child.getValue("Select6"),child.getValue("Select7"),child.getValue("Select8"));


				    $(ContentBody).innerHTML=response;
				}
				catch(e){
                     ShowTip("服务器返回错误代码或者页面出错",ContentBody);
 				     HideAfter3Seconds(ContentBody);
                        }
			},
			onFailure:function(transport){
				ShowTip(errInfo[0],ContentBody);
				HideAfter3Seconds(ContentBody);
			}
		});
}

function GetPhotoVote(ContentTemplate,ContentBody,channelID,ID)
{
    var response = "";	
	var url="ajax.aspx";
	
	var pars="&A=getPostVote&channelID=" + channelID +"&type=2&ID=" + ID;
	pars +="&r="+Math.random();
	var myAjax= new Ajax.Request(
		url,
		{
			method:"get",
			parameters:pars,
			onSuccess:function(transport){
				xmlDoc=new EXDoc(transport.responseText);
			    try{
			
					var child=xmlDoc.root.contents[0];
					response+=GetVoteRes(ContentTemplate,ContentBody,child.getValue("ID"),child.getValue("ChannelID"),child.getValue("Type"),child.getValue("Title"),child.getValue("Select1"),child.getValue("Select2"),child.getValue("Select3"),child.getValue("Select4"),child.getValue("Select5"),child.getValue("Select6"),child.getValue("Select7"),child.getValue("Select8"));


				    $(ContentBody).innerHTML=response;
				}
				catch(e){
                     ShowTip("服务器返回错误代码或者页面出错",ContentBody);
 				     HideAfter3Seconds(ContentBody);
                        }
			},
			onFailure:function(transport){
				ShowTip(errInfo[0],ContentBody);
				HideAfter3Seconds(ContentBody);
			}
		});
}

function GetMediaVote(ContentTemplate,ContentBody,channelID,ID)
{
    var response = "";	
	var url="ajax.aspx";
	
	var pars="&A=getPostVote&channelID=" + channelID +"&type=3&ID=" + ID;
	pars +="&r="+Math.random();
	var myAjax= new Ajax.Request(
		url,
		{
			method:"get",
			parameters:pars,
			onSuccess:function(transport){
				xmlDoc=new EXDoc(transport.responseText);
			    try{
			
					var child=xmlDoc.root.contents[0];
					response+=GetVoteRes(ContentTemplate,ContentBody,child.getValue("ID"),child.getValue("ChannelID"),child.getValue("Type"),child.getValue("Title"),child.getValue("Select1"),child.getValue("Select2"),child.getValue("Select3"),child.getValue("Select4"),child.getValue("Select5"),child.getValue("Select6"),child.getValue("Select7"),child.getValue("Select8"));


				    $(ContentBody).innerHTML=response;
				}
				catch(e){
                     ShowTip("服务器返回错误代码或者页面出错",ContentBody);
 				     HideAfter3Seconds(ContentBody);
                        }
			},
			onFailure:function(transport){
				ShowTip(errInfo[0],ContentBody);
				HideAfter3Seconds(ContentBody);
			}
		});
}


function GetVoteRes(ContentTemplate,ContentBody,id,channelid,type,title,select1,select2,select3,select4,select5,select6,select7,select8)
{
    var temp=$(ContentTemplate).innerHTML;
    var str="";
    var sArray = new Array("Answer1","Answer2","Answer3","Answer4","Answer5","Answer6","Answer7","Answer8");
    var vArray = new Array(select1,select2,select3,select4,select5,select6,select7,select8);
    str += "<div class='title'>"+title+"</div>";
    var s = 0;
    if(type == 0)
    {   
        for (var i=0;i<sArray.length;i++)
        {
            s += 1;
            if (vArray[i] != '')
                if (s == 1)
                    str +="<div class='answerradio'><input type='radio'  name='radiobutton'  value="+sArray[i]+" checked>"+vArray[i]+"</input></div>";
                else
                    str +="<div class='answerradio'><input type='radio'  name='radiobutton'  value="+sArray[i]+">"+vArray[i]+"</input></div>";
        }
        
        var allcookies = document.cookie;
        var cookie_pos = allcookies.indexOf(id+ContentTemplate+type);

        // 如果找到了索引，就代表cookie存在，
        // 反之，就说明不存在。

	    if(cookie_pos == -1)
        {
            str += "<input value='我投票' class='button' type='button' onClick='PostVote(\""+ContentTemplate+"\",\""+ContentBody+"\","+id+","+type+",\"radiobutton\")'/>";
        }
        else
        {
            str += "<div class='thanks'>已经投过，谢谢！</div>";
        }
    }
    else
    {
        for (var i=0;i<sArray.length;i++)
        {
            if (vArray[i] != '')
            str +="<input class='answercheckbox' type='checkbox' name='checkboxgroup'  value="+sArray[i]+">"+vArray[i]+"</input>"
        }
        var allcookies = document.cookie;
        var cookie_pos = allcookies.indexOf(id+ContentTemplate+type);

        // 如果找到了索引，就代表cookie存在，
        // 反之，就说明不存在。

	    if(cookie_pos == -1)
        {
            str += "<div class='button' onClick='PostVote(\""+ContentTemplate+"\",\""+ContentBody+"\","+id+","+type+",\"checkboxgroup\")' >我投票</div>";
        }
        else
        {
            str += "<div class='thanks'>已经投过，谢谢！</div>";
        }
        
    }
    temp=temp.replace("$content$",str);
    return temp;
}

function PostVote(ContentTemplate,ContentBody,id,type,a)
{
    var str = "";
    var field = document.getElementsByName(a);
    if (field && field.length)
    {
        for (var i = 0; i<field.length;i++)
        {
            if (field[i].checked)
            {
                str += field[i].value;
                str += "$";
            }
        }
    }
    
    var response = "";	
	var url="ajax.aspx";
	
	var pars="&A=setVote&str=" + str +"&ID=" + id;
	pars +="&r="+Math.random();
	var myAjax= new Ajax.Request(
		url,
		{
			method:"get",
			parameters:pars,
			onSuccess:function(transport){
				xmlDoc=new EXDoc(transport.responseText);
			    try{
			
					var child=xmlDoc.root.contents[0];
					response+=GetVoteAns(ContentTemplate,child.getValue("ID"),child.getValue("ChannelID"),child.getValue("Title"),child.getValue("Select1"),child.getValue("Select2"),child.getValue("Select3"),child.getValue("Select4"),child.getValue("Select5"),child.getValue("Select6"),child.getValue("Select7"),child.getValue("Select8"),child.getValue("Answer1"),child.getValue("Answer2"),child.getValue("Answer3"),child.getValue("Answer4"),child.getValue("Answer5"),child.getValue("Answer6"),child.getValue("Answer7"),child.getValue("Answer8"),child.getValue("PubTime"),child.getValue("EndTime"),child.getValue("Sum"));


				    $(ContentBody).innerHTML=response;
				    
				     // --- 设置cookie
                    function setCookie(sName,sValue,expireHours) {
                    var cookieString = sName + "=" + escape(sValue);
                    if (expireHours>0) {//设置过期时间
                         var date = new Date();
                         date.setTime(date.getTime() + expireHours * 60 * 1000);//时间单位=毫秒
                         cookieString = cookieString + "; expire=" + date.toGMTString(); // date.toGMTString()转为GMT时间
                    }
                    document.cookie = cookieString;//写cookie
                    }
                    
                    setCookie(id+ContentTemplate+type,"true",20)//设置过期时间20分钟
                    //setCookie("visited","true")//不设置过期时间
				}
				catch(e){
                     ShowTip("服务器返回错误代码或者页面出错",ContentBody);
 				     HideAfter3Seconds(ContentBody);
                        }
			},
			onFailure:function(transport){
				ShowTip(errInfo[0],ContentBody);
				HideAfter3Seconds(ContentBody);
			}
		});

}


function GetVoteAns(ContentTemplate,id,channelid,title,select1,select2,select3,select4,select5,select6,select7,select8,answer1,answer2,answer3,answer4,answer5,answer6,answer7,answer8,pubtime,endtime,sum)
{
    var temp=$(ContentTemplate).innerHTML;
    var str="";
    var vArray = new Array(answer1,answer2,answer3,answer4,answer5,answer6,answer7,answer8);
    var sArray = new Array(select1,select2,select3,select4,select5,select6,select7,select8);
    str += "<div class='title'>"+title+"</div>";
    var w;
    for (var i=0;i<sArray.length;i++)
    {
        if (sArray[i] != '')
        {
            w = (vArray[i]/sum)*310;
            str +="<div class='answer'>"+sArray[i]+"</div>";
			str +="<span class='persent'>"+ForDight((vArray[i]/sum)*100,2)+"%</span>";
            str +="<div class='num'>"+vArray[i]+"</div>";
            str +="<div class='voteImg'><img src='images/bg7.gif' width="+w+" height = '20'></div>";
            str +="";
        }
    }
    
    str +="<div class='pubtime'>"+pubtime+"</div>";
    str +="<div class='endtime'>"+endtime+"</div>";
    temp=temp.replace("$content$",str);
    return temp;
}

/*  
*    ForDight(Dight,How):数值格式化函数，Dight要  
*    格式化的  数字，How要保留的小数位数。  
*/  
function  ForDight(Dight,How)  
{  
           Dight  =  Math.round  (Dight*Math.pow(10,How))/Math.pow(10,How);  
           return  Dight;  
} 

