document.observe("dom:loaded", function() {
	Event.observe('pollForm', 'submit', function(event) {
    
		var pollDetails = $('pollForm').serialize();
		var serverurl = '/front_website/pollResponse.php';
		var url = serverurl+'?'+pollDetails;
		//alert(url);
	  	var params = '';
	  	var ajax = new Ajax.Request(url,{method:'get',
	  			parameters:params,
	  			onSuccess:function(transport){
                    // jQuery and HBX libraries are installed check for hbx tracking
                    if (typeof jQuery == 'function' && typeof _hbSet == 'function' && typeof _hbSend == 'function') {
                        var hbxSeg = [];
                        jQuery('select[name^=answer_] :selected[hbx-pop]').each(function(i, elem) {
                            hbxSeg.push(jQuery(elem).attr('hbx-pop'));
                        });
                        jQuery('input[name^=answer_][hbx-pop]:checked').each(function(i, elem) {
                            hbxSeg.push(jQuery(elem).attr('hbx-pop'));
                        });
                        if (hbxSeg.length > 0) {
                            _hbSet('seg', hbxSeg.join(','));
                            _hbSend();
                            //console.log('Logged seg as ' + hbxSeg.join(','));
                        }
                    }
                                                
	  				if (arguments[0].responseText == 0){
	  					retakePoll();
	  				} else {
	  					var val = arguments[0].responseText;	
		  				$('pollquestions').style.display = 'none';
		  				$('pollresults').update(val);
	  				}
	  			},
	  			onFailure:retakePoll});
	    Event.stop(event); // stop the form from submitting
	});	
});

function retakePoll(){
	$('pollresults').update('<span style="color:red;">An error occured while processing the poll. Please ensure you have filled in the options.</span>');
	$('pollquestions').style.display = 'block';	
}

