1/************************************************************************************************************ 2Ajax poller 3Copyright (C) 2006 DTHMLGoodies.com, Alf Magne Kalleland 4 5This library is free software; you can redistribute it and/or 6modify it under the terms of the GNU Lesser General Public 7License as published by the Free Software Foundation; either 8version 2.1 of the License, or (at your option) any later version. 9 10This library is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13Lesser General Public License for more details. 14 15You should have received a copy of the GNU Lesser General Public 16License along with this library; if not, write to the Free Software 17Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 19Dhtmlgoodies.com., hereby disclaims all copyright interest in this script 20written by Alf Magne Kalleland. 21 22Alf Magne Kalleland, 2006 23Owner of DHTMLgoodies.com 24 25************************************************************************************************************/ 26 27 28 29 30var serverSideFile = 'ajax-poller-cast-vote-php.php'; 31var voteLeftImage = 'images/graph_left_1.gif'; 32var voteRightImage = 'images/graph_right_1.gif'; 33var voteCenterImage = 'images/graph_middle_1.gif'; 34 35var graphMaxWidth = 130; // It will actually be a little wider than this because of the rounded image at the left and right 36var graphMinWidth = 15; // Minimum size of graph 37var pollScrollSpeed = 5; // Lower = faster 38var useCookiesToRememberCastedVotes = false; // Use cookie to remember casted votes 39var txt_totalVotes = 'Total number of votes: '; 40 41var ajaxObjects = new Array(); 42var pollVotes = new Array(); 43var pollVoteCounted = new Array(); 44var totalVotes = new Array(); 45 46 47/* Preload images */ 48 49var preloadedImages = new Array(); 50preloadedImages[0] = new Image(); 51preloadedImages[0].src = voteLeftImage; 52preloadedImages[1] = new Image(); 53preloadedImages[1].src = voteRightImage; 54preloadedImages[2] = new Image(); 55preloadedImages[2].src = voteCenterImage; 56 57/* 58These cookie functions are downloaded from 59http://www.mach5.com/support/analyzer/manual/html/General/CookiesJavaScript.htm 60*/ 61function Poller_Get_Cookie(name) { 62 var start = document.cookie.indexOf(name+"="); 63 var len = start+name.length+1; 64 if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 65 if (start == -1) return null; 66 var end = document.cookie.indexOf(";",len); 67 if (end == -1) end = document.cookie.length; 68 return unescape(document.cookie.substring(len,end)); 69} 70// This function has been slightly modified 71function Poller_Set_Cookie(name,value,expires,path,domain,secure) { 72 expires = expires * 60*60*24*1000; 73 var today = new Date(); 74 var expires_date = new Date( today.getTime() + (expires) ); 75 var cookieString = name + "=" +escape(value) + 76 ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 77 ( (path) ? ";path=" + path : "") + 78 ( (domain) ? ";domain=" + domain : "") + 79 ( (secure) ? ";secure" : ""); 80 document.cookie = cookieString; 81} 82 83 84function showVoteResults(pollId,ajaxIndex) 85{ 86 87 document.getElementById('poller_waitMessage' + pollId).style.display='none'; 88 89 var xml = ajaxObjects[ajaxIndex].response; 90 xml = xml.replace(/\n/gi,''); 91 92 var reg = new RegExp("^.*?<pollerTitle>(.*?)<.*$","gi"); 93 var pollerTitle = xml.replace(reg,'$1'); 94 95 var resultDiv = document.getElementById('poller_results' + pollId); 96 97 var titleP = document.createElement('P'); 98 titleP.className='result_pollerTitle'; 99 titleP.innerHTML = pollerTitle; 100 resultDiv.appendChild(titleP); 101 102 var options = xml.split(/<option>/gi); 103 104 pollVotes[pollId] = new Array(); 105 totalVotes[pollId] = 0; 106 for(var no=1;no<options.length;no++){ 107 108 var elements = options[no].split(/</gi); 109 var currentOptionId = false; 110 for(var no2=0;no2<elements.length;no2++){ 111 if(elements[no2].substring(0,1)!='/'){ 112 var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1'); 113 var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1'); 114 115 if(key.indexOf('optionText')>=0){ 116 var pOption = document.createElement('P'); 117 pOption.className='result_pollerOption'; 118 pOption.innerHTML = value; 119 resultDiv.appendChild(pOption); 120 121 } 122 123 if(key.indexOf('optionId')>=0){ 124 currentOptionId = value/1; 125 } 126 127 if(key.indexOf('votes')>=0){ 128 var voteDiv = document.createElement('DIV'); 129 voteDiv.className='result_pollGraph'; 130 resultDiv.appendChild(voteDiv); 131 132 var leftImage = document.createElement('IMG'); 133 leftImage.src = voteLeftImage; 134 voteDiv.appendChild(leftImage); 135 136 137 var numberDiv = document.createElement('DIV'); 138 numberDiv.style.backgroundImage = 'url(\'' + voteCenterImage + '\')'; 139 numberDiv.innerHTML = '0%'; 140 numberDiv.id = 'result_voteTxt' + currentOptionId; 141 voteDiv.appendChild(numberDiv); 142 143 var rightImage = document.createElement('IMG'); 144 rightImage.src = voteRightImage; 145 voteDiv.appendChild(rightImage); 146 147 pollVotes[pollId][currentOptionId] = value; 148 totalVotes[pollId] = totalVotes[pollId]/1 + value/1; 149 } 150 } 151 } 152 } 153 154 var totalVoteP = document.createElement('P'); 155 totalVoteP.className = 'result_totalVotes'; 156 totalVoteP.innerHTML = txt_totalVotes + totalVotes[pollId]; 157 voteDiv.appendChild(totalVoteP); 158 159 setPercentageVotes(pollId); 160 slideVotes(pollId,0); 161} 162 163function setPercentageVotes(pollId) 164{ 165 for(var prop in pollVotes[pollId]){ 166 pollVotes[pollId][prop] = Math.round( (pollVotes[pollId][prop] / totalVotes[pollId]) * 100); 167 } 168 169 var currentSum = 0; 170 for(var prop in pollVotes[pollId]){
171 currentSum = currentSum + pollVotes[pollId][prop]/1; 172 } 173 pollVotes[pollId][prop] = pollVotes[pollId][prop] + (100-currentSum); 174 175 176} 177 178function slideVotes(pollId,currentPercent) 179{ 180 currentPercent = currentPercent/1 + 1; 181 182 for(var prop in pollVotes[pollId]){ 183 if(pollVotes[pollId][prop]>=currentPercent){ 184 var obj = document.getElementById('result_voteTxt' + prop); 185 obj.innerHTML = currentPercent + '%'; 186 obj.style.width = Math.max(graphMinWidth,Math.round(currentPercent/100*graphMaxWidth)) + 'px'; 187 } 188 } 189 190 if(currentPercent<100)setTimeout('slideVotes("' + pollId + '","' + currentPercent + '")',pollScrollSpeed); 191} 192 193 194function prepareForPollResults(pollId) 195{ 196 document.getElementById('poller_waitMessage' + pollId).style.display='block'; 197 document.getElementById('poller_question' + pollId).style.display='none'; 198} 199 200 201function castMyVote(pollId,formObj) 202{ 203 var elements = formObj.elements['vote[' + pollId + ']']; 204 var optionId = false; 205 for(var no=0;no<elements.length;no++){ 206 if(elements[no].checked)optionId = elements[no].value; 207 } 208 Poller_Set_Cookie('dhtmlgoodies_poller_' + pollId,'1',6000000); 209 if(optionId){ 210 211 var ajaxIndex = ajaxObjects.length; 212 ajaxObjects[ajaxIndex] = new sack(); 213 ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId + '&optionId=' + optionId; 214 prepareForPollResults(pollId); 215 ajaxObjects[ajaxIndex].onCompletion = function(){ showVoteResults(pollId,ajaxIndex); }; // Specify function that will be executed after file has been found 216 ajaxObjects[ajaxIndex].runAJAX(); // Execute AJAX function 217 218 } 219} 220 221function displayResultsWithoutVoting(pollId) 222{ 223 var ajaxIndex = ajaxObjects.length; 224 ajaxObjects[ajaxIndex] = new sack(); 225 ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId; 226 prepareForPollResults(pollId); 227 ajaxObjects[ajaxIndex].onCompletion = function(){ showVoteResults(pollId,ajaxIndex); }; // Specify function that will be executed after file has been found 228 ajaxObjects[ajaxIndex].runAJAX(); // Execute AJAX function 229 230 231} 232
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.