var lastShipToConfirmSelected;
var confirmedInternational=0;
var previousState='';
var selectedCountryID;
    
jQuery.fn.countryChange = function()
{
    if($(this).val() !== 'United States' && $(this).val() !== 'Virgin Islands (US)'){
        if(!$('#'+$(this).attr('tied')).attr('disabled')){
            previousState = $('#'+$(this).attr('tied')).val();
            $('#'+$(this).attr('tied')).val('');
        }
        $('#'+$(this).attr('tied')).attr('disabled','disabled');
        $('label[for="'+$(this).attr('tied')+'"]').removeClass('required');
    }else{
        $('#'+$(this).attr('tied')).removeAttr('disabled');
        $('#'+$(this).attr('tied')).val(previousState);
        $('label[for="'+$(this).attr('tied')+'"]').addClass('required');
    }
    if($(this).val() !== 'United States' 
	&& $(this).val() !== 'Virgin Islands (US)'
	&& $(this).val().length 
	&& lastShipToConfirmSelected !== $(this).val()){
        	selectedCountryID = $(this).attr('id');
        	$("#shipToConfirmModal").modal();
    }
    lastShipToConfirmSelected = $(this).val();
}

$(function(){
    //on change for pulldown trigger modal
        $('select[name="country"]').change(function(){$(this).countryChange();});
        $('select[name="chrCustomerLocationCountry"]').change(function(){$(this).countryChange();});
        
        $('#acceptUSA').live('click',function(){
            $('#'+selectedCountryID).val("United States");
            $.modal.close();
        });
        
        $('#acceptInternational').live('click',function(){
            confirmedInternational=1;
            $.modal.close();
        });
        
        // onload if default payment is international.
        if($('select[name="country"]').length != 0){
            if($('select[name="country"]').val() !== 'United States' 
		&& $('select[name="country"]').val() !== 'Virgin Islands (US)' 
		&& $('select[name="country"]').val() !== 'Canada'
		&& $('select[name="country"]').val().length){
                $('select[name="country"]').countryChange();
            }
        }
        if($('select[name="chrCustomerLocationCountry"]').length != 0){
            if($('select[name="chrCustomerLocationCountry"]').val() !== 'United States' 
		&& $('select[name="chrCustomerLocationCountry"]').val() !== 'Virgin Islands (US)' 
		&& $('select[name="chrCustomerLocationCountry"]').val() !== 'Canada'
		&& $('select[name="chrCustomerLocationCountry"]').val().length){
                $('select[name="chrCustomerLocationCountry"]').countryChange();
            }
        }
});


