Calculate.htc
4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Defines an event of the HTML Component (HTC) to be exposed to the containing document.
// This example is derived from a calculator behavior sample.
// Whenever the result changes, the HTC fires a custom onResultChange event back to the page,
// passing the result as a custom property of the event object.
// <PUBLIC:EVENT
// NAME = sName
// ID = sEventID
// />
<PUBLIC:EVENT NAME="onResultChange" ID="rcID" />
// Binds a function to an event, so that the function is called whenever the event fires on the specified object
// <PUBLIC:ATTACH
// EVENT = sEvent
// FOR = document | element | window
// ONEVENT = sEventHandler
// ID = sID
// />
// EVENT
// Required. String that specifies the name of a Dynamic HTML (DHTML) event, or any of the events specific to the HTML Component (HTC) that are listed in the HTC Reference.
// FOR
// Optional. String that specifies one of the following values to identify the source of the event.
// document Refers to the document object.
// element Default. Refers to the element to which the behavior is attached.
// window Refers to the window object.
// ONEVENT
// Required. String that specifies an inline script or a direct invocation of the event handler function.
// ID
// Optional. String that uniquely identifies the PUBLIC:ATTACH element within the component. This attribute is analogous to the ID attribute in DHTML.
<PUBLIC:ATTACH EVENT="onclick" ONEVENT="doCalc()" />
<PUBLIC:ATTACH EVENT="onkeydown" ONEVENT="doCalc()" />
<SCRIPT LANGUAGE="JScript">
var sResult = '0';
var num1 = 0;
var num2 = 0;
var bNewNumber=true;
var sOperation = '+';
var bEquals = false;
function sign(sVal)
{
num2 = parseFloat(sResult);
sResult = -1 * num2;
bNewNumber=true;
}
function operation (sVal)
{
num2 = parseFloat(sResult);
if (sOperation == '+')
sResult = num1+num2;
else if (sOperation == '-')
sResult = num1 - num2;
else if (sOperation == '*') {
num1mul = 1 ; num2mul = 1 ;
tmpnum1 = "" + num1 ; tmpnum2 = sResult ;
dotindex1 = tmpnum1.indexOf(".") ;
dotindex2 = tmpnum2.indexOf(".") ;
if(dotindex1 != -1) {
dotlength1 = tmpnum1.length - dotindex1 -1 ;
num1mul = Math.pow(10,dotlength1) ;
}
if(dotindex2 != -1) {
dotlength2 = tmpnum2.length - dotindex2 -1 ;
num2mul = Math.pow(10,dotlength2) ;
}
sResult = ((num1*num1mul) * (num2*num2mul))/(num1mul*num2mul);
}
else if (sOperation == '/')
{
if (num2 == 0)
sResult = "Error";
else
sResult = Math.round((num1 / num2)*1000)/1000 ;
}
if (sVal != '=')
{
sOperation = sVal;
// num1 = parseFloat (sResult);
num1 = sResult
}
else
{
// bEquals = true;
num1 = 0;
sOperation = '+';
}
bNewNumber=true;
}
function digit (sVal)
{
if (bNewNumber)
sResult= sVal;
else
sResult= sResult + sVal;
bNewNumber=false;
}
function clear()
{
sResult= '0';
num1 = 0;
num2 = 0;
sOperation = '+';
bNewNumber=true;
}
function stripBlanks (sVal)
{
var sTemp = "";
var i;
for (i=0; i < sVal.length; i++)
{
x = sVal.substr(i,1);
if (x != ' ')
sTemp = sTemp + x;
}
return sTemp;
}
function KeyCodeToChar(iKeyCode)
{
switch (iKeyCode)
{
case 48: return '0';
case 49: return '1';
case 50: return '2';
case 51: return '3';
case 52: return '4';
case 53: return '5';
case 54: return '6';
case 55: return '7';
case 56: return '8';
case 57: return '9';
case 27: return 'C';
case 190:return '.';
case 189:return '-';
case 191:return '/';
// 189: '-';
// 187: '=';
// 187+shift : '+'
// 56+shift : '*'
// 191: '/'
}
}
function doCalc()
{
oElement = event.srcElement;
window.status = oElement.value;
if (event.type == "keydown")
sVal = KeyCodeToChar (event.keyCode);
else
{
if (event.srcElement.type != "button")
return;
sVal = stripBlanks(event.srcElement.value);
}
if ((sResult == "Error") && (sVal != 'C'))
return;
if ((sVal=='0') || (sVal == '1') ||
(sVal=='2') || (sVal == '3') ||
(sVal=='4') || (sVal == '5') ||
(sVal=='6') || (sVal == '7') ||
(sVal=='8') || (sVal == '9') || (sVal == '.'))
digit (sVal);
else if ((sVal == '+') || (sVal == '-') ||
(sVal == '*') || (sVal == '/') ||
(sVal == '='))
operation (sVal);
else if (sVal == '+/-')
sign (sVal);
else if (sVal == 'C')
clear ();
// Generates an event object for passing event context information when using the fireEvent method.
oEvent = createEventObject();
oEvent.result = sResult;
rcID.fire (oEvent);
}
</SCRIPT>