HTMLForm:
[lhc/web/wiklou.git] / skins / common / prefs.js
1 // Timezone stuff
2 // tz in format [+-]HHMM
3 window.checkTimezone = function( tz, msg ) {
4 var localclock = new Date();
5 // returns negative offset from GMT in minutes
6 var tzRaw = localclock.getTimezoneOffset();
7 var tzHour = Math.floor( Math.abs( tzRaw ) / 60 );
8 var tzMin = Math.abs( tzRaw ) % 60;
9 var tzString = ( ( tzRaw >= 0 ) ? '-' : '+' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
10 if ( tz != tzString ) {
11 var junk = msg.split('$1');
12 document.write( junk[0] + 'UTC' + tzString + junk[1] );
13 }
14 };
15
16 window.timezoneSetup = function() {
17 var tzSelect = document.getElementById( 'mw-input-wptimecorrection' );
18 var tzTextbox = document.getElementById( 'mw-input-wptimecorrection-other' );
19
20 if ( tzSelect && tzTextbox ) {
21 addHandler( tzSelect, 'change', function( e ) { updateTimezoneSelection( false ); } );
22 addHandler( tzTextbox, 'blur', function( e ) { updateTimezoneSelection( true ); } );
23 }
24
25 updateTimezoneSelection( false );
26 };
27
28 // in [-]HH:MM format...
29 // won't yet work with non-even tzs
30 window.fetchTimezone = function() {
31 // FIXME: work around Safari bug
32 var localclock = new Date();
33 // returns negative offset from GMT in minutes
34 var tzRaw = localclock.getTimezoneOffset();
35 var tzHour = Math.floor( Math.abs( tzRaw ) / 60 );
36 var tzMin = Math.abs( tzRaw ) % 60;
37 var tzString = ( ( tzRaw >= 0 ) ? '-' : '' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
38 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
39 return tzString;
40 };
41
42 window.guessTimezone = function() {
43 var textbox = document.getElementById( 'mw-input-wptimecorrection-other' );
44 var selector = document.getElementById( 'mw-input-wptimecorrection' );
45
46 selector.value = 'other';
47 textbox.value = fetchTimezone();
48 textbox.disabled = false; // The changed handler doesn't trip, obviously.
49 updateTimezoneSelection( true );
50 };
51
52 window.updateTimezoneSelection = function( force_offset ) {
53 var selector = document.getElementById( 'mw-input-wptimecorrection' );
54
55 if ( selector.value == 'guess' ) {
56 return guessTimezone();
57 }
58
59 var textbox = document.getElementById( 'mw-input-wptimecorrection-other' );
60 var localtimeHolder = document.getElementById( 'wpLocalTime' );
61 var servertime = document.getElementsByName( 'wpServerTime' )[0].value;
62 var minDiff = 0;
63
64 // Compatibility code.
65 if ( !selector.value ) {
66 selector.value = selector.options[selector.selectedIndex].value;
67 }
68
69 // Handle force_offset
70 if ( force_offset ) {
71 selector.value = 'other';
72 }
73
74 // Get min_diff
75 if ( selector.value == 'other' ) {
76 // Grab data from the textbox, parse it.
77 var diffArr = textbox.value.split(':');
78 if ( diffArr.length == 1 ) {
79 // Specification is of the form [-]XX
80 minDiff = parseInt( diffArr[0], 10 ) * 60;
81 } else {
82 // Specification is of the form [-]XX:XX
83 minDiff = Math.abs( parseInt( diffArr[0], 10 ) ) * 60 + parseInt( diffArr[1], 10 );
84 if ( parseInt( diffArr[0], 10 ) < 0 ) {
85 minDiff = -minDiff;
86 }
87 }
88 } else {
89 // Grab data from the selector value
90 var diffArr = selector.value.split('|');
91 minDiff = parseInt( diffArr[1], 10 );
92 }
93
94 // Gracefully handle non-numbers.
95 if ( isNaN( minDiff ) ) {
96 minDiff = 0;
97 }
98
99 // Determine local time from server time and minutes difference, for display.
100 var localTime = parseInt( servertime, 10 ) + minDiff;
101
102 // Bring time within the [0,1440) range.
103 while ( localTime < 0 ) {
104 localTime += 1440;
105 }
106 while ( localTime >= 1440 ) {
107 localTime -= 1440;
108 }
109
110 // Split to hour and minute
111 var hour = String( Math.floor( localTime / 60 ) );
112 if ( hour.length < 2 ) {
113 hour = '0' + hour;
114 }
115 var min = String(localTime%60);
116 if ( min.length < 2 ) {
117 min = '0' + min;
118 }
119 changeText( localtimeHolder, hour + ':' + min );
120
121 // If the user selected from the drop-down, fill the offset field.
122 if ( selector.value != 'other' ) {
123 hour = String( Math.abs( Math.floor( minDiff / 60 ) ) );
124 if ( hour.length < 2 ) {
125 hour = '0' + hour;
126 }
127 if ( minDiff < 0 ) {
128 hour = '-' + hour;
129 }
130 min = String(minDiff%60);
131 if ( min.length < 2 ) {
132 min = '0' + min;
133 }
134 textbox.value = hour + ':' + min;
135 }
136 };
137
138 addOnloadHook( timezoneSetup );