// TemplateName=donations2.js
// $Header: /home/cvs/cvsroot/site_data/001/00000001/static_data/js/donations2.js,v 1.8 2007/04/10 16:22:45 jeffm Exp $

/**
 * @fileoverview This file contains functions for the second generation donation
 * processing application.
 *
 * @requires ObservableComponent defined in obs_comp.js
 */

/**
 * Creates a new Address object.
 * @class Address objects hold the data that make up an address.
 */
function Address (_street1, _street2, _city, _state, _zip, _country)
{
	this.street1 = _street1;
	this.street2 = _street2;
	this.city = _city;
	this.state = _state;
	this.zip = _zip;
	this.country = _country;
}

/**
 * Creates an object containing the observable components for the address fields.
 * @class AddressComponents objects hold observable components for address field inputs.
 *
 * @param _street1CompId the ID of the observable component for the "Street 1" field
 * @param _street2CompId the ID of the observable component for the "Street 2" field
 * @param _cityCompId the ID of the observable component for the "City" field
 * @param _stateCompId the ID of the observable component for the "State" field
 * @param _zipCompId the ID of the observable component for the "ZIP Code" field
 * @param _countryCompId the ID of the observable component for the "Country" field
 */
function AddressComponents (_street1CompId,
														_street2CompId,
														_cityCompId,
														_stateCompId,
														_zipCompId,
														_countryCompId)
{
	this.street1Comp = get_observable_component (_street1CompId);
	this.street2Comp = get_observable_component (_street2CompId);
	this.cityComp    = get_observable_component (_cityCompId);
	this.stateComp   = get_observable_component (_stateCompId);
	this.zipComp     = get_observable_component (_zipCompId);
	this.countryComp = get_observable_component (_countryCompId);
}

/**
 * @class DonAddressCopier is an observer of observable components.
 * Its purpose is to copy one set of address fields into another.
 */
function DonAddressCopier (_srcAddr, _srcAddrComps, _destAddrComps)
{
	this.srcAddr = _srcAddr;
	this.srcAddrComponents = _srcAddrComps;
	this.destAddrComponents = _destAddrComps;
}

/**
 * This is the observe method that makes this class a valid observer.
 * @param _event is an ObservableComponentEvent.
 */
DonAddressCopier.prototype.observe = function (_event)
{
	if (!_event.component || !filter_values_equal (_event.component.get(), true)) return;

	this.copyField (this.srcAddrComponents.street1Comp,
									this.destAddrComponents.street1Comp,
									this.srcAddr.street1);
	this.copyField (this.srcAddrComponents.street2Comp,
									this.destAddrComponents.street2Comp,
									this.srcAddr.street2);
	this.copyField (this.srcAddrComponents.cityComp,
									this.destAddrComponents.cityComp,
									this.srcAddr.city);
	this.copyField (this.srcAddrComponents.stateComp,
									this.destAddrComponents.stateComp,
									this.srcAddr.state);
	this.copyField (this.srcAddrComponents.zipComp,
									this.destAddrComponents.zipComp,
									this.srcAddr.zip);
	this.copyField (this.srcAddrComponents.countryComp,
									this.destAddrComponents.countryComp,
									this.srcAddr.country);
}

/**
 * Copies the value from the source field to the destination field.
 * If there is not a source field, then copy from the source value.
 */
DonAddressCopier.prototype.copyField = function (_srcComp, _destComp, _srcValue)
{
	if (_destComp) {
		var value = _srcComp ? _srcComp.get() : _srcValue;

		if (value)
			_destComp.set (value);
	}
}


/**
 * Name objects hold the components of name values.
 */
function Name (_title,
               _firstName, _middleName, _lastName,
               _suffix, _profSuffix)
{
	this.title      = _title;
	this.firstName  = _firstName;
	this.middleName = _middleName;
	this.lastName   = _lastName;
	this.suffix     = _suffix;
	this.profSuffix = _profSuffix;
}

/**
 * NameComponents objects hold observable components for name field inputs.
 */
function NameComponents (_titleCompId,
                         _firstNameCompId, _middleNameCompId, _lastNameCompId,
                         _suffixCompId, _profSuffixCompId)
{
	this.titleComp      = get_observable_component (_titleCompId);
	this.firstNameComp  = get_observable_component (_firstNameCompId);
	this.middleNameComp = get_observable_component (_middleNameCompId);
	this.lastNameComp   = get_observable_component (_lastNameCompId);
	this.suffixComp     = get_observable_component (_suffixCompId);
	this.profSuffixComp = get_observable_component (_profSuffixCompId);
}

/**
 * DonNameCopier is an observer of observable components.
 * Its purpose is to copy one set of name fields into another.
 */
function DonNameCopier (_srcName, _srcNameComps, _destNameComps)
{
	this.srcName            = _srcName;
	this.srcNameComponents  = _srcNameComps;
	this.destNameComponents = _destNameComps;
}

/**
 * This is the observe method that makes this class a valid observer.
 * @param _event is an ObservableComponentEvent.
 */
DonNameCopier.prototype.observe = function (_event)
{
	if (!_event.component || !filter_values_equal (_event.component.get(), true)) return;

	this.copyField (this.srcNameComponents.titleComp,
	                this.destNameComponents.titleComp,
	                this.srcName.title);
	this.copyField (this.srcNameComponents.firstNameComp,
	                this.destNameComponents.firstNameComp,
	                this.srcName.firstName);
	this.copyField (this.srcNameComponents.middleNameComp,
	                this.destNameComponents.middleNameComp,
	                this.srcName.middleName);
	this.copyField (this.srcNameComponents.lastNameComp,
	                this.destNameComponents.lastNameComp,
	                this.srcName.lastName);
	this.copyField (this.srcNameComponents.suffixComp,
	                this.destNameComponents.suffixComp,
	                this.srcName.suffix);
	this.copyField (this.srcNameComponents.profSuffixComp,
	                this.destNameComponents.profSuffixComp,
	                this.srcName.profSuffix);
}

/**
 * Copies the value from the source field to the destination field.
 * If there is not a source field, then copy from the source value.
 */
DonNameCopier.prototype.copyField = function (_srcComp, _destComp, _srcValue)
{
	if (_destComp) {
		var value = _srcComp ? _srcComp.get() : _srcValue;

		if (value)
			_destComp.set (value);
	}
}

/**
 * Email objects hold the components of email values.
 */
function Email (_email)
{
	this.email = _email;
}

/**
 * EmailComponents objects hold observable components for email field inputs.
 */
function EmailComponents (_emailCompId)
{
	this.emailComp = get_observable_component (_emailCompId);
}

/**
 * DonEmailCopier is an observer of observable components.
 * Its purpose is to copy one set of email fields into another.
 */
function DonEmailCopier (_srcEmail, _srcEmailComps, _destEmailComps)
{
	this.srcEmail            = _srcEmail;
	this.srcEmailComponents  = _srcEmailComps;
	this.destEmailComponents = _destEmailComps;
}

/**
 * This is the observe method that makes this class a valid observer.
 * @param _event is an ObservableComponentEvent.
 */
DonEmailCopier.prototype.observe = function (_event)
{
	if (!_event.component || !filter_values_equal (_event.component.get(), true)) return;

	this.copyField (this.srcEmailComponents.emailComp,
	                this.destEmailComponents.emailComp,
	                this.srcEmail.email);
}

/**
 * Copies the value from the source field to the destination field.
 * If there is not a source field, then copy from the source value.
 */
DonEmailCopier.prototype.copyField = function (_srcComp, _destComp, _srcValue)
{
	if (_destComp) {
		var value = _srcComp ? _srcComp.get() : _srcValue;

		if (value)
			_destComp.set (value);
	}
}

/**
 * Phone objects hold the components of email values.
 */
function Phone (_phone)
{
	this.phone = _phone;
}

/**
 * PhoneComponents objects hold observable components for phone field inputs.
 */
function PhoneComponents (_phoneCompId)
{
	this.phoneComp = get_observable_component (_phoneCompId);
}

/**
 * DonPhoneCopier is an observer of observable components.
 * Its purpose is to copy one set of phone fields into another.
 */
function DonPhoneCopier (_srcPhone, _srcPhoneComps, _destPhoneComps)
{
	this.srcPhone            = _srcPhone;
	this.srcPhoneComponents  = _srcPhoneComps;
	this.destPhoneComponents = _destPhoneComps;
}

/**
 * This is the observe method that makes this class a valid observer.
 * @param _event is an ObservableComponentEvent.
 */
DonPhoneCopier.prototype.observe = function (_event)
{
	if (!_event.component || !filter_values_equal (_event.component.get(), true)) return;

	this.copyField (this.srcPhoneComponents.phoneComp,
	                this.destPhoneComponents.phoneComp,
	                this.srcPhone.phone);
}

/**
 * Copies the value from the source field to the destination field.
 * If there is not a source field, then copy from the source value.
 */
DonPhoneCopier.prototype.copyField = function (_srcComp, _destComp, _srcValue)
{
	if (_destComp) {
		var value = _srcComp ? _srcComp.get() : _srcValue;

		if (value)
			_destComp.set (value);
	}
}
