function isEmail(fld, name) {
	if (fld.value.length) {
		var isEmail = ((fld.value.indexOf("@") != -1) && (fld.value.indexOf(".") != -1));
		if (!isEmail) {
			alert(name+' does not appear to be a valid format.  E-mail addresses must by in the xxx@xxx.com format.');
			fld.focus();
			fld.select();
			return false;
		}
	}
	
	return true;
}

function isNumeric(fld, name) {
	if (fld.value.length) {
		if (isNaN(fld.value)) { 
			alert(name+' must be a number.  Please do not include commas or non-numeric characters in your submission.');
			fld.focus();
			fld.select();
			return false;
		}
	}
	
	return true;
}

function isMoney(fld, name, xmatch) {
	// strip all the currency stuff
	money = _removeCurrency(fld.value);
	if (fld.value.length) {
		if (isNaN(money) || !money.length) {
			alert(name+' appears to be an invalid money format.  Please do not include commas or dollar signs in your submission.');
			fld.focus();
			fld.select();
			return false;
		} else {
			fld.value = money;
		}

	}
	
	return true;
}

function isRequired(fld, name) {
	if (fld.disabled) return true;
	if (!fld.value.length) {
		alert(name+' is a required field.');
		fld.focus();
		if (!fld.options) {
			fld.select();
		}
		return false;
	}
	
	return true;
}

function isMinimum(fld, name, minimum) {
	if (fld.disabled) return true;
	if (fld.value.length < minimum) {
		alert(name+' requires a minimum character count of '+minimum);
		fld.focus();
		fld.select();
		return false;
	}
	return true;
}

function isRequiredSelect(fld, name) {
	if (!fld.options[fld.selectedIndex].value.length) {
		alert(name+ ' is a required field.');
		fld.focus();
		return false;
	}

	return true;

}

function isDate(fld, name, xmatch) {
	if (fld.value.length) {
	
		date = _validateUSDate(fld.value);
		
		if (!date.length) {
			alert(name+' appears to be invalid.  Please use the mm/dd/yyyy format.');
			fld.focus();
			fld.select();
			return false;
		} else {
			fld.value = date;
		}

	}
	
	return true;
}

function isIdentical(fld1, fld2, label1, label2) {

	if (fld1.value !== fld2.value) {
		alert(label1+' does not match '+label2+'.');
		fld2.focus();
		fld2.select();
		return false;
	} else {
		if (!isRequired(fld1, label1)) return false;
	}
	
	return true;
}

function isReserved(fld, name, value) {

	if (fld.value.length) {
		reserved = Array('guest','admin');
	
		val = fld.value.toLowerCase();
		val = escape(val);
		
		for (i=0;i<reserved.length;i++) {
			if (value != reserved[i]) {
				if (val == reserved[i]) {
					if (val != value) {
						alert(name+' is a reserved word and cannot be used!');
						fld.focus();
						fld.select();
						return false;
					}
				}
			} else {
				fld.value = value;
			}
		}
	
		if (val.indexOf('%20', 0) >= 0) {
			alert(name+' must not contain spaces.');
			fld.focus();
			fld.select();
			return false;
		}

	}
	
	// if (!isRequired(fld, name)) return false;
	
	return true;
}





function _removeCurrency( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from
  source string.

PARAMETERS:
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]|[a-zA-Z]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function _validateUSDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return ''; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3) //find date separator
		if (!isNaN(strSeparator)) {
			var strSeparator = strValue.substring(1,2) //find date separator
		}
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1]);
    var intYear = parseInt(arrayDate[2]);
    var intMonth = parseInt(arrayDate[0]);
		
    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return arrayDate[0]+'/'+arrayDate[1]+'/'+intYear; //found in lookup table, good date
    }

    //check for February
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      return arrayDate[0]+'/'+arrayDate[1]+'/'+intYear; //Feb. had valid number of days
  }
  return ''; //any other values, bad date
}
