// Layers Magazine Subscription Page

// Hover and Active styles for field items
var fieldfun = {
	init : function() {
		
		var labels = $('#subscribe label')
		var inputs = $('#subscribe :input');
		var format = $('format_select');
		var country = $('country');
		
		$('#country').change( function(){
			this.value != 'USA' && this.value != 'Canada' ? $('#format_select').show() : $('#format_select').hide();
		});
		
		$(labels).hover( function(){ $(this).addClass('hover'); }, function(){ $(this).removeClass('hover'); });
		
		$(inputs).focus( function(){ $(this).parent().addClass('active'); } )
		$(inputs).blur( function(){ $(this).parent().removeClass('active'); } );
	}
}
$(document).ready(fieldfun.init);

// Address copy
var fieldcopy = {
	originals : 0,
	copies : 0,
	init : function() {
		var copy_button = $('#copy');
		fieldcopy.originals = $('#information :input');
		$(copy_button).click(fieldcopy.toggle);
	},
	toggle: function(e) {
		var pre = 'bill_';
		if( this.checked == true ) {
			for( var i = 0, original; original = fieldcopy.originals[i]; i++ ) {
				var to_copy = '#' + pre + original.id;
				if( $(to_copy) )
					$(to_copy).val(original.value);
			}
		} else {
			for( var i = 0, original; original = fieldcopy.originals[i]; i++ ) {
				to_copy = '#' + pre + original.id;
				if( $(to_copy) )
					$(to_copy).val('');
			}
		}
	}
}
$(document).ready(fieldcopy.init);

// Submit button hover
var button = {
	init : function() {
		var the_button = $('#go_button');
		$(the_button).hover(button.over,button.out);
	},
	over : function() {
		$(this).addClass('hover');
		$(this).parent().addClass('hover');
	},
	out : function() {
		$(this).removeClass('hover');
		$(this).parent().removeClass('hover');
	}
}
$(document).ready(button.init);

// Check Submission Values
var check = {
	form : null,
	init : function() {
		this.form = $('#subscribe');
		var go_button = $('#go_button').click(check.process);
	},
	process : function() {
		var labels = $('#subscribe label');
		var fields = $('#subscribe :input');
		var error = null;
		
		$(fields).each( function(){
			if( $(this).parent().hasClass('req') ) {
				if( this.value.length <= 0 ) {
					$(this).parent().addClass('err');
					error = 'Please Fill in All Required Fields!';
				} else if( this.value.length > 0) {
					$(this).parent().removeClass('err');
					error = '';
				}
			}
		});
		if( error.length > 0 )
			$('#err_message').html(error);
		else
			check.form.submit();
		return false;
		
	}
}
$(document).ready(check.init);
