function isMsie4orGreater()
{ 
  var ua = window.navigator.userAgent;   var msie = ua.indexOf ( "MSIE " );
  if  (msie > 0)
    {return (parseInt ( ua.substring ( msie+5, ua.indexOf ( ".", msie ) ) ) >=4) && (ua.indexOf("MSIE 4.0b") <0) ;}
  else {return false;}
}

function removeChar(argValue)
{
return escape(argValue);
}

function goBack()
{
window.history.go(-1);
}

function frmtString(argString)
{
	var r, re;
	var s = argString;
  re = /\'/gi;
  r = s.replace(re, "’");
  return(r);
}

function getDatePartFromDate(argDate, argPart) 
{
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	var matchArray = argDate.match(datePat); // is the format ok?
	if (matchArray == null) 
		return "";
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) 
		return "";
	if (day < 1 || day > 31) 
		return "";
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
		return "";
	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 "";
		}
	return matchArray[argPart];  // date is valid
}

function isValidEmail(argEmail) 
{
//if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(argEmail))
//	return (true)
//else
//	return (false)
if (argEmail.indexOf(";")>0)
	{
	var stremails = argEmail.split(";");
	for (var i=0;i<stremails.length;i++) 
		{
	    if (emailCheck(stremails[i]) == false)
				return false;
    }
   return true;
  }
else
	return emailCheck(argEmail);
}

function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " ' . [ ]    */
var specialChars="\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
				return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>4) {
   // the address must end in a two - four letter word.
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   return false
}

// valid
return true;
}


function compareDates(argdt1, argdt2)
{
//returns 
//0 - date1 is later
//1 - dates are the same
//2 - date2 is later
//-1 - date1 or date2 is not a date
if (getDatePartFromDate(argdt1, 1) == "" || getDatePartFromDate(argdt2, 1) == "")
	return -1;
else
	{
	var dtstart = new Date(argdt1);
	var dtend = new Date(argdt2);
	if (dtstart.valueOf() == dtend.valueOf())
		return 1;
	else if (dtstart.valueOf() < dtend.valueOf())
		return 2;
	else
		return 0;
	}
}

function isValidDate(argInput) 
{
	var strErrMsg = "Please enter a valid date (mm/dd/yyyy).";
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables

	//resetType() //reset saved flag

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	var dateStr = argInput.value;
	
	if (argInput.value == "")
		return false;

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) 
		{
		alert(strErrMsg);
		argInput.value = "";
		return false;
		}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) 
		{ // check month range
		alert(strErrMsg);
		argInput.value = "";
		return false;
		}
	if (day < 1 || day > 31) 
		{
		alert(strErrMsg);
		argInput.value = "";
		return false;
		}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
		{
		alert(strErrMsg);
		argInput.value = "";
		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)) 
			{
			alert(strErrMsg);
			argInput.value = "";
			return false;
		  }
		}
	if (year.length == 2)
		{ // convert date to 4 digit year
		var dt = new Date()
		year = dt.getYear().toString().substr(0, 2) + year;
		argInput.value = month + "/" + day + "/" + year;
		}
	return true;  // date is valid
}

function isValidTime(argInput) {
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.
var strErrMsg = "Please enter a valid time (hh:ss).";

if (argInput.value == "")
		return false;

if (argInput.value.indexOf(".") > 0)
	{
	var r, re;
	var s = argInput.value;
  re = /\./gi;
  r = s.replace(re, ":");
  argInput.value = r;
	}

if (argInput.value.indexOf(":") == -1)
  argInput.value = argInput.value + ":00";

var timeStr = argInput.value;
var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

var matchArray = timeStr.match(timePat);
if (matchArray == null) 
	{
	alert("Time is not in a valid format.");
	argInput.value = "";
	return false;
	}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];	
ampm = matchArray[6];

if (second=="") {second=null}

if (ampm=="") {ampm=null}

if (hour < 0 || hour > 12) 
	{
	alert(strErrMsg);
	argInput.value = "";
	return false;
	}

if (minute < 0 || minute > 59)
	{
	alert(strErrMsg);
	argInput.value = "";
	return false;
	}
argInput.value = hour + ":" + minute;
return true;  // time is valid
}


function resetType()
{
//document.frmPlan.dbaction.value = "";
}

function isValidCurr(argFld)
{
	if (argFld.value != "")
		argFld.value = formatCurrency(argFld.value);
}

function formatCurrency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100); 
	num = Math.floor(num).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 ('$' + num + "." + cents); 
}

function formatPhone(num)
{
	num = num.toString().replace(/\-|\.|\ |\(|\)|\//g,'');
	if(isNaN(num)) num = "";
	if (num.length == 7)
		num = num.substr(0, 3) + "-" + num.substr(3, 4);
	else if (num.length == 10)
		num = num.substr(0, 3) + "-" + num.substr(3, 3) + "-" + num.substr(6, 4);
	return (num); 
}

function isValidNum(argFld)
{
	if (argFld.value != "")
		argFld.value = formatNum(argFld.value);
}

function formatNum(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0"; 
	num = Math.round(num).toString(); 
	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 (num); 
}

function formatNumNoComma(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0"; 
	num = Math.round(num).toString(); 
	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 (num); 
}

function formatNumDec(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100); 
	num = Math.floor(num).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 (num + "." + cents); 
}

function returnNumber(argString)
{
  var r, re;
  //var s = formatCurrency(argString)
  var s = (argString)
  re = /\$/gi;
  r = s.replace(re, "");
  re = /\,/gi;
  s = r.replace(re, "");
  re = /\-/gi;
  s = r.replace(re, "");
  if (parseFloat(s) < 0)
		return(0);
  else
		if(isNaN(parseFloat(s))) s = "0";
		return(parseFloat(s));
}

function returnNumberNeg(argString)
{
  var r, re;
  //var s = formatCurrency(argString)
  var s = (argString)
  re = /\$/gi;
  r = s.replace(re, "");
  re = /\,/gi;
  s = r.replace(re, "");
  re = /\-/gi;
  s = r.replace(re, "");

  if(isNaN(parseFloat(s))) s = "0";
  return(parseFloat(s));
}

function closeForm()
{
document.location.href = "default.asp";
}

function checkData(argCheckBox)
{
	//resetType() //reset saved flag
	if (isMsie4orGreater() == true)
	{
	var bln = argCheckBox.checked;
	var str = argCheckBox.name;
	var intCount = argCheckBox.subcount;
	for (var i=1; i<=intCount; i++)
		{
		document.frmPlan[str + i].checked = bln;
		}
	}
}

function returnDate()
{
  var d, s="";
  d = new Date();
  s += (d.getMonth() + 1) + "/";
  s += d.getDate() + "/";
  s += d.getYear();
  return(s);
}

function areEqual(arg1, arg2)
{
if (arg2.value == "" & returnNumber(arg1.value) == 0)
	{
	return true;
	}
else if (arg1.value == "" & returnNumber(arg2.value) == 0)
	{
	return true;
	}
else if (returnNumber(arg1.value) == returnNumber(arg2.value))
	{
	return true;
	}
else
	return false;
}
