var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var num = "0123456789"
var alphanum = alpha + num

function isAtLeast5Char(str,FieldStr)
{
	var nCheck = str.value
	var ln_length = nCheck.length
	var i = 0
	var chr_found = false	
	while ((i < ln_length) && (!chr_found)){
		if ((nCheck.charAt(i) == " ")){
			chr_found = true
		    str.focus();
		    str.select();
		    alert("Spaces are not permitted in the "+FieldStr+". Please enter the "+FieldStr+" again.");
			return true;
		}
		i++
	}
  strValue = trim(str.value);
  if (strValue.length < 5)
  {
    str.focus();
    str.select();
    alert("Your "+FieldStr+" should be at least five characters long.");
	return true;
  }
  else if (strValue.length > 15)
  {
    str.focus();
    str.select();
    alert("Your "+FieldStr+" should be at most fifteen characters long.");
	return true;
  }
  else 
    return false
}

// ***********************************************************
// Function: isBlank
// Original Programmer: Ali Khan
// Date Created:        April 1, 1999
//
// Description: Checks if the string is blank and alert the user and 
//				focus and select the field

// Arguments:
//	input:  field to test
//			name of the field in a readable format. 
//				Example 'User Name' instead of UserName

// Return Value: "True" if str is blank. Just spaces is blank too.
//				"False" otherwise.

// Modification History:
// ***********************************************************
function isBlank(str,FieldStr)
{
	var nCheck = str.value
	var ln_length = nCheck.length
	var i = 0
	var chr_found = false	
	while ((i < ln_length) && (!chr_found)){
		if ((nCheck.charAt(i) != " "))
			chr_found = true
		i++
	}
	if (!chr_found){
		str.focus();
		str.select();		
		alert(FieldStr+" is a required field.  Please enter "+FieldStr+".");
		return true
	}
	return false
}

// ***********************************************************
// Function: isNotValidStr
// Original Programmer: Ali Khan
// Date Created:        April 1, 1999
//
// Description: Checks if the string is a subset of cmpStr, otherwise 
//				focus and select the field and alert the user.

// Arguments:
//	input:  str -> field to test
//			FieldStr -> name of the field in a readable format. 
//				Example 'User Name' instead of UserName

// Return Value: "True" if str is not a subset of cmpStr.
//				"False" otherwise.

// Modification History:
// ***********************************************************
function isNotValidStr(str,cmpStr,FieldStr)
{
	var nCheck = trim(str.value)
	var ln_length = nCheck.length
	var i = 0
	var didNotLike = ""
	var not_allowed = true
	cmp=cmpStr
	while ((i < ln_length) && (not_allowed)){
		if ((cmp.indexOf(nCheck.charAt(i))<0)){
			didNotLike = didNotLike + nCheck.charAt(i)
			not_allowed = false
		}
		i++
	}
	if (!not_allowed){
		str.focus();
		str.select();
		if (didNotLike == " ")
			alert("Space is an invalid character. Please enter "+FieldStr+" again.");
		else if (didNotLike == "'")
			alert("Single quote is an invalid character. Please enter "+FieldStr+" again.");
		else
			alert("Invalid character '"+didNotLike+"' found.  Please enter "+FieldStr+" again.");
	}
	return !not_allowed
}

// ***********************************************************
// Function: RequiredChr
// Original Programmer: Ali Khan
// Date Created:        April 1, 1999
//
// Description: Checks if E-Mail has characters in cmpStr otherwise 
//				focus and select the field and alert the user.

// Arguments:
//	input:  str -> field to test
//			FieldStr -> name of the field in a readable format. 
//				Example 'E-Mail' instead of EMail

// Return Value: "True" if all char in cmpStr are not in E-Mail
//				"False" otherwise.

// Modification History:
// ***********************************************************
function RequiredChr(str,cmpStr,FieldStr)
{
	var cmp=cmpStr
	var nCheck = str.value
	var ln_length = cmp.length
	var i = 0
	var Present = true
	while ((i < ln_length) && (Present)){
		if ((nCheck.indexOf(cmp.charAt(i))<0))
			Present = false
		i++
	}
	if (!Present){
		str.focus();
		str.select();
		alert("A valid "+FieldStr+" address was not entered.");
	}
	return !Present
}
// ***********************************************************
// Function: isCardValid
// Original Programmer: Ali Khan
// Date Created:        April 1, 1999
//
// Description: Checks the validity of a Credit Card Number

// Arguments:
//	CCN - Credit Card Number passed in.

// Return Value: "True" if valid
//				"False" otherwise.

// Modification History:
// ***********************************************************
function isCardValid(CCN)
{
  var MASK = "21"
  var CheckSum = 0
  var i, Product

//	'Make Card Length at least 16 digits
  while (CCN.length < 16)
	CCN = "0" + CCN;

  //'Calculate the Check Sum
  for (i=0;i<CCN.length;i++)
  {
	dCCN = CCN.charAt(i);
	dMASK = MASK.charAt(i%2);
    Product = dCCN * dMASK;
    if (Product > 9)
      Product = Product - 9;
    CheckSum = CheckSum + Product;
  }
  sCheckSum = CheckSum.toString()
//	Check If Valid Checksum
  ln_length =  sCheckSum.length
  if (sCheckSum.charAt(ln_length - 1) == "0")
    return true  //card is valid
  else
    return false  //card is not valid
}


// ***********************************************************
// Function: isCardExpired
// Original Programmer: Ali Khan
// Date Created:        April 1, 1999

// Description: Checks that the card hasn't expired

//	Arguments:	aobjMonth - form element that corresponds to a month drop down list values 1-12
//					aobjYear - form element that corresponds to a four digit year drop down list

// Return Value: "True" if expired
//				"False" otherwise.
//
// Modification History:
// ***********************************************************
function isCardExpired(aobjMonth, aobjYear)
{
	var CurrentDate, CurrentYear, CurrentMonth, CardExpired
	var CCMonth = aobjMonth.options[aobjMonth.selectedIndex].value
	var CCYear = aobjYear.options[aobjYear.selectedIndex].value
	vers=navigator.appVersion;              //Browser Version

	CurrentDate = new Date();
	CurrentMonth = CurrentDate.getMonth() + 1;

	// Netscape 3.x does not support Date's getFullYear() method. So we had to capture the 
	// date in a string and get the year. Ali Khan 8/6/1999 4:33pm
	if (vers.indexOf("3")>=0) // IE 3.x or Netscape 3.x
		{
		strDate = CurrentDate.toString();
		CurrentYear = strDate.substring(strDate.length - 4);
		}
	else
		CurrentYear = CurrentDate.getFullYear();
		
	if (CCYear < CurrentYear)
		CardExpired = true
	else if ((CCMonth < CurrentMonth) && (CCYear == CurrentYear))
		CardExpired = true
	else
		CardExpired = false
	if (CardExpired)
	{
	aobjMonth.focus();
		alert("Your credit card has expired");
	}
	return CardExpired
}


function trim(str)
{
	var ln_length = str.length
	var i = 0
	var chr_found = false	
	while ((i < ln_length) && (!chr_found)){
		if ((str.charAt(i) != " "))
			chr_found = true
		i++
	}

	if (chr_found){
		while(str.indexOf(" ") == 0)
	  		str = str.substring(1);
		while(str.lastIndexOf(" ") == str.length - 1)
			str = str.substring(0, str.length - 1);
		return str;
	}
	return ""
}




function validmail(obj)
{
	disallowed="!\"£$%^&*()+={}[]:;\'#<>,?/|\\ ";
	if (obj.indexOf('@')<1)
	{
		alert("Please enter an appropriately formatted e-mail address.")
		return true;
	}
	if (obj.indexOf('.')==0)
	{
		alert("Please enter an appropriately formatted e-mail address.")
		return true;
	}
	for (var i=0;i<(disallowed.length -1);i++)
	{
		if (obj.indexOf(disallowed.substring(i,i+1))!=-1)
		{
			alert("Illegal character: '" + disallowed.substring(i,i+1) + "' found in e-mail address. Please try again.");
			return true;
		}
	}
	objcopy=obj.substring(obj.indexOf('@')+1,obj.length);
	if (objcopy.indexOf('@')!=-1)
	{
		alert("E-mail addresses cannot have two '@' symbols. Please try again.");
		return true;
	}
	if (objcopy.indexOf(".")<1)
	{
		alert("Please enter an appropriately formatted e-mail address.")
		return true;
	}
	if (objcopy.lastIndexOf(".")+1==objcopy.length)
	{
		alert("E-mail addresses cannot end with a period. Please try again.");
		return true;
	}
	objcopy=obj;
	while (objcopy.indexOf('.')!=-1)
	{
		objcopy=objcopy.substring(objcopy.indexOf('.')+1,objcopy.length);
		if (objcopy.indexOf('.')==0)
		{ 
			alert("E-mail addresses cannot have two consecutive periods. Please try again.");
			return true;
		}
	}
	return false;
}

function CheckOutDate(FieldDay, FieldMonth, FieldYear)
{
	if(FieldDay.value != "" || FieldYear.value != "") 
	{
		strdate = FieldMonth.options[FieldMonth.selectedIndex].value +"/"+ FieldDay.value + "/" + FieldYear.value
		if ( !isValidDate(strdate, "Birth Date", 1) )
			{
			FieldDay.focus();
			return false;
			}

		if(FieldYear.value < "1753")
			{
			alert("The birth year can not be earlier than the year 1753");
			FieldYear.focus();
			FieldYear.select();
			return false;
			}
	}
	return true;
}
