Source Code for SaeSolved::sitewidgets.com JavaScript Class Library for Calculators
');
}
function ShowRows(){
var i = 0;
for (var RowNr = 0; RowNr < this.rowindex; RowNr++){
document.write(this.RowFormat[RowNr]);
for (i; i < this.index; i++){
if (this.Element[i].RowLocn != RowNr) break;
this.Element[i].ShowElement(this.HelpUsed,
this.TotalColumns);
if (i == 0 && this.HelpUsed){
document.write(
'
\n',
' \n',
' Click on the "?" next to the input box ' +
'for an item to get help on that item.\n',
' \n',
'
\n');
}
}
document.write(' ' + this.RowFormatEnd[RowNr] + '\n');
}
}
function GetValues(obj){
for (var i = 0; i < this.index; i++){
if (this.Element[i].in_out.indexOf('input') != -1 || this.Element[i].in_out == ''){
if (this.Element[i].type == 'text'){
var numFieldVal = obj[this.Element[i].value_name].value;
//get rid of leading and trailing spaces.
while (numFieldVal.charAt(0) == ' '){
numFieldVal = numFieldVal.substring(1)
}
while (numFieldVal.charAt(numFieldVal.length - 1) == ' '){
numFieldVal = numFieldVal.substring(0, numFieldVal.length - 1)
}
if (numFieldVal == null || numFieldVal.length == 0){
//assume empty or blank fields are zero
this.Element[i].value = 0.0;
}else{
//strip out any commas
this.Element[i].value =
parseFloat(numFieldVal.toString().split(',').join(''));
}
}else if (this.Element[i].type == 'checkbox'){
var checkboxVal = '';
if(obj[this.Element[i].value_name].checked == true){
checkboxVal = obj[this.Element[i].value_name].value;
}
if (this.Element[i].decimals == null) {
this.Element[i].value = checkboxVal;
}else{
if (checkboxVal == '') {
checkboxVal = '0';
}
if (this.Element[i].decimals == 0) {
this.Element[i].value = parseInt(checkboxVal);
}else{
this.Element[i].value = parseFloat(checkboxVal);
}
}
}else if (this.Element[i].type == 'radio_parent'){
var radioVal = '';
var radioNam = new Array();
radioNam = obj[this.Element[i].value_name];
for (var j = 0; j < radioNam.length; j++){
if(radioNam[j].checked == true){
radioVal = radioNam[j].value;
}
}
if (this.Element[i].decimals == null) {
this.Element[i].value = radioVal;
}else{
if (radioVal == '') {
radioVal = '0';
}
if (this.Element[i].decimals == 0) {
this.Element[i].value = parseInt(radioVal);
}else{
this.Element[i].value = parseFloat(radioVal);
}
}
}else if (this.Element[i].type == 'select'){
this.Element[i].value = this.Element[i].options[obj[this.Element[i].value_name].selectedIndex][0];
}
}
}
}
function PutValues(obj){
for (var i = 0; i < this.index; i++){
if (this.Element[i].in_out == 'output' || this.Element[i].in_out == ''){
if (this.Element[i].type == 'text'){
obj[this.Element[i].value_name].value = this.Element[i].ShowDecimals();
}else if (this.Element[i].type == 'checkbox'){
if(this.Element[i].value == obj[this.Element[i].value_name].value){
obj[this.Element[i].value_name].checked = true;
}else{
obj[this.Element[i].value_name].checked = false;
}
}else if (this.Element[i].type == 'radio_parent'){
var radioNam = new Array();
radioNam = obj[this.Element[i].value_name];
for (var j = 0; j < radioNam.length; j++){
if(this.Element[i].value == radioNam[j].value){
radioNam[j].checked = true;
}else{
radioNam[j].checked = false;
}
}
}else if (this.Element[i].type == 'select'){
for (var j = 0; j < this.options.length; j++){
if (this.Element[i].value == this.options[j][0]){
obj[this.Element[i].value_name].selectedIndex = j;
}
}
}
}
}
}
function CalculatorElement(type, name, label, in_out){
this.type = type;
if (this.type == 'radio') {
this.family_name = name;
} else {
this.name = name;
}
this.value_name = name + '_value';
this.help_name = name + '_help';
if (label == null){
this.label = '';
}else{
this.label = label;
}
this.alert_label = this.label.replace(/<[^>]+>/g, '');
if (in_out == null){
this.in_out = '';
}else{
this.in_out = in_out;
}
this.value = '';
this.decimals = null;
this.size = 5;
this.checked = false;
this.options = new Array();
this.help = '';
this.max_value = null;
this.min_value = null;
this.isHeader = false;
this.ColSpan = null;
this.RowLocn = 0;
this.BGColor = null;
this.FontFamily = null;
this.FontSize = null;
this.FontColor = null;
this.LabelAlign = null;
this.LabelFormatBegin = null;
this.LabelFormatMid = null;
this.LabelFormatEnd = null;
this.ValuePrefix = '';
this.ValuePostfix = '';
this.ValueFormatBegin = null;
this.ValueFormatMid = null;
this.ValueFormatEnd = null;
this.HelpFormatBegin = null;
this.HelpFormatEnd = null;
this.NoteFormatBegin = null;
this.NoteFormatMid = null;
this.NoteFormatEnd = null;
this.ButtonFormatBegin = null;
this.ButtonFormatMid = null;
this.ButtonFormatEnd = null;
this.SetFormats = SetFormats;
this.ShowElement = ShowElement;
this.ShowDecimals = ShowDecimals;
this.Validate = Validate;
this.HelpScreen = HelpScreen;
}
function SetFormats(){
// This uses defaults for each of the variables unless they have
// been given explicit values after an object is instantiated
if(this.LabelFormatBegin == null){
if(this.isHeader){
this.LabelFormatBegin = '
';
}
function ShowElement(withHelp, TotalColumns){
this.SetFormats();
var ValueColumns = 1;
if ( this.type == 'text'
|| this.type == 'checkbox'
|| this.type == 'radio'
|| this.type == 'select'
|| this.type == 'string'){
if (this.label == ''){
ValueColumns++;
}else{
document.write(
' ' + this.LabelFormatBegin);
if (this.ColSpan != null){
document.write(' colspan="', this.ColSpan, '"');
}
document.write(this.LabelFormatMid + '\n',
' ', this.label, '\n',
' ' + this.LabelFormatEnd + '\n');
}
document.write(
' ' + this.ValueFormatBegin)
if (this.ColSpan != null){
document.write(' colspan="', this.ColSpan, '"');
}else if (ValueColumns > 1){
document.write(' colspan="', TotalColumns, '"');
}
document.write(this.ValueFormatMid + '\n');
if (this.type == 'checkbox'){
document.write(' ');
for (var i = 0; i < this.options.length; i++){
document.write('');
}
document.write('', this.ValueFormatEnd, this.HelpFormatBegin);
if (withHelp){
if (this.help != null && this.help.length > 0){
document.write('\n');
}else{
document.write(' \n');
}
}
document.write(
' ' + this.HelpFormatEnd + '\n');
}else if (this.type == 'label_only' || (this.type == 'radio_parent' && this.label != '')){
document.write(
' ' + this.LabelFormatBegin);
if (this.ColSpan != null){
document.write(' colspan="', this.ColSpan, '"');
}
document.write(this.LabelFormatMid + '\n',
' ' + this.label + '\n',
' ' + this.LabelFormatEnd + '\n');
}else if (this.type == 'note'){
document.write(
' ' + this.NoteFormatBegin);
if (this.ColSpan != null){
document.write(' colspan="', this.ColSpan, '"');
}else if (TotalColumns > 0){
document.write(' colspan="', TotalColumns, '"');
}
document.write(this.NoteFormatMid + '\n',
' ' + this.label + '\n',
' ' + this.NoteFormatEnd + '\n');
}else{
document.write(
' ' + this.ButtonFormatBegin);
if (this.ColSpan != null){
document.write(' colspan="', this.ColSpan, '"');
}else if (TotalColumns > 0){
document.write(' colspan="', TotalColumns, '"');
}
document.write(this.ButtonFormatMid + '\n',
' \n',
' ' + this.ButtonFormatEnd + '\n');
}
}
function HelpScreen(){
alert(this.help);
return true;
}
function ShowDecimals(){
if (this.decimals == null
|| parseFloat(this.value).toString().length
< this.value.toString().length)
return this.value;
var factr = 1;
for (var i = 0; i < this.decimals; i++){
factr *= 10;
}
var outputStr = Math.round(factr * this.value).toString();
while (outputStr.length - this.decimals < 1){
outputStr = '0' + outputStr;
}
if (this.decimals == 0)
return outputStr;
var pos = outputStr.length - this.decimals;
return outputStr.substring(0, pos) + '.' +
outputStr.substring(pos);
}
function Validate(obj){
if (this.in_out == 'output'){
alert('"' + this.alert_label + '" is an output value.');
obj.value = '';
return true;
}
var val = obj.value;
//strip out any commas
var val1 = val.toString().split(',').join('');
var val2 = val1;
//remove decimal point(s) -- check if there were too many
val2 = val1.toString().split('.').join('');
//get rid of leading signs and zeros
var val3 = val2;
if (val3.charAt(0) == '+' || val3.charAt(0) == '-'){
val3 = val3.substring(1)
}
while (val3.charAt(0) == '0'){
val3 = val3.substring(1)
}
if (val1.length - val2.length > 1) {
alert('The value of "' + this.alert_label
+ '" appears to contain more than one decimal point.');
} else if (val3.length > 0 && (isNaN(parseInt(val3))
|| parseInt(val3).toString().length < val3.length)) {
//check for valid numerical value
alert('You have entered an invalid value (' + val + ') for "'
+ this.alert_label + '."');
} else if (this.max_value != null && val > this.max_value){
alert('The value of "' + this.alert_label
+ '" must not be greater than ' + this.max_value + '.');
} else if (this.min_value != null && val < this.min_value){
alert('The value of "' + this.alert_label
+ '" must not be less than ' + this.min_value + '.');
} else {
return true;
}
obj.focus();
obj.select();
return false;
}
function getArgs(){
/* Based on Example 13-5 in "JavaScript: The Definitive Guide," 3rd ed.,
by David Flanagan, O'Reilly (1998) p. 245. */
var args = new Object();
var query = window.location.search.substring(1);
var pairs = new Array();
if ((pairs = query.split("&")) == null) pairs = localSplit(query, "&");
for (var i = 0; i < pairs.length; i++){
var pos = pairs[i].indexOf('=');
if (pos == -1) continue;
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
args[argname] = unescape(value);
}
return args;
}
// The following is a demo implementation of the above classes
// which can be uncommented and placed between script tags in an
// HTML page after including these classes.
/*
var TextBoxSize = 7;
// New Calculator object
var Demo = new Calculator('DemoCalculator',
'Demo Calculator', true);
// First element used in calculator
var A = new CalculatorElement('text', 'A', 'A');
// Some properties of the first element
A.size = TextBoxSize;
A.help = 'Value for A.';
A.max_value = 110;
A.min_value = -1234;
// Second element used in calculator
var B = new CalculatorElement('text', 'B', 'B');
// Some properties of the second element
B.size = TextBoxSize;
B.help = 'Value for B.';
B.min_value = 0;
// Third element used in calculator
var C = new CalculatorElement('text', 'C',
'C = A + B');
// Some properties of the third element
C.decimals = 2;
C.size = TextBoxSize;
C.help = 'The demo calculation is simply for C ' +
'= A + B.';
// The command element
var CommandElement = new CalculatorElement('Demo.Calculate()',
'commandElement', 'Calculate');
// Add the elements to the calculator
Demo.AddElement(A);
Demo.AddElement(B);
Demo.AddElement(C);
Demo.AddElement(CommandElement);
// Define the operation(s) that will be done using the elements
function OperateOnValues(){
this.C.value = this.A.value + this.B.value;
}
// Add this operation to the calculator
Demo.OperateOnValues = OperateOnValues;
// -->
// Display the calculator
Demo.ShowCalculator();
*/
SaeSolved::SiteWidgets
Web Site Tools from SaeSolved::
64 Conway Cove Drive, Chesterfield, MO 63017-2070
telephone: 636-751-8765
Email