// ===================================================================
// Author: Matt Kruse <mkruse@netexpress.net>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. Instead,
// please just point to my URL to ensure the most up-to-date versions
// of the files. Thanks.
// ===================================================================

//-------------------------------------------------------------------
// add_quotes([input_object]*)
//   Pass this function any number of text input objects, and it will
//   convert all single quotes (') to two single quotes (''). Useful
//   when data will be put into database.
//-------------------------------------------------------------------
function add_quotes() {
	for (var x=0; x<arguments.length; x++) {
		var item = arguments[x];
		var oldvalue = item.value;
		var newvalue = "";
		var inquotes = false;
		for ( var i = 0; i < oldvalue.length; i++) {
			if (oldvalue.charAt(i) == "'") {
				if (!inquotes) { inquotes=true; newvalue+="''"; }
				}
			else {
				newvalue += oldvalue.charAt(i);
				inquotes = false;
				}
			}
		item.value = newvalue;
		}
	}

//-------------------------------------------------------------------
// remove_quotes([input_object]*)
//   Pass this function any number of text input objects, and it will
//   convert all multiple quotes (''''*) to a single quote ('). Useful
//   when data comes from a database.
//-------------------------------------------------------------------
function remove_quotes() {
	for (var x=0; x<arguments.length; x++) {
		var item = arguments[x];
		var oldvalue = item.value;
		var newvalue = "";
		var inquotes = false;
		for ( var i = 0; i < oldvalue.length; i++) {
			if (oldvalue.charAt(i) == "'") {
				if (!inquotes) { inquotes=true; newvalue+="'"; }
				}
			else {
				newvalue += oldvalue.charAt(i);
				inquotes = false;
				}
			}
		item.value = newvalue;
		}
	}

//-------------------------------------------------------------------
// MoneyFormat (value)
//   Pass a value to be converted to money format - 2 decimal places.
//   Returns the formatted value
//-------------------------------------------------------------------
function MoneyFormat(amount) {
	var val = parseFloat(amount);
	if (isNaN(val)) { return "0.00"; }
	if (val <= 0) { return "0.00"; }
	else { val = Math.round(val*100)/100; }
	val = (val == Math.floor(val)) ? val + '.00' : ((val*10 == Math.floor(val*10)) ? val + '0' : val);
	return val;
	}

//-------------------------------------------------------------------
// MoneyFormatWithCommas (value)
//   Pass a value to be converted to money format - 2 decimal places.
//   Also inserts commas into the appropriate places
//   Returns the formatted value
//-------------------------------------------------------------------
function MoneyFormatWithCommas(amount) {
	var delimeter = ",";
	var val = parseFloat(MoneyFormat(amount));
	if (isNaN(val)) { return "0.00"; }
	if (val <= 0) { return "0.00"; }
	val = val+"";
	var i = val.indexOf('.');
	while (i>3) {
		val = val.substring(0,i-3)+delimeter+val.substring(i-3);
		i = i-3;
		}
	return val;
	}

// -------------------------------------------------------------------
// ExtractFloat(value)
//   Strips everything from a string except digits and '.' and returns
//   the 'bare' value
// -------------------------------------------------------------------
function ExtractFloat(val) {
	var newval = "";
	val = val+"";
	for (var i=0; i<val.length; i++) {
		var c = val.charAt(i);
		if (isDigit(c) || c=='.') {
			newval += c;
			}
		}
	return newval;
	}

//-------------------------------------------------------------------
// getElementIndex(input_object)
//   Pass an input object, returns index in form.elements[] for the object
//   Returns -1 if error
//-------------------------------------------------------------------
function getElementIndex(obj) {
	var theform = obj.form;
	for (var i=0; i<theform.elements.length; i++) {
		if (obj.name == theform.elements[i].name) {
			return i;
			}
		}
	return -1;
	}

//-------------------------------------------------------------------
// breakOutOfFrame()
// make sure that the current frame is the top frame
//-------------------------------------------------------------------
function breakOutOfFrame()
{
  if (top.location != location) {
    top.location.href = document.location.href ;
  }
}

//-------------------------------------------------------------------
// confirmLogOff(message)
// confirm that the user wants to log off
//-------------------------------------------------------------------
function confirmLogOff(message)
{
	if (confirm(message))
		window.parent.location.href = '/login/logOff.jsp';
}

