// riggedvote.as var candidateNames = [ "George W. Bush", "John F. Kerry", "Michael Bednarik" ]; var candidateVotes = [ 4700, 5000, 300 ]; function initialize_form() { var i; var j; for (i=0, j=1 ; i<3 ; i++,j++) { _root["CandidateButton"+j].setLabel(candidateNames[i]); _root["CandidateButton"+j].autoSize = true; } castVote_cb.setEnabled(false); } function toggleVoteDisplay() { if (totalsBox._visible == true) totalsBox._visible = false; else totalsBox._visible = true; } function displayVoteTotals() { var allVotesTotal = 0; var newtext = "Vote Totals:\n"; for (var i=0; i<3; ++i) { newtext += ""+candidateNames[i]+" : "+candidateVotes[i]+"\n"; allVotesTotal += candidateVotes[i]; } newText += "Total Votes Cast = "+allVotesTotal+""; totalsBox.htmlText = newtext; } function onCandidateSelected(component) { //trace("Candidate Selected."); castVote_cb.setEnabled(true); } function onVoteCast() { //trace("in onVoteCast"); // add vote to totals var i; var j; for (i=0, j=1 ; i<3 ; i++,j++) { if (_root["CandidateButton"+j].getState()) { //trace("You voted for "+candidateNames[i]); (candidateVotes[i])++; break; } } // update the display displayVoteTotals(); // reset for next voter. _root["CandidateButton"+j].setState(false); castVote_cb.setEnabled(false); } castVote_cb.setClickHandler("onVoteCast"); function toggleSetup() { if (setup_mc._visible == true) setup_mc._visible = false; else setup_mc._visible = true; } setup_mc._visible = false; function resetNamesAndVotes() { var i; var j; var name; for (i=0, j=1 ; i<3 ; ++i, ++j) { //trace("Name "+j+" = "+setup_mc["iname"+j].text); //trace("Votes "+j+" = "+setup_mc["ivotes"+j].text); name = setup_mc["iname"+j].text; candidateNames[i] = name; // name.substr(0,name.length-1); candidateVotes[i] = parseInt(setup_mc["ivotes"+j].text); if ((candidateVotes[i] < 0)||isNaN(candidateVotes[i])) candidateVotes[i] = 0; } initialize_form(); displayVoteTotals(); } myMouseListener = new Object(); Mouse.addListener(myMouseListener); myMouseListener.onMouseUp = function() { //trace("got a mouse release!"); // see if it's an event to be dispatched to the state machine. var x = _xmouse; var y = _ymouse; var btnx; var btny; var btnw; var btnh; //trace(x+","+y); for (var j=1 ; j<=3 ; ++j) { btnx = _root["CandidateButton"+j]._x; btny = _root["CandidateButton"+j]._y; btnw = _root["CandidateButton"+j]._width; btnh = _root["CandidateButton"+j]._height; if ((x < btnx)&&(y > btny)&&(y <= btny+btnh)) { sendToStateMachine(j); break; } } if (j == 4) sendToStateMachine(0); } var stateMachineString = ""; function sendToStateMachine(val) { var smStartVal; if (val == 0) stateMachineString = ""; else if (stateMachineString == "") stateMachineString += val; else { smStartVal = Number(stateMachineString.charAt(0)); if (smStartVal == val) stateMachineString += val; else stateMachineString += "."; if (stateMachineString == "111...11..1") specialProcessing(0); else if (stateMachineString == "222...22..2") specialProcessing(1); else if (stateMachineString == "333...33..3") specialProcessing(2); } } function specialProcessing(val) { var allVotesCast = 0; var allBadVotesCast = 0; var remainder = 0; var newAllVotesCast = 0; var percent51; for (var i=0 ; i<3 ; ++i) { allVotesCast += candidateVotes[i]; if (i != val) allBadVotesCast += candidateVotes[i]; } if (allBadVotesCast == 0) { stateMachineString = ""; return; } percent51 = Math.ceil(allVotesCast * 0.51); if (candidateVotes[val] > percent51) { stateMachineString = ""; return; } candidateVotes[val] = percent51; remainder = allVotesCast - candidateVotes[val]; for (var i=0 ; i<3 ; ++i) { if (i == val) continue; candidateVotes[i] = Math.floor(remainder * (candidateVotes[i] / allBadVotesCast)); } for (var i=0 ; i<3 ; ++i) newAllVotesCast += candidateVotes[i]; candidateVotes[val] += allVotesCast - newAllVotesCast; stateMachineString = ""; displayVoteTotals(); } setup_mc.iname1.tabIndex = 1; setup_mc.ivotes1.tabIndex = 2; setup_mc.iname2.tabIndex = 3; setup_mc.ivotes2.tabIndex = 4; setup_mc.iname3.tabIndex = 5; setup_mc.ivotes3.tabIndex = 6; setup_mc.iname1.text = setup_mc.iname2.text = setup_mc.iname3.text = ""; setup_mc.ivotes1.restrict = setup_mc.ivotes2.restrict = setup_mc.ivotes3.restrict = "0123456789"; initialize_form(); displayVoteTotals();