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 | with (rateButton.style) |
---|
26 | { |
---|
27 | textIndent = "-50px"; //hide the text FF |
---|
28 | marginLeft = marginRight = 0; |
---|
29 | } |
---|
30 | |
---|
31 | if (i!=gRatingButtons.length-1 && rateButton.nextSibling.nodeType == 3 /*TEXT_NODE*/) |
---|
32 | rateButton.parentNode.removeChild(rateButton.nextSibling); |
---|
33 | if (i>0 && rateButton.previousSibling.nodeType == 3 /*TEXT_NODE*/) |
---|
34 | rateButton.parentNode.removeChild(rateButton.previousSibling); |
---|
35 | |
---|
36 | if(window.addEventListener){ // Mozilla, Netscape, Firefox |
---|
37 | rateButton.addEventListener("click", updateRating, false ); |
---|
38 | rateButton.addEventListener("mouseout", resetRatingStarDisplay, false ); |
---|
39 | rateButton.addEventListener("mouseover", updateRatingStarDisplayEvt, false ); |
---|
40 | } |
---|
41 | else if(window.attachEvent) { // IE |
---|
42 | rateButton.attachEvent("onclick", updateRating); |
---|
43 | rateButton.attachEvent("onmouseout", resetRatingStarDisplay); |
---|
44 | rateButton.attachEvent("onmouseover", updateRatingStarDisplayEvt); |
---|
45 | } |
---|
46 | } |
---|
47 | resetRatingStarDisplay(); |
---|
48 | } |
---|
49 | |
---|
50 | function resetRatingStarDisplay() |
---|
51 | { |
---|
52 | updateRatingStarDisplay( gUserRating ); |
---|
53 | } |
---|
54 | |
---|
55 | function updateRatingStarDisplay(userRating) |
---|
56 | { |
---|
57 | for (i=0; i<gRatingButtons.length; i++) |
---|
58 | { |
---|
59 | var rateButton = gRatingButtons[i]; |
---|
60 | if (userRating!=="" && userRating>=rateButton.initialRateValue ) |
---|
61 | { |
---|
62 | rateButton.className = "rateButtonStarFull"; |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | rateButton.className = "rateButtonStarEmpty"; |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | function updateRatingStarDisplayEvt(e) |
---|
72 | { |
---|
73 | if (e.target) |
---|
74 | updateRatingStarDisplay(e.target.initialRateValue); |
---|
75 | else //IE |
---|
76 | updateRatingStarDisplay(e.srcElement.initialRateValue); |
---|
77 | } |
---|
78 | |
---|
79 | function updateRating(e) |
---|
80 | { |
---|
81 | if (e.target) |
---|
82 | var rateButton = e.target; |
---|
83 | else //IE |
---|
84 | var rateButton = e.srcElement; |
---|
85 | if (rateButton.initialRateValue == gUserRating) |
---|
86 | return false; //nothing to do |
---|
87 | // some ajax here one day would be nice |
---|
88 | rateButton.value = rateButton.initialRateValue; // put back real value |
---|
89 | return true; |
---|
90 | } |
---|