/*
FormFieldSync Class.
Sync Data values between specified Form Fields.
requires MooTools 1.2
author: Cornel Boudria
company: 3PRIME, LLC.
*/

//
// FormFieldSync Definition
// 
var FormFieldSync = new Class({
	Implements: Options,

	options: {
		source: {fields:[], property:'value',separator:''},
		target: {fields:[], property:'value'},
		handle: {field:null, property:'value',propertyFlagValue:true}
	},

	initialize: function(options) {
		this.setOptions(options);

		for(i=0; i< this.options.source.fields.length; i++ ) {
			src = this.options.source.fields[i];
			this.setSync(src);
		};

		// Initialize the Handle
		this.initHandle(this.options.handle.field);

	},

	initHandle: function(hdl) {
		$(hdl).addEvent('click', this.sync.bindWithEvent(this));
		$(hdl).addEvent('keyup', this.sync.bindWithEvent(this));
	},

	setSync: function(srcElement) {
		$(srcElement).addEvent('keyup', this.sync.bindWithEvent(this));
		$(srcElement).addEvent('blur', this.sync.bindWithEvent(this));
	},

	hasSyncFlag: function() {
		hdl_element      = $(this.options.handle.field);
		propertyName     =   this.options.handle.property;
		propFlagValue    =   this.options.handle.propertyFlagValue;
		currentPropValue =   hdl_element[propertyName];

		return (currentPropValue == propFlagValue);
	},

	sync: function() {
		if( !this.hasSyncFlag() ) return;

		// Compile the Source Value
		src_values = [];
		propertyName = this.options.source.property;
		glue = this.options.source.separator;
		this.options.source.fields.each( function(src_field_ref) {
			src_values[src_values.length] = $(src_field_ref)[propertyName];
		});
		src_value = src_values.join(glue);

		this.options.target.fields.each( function(target_field_ref) {
			$(target_field_ref)[propertyName] = src_value;
		});
	}
});

//
// Specific Implementation for /auto-carrier-order.php
// client: PatriotAutoCarrier.com
//
window.addEvent('domready', function() {
	customer_section_fields_ids = ['first_name','last_name','company_name','email','phone','phone_2','phone_cell','phone_fax','address','address2','city','state_code','zip'];
	pickup_section_fields_ids   = ['pickup_contact','pickup_company_name','pickup_buyer_number','pickup_phone','pickup_phone_2','pickup_phone3','pickup_phone_cell','pickup_contact_address','pickup_contact_address2','pickup_contact_city','pickup_contact_state_code','pickup_contact_zip'];
	dropoff_section_fields_ids  = ['dropoff_contact','dropoff_company_name','dropoff_phone','dropoff_phone_2','dropoff_phone3','dropoff_phone_cell','dropoff_contact_address','dropoff_contact_address2','dropoff_contact_city','dropoff_contact_state_code','dropoff_contact_zip'];

	//
	// PICUKP Section Sync w/Customer Info.
	//
	pickup_section_fields_ids.each( function(field_id) {
		srcElements = {fields:[], separator:" "};
		targetElement = {fields:[$(field_id)]};
		handleElement = {field:$('pickup_contact_id'),property:'checked'};

		if( field_id == "pickup_contact" ) {
			// The field is a contact name field and requires concatenating the first_name and last_name fields
			srcElements.fields[srcElements.fields.length] = $('first_name');
			srcElements.fields[srcElements.fields.length] = $('last_name');
		}
		else {
			search_id = field_id.replace(/^pickup_(contact_)?/,"");

			if( !$(search_id) ) return; // If there is no element match, then return out of function

			srcElements.fields[srcElements.fields.length] = $(search_id);
		}

		// Create a Sync Instance
		new FormFieldSync({source: srcElements, target:targetElement, handle:handleElement});
	});

	//
	// DELIVERY Section Sync w/Customer Info.
	//
	dropoff_section_fields_ids.each( function(field_id) {
		srcElements = {fields:[], separator:" "};
		targetElement = {fields:[$(field_id)]};
		handleElement = {field:$('dropoff_contact_id'),property:'checked'};

		if( field_id == "dropoff_contact" ) {
			// The field is a contact name field and requires concatenating the first_name and last_name fields
			srcElements.fields[srcElements.fields.length] = $('first_name');
			srcElements.fields[srcElements.fields.length] = $('last_name');
		}
		else {
			search_id = field_id.replace(/^dropoff_(contact_)?/,"");

			if( !$(search_id) ) return; // If there is no element match, then return out of function

			srcElements.fields[srcElements.fields.length] = $(search_id);
		}

		// Create a Sync Instance
		new FormFieldSync({source: srcElements, target:targetElement, handle:handleElement});
	});
});
