var REGDCNT = 6;

////////////////////////////////////////////////////////////
//Displaying
function ShowControl( _ctl, _bShow )
{
	var hideval = "hidden";
	if( _bShow == true )
		hideval = "visible";

	var obj = document.getElementById( _ctl );
	if( obj )
		obj.style.visibility = hideval;
}

////////////////////////////////////////////////////////////
//Date control items.
function ShowDateControl( _ctl, _bShow )
{
	ShowControl( _ctl+"_dropDate", _bShow );
	ShowControl( _ctl+"_dropYear", _bShow );
	ShowControl( _ctl+"_lblDay", _bShow );
	ShowControl( _ctl+"_lblMY", _bShow );
	ShowControl( _ctl+"_hlCalendar", _bShow );
}

function NullDate( _ctl )
{
	var ctlDOB = document.getElementById( _ctl+"_dropDate" );
	if( ctlDOB )
	{
		if( ctlDOB.value == "0" )
			return( true );
	}
	
	return( false );
}

////////////////////////////////////////////////////////////
//Getting special control objects

function GetCell( _ctl )
{
	return( document.getElementById( _ctl ) );
}

function GetNumberControl( _ctl )
{
	return( GetCell( _ctl+"_txtNumber" ) );
}

function GetStateControl( _ctl )
{
	return( GetCell( _ctl+"_ddlState" ) );
}

function GetSuffixControl( _ctl )
{
	return( GetCell( _ctl+"_ddlSuffix" ) );
}

function GetTitleControl( _ctl )
{
	return( GetCell( _ctl+"_ddlTitle" ) );
}

function GetAmountControl( _ctl )
{
	return( GetCell( _ctl+"_txtAmount" ) );
}

function GetZipControl1( _ctl )
{
	return( GetCell( _ctl+"_txtCell1" ) );
}

function GetZipControl2( _ctl )
{
	return( GetCell( _ctl+"_txtCell2" ) );
}

function GetPhoneControl1( _ctl )
{
	return( GetCell( _ctl+"_txtCell1" ) );
}

function GetPhoneControl2( _ctl )
{
	return( GetCell( _ctl+"_txtCell2" ) );
}

function GetPhoneControl3( _ctl )
{
	return( GetCell( _ctl+"_txtCell3" ) );
}

function GetSSNControl1( _ctl )
{
	return( GetCell( _ctl+"_txtCell1" ) );
}

function GetSSNControl2( _ctl )
{
	return( GetCell( _ctl+"_txtCell2" ) );
}

function GetSSNControl3( _ctl )
{
	return( GetCell( _ctl+"_txtCell3" ) );
}

////////////////////////////////////////////////////////////
//Setting HTML or text methods
function SetInnerText( _ctl, _strMsg )
{
	//Netscape
	if( document.layers )
	{
//				if( lblError.childNodes.length > 0 )
//					lblError.childNodes(0).removeNode();

			//Set the error text			
//				lblError.appendChild( document.createTextNode( _strErrorMsg ) );
	}
	//IE
	else
	{
		var ctlTag = document.getElementById( _ctl );
		if( ctlTag )
			ctlTag.innerText = _strMsg;
	}
}

function SetInnerHTML( _ctl, _strMsg )
{
	//Netscape
	if( document.layers )
	{
//				if( lblError.childNodes.length > 0 )
//					lblError.childNodes(0).removeNode();

			//Set the error text			
//				lblError.appendChild( document.createTextNode( _strErrorMsg ) );
	}
	//IE
	else
	{
		var ctlTag = document.getElementById( _ctl );
		if( ctlTag )
			ctlTag.innerHTML = _strMsg;
	}
}

////////////////////////////////////////////////////////////
//Options
function RemoveOptions( _ctlDD )
{
	while( _ctlDD.options.length )
		_ctlDD.options.remove(0);
}

function CreateOption( _ctlDD, _strTxt, _strId )
{
	var oOption = document.createElement("OPTION");

	oOption.text  = _strTxt;
  oOption.value = _strId;

	//IE
  if (document.all)
		_ctlDD.add( oOption );  
	//netscape
  else
		_ctlDD.appendChild( oOption );
}

////////////////////////////////////////////////////////////
//String manipulation
function Trim( _str, _ch )
{
	var strN  = "";
	var bDone = false;

	//Trim the leading characters.	
	for( i=0; i<_str.length; i++ )
	{
		if(( !bDone )&&( _str.charAt( i ) == _ch ))
		{
		}
		else
		{
			bDone = true;
			strN += _str.charAt( i );
		}
	} //end of for

	//Strip ending chars.
	for( i=strN.length-1; i>=0; i-- )
	{
		if( strN.charAt( i ) != _ch )
			break;
	} //end of for	
	
	return( strN.substring( 0, i+1 ) );
}

//Strip out any , so we get a valid float value back.
function FloatValue( _obj )
{
	var str  = String( _obj );
	var nLen = str.length;
	var nStr = "";

	for( i=0; i<nLen; i++ )
		if( str.charAt( i ) != ',' )
			nStr += str.charAt( i );

	return( parseFloat( nStr ) );
}

////////////////////////////////////////////////////////////
//Validation

function NumberControl_ValidateMinMax( _txtControl, _nMin, _nMax, _bValidate )
{
	if( _bValidate )
	{
		if(( parseInt( _txtControl.value ) < _nMin )||( FloatValue( _txtControl.value ) > _nMax ))
		{
			alert( "Number must be between "+_nMin+" and "+_nMax+"." );
			
			_txtControl.focus();
			_txtControl.select();
		}
	}
}

function AmountControl_ValidateMinMax( _txtControl, _nMin, _nMax, _bValidate )
{
	if( _bValidate )
	{
		if(( parseFloat( _txtControl.value ) < _nMin )||( FloatValue( _txtControl.value ) > _nMax ))
		{
			alert( "Amount must be between "+_nMin+" and "+_nMax+"." );
			
			_txtControl.focus();
			_txtControl.select();
		}
	}
}








function ValidateAmount( _fMinimum, _ctlAmtId, _ctlError, _strErrorMsg )
{
	var fAmount = document.getElementById( _ctlAmtId );
	
	if(( fAmount )&&( FloatValue( fAmount.value ) < FloatValue( _fMinimum ) ))
	{
		ShowErrorMsg( _ctlError, true, _strErrorMsg );	
		return( false );
	}
	else
	{
		return( true );
	}
}

function RequiredField( _ctlFieldId, _ctlError, _strErrorMsg )
{
	var cField = document.getElementById( _ctlFieldId );
	
	if(( cField )&&( cField.value.length == 0 ))
	{
		SetInnerText( _ctlError, _strErrorMsg );
		return( false );
	}
	else
	{
		return( true );
	}
}

function IdenticalFields( _ctlFieldId1, _ctlFieldId2, _ctlError, _strErrorMsg )
{
	var cField1 = document.getElementById( _ctlFieldId1 );
	var cField2 = document.getElementById( _ctlFieldId2 );
	
	if(( cField1 )&&( cField2 )&&( cField1.value != cField2.value ))
	{
		SetInnerText( _ctlError, _strErrorMsg );
		return( false );
	}
	else
	{
		return( true );
	}
}

///////////////////////////////////////////////////
//Specific methods.
function DisplayTransRegD( _ctlSel, _strURL, _ctlLabel )
{
	var strURL = "<A target=\"_blank\" HREF=\"" + _strURL + "\">";
	var nCnt   = document.getElementById( _ctlSel ).value.split("-")[1];

	if( nCnt == 100 )
		SetInnerHTML( _ctlLabel, "" );
	else if( nCnt == 0 )
		SetInnerHTML( _ctlLabel, "no transfers so far this month, "+strURL+REGDCNT+" remaining</A>" );
	else if( nCnt >= REGDCNT )
		SetInnerHTML( _ctlLabel, "all transfers used this month, "+strURL+" 0 remaining</A>" );
	else
		SetInnerHTML( _ctlLabel, nCnt+" transfers used this month, "+strURL+(REGDCNT-nCnt)+" remaining</A>" );
}

function ValidateTransRegD( _ctlSel, _ctlError )
{
	var nCnt = document.getElementById( _ctlSel ).value.split("-")[1];
	
	if(( parseInt( nCnt ) != 100 )&&( parseInt( nCnt ) >= REGDCNT ))
	{
		ShowErrorMsg( _ctlError, true, "You have no more transfers available this month." );
		return( false );
	}

	return( true );
}

function CrossMemberChange( _thisPtr, _ctlList, _ctlValues )
{
	var arrAccts = document.getElementById( _ctlValues ).value.split( "|" );
	var arrSubs  = arrAccts[_thisPtr.selectedIndex].split( "~" );
	var ctlDD    = document.getElementById( _ctlList );

	RemoveOptions( ctlDD );

	for( i=0; i<arrSubs.length; i++ )
		CreateOption( ctlDD, arrSubs[i], i );
		
	ctlDD.selectedIndex = 0;
}

function ValidateAccountTransfer( _ctlAcct1, _ctlAcct2, _ctlError )
{
	var arrAccts1 = document.getElementById( _ctlAcct1 ).value.split( "-" );
	var arrAccts2 = document.getElementById( _ctlAcct2 ).value.split( "-" );
	
	if( arrAccts1[0] == arrAccts2[0] )
	{
		ShowErrorMsg( _ctlError, true, "Source and destination account cannot be the same." );
		return( false );
	}

	return( true );	
}

function TransferEnoughFunds( _ctlAmt, _ctlAcct, _ctlError )
{
	var fAmt   = document.getElementById( _ctlAmt ).value;
	var fAvail = document.getElementById( _ctlAcct ).value.split( "-" )[2];
	
	if( FloatValue( fAvail ) < FloatValue( fAmt ) )
	{
		ShowErrorMsg( _ctlError, true, "Do not have sufficent funds in source account." );
		return( false );
	}
	
	return( true );
}

function OpenAcctEnoughFunds( _ctlAmt, _ctlAcct, _ctlError )
{
	var fAmt   = document.getElementById( _ctlAmt ).value;
	var fAvail = document.getElementById( _ctlAcct ).value.split( "~" )[1];
	
	if( FloatValue( fAvail ) < FloatValue( fAmt ) )
	{
		ShowErrorMsg( _ctlError, true, "Do not have sufficent funds in source account." );
		return( false );
	}
	
	return( true );
}



