/*
 * Format.js
 * Brian Rogers - National Western Life
 * 
 * checks that values entered in text boxes are of correct format (client side)
 * 
 * $History: NWL_TextBox.js $
 * 
 * *****************  Version 1  *****************
 * User: Brogers      Date: 8/29/06    Time: 1:31p
 * Created in $/Agency_1/JavaScript
 * 
 * *****************  Version 6  *****************
 * User: Brogers      Date: 11/29/05   Time: 3:26p
 * Updated in $/SecuritySolution/Security/JavaScript
 * 
 * *****************  Version 5  *****************
 * User: Brogers      Date: 10/18/05   Time: 2:40p
 * Updated in $/NWL_Shared/NWL_WebControlLibrary/JavaScript
 * 
 * *****************  Version 4  *****************
 * User: Brogers      Date: 10/18/05   Time: 10:43a
 * Updated in $/NWL_Shared/NWL_WebControlLibrary/JavaScript
 * 
 * *****************  Version 2  *****************
 * User: Brogers      Date: 6/02/05    Time: 11:14a
 * Updated in $/Reports/Reports
 * 
 * *****************  Version 5  *****************
 * User: Brogers      Date: 6/02/05    Time: 11:06a
 * Updated in $/Reports/Reports
 * 
 * *****************  Version 4  *****************
 * User: Brogers      Date: 6/01/05    Time: 1:02p
 * Updated in $/Reports/Reports
 * 
 * *****************  Version 3  *****************
 * User: Brogers      Date: 1/05/04    Time: 9:52a
 * Updated in $/Reports
 * Initial Release Version
 * $NoKeywords: $
 * */

function Trim(element) 
{
	var reTrim = new RegExp(' *([^ ].*[^ ]|[^ ]) *| *', '');
	var arr = reTrim.exec(element.value);
	return arr[1];
}

function String_OnBlur(element, allCaps)
{
	var value;
	if (element.tagName != 'TEXTAREA')
		value = Trim(element);
	else
		value = element.value;
	if (allCaps)
		value = value.toUpperCase();
	if (element.value != value) {
		element.value = value;
		element.fireEvent('onchange');
	}
}

function TIN_OnBlur(element)
{
	element.className = 'color';
	var reDate;
	value = Trim(element);
	if (value != '') {
		var reComma = /-/g; 
		value = value.replace(reComma, '');
		if (element.TypeOfTIN == 'EIN') 
		{
			reDate = new RegExp('([0-9]{2})([0-9]{7})');
			arr = reDate.exec(value);
			if (arr != null && arr[0] == value)
				value = arr[1] + '-' + arr[2] ;
			else 
			{
				// error: highlight box and show message.
//				element.focus();
//				element.select();
//				window.alert('Not a valid EIN');
				element.className = 'error';
				return;
			}	
		}	
		else 
		{
			reDate = new RegExp('([0-9]{3})([0-9]{2})([0-9]{4})');
			arr = reDate.exec(value);
			if (arr != null && arr[0] == value)
				value = arr[1] + '-' + arr[2] + '-' + arr[3];
			else 
			{
				// error: highlight box and show message.
//				element.focus();
//				element.select();
//				window.alert('Not a valid SSN');
				element.className = 'error';
				return;
			}
		}
	}

	if (element.value != value) {
		element.value = value;
		element.fireEvent('onchange');
	}
//	if (element.TypeOfTIN == 'EIN')

}

function Date_OnKeyDown(element)
{
	if (element.isDisabled || element.readOnly)
		return false;
	if (event.keyCode == 67) {
		currentDate = new Date();
		element.value = (currentDate.getMonth()+1) + "/" + currentDate.getDate() + "/" + currentDate.getYear();
		element.select();
		element.fireEvent('onchange');
		return false;
	}
	else
	if (event.keyCode == 38 || event.keyCode == 40) {
		var thisDate = new Date(element.value);
		if (isNaN(thisDate))
			return true;
		if (event.keyCode == 38)
			thisDate.setDate(thisDate.getDate() + 1)
		else 
			thisDate.setDate(thisDate.getDate() - 1)
		element.value = (thisDate.getMonth()+1) + "/" + thisDate.getDate() + "/" + thisDate.getYear();	
		element.select();
		element.fireEvent('onchange');
		return false;
	}
	else
		return true;
}

function Date_OnBlur(element, allowNulls)
{
	element.className = 'color';
	var newValue = Trim(element);
	var hasYear = true;
	var month, day, year;
	var foundMatch = false;
	var currentdate, currentYear, currentMonth, currentDay;
	if (element.value != newValue)
		element.value = newValue;
	if (element.value != '' || !allowNulls)
	{
		// format mm/dd/yyyy
		var reDate = new RegExp('([0-9]{1,2})(?:/|\-)([0-9]{1,2})(?:/|\-)([0-9]{4})');
		arr = reDate.exec(newValue);
		if (arr != null && arr[0] == newValue){
			month = new Number(arr[1]);
			day = new Number(arr[2]);
			year = new Number(arr[3]);
			foundMatch = true;
		}
		
		// format mm/dd/yy
		var reDate = new RegExp('([0-9]{1,2})(?:/|\-)([0-9]{1,2})(?:/|\-)([0-9]{1,2})');
		arr = reDate.exec(newValue);
		if (arr != null && arr[0] == newValue){
			month = new Number(arr[1]);
			day = new Number(arr[2]);
			year = new Number(arr[3]);
			if (year > 40) year+=1900 
			else year += 2000;
			foundMatch = true;
		}
		
		// format mmddyy
		var reDate = new RegExp('([0-9]{2})([0-9]{2})([0-9]{2})');
		arr = reDate.exec(newValue);
		if (arr != null && arr[0] == newValue){
			month = new Number(arr[1]);
			day = new Number(arr[2]);
			year = new Number(arr[3]);
			if (year > 40) year+=1900 
			else year += 2000;
			foundMatch = true;
		}
		
		// format mmddyyyyy
		var reDate = new RegExp('([0-9]{2})([0-9]{2})([0-9]{4})');
		arr = reDate.exec(newValue);
		if (arr != null && arr[0] == newValue){
			month = new Number(arr[1]);
			day = new Number(arr[2]);
			year = new Number(arr[3]);
			foundMatch = true;
		}
		
		// format mm/dd
		var reDate = new RegExp('([0-9]{1,2})(?:/|\-)([0-9]{1,2})');
		arr = reDate.exec(newValue);
		if (arr != null && arr[0] == newValue){
			month = new Number(arr[1]);
			day = new Number(arr[2]);
			hasYear = false;
			foundMatch = true;
		}
		
		// format mmdd
		var reDate = new RegExp('([0-9]{2})([0-9]{2})');
		arr = reDate.exec(newValue);
		if (arr != null && arr[0] == newValue){
			month = new Number(arr[1]);
			day = new Number(arr[2]);
			hasYear = false;
			foundMatch = true;
		}
		
		if (foundMatch)
				// sanity checks:
			if (month<1 || month>12 || day<1 || day>31 || year<1900 || year>2100)
				foundMatch = false;
		if (foundMatch) {
			if (!hasYear) {
				// compare to current date to find year
				currentDate = new Date();
				currentYear = currentDate.getFullYear();
				currentMonth = currentDate.getMonth()+1;
				currentDay = currentDate.getDate();
				if (month > currentMonth)
					year = currentYear - 1;
				if (month < currentMonth)
					year = currentYear;
				if (month == currentMonth)
					if (day > currentDay)
						year = currentYear-1;
					else	
						year = currentYear;
			}
			// make sure this date actually exists (if it doesn't, date created will not match this date)
			var dt = new Date(year, month-1, day);
			if (dt.getDate() != day || dt.getMonth() != (month-1) || dt.getFullYear() != year)
				foundMatch = false;
			else
				// set value to date in correct format
				newValue = month + '/' + day + '/' + year;
		}
		if (!foundMatch){
			// error: highlight box and show message.
//			element.focus();
//			element.select();
//			window.alert('Not a valid date');
			element.className = 'error';
		}
	}
	if (element.value != newValue) {
		element.value = newValue;
		element.fireEvent("onchange");
	}
}

function Integer_OnBlur(element, allowNulls, min, max)
{
	element.className = 'color';
	element.value = Trim(element);
	if (allowNulls && element.value == '')
		return;
	var reInteger = new RegExp('[0-9][0-9]*');
	arr = reInteger.exec(element.value);
	if (arr == null || arr[0] != element.value){
//		element.focus();
//		element.select();
//		window.alert('Not a valid integer');
		element.className = 'error';
	} else {
		var value = new Number(element.value);
		var vString = value.toString();
		if (vString != element.value)
			element.value = vString;
		if (min != null && value < min) {
//			element.focus();
//			element.select();
			element.className = 'error';
//			window.alert('minimum value is ' + min);
		}
		if (max != null && value > max) {
//			element.focus();
//			element.select();
//			window.alert('maximum value is ' + max);
			element.className = 'error';
		}
	}
}

function Text_OnFocus(element)
{
	element.select();
}

function Decimal_OnBlur(element, precision, allowNulls, min, max)
{
	element.className = 'color';
	var newText;
	newText = Trim(element);
	var returnVal = false;
	var errorMsg;
	
	if (newText != '' || !allowNulls) {
		var reComma = /,/g; 
		newText = newText.replace(reComma, '');
		if (isNaN(newText)) 
			errorMsg = 'Not a valid amount';
		else {
			var number = new Number(newText);
			newText = number.toFixed(precision);
			if (min != null && number < min) {
				element.focus();
				element.select();
				errorMsg = 'minimum value is ' + min;
			} else
			if (max != null && number > max) {
				element.focus();
				element.select();
				errorMsg = 'maximum value is ' + max;
			}
			else 
				returnVal = true;
		}
	} else 
		returnVal = true;
	
	var oldNumber = new Number(element.value);
	var newNumber = new Number(newText);	
	
	if (element.value != newText) 
		element.value = newText;
	if (oldNumber.toString() != newNumber.toString() && returnVal==true) 
		element.fireEvent("onchange");
	
	if (!returnVal)
	{
//		element.focus();
//		element.select();
//		window.alert(errorMsg);	
		element.className = 'error';	
	}
	return returnVal;
}

