function isEmail(fld, name) {
	var re_mail = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
	if (!re_mail.test(fld.value)) {
		alert(name+' does not appear to be a valid format.');
		fld.focus();
		fld.select();
		return false;
	}
	
	return true;
}

function isNumeric(fld, name) {
	if (isNaN(fld.value)) { 
		alert(name+' must be a number.');
		fld.focus();
		fld.select();
		return false;
	}
	
	return true;
}

function isPercent(fld, name) {

	// Strip non-numeric characters
	var objRE = /[^0-9\.]/g;
	fld.value = fld.value.replace(objRE,'');

	if (fld.value > 100) { 
		alert(name+' must be a percentage less than or equal to 100.');
		fld.focus();
		fld.select();
		return false;
	}

	return true;

}

function isDaysInYear(fld, name) {

	// Strip non-numeric characters
	var objRE = /[^0-9]/g;
	fld.value = fld.value.replace(objRE,'');

	if (fld.value > 365) { 
		alert(name+' must be a number less than or equal to 365 (# days in a year).');
		fld.focus();
		fld.select();
		return false;
	}

	return true;

}

function isMoney(fld, name) {
	// strip all the currency stuff
	money = _removeCurrency(fld.value);
	
	if (isNaN(money)) {
		alert(name+' appears to be an invalid format.');
		fld.focus();
		fld.select();
		return false;
	}
	
	return true;
}

function isRequired(fld, name) {
	if (!fld.value.length) {
		alert(name+' is a required field.');
		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) {
	date = _validateUSDate(fld.value);
	
	if (!date.length) {
		alert(name+' appears to be invalid');
		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) {

	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;
	}
	
	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
    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
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

