function validate_address_and_gift_certificates(obj_form, model_name)
{
	validator.init();

	validator.clear_all_errors();

	//Validate gift certificates
	if(gift_cert_div = document.getElementById('gift-certificates'))
		validator.validate_all_el_required(gift_cert_div);

	$('.email-address').each(function(){
		var email_regex = /^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i;
		if(this.value != '' && !email_regex.test(this.value))
		{
			$(this).addClass(validator.error_class);
			validator.error_string += '<li><a href="#'+this.id+'">A valid Recipient Email address is Required</a></li>';
			validator.is_valid = false;
		}
	});

	//Valiate if a new address has been selected
	if($('#address_id_new').attr('checked'))
	{
		return validator.validate_form(obj_form, model_name);
	}
	else
	{
		if(validator.is_valid)
			return true;
		else
		{
			validator.show_error_messages();
			return false;
		}
	}
}

function validate_new_address(obj_form)
{
	validator.init();

	validator.clear_error_messages();

	var is_billing_address_valid = validator.validate_all_el_required(document.getElementById('new-addressbilling'));
	var is_shipping_address_valid = true;
	var is_gift_certificates_valid = true;

	//Valiate if a new address has been selected
	if($('#use-my-billing-address').attr('checked') != true)
		is_shipping_address_valid = validator.validate_all_el_required(document.getElementById('new-addressshipping'));

	if(gift_cert_div = document.getElementById('gift-certificates'))
		is_gift_certificates_valid = validator.validate_all_el_required(gift_cert_div);

	if(is_billing_address_valid && is_shipping_address_valid && is_gift_certificates_valid)
		return true;

	validator.show_error_messages();

	return false;
}

function hide_shipping_state_element()
{
    if( $('#shippingcountry_id').get(0).value == 'US' )
        $('#el_shippingstate_id').show()
    else
    {
        $('#el_shippingstate_id').hide()
        $('#shippingstate_id').get(0).value = '';
    }
}
function hide_billing_state_element()
{
    if( $('#billingcountry_id').get(0).value == 'US' )
        $('#el_billingstate_id').show()
    else
    {
        $('#el_billingstate_id').hide()
        $('#billingstate_id').get(0).value = '';
    }
}

$(document).ready(function(){
	$('#use-my-billing-address').bind('click',function(){
		$('#new-addressshipping').toggle();
	});

	$('#shippingcountry_id').bind('change',function(){
		hide_shipping_state_element();
	});
	$('#billingcountry_id').bind('change',function(){
		hide_billing_state_element();
	});


	hide_shipping_state_element();
	hide_billing_state_element();
});
