/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var api;
$(document).ready(function(){
    $.sAjax("php/surveys.php", "serviceId=1", function(data){
        $("#survey").html("");
        $.each(data, function(idx){
            var row="";            
            if(idx>0)
                row+="-----------------------------";
            row+="<div><form>"
            row+=this.survey.text+"<br/>";
            for(i=0;i<this.options.length;i++){
                var item=this.options[i];            
                //$.log(item);
                row+="<input type='radio' name='option' value='"+item.id+"'/> "+item.text+"<br/>"
            }
            row+="<input type='button' value='Votar' onclick='sendAnswer(this.form)'/>";
            row+="<input type='hidden' name='surveyId' value='"+this.survey.id+"'/>";
            row+="</form></div>";
            
            $("#survey").append(row);
        })
    //$.log(data)
    })    
    api = new jGCharts.Api();
})

function sendAnswer(form){
    if($(form).children(':checked').length>0)
        $.sAjax("php/surveys.php", "serviceId=2&"+$(form).serialize(), function(json){
            $(form).replaceWith(
                $('<img>').attr('src', api.make({ 
                    data : json.data,//mandatory  
                    legend:json.legend,
                    type : 'bvg',
                    size : '200x200'
                })) );
            $.log(data);        
        })
    else
        alert("Selecciona una respuesta");
    
    
}



