/*
JavaScript Library for bankrate.com(SM) Utility file
Copyright (C) 1999 Intelligent Life (SM) Corp.
Calculator created by SaeSolved::(TM), http://www.sitewidgets.com.
Edited 12/14/99

Intelligent Life (SM) Corp. may be contacted at 11811 U.S. Highway 1, North Palm Beach, FL 33408, USA; email: webmaster@bankrate.com.  SaeSolved::(TM) may be contacted at 16 Beverly Road East, Tequesta, FL 33469-3108, USA; email: webmaster@saesolved.com.
----------------
Updated 19991220 - 
Updated 19991217 - round(): added
Updated 19991216 - checkLimits(): added parseFloat() to test values; sets an invalid response to minimum value
				 - validateReqNum() added
Updated 19991214
Updated 19991208
*/
var license = '<DIV ALIGN="left"><font face="Arial, Helvetica, sans-serif" size="1">' + 
        'Calculator created by SaeSolved::&#153;' + 
        '<A HREF="http://www.sitewidgets.com" target="_top">sitewidgets.com</A>&#153; ' + 
        'for bankrate.com (SM).  Copyright (C) 1999 Intelligent Life (SM) Corp.  ' + 
        'This calculator is provided in the hope that it will be useful, ' + 
        'but WITHOUT ANY WARRANTY; without even the implied warranty of ' + 
        'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  ' + 
        'Intelligent Life (SM) Corp. may be contacted at ' + 
        '<A HREF="mailto:webmaster@bankrate.com">webmaster@bankrate.com</A>.  ' + 
        'SaeSolved::&#153; may be contacted at ' + 
        '<A HREF="mailto:webmaster@saesolved.com">webmaster@saesolved.com</A>.' + 
        '</font></DIV>';

function checkNumber(objValue) {
    var decPtAt = 0;
    for (var i = 0; i < objValue.length; i++){
        var localChar = objValue.charAt(i);
        if (localChar < "0" || localChar > "9"){
            if (localChar == "." && decPtAt == 0){
                decPtAt = i + 1;
            }else{
                return false;
            }
        }
    }
    return true;
}
function formatFloat (expr, decplaces){
// Based on Danny Goodman's JavaScript Bible (3rd ed) pg. 569
	// raise incoming value by power of 10 times the number of decimal places;
	// round to an integer; convert to string
	var str = "" + Math.round( eval(expr) * Math.pow(10, decplaces) );
	// pad small value strings with zeros to the left of rounded number
	while (str.length <= decplaces){
		str = "0" + str;
	}
	// establish location of decimal point
	var decpoint = str.length - decplaces;
	// assemble final result from: (a) the string up to the position of the decimal point;
	// (b) the decimal point; (c) the balance of the string. Return finished product.
	return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
}

function formatCurrency(expr){
// Based on Danny Goodman's JavaScript Bible (3rd ed) pg. 569
	return "$" + formatFloat(expr, 2);
}

function round(objValue, decPlaces){
	if (!(decPlaces)) decPlaces = 0;
	if (checkNumber(objValue)){
		var localStr = new String(objValue);
		var newStr = new String("");
		// find decimal point, if none, pos is after length
		decPos = localStr.indexOf(".");
		if (!(decPos)) decPos = localStr.length + 1;
		// decPlus - precision of the floating point number
		decPlus = decPos + decPlaces;
		// check if rounding must occur - if not, newStr = substring
		if (localStr.charAt(decPlus + 1) > 4){
			// rounding:  construct the "+ 1", adjusted for decimal place
			var plusOne = new String(".");
			for (var j = 0; j < decPlaces - 1; j++){
				plusOne += "0";
			}
			plusOne += "1"; // produces ".01" for 2 points of precision
			// add one to the digit of precision [an error occurs here]
			newStr = parseFloat(localStr.substring(0, decPlus + 1)) + parseFloat(plusOne);
		}
		else newStr = localStr.substring(0, decPlus + 1); // no rounding occurred
	}
	else newStr = -1; // checkNumber failed
	return newStr;
}

function validateReqNum(localItem, minValue, maxValue, msg) {
	if (localItem.value.length < 1){
//		alert("Please enter a value for the " + msg + " field.");
		localItem.value = parseFloat(minValue);
		return false;
	}else return validateNumber(localItem, minValue, maxValue, msg);
}
function validateNumber(localItem, minValue, maxValue, msg) {
    var decPtAt = 0;
    for (var i = 0; i < localItem.value.length; i++){
        var localChar  = localItem.value.charAt(i);
        if (localChar < "0" || localChar > "9"){
            if (localChar == "." && decPtAt == 0){
                decPtAt = i + 1;
            }else{
                if (localChar == " "){
                    localChar = "Spaces cannot";
                }else if (localChar == "."){
                    localChar = "Only one decimal point can";
                }else{
                    localChar = "The character \"" + localChar
                                + "\" cannot";
                }
                alert(localChar + " be used in the " + msg + " field." +
                    "  Please correct your entry to use only numerical" +
                    " values, a single decimal point, and no spaces or" +
                    " commas.");
                localItem.focus();
                localItem.select();
                return false;
            }
        }
    }
    return checkLimits(localItem, minValue, maxValue, msg);
}

function checkLimits(localItem, minValue, maxValue, msg) {
    if (parseFloat(localItem.value) < parseFloat(minValue) || parseFloat(localItem.value) > parseFloat(maxValue)){
        alert("Value for the " + msg + " field must be between "
            + minValue + " and " + maxValue + ".");
		localItem.value = parseFloat(minValue);
        localItem.focus();
        localItem.select();
        return false;
    }
    return true;
}

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;
}

function localSplit(thisStr, thisChar){
    var localStr;
    var pairs = new Array();
    var pos = 0;
    var i = 0;
    while ((pos = localStr.indexOf(thisChar)) > -1){
        pairs[i++] = localStr.substring(0, pos);
        localStr = localStr.substring(pos + 1);
    }
    return pairs;
}

function displayDec(val, decs){
    var factr = 1;
    for (var i = 0; i < decs; i++){
        factr *= 10;
    }
    var outputStr = Math.round(factr * val) + '';
    while (outputStr.length - decs < 1){
        outputStr = '0' + outputStr;
    }
    var pos = outputStr.length - decs;
    return outputStr.substring(0, pos) + '.' + outputStr.substring(pos);
}

function getFourDigitYear(date_obj){
    var thisyear = date_obj.getYear();
    // Following Y2k fix should work till 3799 :-), by which time getYear() should be fully replaced by getFullYear()...
    if (thisyear < 1900){
        thisyear += 1900;
    }
    return thisyear;
}

function setFourDigitYear(date_obj, thisyear){
    // Y2k fix
    if (navigator.appName == "Microsoft Internet Explorer" && thisyear < 2000){
        thisyear = thisyear - 1900;
    }
    date_obj.setYear(thisyear);
}