// Miva Merchant
//
// This file and the source codes contained herein are the property of
// Miva, Inc.  Use of this file is restricted to the specific terms and
// conditions in the License Agreement associated with this file.  Distribution
// of this file or portions of this file for uses not covered by the License
// Agreement is not allowed without a written agreement signed by an officer of
// Miva, Inc.
//
// Copyright 1998-2019 Miva, Inc.  All rights reserved.
// http://www.miva.com
//

function ToggleDetails_Class()
{
	this.primary_fields		= null;
	this.secondary_fields	= null;
	this.primary_tag		= '';
	this.secondary_tag		= '';
	this.trigger			= null;
	this.controls			= null;
	this.shipping_selector	= null;
	this.billing_selector	= null;
	this.active_selector	= null;
	this.primary_inputs		= [];
	this.primary_selects	= [];
	this.secondary_inputs	= [];
	this.secondary_selects	= [];
}

ToggleDetails_Class.prototype.Initialize = function()
{
	var self = this;

	this.event_copyfields		= function() { self.CopyFields(); };
	
	this.shipping_selector		= document.getElementById( 'shipping_selector' );
	this.billing_selector		= document.getElementById( 'billing_selector' );

	
	this.primary_fields			= document.getElementById( 'shipping_fields' );
	this.secondary_fields		= document.getElementById( 'billing_fields' );
	this.trigger				= document.getElementById( 'billing_to_show' );
	this.controls				= document.getElementById( 'billing_controls' );
	this.active_selector		= document.getElementById( 'billing_selector' );
	this.primary_tag			= 'Ship';
	this.secondary_tag			= 'Bill';
	this.primary_inputs			= this.primary_fields.getElementsByTagName( 'input' );
	this.primary_selects		= this.primary_fields.getElementsByTagName( 'select' );
	this.secondary_inputs		= this.secondary_fields.getElementsByTagName( 'input' );
	this.secondary_selects		= this.secondary_fields.getElementsByTagName( 'select' );

	if ( this.shipping_selector && this.billing_selector )
	{
		this.AddEvent( this.shipping_selector, 	'change', 	function() { self.SetFields( self.primary_inputs, self.primary_selects, self.primary_tag, self.shipping_selector.options[ self.shipping_selector.selectedIndex ].value ); } );
		this.AddEvent( this.billing_selector, 	'change', 	function() { self.SetFields( self.secondary_inputs, self.secondary_selects, self.secondary_tag, self.billing_selector.options[ self.billing_selector.selectedIndex ].value ); } );
	}
	

	this.AddEvent( this.trigger, 'click', function() { self.Go(); } );

	this.controls.style.visibility = 'visible';

	if ( this.trigger.checked )
	{
		this.AddEvent( this.trigger.form, 'submit', this.event_copyfields );
		this.secondary_fields.style.display	= 'none';

		if ( this.active_selector ) this.active_selector.style.visibility = 'hidden';
	}
}

ToggleDetails_Class.prototype.Go = function()
{
	if ( this.trigger.checked )
	{
		this.AddEvent( this.trigger.form, 'submit', this.event_copyfields );
		this.secondary_fields.style.display = 'none';

		if ( this.active_selector ) this.active_selector.style.visibility = 'hidden';
	}
	else
	{
		this.RemoveEvent( this.trigger.form, 'submit', this.event_copyfields );
		this.secondary_fields.style.display = '';

		if ( this.active_selector ) this.active_selector.style.visibility = 'visible';
	}
}

ToggleDetails_Class.prototype.CopyFields = function()
{
	var i, member, name_swap, primary_fields, primary_select_fields;

	primary_fields			= new Object();
	primary_select_fields	= new Object();

	for ( i = 0; i < this.primary_inputs.length; i++ )
	{
		name_swap = this.primary_inputs[ i ].name.replace( this.primary_tag, this.secondary_tag );

		primary_fields[ name_swap ] 		= new Object();
		primary_fields[ name_swap ].value 	= this.primary_inputs[ i ].value;
		
		if ( this.primary_inputs[ i ].checked )	primary_fields[ name_swap ].checked = true;
		else									primary_fields[ name_swap ].checked = false;
	}

	for ( i = 0; i < this.primary_selects.length; i += 1 )
	{
		name_swap = this.primary_selects[ i ].name.replace( this.primary_tag, this.secondary_tag );

		primary_select_fields[ name_swap ] 					= new Object();
		primary_select_fields[ name_swap ].selectedIndex 	= this.primary_selects[ i ].selectedIndex;
	}

	for ( member in primary_fields )
	{
		if ( this.trigger.form[ member ] )
		{
			this.trigger.form[ member ].value	= primary_fields[ member ].value;
			this.trigger.form[ member ].checked	= primary_fields[ member ].checked;
		}
	}
	
	for ( member in primary_select_fields )
	{
		this.trigger.form[ member ].selectedIndex = primary_select_fields[ member ].selectedIndex;
	}
}

ToggleDetails_Class.prototype.SetFields = function( input_fields, select_fields, tag, addr_id )
{
	var state_found;
	var activeaddress;
	var i, j, i_len, j_len;
	var active_input_fields;
	var active_select_fields;

	activeaddress = ( typeof MvCustomerAddresses !== 'undefined' ) && MvCustomerAddresses[ addr_id ] ? MvCustomerAddresses[ addr_id ] : new Object();

	for ( i = 0, i_len = select_fields.length; i < i_len; i++ )
	{
		if ( select_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'StateSelect' )
		{
			select_fields[ i ].selectedIndex = 0;
			
			for ( j = 0, j_len = select_fields[ i ].options.length; j < j_len; j++ )
			{
				if ( select_fields[ i ].options[ j ].value == activeaddress.state )
				{
					select_fields[ i ].selectedIndex = j;
					state_found = true;
					break;
				}
			}
		}
		else if ( select_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'Country' )
		{
			select_fields[ i ].selectedIndex = 0;

			for ( j = 0, j_len = select_fields[ i ].options.length; j < j_len; j++ )
			{
				if ( select_fields[ i ].options[ j ].value == activeaddress.cntry )
				{
					select_fields[ i ].selectedIndex = j;
					break;
				}
			}
		}
	}

	for ( i = 0, i_len = input_fields.length; i < i_len; i++ )
	{
		if ( input_fields[ i ].name.replace( 'Customer_', '' )		== tag + 'Description' )	input_fields[ i ].value = activeaddress.descrip || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' )	== tag + 'FirstName' )		input_fields[ i ].value = activeaddress.fname || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'LastName' )		input_fields[ i ].value = activeaddress.lname || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'Email' )			input_fields[ i ].value = activeaddress.email || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'Phone' )			input_fields[ i ].value = activeaddress.phone || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'Fax' )			input_fields[ i ].value = activeaddress.fax || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'Company' )		input_fields[ i ].value = activeaddress.comp || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'Address1' )		input_fields[ i ].value = activeaddress.addr1 || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'Address2' )		input_fields[ i ].value = activeaddress.addr2 || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'City' )			input_fields[ i ].value = activeaddress.city || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'Zip' )			input_fields[ i ].value = activeaddress.zip || '';
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'Residential' )	input_fields[ i ].value = activeaddress.resdntl || 0;
		else if ( input_fields[ i ].name.replace( 'Customer_', '' ) == tag + 'State' )		
		{
			input_fields[ i ].value = '';

			if ( !state_found )
			{
				input_fields[ i ].value = activeaddress.state || '';
			}
		}
	}
}

ToggleDetails_Class.prototype.AddEvent = function( obj, eventType, fn  )
{
	if ( obj.addEventListener )
	{
		obj.addEventListener( eventType, fn, false );
		return true;
	}
	else if ( obj.attachEvent )
	{
		var r = obj.attachEvent( 'on' + eventType, fn );
		return r;
	}
	else
	{
		return false;
	}
}

ToggleDetails_Class.prototype.RemoveEvent = function ( obj, type, fn )
{
	if ( obj.removeEventListener )
	{
		obj.removeEventListener( type, fn, false );
	}
	else if ( obj.detachEvent )
	{
		obj.detachEvent( 'on' + type, fn );
	}
}

var ToggleDetails = new ToggleDetails_Class();
ToggleDetails.Initialize();
