/*****************************************************************************
File Name: Validate.js

Description: File to Hold DHTML functions for use with HTML Text inputs representing date
	and time and numeric values

Program Name: Booking Maintenance
Program Type: Web Application
File Version: 1.3
Author: Robert Plates
Origination Date: 08-13-02

Language: JavaScript
Compiler: Visual Studio 6.0 Enterprise


This source code is under U.S. and International Copyright and Trade Secret
Protection. Copyright (c) Aug 2002 AAA Auto Club South Inc.


MODIFICATION HISTORY
******************************************************************************
9-27-02 Rob Plates: Made modifications to the time entry no not force a leading zero in the hour
portion of the time processing.
11-15-02 Rob Plates added currency handling code
12-16-02 Rob Plates added function to handle numeric codes key entry
12-23-02 Rob Plates added Alpha UCase event handler function
02-05-03 Rob Plates allowed minus (char 45) in currency on keypress
02-14-03 Rob Plates the was a problem validating a date "05/08/03". In debugging this
	I found that the Day portion was the problem. It seems the parseInt() function's optional
	second parameter (radix) defaults to octal. Thus the result of parseInt("08") results in 0.
	to correct this you must use parseInt("08", 10) specifying the radix as decimal and the result
	is 8. In the date_validate() function I am adding this second parameter to all parseInt calls
03-20-03 Rob Plates added date and time assist functions

03-26-03 Rob Plates Added functionality to currency_sformat to deal with rounding up 9's  this
	and currency_format required a more mathematical solution than a Logical one (as prevously done)
	Ileft the old code in comments in case we have to re-examine this routine
04-14-03 Rob Plates
	Imporved Time and Date Assist functions to erase a "full" field.
	ITEM #1 time_onkeypress_assist
	ITEM #2 date_onkeypress_assist
04-17-03 Rob Plates
	Added Function: decimal_onkeypress()
04-22-03 Rob Plates
	Added Phone Number Formatting
04-23-03 Rob Plates 
	Added String version for Phone Strip Formatting
05-06-03 Rob PLates
	Added Phone String Formatting
05-25-03 Rob Plates
	Format Currency had some problems formatting negative currencies. Fixed the Problem
	added code to deal with negative numbers is currency_sformat and currency_format
05-27-03 Rob Plates
	Format Currency still had problems becuase we used JavaScript "MATH". If you add 1 + 0.14
	the result is 1.14000000000001. Thus after we do the rounding we still need to insure that only
	2 decimal places a the result. Thus this change was made.
10-23-03 Rob Plates
	Added alpha_numeric_onkeypress function
	Added alpha_only_ucase_onkeypress function
	Added email_onkeypress function
	Added email_validate function
	Added credit card validation functions.
11-19-03
	In testing my web page I found that we could have more than one period character (my email address for example)
	ITEM #1 email_validate: changed the else condition to allow more than one dot char in the string just not consequetively
02-16-04
	Added: alpha_ucase_numeric_onkeypress() for International Zip Codes.
******************************************************************************/



/*********************************************************************************************************
	Function: date_onkeypress
	Description: Basic function that eliminates keystrokes in appropriate for date entry.
	Assumes the  forward slash "/" (ascii 47) will be the delimiter for the date components.
	
	Parameters:
*********************************************************************************************************/
function date_onkeypress()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;
	
	//if(field.value.length==10)
	//	return window.event.returnValue = false;
	if(inputChar==8 || inputChar==9)
		return window.event.returnValue = true;
		
	if(inputChar >= 47 && inputChar <=57)
		return window.event.returnValue = true;
	else
		return window.event.returnValue = false;
}


/*********************************************************************************************************
	Function: date_keypress_assist
	
	Description: function that attempts to anticipate what a user will enter respecting a date value.
	
	Parameters:
*********************************************************************************************************/
function date_onkeypress_assist()
{
	var b_inMonth, b_inDay, b_inYr;

	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;
	var keyVal;
	var sTmp;
	
	//allow system to process backspaces
	if(inputChar==8 || inputChar==9)
		return window.event.returnValue = true;

	/*
	ITEM #2 04-15-03
	If the Length >= 10 then erase the field and start over
	*/		
	if(field.value.length >= 10)
		field.value=""
		// Old Code: return window.event.returnValue = false;
	
	//Convert other Characters that would possibly be used for date delimiters
	switch(inputChar)
		{
		case 45:
		case 92:
			inputChar=47;
			break;
		}
		
	if(inputChar >= 47 && inputChar <=57)
		{
		if(field.value.length==0)
			{
			keyval=String.fromCharCode(inputChar);
			if(keyval != "/")
				{
				field.value += keyval;
				if(parseInt(keyval) > 1)
					field.value += "/";
				}	
			}
		else
			{
			var aComponents=field.value.split("/");
			switch(aComponents.length)
				{
					case 1: //in the Month Portion
						//If we get here there is already 1 digit in the textbox
						keyval=String.fromCharCode(inputChar);
						if(parseInt(field.value)==0)
								field.value += keyval + "/";
						else
							if(parseInt(keyval) < 3)
								field.value += keyval + "/";
							else
								if(! isNaN(keyval))
									field.value +="/" + keyval;
								else
									field.value +="/";
						break;
					case 2: //in the Day Portion
						var mnth=aComponents[0];
						var dy=aComponents[1];
						keyval=String.fromCharCode(inputChar);
						if(keyval=="/")
							{
							if(dy.length > 0)
								field.value+=keyval;
							}
						else // a Number was keyed in
							{
							if(dy.length > 0) //this is the second digit
								{
								switch(parseInt(mnth*1))
									{
									case 9:
									case 4:
									case 6:
									case 11:
										if(dy+keyval <= 30)
											field.value+=keyval+"/";
										break;
									case 1:
									case 3:
									case 5:
									case 7:
									case 8:
									case 10:
									case 12:
										if(dy+keyval <= 31)
											field.value+=keyval+"/";
										break;
									case 2:
										if(dy+keyval <= 29)
											field.value+=keyval+"/";
										break;
									}
								}
							else //this is the first digit
								{
								if(parseInt(keyval*1) > 3)
									field.value+=keyval+"/";
								else
									{
									if(parseInt(mnth*1) != 2)
										field.value+=keyval;
									else
										if(parseInt(keyval*1) < 3) 
											field.value+=keyval;
									}
								}
							}
						break;
					case 3: // in the Year Portion
						keyval=String.fromCharCode(inputChar);
						if(keyval != "/")
							field.value+=keyval;
						break;
				}
			}
		
		}
	return window.event.returnValue = false;
}

/*********************************************************************************************************
	Function: date_validate
	Description: Basic function that checks the text for mm/dd/yy or mm/dd/yyyy format
	
	Parameters:
*********************************************************************************************************/
function date_validate()
{

	var field = window.event.srcElement;
	var err;
	var sErrMssg;
	
	//Allow a Blank Date
	if(field.value.length==0)
		return false;
	
	var aComponents=field.value.split("/");
	
	if(aComponents.length != 3)
		{
		alert("The Date you provided is incomplete or not in \"mm\/dd\/yyyy\" format. Process=Split");
		field.select();
		field.focus();
		return false;
		}
	var mnth=aComponents[0];
	var dy=aComponents[1];
	var yr=aComponents[2];

	
	//alert("mnth=" + mnth + "  dy=" + dy + " yr=" + yr);
	//alert("mnth=" + parseInt((mnth * 1)) + "  dy=" + parseInt(dy) + " yr=" + parseInt(yr));
	
	
	switch(yr.length)
	{
		case 2: //force 4 digit Year
			if(parseInt(yr, 10) < 20)
				yr="20" + yr;
			else
				yr="19" + yr;
		case 4:
			break;
		default:
			{
			alert("The Date you provided is incomplete. The Year value is not the correct length");
			field.select();
			field.focus();
			return false;
			}
		
	}
	
	if(mnth.length < 1 || dy.length < 1 || yr.length < 2)
		{
		alert("The Date you provided is incomplete. Missing the Month and/or Day and/or Year");
		field.select();
		field.focus();
		return false;
		}
	
	//Check the month value
	x = parseInt((mnth*1),10);
	if(x < 1 || x > 12)
		{
		alert("The Date you provided is incomplete or not in \"mm\/dd\/yy\" format. Month Check = (" + x + ")");
		field.select();
		field.focus();
		return false;
		}
	//Normalize if a 1 digit month
	if(mnth.length < 2)
		mnth="0" + mnth;
		
	//Check the Day Value
	if(parseInt((dy*1), 10) < 1)
		{
		alert("The Date you provided is incomplete or not in \"mm\/dd\/yy\" format. Day Check = (" + parseInt(dy) + ")");
		field.select();
		field.focus();
		return false;
		}
	err=false;
	switch(parseInt((mnth*1), 10))
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if(parseInt(dy, 10) < 1 || parseInt(dy, 10) > 31)
				{
				err=true;
				sErrMssg="The \"Day\" value is not correct for the month.";
				}
			break;
		case 2:
			if(yr % 4 == 0)
				{
				if(parseInt(dy, 10) < 1 || parseInt(dy, 10) > 29)
					{
					err=true;
					sErrMssg="The \"Day\" value is not correct for February.";
					}
				}
			else
				{
				if(parseInt(dy, 10) < 1 || parseInt(dy, 10) > 28)
					{
					err=true;
					sErrMssg="The \"Day\" value is not correct for February. The year may not be a Leap Year";
					}
				}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			if(parseInt((dy*1), 10) < 1 || parseInt((dy*1), 10) > 30)
				err=true;
			break;
		}
	if(err)
		{
		alert(sErrMssg);
		field.select();
		field.focus();
		return false;
		}
	else
		{
		if(dy.length < 2)
			dy="0" + dy;
		
		field.value=mnth + "/" + dy + "/" + yr
		return true;
		}
}



/*********************************************************************************************************
	Function: time_onkeypress
	Description: Basic function insures appropriate keystrokes for inputting a time value.
	
	Parameters:
*********************************************************************************************************/
function time_onkeypress()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;
	var err;

//	if(field.value.length==8)
//		return window.event.returnValue = false;	
	
	switch(inputChar)
	{
		case 8:
		case 9:
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
		case 58:
		case 109:
		case 77:
		case 65:
		case 97:
		case 80:
		case 112:
		case 32:
			err=true;
			break;
		default:
			err=false;
			break;
			
	}
	
		return window.event.returnValue = err;
}

/*********************************************************************************************************
	Function: time_onkeypress
	Description: Basic function insures appropriate keystrokes for inputting a time value.
	
	Parameters:
*********************************************************************************************************/
function time_onkeypress_assist()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;
	var err;
	
	
	if(inputChar==8 || inputChar==9)
		return window.event.returnValue = true;

	if(field.value.length > 8)
		return window.event.returnValue = false;	

	
	switch(inputChar)
	{
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
		case 58:
		case 109:
		case 77:
		case 65:
		case 97:
		case 80:
		case 112:
			err=true;
			break;
		default:
			err=false;
			break;
			
	}
	
	if(err==false)
		return window.event.returnValue = err;
	
	/*
	ITEM #1 04-15-03
	If an 'M' Exists in the field erase it before the next keystroke
	*/
	if(field.value.indexOf("M") > -1)
		field.value="";
		
	var keyval=String.fromCharCode(inputChar);
	if(field.value.length==0)
		{
		//Do not accept anything but a number as the first character
		if(isNaN(keyval))
			return window.event.returnValue = false;
		if(parseInt(keyval) > 1)
			field.value += keyval + ":";
		else
			field.value += keyval;
		}
	else // the field has at least 1 digit
		{
		//See if the semi colon exists in the Field
		var aComponents=field.value.split(":");
		switch(aComponents.length)
			{
			case 1: 
				switch(keyval)
					{
					case "a":
					case "A":
						field.value+=":00 AM";
						break;
					case "p":
					case "P":
						field.value+=":00 PM";
						break;
					case ":":
						sTmp=field.value;
						if(sTmp.indexOf(":") < 0)
							field.value+=":";
						break;
					case "0":
					case "1":
					case "2":
						if(field.value.length==1)
							field.value+=keyval;
						else
							field.value+=":"+keyval;
						break;
					case "3":
					case "4":
					case "5":
						field.value+=":" + keyval;
						break;
					default:
						break;
					}
				break;
			default: 
				var hr=aComponents[0];
				var mn=aComponents[1];
				hr=hr.slice(0,2);
				mn=mn.slice(0,2);
				switch(keyval)
					{
					case ":":
						sTmp=field.value;
						if(sTmp.indexOf(":") < 0)
							field.value+=":";
						break;
					case "a":
					case "A":
						if(mn.length == 0)
							field.value+="00 AM";
						else
							{
							if(mn.length < 2)
								field.value = hr + ":0" + mn + " AM";
							else
								field.value = hr + ":" + mn + " AM";
							}
						break
					case "P":
					case "p":
						if(mn.length == 0)
							field.value+="00 PM";
						else
							{
							if(mn.length < 2)
								field.value = hr + ":0" + mn + " PM";
							else
								field.value = hr + ":" + mn + " PM";
							}
						break;
					case "M":
					case "m":
						sTmp=field.value;
						if(sTmp.charAt(sTmp.length-1).toUpperCase()=="P" || sTmp.charAt(sTmp.length-1).toUpperCase()=="A")
							field.value+="M";
						break;
					case "0":
					case "1":
					case "2":
					case "3":
					case "4":
					case "5":
						if(mn.length < 2)
							field.value+=keyval;
						break;
					case "6":
					case "7":
					case "8":
					case "9":
						if(mn.length < 2 && mn.length > 0)
							field.value+=keyval;
						break;
					}
				break;
			}
		}
		
	return window.event.returnValue = false;
}

/*********************************************************************************************************
	Function: time_validate
	Description: Basic function insures a time value entry complies with an hh:mm AM/PM format.
	
	Parameters:
09-27-02 Rob Plates modification for not forcing a leading zero in the hour portion of the time.
'ITEM #1 Changed Required Length from 8 to 7
*********************************************************************************************************/
function time_validate()
{
	var field = window.event.srcElement;
	var err;
	

	//Allow a Blank Time
	if(field.value.length==0)
		return;
	
	//Item #1 09-27-02	
	if(field.value.length < 7)
		{
		alert("Please provide the Time Format in \"hh:mm AM/PM\" format.");
		field.select();
		field.focus();
		return;
		}

	// Split the Time and day portion
	var aComponents=field.value.split(" ");
	if(aComponents.length != 2)
		{
		alert("Please provide the Time Format in \"hh:mm AM/PM\" format.");
		field.select();
		field.focus();
		return;
		}
	
	
	var ampm=aComponents[1].toUpperCase();

	aComponents=aComponents[0].split(":");

	if(aComponents.length != 2)
		{
		alert("Please provide the Time Format in \"hh:mm AM/PM\" format.");
		field.select();
		field.focus();
		return;
		}
		
	
	var hr=aComponents[0];
	var mn=aComponents[1];

	// does the string end in AM or PM ????
	if(ampm != "AM" && ampm != "PM")
		{
		alert("Please provide the Time Format in \"hh:mm AM/PM\" format.");
		field.select();
		field.focus();
		return;
		}
	
	if(parseInt(hr * 1) < 1 || parseInt(hr * 1) > 12)
		{
		alert("Please provide the Time Format in \"hh:mm AM/PM\" format.");
		field.select();
		field.focus();
		return;
		}
	

	if(parseInt(mn * 1) < 0 || parseInt(mn * 1) > 59)
		{
		alert("Please provide the Time Format in \"hh:mm AM/PM\" format.");
		field.select();
		field.focus();
		return;
		}
	//If we get here all the components are good now normalize
	field.value=hr + ":" + mn + " " + ampm;
	
}

/*********************************************************************************************************
	Function: number_onkeypress
	Description: Basic function insures a numbers only are entered in the keyboard
	
	Parameters:
*********************************************************************************************************/
function number_onkeypress()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;
	switch(inputChar)
		{
		case 8:
		case 9:
		case 48: //0
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			return window.event.returnValue = true;
			break;
		default:
			return window.event.returnValue = false;
			break;
		}
}


/*********************************************************************************************************
	Function: decimal_onkeypress
	Description: Basic function insures a numbers and 1 decimal point are entered in the keyboard
	
	Parameters:
*********************************************************************************************************/
function decimal_onkeypress()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;
	switch(inputChar)
		{
		case 8:
		case 9:
		case 48: //0
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			return window.event.returnValue = true;
			break;
		case 46:
			if(field.value.indexOf(".") >= 0)
				return window.event.returnValue = false;
			else
				return window.event.returnValue = true;
			break;
		default:
			return window.event.returnValue = false;
			break;
		}
}

/*********************************************************************************************************
	Function: number_code onkeypress
	Description: Basic function insures a numbers and some "seperator" characters to "codize" the resulting input
	
	Parameters:
*********************************************************************************************************/
function number_code_onkeypress()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;
	switch(inputChar)
		{
		case 8:
		case 9:
		case 32: //Space
		case 45: //Minus or Hyphen
		case 48: //0
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			return window.event.returnValue = true;
			break;
		default:
			return window.event.returnValue = false;
			break;
		}
}

/*********************************************************************************************************
	Function: currency_keypress
	Description: Basic function insures a numbers and 1 decimal point are entered in the keyboard
	
	Parameters:
*********************************************************************************************************/
function currency_keypress()
{
	//Only Allow Numbers and Decimal
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;
	switch(inputChar)
		{
		case 8:
		case 9:
		case 45: //Minus or Hyphen
		case 48: //0
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			return window.event.returnValue = true;
			break;
		case 46:
			//For this char make sure it does not exist already.
			if(field.value.indexOf('.') == -1)
				return window.event.returnValue = true;
			else
				return window.event.returnValue = false;
			break;
		default:
			return window.event.returnValue = false;
			break;
		}
}

/*********************************************************************************************************
	Function: currency_onfocus
	Description: Basic function removes the Currency symbol and "selects" the element to prepare for numeric data entry
	
	Parameters:
*********************************************************************************************************/
function currency_onfocus()
{
	var field = window.event.srcElement;
	field.value=currency_strip_symbol(field.value);
	field.select();
}

/*********************************************************************************************************
	Function: currency_strip_symbol
	Description: Basic function removes the Currency symbol if there in a string Assumes the Currency Sysmbol leads the value
	(U.S. Currency Format Only)
	
	Parameters:
*********************************************************************************************************/
function currency_strip_symbol(val)
{
	if(val.indexOf('$') != -1)
		return val.substring(1, val.length);
	else
		return val;
}

/*********************************************************************************************************
	Function: currency_validate
	Description: Uses custom element attributes to see if a number needs to be entered. 
	
	Parameters:
*********************************************************************************************************/
function currency_validate()
{
	var field = window.event.srcElement;
	var strTemp=currency_strip_symbol(field.value);
	if(field.value.length==0)
		if(field.required != undefined)
			{
			alert("You must provide a value");
			field.focus();
			return;
			
			}
	//Is the Value a Number
	if (isNaN(field.value))
		{
		alert("The value in this field must be numeric.");
		field.select();
		field.focus();
		return;
		}
	else
		currency_format(field);
}	

/*********************************************************************************************************
	Function: currency_format
	Description: checks page element and adjusts (if possible) Uses a custom attribute to force zeros to be output if a blank field is
	given
	
	Parameters:
*********************************************************************************************************/
function currency_format(field)
{
	//find the decimal
	var strTemp=field.value;
	var bNoLeftDigits=false;
	var decimal_pos=-1;
	
	var bIsNegative=(parseFloat(strTemp) < 0);
	if(bIsNegative)
		strTemp=String(parseFloat(strTemp) * -1);

	while(decimal_pos==-1)
		{
		decimal_pos=strTemp.indexOf('.');
		if(decimal_pos==-1) //No Decimal in Number
			{
			strTemp= strTemp + ".00";
			}
		}
	if(decimal_pos==0)
		{
		bNoLeftDigits=true;
		strTemp="0" + strTemp;
		decimal_pos++;
		}
	//find how many chars to the right of the decimal
	decimal_pos++;
	if(strTemp.length-decimal_pos > 2)
		{
		var post_digit=parseInt(strTemp.charAt(decimal_pos+2));
		strTemp=String(parseInt(strTemp.substring(0,decimal_pos-1)) + (Math.round(strTemp.substring(decimal_pos,decimal_pos+2)+"."+post_digit.toString()) / 100));
		strTemp=strTemp.substr(0,decimal_pos+2);
		decimal_pos=-1;
		while(decimal_pos==-1)
			{
			decimal_pos=strTemp.indexOf('.');
			if(decimal_pos==-1) //No Decimal in Number
				{
				strTemp= strTemp + ".00";
				}
			}
		if(decimal_pos==0)
			{
			//bNoLeftDigits=true;
			strTemp="0" + strTemp;
			decimal_pos++;
			}
		decimal_pos++;
		}
	if(strTemp.length-decimal_pos < 2)
		{
		for(var x=0;x<=2-(strTemp.length-decimal_pos);x++)
			strTemp += "0";
		}
	if(parseFloat(strTemp)==0)
		if(field.ForceZeros==undefined)
			strTemp="";


	if(strTemp.length > 0)
		{
		if(bIsNegative)
			strTemp="-"+strTemp;
		field.value="$" + strTemp;
		}
	else
		field.value=strTemp;
}

/*********************************************************************************************************
	Function: currency_sformat
	Description: checks a string and adjusts (if possible) Uses a custom attribute to force zeros to be output if a blank field is
	given
	
	Parameters:
*********************************************************************************************************/
function currency_sformat(field)
{
	//find the decimal
	var strTemp=field;
	//var bNoLeftDigits=false;
	var decimal_pos=-1;
	
	if(field.length==0)
		return "";

	var bIsNegative=(parseFloat(strTemp) < 0);
	if(bIsNegative)
		strTemp=String(parseFloat(strTemp) * -1);

	while(decimal_pos==-1)
		{
		decimal_pos=strTemp.indexOf('.');
		if(decimal_pos==-1) //No Decimal in Number
			{
			strTemp= strTemp + ".00";
			}
		}
	if(decimal_pos==0)
		{
		//bNoLeftDigits=true;
		strTemp="0" + strTemp;
		decimal_pos++;
		}
	//find how many chars to the right of the decimal
	decimal_pos++;
	if(strTemp.length-decimal_pos > 2)
		{
		var post_digit=parseInt(strTemp.charAt(decimal_pos+2));
		strTemp=String(parseInt(strTemp.substring(0,decimal_pos-1)) + (Math.round(strTemp.substring(decimal_pos,decimal_pos+2)+"."+post_digit.toString()) / 100));
		strTemp=strTemp.substr(0,decimal_pos+2);
		decimal_pos=-1;
		while(decimal_pos==-1)
			{
			decimal_pos=strTemp.indexOf('.');
			if(decimal_pos==-1) //No Decimal in Number
				{
				strTemp= strTemp + ".00";
				}
			}
		if(decimal_pos==0)
			{
			//bNoLeftDigits=true;
			strTemp="0" + strTemp;
			decimal_pos++;
			}
		decimal_pos++;
		}
	if(strTemp.length-decimal_pos < 2)
		{
		for(var x=0;x<=2-(strTemp.length-decimal_pos);x++)
			strTemp += "0";
		}

	if(bIsNegative)
		strTemp="-"+strTemp;

	if(strTemp.length > 0)
		return "$" + strTemp;
	else
		return strTemp;
}

/*********************************************************************************************************
	Function: alpha_ucase_onkeypress
	Description: If the keystroke being processed is an alpha character, it is converted to upper case.
	
	Parameters:
*********************************************************************************************************/
function alpha_ucase_onkeypress()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;
	if(inputChar==8 || inputChar==9 || inputChar==32)
		return window.event.returnValue = true;

	if (inputChar >=97 && inputChar <=122)
		{
		window.event.keyCode=inputChar-32;
		}
}


/*********************************************************************************************************
	Function: alpha_numeric_onkeypress
	Description: Allow only alpha and numeric characters disallow all others
	Parameters:
*********************************************************************************************************/
function alpha_numeric_onkeypress()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;

	if(inputChar==8 || inputChar==9 || inputChar==32)
		return window.event.returnValue = true;

	if ((inputChar >=47 && inputChar <=57) || (inputChar >=65 && inputChar <=90) || (inputChar >=97 && inputChar <=122))
		return window.event.returnValue = true;
	else
		return window.event.returnValue = false;
}

/*********************************************************************************************************
	Function: alpha_ucase_numeric_onkeypress
	Description: Allow only alpha and numeric characters disallow all others. Convert Alpha to Upper case.
	Parameters:
*********************************************************************************************************/
function alpha_ucase_numeric_onkeypress()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;

	if(inputChar==8 || inputChar==9 || inputChar==32)
		return window.event.returnValue = true;

	if ((inputChar >=47 && inputChar <=57) || (inputChar >=65 && inputChar <=90) || (inputChar >=97 && inputChar <=122))
		{
		if(inputChar >=97 && inputChar <=122)
			window.event.keyCode=inputChar-32;
		return window.event.returnValue = true;
		}
	else
		return window.event.returnValue = false;
}


/*********************************************************************************************************
	Function: alpha_only_ucase_onkeypress
	Description: If the keystroke being processed is an alpha character, it is converted to upper case.
	
	Parameters:
*********************************************************************************************************/
function alpha_only_ucase_onkeypress()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;
	
	if(inputChar==8 || inputChar==9)
		return window.event.returnValue = true;

	if ((inputChar >=97 && inputChar <=122) || (inputChar >=65 && inputChar <=90))
		{
		if(inputChar >=97 && inputChar <=122)
			window.event.keyCode=inputChar-32;
		return window.event.returnValue=true;
		}
	else
		return window.event.returnValue=false;
}

/*********************************************************************************************************
Function to Format Phone Numbers entered as Straight digits
*********************************************************************************************************/
function phone_validate(bForceAreaCode)
{
	var field = window.event.srcElement;

	var ex="";
	var ac="";
	var nn="";

	//Allow a Blank
	if(field.value.length==0)
		return false;
	
	if(bForceAreaCode)
		{
		if(field.value.length < 10)
			{
			alert("Please Provide a AAAXXXNNNN Formatted phone number where\n\"AAA\"=AreaCode\n\"XXX\"=The Exchange Number\n\"NNNN\"=The Remaining Phone Number Digits.");
			field.focus();
			field.select();
			return false;
			}
		else
			{
			var ac=field.value.substring(0,3);
			var ex=field.value.substring(3,6);
			var nn=field.value.substring(6,10);
			field.value="(" + ac + ") " + ex + " " + nn
			}
		}
	else // Area Code Not Mandatory but accepted
		{
		if(field.value.length > 7)
			{
			//Check for Full Number
			if(field.value.length < 10)
				{
				alert("Please Provide a AAAXXXNNNN Formatted phone number where\n\"AAA\"=AreaCode\n\"XXX\"=The Exchange Number\n\"NNNN\"=The Remaining Phone Number Digits.");
				field.focus();
				field.select();
				return false;
				}
			else
				{
				var ac=field.value.substring(0,3);
				var ex=field.value.substring(3,6);
				var nn=field.value.substring(6,10);
				field.value="(" + ac + ") " + ex + " " + nn
				}
			}
		else
			{
			if(field.value.length < 7)
				{
				alert("The Phone Number Given is incomplete.");
				field.focus();
				field.select();
				return false;
				}
			else
				{
				var ex=field.value.substring(0,3);
				var nn=field.value.substring(3,7);
				field.value=ex + " " + nn
				}
			}
		}	
}

/*********************************************************************************************************
function to strip the formatting of a phone number element
*********************************************************************************************************/
function phone_strip_formatting()
{
	var field = window.event.srcElement;
	var sTmp="";
	
	if(field.value.length==0)
		return true;
	for(var x=0;x<field.value.length;x++)
		{
		switch( field.value.substr(x,1) )
			{
			case "0":
			case "1":
			case "2":
			case "3":
			case "4":
			case "5":
			case "6":
			case "7":
			case "8":
			case "9":
				sTmp+=field.value.substr(x,1);
				break;	
			}
		}	
	field.value=sTmp;
	field.select();
	return true;
}


/*********************************************************************************************************
function to strip the formatting of a phone number element
*********************************************************************************************************/
function phone_strip_formatting_string(sFormattedPhoneNumber)
{
	
	var sTmp="";
	if(sFormattedPhoneNumber.length==0)
		return sFormattedPhoneNumber;
	for(var x=0;x<sFormattedPhoneNumber.length;x++)
		{
		switch( sFormattedPhoneNumber.substr(x,1) )
			{
			case "0":
			case "1":
			case "2":
			case "3":
			case "4":
			case "5":
			case "6":
			case "7":
			case "8":
			case "9":
				sTmp+=sFormattedPhoneNumber.substr(x,1);
				break;	
			}
		}	
	return sTmp;
}

/*********************************************************************************************************
Function to Format Phone Numbers entered as Straight digits
*********************************************************************************************************/
function phone_format_string(sPhoneDigits)
{
	var ex="";
	var ac="";
	var nn="";

	//Allow a Blank
	if(sPhoneDigits.length==0)
		return "";
	
	if(sPhoneDigits.length >= 10)
		{
			ac="("+sPhoneDigits.substring(0,3)+") ";
			ex=sPhoneDigits.substring(3,6)+" ";
			nn=sPhoneDigits.substring(6,10);
		}
	
	if(sPhoneDigits.length < 10 && sPhoneDigits.length == 7)
		{
			ex=sPhoneDigits.substring(0,3)+" ";
			nn=sPhoneDigits.substring(3,7);
		}
	if(sPhoneDigits.length < 10 && sPhoneDigits.length != 7)
		{
		return sPhoneDigits;
		}
	return ac+ex+nn;
}


/*********************************************************************************************************
	Function: email_onkeypress
	Description: only allows alpha, numbers and the hyphen character
	
	Parameters:
*********************************************************************************************************/
function email_onkeypress()
{
	var field = window.event.srcElement;
	var inputChar = window.event.keyCode;

	if(inputChar==8 || inputChar==9)
		return window.event.returnValue = true;

	if (inputChar==64 || inputChar==45 || inputChar==46 || (inputChar >=47 && inputChar <=57) || (inputChar >=65 && inputChar <=90) || (inputChar >=97 && inputChar <=122))
		return window.event.returnValue = true;
	else
		return window.event.returnValue = false;
}

/*********************************************************************************************************
	Function: email_validate
	Description: checks for a proper email format
	returns true if there is a problem with the given email string
	
	Parameters:
*********************************************************************************************************/
function email_validate(sEmailString, bAllowBlank)
{
	if(sEmailString.length==0)
		{
		if(bAllowBlank==true)
			return false;
		else
			return true;
		}
	if(sEmailString.length <= 5)
		return true; // must have more than 5 characters a@b.c
		
	var iAtCharPos=sEmailString.indexOf("@");
	if (iAtCharPos==-1 || iAtCharPos==0)
		return true; //@ char missing or in wrong position
	
	if(sEmailString.indexOf("@", iAtCharPos+1) > -1)
		return true; // More than one
		
	var iDotChar=sEmailString.indexOf(".")
	if(iDotChar==-1 || iDotChar==0 || iDotChar < iAtCharPos || iDotChar==sEmailString.length-1 || iDotChar==sEmailString.length-2)
		return true;
	//ITEM #1 11-19-03
	else
		{
		var iDotChar2=sEmailString.indexOf(".", iDotChar+1);
		if(iDotChar2==0 || iDotChar2==sEmailString.length-1 || iDotChar2==sEmailString.length-2)
			return true;
		else
			return false;
		}
}

/*********************************************************************************************************
	Function: amex_validate
	Description: checks for a proper amex format
	returns true if there is a problem with the given string
	
	Parameters:
*********************************************************************************************************/
function amex_validate(sCardNumber)
{
	var sTest="";
	var sChar;
	if(sCardNumber.length==0)
		return true;
		
	//First Strip anything but numbers
	for(var x=0;x<sCardNumber.length;x++)
		{
		sChar=sCardNumber.charCodeAt(x);
		if(sChar >= 48 && sChar <= 57)
			sTest+=sCardNumber.charAt(x);
		}
	// Is it Long Enough
	if(sTest.length != 15)
		return true;
	// Good Start
	if(sCardNumber.charAt(0) != "3")
		return true;
	else
		return false;
}



/*********************************************************************************************************
	Function: dociver_validate
	Description: checks for a proper amex format
	returns true if there is a problem with the given string
	
	Parameters:
*********************************************************************************************************/
function discover_validate(sCardNumber)
{
	var sTest="";
	var sChar;
	if(sCardNumber.length==0)
		return true;
		
	//First Strip anything but numbers
	for(var x=0;x<sCardNumber.length;x++)
		{
		sChar=sCardNumber.charCodeAt(x);
		if(sChar >= 48 && sChar <= 57)
			sTest+=sCardNumber.charAt(x);
		}
	// Is it Long Enough
	if(sTest.length != 16)
		return true;
	// Good Start
	if(sCardNumber.charAt(0) != "6")
		return true;
	else
		return false;
}

/*********************************************************************************************************
	Function: visa_validate
	Description: checks for a proper amex format
	returns true if there is a problem with the given string
	
	Parameters:
*********************************************************************************************************/
function visa_validate(sCardNumber)
{
	var sTest="";
	var sChar;
	if(sCardNumber.length==0)
		return true;
		
	//First Strip anything but numbers
	for(var x=0;x<sCardNumber.length;x++)
		{
		sChar=sCardNumber.charCodeAt(x);
		if(sChar >= 48 && sChar <= 57)
			sTest+=sCardNumber.charAt(x);
		}
	// Is it Long Enough
	if(sTest.length != 16)
		return true;
	// Good Start
	if(sCardNumber.charAt(0) != "4")
		return true;
	else
		return false;
}

/*********************************************************************************************************
	Function: visa_validate
	Description: checks for a proper amex format
	returns true if there is a problem with the given string
	
	Parameters:
*********************************************************************************************************/
function mastercard_validate(sCardNumber)
{
	var sTest="";
	var sChar;
	if(sCardNumber.length==0)
		return true;
		
	//First Strip anything but numbers
	for(var x=0;x<sCardNumber.length;x++)
		{
		sChar=sCardNumber.charCodeAt(x);
		if(sChar >= 48 && sChar <= 57)
			sTest+=sCardNumber.charAt(x);
		}
	// Is it Long Enough
	if(sTest.length != 16)
		return true;
	// Good Start
	if(sCardNumber.charAt(0) != "5")
		return true;
	else
		return false;
}

