LastFN = ""; // used by SetFullName

function SetFullName(){
 // Don't change it if user did.
 if (document.entryform.FullName.value != LastFN) {return true;}   
   
 var theFullName = document.entryform.FirstName.value;

 if (document.entryform.Title.value != "") {
  theFullName = document.entryform.Title.value 
              + " " 
              + theFullName;
 }

 if (document.entryform.MiddleName.value != "") {
  theFullName = theFullName 
              + " " 
              + document.entryform.MiddleName.value;
 }

 theFullName = theFullName  
             + " "
             + document.entryform.LastName.value;
             
 if (document.entryform.Suffix.value != "") {
  theFullName = theFullName 
              + ", " 
              + document.entryform.Suffix.value;
 }
 
 LastFN = theFullName;
 document.entryform.FullName.value = theFullName;
 return true;
}

function AskAbandon(){  // AddEdit set in EditCard.cfm
 if (!AddEdit) {
  var Mess1 = "Are you sure you want to abandon?";
  var Mess2 = "entering the information";
 }else{
  var Mess1 = "Are you sure you want to Abandon " +AddEdit+ "ing This Record?";
  var Mess2 = AddEdit + "ing the information.";
 }

 var ConfirmMess = Mess1 + "\nPress OK to Abandon.\nPress Cancel to continue " +Mess2;
 
 if ( confirm(ConfirmMess) ){
  if (AddEdit=="Add") {                       // Take user to next Comment Card as if adding, but don't save.
    document.entryform.submit();  
 
  }else{
    history.back();
  }
  return true;
  
 }
 return false;
}


function isEmptyMess(theField,theMessage){  // Customized message, returns true if field is empty
 if (theField.value =="") {
  var AlertMess = "Please enter " +theMessage;
  alert(AlertMess);
  theField.focus();
  return true;
 }
 return false;
}

function isEmpty(theField,FieldName,AddMess){  // Generic Message based on passed FieldName, returns true if field is empty
 if (theField.value =="") {
  var AlertMess = "Please enter your " +FieldName;
  alert(AlertMess);
  theField.focus();
  return true;
 }
 return false;
}

function isSelectNONE(theField) {  // returns true if "NONE" is the selected value
 if (theField.value=="NONE") {
  var theAlert = "Please Select One " + theField.name;
  alert(theAlert);
  theField.focus();
  return true; 
 }
 return false;
}
 
function BadChar(theString,theList,FirstLast){ // Allow "@_-."
                                     
 if (!theList){theList="`~!#$%^&*()+={}|[']\:;<>?,/";}

 var dq = '"'; 
 var sp = " ";
	var dd = "..";
 
 if (theString.indexOf(dq) >-1){return "Quotes are";}
 if (theString.indexOf(sp) >-1){return "Spaces are";}
 if (theString.indexOf(dd) >-1){return "Two dots next to each other are";}
	
 for (var i = 0; i < theList.length; i++) { 
  theChar = theList.charAt(i);
  if (theString.indexOf(theChar) >-1) {return theChar +" is";}
 }
 return "";
}
 
function ValidEmail(theID,isPager,Field2Stuff,Field2Check) {
 var theField  = document.getElementById(theID);
 if (!theField) {return true;}       // Old Browsers can't do this.
	var theEmail  = theField.value;

 if (theEmail == "") {return true;}

 theBadChar = BadChar(theEmail);
 if (theBadChar !=""){
  alert(theBadChar +" not allowed in an email address.");
  theField.focus();
  return false;
 }
 
	var theAt     = theEmail.indexOf("@") +1; 
 if (theEmail.indexOf("@",theAt) >=0) {
  alert("Only One At Sign [ @ ] is allowed in an email address.");
  theField.focus();
  return false;
 }
 
	var DotAfter  = theEmail.indexOf(".",theAt) +1;
	var theLen   = theEmail.length;
	
	BadFirstLast = ".@";
 if (BadFirstLast.indexOf(theEmail.charAt(0))>-1){
  alert("You cannot use  " +theEmail.charAt(0)+ "  as the first character in an email address.");
  theField.focus();
  return false;		
 }
	
	if (BadFirstLast.indexOf(theEmail.charAt(theLen -1))>-1){
  alert("You cannot use  " +theEmail.charAt(theLen -1)+ "  as the last character in an email address.");
  theField.focus();
  return false;		
 }

	if (theAt >1 && theAt+1 < DotAfter && DotAfter <= theLen -2) {
  if (Field2Stuff){                     // Only stuff if target field is blank
   sField = document.getElementById(Field2Stuff);
   if (sField.value==""){sField.value = theEmail;}
  }

  if (Field2Check){
   document.getElementById(Field2Check).checked = true;
  }
  
  return true;
 }

 if (isPager){
  var theMess = "All pager email addresses must be formatted in the same way as a regular email address.";
 }else{
  theMess = "The Email address does not appear to be properly formatted.";
 }
	alert(theMess + 
      "  All email addresses use the same format, for example, johnq@public.com.  Please check it carefully and try again.");
	theField.focus();
	return false;
}

function GetRadioValue(theRadio) {
	for (var i = 0; i <= theRadio.length -1; i++) {
		if (theRadio[i].checked) {
			return theRadio[i].value;
		}
	}
 return "NONE"; // Nothing was checked, so return NONE
}

function CheckDate(theObject,BlankMess) { // Validates Dates.  Option to require input
 var theValue = theObject.value;
 
 if (theValue.length == 0 && BlankMess){ // Send a string as 2nd parameter if input is required
  theObject.focus();
  return "Please enter a " +BlankMess;
 }
 
 if (!_CF_checkdate(theObject.value)){
  theObject.focus();  
  return theValue +" is not a valid date.  Please enter a valid date in the format mm/dd/yyyy";
 }
 return "";
} 

function _CF_checkdate(object_value)
    {
    //Returns true if value is a date format or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a date in the mm/dd/yyyy format
	isplit = object_value.indexOf('/');

	if (isplit == -1 || isplit == object_value.length)
		return false;

    sMonth = object_value.substring(0, isplit);

	if (sMonth.length == 0)
        return false;

	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length)
		return false;

    sDay = object_value.substring((sMonth.length + 1), isplit);

	if (sDay.length == 0)
        return false;

	sYear = object_value.substring(isplit + 1);

	if (!_CF_checkinteger(sMonth)) //check month
		return false;
	else
	if (!_CF_checkrange(sMonth, 1, 12)) //check month
		return false;
	else
	if (!_CF_checkinteger(sYear)) //check year
		return false;
	else
	if (!_CF_checkrange(sYear, 0, 9999)) //check year
		return false;
	else
	if (!_CF_checkinteger(sDay)) //check day
		return false;
	else
	if (!_CF_checkday(sYear, sMonth, sDay)) // check day
		return false;
	else
		return true;
    }



function _CF_checkday(checkYear, checkMonth, checkDay)
    {

	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
			checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else
	if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	return _CF_checkrange(checkDay, 1, maxDay); //check day
    }



function _CF_checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	return _CF_checknumber(object_value);
    else
	return false;
    }



function _CF_numberrange(object_value, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
	{
        if (object_value < min_value)
		return false;
	}

    // check maximum
    if (max_value != null)
	{
	if (object_value > max_value)
		return false;
	}
	
    //All tests passed, so...
    return true;
    }



function _CF_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
    }



function _CF_checkrange(object_value, min_value, max_value)
    {
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;


    if (!_CF_checknumber(object_value))
     	{
     	return false;
     	}
         else
     	{
     	return (_CF_numberrange((eval(object_value)), min_value, max_value));
     	}
	
    //All tests passed, so...
    return true;
    }
