﻿// Clear the credit card number.
function ClearCreditCardNumber(txtID, lblID)
{
	// local variables
	var lbl;
	var txt;

	// initialization
	lbl = document.getElementById(lblID);
	txt = document.getElementById(txtID);

	if(txt.value.startsWith('*'))
	{
		txt.value = '';
		lbl.style.visibility = 'visible';
	}
}

// Format phone number.
function FormatPhoneNumber(txtID)
{
	// 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;
}


function Validate(labels, hiddenID)
{
	// local variables
	var found;
	var hidden;
	var index;
	
	// initialization
	found = false;
	hidden = document.getElementById(hiddenID);
	for(index = 0; index < labels.length && !found; ++index)
	{
		if(document.getElementById(labels[index]).style.visibility != 'hidden')
		{
			found = true;
			ShowDarkInvalid();
		}
	}
	
	if(!found)
	{
		hidden.value = 'save';
		document.forms[0].submit();
	}
	else
	{
		hidden.value = '0';
	}
}


function ValidateCheckBox(cbID, lblID)
{
	// local variables
	var cb;
	var lbl;
	var value;

	// initialization
	cb = document.getElementById(cbID);
	lbl = document.getElementById(lblID);
	value = cb.checked;

	// validate
	if(value)
	{
		lbl.style.visibility = 'hidden';
	}
	else
	{
		lbl.style.visibility = 'visible';
	}
}


function ValidateCreditCardNumber(txtID, lblID)
{
	// local variables
	var lbl;
	var number;
	var txt;
	var value;

	// initialization
	lbl = document.getElementById(lblID);
	txt = document.getElementById(txtID);
	value = txt.value;
	number = parseInt(value, 10);

	// validate by verifying
	//  1) length is 13, 15, or 16
	//  2) is a number (disallows 1a)
	//  3) is an integer (disallows 1.5)
	//  4) is greater than 0
	if((value.length == 13 || value.length == 15 || value.length == 16) && !isNaN(value) && isInteger(value) && number > 0)
	{
		lbl.style.visibility = 'hidden';
	}
	else
	{
		lbl.style.visibility = 'visible';
	}
}


function ValidateCreditCardSecurityCode(txtID, lblID)
{
	// local variables
	var lbl;
	var number;
	var txt;
	var value;

	// initialization
	lbl = document.getElementById(lblID);
	txt = document.getElementById(txtID);
	value = txt.value;
	number = parseInt(value, 10);

	// validate by verifying
	//  1) length is 3 or 4
	//  2) is a number (disallows 1a)
	//  3) is an integer (disallows 1.5)
	//  4) is greater than 0
	if((value.length == 3 || value.length == 4) && !isNaN(value) && isInteger(value) && number > 0)
	{
		lbl.style.visibility = 'hidden';
	}
	else
	{
		lbl.style.visibility = 'visible';
	}
}


function ValidateCurrency(txtID, lblID)
{
	// local variables
	var decimalCount;
	var dollarCount;
	var dotCount;
	var formatted;
	var index;
	var lbl;
	var numberCount;
	var txt;
	var value;

	// initialization
	decimalCount = 0;
	dollarCount = 0;
	dotCount = 0;
	formatted = "";
	lbl = document.getElementById(lblID);
	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)
	{
		lbl.style.visibility = 'hidden';
	}
	else
	{
		lbl.style.visibility = 'visible';
	}
}


function ValidateDate(txtID, lblID)
{
	if(isDate(document.getElementById(txtID).value) == false)
	{
		document.getElementById(lblID).style.visibility = 'visible';
	}
	else
	{
		document.getElementById(lblID).style.visibility = 'hidden';
	}
}


function ValidateDDL(ddlID, lblID)
{
	// local variables
	var lbl;
	var ddl;

	// initialization
	lbl = document.getElementById(lblID);
	ddl = document.getElementById(ddlID);
	
	if(ddl.selectedIndex <= 0)
	{
		lbl.style.visibility = 'visible';
	}
	else
	{
		lbl.style.visibility = 'hidden';
	}
}


function ValidateEmailAddress(txtID, lblID)
{
	// local variables
	var lbl;
	var txt;
	var value;

	// initialization
	lbl = document.getElementById(lblID);
	txt = document.getElementById(txtID);
	value = txt.value;

	if(value.indexOf('@') <= 0)
	{
		lbl.style.visibility = 'visible';
	}
	else
	{
		value = value.substring(value.indexOf('@'));
		if(value.indexOf('.') <= 0)
		{
			lbl.style.visibility = 'visible';
		}
		else
		{
			value = value.substring(value.indexOf('.'));
			if(value.length <= 2)
			{
				lbl.style.visibility = 'visible';
			}
			else
			{
				lbl.style.visibility = 'hidden';
			}
		}
	}
}


function ValidateInteger(txtID, lblID)
{
	// local variables
	var lbl;
	var number;
	var txt;
	var value;

	// initialization
	lbl = document.getElementById(lblID);
	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 && !isNaN(value) && isInteger(value) && number > 0)
	{
		lbl.style.visibility = 'hidden';
	}
	else
	{
		lbl.style.visibility = 'visible';
	}
}


function ValidatePhoneNumber(txtID, lblID)
{
	// local variables
	var formatted;
	var index;
	var lbl;
	var txt;
	var value;

	// initialization
	formatted = "";
	lbl = document.getElementById(lblID);
	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)
	{
		lbl.style.visibility = 'hidden';
	}
	else
	{
		lbl.style.visibility = 'visible';
	}
}


function ValidatePhoneNumberOptional(txtID, lblID)
{
	// local variables
	var lbl;
	var txt;

	// initialization
	lbl = document.getElementById(lblID);
	txt = document.getElementById(txtID);
	
	// remove all characters	
	if(txt.value.length == 0)
	{
		lbl.style.visibility = 'hidden';
	}
	else
	{
		ValidatePhoneNumber(txtID, lblID);
	}
}

function ValidateIntegerKeyPress(evt)
{
	// local variables
	var charCode;

	// initialization
	charCode = (evt.which) ? evt.which : event.keyCode;
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function ValidateInteger(txtID, lblID)
{
	// local variables
	var lbl;
	var number;
	var txt;
	var value;

	// initialization
	lbl = document.getElementById(lblID);
	txt = document.getElementById(txtID);
	value = txt.value;
	number = parseInt(value, 10);

	// validate by verifying
	//  1) is a number (disallows 1a)
	//  2) is an integer (disallows 1.5)
	//  3) is greater than 0
	if(value.length > 0 && !isNaN(value) && isInteger(value) && number > 0)
	{
		lbl.style.visibility = 'hidden';
	}
	else
	{
		lbl.style.visibility = 'visible';
	}
}

function ValidatePostalCode(txtID, lblID)
{
	// local variables
	var lbl;
	var number;
	var txt;
	var value;

	// initialization
	lbl = document.getElementById(lblID);
	txt = document.getElementById(txtID);
	value = txt.value;

	// validate by verifying
	//  1) length greater than or equal to 5
	if(value.length >= 5)
	{
		lbl.style.visibility = 'hidden';
	}
	else
	{
		lbl.style.visibility = 'visible';
	}
}


function ValidateString(txtID, lblID)
{
	// local variables
	var lbl;
	var txt;
	
	// initialization
	lbl = document.getElementById(lblID);
	txt = document.getElementById(txtID);
	
	if(txt.value.length > 0)
	{
		lbl.style.visibility = 'hidden';
	}
	else
	{
		lbl.style.visibility = 'visible';
	}
}



// 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;
}

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;
}