/**
 * Manager Address Form
 * Dependencies: Mootools v1.1+
 * @author Kevin Dew <kev@dewsolutions.co.uk>
 * @copyright Copyright Kevin Dew, 2007
 */

window.addEvent('domready', function()
{
        //function to make billing details match company
        function updateBillingDetails()
        {
                if($('c_as_b') && $('c_as_b').checked)
                {
                        $('b_name').value = $('c_name').get('value');
                        $('b_address_1').value = $('c_address_1').get('value');
                        $('b_address_2').value = $('c_address_2').get('value');
                        $('b_address_3').value = $('c_address_3').get('value');
                        $('b_address_4').value = $('c_address_4').get('value');
                        $('b_address_5').value = $('c_address_5').get('value');
                        $('b_town').value = $('c_town').get('value');
                        $('b_county').value = $('c_county').get('value');
                        $('b_postcode').value = $('c_postcode').get('value');
                        $('b_country').value = $('c_country').get('value');
                }
        }
        
        //add on change events
        $each(['c_name', 'c_address_1', 'c_address_2', 'c_address_3', 'c_address_4', 'c_address_5', 'c_town', 'c_county', 'c_postcode', 'c_country'],
        function(id)
        {
                if($(id))
                        $(id).addEvent('change', updateBillingDetails);
        });

        //checkbox event
        if($('c_as_b'))
                $('c_as_b').addEvent('click', updateBillingDetails);
                
        //billing on change events
        $each(['b_name', 'b_address_1', 'b_address_2', 'b_address_3', 'b_address_4', 'd_address_5', 'b_town', 'b_county', 'b_postcode', 'b_country'],
        function(id)
        {
                if($(id))
                        $(id).addEvent('change',
                        function()
                        {
                                if($('c_as_b') &&
                                   ($('b_name').get('value').trim() != $('c_name').get('value').trim() ||
                                   $('b_address_1').get('value').trim() != $('c_address_1').get('value').trim() ||
                                   $('b_address_2').get('value').trim() != $('c_address_2').get('value').trim() ||
                                   $('b_address_3').get('value').trim() != $('c_address_3').get('value').trim() ||
                                   $('b_address_4').get('value').trim() != $('c_address_4').get('value').trim() ||
                                   $('b_address_5').get('value').trim() != $('c_address_5').get('value').trim() ||
                                   $('b_town').get('value').trim() != $('c_town').get('value').trim() ||
                                   $('b_county').get('value').trim() != $('c_county').get('value').trim() ||
                                   $('b_postcode').get('value').trim() != $('c_postcode').get('value').trim() ||
                                   $('b_country').get('value').trim() != $('c_country').get('value').trim())
                                  )
                                        $('c_as_b').checked = false;

                        });
        });        

});
