﻿function FormatCurrency(txtID, imgID, required)
{
	// local variables
	var decimalCount;
	var dollarCount;
	var dotCount;
	var formatted;
	var numberCount;
	var txt;
	var value;

	// initialization
	decimalCount = 0;
	dollarCount = 0;
	dotCount = 0;
	formatted = "";
	numberCount = 0;
	txt = document.getElementById(txtID);
	value = txt.value;

	// remove all characters
	for (var i = 0; i < value.length; ++i)
	{
		if (value.charAt(i) == '0' || value.charAt(i) == '1' || value.charAt(i) == '2' || value.charAt(i) == '3' || value.charAt(i) == '4' || value.charAt(i) == '5' || value.charAt(i) == '6' || value.charAt(i) == '7' || value.charAt(i) == '8' || value.charAt(i) == '9' || value.charAt(i) == '.' || (i == 0 && (value.charAt(i) == '-' || value.charAt(i) == '$')) || (i == 1 && value.charAt(0) == '-' && value.charAt(i) == '$'))
			formatted = formatted + value.charAt(i);

		if (value.charAt(i) == '$')
			++dollarCount;

		if (value.charAt(i) == '.')
			++dotCount;

		if (isInteger(value.charAt(i)))
		{
			if (dotCount == 1)
				++decimalCount;
			else
				++numberCount;
		}
	}

	if (dollarCount == 0)
		formatted = "$" + formatted;

	if (numberCount == 0)
		formatted = formatted + "0";

	if (dotCount == 0)
		formatted = formatted + ".";

	for (var i = 2; i > decimalCount; --i)
		formatted = formatted + "0";

	// set correct format
	if(value.length > 0)
		txt.value = formatted;

	// validate
	ValidateCurrency(txtID, imgID, required);
}

// Format phone number.
function FormatPhoneNumber(txtID, imgID, required)
{
	// local variables
	var formatted;
	var index;
	var txt;
	var value;

	// initialization
	formatted = "";
	txt = document.getElementById(txtID);
	value = txt.value;

	// remove all characters
	for (index = 0; index < value.length; ++index)
	{
		if (value.charAt(index) == '0' || value.charAt(index) == '1' || value.charAt(index) == '2' || value.charAt(index) == '3' || value.charAt(index) == '4' || value.charAt(index) == '5' || value.charAt(index) == '6' || value.charAt(index) == '7' || value.charAt(index) == '8' || value.charAt(index) == '9')
		{
			formatted = formatted + value.charAt(index);
		}
	}

	// verify that it is valid
	if (formatted.length < 10)
	{
		return;
	}

	// add characters
	formatted = "(" + formatted;
	formatted = formatted.substring(0, 4) + ") " + formatted.substring(4);
	formatted = formatted.substring(0, 9) + "-" + formatted.substring(9);

	// add extension
	if (formatted.length > 14)
	{
		formatted = formatted.substring(0, 14) + " ext. " + formatted.substring(14);
	}

	// set new format
	txt.value = formatted;

	// validate
	ValidatePhoneNumber(txtID, imgID, required);
}

function KeyPressCurrency(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	return charCode < 32 || charCode > 47 && charCode < 58 || charCode == 36 || charCode == 46;
}

function KeyPressDate(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	return charCode < 32 || charCode > 47 && charCode < 58 || charCode == 47;
}

function KeyPressDouble(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	return charCode < 32 || charCode > 47 && charCode < 58 || charCode == 46;
}

function KeyPressInt32(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	return charCode < 32 || charCode > 47 && charCode < 58;
}

function KeyPressPhoneNumber(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	return charCode < 33 || charCode > 47 && charCode < 58 || charCode == 40 || charCode == 41 || charCode == 45;
}

function ValidateCurrency(txtID, imgID, required)
{
	// local variables
	var decimalCount;
	var dollarCount;
	var dotCount;
	var formatted;
	var img;
	var index;
	var numberCount;
	var txt;
	var value;

	// initialization
	decimalCount = 0;
	dollarCount = 0;
	dotCount = 0;
	formatted = "";
	img = document.getElementById(imgID);
	numberCount = 0;
	txt = document.getElementById(txtID);
	value = txt.value;

	// remove all characters
	for (index = 0; index < value.length; ++index)
	{
		if (value.charAt(index) == '0' || value.charAt(index) == '1' || value.charAt(index) == '2' || value.charAt(index) == '3' || value.charAt(index) == '4' || value.charAt(index) == '5' || value.charAt(index) == '6' || value.charAt(index) == '7' || value.charAt(index) == '8' || value.charAt(index) == '9' || value.charAt(index) == '.' || (index == 0 && (value.charAt(index) == '-' || value.charAt(index) == '$')) || (index == 1 && value.charAt(0) == '-' && value.charAt(index) == '$'))
		{
			formatted = formatted + value.charAt(index);
		}

		if (value.charAt(index) == '$')
		{
			++dollarCount;
		}

		if (value.charAt(index) == '.')
		{
			++dotCount;
		}

		if (isInteger(value.charAt(index)))
		{
			++numberCount;

			if (dotCount == 1)
			{
				++decimalCount;
			}
		}
	}

	if (formatted.length == value.length && dollarCount <= 1 && dotCount <= 1 && numberCount > 0 && decimalCount <= 2)
	{
		img.style.visibility = "hidden";
		txt.style.backgroundColor = "White";
	}
	else
	{
		img.style.visibility = "visible";
		txt.style.backgroundColor = "#FFDDDD";
	}
}

function ValidateDate(txtID, imgID, required)
{
	// local variables
	var img;
	var txt;

	// initialization
	img = document.getElementById(imgID);
	txt = document.getElementById(txtID);

	if (isDate(document.getElementById(txtID).value) || (value == "" && !required))
	{
		img.style.visibility = "hidden";
		txt.style.backgroundColor = "White";
	}
	else
	{
		img.style.visibility = "visible";
		txt.style.backgroundColor = "#FFDDDD";
	}
}

function ValidateDDL(ddlID, imgID, required)
{
	// local variables
	var ddl;
	var img;

	// initialization
	img = document.getElementById(imgID);
	ddl = document.getElementById(ddlID);

	if (ddl.selectedIndex > 0 || !required)
	{
		img.style.visibility = "hidden";
		ddl.style.backgroundColor = "White";
	}
	else
	{
		img.style.visibility = "visible";
		ddl.style.backgroundColor = "#FFDDDD";
	}
}

function ValidateDouble(txtID, imgID, length, required)
{
	// local variables
	var img;
	var number;
	var txt;
	var value;

	// initialization
	img = document.getElementById(imgID);
	txt = document.getElementById(txtID);
	value = txt.value;
	number = parseFloat(value);

	// validate by verifying
	//  1) length is greater than 0
	//  2) is a number (disallows 1a)
	if ((value.length == 0 && !required) || (value.length > 0 && !isNaN(value) && (length == -1 || value.length == length)))
	{
		img.style.visibility = "hidden";
		txt.style.backgroundColor = "White";
	}
	else
	{
		img.style.visibility = "visible";
		txt.style.backgroundColor = "#FFDDDD";
	}
}

function ValidateInt32(txtID, imgID, length, required)
{
	// local variables
	var img;
	var number;
	var txt;
	var value;

	// initialization
	img = document.getElementById(imgID);
	txt = document.getElementById(txtID);
	value = txt.value;
	number = parseInt(value, 10);

	// validate by verifying
	//  1) length is greater than 0
	//  2) is a number (disallows 1a)
	//  3) is an integer (disallows 1.5)
	//  4) is greater than 0
	if ((value.length == 0 && !required) || (value.length > 0 && !isNaN(value) && isInteger(value) && number > 0 && (length == -1 || value.length == length)))
	{
		img.style.visibility = "hidden";
		txt.style.backgroundColor = "White";
	}
	else
	{
		img.style.visibility = "visible";
		txt.style.backgroundColor = "#FFDDDD";
	}
}

function ValidateListBox(lstID, imgID, required)
{
	// local variables
	var lst;
	var img;

	// initialization
	img = document.getElementById(imgID);
	lst = document.getElementById(lstID);

	if (lst.length > 0 || !required)
	{
		img.style.visibility = "hidden";
		lst.style.backgroundColor = "White";
	}
	else
	{
		img.style.visibility = "visible";
		lst.style.backgroundColor = "#FFDDDD";
	}
}

function ValidatePhoneNumber(txtID, imgID, required)
{
	// local variables
	var formatted;
	var img;
	var index;
	var txt;
	var value;

	// initialization
	formatted = "";
	img = document.getElementById(imgID);
	txt = document.getElementById(txtID);
	value = txt.value;

	// remove all characters
	for (index = 0; index < value.length; ++index)
	{
		if (value.charAt(index) == '0' || value.charAt(index) == '1' || value.charAt(index) == '2' || value.charAt(index) == '3' || value.charAt(index) == '4' || value.charAt(index) == '5' || value.charAt(index) == '6' || value.charAt(index) == '7' || value.charAt(index) == '8' || value.charAt(index) == '9')
		{
			formatted = formatted + value.charAt(index);
		}
	}

	if (formatted.length >= 10 || (formatted.length == 0 && !required))
	{
		img.style.visibility = "hidden";
		txt.style.backgroundColor = "White";
	}
	else
	{
		img.style.visibility = "visible";
		txt.style.backgroundColor = "#FFDDDD";
	}
}

function ValidatePostalCode(txtID, imgID, required)
{
	// local variables
	var img;
	var number;
	var txt;
	var value;

	// initialization
	img = document.getElementById(imgID);
	txt = document.getElementById(txtID);
	value = txt.value;

	// validate by verifying
	//  1) length greater than or equal to 5
	if (value.length >= 5)
	{
		img.style.visibility = "hidden";
		txt.style.backgroundColor = "White";
	}
	else
	{
		img.style.visibility = "visible";
		txt.style.backgroundColor = "#FFDDDD";
	}
}

function ValidateString(txtID, imgID, required)
{
	// local variables
	var img;
	var txt;

	// initialization
	img = document.getElementById(imgID);
	txt = document.getElementById(txtID);

	if (txt.value.length > 0 || !required)
	{
		img.style.visibility = "hidden";
		txt.style.backgroundColor = "White";
	}
	else
	{
		img.style.visibility = "visible";
		txt.style.backgroundColor = "#FFDDDD";
	}
}







function isDate(dtStr)
{
	var daysInMonth = DaysArray(12);
	var pos1 = dtStr.indexOf(dtCh);
	var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
	var strMonth = dtStr.substring(0, pos1);
	var strDay = dtStr.substring(pos1 + 1, pos2);
	var strYear = dtStr.substring(pos2 + 1);
	strYr = strYear;
	if (strDay.charAt(0) == "0" && strDay.length > 1)
	{
		strDay = strDay.substring(1);
	}
	if (strMonth.charAt(0) == "0" && strMonth.length > 1)
	{
		strMonth = strMonth.substring(1);
	}
	for (var i = 1; i <= 3; i++)
	{
		if (strYr.charAt(0) == "0" && strYr.length > 1)
		{
			strYr = strYr.substring(1);
		}
	}
	month = parseInt(strMonth);
	day = parseInt(strDay);
	year = parseInt(strYr);
	if (pos1 == -1 || pos2 == -1)
	{
		//		alert("The date format should be : mm/dd/yyyy")
		return false;
	}
	if (strMonth.length < 1 || month < 1 || month > 12)
	{
		//		alert("Please enter a valid month")
		return false;
	}
	if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
	{
		//		alert("Please enter a valid day")
		return false;
	}
	if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear)
	{
		//		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false;
	}
	if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false)
	{
		//		alert("Please enter a valid date")
		return false;
	}

	return true;
}




// Declaring valid date character, minimum year and maximum year
var dtCh = "/";
var minYear = 1753;
var maxYear = 9999;

function isInteger(s)
{
	var i;
	for (i = 0; i < s.length; i++)
	{
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9")))
		{
			return false;
		}
	}

	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag)
{
	var i;
	var returnString = "";

	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1)
		{
			returnString += c;
		}
	}

	return returnString;
}

function daysInFebruary(year)
{
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n)
{
	for (var i = 1; i <= n; i++)
	{
		this[i] = 31;
		if (i == 4 || i == 6 || i == 9 || i == 11)
		{
			this[i] = 30;
		}
		if (i == 2)
		{
			this[i] = 29;
		}
	}

	return this;
}