2010-07-07 11:30:04 +07:00
|
|
|
|
|
|
|
function Calc()
|
|
|
|
{
|
2014-07-12 08:11:30 +07:00
|
|
|
this.validateUp = validateUp;
|
|
|
|
this.validatePress = validatePress;
|
2010-07-07 11:30:04 +07:00
|
|
|
this.clear = clear;
|
|
|
|
this.clearAll = clearAll;
|
|
|
|
this.evaluate = evaluate;
|
|
|
|
this.append = append;
|
|
|
|
|
2014-07-12 08:11:30 +07:00
|
|
|
function validatePress(displayTextId, calcTextId, integral, separatorKey, e)
|
2010-07-07 11:30:04 +07:00
|
|
|
{
|
|
|
|
var key;
|
|
|
|
|
|
|
|
if(window.event)
|
|
|
|
key = e.keyCode; //IE
|
|
|
|
else
|
|
|
|
key = e.which; //Firefox
|
2014-07-12 08:11:30 +07:00
|
|
|
// console.log("validatePress: " + displayTextId + " / " + calcTextId + " / " + integral + " / " + separatorKey + " / " + key);
|
|
|
|
if (key == 61) // =
|
2010-07-07 11:30:04 +07:00
|
|
|
{
|
2013-03-08 12:51:57 +07:00
|
|
|
evaluate(displayTextId, calcTextId, String.fromCharCode(separatorKey));
|
2010-07-07 11:30:04 +07:00
|
|
|
}
|
2014-07-12 08:11:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
function validateUp(displayTextId, calcTextId, integral, separatorKey, e, processDotKeypad)
|
|
|
|
{
|
|
|
|
var key;
|
|
|
|
if(window.event)
|
|
|
|
key = e.keyCode; //IE
|
|
|
|
else
|
|
|
|
key = e.which; //Firefox
|
|
|
|
// console.log("validateUp: " + displayTextId + " / " + calcTextId + " / " + integral + " / " + separatorKey + " / " + key + " / " + processDotKeypad);
|
|
|
|
if (key == 13) // Enter
|
2010-07-07 11:30:04 +07:00
|
|
|
{
|
2014-07-12 08:11:30 +07:00
|
|
|
evaluate(displayTextId, calcTextId, String.fromCharCode(separatorKey));
|
2010-07-07 11:30:04 +07:00
|
|
|
}
|
2014-07-12 08:11:30 +07:00
|
|
|
else if (processDotKeypad && separatorKey != 46 && (key == 110 || key == 190) && !window.opera) // numeric dot on keypad (not working for opera)
|
2010-07-07 11:30:04 +07:00
|
|
|
{
|
2014-07-12 08:11:30 +07:00
|
|
|
append(calcTextId, String.fromCharCode(separatorKey));
|
|
|
|
e.stop;
|
2010-07-07 11:30:04 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearAll(calcTextId)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-09-04 16:27:04 +07:00
|
|
|
var id = "$".concat(calcTextId);
|
|
|
|
var calcText = jq(id)[0];
|
2010-07-07 11:30:04 +07:00
|
|
|
calcText.value = "";
|
|
|
|
}
|
|
|
|
catch (err)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear(calcTextId)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-09-04 16:27:04 +07:00
|
|
|
var id = "$".concat(calcTextId);
|
|
|
|
var calcText = jq(id)[0];
|
2010-07-07 11:30:04 +07:00
|
|
|
var val = calcText.value;
|
|
|
|
if (val != "")
|
|
|
|
{
|
|
|
|
val = val.substring(0, val.length - 1);
|
|
|
|
calcText.value = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (err)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluate(displayTextId, calcTextId, separator)
|
|
|
|
{
|
2014-07-12 08:11:30 +07:00
|
|
|
// console.log("evaluate: " + displayTextId + " / " + calcTextId + " / " + separator);
|
|
|
|
var newValue = "error";
|
2010-07-07 11:30:04 +07:00
|
|
|
try
|
|
|
|
{
|
2012-09-04 16:27:04 +07:00
|
|
|
var id = "$".concat(calcTextId);
|
|
|
|
var calcText = jq(id)[0];
|
2010-07-07 11:30:04 +07:00
|
|
|
var value = calcText.value;
|
|
|
|
if (separator != '.')
|
|
|
|
{
|
2013-09-05 07:18:13 +07:00
|
|
|
var re = new RegExp("[" + separator + "]", "g");
|
2010-07-07 11:30:04 +07:00
|
|
|
value = value.replace(re,'.');
|
|
|
|
}
|
2014-07-12 08:11:30 +07:00
|
|
|
var reclean = new RegExp("[^1234567890+-/*%() ]", "g"); // sanitize
|
|
|
|
value = value.replace(reclean,'');
|
|
|
|
var reperc = new RegExp("[%]", "g"); // percentage
|
|
|
|
value = value.replace(reperc,'/100 ');
|
|
|
|
newValue = value;
|
2010-07-07 11:30:04 +07:00
|
|
|
var result = "" + eval(value);
|
|
|
|
if (separator != '.')
|
|
|
|
{
|
|
|
|
result = result.replace(/\./, separator);
|
|
|
|
}
|
|
|
|
calcText.value = result;
|
|
|
|
|
2012-09-04 16:27:04 +07:00
|
|
|
id = "$".concat(displayTextId);
|
|
|
|
var displayText = jq(id)[0];
|
2010-07-07 11:30:04 +07:00
|
|
|
|
|
|
|
if (!displayText.readOnly && calcText.value != 'undefined')
|
|
|
|
{
|
|
|
|
displayText.value = calcText.value;
|
2012-09-04 16:27:04 +07:00
|
|
|
setTimeout("jq('$" + displayTextId + "')[0].focus()", 100);
|
2010-07-07 11:30:04 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (err)
|
|
|
|
{
|
2014-07-12 08:11:30 +07:00
|
|
|
calcText.value = newValue;
|
2010-07-07 11:30:04 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function append(calcTextId, val)
|
|
|
|
{
|
2012-09-04 16:27:04 +07:00
|
|
|
var id = "$".concat(calcTextId);
|
|
|
|
var calcText = jq(id)[0];
|
2010-07-07 11:30:04 +07:00
|
|
|
calcText.value += val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var calc = new Calc();
|