/******************************************************************
 * Function populateForm
 * Purpose:
 * 	Used in registerAccount.cfm.  Populates the aForm.system and
 *	aForm.account fields to the values passed in sSystem and 
 *	sAccount.  Note that form.system is a dropdown so this function
 *	scans for an entry matching "sSystem" and selects it if found.
 * Parameters:
 *		aForm - html form object.
 *		sSystem - The system to select from aForm.system.
 *		sAccount - Account number to populate into aForm.account.
 */
function populateForm( aForm, sSystem, sAccount ) {
	for( var i=0; i<aForm.system.options.length; i++ ) {
		if( aForm.system.options[i].value == sSystem ) {
			aForm.system.selectedIndex = i;
			break;
		}
	}
	aForm.account.value = sAccount;
	aForm.accesscode.value = "";
	aForm.accesscode.focus();
	selectLabel( aForm, sSystem );
	return true;
}

/******************************************************************
 * Function showReadings
 * Purpose:
 * 	Used in UBAccountDetails.cfm.  First hides all readings
 *	and readings graphs.  Then shows the readings and graphs for the
 *	meter 'regSerial'.
 * Parameters:
 *		regSerial - The register serial number of the meter
 *			to be made visible.  All others should be hidden.
 */
function showReadings( regSerial ) {
	var allDivs = document.getElementsByTagName( "DIV" );
	//hide all graphs and readings.
	for( var i=0; i<allDivs.length; i++ ) {
		if( allDivs[i].id.indexOf( "graph_" ) == 0 || allDivs[i].id.indexOf( "readings_" ) == 0 || allDivs[i].id.indexOf( "header_" ) == 0 )
			allDivs[i].style.display="none";
		if((allDivs[i].id == 'graph_'+regSerial) || 
		   (allDivs[i].id == 'readings_'+regSerial) ||
		   (allDivs[i].id == 'graph_total_'+regSerial) ||
		   (allDivs[i].id == 'header_'+regSerial)
		   )
			allDivs[i].style.display="block";
	}
	return true;
}


/******************************************************************
 * Function selectLabel
 * Purpose:
 * 	Used in registerAccount.cfm.  Shows the appropriate label
 *      for the selected account type ("folio" or "account number" for example)
 * Parameters:
 *	sSystem - the system selected.
 */

function selectLabel( aForm, sSystem )
{
	var systemLabel, systemBillImage;

	/* hide all the labels and bill images */
	for( var i=0; i<aForm.system.options.length; i++ ) {
		systemLabel = document.getElementById( aForm.system.options[i].value + "_Label");
		systemBillImage = document.getElementById( aForm.system.options[i].value + "_BillLink");
		systemLabel.style.display = "none";
		if( systemBillImage ) {
			systemBillImage.style.display = "none";
		}
	}
	/* show the selected system's label and bill image. */
	systemLabel =  document.getElementById( sSystem + "_Label");
	systemBillImage = document.getElementById( sSystem + "_BillLink" );
	systemLabel.style.display = "inline";
	if( systemBillImage ) {
		systemBillImage.style.display = "block";
	}

}

/* 
 * toggle the element "idString" between display states "display1" and
 * "display2".
 */
function toggleSection( idString, display1, display2 ) {
	var obj=document.getElementById( idString );
	obj.style.display = obj.style.display == display1 ? display2 : display1;
}

/* 
 * set elements "idString_<index1>" through idstring_<index2> to have display style "display".
 */
function displayAllSections( idString, index1, index2, display ) {
	var sectionId;
	for(var i=index1; i<=index2; i++ ) {
		sectionId = idString + new Number(i).toString();
		var obj=document.getElementById( sectionId );
		if( obj )
		{
			obj.style.display = display;
		}
	}
}

/*
 * Confirm that user means to remove an account from their profile.
 */
function confirmDelete( deleteFormName, systemName, accountNumber )
{

	if( confirm( 'Remove ' + systemName + ' account ' + accountNumber + ' from your profile?' ) ) {
		return true;
	}
	return false;
}

/* Login form: verify that the email address is filled before proceeeding to the forgot password page. */
function login_checkEmail() {
	if( document.loginForm.username.value == "" ) {
		alert( 'Please fill in your email address.' );
		document.loginForm.username.focus();
		return false;
	}
	document.resetPasswordForm.username.value = document.loginForm.username.value;
	document.resetPasswordForm.submit();
	return true;
}
