//===============================================================
// validate.js
//
// Author:  Malia Miller, BNA Inc.
//
// Description:
// JavaScript Function Library for use by BNA email registration
// applications. Requires FormChek.js, another JavaScript 
// Function Library with open source.
//================================================================




//=========================================
// Trim white space from strings
//=========================================
function trim(str){ 
                  newstr=chr=pchr=""; begin=1; len=str.length; 
                  for (var i=0; i<len; i++){
                  chr=str.substring(i,i+1); 
                  if (chr != " "){newstr=newstr+pchr+chr; pchr=""; begin=0}else{
                  if (begin==1 || i-1== len){pchr=""}else{pchr=" "} 
                  } 
                  }
                  return newstr
                  } 

//=========================================
// Text Box Validation 
//=========================================
function ValTextBox(theField,theMsg)
{
var theResult2 = true;
var theValue2 = stripInitialWhitespace (theField.value);
if (isWhitespace (theValue2))
{
alert(theMsg);
theResult2 = false;
theField.focus();
theField.select();
}
return theResult2;
}

//=========================================
// List Box Validation 
//=========================================
function ValListBox(theField,theMsg) {
   var theResult7 = true;
   theValue = "";
   for (i=0;i < theField.options.length;i++) {
     if( theField.options[i].selected ) theValue += theField.options[i].text
   }
   if ( (isWhitespace(theValue)) || (theValue == "select...") || (theValue == "Select...")) {
       alert(theMsg);
       theResult7 = false;
       theField.focus();
   }
   return theResult7;
}

//=========================================
// Email Validation 
//=========================================
function ValEmail(theField,theMsg)
{
var theResult3 = true;
var theValue3 = stripWhitespace(theField.value);
theValue3 = stripCharsInBag (theValue3, "*")
if (isEmail (theValue3)) 
{
return theResult3;
}
alert(theMsg);
theResult3 = false;
theField.focus();
theField.select();
}

//=========================================
// Validate function triggers specified validationsfor Register Form.
//=========================================
function validate()
{
var AResult = true;

AResult = ValEmail(document.forms[0].email,"Please enter a valid email address.") 
 ;

if (AResult== true)
{
document.forms[0].submit();
}
return AResult;
}

//=========================================
// ValidateCancel function triggers specified validations for Cancel Form.
//=========================================
function validateCancel()
{
var AResult = true;

AResult = ValListBox(document.forms[0].ProductTitle,"Please select an email product.")  
&& ValEmail(document.forms[0].email,"Please enter a valid email address. This must match the address the email is currently being sent to.") 
 ;

if (AResult== true)
{
document.forms[0].submit();
}
return AResult;
}


//=========================================
// ValidateChangeEmail function triggers specified validations for EmailChange Form.
//=========================================
function validateChangeEmail()
{
var AResult = true;

AResult = ValListBox(document.forms[0].ProductTitle,"Please select an email product.")  
&& ValTextBox(document.forms[0].FName,"Please enter your first name.") 
&& ValTextBox(document.forms[0].LName,"Please enter your last name.") 
&& ValEmail(document.forms[0].email,"Please enter a valid email address. This must match the address the email is currently being sent to.") 
&& ValEmail(document.forms[0].emailnew,"Please enter a valid email address.") 
 ;

if (AResult== true)
{
document.forms[0].submit();
}
return AResult;
}

