function isRequired(fld, name, msg) {
	if (!msg) { msg = "'" +name+"'"+' is a required field.'; }
	if (fld.disabled) return true;
	if (!fld.value.length) {
		alertAndFocus(fld, msg);
		return false;
	}
	
	return true;
}

function isNumeric(fld, name) {
	msg = "'"+name+"'"+' must be a number.  Please do not include commas or non-numeric characters.';
	if (fld.value.length) {
		if (isNaN(fld.value)) { 
			alertAndFocus(fld, msg);
			return false;
		}
	}
	
	return true;
}

// Value must be a number above 0.
function isPositive(fld, name) {
	msg = "'"+name+"'"+' must be a positive number.  Please do not include commas or non-numeric characters.';
	if (fld.value.length) {
		if (isNaN(fld.value) || fld.value <= 0.000001) { 
			alertAndFocus(fld, msg);
			return false;
		}
	}
	
	return true;
}

function isEmail(fld, name) {
	if (fld.value.length) {
		var isEmail = ((fld.value.indexOf("@") != -1) && (fld.value.indexOf(".") != -1));
		var msg = name+' does not appear to be a valid format.  E-mail addresses must by in the xxx@xxx.com format.';
		if (!isEmail) { alertAndFocus(fld, msg); return false; }
	}
	
	return true;
}

function isMoney(fld, name, xmatch) {
	money = stripCurrency(fld.value); // Strip
	if (fld.value.length) {
		if (isNaN(money) || !money.length) {
			var msg = name+' appears to be an invalid money format.  Please do not include commas or dollar signs in your submission.';
			alertAndFocus(fld, msg);
			return false;
		} else if (money > 99999) {
			var msg = name+' is greater than $99,999.  Please enter a lower value.';
			alertAndFocus(fld, msg);
			return false;		
		} else {
			money -= 0;
			money = (money == Math.floor(money)) ? money + '.00' : ( (money*10 == Math.floor(money*10)) ?  money + '0' : money);
			fld.value = money;
		}

	}
	
	return true;
}

function isDate(fld, name) {
	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 formatPhone(el) {

    var phone = el.value;

    phone = phone.replace(/[^0-9]/g, "");

    if (phone.length != 10) {
        alert("Phone number does not appear to be a valid format.  Phone numbers must by in the 111-111-1212 format.");
        el.focus();
        return false;
    }

    phone = phone.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");

    el.value = phone;

    return true;
}

function stripCurrency( strValue ) { // Strips currency symbols, decimals, etc.
  
	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{2,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 = arrayDate[1]; // if (intDay < 10) { intDay = '0' + intDay; }
    var intYear = arrayDate[2]; 
    var intMonth = arrayDate[0]; // if (intMonth < 10) { intMonth = '0' + intMonth; }

    if (intYear == '00') { intYear = '2000'; } else if (intYear < 50) { intYear = parseInt(intYear) + 2000; } else if (intYear > 50 && intYear < 100) { intYear = parseInt(intYear) + 1900; }
    
    //check if month value and day value agree
    if(arrayLookup[intMonth] != null) {
      if(intDay <= arrayLookup[intMonth] && intDay != 0)
        return intMonth+'/'+intDay+'/'+intYear; //found in lookup table, good date
    }

    //check for February
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      return intMonth+'/'+intDay+'/'+intYear; //Feb. had valid number of days
  }
  return ''; //any other values, bad date
}

function setConfirmUnload(on) {

	window.onbeforeunload = (on) ? unloadMessage : null;

}

function unloadMessage() {
	
	return 'You have entered new data on this page.  If you navigate away from this page without first saving your data, the changes will be lost.';

}

function alertAndFocus(el, msg) {

	alert(msg);
	setFocus(el);

}

function setFocus(el) {

	el.focus();
	if (!el.options) { el.select(); }

}

