var strButton = "";

/***********************************************************************************
*	FUNCTION: IsAlphaNum
*
*	INPUT: TextField = Field that is to be validated
*	OUTPUT: True or False
*
*	PURPOSE: Ensures that the data in the textbox is only alphanumeric (A-Z, a-z, 0-9 or space).
*			 no punctuation is allowed.
*/
function IsAlphaNum(TextValidate){
	strAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
	
	blnBadString=false
	strValue = trim(TextValidate.value)
	intLength = strValue.length
	
	for(i=0; i<intLength; i++){
		if(strAlphabet.indexOf(strValue.charAt(i))==-1){
			blnBadString=true
		}
	}

	if(blnBadString==true){
		alert("You may only put alphanumerics or spaces in this field.")
		TextValidate.focus()
		return false
	}
	
	TextValidate.value=strValue
	return true
}
/***********************************************************************************
*	FUNCTION: TextValidate
*
*	INPUT: TextField = Field that is to be validated
*	OUTPUT: True or False
*
*	PURPOSE: Ensures that the data in the textbox is only text (A-Z, a-z or space).
*			 no punctuation is allowed.
*/
function TextValidate(TextValidate){
	strAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
	
	blnBadString=false
	strValue = trim(TextValidate.value)
	intLength = strValue.length
	
	for(i=0; i<intLength; i++){
		if(strAlphabet.indexOf(strValue.charAt(i))==-1){
			blnBadString=true
		}
	}

	if(blnBadString==true){
		alert("You may only put letters or spaces in this field.")
		TextValidate.focus()
		return false
	}
	
	TextValidate.value=strValue
	return true
}

/***********************************************************************************
*	FUNCTION: ZipValidate
*
*	INPUT: ZipField = Form Textbox that will be verified
*	OUTPUT: True or False
*
*	PURPOSE: Ensures that the data in the textbox is a standard 5 digit US zipcode.
*			 The 4 digit zipcode suffix will also be checked for but not required.
*			 NOTE: This function only works for US Zipcodes.
*			 NOTE: trim function must also be included when using this function.
*/
function ZipValidate(ZipField){
	strAlphabet = "1234567890"
	
	strZipField = trim(ZipField.value)
	intZipLength = strZipField.length

	if(intZipLength > 0){
		if(intZipLength < 5 || (intZipLength > 5 && intZipLength < 10)){
			alert("You have entered an invalid zipcode.  Please use numbers only and the XXXXX OR XXXXX-XXXX format.")
			ZipField.focus()
			return false
		}
	}
		
	if(intZipLength > 5 && strZipField.charAt(5) != "-"){
		alert("If you are using the 4 digit zip extension please use the following format XXXXX-XXXX")
		ZipField.focus()
		return false
	}
	
	for(i=0; i<intZipLength; i++){
		// POSITION #5 IS THE - IN A 10 DIGIT ZIPCODE (EX. 12345-6789)
		if(i!=5){
			if(strAlphabet.indexOf(strZipField.charAt(i))==-1){
				alert("You have entered an invalid zipcode.  Please use numbers only and the XXXXX OR XXXXX-XXXX format.")
				ZipField.focus()
				return false
			}
		}		
	}

	ZipField.value = strZipField

	return true
}

/***********************************************************************************
*	FUNCTION: PhoneValidate
*
*	INPUT: PhoneField = Form Textbox that will be verified
*	OUTPUT: True or False
*
*	PURPOSE: Checks to see that only numbers are present in the phone field and
*			 checks to make sure that there are 10 numbers present
*			 3 (Area Code) + 3(Prefix) + 4(Suffix)
*/
function PhoneValidate(PhoneField){
	strAlphabet = "1234567890"
	strIllegalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	strPhone = ""
	
	strPhoneNumber = PhoneField.value
	intPhoneLength = strPhoneNumber.length
	blnIllegalChar = false	
	
	for(i=0; i<intPhoneLength; i++){
		if(strIllegalChars.indexOf(strPhoneNumber.charAt(i))!=-1){
			blnIllegalChar=true
		}
	}

	for(i=0; i<intPhoneLength; i++){
		if(strAlphabet.indexOf(strPhoneNumber.charAt(i))!=-1){
			strPhone += strPhoneNumber.charAt(i)
		}
	}
	if(strPhone.length > 0){
		if(strPhone.length != 10){
			alert("Please enter an 10 digit phone number XXX-XXX-XXXX")
			PhoneField.focus()
			return false
		}
	}
	
	if(blnIllegalChar==true){
		alert("Your phone number may not contain letters of the alphabet")
		PhoneField.focus()
		return false
	}

	return true
}

/***********************************************************************************
*	FUNCTION: EmailValidate
*
*	INPUT: EmailField = Form Textbox that will be verified
*	OUTPUT: True or False
*
*	PURPOSE: Ensure that an @ and a valid extension (.com, .edu, .gov, .mil, .net, .org, .cc)
*			 or one of the international e-mail codes are present in the EmailField Textbox.
*/
function EmailValidate(EmailField){

	// FOR VALIDATING INTERNATIONAL E-MAIL ADDRESSES
  var Countries = [ 
  'AD','AE','AF','AG','AI','AL','AM','AN','AO','AQ','AR','AS','AT','AU','AW','AZ','BA',
  'BB','BD','BE','BF','BG','BH','BI','BJ','BM','BN','BO','BR','BS','BT','BV','BW','BY',
  'BZ','CA','CC','CF','CG','CH','CI','CK','CL','CM','CN','CO','CR','CS','CU','CV','CX',
  'CY','CZ','DE','DJ','DK','DM','DO','DZ','EC','EE','EG','EH','ER','ES','ET','FI','FJ',
  'FK','FM','FO','FR','FX','GA','GB','GD','GE','GF','GH','GI','GL','GM','NT','GN','NU',
  'GP','NZ','GQ','OM','GR','PA','GS','PE','GT','PF','GU','PG','GW','PH','GY','PK','HK',
  'PL','HM','PM','HN','PN','HR','PR','HT','PT','HU','PW','ID','PY','IE','QA','IL','RE',
  'IN','RO','IO','RU','IQ','RW','IR','SA','IS','SB','IT','SC','JM','SD','JO','SE','JP',
  'SG','KE','SH','KG','SI','KH','SJ','KI','SK','KM','SL','KN','SM','KP','SN','KR','SO',
  'KW','SR','KY','ST','KZ','SU','LA','SV','LB','SY','LC','SZ','LI','TC','LK','TD','LR',
  'TF','LS','TG','LT','TH','LU','TJ','LV','TK','LY','TM','MA','TN','MC','TO','MD','TP',
  'MG','TR','MH','TT','MK','TV','ML','TW','MM','TZ','MN','UA','MO','UG','MP','UK','MQ',
  'UM','MR','US','MS','UY','MT','UZ','MU','VA','MV','VC','MW','VE','MX','VG','MY','VI',
  'MZ','VN','NA','VU','NC','WF','NE','WS','NF','YE','NG','YT','NI','YU','NL','ZA','NO',
  'ZM','NP','ZR','NR','ZW']
	
	
	intIndexAt = EmailField.value.indexOf("@")
	intLastDotPosition = EmailField.value.lastIndexOf(".")
	strExtension = EmailField.value.slice(intLastDotPosition+1).toUpperCase()
	
	if(intIndexAt==-1){
		alert("Your E-mail address must include the @ sign.")
		EmailField.focus()
		return false
	}
	
	if(strExtension != "COM" && strExtension != "EDU" && strExtension != "GOV" &&
	   strExtension != "MIL" && strExtension != "NET" && strExtension != "ORG" &&
	   strExtension != "CC" && strExtension != "INT" && strExtension != "ARPA" &&
	   strExtension != "NATO"){

		blnOK=false
		for(i=0; i < Countries.length; i++){
			if(strExtension == Countries[i]){
				blnOK=true
				break;
			}
		}

		if(blnOK==false){
			alert("Your E-mail must end with a valid extension (.com, .edu, .gov, .mil, .net, .org, .cc, .int, .arpa, .nato) or a country code (.au, .de, .br)")
			EmailField.focus()
			return false
		}
	}

	return true
}

/***********************************************************************************
*	FUNCTION: IsInteger
*
*	INPUT: FormField = Form textbox that is to be validated
*	OUTPUT: True or False
*
*	PURPOSE: Ensure that the value entered into the textbox contains only numbers
*/
function IsInteger(FormField){
	strAlphabet = "1234567890"
	
	intLength = FormField.value.length
	blnBadString=false
	
	for(i=0; i<intLength; i++){
		intIndex = strAlphabet.indexOf(FormField.value.charAt(i))
		if(intIndex == -1){
			blnBadString=true
		}
	}
	
	if(blnBadString==true){
		alert("This field must contain only numbers")
		FormField.focus()
		return false
	}
	
	return true
}

/***********************************************************************************
*	FUNCTION: trim
*
*	INPUT: strString = string to be trimmed
*	OUTPUT: strString with leading and trailing spaces removed
*
*	PURPOSE: Removes leading and trailing spaces from strString, if strString is
*			 blank then a blank string is returned.
*/
function trim(strString){
	intLength = strString.length
	if(intLength > 0){
		for(start=0; start< intLength && strString.charAt(start)==" "; start++){}
		for(end=intLength-1; end >= 0 && strString.charAt(end)==" "; end--){}
		end++
		return (strString.slice(start,end))
	}
	
	return ""
}

/***********************************************************************************
*	FUNCTION: testCard
*
*	INPUT: strCCfield = form field control which holds the credit card number
*	OUTPUT: None
*
*	PURPOSE: Checks the cretit card with the LUNH checksum
*
*/
function testCard(strCCfield) { 

var s=strCCfield.value
var ccnumber="" 
var digits = "0123456789" 
var sum=0; 

var i 

if(strCCfield.value==0){
	return true
}

//Trim to digits 
 for(i=0;i<s.length;i++) { 
  t=s.charAt(i) 
  if (digits.indexOf(t)>=0) ccnumber=ccnumber+t 
 } 

//For a card with an even number of digits, double every odd numbered 
//digit and subtract 9 if the product is greater than 9. Add 
//up all the even digits as well as the doubled-odd digits, and the 
//result must be a multiple of 10 or it's not a valid card. If the card 
//has an odd number of digits, perform the same addition doubling the 
//even numbered digits instead. 

//The above means go backwards through digits doubling every other one (not first) 
//if double is above 9, or digit is over 4, then subtract 9 
//Do Mod(10) on result to see if it has zero remainder for division by 10 

var lastDoubled=0; 
 for(i=(ccnumber.length - 1);i>=0;i--) { 
  digit=ccnumber.charAt(i) 
  sum += parseInt(digit) 
  lastDoubled++ 
  if(lastDoubled>1) { 
   lastDoubled=0 
   sum += parseInt(digit) 
   if(digit>4) sum-=9; 
  } 
 } 

 s=sum.toString() 
 if (s.charAt(s.length-1)!="0" || ccnumber.length<10 || ccnumber.length>17 || sum==0) { 
  alert("Recheck your Credit Card Number.  It appears to be invalid.") 
  strCCfield.focus();
  return false
 }
 
 return true
} 
//Lunh test END 


/***********************************************************************************
*	FUNCTION: CheckDate
*
*	INPUT: strDateField = date field being passed in
*	OUTPUT: None
*
*	PURPOSE: Checks if the date passed in is greater than or equal to today's date
*			and checks for invalid characters (letters and characters except the /)
*/
function CheckDate(strDateField){
	strAlphabet = "1234567890/"

	strDate = trim(strDateField.value)
	intLength = strDate.length
	blnBadString=false

	for(i=0; i<intLength; i++){
		intIndex = strAlphabet.indexOf(strDate.charAt(i))
		if(intIndex == -1){
			blnBadString=true
		}
	}

	if(blnBadString==true){
		alert("The date field may only contain numbers and the '/' character")
		strDateField.focus()
		return false
	}

	dtmFieldDate = new Date(strDateField.value)
	dtmTempDate = new Date()
	strDateString = dtmTempDate.getMonth()+1 + "/" + dtmTempDate.getDate() + "/" + dtmTempDate.getYear()
	dtmCurrDate = new Date(strDateString)

	if(dtmFieldDate < dtmCurrDate){
		alert("The date you have entered is in the past.  Please change it to a present or future date.")
		strDateField.focus()
		return false
	}
	
	strDateField.value = strDate
}

/***********************************************************************************
*	FUNCTION: IsMoney
*
*	INPUT: FormField = Form textbox that is to be validated
*	OUTPUT: True or False
*
*	PURPOSE: Ensure that the value entered into the textbox contains valid dollar amount
*/
function IsMoney(FormField){
	strAlphabet = "$.,0123456789"
	
	intLength = FormField.value.length
	blnBadString=false
	
	for(i=0; i<intLength; i++){
		intIndex = strAlphabet.indexOf(FormField.value.charAt(i))
		if(intIndex == -1){
			blnBadString=true
		}
	}
	
	if(blnBadString==true){
		alert("This field must contain a valid dollar amount.")
		FormField.focus()
		return false
	}
	
	return true
}

/*******************************************************************/
function fncCheck()
{
	if(strButton == "modify")
	{
		return(true);
	}
	
	else if(strButton == "delete")
	{
		if(confirm("Are you sure you want to DELETE this record?"))
		{
			return(true);
		}

		else
		{
			return(false);
		}

	}

	else if(strButton == "cancel")
	{
		return(true);
	}


	return(true);
}
