/*
*********************************************************************************
	Author:  Craig Kelley, (mail@vdomains.com)
			 Bob Vogel
	
	This library contains a variety of functions that are used throughout
	AIMS.  As you add to the library be sure to comment each function
********************************************************************************
*/

//======================================================
//	Function:  frameBuster()
//	Reloads URL in frameless page.
//	Example call:  frameBuster()
//======================================================	
//setTimeout ("frameBuster()", 3000);
function frameBuster() {
	if (self.parent.frames.length != 0)
		self.parent.location=document.location;
}

//======================================================
//	Function:  redirect(URL,Frame)
//	Redirects to whatever URL 'X' in Frame 'Y'
//	If 'Y' is left blank '' then it will redirect in the 
//  current window.
//	Example call:  redirect("myURL.asp", "bottom")
//======================================================	
function redirect(X,Y) {
	if (Y != ""){
		parent.frames[Y].location = X;
	} else {
		window.location = X;
	}
}

//======================================================
//	Function:  is_Date(x, desc)
//  Validates that the date is in MM/DD/YYYY format	
//	Parameters:	x = date to verify	y = Text discription of the date
//	Example call:  is_Date(text1.text, "To Date: ")
//	written by Bob Vogel 10/31/00
//======================================================	
function is_Date(x, desc) {
	//specify format string
	var datePat = /^(\d{2})(\/)(\d{2})(\/)(\d{4})$/;
	//load input into matchArray
	var matchArray = x.match(datePat);
	
	
		
	if (matchArray == null) {
		if (desc != "")
			alert(desc + " must be in format MM/DD/YYYY.");
		return false;
	}
	// set values to vars
	month = matchArray[1];
	day = matchArray[3];
	year = matchArray[5];
	//check valid month
	if (month < 1 || month > 12) {
		if (desc != "")
			alert(desc + " Month must be between 1 and 12.");
		return false;
	}
	//check max & min days
	if (day < 1 || day > 31) {
		if (desc != "")
			alert(desc + " Day must be between 1 and 31.");
		return false;
	}
	// check for 30 days
	if ((month=='04' || month=='06' || month=='09' || month=='11' || month=='4' || month=='6' || month=='9') && day==31) {
		if (desc != "")
			alert(desc + " Month doesn't have 31 days!");
		return false;
	}
	// checking feb for leap year
	if (month == 2) 	{
		if (LeapYear(year) == true) 	{
			if (day > 29) {
				if (desc != "")
					alert(desc + " Month doesn't have 29 days!");
				return false;
			}
		}
		else 	{
			if (day > 28) 	{
				if (desc != "")
					alert(desc + " Month doesn't have 28 days!")
				return false;
			}
		}
	}// end of leap year test

	return true;
}

//======================================================
//	Function:  LeapYear(x)
//  Determines if the year passed is a leap year	
//	Parameters:	x = Year
//	Example call:  LeapYear(1995) returns false
//	written by Bob Vogel 10/31/00
//======================================================	
function LeapYear(x) 
//determine if it is a leap year
{
	if (x % 100 == 0) 	{
		if (x % 400 == 0)		{ 
			return true;	
		}
	}
	else 	{
		if ((x % 4) == 0)		{
			return true; 
		}
	}
	return false;
}
//======================================================
//	Function:  Date_Diff(x, y)
//  Calculated the number of days inbetween two dates	
//	Parameters:	x = To date	y = From Date
//	Example call:  Date_Diff(10/10/1995, 10/11/1995) returns 1
//	written by Bob Vogel 10/31/00
//======================================================	
function Date_Diff (x, y){
	date1 = new Date();
	date1 = Date.parse(x);
	date2 = new Date();
	date2 = Date.parse(y);
	// calculate difference and convert to days	
	result = date1 - date2;
	result = result/86400000;
	return result;
}

//======================================================
//	Function:  StripNonAlphaNumeric(strText)
//  Author:  Bob Vogel
//	removes undesired characters from user input
//	Example call:  StripNonAlphaNumeric("asdf1561")
//======================================================	

function StripNonAlphaNumeric(strText) {
		strOutput = new String("");
		i = strText.length;
		ii = 0
		while(ii<i)
		{
			//get one char at a time
			strTemp = strText.charAt(ii);
			//get ascii value
			iii = strText.charCodeAt(ii)
			//check if A-Z or a-z or 0-9 or " "				
			if (( iii>=65 && iii<=90 ) 
					|| ( iii>=97 && iii<=122) 
					|| ( iii>= 48 && iii<=57) 
					|| (iii == 32))
			{
				strOutput = strOutput + strTemp				
			} 
			ii++;
		}
		return strOutput;
	}
	
//======================================================
//	Function:  StripNonNumeric(strText)
//  Author:  Bob Vogel
//	removes undesired characters from user input
//	Example call:  StripNonAlphaNumeric("asdf1561")
//======================================================	

function StripNonNumeric(strText) {
		strOutput = new String("");
		i = strText.length;
		ii = 0
		while(ii<i)
		{
			//get one char at a time
			strTemp = strText.charAt(ii);
			//get ascii value
			iii = strText.charCodeAt(ii)
			//check if A-Z or a-z or 0-9 or " "				
			if (iii>= 48 && iii<=57)
			{
				strOutput = strOutput + strTemp				
			} 
			ii++;
		}
		return strOutput;
	}
	
//======================================================
//	Function:  StripCharacters(strText, UnwantedChars)
//  Author:  Bob Vogel, Chris Abdelmalek
//	removes undesired characters from user input
//  The characters to be removed are passed as a string 
//  in the second argument
//	Example call:  StripCharacters("asdf1561", "$&^%")
//======================================================	
function StripCharacters(strText, strChars) {
		strOutput = new String("");
		i = strText.length;
		ii = 0
		while(ii<i)
		{
			//get one char at a time
			strTemp = strText.charAt(ii);
			
			//check if this character is one of the unwanted ones
			if ( strChars.indexOf( strTemp ) < 0 )
			{
				strOutput = strOutput + strTemp;			
			} 
			ii++;
		}
		return strOutput;
	}
			
//======================================================
//	Function:  StripBlanks(strText)
//  Author:  Bob Vogel, Chris Abdelmalek
//  this function will remove all blanks
//	Example call:  StripBlanks(somevariable)
//======================================================	
	
function StripBlanks(strText)
{
	return (StripCharacters( strText, " "));
}	

//======================================================
//	Function:  StripDollar(strText)
//  Author:  Bob Vogel, Chris Abdelmalek
//  this function will remove all Dollar Signs
//	Example call:  StripDollar(somevariable)
//======================================================	
	
function StripDollar(strText) 
{
	return (StripCharacters( strText, "$"));
}	
	
//======================================================
//	Function:  isThere(strIn)
//  Author:  Bob Vogel, Chris Abdelmalek
//	used to determine if a field contains any values
//  returns true if the string contains at least 1 non-blank character,
//  returns false otherwise.
//	Example call:  isThere(somestring)
//======================================================			
function isThere(strIn) 
{
	strTest = new String("");
	//remove blanks
	strTest = StripBlanks(strIn);
	return (strTest.length > 0)

}

//======================================================
//	Function:  isBlank(strIn)
//  Author:  Bob Vogel, Chris Abdelmalek
//	used to determine if a field only contains blanks
//  returns true if the string is null, or contains nothing but blanks,
//  returns false otherwise.
//	Example call:  isBlank(somestring)
//======================================================			
function isBlank(strIn) 
{
	strTest = new String("");
	//remove blanks
	strTest = StripBlanks(strIn);
	return (strTest.length == 0)

}

//======================================================
//	Function:  isAlpha(strIn)
//  Author:  Chris Abdelmalek
//	used to determine if a field contains only letters
//  returns true if the string only contains letters A-Z, 
//  and a-z and Spaces
//  returns false otherwise.
//	Example call:  isAlpha(somestring)
//======================================================			
function isAlpha( strIn )
{
	//this will match the first non A-Z, a-z character or space
	re = /[^A-Za-z ]/;
	return (!re.test(strIn));
}

//======================================================
//	Function:  isDigit(strIn)
//  Author:  Chris Abdelmalek
//	used to determine if a field contains only number
//  returns true if the string only contains numbers 0-9
//  returns false otherwise.
//	Example call:  isDigit(somestring)
//======================================================			
function isDigit( strIn )
{
	//match the first non-number
	re = /[^0-9]/;
	return (!re.test(strIn));
}

//======================================================
//	Function:  isNumeric(strIn)
//  Author:  Chris Abdelmalek
//	used to determine if input is a valid number
//  It could be positive or negative, integer or decimal
//	Example call:  isNumeric(somestring)
//======================================================			
function isNumeric( strIn )
{
	return (!isNaN(strIn));
}

//======================================================
//	Function:  isInteger(strIn)
//  Author:  Chris Abdelmalek
//	used to determine if a field is a positive or negative integer.
//	Example call:  isInteger(somestring)
//======================================================			
function isInteger( strIn )
{
	return (isNumeric(strIn) && (strIn.indexOf(".") == -1));
}

//======================================================
//	Function:  isDecimal(strIn)
//  Author:  Chris Abdelmalek
//	used to determine if a field is a positive or negative
//  real decimal number. Must contain a fractional part.
//	Example call:  isDecimal(somestring)
//======================================================			
function isDecimal( strIn )
{
	return (isNumeric(strIn) && (strIn.indexOf(".") > -1));
}


//======================================================
//	Function:  isZip(strIn)
//  Author:  Chris Abdelmalek
//	used to determine if a field contains a zip code.
//  returns true if the string contains EXACTLY 5 digits
//  returns false otherwise.
//	Example call:  isZip(somestring)
//======================================================			
function isZip( strIn )
{
	re = /\d{5}/;
	return (re.test(strIn));
}

//======================================================
//	Function:  isZip(strIn)
//  Author:  Chris Abdelmalek
//	used to determine if a field contains a valid zip code ext.
//  returns true if the string contains EXACTLY 4 digits
//  returns false otherwise.
//	Example call:  isZipExt(somestring)
//======================================================			
function isZipExt( strIn )
{
	re = /\d{4}/;
	return (re.test(strIn));
}

//======================================================
//	Function:  isPhone(strIn)
//  Author:  Chris Abdelmalek
//	used to determine if a field contains a phone number.
//  returns true if the string contains EXACTLY 10 digits
//  returns false otherwise.
//	Example call:  isPhone(somestring)
//======================================================	
function isPhone( strIn )
{
	re = /\d{10}/;
	return (re.test(strIn));
}

function ValidTime(timeStr)
{
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.
	
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var sMsg = 'Time must be entered in format hh:mm AM/PM';
	var matchArray = timeStr.value.match(timePat);
	
	if (matchArray == null) 
	{
		alert(sMsg);
		return false;
	}
	
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	
	if (second == '') 
		second = null; 
		
	if (ampm == '') 
		ampm = null 

	if (hour < 0  || hour > 23) 
	{
		alert('Hour must be between 1 and 12. (or 0 and 23 for military time)');
		return false;
	}

	if (hour <= 12 && ampm == null) 
	{
		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) 
		{
			alert("You must specify AM or PM.");
			return false;
	   	}
	}
	
	if  (hour > 12 && ampm != null) 
	{
		alert("You can't specify AM or PM for military time.");
		return false;
	}
	
	if (minute<0 || minute > 59) 
	{
		alert ("Minute must be between 0 and 59.");
		return false;
	}

	if (second != null && (second < 0 || second > 59)) 
	{
		alert ("Second must be between 0 and 59.");
		return false;
	}
	
	//timeStr.value = timeStr.value.toUpperCase();
}

//======================================================
//	Function:  isState(strIn)
//  Author:  Bob Vogel, Chris Abdelmalek
//	used to determine if user input has 2 positions 
//  and is Alpha, and Capital letters
//  returns true if the string contains EXACTLY 2 letters A-Z
//  returns false otherwise.
//	Example call:  isState(x)
//======================================================	
		
function isState(strIn) {
	re = /[A-Z]{2}/;
	return (re.test(strIn));
}	

//======================================================
//	Function:  stringLength(strIn)
//  Author:  Craig Kelley
//	used to determine String Length
//  returns string length
//	Example call:  stringLength(x)
//======================================================	
function stringLength(strIn){
	myString = new String(strIn)
	return myString.length
}

var SystemDate = new Date();

function CompareToSystemDate(sDate)
{
  	var DateEntered = new Date(sDate.value);
	if (DateEntered > SystemDate)
	{
		alert("Date cannot be greater than the System Date.");
		sDate.select();
		sDate.focus();
		return false
	}	
}

function GreaterThanSystemDate(sDate)
{
  	var DateEntered = new Date(sDate.value);
	if (DateEntered <= SystemDate)
	{
		alert("Date must be greater than the System Date.");
		sDate.select();
		sDate.focus();
		return false
	}	
}

function CompareTo60DaysPriorDate(sDate)
{
	var Difference = Date_Diff(SystemDate, sDate.value);
	if (Difference > 60) 
	{
		alert("Date cannot be more than 60 days prior to the System Date.");
		sDate.select();
		sDate.focus();
		return false;
	}
}

function ValueExists(sValue,sMsg)
{
	if (sValue.value == '')
	{
		if (sMsg != '')
			alert(sMsg);
		sValue.focus();
		return false;
	}
	else
		return true;		
}

function ValidDate(sDate)
{
	if(!(is_Date(sDate.value,'Date : ')))
	{
		sDate.focus();
		sDate.select();
		return false;
	}
}

function Checked(sCheck)
{
	if (sCheck.checked == true)
		return true;
	else
		return false;
}

function ValidLength(sValue,iLength,sMsg)
{
	if (sValue.value.length != iLength)
	{
		if (sMsg != '')
			alert(sMsg);
		sValue.focus();
		sValue.select();
		return false;
	}
	else
		return true;
}

function ValidNumber(sValue,sMsg)
{
	if (isNumeric(sValue.value) == false)
	{
		if (sMsg != '')
			alert(sMsg);
		sValue.focus();
		sValue.select();
		return false;
	}
	else
		return true;
}

function ValidMoney(sValue,iDollar,iCents)
{
	var	sDollar = '';
	var sCents = '';
	var I;
	
	for (I=1; I<=iDollar; I++)
	{
		sDollar = sDollar + "x";
	}

	for (I=1; I<=iCents; I++)
	{
		sCents = sCents + "x";
	}

	re = eval('/\\d{1,' + iDollar + '}\\.\\d{' + iCents + '}/');

	if (re.test(sValue.value) == false)
	{
		alert('Amount must be entered in format:  ' + sDollar + '.' + sCents);
		sValue.focus();
		sValue.select();
		return false;
	}

	return true;
}

function GetYesNo(sMsg)
{
	return confirm(sMsg);
}

// Open new window with the special attributes
function popUp(sURL,sName,sWidth,sHeight) {
	//alert(sURL)
 	window.open(sURL,sName,"resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,width="+sWidth+",height="+sHeight)
}

// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

// -------------------------------------------------------------------
// autoComplete (text_input, select_input, ["text"|"value"], [true|false])
//   Use this function when you have a SELECT box of values and a text
//   input box with a fill-in value. Often, onChange of the SELECT box
//   will fill in the selected value into the text input (working like
//   a Windows combo box). Using this function, typing into the text
//   box will auto-select the best match in the SELECT box and do
//   auto-complete in supported browsers.
//   Arguments:
//      field = text input field object
//      select = select list object containing valid values
//      property = either "text" or "value". This chooses which of the
//                 SELECT properties gets filled into the text box -
//                 the 'value' or 'text' of the selected option
//      forcematch = true or false. Set to 'true' to not allow any text
//                 in the text box that does not match an option. Only
//                 supported in IE (possible future Netscape).
// -------------------------------------------------------------------
function autoComplete (field, select, property, forcematch) {
	var found = false;
	for (var i = 0; i < select.options.length; i++) {
	if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
		found=true; break;
		}
	}
	if (found) { select.selectedIndex = i; }
	//else { select.selectedIndex = -1; }
	else { select.selectedIndex = 0; } //Default it to the blank option tag
	if (field.createTextRange) {
		if (forcematch && !found) {
			field.value=field.value.substring(0,field.value.length-1); 
			return;
			}
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
				}
			}
		}
	}
<!-- AUTO TEXT DROP DOWN FUNCTIONS ------------- //>