﻿// JScript File
  function VDate( oField ) {
	  //** Requires stripSpaces function **//
	  if(FldHasValue( oField )) {
		  var inStr = oField.value;
		  var myinStr = "";
		  var TodaysDate = new Date();
		  inStr = stripSpaces(inStr);
		  if ( inStr.length == 0 ) return false;
		  if (inStr.length > 10 || inStr.length < 6) {
			  alert("Please enter a valid date in the format MM/DD/YYYY");
			  oField.value = "";
			  return false; }
		  var Slash0 = inStr.indexOf("/");
		  if (Slash0 == -1) {
			  myinStr = inStr.substring(0,2) + "/" + inStr.substring(2,4) + "/" + inStr.substring(4, inStr.length);
			  inStr = myinStr; }
		  var Slash1 = inStr.indexOf("/");
		  if (Slash1 < 1 || Slash1 > 2) {
			  alert("The date must be entered in the format MM/DD/YYYY");
			  oField.value = "";
			  return false; }
		  var Slash2 = inStr.indexOf("/", Slash1+1);
		  if (Slash2 < 3 || Slash2 > 5) {
			  alert("The date must be entered in the format MM/DD/YYYY");
			  oField.value = "";
			  return false; }
		  Month = inStr.substring(0, Slash1) // month
		  Day = inStr.substring(Slash1 + 1, Slash2) // day
		  Year = inStr.substring(Slash2 + 1, inStr.length) // year
		  var YLen = Year.length;

		  if (!ChkNumeric2(Month) || !ChkNumeric2(Year) || !ChkNumeric2(Day) || 
		     (Year > 2100) || (Month < 1) || (Month > 12) || (Day < 1) || (Day > 31)) {
			  alert("Please enter a valid date in the format MM/DD/YYYY");
			  oField.value = "";
			  return false 
		  }
		  if (YLen == 2) {
			  if (Year < 0 || Year > 99) {
				  alert("Please enter a valid value for the year in the format MM/DD/YYYY");
				  oField.value = "";
				  return false;
			  }
			  if (Year > 75 && Year < 100) Year = "19" + Year;
			  else Year = "20" + Year;
			  if (Day.length < 2) Day = "0" + Day;
			  if (Month.length < 2) Month = "0" + Month;
			  oField.value = Month + "/" + Day + "/" + Year;
		  }
		  else {
			  if (YLen != 4 || (!ChkNumeric2(Year))) {
				  alert("Please enter a valid value for the year in the format MM/DD/YYYY");
				  oField.value = "";
				  return false; }
			  if (Year < 1880) {
				  alert("Please Enter a valid year in in the format MM/DD/YYYY");
				  oField.value = "";
				  return false }
		  }
		  if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {
			  if (Day == 31) {
				  alert("Please enter a valid value for the day - Month selected has 30 days or less");
				  oField.value = "";
				  return false; 
			  }
		  }
		  if (Month == 2){
			  var g=parseInt(Year/4)
			  if (isNaN(g)) {
				  alert("Please enter a valid value for the date");
				  oField.value = "";
				  return false; 
			  }
			  if (Day > 29) {
				  alert("Please enter a valid value for the date");
				  oField.value = "";
				  return false; 
			  }
			  if (Day == 29 && ((Year/4)!=parseInt(Year/4))) {
				  alert("Please enter a valid value for the date");
				  oField.value = "";
				  return false; 
			  }
		  }
		  if (Day.length < 2) Day = "0" + Day;
		  if (Month.length < 2) Month = "0" + Month;
		  oField.value = Month + "/" + Day + "/" + Year;
		  return true;
	  }
	  else {
		  return true;
	  }
  } // VDate
<!-- ************************************************ -->

	function FormatPhone(PhoneNo) {
		// Requires FldHasValue , NumOnly , stripSpaces , chkNumeric2 function
		if(FldHasValue(PhoneNo)) {
			var DF = PhoneNo.value + "";
			DF = NumOnly(DF);//** strip out any character other than numbers
			DF = stripSpaces(DF);//** strip out any extra spaces from the beginning or end of the value
			PhoneNo1 = DF.substring(0, 3); // First part of PhoneNo
			PhoneNo2 = DF.substring(3, 6); // Second part of PhoneNo
			PhoneNo3 = DF.substring(6, 10); // Third part of PhoneNo
			PhoneNo.value = "(" + PhoneNo1 + ") " + PhoneNo2 + "-" + PhoneNo3;
			return true;
		}
	}// ValidPhone
<!-- ************************************************ -->
	function ChkNumeric2( FieldName ) {
	var ValueSent = FieldName;
	var Vlen = FieldName.length
	for (var i = 0; i < Vlen; ++i) {
		var  ch = ValueSent.substring(i,i + 1);
		if (ch < "0" || "9" < ch)
			return false;
	}//for
	return true;
	}// ChkNumeric2
<!-- ************************************************ -->
	function NumOnly( FieldName ) {
		// by Brett W for PPDocs.com, 02 Aug 2000
		var ValueSent = FieldName;
		var Vlen = FieldName.length
		x = "";
		for (var i = 0; i < Vlen; ++i) {
			var  ch = ValueSent.substring(i,i + 1);
			if ("0" <= ch && ch <= "9") {
				x = x + ch
			}//if
		}//for
		return x;
	}// NumOnly
<!-- ************************************************ -->
	function FldHasValue( FieldName ) {
		if ((FieldName.value == "") || (FieldName.value == "undefined" )) 
			return false;
		else
			return true;
	}	//FldHasValue
<!-- ************************************************ -->
	function stripSpaces(stg) {
		//** Trim leading and trailing spaces off string value **
	  x = stg + "";
	  while (x.substring(0,1) == ' ') x = x.substring(1);
	  while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
	  return x;
	}//stripSpaces
<!-- ************************************************ -->
	function FormatZip( oField ) {
		if(FldHasValue(oField)) {
			inStr = oField.value;
			inLen = inStr.length;
			if (inLen == 5) return false;
			var dash = inStr.indexOf("-", 0);
			if (dash == -1)  {	//no dash in zip
				inStr = stripSpaces(inStr);
				FirstFive = inStr.substring(0, 5) // first 5 digits of zip code
				LastFour = inStr.substring(5, inStr.length) // last 4 digits of zip code
				oField.value = FirstFive + "-" + LastFour;
				return true;
			}
		}
	}// FormatZip
<!-- ************************************************ -->
function FormatState(oField) {
	if(FldHasValue(oField)) {
		var inStr = oField.value;
		switch ( inStr.toUpperCase() ) {
			case "AK": case "AL": case "AR": case "AZ": case "CA": case "CO": case "CT": case "DC": case "DE": 
			case "FL": case "GA": case "GU": case "HI": case "IA": case "ID": case "IL": case "IN": case "KS": 
			case "KY": case "LA": case "MA": case "MD": case "ME": case "MI": case "MN": case "MO": case "MS": 
			case "MT": case "NC": case "ND": case "NE": case "NH": case "NJ": case "NM": case "NV": case "NY":
			case "OH": case "OK": case "OR": case "PA": case "PR": case "RI": case "SC": case "SD": case "TN":
			case "TX": case "UT": case "VA": case "VI": case "VT": case "WA": case "WI": case "WV": case "WY":
			case "US":
				oField.value = inStr.toUpperCase();
				break;
			default:
				oField.value = "";
				return false;
		}
	}
}
// FormatState
<!-- ************************************************ -->

function VPC(oField) {
	if(FldHasValue( oField )) {	
		var instr = oField.value;
		var instrlen = instr.length;
		var ch = "";
		var newValue = "";
		var p = 0;
		//strip out percent sign
		for (var x=0; x<instrlen; x++){
			ch = instr.substring(x,x+1);
			if (ch != "%") newValue += ch;
			else ++p;
		}
		instrlen -= p;
		//check for more than one decimal point
		var DecimalPt = newValue.indexOf(".", 0);
		var DecimalPt2 = newValue.indexOf(".", DecimalPt + 1);
		if (DecimalPt2 != -1){
			alert("Please Enter A Numeric Value (1.234)");
			return false;
		}
		instrlen = newValue.length;
		for (var i = 0; i < instrlen ; ++i) {
			var ch = newValue.substring(i,i + 1);
			if ((ch < "0" || "9" < ch) && ch != '.') {
				alert("Please Enter A Numeric Value (1.234)");
				return false;
			}
		}
		// no more than 3 decimal places
		var places = newValue.substring(DecimalPt+1);
		if ( places.length > 3 ){
			alert("No more than 3 decimal places are required. (1.234)");
			return false;
		}
		// returns the X.999 format
		newValue -= 0;
		if (newValue == Math.floor(newValue)) newValue += ".000";
		else if (newValue*10 == Math.floor(newValue*10)) newValue += "00";
		else if (newValue*100 == Math.floor(newValue*100)) newValue += "0";
		oField.value = newValue;
		return true;
	}
	else {
		return true;
	}
}


function VCurr( oField ) {
	if(FldHasValue( oField )) {
		var ValueSent = oField.value;
		var RevisedValue = "";
		var ch = "";
		//strip out dollar signs and commas
		for (var i = 0; i < ValueSent.length; ++i) {
			ch = ValueSent.charAt(i);
			if ((ch != ",") && (ch != "$") )
				RevisedValue = RevisedValue + ch;
		}
		//check for more than one decimal point
		var FieldLength = RevisedValue.length;
		var DecimalPt = RevisedValue.indexOf(".", 0);
		if (DecimalPt != -1) {
			if (DecimalPt < FieldLength - 3) {
				alert("Please enter a valid monetary amount");
				return false; 
			}
			var DecimalPt2 = RevisedValue.indexOf(".", DecimalPt + 1);
			if (DecimalPt2 != -1) {
				currencyError = true;
				alert("Please enter a valid monetary amount");
				return false; 
			}
		}
		for (var i = 0; i < RevisedValue.length; ++i) {
			ch = RevisedValue.charAt(i);
			if ((ch < "0" || "9" < ch) && ch != '.' && ch != '$') {
				alert("Please enter a valid monetary amount");
				return false; 
			}
		}
		oField.value = outputComma(formatCent(RevisedValue));
		return true;
	}
	else {
		return true;
	}
} // VCurr

function formatCent(amount) {
	amount -= 0;
	return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}
function outputComma(number) {
	//** accounts for Negative values **
	var neg = "";
	number = '' + number
	if (number.charAt(0) == "-"){
		neg = "-";
		number = number.substring(1,number.length)
	}
	if (number.length > 6) {
		var mod = number.length%3;
		var output = (mod > 0 ? (number.substring(0,mod)) : '');
		for (i=0 ; i < Math.floor(number.length/3) ; i++) {
			if (((mod ==0) && (i ==0)) || ( output.length > number.length-3 ))
				output+= number.substring(mod+3*i,mod+3*i+3);
			else 
				output+= ',' + number.substring(mod+3*i,mod+3*i+3); 
		}
		return (neg + output);
	}
	else return neg + number;
}

function stripCommas(ValueSent) {
	// ** Clean , and $ sign from number/string values **
	var Vlen = ValueSent.length;
	var ch;
	var nval = "";
	for (var i = 0; i < Vlen; ++i) {
		ch = ValueSent.substring(i,i + 1);
		if(ch != "," && ch != "$")
			nval = nval + ch;
	}
	return nval;
}