window.addEvent('domready',function(){
/*/////////////////////////////////////////////////////////////////////////////////////////////
// Dynamic Country / Province Toggling
/*/////////////////////////////////////////////////////////////////////////////////////////////
    if( $('Country') ){
        $('Country').addEvent('change',function(){
            var pars = 'country_code='+$('Country').options[$('Country').selectedIndex].value;
            if ($('Country').options[$('Country').selectedIndex].value != ""){
                new Ajax( '/ajax/select_province.php', {
                    method: 'post',
                    postBody: pars,

                    update: $('ContestProvinceDiv') || $('InputProvinceDiv') || $('InputProvince')
                } ).request();
            }
        });
    }

    if( $('B_country') ){
        $('B_country').addEvent('change',function(){
            var pars = 'country_sid='+$('B_country').options[$('B_country').selectedIndex].value+'&field_name=province&field_id=B_province';
            if ($('B_country').options[$('B_country').selectedIndex].value != ""){
                new Ajax( '/ajax/select_province.php', {
                    method: 'post',
                    postBody: pars,

                    update: $('DivB_province')
                } ).request();
            }
        });
    }
    if( $('S_country') ){
        $('S_country').addEvent('change',function(){
            var pars = 'country_sid='+$('S_country').options[$('S_country').selectedIndex].value+'&field_name=s_province&field_id=S_province';
            if ($('S_country').options[$('S_country').selectedIndex].value != ""){
                new Ajax( '/ajax/select_province.php', {
                    method: 'post',
                    postBody: pars,

                    update: $('DivS_province')
                } ).request();
            }
        });
    }


/*/////////////////////////////////////////////////////////////////////////////////////////////
// This function simply copies all values in the shopping
// cart billing form over to the shopping cart shipping form.
/*/////////////////////////////////////////////////////////////////////////////////////////////

	if ($('ShoppingCartForm') && $('same_')){
	    var selects=[
    	    'country',
    	    'province'
	    ];
        var fields=[
    		  'fname',
    		  'lname',
    		  'address',
    		  'city',
    		  'postal_code',
    		  'email',
    		  'taxid',
    		  'phone'
        ];

		$('same_').addEvent('click',function(){
		    if($('same_').checked){
		        fields.each(function(inp){	$('S_'+inp).value = $('B_'+inp).value;  });
		        selects.each(function(inp){
		            for(i=$('B_'+inp).options.length-1;i>=0;i--){
		                if ($('B_'+inp).options[i].selected == true){
                            $('S_'+inp).options[i].selected = true;
		                }
		            }
		        });
		    };
		});
	};
});
