function GetFullYear(intYear)
{
	if (intYear <= 20) return (2000 + parseFloat(intYear));
	if (intYear > 20 && intYear <= 99 ) return (1900 + parseFloat(intYear));

	return intYear;
}

function FormatDateInput(objDateInput)
{

	if (!isDate(objDateInput.value)) return;

	var objDate  = new Date(objDateInput.value)
	var lstDate  = objDateInput.value.split("/");
	var intMonth = lstDate[0];
	var intDay   = lstDate[1];
	var intYear  = lstDate[2];

	objDateInput.value = intMonth + "/" + intDay + "/" + GetFullYear(intYear);
}


// Returns false if strFromDate < strToDate
function ValidateDateRange(strFromDate, strToDate)
{	
	if ((strFromDate != "") || (strToDate != ""))
	{
		var objFromDate = new Date(strFromDate);			   
		var objToDate   = new Date(strToDate);

		return (objFromDate.getTime() <= objToDate.getTime())
	}
	else
	{
		return true;
	}
}


// Returns false if strFromDate < strToDate and both exists
function ValidateDateRangeOptional(strFromDate, strToDate)
{	
	if ((strFromDate != "") && (strToDate != ""))
	{
		var objFromDate = new Date(strFromDate);
		var objToDate   = new Date(strToDate);

		return (objFromDate.getTime() <= objToDate.getTime())
	}
	else
	{
		return true;
	}
}


// Validates date range inputs and alerts user if there is something wrong
function ValidateReportDateRangeInputs(objFromDateInput, objToDateInput)
{
	
	if ((objFromDateInput.value != "") && (!isDate(objFromDateInput.value)))
	{
		alert("Start Date is not a valid date");
		return false;
	}

	if ((objToDateInput.value != "") && (!isDate(objToDateInput.value)))
	{
		alert("End Date is not a valid date");
		return false;
	}

	if (!ValidateDateRange(objFromDateInput.value, objToDateInput.value))
	{
		alert("The Date range is invalid.");
		return false;
	}

	return true;
}


function ValidateDateInputs(objFromDateInput, objToDateInput)
{
	
	if ((objFromDateInput.value != "") && (!isDate(objFromDateInput.value)))
	{
		alert("Start Date is not a valid date");
		return false;
	}

	if ((objToDateInput.value != "") && (!isDate(objToDateInput.value)))
	{
		alert("End Date is not a valid date");
		return false;
	}

	if (!ValidateDateRangeOptional(objFromDateInput.value, objToDateInput.value))
	{
		alert("The Date range is invalid.");
		return false;
	}

	return true;
}


// Verifies that strDate is mm/dd/yyyy mm/dd/yy format
function isDate(strDate)
{
	var objDate = new Date(strDate)
	var lstDate = strDate.split("/");

	// check that there is a month day and a year
	if (lstDate.length != 3) return false;

	var intMonth = lstDate[0];
	var intDay   = lstDate[1];
	var intYear  = lstDate[2];


	// check that there is a month day and a year are numeric
	if (isNaN(intMonth) || isNaN(intDay) || isNaN(intYear)) return false;

	// check that month is 1-12
	if (intMonth < 1 || intMonth > 12) return false;

	// check if the day is valid for the month.
        // if the date is 2/30/2004, getDate will return 1, meaning 3/1/2004
	if (objDate.getDate() != intDay) return false;

	// check year
	intYear = parseFloat(intYear);

	if (intYear <= 20) 
	{
		intYear = 2000 + intYear;
	}
	else
	{
		if (intYear > 20 && intYear <= 99 ) intYear  = 1900 + intYear;
	}


        if (intYear < 1900 || intYear > 2020) return false; 

	return true;
}


// Prevents users from entering non numeric values in a from input
// Example usage:
// <INPUT id="NUMERIC_INPUT_FIELD" onkeypress="return SuppressNonNumericKeys('NUMERIC_INPUT_FIELD');"/>
function SuppressNonNumericKeys(strInputId)
{
	var objElement    = document.getElementById(strInputId);
	var strPressedKey = String.fromCharCode(window.event.keyCode);
	var strValidChars = "0123456789"; 

	// Add a decimal point to list of valid characters if it has not yet been typed.	
	if (objElement.value.indexOf('.') == -1)
	{
		strValidChars += ".";
	}

	return strValidChars.indexOf(strPressedKey) > -1;
}



// Takes the name of an <INPUT> and formats it to 0,000.00
// Example usage:
// <INPUT id="CURRENCY_INPUT_FIELD" onchange="FormatInputToCurrency('CURRENCY_INPUT_FIELD');"/>  
function FormatInputToCurrency(strInputId)
{
	
	var objElement = document.getElementById(strInputId);
	
	if (objElement.value.length == 0)
		objElement.value = "0.00";
	
	if (IsNumeric(FormatCurrencyToNumber(objElement.value)))
	{
		objElement.value = FormatToCurrency(parseFloat(FormatCurrencyToNumber(objElement.value)));
	}
}


// Formats a numeric value to 0,000.00
function FormatToCurrency(numCurrencyValue)
{
	var formattedNumber;
	
	formattedNumber = Math.round(numCurrencyValue * 100)/100;

	// Whole number
	if (formattedNumber == Math.round(numCurrencyValue))
	{
		formattedNumber += '.00';
	}
	else if ((formattedNumber * 10) == Math.round(formattedNumber * 10))
	{
		formattedNumber += '0';
	}
	
	return FormatNumberToComma(formattedNumber);
	//return formattedNumber;
}

function FormatNumberToComma(amount)
{
	var strAmount = "" + amount;
	var delimiter = ","; // replace comma if desired
	var a = strAmount.split('.');
	var d = a[1];
	var i = parseInt(a[0]);
	var minus = '';

	if(!IsNumeric(i)) 
		return ''; 

	if(i < 0) 
		minus = '-'; 

	i = Math.abs(i);
	var n = new String(i);
	var a = [];

	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}

	if(n.length > 0) 
		a.unshift(n); 

	n = a.join(delimiter);
	if(d.length < 1) 
	{
		amount = n; 
	}	
	else 
	{	
		amount = n + '.' + d; 
	}	
	amount = minus + amount;

	return amount;
}

function FormatCurrencyToNumber(amount)
{
	// removes dollar sign and commas
	return amount.replace(/[\$,]/g, "");
}


// objForm  - the form to submit
// objEvent - Event object used to determine the key that was pressed
// objBooleanFunction - optional function to be called before the form is submitted. If it returns false, the form will not submit.
function SubmitEnter(objForm, objEvent, objBooleanFunction) 
{ 
	var keycode;

        // determine the key that was pressed
	if (window.event)       
           keycode = window.event.keyCode; 
	else if (objEvent) 		
           keycode = objEvent.which; 
	else 			
           return true; 


	// If a carriage return is pressed the the active element is not a button, submit the form
	if (keycode == 13 && document.activeElement.tagName.toUpperCase() != "BUTTON")	
        {
	   // cancel the keycode so we don't get any annoying beeps
           if (window.event)       
              window.event.keyCode = 0; 
           else if (objEvent) 		
              objEvent.which = 0; 

	   // Run preprocessing function if it is specified
           if (objBooleanFunction)
	   {
	      if (objBooleanFunction() == false) return false;
           }


           // submit the form
           objForm.submit(); 
           return false; 
        } 

	else
        {			
           return true; 
        }
}



// Trucates to contents of a text box to the specified maximum length and alerts the user.
function ValidateAndTruncateInputText(objInputText, intMaxLength, strMsg)
{
   if (objInputText.value.length > intMaxLength)   
   {
     if (strMsg != "")
     {
        alert(strMsg);
     }
     else
     {
       alert("Text has been truncated to the maximum length of " + intMaxLength + " characters.");
     }

     objInputText.value = objInputText.value.slice(0, intMaxLength);
     return false;
   }

   return true;
}


