function formCheck(formName, checkString) {
  //---------------------------------------------------------------------------------------
  //      Name:  formCheck()
  //   Purpose:  Return Errors if form check tests do not pass
  //     Usage:  -Ensure all HTML names and values have quotes around them.  Eg. name="city"
  //			 -To use form check use: onSubmit="JavaScript: return formCheck('nameOfForm','homePhone=true=phone')"
  //			 -Pass a string to the formCheck function containing the
  //	 	      ordered arguments: nameOfField=checkType=true=ErrorDisplayMessage
  //             -Seperate ordered arguments with an ampersand (&)
  //			 -If a field is optional then  don't include =true
  //			 -Example:
  //				Validate optional email field:
  //					email=email (will flag user only if an incorrect email entered)
  //				Validate a manditory email field:
  //					email=email=true (will flag if incorrect email or no value entered)
  //				Validate that a value has been typed into the fname field:
  //					fname=true
  // Check Type:    Description:
  // 1. true		Make sure a field has a value other than spaces
  // 2. false       Make sure a field is left blank with no spaces
  // 3. email		Validate proper email
  // 4. credit		Validate proper American Express, Visa, Master Card number
  // 5. code		Validate proper Postal Code or American Zip Code & reformat.
  // 6. image		Make sure a field contains a .jpg or .gif extention.
  // 7. phone		make sure an areacode + number is submitted and reformat.
  // 8. noIllegalChars makes sure a field does not contain illegal characters
  // 9. singleValueCheckbox Make sure a checkbox passes only a single value
  // 10. groupCheckbox make sure that in a list of checkboxes, at least n are checked.  
  //				--> n is the number passed into the javascript of checkboxes that need to be checked
  //				--> format field|field|field|field=groupCheckbox|3=field name|field name|field name 
  //
  // Other Functions:
  //    
  //---------------------------------------------------------------------------------------
  
  
  //---------------------------------------------------------------------------------------
  //Error codes to be displayed to the end user:
  //---------------------------------------------------------------------------------------
  var errorText = new Array();
  var errorTextCode;
  var errorExists = false;  
  
  //Field not filled in that should be filled in.
  	errorText[0] = "Check the following entries for omissions and errors:\n\n";
  //Email not the correct format.
  	errorText[1] = "  - Please enter a valid email address.\n";
  //Credit Card format not correct.
    errorText[2] = "  - Enter a valid credit card number.\n";
  //Uploaded file is not an image file:
    errorText[3] = "  - Ensure that the file you are uploading\n  is an image file with a .gif or .jpg extention.\n";
  //Postal/Zip Code format not correct.
    errorText[4] = "  - Enter a valid Postal/ZIP code.\n";
  //Phone number and area code not valid.
    errorText[5] = "  - Enter Area Code and Phone Number (xxx-xxx-xxxx).\n";
  //Field contains illegal characters.
    errorText[6] = "  - This field contains one of the following illegal characters:\n<  > { } [ ] ( ) * & ^ $ # @ ! ~ \\ / = + ' ` ? : ; | % .";
  //Checkbox contains more than one value
  	errorText[7] = "  - This field should only have a single value checked.\n";
  //List of checkboxes need to have at least n checked
  	errorText[8] = "  - At least ";
	errorText[9] = " of the following checkbox(es) need to be selected.\n";
  
  //---------------------------------------------------------------------------------------

  
  
  
  //Generate array or ordered commands
  var checkArray = checkString.split("&");
  var checkArrayLen = checkArray.length;
  
  //Declare variables
  var subCheckArray;
  var subCheckArrayLen;
  var groupCheckboxNumber;
  var groupCheckboxArray;
  var fieldTrue;
  var fieldFalse;
  var listOfErrorFields= "";
  listOfErrorFields = listOfErrorFields + errorText[0];
 
  

  
  
  //If no array passed then return true;
  if (checkArray[0] != "no") {
	
	//loop through array:
	for (i = 0; i < checkArrayLen; i++){

   	    //alert("There Are: " + checkArrayLen + " elements in the checkArray");		
		
		//Create a new array for the ordered arguments within each CheckArrey element.
		subCheckArray = checkArray[i].split("=");
		subCheckArrayLen = subCheckArray.length;

		//----------------------------------------------
		//  STEP 1. Check for true or false argument   -
		//----------------------------------------------
		
		//set whether field is true or false to false to start off with
		var fieldTrue = false;
		var fieldFalse = false;
		var checkFormatting = false;
		var formatVarified = false;
		var formatLocation;
		var formatType = "";
		var testString = "";
		
  //---------------------------------------------------------------------------------------
		//Loop through subArray and check if field required, and if formatting is needed.
		//Fields that are not set to true but require formatting will only be checked
		//if a value has been entered.
  //---------------------------------------------------------------------------------------
		
		//used to override the set value, since with groupCheckbox the value is NULL
		var doNotSetValue = 0;
		
		for (y = 0; y < subCheckArrayLen; y++) {
		  if (subCheckArray[y] == "true") {
		    fieldTrue = true;
		  } else if (subCheckArray[y] == "false") {
		    fieldFalse = true;
		  } else if (subCheckArray[y].indexOf("groupCheckbox") > -1) {
		  	checkFormatting = true;
  			groupCheckboxArray = subCheckArray[y].split("|");
			for (w = 0; w < groupCheckboxArray.length; w++) {
				if (w == 0) {
					formatType = groupCheckboxArray[w];
				} else {
					groupCheckboxNumber = groupCheckboxArray[w];
				}
			}
			fieldValue = "hithere";
			doNotSetValue = 1;
		  } else  if (subCheckArray[y] == "email" || subCheckArray[y] == "credit" || subCheckArray[y] == "code" || subCheckArray[y] == "date" || subCheckArray[y] == "image" || subCheckArray[y] == "phone" || subCheckArray[y] == "noIllegalChars" || subCheckArray[y] == "singleValueCheckbox") {
		  	checkFormatting = true;
			formatType = subCheckArray[y];
			//alert("Format Type: " + formatType);
		  } else {
		  	errorDisplayMsg = subCheckArray[y];
		  }
		 }

  //---------------------------------------------------------------------------------------
		  
		
		
  //---------------------------------------------------------------------------------------
  //  Get the value of the field being checked:
  //---------------------------------------------------------------------------------------
  		if (doNotSetValue == 0) {
		  var fieldValue = getFormFieldValue(formName, subCheckArray[0]);
		}
		  //alert("Form Name: " + formName + "\n Field Name: " + subCheckArray[0] + "\nValue of Field: " + fieldValue + "\nFormat Type: " + formatType);
		  
		//Determin if the form field meets true condition
		if (fieldTrue && fieldValue == "null") {
		   if (errorDisplayMsg == "") {
		  		listOfErrorFields = listOfErrorFields + "  - " + subCheckArray[0] + "\n";
			} else {
				listOfErrorFields = listOfErrorFields + "  - " + errorDisplayMsg + "\n";
			}
		   errorExists = true;
		}
		if (fieldValue != "null" && checkFormatting) {
			//-----------------------------------------------------------------------------
			//Check for various types of formatting:
			//-----------------------------------------------------------------------------
			if (formatType == "email") {
				formatVarified = varifyEmail(fieldValue);
				if (!formatVarified) {
					listOfErrorFields = listOfErrorFields + errorText[1] + "\n";
					errorExists = true;
				}
			}
			if (formatType == "credit") {
				cardType = validateCard(fieldValue);
				if (cardType == "unknownCard") {
					listOfErrorFields = listOfErrorFields + errorText[2] + "\n";
					errorExists = true;
				}
			}
			if (formatType == "code") {
				codeType = validateCode(fieldValue);
				if (codeType == "unknownCode") {
					listOfErrorFields = listOfErrorFields + errorText[4] + "\n";
					errorExists = true;
				}
				else {
				//Set the field value to the new improved formatted code
				eval("document." + formName + "." + subCheckArray[0] + ".value = codeType");
				}
			}
			if (formatType == "image") {
				if (fieldValue.indexOf(".gif") < 0 && fieldValue.indexOf(".jpg") < 0) {
					listOfErrorFields = listOfErrorFields + errorText[3] + "\n";
					errorExists = true;
				}

			}
			if (formatType == "phone") {
				//validate phone number
				phoneNumber = validatePhone(fieldValue);
				alert("The phone number returned is: " + phoneNumber);
				if (phoneNumber == "unknownNumber") {
					listOfErrorFields = listOfErrorFields + errorText[5] + "\n";
					errorExists = true;
				}
				else {
					eval("document." + formName + "." + subCheckArray[0] + ".value = phoneNumber");
				}
			}
			
			if (formatType == "noIllegalChars") {
				//validate illegal chars
				illegalCharList = "<>{}[]()*&^$#@!~\\/=+\'`?)(:;|%,.";
				for (validateStr = 0; validateStr < illegalCharList.length; validateStr++) {
					//alert(illegalCharList.charAt(validateStr));
					if (fieldValue.indexOf(illegalCharList.charAt(validateStr)) > 0) {
						 if (errorDisplayMsg == "") {
					  		listOfErrorFields = listOfErrorFields + subCheckArray[0] + "\n\n";
						} else {
							listOfErrorFields = listOfErrorFields + errorDisplayMsg + "\n\n";
						}
						listOfErrorFields = listOfErrorFields + errorText[6] + "\n";
						errorExists = true;
					}
				}
			}
			
			if (formatType == "singleValueCheckbox") {
				//ensure checkbox only has one value
				var checkCount = 0;
				var checkboxLength = eval("document." + formName + "." + subCheckArray[0] + ".length");
				//alert("checkboxLength: " + checkboxLength);
				for (cb = 0; cb < checkboxLength; cb++) {
					if (eval("document." + formName + "." + subCheckArray[0] + "[" + cb + "]" + ".checked") == true) {
						checkCount = checkCount + 1;
						//alert("checkCount :" + checkCount);
					}
				}
				if (checkCount > 1) {
					 if (errorDisplayMsg == "") {
				  		listOfErrorFields = listOfErrorFields + subCheckArray[0] + "\n\n";
					} else {
						listOfErrorFields = listOfErrorFields + errorDisplayMsg + "\n\n";
					}
					listOfErrorFields = listOfErrorFields + errorText[7] + "\n";
					errorExists = true;
				}
			}
			
			if (formatType == "groupCheckbox") {
				//ensure at least n of the listed checkboxes are checked
				var groupCheckCount = 0;
				var checkBoxArray = subCheckArray[0].split("|");
				var groupCheckBoxLength = 0;
				var errDispWithLB = "";
				//alert("checkboxLength: " + checkboxLength);
				for (chb = 0; chb < checkBoxArray.length; chb++) {
					if (eval("document." + formName + "." + checkBoxArray[chb] + ".checked") == true) {
							groupCheckCount = groupCheckCount + 1;
					}
				}
				if (groupCheckCount < groupCheckboxNumber) {
					listOfErrorFields = listOfErrorFields  + "\n\n" + errorText[8] + groupCheckboxNumber + errorText[9] + "\n\n";
					if (errorDisplayMsg == "") {			
						listOfErrorFields = listOfErrorFields + "hello";	 
				  		listOfErrorFields = listOfErrorFields + subCheckArray[0] + "\n\n";
					} else {
						var checkBoxNamesArray = errorDisplayMsg.split("|");
						
						for(chbn = 0; chbn < checkBoxNamesArray.length; chbn++) {
							//alert(checkBoxNamesArray[chbn]);
							errDispWithLB = errDispWithLB + checkBoxNamesArray[chbn] + "\n";
						}
						listOfErrorFields = listOfErrorFields + errDispWithLB + "\n\n";
					}
					errorExists = true;
				}
			}
			//-----------------------------------------------------------------------------
		}
	}
	//End loop through Array
  }
  //---------------------------------------------------------------------------------------  
  //Check to see if an error was detected, and display errors.
  //---------------------------------------------------------------------------------------
  if (errorExists) {
  	alert(listOfErrorFields);
	return false;
  }	
  return true
  //---------------------------------------------------------------------------------------
}


function validatePhone(number) {
	//Step 1 remove all non-numerical characters
	var validChr = "0123456789";
	var tempNumber = "";
	for (cn = 0; cn < number.length; cn++) {
		if (validChr.indexOf(number.charAt(cn)) > -1) {
			tempNumber = tempNumber + number.charAt(cn);
		}
	}
	number = tempNumber;
	
	//check to see if 10 digits long
	if (number.length != 10) {
		return "unknownNumber";
	}
	else {
	  number = "(" + number.substr(0,3) + ")" + " " + number.substr(3,3) + "-" + number.substr(6);
	}
	return number;	

}


function validateCode(code) {
	//Validate postal code for M9B 3V3 or 90210-2030 or 90210 format:
	//if Valid returns formatted code
	//if Not Valid returns Null
	
	codeType = "unknownCode"
	alert("code: " + code);
	//Remove all spaces and - ( ) characters added to the number field
	var newCode = "";
	for (cp = 0; cp < code.length; cp++) {
		if (code.charAt(cp) != " " && code.charAt(cp) != "-" && code.charAt(cp) != "(" && code.charAt(cp) != ")" ) {
			newCode = newCode + code.charAt(cp);
		}
	}
	code = newCode.toUpperCase();
	alert("code: " + code);
	
	var validChrCan = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var validChrUS = "01234567890";
	
	//  Validate the Canadian Postal Code
	if (validChrCan.indexOf(code.charAt(0)) >= 0) {
		codeType = "can";
		alert("Valid Can Character! " + code);
		
		//Check to see if has 6 characters:
		if (code.length != 6) {
			return "unknownCode";
		}
		var position = 0;
		for (cc = 0; cc < code.length; cc = cc + 2) {
			if (validChrCan.indexOf(code.charAt(cc)) < 0 || validChrUS.indexOf(code.charAt(cc+1)) < 0) {
				 return "unknownCode";
				 alert("position: " + position + "," + cc + "  Current Character: " + code.charAt(0));
			}
		}
		codeType = code.substring(0,3) + " " + code.substr(3);
	
	}
	
	//  Validate the American Zip Code
	if (validChrUS.indexOf(code.charAt(0)) >= 0) {
		codeType = "us";
		alert("Valid US Character! " + code );
		
		//step one check all values are numbers
		for (cc = 0; cc < code.length; cc++) {
			if (validChrUS.indexOf(code.charAt(cc)) < 0) {
			  return "unknownCode";
			}
		}
		//step 2, check the length is either 5 or 9 characters
		if (code.length != 5 && code.length != 9) {
			return "unknownCode";
		}
		
		//format code properly
		if (code.length == 9) {
			codeType = code.substring(0,5) + "-" + code.substring(5);
		}
		else {
			codeType = code;
		}
		
		
	}

	return codeType;
	
}

function validateCard(cardNumber) {
	//Validate only for Master Card, Visa, and American Express
	//This function will determin the type of credit card and return the name of the card if it is validated
	//Otherwise "unknownCard" will be returned.
	
	var cardType = "unknownCard";
	var checkType = "";
	
	//Remove all spaces added to the number field
	var newCardNumber = "";
	for (cp = 0; cp < cardNumber.length; cp++) {
		if (cardNumber.charAt(cp) != " ") {
			newCardNumber = newCardNumber + cardNumber.charAt(cp)
		}
	}
	cardNumber = newCardNumber;
	
	
	if (cardNumber.charAt(0) == "5") {
		//Proceed with validation of a Master Card Number
		cardType = "MasterCard";

		if (cardNumber.length != 16) {
			alert (cardNumber.length + "  Card Number: " + cardNumber);
			return "unknownCard";
		}
		
		//Check for valid Master Card Prefix
		prefix = cardNumber.substring(0,2);
		if (prefix != 51 && prefix != 52 && prefix != 53 && prefix != 54 && prefix != 55) {
			alert("Card Prefix: " + prefix);
			return "unknownCard";
		} 

		//Check for MOD 10 Alogorhythm: (returns true or false)
		mod10 = checkMod10(cardNumber);
		if (!mod10) {
			return "unknownCard";
		}
	}


	if (cardNumber.charAt(0) == "4") {
		//Proceed with validation of a Visa Card Number
		cardType = "Visa";
		if (cardNumber.length != 16 && cardNumber.length != 13) {
			return "unknownCard";
		}
		
		//Check for Mod 10 Alogorhythm: (regurns true of false);
		mod10 = checkMod10(cardNumber);
		if (!mod10) {
			return "unknownCard";
		}
		
		
	}


	if (cardNumber.charAt(0) == "3") {
		//Proceed with validation of an American Express Card Number
		cardType = "AmericanExpress";
		
		prefix = cardNumber.substring(0,2);
		if (prefix != 34 && prefix != 37) {
			alert("Card Prefix: " + prefix);
			return "unknownCard";
		} 
		
		//Check length is 15;
		if (cardNumber.length != 15) {
			return "unknownCard";
		}
		
		//Check for Mod 10 Alogorhythm: (regurns true of false);
		mod10 = checkMod10(cardNumber);
		if (!mod10) {
			return "unknownCard";
		}

	}
	
	//If no errors nave caused a prior RETURN then return the cardType.
	return cardType;

}


function checkMod10(cardNumber) {
	var tempString = "";
	var position = 0;
	for (cm = cardNumber.length - 1; cm > -1; cm = cm - 1) {
		if (position == 0) {
			newValue = cardNumber.charAt(cm);
			position = 1;
		}
		else {
			newValue = 2 * cardNumber.charAt(cm);
			position = 0;
		}		
  		tempString = tempString + newValue;
	}
	alert("Double Value String: " + tempString);
	//Tally all characters in the string:
	var tally = 0;
	for (cm = 0; cm < tempString.length; cm++) {
	 tally = tally + eval(tempString.charAt(cm));
	}
	
	//Check to see if the last digit in the tally is a "0" so that Mod10 will be true;
	testString = "" + tally;
	zeroPosition = testString.length - 1;
	testCharacter = testString.charAt(zeroPosition);
	
	if (testCharacter == "0") {
		return true;
	}
	return false;
}

function varifyEmail(email) {
//email must be at least 6 characters
//@ must be in the second or greater position
//a . must be eight 2 or 3 positions from the string end.
// The following characters must not be present:  / > ! # ?

	emailOk = true;
	domain1 = email.length - 3;
	domain2 = email.length - 4;
	if (email.length < 6) {
		emailOk = false;
	}
	if (email.indexOf("@") < 1) {
		emailOk = false;
	}
	if (email.charAt(domain1) != "." && email.charAt(domain2) != ".") {
		emailOk = false;
	}
	if (email.indexOf("/") >= 0) {
		emailOk = false;
	}
	if (email.indexOf("!") >= 0) {
		emailOk = false;
	}
	if (email.indexOf("?") >= 0) {
		emailOk = false;
	}
	if (email.indexOf(">") >= 0) {
		emailOk = false;
	}
	if (emailOk) {
		return true;
	}
	else {
	 	return false;
	}
}


function getFormFieldValue(formName, fieldName) {
 //set formValue variable to either true, false, or some string value
 var formValue = "null";
 var formElementType = getElementType(formName, fieldName);

 
 //Get the number of elements in the form
 var numOfElements = eval("document." + formName + ".elements.length");
 //alert("Elements in Form: " + numOfElements)
 
 //Depending on element type check to see what the value is.
 if (formElementType == "text" || formElementType == "password" || formElementType == "file" || formElementType =="textarea") {
   formValue = eval("document." + formName + "." + fieldName + ".value");
   
   
   
   //alert("Form Name: " + formName + "\nField Name: " + fieldName + "\nForm Value: " + formValue);
   //determin if 0 length string
   //determin if longer string is only 'space characters'
   if (formValue.length == 0) {
     formValue = "null";
    }
	if (formValue.length >= 1) {
	 isBlankEntry = isBlankSpace(formValue);
	 if (isBlankEntry) {
	   formValue = "null";
	 }
	}
 }
 if (formElementType.indexOf("select") >= 0) {
 	isSelected = eval("document." + formName + "." + fieldName + ".selectedIndex");
      if (isSelected <= 0) {
	  formValue = "null";
	}
	else {
    	  formValue = eval("document." + formName + "." + fieldName + ".options[" + isSelected + "].value")
	}
	//If an element has been selected then loop through the form elements and get the value


 }
 if (formElementType == "radio") {
	//alert(fieldName + " Is a Radio Button " + numOfElements);
      //loop through form elements where name = formName and see if any are checked.
      for (a=0; a < numOfElements; a++) {
	  var elementName = eval("document." + formName + ".elements[" + a + "].name");
	  if (elementName == fieldName) {
	    //alert("Element Name: " + elementName);
	    if (eval("document." + formName + ".elements[" + a + "].checked")) {
		formValue = eval("document." + formName + ".elements[" + a + "].value")
	    }
        }
	}
 }
 if (formElementType == "checkbox") {
	//alert(fieldName + " Is a CheckBox " + numOfElements);
	//Loop through elements and if none checked return "null"
	//Otherwise return a comma delineated list of elements checked.
	formValue = "";
	var numChecked = 0;

      for (a=0; a < numOfElements; a++) {
	  var elementName = eval("document." + formName + ".elements[" + a + "].name");
	  if (elementName == fieldName) {
	    //alert("Element Name: " + elementName);
	    if (eval("document." + formName + ".elements[" + a + "].checked")) {
		formValue = formValue + eval("document." + formName + ".elements[" + a + "].value") + ","
		numChecked++;
	    }
        }
	}
	if (numChecked == 0) {
	  formValue = "null";
	}
	else {
	  var formValueChopAt = formValue.length - 1;
	  formValue = formValue.substring(0,formValueChopAt);
	}
 }

//Complete if there is a reason for testing the value of the submit button 
 if (formElementType == "submit") {
 }

  return formValue
}


function getElementType(formName, fieldName) {
 //alert("Getting Element Type for: " + fieldName)
 //loop through elements and get the number of the element with thie formName
 var formElementLen = eval("document." + formName + ".elements.length");
 var z = 0;

 for (u = 0; u < formElementLen; u++) {
   elementName = eval("document." + formName + ".elements[" + u + "].name");
   if (elementName == fieldName) {
     z = u
   }
 }
 elementType = eval("document." + formName + ".elements[" + z + "].type");
 
 return elementType
}



function isBlankSpace(formValue) {
	for (a=0; a < formValue.length; a++) {
	  var c = formValue.charAt(a);
	  //alert("Character(" + a + ") >" + c + "<");
	  if ((c != ' ') && (c != '\n') && (c != '/t')) return false;
	}
	return true;
}