/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function(){
   /* $.getJSON("SvtSurveysWeb", "serviceId=1", function(data){
        $("#survey").html("");
        $.each(data.surveys, function(idx){
            $.log(this);
            var row="";            
            if(idx>0)
                row+="-----------------------------";
            row+="<div><form>"
            row+="<div class='rowTitle'>"+this.text+"</div>";
            for(i=0;i<this.surveryOptionList.length;i++){
                var item=this.surveryOptionList[i];            
                //$.log(item);
                row+="<input type='radio' name='option' value='"+item.surveyOptionId+"'/> "+item.text+"<br/>"
            }
            row+="<input type='button' value='Votar' onclick='sendAnswer(this.form)'/>";
            row+="<input type='hidden' name='surveyId' value='"+this.surveyId+"'/>";
            row+="</form></div>";
            
            $("#survey").append(row);
        })
    //$.log(data)
    })     */ 
})

function sendAnswer(form){
    if($(form).children(':checked').length>0)
        $.getJSON("SvtSurveysWeb", "serviceId=2&"+$(form).serialize(), function(json){                      
            
            //var option = data.option;
            var table = '<table width="100%" border="0" cellpadding="2" cellspacing="0">';
            //var length = json.data.length;            
            table += "<caption class = 'survey-title'>"+json.title+"</caption>";     
            $.each(json.data,function(){
                var percent = Math.round((this[1]*100)/json.total);                
                table += "<tr>";                
                table += "<td width='100%'>"+this[0]+" ("+this[1]+") </td>";                   
                table += "</tr> <tr>";
                table += "<td width='100%'><div class='survey-bar' style='width:"+percent+"%; height:20px'> "+percent+"%</div></td>";              
                table += "</tr>";    
            });
            table += "</table>";
            
            $(form).replaceWith(table);            
            
            
            $.log(json);        
        })
    else
        alert("Selecciona una respuesta");
    
    
}



