1 | makeNiceRatingForm(); |
---|
2 | |
---|
3 | function makeNiceRatingForm() |
---|
4 | { |
---|
5 | var form = document.getElementById('rateForm'); |
---|
6 | if (!form) return; //? template changed |
---|
7 | gRatingButtons = form.getElementsByTagName('input'); |
---|
8 | |
---|
9 | gUserRating = ""; |
---|
10 | for (var i=0; i<gRatingButtons.length; i++) |
---|
11 | { |
---|
12 | if ( gRatingButtons[i].type=="button" ) |
---|
13 | { |
---|
14 | gUserRating = gRatingButtons[i].value; |
---|
15 | break; |
---|
16 | } |
---|
17 | } |
---|
18 | |
---|
19 | for (var i=0; i<gRatingButtons.length; i++) |
---|
20 | { |
---|
21 | var rateButton = gRatingButtons[i]; |
---|
22 | rateButton.initialRateValue = rateButton.value; // save it as a property |
---|
23 | |
---|
24 | rateButton.value = ""; //hide the text IE/Opera |
---|
25 | rateButton.style.textIndent = "-50px"; //hide the text FF |
---|
26 | |
---|
27 | if (i!=gRatingButtons.length-1 && rateButton.nextSibling.nodeType == 3 /*TEXT_NODE*/) |
---|
28 | rateButton.parentNode.removeChild(rateButton.nextSibling); |
---|
29 | if (i>0 && rateButton.previousSibling.nodeType == 3 /*TEXT_NODE*/) |
---|
30 | rateButton.parentNode.removeChild(rateButton.previousSibling); |
---|
31 | |
---|
32 | if(window.addEventListener){ // Mozilla, Netscape, Firefox |
---|
33 | rateButton.addEventListener("click", updateRating, false ); |
---|
34 | rateButton.addEventListener("mouseout", resetRatingStarDisplay, false ); |
---|
35 | rateButton.addEventListener("mouseover", updateRatingStarDisplayEvt, false ); |
---|
36 | } |
---|
37 | else if(window.attachEvent) { // IE |
---|
38 | rateButton.attachEvent("onclick", updateRating); |
---|
39 | rateButton.attachEvent("onmouseout", resetRatingStarDisplay); |
---|
40 | rateButton.attachEvent("onmouseover", updateRatingStarDisplayEvt); |
---|
41 | } |
---|
42 | } |
---|
43 | resetRatingStarDisplay(); |
---|
44 | } |
---|
45 | |
---|
46 | function resetRatingStarDisplay() |
---|
47 | { |
---|
48 | updateRatingStarDisplay( gUserRating ); |
---|
49 | } |
---|
50 | |
---|
51 | function updateRatingStarDisplay(userRating) |
---|
52 | { |
---|
53 | for (i=0; i<gRatingButtons.length; i++) |
---|
54 | { |
---|
55 | var rateButton = gRatingButtons[i]; |
---|
56 | if (userRating!=="" && userRating>=rateButton.initialRateValue ) |
---|
57 | { |
---|
58 | rateButton.className = "rateButtonStarFull"; |
---|
59 | } |
---|
60 | else |
---|
61 | { |
---|
62 | rateButton.className = "rateButtonStarEmpty"; |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | function updateRatingStarDisplayEvt(e) |
---|
68 | { |
---|
69 | if (e.target) |
---|
70 | updateRatingStarDisplay(e.target.initialRateValue); |
---|
71 | else //IE |
---|
72 | updateRatingStarDisplay(e.srcElement.initialRateValue); |
---|
73 | } |
---|
74 | |
---|
75 | function updateRating(e) |
---|
76 | { |
---|
77 | if (e.target) |
---|
78 | var rateButton = e.target; |
---|
79 | else //IE |
---|
80 | var rateButton = e.srcElement; |
---|
81 | if (rateButton.initialRateValue == gUserRating) |
---|
82 | return false; //nothing to do |
---|
83 | // some ajax here one day would be nice |
---|
84 | rateButton.value = rateButton.initialRateValue; // put back real value |
---|
85 | return true; |
---|
86 | } |
---|