/** The javasccript that warns the user when they move off a page that has had edits
 * To mark a page as edited call windowChanged()
 */
window.changed = false;
function checkForChanges( /*event*/ evt ) {
	if( window.ignoreChange ) {
		window.ignoreChange = false;
	} else {
		// IE supports evt.returnValue to cancel in the onBeforeUnload handler
		// firefox supports evt.preventDefault() in the onUnload handler
		//    (but onBeforeUnload appears to work also so we are using that).
		if ( window.changed ) {
			if ( evt.preventDefault ) {
				evt.preventDefault();
			} else {
				evt.returnValue = "This window has changed";
			}
		}
	}
}
function recordChange() {
	window.changed = true;
}
function clearChangeNotification() {
	window.changed = false;
	return true;
}


/** Uses the format function (below) to format currency correctly. */
function /*String*/ formatCurrency( /*Number*/ n ) {
	var formatted = format( n, 2 );
	if( formatted>=0 ) {
		return "$" + formatted;
	} else {
		return "($" + format( -n, 2 ) + ")";
	}
}

function /*Number*/ parseCurrency( /* String */ s ) {
	var toParse = "";
	var c = null;
	var i;
	if( s && s.length > 0 ) {
		for( i=0; i<s.length; i++ ) {
			c = s.charAt( i );
			if( c == "(" ) {
				toParse += "-";
			} else if( c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9'||
					   c=='.'||c=='-' ) {
				toParse += c;
			}
		}
	// treat empty strings as $0.00
	} else {
		toParse = "0";
	}

	return parseFloat( toParse );
}

/**
 * This function formats a number with trailing zeros.
 * For example: format( 22, 2 ) -> 22.00
 *              format( 123.45, 6 ) -> 123.450000
 */
function /*String*/ format( /*Number*/ n, scale ) {
	var p = Math.pow(10,scale);
	var s = (Math.round( parseFloat(n)*p ) / p).toString();
	var d = s.indexOf(".");
	if( d == -1 ) {
		s = s + ".";
		d = 1;
	} else {
		d = s.length - d;
	}
	var i;
	for( i=0; i<=(scale-d); i++ ) {
		s = s + "0";
	}

	return s;
}

/**
 * A shorthand for
 *   return formatCurrency( parseCurrency(x) + parseCurrency(y) );
 */
function addCurrency( x, y ) {
	return formatCurrency( parseCurrency(x) + parseCurrency(y) );
}

/**
 * A shorthand for
 *   return formatCurrency( parseCurrency(x) - parseCurrency(y) );
 */
function subtractCurrency( x, y ) {
	return formatCurrency( parseCurrency(x) - parseCurrency(y) );
}

/** Select all checkboxes with the given name */
function allCheckboxes( name ) {
	var checkboxes = document.getElementsByName( name );
	for( i = 0; i<checkboxes.length; i++ ) {
		checkboxes[ i ].checked = true;
	}
}

/** Un-select all checkboxes with the given name */
function noCheckboxes( name ) {
	var checkboxes = document.getElementsByName( name );
	for( i = 0; i<checkboxes.length; i++ ) {
		checkboxes[ i ].checked = false;
	}
}

/** Select all options in a multiselect specified by the id */
function allOptions( id ) {
	var options = document.getElementById( id ).options;
	for( i = 0; i<options.length; i++ ) {
		options[ i ].selected = true;
	}
}

/** Un-select all options in a multiselect specified by the id */
function noOptions( id ) {
	var options = document.getElementById( id ).options;
	for( i = 0; i<options.length; i++ ) {
		options[ i ].selected = false;
	}
}
