honorFieldRequirements = true;

//
//Completely validate a money-type control
//
function validateMoney( Control, ControlName, MinValue, MaxValue, IsRequired )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( MinValue == null )
		MinValue			= -2000000000; //largest range allowable by data type
	if ( MaxValue == null )
		MaxValue			= 2000000000; //largest range allowable by data type
		
	var variable = commaStrip( Control.value );
	
	if ( variable != "" )
	{
		if ( isNaN( variable ) )
		{
			alert(  ControlName + " is not numeric." );
			Control.select();
			return false;
		}
		
		if ( variable > MaxValue )
		{
			alert(  ControlName + " must be less than or equal to " + MaxValue );
			Control.select();
			return false;
		}
		
		if ( variable < MinValue )
		{
			alert(  ControlName + " must be greater than or equal to " + MinValue );
			Control.select();
			return false;
		}
	}
	else if ( variable == "" && IsRequired && honorFieldRequirements )
	{
		alert( ControlName + " is required." );
		Control.select();
		return false;
	}
	
	Control.value = ( variable == undefined ? "" : variable );
	
	return true;
}

//
//Completely validate a integer-type control
//
function validateInteger( Control, ControlName, MinValue, MaxValue, IsRequired )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( MinValue == null )
		MinValue			= -2000000000; //largest range allowable by data type
	if ( MaxValue == null )
		MaxValue			= 2000000000; //largest range allowable by data type
		
	var variable = commaStrip( Control.value );
	
	if ( variable != "" )
	{			
		if ( isNaN( variable ) )
		{
			alert(  ControlName + " is not numeric." );
			Control.select();
			return false;
		}
		
		if ( !isInteger( variable ) )
		{
			alert(  ControlName + " cannot include decimals." );
			Control.select();
			return false;
		}
		
		if ( variable > MaxValue )
		{
			alert(  ControlName + " must be less than or equal to " + MaxValue );
			Control.select();
			return false;
		}
		
		if ( variable < MinValue )
		{
			alert(  ControlName + " must be greater than or equal to " + MinValue );
			Control.select();
			return false;
		}
	}
	else if ( variable == "" && IsRequired && honorFieldRequirements )
	{
		alert( ControlName + " is required." );
		Control.select();
		return false;
	}
	
	Control.value = ( variable == undefined ? "" : variable );
	
	return true;
}

//
//Completely validate a float-type control
//
function validateFloat( Control, ControlName, MinValue, MaxValue, MaxPrecision, IsRequired )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( MinValue == null )
		MinValue			= -2000000000; //largest range allowable by data type
	if ( MaxValue == null )
		MaxValue			= 2000000000; //largest range allowable by data type
		
	var variable = commaStrip( Control.value );
	
	if ( variable != "" )
	{
		if ( isNaN( variable ) )
		{
			alert(  ControlName + " is not numeric." );
			Control.select();
			return false;
		}
		
		if ( Number( variable ) > MaxValue )
		{
			alert(  ControlName + " must be less than or equal to " + MaxValue );
			Control.select();
			return false;
		}
		
		if ( Number( variable ) < MinValue )
		{
			alert(  ControlName + " must be greater than or equal to " + MinValue );
			Control.select();
			return false;
		}
		
		var precision = variable.indexOf( "." );
		if ( precision >= 0 )
		{
		    precision = variable.length - ( precision + 1 );
		    
		    if ( precision > MaxPrecision )
		    {
		        alert(  ControlName + " must have " + MaxPrecision + " or less decimal places." );
			    Control.select();
		        return false;
		    }
		}
	}
	else if ( variable == "" && IsRequired && honorFieldRequirements )
	{
		alert( ControlName + " is required." );
		Control.select();
		return false;
	}
	
	Control.value = ( variable == undefined ? "" : variable );
	
	return true;
}

//
//Completely validate a percent-type control
//
function validatePercent( Control, ControlName, MinValue, MaxValue, MaxPrecision, IsRequired )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( MinValue == null )
		MinValue			= -2000000000; //largest range allowable by data type
	if ( MaxValue == null )
		MaxValue			= 2000000000; //largest range allowable by data type
		
	var variable = commaStrip( Control.value );
	if ( variable != "" )
	{	
		if ( isNaN( variable ) )
		{
			alert(  ControlName + " is not numeric." );
			Control.select();
			return false;
		}
		
		if ( Number( variable ) > MaxValue )
		{
			alert(  ControlName + " must be less than or equal to " + MaxValue + "%" );
			Control.select();
			return false;
		}
		
		if ( Number( variable ) < MinValue )
		{
			alert(  ControlName + " must be greater than or equal to " + MinValue + "%"  );
			Control.select();
			return false;
		}
		
		var precision = variable.indexOf( "." );
		if ( precision >= 0 )
		{
		    precision = variable.length - ( precision + 1 );
		    
		    if ( precision > MaxPrecision )
		    {
		        alert(  ControlName + " must have " + MaxPrecision + " or less decimal places." );
			    Control.select();
		        return false;
		    }
		}
	}
	else if ( variable == "" && IsRequired && honorFieldRequirements )
	{
		alert( ControlName + " is required." );
		Control.select();
		return false;
	}
	
	Control.value = ( variable == undefined ? "" : variable );
	
	return true;
}

//
//Completely validate a string-type control
//
function validateString( Control, ControlName, MaxLength, IsRequired, IsAlphanumericOnly, Message )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( MaxLength == null )
	{
		MaxLength			= 16000000000; //largest range allowable by text
	}
	
	if ( Control.value.length > MaxLength )
	{
		alert( ControlName + " is " + Control.value.length + 
			" characters long, but may only be " + MaxLength + "characters long." );
		return false;
	}
	
	if ( !isStringBlank( Control.value ) )
	{
		if ( IsAlphanumericOnly )
		{
			if ( !isAlphaNumericString( Control.value ) )
			{
				alert(  ControlName + " must only contain letters and numbers." );
				Control.select();
				return false;
			}
		}
	}
	else if ( IsRequired && honorFieldRequirements )
	{
		if ( Message == "" )
			alert( ControlName + " is required." );
		else
			alert( Message );
		Control.select();
		return false;
	}
	
	if ( Control.value.indexOf( "\u201C" ) >= 0 ) //unicode “
	    Control.value = Control.value.replace( /\u201C/g, "\"" );
	if ( Control.value.indexOf( "\u201D" ) >= 0 ) //unicode ”
	    Control.value = Control.value.replace( /\u201D/g, "\"" );
	
	return true;
}

//
//Validate the length of a string-type control
//
function validateStringLength( Control, ControlName, MaxLength, IsRequired, Message )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( MaxLength == null )
	{
		MaxLength			= 16000000000; //largest range allowable by text
	}
	
	if ( Control.value.length > MaxLength )
	{
		alert( ControlName + " is " + Control.value.length + 
			" characters long, but may only be " + MaxLength + "characters long." );
		return false;
	}
	
	if ( !isStringBlank( Control.value ) )
	{
	}
	else if ( IsRequired && honorFieldRequirements )
	{
		if ( Message == "" )
			alert( ControlName + " is required." );
		else
			alert( Message );
		Control.select();
		return false;
	}
	
	if ( Control.value.indexOf( "\u201C" ) >= 0 ) //unicode “
	    Control.value = Control.value.replace( /\u201C/g, "\"" );
	if ( Control.value.indexOf( "\u201D" ) >= 0 ) //unicode ”
	    Control.value = Control.value.replace( /\u201D/g, "\"" );
	
	return true;
}

//
//Completely validate a dropdown control
//
function validateDropDown( Control, Message, IsRequired )
{
	if ( Control == null )
	{
		//alert( "Warning: Dropdown with message '" + Message + "' is missing." );
		return true;
	}
	
	var valIsBlank = ( Control.value.length == 0 );
	
	if ( valIsBlank )
	{
		if ( Control.options.length > Control.selectedIndex && Control.selectedIndex >= 0 )
		{
			valIsBlank = ( Control.options[ Control.selectedIndex ].value != "" );
		}
	}
	
	if ( ( Control.options.length == 0 || Control.value == "#" || valIsBlank ) && 
	    IsRequired && honorFieldRequirements )
	{
		alert( Message );
		return false;
	}
	
	return true;
}

//
//Completely validate a listbox control
//
function validateListbox( ControlName, Message, IsRequired )
{
    var Control = document.getElementById( ControlName );
	if ( Control == null )
		return true;
	
	if ( ( Control.value.length == 0 || Control.value == "#" ) && 
	    IsRequired && honorFieldRequirements )
	{
		alert( Message );
		return false;
	}
	
	return true;
}

//
//Completely validate a list of checkboxes
//
function validateChecklist( ControlBase, ControlName, NumberRequired )
{
	var inputs			= document.getElementsByTagName( "input" );
	var assigned		= false;
	var numberSelected	= 0;
	for ( var i = 0; i < inputs.length; i++ )
	{
		if ( inputs[i].id.indexOf( ControlBase ) >= 0 ) 
		{
			if ( inputs[i].checked || ( inputs[i].value == 1 ) || ( inputs[i].getAttribute('chk') == 1 ) )
			{
				numberSelected++;
				
				if ( numberSelected >= NumberRequired )
				{
					assigned = true;
					break;
				}
			}
		}
	}
	
	if ( !assigned && honorFieldRequirements )
	{
		alert( "Please select at least " + NumberRequired + " " + ControlName );
		return false;
	}
	
	return true;
}

function validateChecklistChanges( ControlBase, ChangesRequired, Message )
{
	var inputs			= document.getElementsByTagName( "input" );
	var numberChanged	= 0;
	
	for ( var i = 0; i < inputs.length; i++ )
		if ( inputs[i].id.indexOf( "c+" + ControlBase ) == 0 && !inputs[i].disabled )
			numberChanged++;
	
	if ( numberChanged < ChangesRequired && honorFieldRequirements )
	{
		alert( Message );
		return false;
	}
	
	return true;
}

function validateChecklistRemaining( ControlBase, ControlName, NumberRequired )
{
	var inputs			= document.getElementsByTagName( "input" );
	var assigned		= false;
	var numberRemaining	= 0;
	for ( var i = 0; i < inputs.length; i++ )
	{
		if ( inputs[i].id.indexOf( ControlBase ) >= 0 ) 
		{
			if ( !inputs[i].checked && ( inputs[i].value != 1 ) && ( inputs[i].getAttribute('chk') != 1 ) )
			{
				numberRemaining++;
				
				if ( numberRemaining >= NumberRequired )
				{
					assigned = true;
					break;
				}
			}
		}
	}
	
	if ( !assigned && honorFieldRequirements )
	{
		alert( "Please leave at least " + NumberRequired + " " + ControlName );
		return false;
	}
	
	return true;
}

//
//Completely validate a list of radio buttons
//
function validateRadioButtons( ControlBase, ControlName )
{
	var inputs			= document.getElementsByTagName( "input" );
	var assigned		= false;
	for ( var i = 0; i < inputs.length; i++ )
	{
		if ( inputs[i].id.indexOf( ControlBase ) >= 0 ) 
		{
			if ( inputs[i].checked )
			{
				assigned = true;
				break;
			}
		}
	}
	
	if ( !assigned && honorFieldRequirements )
	{
		alert( "Please select a " + ControlName );
		return false;
	}
	
	return true;
}

//
//Completely validate an email address control
//
function validateEmail( Control, ControlName, MaxLength, IsRequired, Message )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( MaxLength == null )
	{
		MaxLength			= 16000000000; //largest range allowable by text
	}
	
	if ( Control.value.length > MaxLength )
	{
		alert( ControlName + " is " + Control.value.length + 
			" characters long, but may only be " + MaxLength + "characters long." );
		return false;
	}
	
	if ( Control.value != "" )
	{
		if ( !isEmailAddress( Control.value  ) )
		{
			alert(  ControlName + " is not a valid email address." );
			Control.select();
			return false;
		}
	}
	else if ( IsRequired && Control.value == "" && honorFieldRequirements )
	{
		if ( Message == "" )
			alert( ControlName + " is required." );
		else
			alert( Message );
		Control.select();
		return false;
	}
	
	return true;
}

//
//Completely validate a phone number control
//
function validatePhone( ControlID, ControlName, IsRequired, IncludesExtension )
{
    var mainNumber  = document.getElementById( ControlID );
	var extension   = ( IncludesExtension ? document.getElementById( ControlID + "+Ext" ) : null );
	
	if ( mainNumber == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	var numVal = mainNumber.value.replace( /-/g, '' );
	var extVal = ( IncludesExtension ? extension.value : '' );
	
	numVal = numVal.replace( /\(/g, '' ).replace( /\)/g, '' ).replace( / /g, '' ).replace( /\./g, '' );

	if ( numVal.length > 0 || ( IncludesExtension && extVal.length > 0 ) )
	{
		if ( numVal.length != 10 || isNaN( numVal ) )
		{
			alert( ControlName + " is not valid.\n\nValid formats include:\n(123) 456-7890\n(123)456-7890\n123-456-7890\n123.456.7890\n1234567890" );
			mainNumber.select();
			return false;
		}
		
		if ( IncludesExtension && isNaN( extVal ) )
		{
			alert( ControlName + " Extension is not valid.\n\nValid formats include only numbers." );
			extension.select();
			return false;
		}
	}
	else if ( IsRequired && honorFieldRequirements )
	{
		alert( ControlName + " is required." );
		mainNumber.select();
		return false;
	}
	
	return true;
}

//
//Completely validate a social security number control
//
function validateSSN( Control, ControlName, IsRequired )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	var ssnVal = Control.value.replace( /-/g, '' );
	
	if ( ssnVal != "" )
	{
		if ( ssnVal.length != 9 || isNaN( ssnVal ) )
	    {
		    alert( ControlName + " is not a valid Social Security Number.\n\nValid formats:\n123-45-6789\n123456789" );
			Control.select();
		    return false;
	    }
	}
	else if ( IsRequired && honorFieldRequirements )
	{
		alert( ControlName + " is required." );
		Control.select();
		return false;
	}
	
	return true;
}

//
//Completely validate a zipcode-type control
//
function validateZipCode( Control, ControlName, IsRequired )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	var zipVal = Control.value.replace( /-/g, '' );
	
	if ( zipVal != "" )
	{
	    if ( !isInteger( zipVal ) )
		{
			alert(  ControlName + " is not valid.\n\nZipcodes must be numeric!" );
			Control.select();
			return false;
		}
		
		if ( zipVal.length != 5 && zipVal.length != 9 )
	    {
		    alert( ControlName + " is not a valid zip code.\n\nValid formats:\n12345\n12345-6789" );
			Control.select();
		    return false;
	    }
	}
	else if ( IsRequired && honorFieldRequirements )
	{
		alert( ControlName + " is required." );
		Control.select();
		return false;
	}
	
	return true;
}

//
//Completely validate a date control
//
var dateFormatMessage = "\n\nValid Date Format: MM/DD/YYYY";
function validateDate( ControlID, ControlName, IsRequired )
{
    var item    = document.getElementById( ControlID );
    
    if ( item == null )
		return true;
		
	if ( item.value == "" )
	{
	    if ( IsRequired && honorFieldRequirements )
	    {
		    alert( ControlName + " is required." );
		    item.select();
		    return false;
	    }
	    else
	        return true;
	}
		
    item.value  = item.value.toString().replace( /\\/g, '/' );
    var parts   = item.value.toString().split('/');
    
    if ( parts.length != 3 )
    {
        alert( "Invalid " + ControlName + " specified." + dateFormatMessage );
        return false;
    }
    
    if ( isNaN( parts[0] ) || isNaN( parts[1] ) || isNaN( parts[2] ) )
    {
        alert( "Invalid " + ControlName + " specified." + dateFormatMessage );
        return false;
    }
    
    var iMon = Number( parts[0] );
    var iDay = Number( parts[1] );
    var iYea = Number( parts[2] );
    
    if ( !validateMonth( iMon ) )
	{
		alert( "Invalid " + ControlName + " month specified." + dateFormatMessage );
		item.select();
		return false;
	}
	if ( !validateDay( iDay ) )
	{
		alert( "Invalid " + ControlName + " day specified." + dateFormatMessage );
		item.select();
		return false;
	}
	
	var altMinYear = item.getAttribute( 'altMinYear' );
    var altMaxYear = item.getAttribute( 'altMaxYear' );
    
    if ( altMinYear == null )
        altMinYear = LOWER_YEAR_CUTOFF;
    else
        altMinYear = Number( altMinYear );
    if ( altMaxYear == null )
        altMaxYear = UPPER_YEAR_CUTOFF;
    else
        altMaxYear = Number( altMaxYear );
	    
	if ( !validateYearForCutoff( iYea, altMinYear, altMaxYear ) )
	{
	    alert( "Invalid " + ControlName + " year specified." + dateFormatMessage );
	    item.select();
	    return false;
	}
	
	var dateTest = new Date( iYea, iMon - 1, iDay );
	
	if ( Number(dateTest.getDate()) != Number(iDay) )
	{
		alert( "Invalid " + ControlName + " day specified. There are not that many days in that month." );
		item.select();
		return false;
	}
	
	return true;
}

//
//Completely validate a census tract control
//
function validateCensus( ControlID, ControlName, IsRequired )
{
	var part1	= document.getElementById( ControlID + "1" );
	var part2	= document.getElementById( ControlID + "2" );
	
	if ( part1 == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( part1.value != "" || part2.value != "" )
	{
		if ( part1.value != "" && isNaN( part1.value ) )
		{
			alert( "Invalid " + ControlName );
			part1.select();
			return false;
		}
		if ( part2.value != "" && isNaN( part2.value ) )
		{
			alert( "Invalid " + ControlName );
			part2.select();
			return false;
		}
	}
	else if ( IsRequired && honorFieldRequirements )
	{
		alert( ControlName + " is required." );
		part1.select();
		return false;
	}
	
	return true;
}

//
//Validate a URL control that has already been validated as a string
//
function validateURL( Control, ControlName, IsSubdomain )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( !isValidURL( Control.value  ) )
	{
		alert(  ControlName + " is not a valid URL." );
		Control.select();
		return false;
	}
	else if ( IsSubdomain )
	{
		if ( Control.value.indexOf( "." ) != -1 )
		{
			alert(  ControlName + " may not contain periods." );
			Control.select();
			return false;
		}
	}

	return true;
}

function validateTime( Control, ControlName, IsRequired )
{
	if ( Control == null )
	{
		//alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	var Time = Control.value;

	if ( Time == "" )
	{
		if ( IsRequired && honorFieldRequirements )
		{
			alert( ControlName + " is required." );
			Control.select();
			return false;
		}
		else
			return true;
	}
	
	Control.value = Time = Time.replace( 'p', 'P' ).replace( 'a', 'A' ).replace( 'm', 'M' );

    //simply having a single number is not valid
	if ( !isNaN( Time ) )
	{
		alert(  ControlName + " is not a valid time." );
		Control.select();
		return false;
	}

	var parts = Time.split( ':' );
	
	if ( parts.length > 3 )
	{
		alert(  ControlName + " is not a valid time." );
		Control.select();
		return false;
	}

	var isMilitaryTime = false;
	
	//hours
	var part = TrimTimePartNumber( parts[0] );
	if ( isNaN( part ) )
	{
		alert(  ControlName + " is not a valid time." );
		Control.select();
		return false;
	}

	var partInt = Number( part );
	if ( partInt < 0 || partInt > 23 )
	{
		alert(  ControlName + " is not a valid time." );
		Control.select();
		return false;
	}

	isMilitaryTime = ( partInt < 1 || partInt > 12 );

	if ( parts.length == 1 )
		return ValidateAMPM( parts[0], Control, ControlName, isMilitaryTime );

	//minutes
	part = TrimTimePartNumber( parts[1] );
	if ( isNaN( part ) )
	{
		alert(  ControlName + " is not a valid time." );
		Control.select();
		return false;
	}

	partInt = Number( part );
	if ( partInt < 0 || partInt > 59 )
	{
		alert(  ControlName + " is not a valid time." );
		Control.select();
		return false;
	}

	if ( parts.length == 2 )
		return ValidateAMPM( parts[1], Control, ControlName, isMilitaryTime );

	//seconds
	part = TrimTimePartNumber( parts[2] );
	if ( isNaN( part ) )
	{
		alert(  ControlName + " is not a valid time." );
		Control.select();
		return false;
	}

	partInt = Number( part );
	if ( partInt < 0 || partInt > 59 )
	{
		alert(  ControlName + " is not a valid time." );
		Control.select();
		return false;
	}

	if ( parts.length == 3 )
		return ValidateAMPM( parts[2], Control, ControlName, isMilitaryTime );

	// if nothing has been returned by now, the entry is not valid.
	alert(  ControlName + " is not a valid time." );
	Control.select();
	return false;
}

function TrimTimePartNumber( TimePart )
{
	return TimePart.replace( " ", "" ).replace( "AM", "" ).replace( "PM", "" );
}

function ValidateAMPM( TimePart, Control, ControlName, IsMilitaryTime )
{
	var index = TimePart.indexOf( "AM" );
	
	if ( index < 0 )
		index = TimePart.indexOf( "PM" );

	if ( index < 0 )
		return true;
		
	if ( IsMilitaryTime )
	{
		alert(  ControlName + " is not a valid time." );
		Control.select();
		return false;
	}
	
	return true;
}

//Make the item field value match the source field value, if the item field value is empty
function syncFields( source, item, truncateEmail )
{
	if ( source != undefined )
		if ( item.value.length == 0 )
		{
			if ( truncateEmail && source.value.indexOf( '@' ) >= 0 )
				item.value = source.value.substring( 0, source.value.indexOf( '@' ) );
			else
				item.value = source.value;
		}
}

//Allows for certain key inputs in code-type text areas
function codeKD( evt )
{
    if (!evt) evt = window.event;
		tgt = getEventTarget( evt );
    
    if( evt.which || evt.keyCode)
    {
        if ( evt.which == 9 || evt.keyCode == 9 )  //allow normal entry of tabs
        {
            tgt.selection = document.selection.createRange();
            tgt.selection.text = String.fromCharCode(9);
            return false;
        }
    } 
    
    return true;
}

//Enforce a maxlength parameter on textareas
function checkAreaLength( Text, Length )
{
	if ( Text.value.length > Length )
		Text.value = Text.value.substring( 0, Length );
}

function commaStripControl( Control )
{
	Control.value = commaStrip( Control.value );
}

function isStringBlank( String )
{
	return ( String.replace( /\s/g, "").length == 0 );
}

function ClearAllControls()
{
	controlCollection = document.getElementsByTagName( "input" );
	
	if ( controlCollection != null )
		for ( var i = 0; i < controlCollection.length; i++ )
			switch ( controlCollection[i].type )
			{
				case "text" :
					controlCollection[i].value = "";
					break;
				case "checkbox" :
				case "radio" :
					controlCollection[i].checked = false;
					break;
			}
			
	controlCollection = document.getElementsByTagName( "select" );
	
	if ( controlCollection != null )
		for ( var i = 0; i < controlCollection.length; i++ )
			if ( controlCollection[i].size > 0 )
				controlCollection[i].selectedIndex = 0;

	controlCollection = document.getElementsByTagName( "textarea" );
	
	if ( controlCollection != null )
		for ( var i = 0; i < controlCollection.length; i++ )
			controlCollection[i].innerHTML = "";
}

//
//	Validates that the sum of a group of numeric controls does not exceed a value or another control
//
function validateLimit ( MaxTotal, Message, IsWarningOnly /*, ControlName1, ..., ControlNameN */ )
{
	var total = 0;
	
	if ( MaxTotal == undefined )
		return true;

    if ( document.getElementById( MaxTotal ) != undefined )
		MaxTotal = Number(commaStrip( document.getElementById( MaxTotal ).value ) );
    else
        MaxTotal = Number( MaxTotal );
	
	if ( isNaN( MaxTotal ) )
	    return true;
	
	var curArg;
	for ( var i = 3; i < arguments.length; i++)
	{
		curArg = arguments[i];
		
		if ( curArg != undefined )
		{
		    if ( document.getElementById( curArg ) != undefined )
			    total += Number( commaStrip( document.getElementById( curArg ).value ) );
	    }
	}

	if ( total > MaxTotal )
	{
		if ( IsWarningOnly )
		{
		    if ( !confirm( Message + "\nDo you wish to continue?" ) )
				return false;
	    }
	    else
		{
			alert( Message );
			return false;
		}
	}

	return true;
}


//
//Control showing/hiding
//---------------------------------------------------------------------------------------------
//
function switchDataPointRows( DisplayID1, DisplayID2, ToggleID, ValueForDisplay1 )
{
    if ( isToggleSetToValue( ToggleID, ValueForDisplay1 ) )
    {
        document.getElementById( DisplayID1 ).style.display = '';
        document.getElementById( DisplayID2 ).style.display = 'none';
    }
    else
    {
        document.getElementById( DisplayID1 ).style.display = 'none';
        document.getElementById( DisplayID2 ).style.display = '';
    }
}

function isToggleSetToValue( ToggleID, ToggleValue )
{
    if ( document.getElementById( ToggleID ) )
        return document.getElementById( ToggleID ).value.toString() == ToggleValue.toString();
    else
        return false;
}

//
//Control finding
//---------------------------------------------------------------------------------------------
//

function getDropdown( id )
{
	return document.getElementById(id);
}

function getSingleCheckBox( id )
{
	return document.getElementById(id);
}

function getDateValue( id )
{
	var date	= document.getElementById( "m+" + id );
	
	return date.value;
}