$(function(){
	// copy billing address to shipping address when checked,
	// clear shipping address when unchecked
	$('#shipping-copy').change(function(){
		var fields = ['street1', 'street2', 'city', 'state', 'zip', 'country'];
	
		if($(this).is(':checked')){
			for(f in fields){ 
				$('#shipping-' + fields[f]).val( $('#billing-' + fields[f]).val() ).attr('disabled','true');
			}		
		} else {
			for(f in fields){ $('#shipping-' + fields[f]).removeAttr('disabled'); }
		}
	});
	
	// watch billing address for changes while copy is checked
	$('input[name^="billing-"], #billing-country').change(function(){
		if($('#shipping-copy').is(':checked')) {
		    billinput = $('#shipping-' + this.id.substr(8));
			$(billinput).val($(this).val());
			$(billinput).parent().removeClass('erroroutline');
		}
	});
	
	// make sure email values match
	$('#newacct-email2').keyup(function(){
		$(this).val() != $('#newacct-email').val() ? $(this).parent().addClass('erroroutline') : $(this).parent().removeClass('erroroutline');
	});
	
	// make sure email values match
	$('#newacct-password2').keyup(function(){
		$(this).val() != $('#newacct-password').val() ? $(this).parent().addClass('erroroutline') : $(this).parent().removeClass('erroroutline');
	});
	

	// validate submission
	$('#newacct').submit(function(){
		// re-enable disabled fields before submit to make sure they are passed
		var copyBox = $('#shipping-copy');
		if(copyBox.is(':checked')) copyBox.attr('checked', false).change();
	
		var problems = '';
		var emptyFields = 0;
	
		$('#newacct input[type="text"], #newacct input[type="password"], #newacct select').not('#newacct-organization, #newacct-email2, #newacct-password2, #shipping-street2, #billing-street2' ).each(function(){
			if($(this).val() == ''){
				emptyFields++;
				$(this).parent().addClass('erroroutline');
			}
		});
				
		if(emptyFields) problems += '- The outlined fields are required.\n';
		
		// make sure email and password values match
		
		if($('#newacct-email').val() != $('#newacct-email2').val()) {
			problems += '- Email values do not match.\n';
			$('#newacct-email2').val() != $('#newacct-email').val() ? $('#newacct-email2').parent().addClass('erroroutline') : $('#newacct-email2').parent().removeClass('erroroutline');
		};
		
		if($('#newacct-password').val() != $('#newacct-password2').val()) {
		    problems += '- Password values do not match.\n';
    		$('#newacct-password2').val() != $('#newacct-password').val() ? $('#newacct-password2').parent().addClass('erroroutline') : $('#newacct-password2').parent().removeClass('erroroutline');
	    };
		
		
		if(problems.length){
			alert('Please resolve the following problems:\n' + problems);
			return false;		
		}else{
			return true;
		}
	});
	
	// get rid of red outline when values are entered
	$('#newacct input[type="text"], #newacct input[type="password"], #newacct select').not('#newacct-organization, #newacct-email2, #newacct-password2, #shipping-street2, #billing-street2').change(function(){
		if($(this).val() != '') $(this).parent().removeClass('erroroutline');
	});
});

