function vote(poll) { var form = $('#pollcoursive_form_' + poll); $('#pollcoursive_notification_' + poll).hide(); if (!check_votes(poll)) { $('#pollcoursive_notification_' + poll).show(); $('#pollcoursive_notification_' + poll).html('Please vote on all the questions'); $('#pollcoursive_notification_' + poll).fadeOut(3000); return false; } make_request(form.serialize()); recalculate_results(poll); toggle_results(poll); hide_buttons(poll); } function make_request(params) { var d = new Date(); var img = new Image(1,1); img.src = "http://" + POLLCOURSIVE_HOST + "/vote.php?r=" + d.getTime() + "&" + params; } function toggle_results(poll) { $('#pollcoursive_vote_' + poll).slideToggle('slow'); $('#pollcoursive_results_' + poll).slideToggle('slow'); } function hide_buttons(poll) { $('#pollcoursive_buttons1_' + poll).hide(); $('#pollcoursive_buttons2_' + poll).hide(); } function recalculate_results(poll) { var form = $('#pollcoursive_form_' + poll); var tmp = form.serializeArray(); var results = {}; for (i in tmp) { results[tmp[i].name] = tmp[i].value; } var regexp = /\[(\d)*\]/; for (var i in results) { if (i.substr(0, 9) == 'questions') { var vote = results[i]; var match = regexp.exec(i); var question_id = match[1]; var new_votes = POLLCOURSIVE_VOTES[poll][question_id]['votes'] * 1 + 1; var new_average = (POLLCOURSIVE_VOTES[poll][question_id]['avg'] * POLLCOURSIVE_VOTES[poll][question_id]['votes'] + vote * 1) / new_votes; $('#pollcoursive_result_' + question_id).html(sprintf('%.2f (%d votes)', new_average, new_votes)); } } } function check_votes(poll) { var form = $('#pollcoursive_form_' + poll); var tmp = form.serializeArray(); var results = {}; for (i in tmp) { results[tmp[i].name] = tmp[i].value; } for (i in POLLCOURSIVE_VOTES[poll]) { var k = "questions[" + i + "]"; if (!results[k]) { return false; } } return true; } var POLLCOURSIVE_HOST = "pollcoursive.recoursive.com"; if (!POLLCOURSIVE_VOTES) var POLLCOURSIVE_VOTES = []; POLLCOURSIVE_VOTES[1] = {"1":{"id":"1","0":"1","poll_id":"1","1":"1","description":"Pollcoursive","2":"Pollcoursive","is_mandatory":"1","3":"1","avg":"5.5455","votes":"11"},"2":{"id":"2","0":"2","poll_id":"1","1":"1","description":"Mark Penn","2":"Mark Penn","is_mandatory":"1","3":"1","avg":"6.0909","votes":"11"},"3":{"id":"3","0":"3","poll_id":"1","1":"1","description":"Crummy old boring poll","2":"Crummy old boring poll","is_mandatory":"1","3":"1","avg":"6.0000","votes":"11"},"4":{"id":"4","0":"4","poll_id":"1","1":"1","description":"CNN Instapoll","2":"CNN Instapoll","is_mandatory":"1","3":"1","avg":"6.4545","votes":"11"}}; document.write("
Rate the following polling solutions (0 = lowest, 9 = highest)
Pollcoursive
Mark Penn
Crummy old boring poll
CNN Instapoll
Pollcoursive 5.55 (11 votes)
Mark Penn 6.09 (11 votes)
Crummy old boring poll 6.00 (11 votes)
CNN Instapoll 6.45 (11 votes)
"); /** * * Javascript sprintf * http://www.webtoolkit.info/ * * **/ sprintfWrapper = { init : function () { if (typeof arguments == "undefined") { return null; } if (arguments.length < 1) { return null; } if (typeof arguments[0] != "string") { return null; } if (typeof RegExp == "undefined") { return null; } var string = arguments[0]; var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g); var matches = new Array(); var strings = new Array(); var convCount = 0; var stringPosStart = 0; var stringPosEnd = 0; var matchPosEnd = 0; var newString = ''; var match = null; while (match = exp.exec(string)) { if (match[9]) { convCount += 1; } stringPosStart = matchPosEnd; stringPosEnd = exp.lastIndex - match[0].length; strings[strings.length] = string.substring(stringPosStart, stringPosEnd); matchPosEnd = exp.lastIndex; matches[matches.length] = { match: match[0], left: match[3] ? true : false, sign: match[4] || '', pad: match[5] || ' ', min: match[6] || 0, precision: match[8], code: match[9] || '%', negative: parseInt(arguments[convCount]) < 0 ? true : false, argument: String(arguments[convCount]) }; } strings[strings.length] = string.substring(matchPosEnd); if (matches.length == 0) { return string; } if ((arguments.length - 1) < convCount) { return null; } var code = null; var match = null; var i = null; for (i=0; i