// ******************************************
// **++++++++++++++++++++++++++++++++++++++++
// **++ Universal Validation Functions ++++++
// **++++++++++++++++++++++++++++++++++++++++

// ** LAST UPDATE: 05/29/03

// *********************
// How to use:
// ----------

// This JS file holds universal validation functions for a variety of form field types, including
// Phone number, postal code, required text field, required pulldown (list) field and required radio boxes.

// If any additions are made, please place a copy of the file in 111New Project/HTML/scripts

// Top use the functions, place this javascript file as an include in your form page.

// Place the following script into the form file, and remove the '//' tags, 
// except where the line is noted as a 'COMMENT'

// function validate()
// {
//
// var errormessage = ""
// COMMENT: starts with a blank error message and creates any messages based on above functions

// errormessage=errormessage+ValRequired("FirstName","First Name")
// errormessage=errormessage+ValRequired("LastName","Last Name")
// errormessage=errormessage+ValRequired("Address1","Address 1")
// errormessage=errormessage+ValRequired("City","City")
// errormessage=errormessage+ValRequiredList("ProfessionalQualifications","Professional Qualifications")

// if (errormessage != "") {alert(errormessage)} else {document.forms[1].submit();}
// }

// The error message builds upon itself based on the fields you ask it to check. In most cases (check this file to get a more detailed understanding), you simply call the individual function (ie errormessage=errormessage+ValRequired("FirstName","First Name") ) and include the form name as the first variable and the title of the field you would like the error message to use. 

// You may need to make some adjustments to this file if, for example, the form you plan on adjusting 
// is not the first on the page, or the messages aren't appropriate for your use. If you make any 
// changes (not including additions that don't change existing code) DO NOT replace the file 
// found 111New Project/HTML/scripts. If you feel your changes could make life easier for others, 
// place the file in the New Project folder with a new, 
// descriptive name (ie validtion_for_calculators.js).


// ******************************
// AND NOW THE CODE +++++++++++++


// ValPhone
function ValPhone(phonevar,phonename)
{

var finalfield = ""
function ValNumber(fieldnumber) {
// checks to see if the fieldnumber variable is any of the following characters
if ((fieldnumber != " ") && (fieldnumber != "(") && (fieldnumber != ")") && (fieldnumber != "-") && (fieldnumber >= "0" && fieldnumber <= "9")) {
// using the loop further on, each individual character is put back together without the formatting characters
finalfield = finalfield + fieldnumber
}
}

var field = document.forms[1][phonevar].value;
// loops through the field, and breaks it into individual characters for above function
for (var i = 1; i <= document.forms[1][phonevar].value.length; i++) {
var j = i - 1;
ValNumber(field.substring(j,i));
}

// since phone is not a required field, allows for empty strings
if (document.forms[1][phonevar].value != "") {
// checks to see that the raw field after formatting is removed is ten digits
if (finalfield.length != 7) {
return("Your " + phonename + " must be a ten digit phone number.\n");
} 
else
{
// concatenates the field into a standard format for the database - (111)111-1111
document.forms[1][phonevar].value = "(" + finalfield.substring(0,1) + finalfield.substring(1,2) + finalfield.substring(2,3) + ")" + finalfield.substring(3,4) + finalfield.substring(4,5) + finalfield.substring(5,6) + "-" + finalfield.substring(6,7) + finalfield.substring(7,8) + finalfield.substring(8,9) + finalfield.substring(9,10);
return("");
}
}
else
{
return("");
}
}

// ValRequired
function ValRequired(requiredvar,requiredvarname)
{
// confirms that variable exists
  if (document.forms[1][requiredvar].value != "") {
 return("")
 } else {
 return(requiredvarname + " is a required field\n") }
} 

// ValRequiredList
function ValRequiredList(requiredvar,requiredvarname)
{
// confirms that variable exists
  if (document.forms[1][requiredvar].selectedIndex != 0) {
 return("")
 } else {
 return(requiredvarname + " is a required field\n") }
} 

// ValPostalCode
function ValPostalCode(postalcodevar,postalcodename)
{

// confirms that a postal code entry has been entered 
  var postalcode = document.forms[1][postalcodevar].value;
  if (postalcode == "") {
 return(postalcodename + " is a required field\n")
}
  if (postalcode != "") {

// starts breaking the string into individual character positions
  var positionOne = postalcode.substring(0,1);
  var positionTwo = postalcode.substring(1,2);
  var positionThree = postalcode.substring(2,3);
  var positionFour = postalcode.substring(3,4);

// accounts for common methods of formatting postal codes
  if (positionFour == "-" || positionFour == " ") {
    var positionFour = postalcode.substring(4,5);
    var positionFive = postalcode.substring(5,6);
    var positionSix = postalcode.substring(6,7);
  }
  else
  {
    var positionFive = postalcode.substring(4,5);
    var positionSix = postalcode.substring(5,6);
  }

// sets default status for error message (negative)
  var message = "Off";

// function is created to check for letter value
  function ValLetter(letter) {
  if (letter >= "0" && letter <= "9") {
// sets positive status for error message if any of above conditions are met
    message = "On";
  }
  }

// calls letter check function on relevant positions of postal code  
  ValLetter(positionOne);
  ValLetter(positionThree);
  ValLetter(positionFive);

// function is created to check for number value
  function ValNumber(number) {
  if ((number >= "a" && number <= "z") || (number >= "A" && number <= "Z")) {
// sets positive status for error message if any of above conditions met   
   message = "On";
  }
  }

// calls numeric check function on relevant positions of postal code  
  ValNumber(positionTwo);
  ValNumber(positionFour);
  ValNumber(positionSix);

// sends error message if status is positive
  if (message == "On") {
    return postalcodename + " must be a valid format K1K 1K1\n"
  }
  else
  {
// else sets postalcode value to proper format for database input  
  document.forms[1][postalcodevar].value = positionOne + positionTwo + positionThree + " " + positionFour + positionFive + positionSix;
    return ""
  }

}
}
 


// Radio button validator

function ValRequiredRadio(FieldName,FieldTitle,Alternative) {

FieldName = document.forms[1][FieldName]

// If the you want to use a custom message, type 'yes' as a third variable when calling the function
if (Alternative == "yes")  {
	RadioMessage = FieldTitle
	}
	else
	{
	RadioMessage = FieldTitle + " is a required field.\n";
}


//alert(FieldName);

// For later comparison of total available radio choices versus total empty choices
FalseCount = 0


// Loops through each and counts how many empty boxes exist
for(var i = 0 ; i < FieldName.length ; ++i) {

 if (FieldName[i].checked == false) {
FalseCount = FalseCount + 1
 }
   }

//alert("i=" + i + ", FalseCount =" + FalseCount);   
   
if (FalseCount == i) {
  return RadioMessage
  }
  else
  {
  return "";
  }
  
}  
