$(document).ready(function() {

  $('a.city').click(function() { $('#city').show(); $('#country').hide(); $('#state').hide(); 
  	$('select[name="state[]"]').val('');  $('select[name="country[]"]').val(''); return false; });
  $('a.state').click(function() { $('#city').hide(); $('#country').hide(); $('#state').show();
  	$('select[name="city[]"]').val('');  $('select[name="country[]"]').val(''); return false; });
  $('a.country').click(function() { $('#city').hide(); $('#country').show(); $('#state').hide();
  	$('select[name="state[]"]').val('');  $('select[name="city[]"]').val(''); return false; });

  $('#surveyform').ajaxForm();  

  submitsurvey();

/*  $('#surveyform input[type="checkbox"]').bind('click', function() {

    submitsurvey();


  });
*/
  $('.submitsurvey').bind('click', function() {

  $('#messagebox').fadeIn();
    submitsurvey();
  $('#messagebox').fadeOut();
    return false;

  });

  $('.resetsurvey').bind('click', function() {


  $('#messagebox').fadeIn();
    document.getElementById('surveyform').reset();
    submitsurvey();
  $('#messagebox').fadeOut();
    return false;

  });




});

function submitsurvey() {


  $('#surveyform input[type="checkbox"]').parent().parent()
  	.find("td[width='400']").find("img").attr('width', 0).parent().find("span").html('');

  $('#surveyform').ajaxSubmit({dataType: 'json', success: updatesurvey});


}

function updatesurvey(data) {

  $.each(data.items, function(key,item){
    $.each(item, function(val,count){

	$("#surveyform input[name='" + key + "[]'][value='"+ val +"']").parent().parent()
		.find("td[width='400']").find("img").attr('width', 1.5*count).parent().find("span").html(count+'%');

    });

  });


}
