/*-----------------------------------------------------------------------
MDF JavaScript Repository

All JavaScripts created by Aaron Gustafson(agustafson@cronin-co.com) on 
1-3-03 unless otherwise noted. Please list revisions.

REVISIONS:
Revision By				Date			Modifications
A Gustafson             4-2             checkFields added
-----------------------------------------------------------------------*/


<!-- 
// ********** BEGIN VALIDATION FUNCTIONS ***********
	// Format the error message. 
var errorLine1		= "Before you continue, please fix the fields listed below."; 
var errorLine2		= "_________________________________________________       "; 
var errorLine3		= "Form validation errors:"; 
var errorBullet		= "   ·   "; 
 
	// Define the valid characters. Used by checkValidChars(). 
var validChars		= "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
 
	// Define the non-sequential order. Used by checkIdentical(). 
var numSeqStr		= 6; 
var strSeq			= new Array(); 
strSeq[1]			= "abcdefghijklmnopqrstuvwxyz"; 
strSeq[2]			= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
strSeq[3]			= "zyxwvutsrqponmlkjihgfedcba"; 
strSeq[4]			= "ZYXWVUTSRQPONMLKJIHGFEDCBA"; 
strSeq[5]			= "0123456789"; 
strSeq[6]			= "9876543210"; 
 
	// Define (required) fields and messages 
var numChecks		= 9; 
var checkFields		= new Array(); 
 
 
	// Begin validation 
function defineFields() { 
 
		// isRequired:			field_name, field_label, error_label 
		// checkValidChars:		field_name, field_label, error_label 
		// validateMinMax:		field_name, field_label, min_lenght, max_lenght 
		// checkIdentical:		field_name, field_label, error_label 
		// checkSequential:		field_name, field_label, error_label 
		// validateNumBox:		field_name, field_label, error_label 
		// validateEmailBox:	field_name, field_label, error_label (if any, it becomes a 
		// 						general label, if not - a separate label is built for each error) 
		// validateSelectBox:	field_name, field_label, error_label, non_valid_options_value 
		// validateBtnsBox:		field_name, field_label, error_label 
 
	checkFields[1]	= isRequired( "lastName", "Last Name", "Please enter your last name." ); 
	checkFields[2]	= isRequired( "firstName", "First Name", "Please enter your first name." ); 
	checkFields[3]	= isRequired( "company", "Company", "Please enter your company\'s name." ); 
	checkFields[4]	= isRequired( "address1", "Address", "Please enter your company\'s address." ); 
	checkFields[5]	= isRequired( "city", "City", "Please enter your company\'s city." ); 
	checkFields[6]	= isRequired( "state", "State/Prov", "Please enter your company\'s state." ); 
	checkFields[7]	= isRequired( "zip", "Post Code", "Please enter your company\'s zip/postal code." ); 
	checkFields[8]	= isRequired( "country", "Country", "Please enter your company\'s country." ); 
	checkFields[9]	= validateEmailBox( "emailAddress", "Email", "" ); 
 
	runValidation(); 
} 

function runValidation() {

		// Add the error messages, if any
	var obj_Error	= new ErrorMsg();
	obj_Error.AddCategory();
	for ( var i = 1; i < ( numChecks + 1 ); i++ )  {
		if ( checkFields[i] != "" ) obj_Error.AddMessage( checkFields[i] );
	}

		// Display errors
	if( obj_Error.errors == false ) {
		document.forms[0].submit();
	} else {
		obj_Error.DisplayMessage();
		return false;
	}
}

	// ===== Check if field is required =====
function isRequired ( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	if(( fieldValue == "" ) || ( fieldValue == " " )) {
		errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel + "";
	}
	return errorMsg ;
}

	// ===== Validate field minimum and maximum length =====
function validateMinMax ( fieldName, fieldDescription, minLength, maxLength ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;
	if ( fieldLength < minLength ) {
		errorMsg = "\"" + fieldDescription +  "\"  - Please enter at least " + minLength + " characters.";
	} else if (( fieldLength > maxLength ) && ( maxLength > 0 )) {
		errorMsg = "\"" + fieldDescription +  "\"  - Please enter less than " + maxLength + " characters.";
	}
	return errorMsg ;
}

	// ===== Check if netry contains only valid characters =====
function checkValidChars( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;
	for( var i=0; i<fieldLength; i++ ) {
		if ( validChars.indexOf( fieldValue.charAt( i )) == -1 ) {
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

	// ===== Check if entry contains entirely identical characters =====
function checkIdentical( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;

	if (( fieldLength > 1 ) && ( isComposedOfChars( fieldValue.charAt( 0 ), fieldValue ))) {
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg ;
}

function isComposedOfChars( curChar, inString ) {
	return ( indexOfFirstNotIn( curChar, inString ) == -1 );
}

function indexOfFirstNotIn( okayChars, inString ) {
	for ( var i=0; i<inString.length; i++ ) {
		if ( okayChars.indexOf( inString.charAt( i )) == -1 ) {
			return i;
		}
	}
	return -1;
}

	// ===== Check if entry contains entirely sequential characters =====
function checkSequential( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;

	if ( fieldLength > 1 ) {
		for ( var i = 1; i < ( numSeqStr + 1 ); i++ )  {
			if ( strSeq[i].indexOf( fieldValue ) != -1 ) {
				errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
			}
		}
	}
	return errorMsg ;
}

	// ===== Check if an option has been selected =====
function validateSelectBox( fieldName, fieldDescription, errorLabel, checkWhat ) {
	errorMsg	= "";
	optSelected	= document.forms[0][fieldName].selectedIndex;
	fieldValue	= document.forms[0][fieldName].options[optSelected].value;
	if( fieldValue == checkWhat ) {
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg ;
}

	// ===== Check if a checkbox or radio button has been selected =====
function validateBtnsBox( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	selection = null;
	thisButton		= document.forms[0][fieldName];
	for( var i=0; i<thisButton.length; i++ ) {
		if( thisButton[i].checked ) {
		   selection = thisButton[i].value;
		}
	}
	if( selection == null ) {
		errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
	}
	return errorMsg;
}

	// ===== Check if entry contains only numbers =====
function validateNumBox( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;

	for( var i = 0; i < fieldLength; i++ ) {
		var ch = fieldValue.substring( i, i + 1 );
		if (( ch < "0" ) || ( ch > "9" )) {
			errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
		}
	}
	return errorMsg ;
}

	// ===== Validate an email address =====
function validateEmailBox ( fieldName, fieldDescription, errorLabel ) {
	errorMsg	= "";
	fieldValue	= document.forms[0][fieldName].value;
	fieldLength	= document.forms[0][fieldName].value.length;

	if ( fieldLength > 0 ) {

		if (( errorLabel == "" ) || ( errorLabel == null )) {

			errorLabel1	= "This is not a valid email address.";			// Not valid
			errorLabel2	= "Missing [ @ ] sign.";						// Missing "@"
			errorLabel3	= "Missing [ . ].";								// Missing "."
			errorLabel4	= "Cannot start with a space.";					// Starts with " "
			errorLabel5	= "Cannot start with [ @ ] sign.";				// Starts with "@"
			errorLabel6	= "Cannot start with [ . ].";					// Starts with "."

			if ( fieldValue.indexOf( "@" ) == -1 ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel2 + "";
			} else if ( fieldValue.indexOf( "." ) == -1 ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel3 + "";
			} else if ( fieldValue.charAt( 0 ) == " " ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel4 + "";
			} else if ( fieldValue.charAt( 0 ) == "@" ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel5 + "";
			} else if ( fieldValue.charAt( 0 ) == "." ) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel6 + "";
			}

		} else {

			if (( fieldValue.indexOf( "@" ) == -1 ) || 					// Missing "@"
				( fieldValue.indexOf( "." ) == -1 ) || 					// Missing "."
				( fieldValue.charAt( 0 ) == "@" ) || 					// Starts with "@"
				( fieldValue.charAt( 0 ) == "." ) 						// Starts with "."
				) {
				errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel1 + "";
			}
		}
	} else {
      errorMsg = "\"" + fieldDescription +  "\"  -  Please enter your email address.";
    }
	return errorMsg ;
}
// *********** END VALIDATION FUNCTIONS ************


// ************ BEGIN OBJECT FUNCTIONS *************
function AddCategory( str_Name ) {
		// Creates a new category object at the next space in the array
	this.obj_Errors[this.obj_Errors.length] = new NewCategory( str_Name );
	this.NewCategory = NewCategory;
}

function NewCategory( str_Name) {
		//  Initializes the category values
	this.str_Name		= str_Name;
	this.str_Error		= "";
}

function AddMessage( str_Msg ) {
	for ( var i = 0; i < this.obj_Errors.length; i++ )  {
		this.obj_Errors[i].str_Error += errorBullet;
		this.obj_Errors[i].str_Error += str_Msg + "\n";
		this.errors = true;
		return true;
	}
}

function ErrorMsg() {
		//  This is the object you create to keep track of errors in the document
	this.obj_Errors		= new Array();
	this.errors			= false;
	this.AddCategory	= AddCategory;
	this.AddMessage		= AddMessage;
	this.DisplayMessage	= DisplayMessage;
}
// ************* END OBJECT FUNCTIONS **************


function DisplayMessage() {
		// Displays the error messages.
		// If none, false is returned and no message is displayed.
	if ( this.errors == false ) {
		return false;
	} else {
		var str_msg = "";
		str_msg += errorLine1 + "\n";
		str_msg += errorLine2 + "\n";
		for ( var i = 0; i < this.obj_Errors.length; i++ ) {			// Go through all of the objects
			if ( this.obj_Errors[i].str_Error != '' ) {					// If errors, write the errors
				str_msg += "\n" + errorLine3 + "\n";
				str_msg += this.obj_Errors[i].str_Error + "\n";
			}
		}
		alert( str_msg );												// Display the error message
		return true;
	}
}


// showHide
// added 1-3-03
// used to show/hide layers/spans
function showHide(show,hide){
  if (show) {
    var showArray = show.split(",")
    var s = showArray.length
    for(i = 0; i < s; i++) {
  	  var imShowing = showArray[i];
	  showMe(imShowing);
    }
  }
  if (hide) {
    var hideArray = hide.split(",")
    var h = hideArray.length
    for(i = 0; i < h; i++) {
  	  var imHiding = hideArray[i];
	  hideMe(imHiding);
    }
  }
}
// showMe
// added 1-3-03
// part of showHide
function showMe(what) {
  what = findObj(what);
  what.style.display='block';
}
// hideMe
// added 1-3-03
// part of showHide
function hideMe(what) {
  what = findObj(what);
  what.style.display='none';
}

// subMenu
// added 1-3-03
// for navigation
function subMenu(what) {
  what = findObj(what);
  if (what.style.display=='none') {
    what.style.display='block';
  } else if (what.style.display=='block') {
    what.style.display='none';
  }
}

/*--------- Utility Functions -----------*/
// findObj
// added 1-3-03
// used to find objects in IE & NS
// written by Macromedia
// cleaned up by Aaron Gustafson
function findObj(n,d) {
  var p,i,x;
  if(!d) d = document;
  if((p=n.indexOf("?")) > 0 && parent.frames.length) {
	d = parent.frames[n.substring(p+1)].document;
	n = n.substring(0,p);
  }
  if(!(x=d[n]) && d.all)
	x = d.all[n];
  for(i=0; !x && i < d.forms.length; i++)
	x = d.forms[i][n];
  for(i=0; !x && d.layers && i < d.layers.length; i++)
    x = findObj(n,d.layers[i].document);
  if(!x && d.getElementById)
	x = d.getElementById(n);
  return x;
}

// -->
