//----------------------------------------------------------
// VALIDATION FUNCTIONS
//----------------------------------------------------------

//----------------------------------------------------------
// check if field is empty and throw error.
//----------------------------------------------------------
function isEmpty(theField, eMsg) {
   if (theField.value == "" | theField.value == null) {
      warnEmpty(theField, eMsg);
      return true;
   } else { return false; }
}

function warnEmpty(theField, eMsg) {
   alert(eMsg + " is a required field.");
   theField.focus();
}

//----------------------------------------------------------
// check if field is valid numeric and throw error.
//----------------------------------------------------------
function isNumber(theField, eMsg) {
   var strVal = theField.value;
   var oneChar = "";
   for (var i=0; i<strVal.length ; i++) {
      oneChar = strVal.charAt(i);
      if (oneChar < "0" || oneChar >"9" ) {
         warnInvalid(theField, eMsg);
         return false;
      } 
   }
   { return true; }
}

function warnInvalid(theField, eMsg) {
   alert(eMsg);
   theField.focus();
   theField.select();
}

//----------------------------------------------------------
// transform field -- reformat
//----------------------------------------------------------
function upThis(theField, eMsg) {
   if (! isEmpty(theField, eMsg)) {
      theField.value = theField.value.toUpperCase();
   }
}

function properCase(theField, eMsg) {
   var strInput = theField.value;
   nameArray = strInput.split(" ");
   var finalName = "";

   for (var i=0; i<nameArray.length; i++) {
      var strValue = nameArray[i];
      var firstTwo = strValue.substring(0,2);
      if (strValue.toLowerCase() != "von" && strValue.toLowerCase() != "mac" && firstTwo.toLowerCase() != "mc" ) {
         finalName += strValue.charAt(0).toUpperCase() 
         finalName += strValue.substring(1,strValue.length).toLowerCase()
         finalName += " ";
      //} else { finalName += strValue.toLowerCase() + " "; }
      } else { finalName = strValue + " "; }
   }
   theField.value = finalName.substring(0,finalName.length-1);
}

//----------------------------------------------------------
// FIELD-LEVEL VALIDATION
//----------------------------------------------------------

function chkUp(theField, eMsg) {
   if (! isEmpty(theField, eMsg)) {
      upThis(theField, eMsg);
      return true;
   } else { return false; }
}

function chkNum(theField, eMsg) {
   if (! isEmpty(theField, eMsg)) {
      if (isNumber(theField, eMsg)) {
         return true;
      }
   } else { return false; }
}

function maxNum(theField, intMax, eMsg) {
   var myNum = theField.value;
   var strErr = eMsg + intMax + ".";
   if (isNumber(theField, strErr)) {
      if (myNum <= intMax) {
         return true;
      } else {
         warnInvalid(theField, strErr);
         return false;
      }
   } else {
      return false;
   }
}

function aolClean(theField, eMsg) {
   if (chkUp(theField, eMsg)) {
      var strVal = theField.value;
      var intCut = strVal.indexOf("@");
      if (intCut > 1) { 
         theField.value = strVal.substring(0,intCut);
         return true;
      } else { 
         return true;
      }
   } else {
      warnInvalid(theField, eMsg);
      return false;
   }
}

function emailClean(theField, eMsg, iMsg) {
   if (chkUp(theField, eMsg)) {
      var strVal = theField.value;
      var retVal = false;
      var intAt = strVal.indexOf("@");
      var intDot = strVal.substr(intAt).indexOf(".") + intAt;
      // Make sure the address has proper form
      if ((intAt > 1) && (intDot > intAt)) {
         // Make sure there are no goofy characters in address
         if (strVal.indexOf(" ") < 0 && strVal.indexOf(",") < 0 && 
             strVal.indexOf("|") < 0 && strVal.indexOf("~") < 0 &&
             strVal.indexOf("!") < 0 && strVal.indexOf("#") < 0 &&
             strVal.indexOf("$") < 0 && strVal.indexOf("%") < 0 &&
             strVal.indexOf("^") < 0 && strVal.indexOf("&") < 0 &&
             strVal.indexOf("*") < 0 && strVal.indexOf("=") < 0 &&
             strVal.indexOf("(") < 0 && strVal.indexOf(")") < 0 &&
             strVal.indexOf("{") < 0 && strVal.indexOf("}") < 0 &&
             strVal.indexOf("[") < 0 && strVal.indexOf("]") < 0 &&
             strVal.indexOf(";") < 0 && strVal.indexOf("'") < 0 &&
             strVal.indexOf("<") < 0 && strVal.indexOf(">") < 0 &&
             strVal.indexOf("/") < 0 && strVal.indexOf("\\") < 0 &&
             strVal.indexOf(":") < 0 && strVal.indexOf("`") < 0 &&
             strVal.indexOf("@@") < 0 && strVal.indexOf("..") < 0 &&
			 strVal.indexOf("WWW.") < 0 && strVal.indexOf("-") != 0) {
				// Make sure there is not a dot at the end of the address
				if (strVal.substring(strVal.length-1) != ".") {
					retVal = true;
				}
         }
      }
      if (retVal == false) {
         warnInvalid(theField, iMsg);
      }
   }
   return retVal;
}

// Select Boxes selected
function selectIt(theField, eMsg) {
	if (theField.selectedIndex != 0) {
		return true;
	} else {
		alert("Please " + eMsg + ".");
		theField.focus();
		return false;
	}
}

// Radio Boxes selected
function radioSelect(theField, eMsg) {
	for (var i = 0; i < theField.length; i++) {
		if (theField[i].checked) {
			return true;
		}
	}
	alert ("Please " + eMsg + ".");
	return false;
}

// Field-level validation

function chkName(theField, eMsg) {
	if (! isEmpty(theField, eMsg)) {
		//properCase(theField, eMsg);
		return true;
	} else { return false; }
}

function chkUp(theField, eMsg) {
	if (! isEmpty(theField, eMsg)) {
		upThis(theField, eMsg);
		return true;
	} else { return false; }
}



function chkState(theField, eMsg, iMsg) {
	if (! isEmpty(theField, eMsg)) {
		var strField = theField.value.toUpperCase();
		if (USStates[strField] != "" && USStates[strField] != null) {
			theField.value = USStates[strField];
			return true;
		} else {
			warnInvalid(theField, iMsg);
			return false;
		}
	}
}

function chkZip(theField, eMsg, iMsg) {
	if (! isEmpty(theField, eMsg)) {
		var strInput = theField.value;
		var oneChar;
		var strFinal = "";

		for (var i=0; i<strInput.length; i++) {
			oneChar = strInput.charAt(i);
			if (oneChar >= "0" && oneChar <= "9") {
				strFinal += oneChar;
			}
		}
		switch (strFinal.length) {
			case 5:
				theField.value = strFinal;
				return true;
				break;
			case 9:
				theField.value = strFinal.substring(0,5) + "-" + strFinal.substring(5,10);
				return true;
				break;
			default:
				warnInvalid(theField, iMsg);
				return false;
				break;
		}
	} else { return false; }
}

function chkSSN(theField, eMsg, iMsg) {
   if (! isEmpty(theField, eMsg)) {
      if (isNumber(theField, iMsg)) {
      	 if (theField.value.length == 4) {
            return true;
         } else { 
   	   warnInvalid(theField, iMsg);   	  
   	   return false; }
      }
   } 
}


function ncase(theField, eMsg) {
   var strInput = theField.value;
   // clean up goofy stuff
   strInput = strInput.replace(/\s+/g, ' ');   // multiple spaces
   strInput = strInput.replace(/-\s+/g, '-');   // '- '
   var regexp = /[\'\-\s]/g;     // look for apostrophe, hyphen, or space
   nameArray = strInput.split(regexp);
   var finalName = '';
   var totalLen = 0;

   for (var i=0; i<nameArray.length; i++) {
      var strValue = nameArray[i];
      if (strValue.toLowerCase() != 'von' && strValue.toLowerCase() != 'mac') {
         finalName += strValue.charAt(0).toUpperCase()
         finalName += strValue.substring(1,strValue.length).toLowerCase()
      } else { finalName += strValue.toLowerCase(); }
      totalLen += strValue.length + 1;
      finalName += strInput.substr(totalLen-1, 1);
   }
   theField.value = finalName.substring(0,finalName.length);
}

function chkPW(pw1, pw2, isUpdate, eMsg) {
   isUpdate = isUpdate.value;
   //alert ("member_flag: " + isUpdate + "\nPW1: " + pw1.value + "\nPW2: " + pw2.value);
   if ( (! isEmpty(pw1, ePW1) && ! isEmpty(pw2, ePW2)) && (pw1.value == pw2.value) ) {
      if ( pw1.value.length >= 4 && pw1.value.length <= 15) {
         return true;
      } else {
         alert('Your password must be between 4 and 15 characters in length');
         return false;
      }
   } else {
      alert(eMsg);
      return false;
   }
}

function chkAddress(theForm) {
 if (chkName(theForm.address1, eAddress) &&
      chkName(theForm.city, eCity) &&
      selectIt(theForm.state, eState)
    ) {
      return true;
    } else {
      return false;
    }
}

function DoIt(theForm) {
   // submit the form
   if (theForm.reward[1].checked)
      {
         if (chkName(theForm.aanum, eAAnum) != true)
           {
            return false;
         } 
   }
   if (theForm.reward[0].checked)
      {
         if (emailClean(theForm.pp_email, ePPmail, iEmail) != true)
           {
            return false;
         } 
   }
   
   if (    
       chkName(theForm.fName, eFName) &&
       chkName(theForm.lName, eLName) &&
       selectIt(theForm.bMonth, ebMonth) &&
       selectIt(theForm.bDay, ebDay) &&
       selectIt(theForm.bYear, ebYear) &&
       chkAddress(theForm) &&
       chkZip(theForm.zip, eZip, iZip) &&
       emailClean(theForm.email, eEmail, iEmail) &&
       chkPW(theForm.pw1, theForm.pw2, 0, ePWmatch)  
      ) {
      theForm.submit();
   } else {
      return false;
   }
}

function DoItAOL(theForm) {
   // submit the form
      
   if (    
       emailClean(theForm.email, eEmail, iEmail) &&
       chkPW(theForm.pw1, theForm.pw2, 0, ePWmatch)  
      ) {
      theForm.submit();
   } else {
      return false;
   }
}
