// -------------------------------------------------
// Debug(str)
// -------------------------------------------------
// if Debug is on, displays string in status window
// -------------------------------------------------
function Debug(str)
{
	if ( DebugLvl > 0 )
		window.status = str;
	if ( DebugLvl > 1 )
		alert( str );
}


// -------------------------------------------------
// SaveAndGo(str)
// -------------------------------------------------
// -------------------------------------------------
function SaveAndGo(str)
{
	Debug("NextPage = " + str);
	document.form1.NextPage.value = str;
	document.form1.submit();
}


// -------------------------------------------------
// Today()
// -------------------------------------------------
// returns a string of the form:
//   dayofweek, month day, year
//   ex: Thursday, March 31, 2005
// -------------------------------------------------
function Today()
{
	today = new Date();
	return today.toLocaleDateString();
}


// -------------------------------------------------
// DispHelp(helpid)
// -------------------------------------------------
// populates the HelpText area (<div>) with the
// text indicated by the helpid
// -------------------------------------------------
function DispHelp(helpid)
{
	if ( ht=document.getElementById("HelpText") )
		ht.innerHTML = HelpTextArr[helpid];
}


// -------------------------------------------------
// Trim Functions
// -------------------------------------------------
function Trim(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function leftTrim(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString) 
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


// -------------------------------------------------
// Padding Functions
// -------------------------------------------------
function ZPad(str, sz)
{
	var PadStr = "";
	var NumStr = Trim("" + str);	//forces arg to a string
	var PadSz = sz - NumStr.length;
	for (var i = 0; i < PadSz; i++)
		PadStr += "0";
	return PadStr+NumStr;
}

function SPad(str, sz)
{
	var PadStr = "";
	var NumStr = Trim("" + str);	//forces arg to a string
	var PadSz = sz - NumStr.length;
	for (var i = 0; i < PadSz; i++)
		PadStr += " ";
	return PadStr+NumStr;
}

function SPad2(str, sz)
{
	var PadStr = "";
	var NumStr = Trim("" + str);	//forces arg to a string
	var PadSz = sz - NumStr.length;
	for (var i = 0; i < PadSz; i++)
		PadStr += "&nbsp;";
	return PadStr+NumStr;
}


function SetSubSelect(MstList, SubList, ResList, ClearFlg)
{
  //alert('Master = ' + MstList.options.length 
  //  + '\nSubList = ' + SubList.options.length 
  //  + '\nResList = ' + ResList.options.length);
	MstValue = MstList.options[MstList.selectedIndex].value;

	if ( ClearFlg )
    ResList.options.length = 0;
  
	for ( i=0; i<SubList.options.length; i++ )
	{
		if ( SubList.options[i].id == 'Idx'+MstValue )
		{
			CloneOpt = SubList.options[i].cloneNode(false);
			CloneOpt.value = SubList.options[i].value;
			CloneOpt.text = SubList.options[i].text;
			if ( window.clientInformation.appName == 'Netscape' )
				ResList.add(CloneOpt, null);
			else
				ResList.options.add(CloneOpt);
		}
	}
}

function SetSubSelect2(MstList, SubList, ResList, ClearFlg, Filter)
{
  /////// STILL NEEDS WORK - SFF 2/19/07 ////////
  //alert('Master = ' + MstList.options.length 
  //  + '\nSubList = ' + SubList.options.length 
  //  + '\nResList = ' + ResList.options.length);
  var FilterList;
	MstValue = MstList.options[MstList.selectedIndex].value;

	if ( ClearFlg )
    ResList.options.length = 0;
  
	for ( i=0; i<SubList.options.length; i++ )
	{
		if ( SubList.options[i].id == 'Idx'+MstValue )
		{
			CloneOpt = SubList.options[i].cloneNode(false);
			CloneOpt.value = SubList.options[i].value;
			CloneOpt.text = SubList.options[i].text;
			ResList.options.add(CloneOpt);
		}
	}
}


function SetDbLoc(currdbloc)
{
	document.forms[0].DbLoc.value= currdbloc;
	document.forms[0].submit();
	//alert(document.forms[0].DbLoc.value);
}


function ShowHelp()
{
	window.open('Help.asp', 'help', 'width=420, height=560, resizable=yes, scrollbars=yes');
}


function IsNumeric(sText)
{
   var ValidChars = "0123456789.$, ";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}




function FldValSSN(fld)
{
	var NumChars = '0123456789';
	var RawSSN = fld.value;
	var NumSSN = '';
	var FmtSSN = '';
	
	if ( RawSSN.length < 4 )
		return false;
	
	// Strip Non-numeric Chars
	for ( i=0; i < RawSSN.length; i++ ) 
		if ( NumChars.indexOf(RawSSN.charAt(i)) >= 0 )
			NumSSN = NumSSN + RawSSN.charAt(i);
	
	// Format Number
	FmtSSN = NumSSN.substr(0,3) + '-'+ NumSSN.substr(3,2) ;
	if ( RawSSN.length > 6 )
		FmtSSN = FmtSSN + "-" + NumSSN.substr(5,4);
	
	fld.value = FmtSSN;
	
	// Check for 10-digit Number
	if ( FmtSSN.length == 11 )
	{
		fld.style.color = 'Black';
		return true;
	}
	else
	{
		fld.style.color = 'Red';
		return false;
	}
}


function FldValPhone(fld)
{
	var NumChars = '0123456789';
	var RawPhone = fld.value;
	var NumPhone = '';
	var FmtPhone = '';
	
	if ( RawPhone.length < 6 )
		return false;
	
	// Strip Non-numeric Chars
	for ( i=0; i < RawPhone.length; i++ ) 
		if ( NumChars.indexOf(RawPhone.charAt(i)) >= 0 )
			NumPhone = NumPhone + RawPhone.charAt(i);
	
	// Format Number
	FmtPhone = '(' + NumPhone.substr(0,3) + ') '+ NumPhone.substr(3,3) ;
	if ( RawPhone.length > 9 )
		FmtPhone = FmtPhone + "-" + NumPhone.substr(6,4);
	
	fld.value = FmtPhone;
	
	// Check for 10-digit Number
	if ( NumPhone.length == 10 )
		return true;
	else
		return false;
}


function FldValDate(fld)
{
	var NumChars = '0123456789';
	var ValChars = '0123456789-/';
	var RawDate = fld.value;
	var ValDate = '';
	var NumDate = '';
	var FmtDate = '';
	
	// Check for Date String Large Enough to Process
	if ( RawDate.length < 3 )
		return false;
	
	// Strip Non-valid Chars
	for ( i=0; i < RawDate.length; i++ ) 
		if ( ValChars.indexOf(RawDate.charAt(i)) >= 0 )
			ValDate = ValDate + RawDate.charAt(i);

	// Check Month
	if ( ValDate.charAt(1) == '/' || ValDate.charAt(1) == '-' )
		NumDate = '0' + ValDate.substr(0,1)
	else
		NumDate = ValDate.substr(0,2)
	if ( ValDate.charAt(2) == '/' || ValDate.charAt(2) == '-' )
		RawDate = ValDate.substr(3)
	else
		RawDate = ValDate.substr(2)
	ValDate = ''

	// Check Day
	if ( RawDate.length > 0 )
	{
	if ( RawDate.charAt(1) == '/' || RawDate.charAt(1) == '-' )
		NumDate = NumDate + '0' + RawDate.substr(0,1)
	else
		NumDate = NumDate + RawDate.substr(0,2)
	if ( RawDate.charAt(2) == '/' || RawDate.charAt(2) == '-' )
		ValDate = RawDate.substr(3)
	else
		ValDate = RawDate.substr(2)
	}

	// Check Year
	if ( ValDate.length > 0 )
	{
	if ( ValDate.charAt(0) == '0' )
		NumDate = NumDate + '20' + ValDate.substr(0,2)
	else if ( ValDate.charAt(0) >= '3' )
		NumDate = NumDate + '19' + ValDate.substr(0,2)
	else if ( ValDate.length > 1 && ValDate.substr(0,2) != '19' && ValDate.substr(0,2) != '20' )
		NumDate = NumDate + '19' + ValDate.substr(0,2)
	else
		NumDate = NumDate + ValDate.substr(0,4)
	}
	
	// Format Number
	FmtDate = NumDate.substr(0,2) + '/'+ NumDate.substr(2,2);
	if ( NumDate.length > 4 )
		FmtDate = FmtDate + "/" + NumDate.substr(4,4);
	
	fld.value = FmtDate;

	// Check for valid date 
	if ( FmtDate.length == 10 )
	{
		var testDate=new Date(FmtDate);
		//window.status = testDate.toDateString() + " : " + (testDate.getMonth()+1) + " == " + parseInt(FmtDate.substr(0,2),10) + " (" + FmtDate.substr(0,2) + ")";
		if ( testDate.getMonth()+1 == parseInt(FmtDate.substr(0,2),10) )
		{
			fld.style.color = 'Black';
			return true;
		}
		else
		{
			fld.style.color = 'Red';
			return false;
		}
	} 
}


function FldValCur(fld, centflg)
{
	var NumChars = '0123456789';
	var ValChars = '0123456789-.';
	var RawCur = fld.value;
	var ValCur = '';
	var DolCur = '';
	var CntCur = '';
	var FmtCur = '';
	var NegFlg = 0;
	var DecFlg = 0;
	var DecPos = 0;
	
	// Check for Cur String Large Enough to Process
	if ( RawCur.length < 3 )
		return false;
	
	// Strip Non-valid Chars
	for ( i=0; i < RawCur.length; i++ ) 
		if ( ValChars.indexOf(RawCur.charAt(i)) >= 0 )
			ValCur = ValCur + RawCur.charAt(i);

	// Check for Negative
	if ( ValCur.charAt(0) == '-' )
		NegFlg = 1;
	
	// Check For Decimal
	if ( (DecPos=ValCur.indexOf('.')) >= 0 )
		DecFlg = 1;
	else
		DecPos = ValCur.length;
	
	// Strip Non-numeric Chars From Dollar
	for ( i=NegFlg; i < DecPos; i++ ) 
		if ( NumChars.indexOf(ValCur.charAt(i)) >= 0 )
			DolCur = DolCur + ValCur.charAt(i);
	
	if ( centflg == 1 && DecFlg == 1 )
	{
		// Strip Non-numeric Chars From Cents
		for ( i=1; i <= 2; i++ )
			if ( DecPos+1 < ValCur.length ) 
				if ( NumChars.indexOf(ValCur.charAt(DecPos+i)) >= 0 )
					CntCur = CntCur + ValCur.charAt(DecPos+i);
	}
	
	// Format Number
	if ( NegFlg ) 
		FmtCur = '-';
	for ( i=0; i < DolCur.length; i++ )
	{
		FmtCur = FmtCur + DolCur.charAt(i);
		if ( (DolCur.length - i) > 1 && (DolCur.length - i) % 3 == 1 )
			FmtCur = FmtCur + ',';
	}
	//if ( centflg == 1 || ( centflg == 2 && DecFlg == 1 ) )
	if ( centflg == 1 && DecFlg == 1 )
	{
		FmtCur = FmtCur + '.';
		FmtCur = FmtCur + CntCur;
	}
	
	fld.value = FmtCur;
	
	// Check for 10-digit Number
	if ( DolCur.length == 8 )
		return true;
	else
		return false;
}


function IsIntStr(str)
{
	var ValidChars = '0123456789-';
	return ConsistsOf(str, ValidChars);
}


function ConsistsOf(str, set)
{
	for ( i=0; i < str.length; i++ ) 
		if ( set.indexOf(str.charAt(i)) == -1 ) 
			return false;
	return true;
}
