function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function chkDate(field, e, keyEvnt) {
	if (keyEvnt) {
		if (window.Event) {return;}
		var key = e.keyCode;
		if (key == 8 || key == 0 || key == 9) {return;}
	}
	var len = field.value.length;
	var value = field.value;
	var valid = "1234567890"
	var tmp = tmp1 = "";
	for (var x=0; x<len; x++) {
		if (valid.indexOf(value.charAt(x)) > -1 && x < 10) {tmp += value.charAt(x);}
	}
	for (var x=0; x<tmp.length; x++) {
		if (x < 8) {tmp1 += tmp.charAt(x); if (x == 1 || x == 3) {tmp1 += "/";}}
	}
	field.value = tmp1;
}

function chkDateShort(field, e, keyEvnt) {
	if (keyEvnt) {
		if (window.Event) {return;}
		var key = e.keyCode;
		if (key == 8 || key == 0 || key == 9) {return;}
	}
	var len = field.value.length;
	var value = field.value;
	var valid = "1234567890";
	var tmp = tmp1 = "";
	for (var x=0; x<len; x++) {
		if (valid.indexOf(value.charAt(x)) > -1 && x < 8) {tmp += value.charAt(x);}
	}
	for (var x=0; x<tmp.length; x++) {
		if (x < 8) {tmp1 += tmp.charAt(x); if (x == 1) {tmp1 += "-";}}
	}
	field.value = tmp1;
}

function chkNumeric(field, e, keyEvnt) {
	if (keyEvnt) {
		if (window.Event) {return;}
		var key = e.keyCode;
		if (key == 8 || key == 0 || key == 9) {return;}
	}
	var value = field.value;
	var valid = "1234567890";
	var tmp = "";
	for (var x=0; x<value.length; x++) {
		if (valid.indexOf(value.charAt(x)) > -1) {tmp += value.charAt(x);}
	}
	field.value = tmp;
}

function chkName(field, e, keyEvnt) {
	if (keyEvnt) {
		if (window.Event) {return;}
		var key = e.keyCode;
		if (key == 8 || key == 0 || key == 9) {return;}
	}
	var value = field.value;
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.";
	var tmp = "";
	for (var x=0; x<value.length; x++) {
		if (valid.indexOf(value.charAt(x)) > -1) {tmp += value.charAt(x);}
	}
	field.value = tmp;
}

function chkMoney(field, e, keyEvnt) {
	if (keyEvnt) {
		if (window.Event) {return;}
		var key = e.keyCode;
		if (key == 8 || key == 0 || key == 9) {return;}
	}
	var value = field.value;
	var valid = "1234567890.";
	var tmp = "";
	for (var x=0; x<value.length; x++) {
		if (valid.indexOf(value.charAt(x)) > -1 && tmp.length < 8) {tmp += value.charAt(x);}
	}
	if (!keyEvnt) field.value = formatMoney(tmp);
	else field.value = tmp;
}

function chkZip(field, e, keyEvnt) {
	if (keyEvnt) {
		if (window.Event) {return;}
		var key = e.keyCode;
		if (key == 8 || key == 0 || key == 9) {return;}
	}
	var len = field.value.length;
	var value = field.value;
	var valid = "1234567890"
	var tmp = tmp1 = "";
	for (var x=0; x<len; x++) {
		if (valid.indexOf(value.charAt(x)) > -1 && x < 10) {tmp += value.charAt(x);}
	}
	for (var x=0; x<tmp.length; x++) {
		if (x == 5) tmp1 += "-";
		tmp1 += tmp.charAt(x);
	}
	field.value = tmp1;
}

function chkPhone(field, e, keyEvnt) {
	if (keyEvnt) {
		if (window.Event) {return;}
		var key = e.keyCode;
		if (key == 8 || key == 0 || key == 9) {return;}
	}
	var len = field.value.length;
	var value = field.value;
	var valid = "1234567890"
	var tmp = tmp1 = "";
	for (var x=0; x<len; x++) {
		if (valid.indexOf(value.charAt(x)) > -1 && x < 12) {tmp += value.charAt(x);}
	}
	for (var x=0; x<tmp.length; x++) {
		if (x < 10) {
			tmp1 += tmp.charAt(x);
			if (x == 2) {tmp1 += "/";}
			if (x == 5) {tmp1 += "-";}
		}
	}
	field.value = tmp1;
}


function chkSSN(field, e, keyEvnt) {
	if (keyEvnt) {
		if (window.Event) {return;}
		var key = e.keyCode;
		if (key == 8 || key == 0 || key == 9) {return;}
	}
	var len = field.value.length;
	var value = field.value;
	var valid = "1234567890"
	var tmp = tmp1 = "";
	for (var x=0; x<len; x++) {
		if (valid.indexOf(value.charAt(x)) > -1 && x < 11) {tmp += value.charAt(x);}
	}
	for (var x=0; x<tmp.length; x++) {
		if (x < 9) {
			tmp1 += tmp.charAt(x);
			if (x == 2) {tmp1 += "-";}
			if (x == 4) {tmp1 += "-";}
		}
	}
	field.value = tmp1;
}

function chkFld(field, desc, minLen) {
	if (field.type == "text") {
		if (field.value.length < minLen) {
			alert("Please fill in the \"" + desc + "\" field.");
			field.focus();
			return false;}
	}
	if (field.type == "select-one") {
		if (field[field.selectedIndex].value == "#") {
			alert("Please select a value in the \"" + desc + "\" field.");
			field.focus();
			return false;}
	}
	return true;
}

function chkEmail(field) {
  if (field.value == "") {
    return true;
  }
  if (field.value.match("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9])+$")) {
    return true;
  }
  alert("" + field.value + " is an invalid email address.");
//  field.select();
//  field.focus();
  return false;
}

function chkPassword(field, orig)	{
	if (field.value == orig.value) {
		return true;
	}
	
	if (field.value.match("(\\S)\\1\\1")) {
		alert("Passwords cannot contain more than 2 of the same character in a row.");
  		field.focus();
  		return false;
	}
	
	if (field.value.length < 6) {
		alert("Please enter a password of at least 6 characters.");
		field.focus();
  		return false;
	}

	if (!field.value.match("^\\w*(?=\\w*\\d)(?=\\w*[a-zA-Z])\\w*$")) {
		alert("Please enter a password consisting of both letters and numbers.");
		field.focus();
		return false;
	}
	
	return true;
}

function chkCCNumLength(field) {
	if (field.value != "") {
		if (field.value.length != 16 && field.value.length != 15) {
	        alert("\nPlease re-enter your Credit Card number.");
			field.focus();
	        return false;
		}   
	}
	return true;
}





function testDate(el) {
	if (isDate(el.value)) {
		return true;
	}
	else {
		alert("Please enter a valid date in mm/dd/yyyy format.");
		el.focus();
		return false;
	}
}

function testDateShort(el) {
	if (isDateShort(el.value)) {
		return true;
	}
	else {
		alert("Please enter a valid date in mm/yyyy format.");
		el.focus();
		return false;
	}
}

function isDateShort(dateStr) {
// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-yyyy or mm/yyyy. 
// The function returns true if a valid date, false if not.
// ******************************************************************

    var matchArray = dateStr.match("^(\\d{1,2})(\\/|-)(\\d{4})$"); // is the format ok?

		if (matchArray == null) {      
				return false;
    }
		else {
  		   month = matchArray[1]; // parse date into variables
      	 year = matchArray[3];
  			
  		
          if (month < 1 || month > 12) { // check month range
      				return false;
          }
 
  	    return true; // date is valid
			}
}

function isDate(dateStr) {
// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.
// The function returns true if a valid date, false if not.
// ******************************************************************

    var matchArray = dateStr.match("^(\\d{1,2})(\\/|-)(\\d{1,2})(\\/|-)(\\d{4})$"); 

		if (matchArray == null) {      
				return false;
    }
		else {
  		   month = matchArray[1]; // parse date into variables
      	 day = matchArray[3];
      	 year = matchArray[5];
  			
  		
          if (month < 1 || month > 12) { // check month range
      				return false;
          }
      
          if (day < 1 || day > 31) {
      				return false;
          }
      		
          if ((month==4 || month==6 || month==9 || month==11) && day==31) {
      				return false;
          }
      		
      		if (month == 2) { // check for february 29th
              var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
              if (day > 29 || (day==29 && !isleap)) {
      				return false;
              }
      		}
  	    return true; // date is valid
			}
}

function formatMoney(str) {
	var num = new NumberFormat(str);
	var val = num.toFormatted();
	if (val == 'NaN.00') return '0.00';
	else return val;
}

function NumberFormat(num)
{
	// num - The number to be formatted
	// isCommas - Switch that indicates if there should be commas
	// isCurrency - Switch that indicates if should be displayed as currency
	// setCurrencyPrefix - The symbol that precedes currency
	// places - The precision of decimal places

	// variables
	this.num = (num==null) ? 0 : num;
	this.isCommas = false;
	this.isCurrency = true;
	this.currencyPrefix = '';
	this.places = 2;

	this.toFormatted = toFormatted;

	// internal methods
	this.getRounded = getRounded;
	this.preserveZeros = preserveZeros;

}


// Returns the number formatted according to the settings (a string)
function toFormatted() {
	var pos;
	var nNum = this.num; // number as a number
	var nStr;            // number as a string

	// round decimal places
	nNum = this.getRounded(nNum);
	nStr = this.preserveZeros(Math.abs(nNum)); // this step makes nNum into a string. Math.abs

	if (this.isCommas) {
		pos = nStr.indexOf('.');
		if (pos == -1) {
			pos = nStr.length;
		}
		while (pos > 0) {
			pos -= 3;
			if (pos <= 0) break;
			nStr = nStr.substring(0,pos) + ',' + nStr.substring(pos, nStr.length);
		}
	}
	
	nStr = (nNum < 0) ? '-' + nStr : nStr;

	if (this.isCurrency) {
		// add dollar sign in front
		nStr = this.currencyPrefix + nStr;
	}

	return (nStr);
}

function getRounded(val) {
	var factor;
	var i;

	// round to a certain precision
	factor = 1;
	for (i=0; i<this.places; i++)
	{	factor *= 10; }
	val *= factor;
	val = Math.round(val);
	val /= factor;

	return (val);
}

function preserveZeros(val) {
	var i;

	// make a string - to preserve the zeros at the end
	val = val + '';
	if (this.places <= 0) return val; // leave now. no zeros are necessary
	
	var decimalPos = val.indexOf('.');
	if (decimalPos == -1) {
		val += '.';
		for (i=0; i<this.places; i++) {
			val += '0';
		}
	}
	else {
		var actualDecimals = (val.length - 1) - decimalPos;
		var difference = this.places - actualDecimals;
		for (i=0; i<difference; i++) {
			val += '0';
		}
	}
	
	return val;
}

function stripCommas(el) {
	var strVal = new String();
	strVal = el.value;
	var re = new RegExp (',', 'gi') ;	
	el.value = strVal.replace(re, '');
}

