
$(function() {
	$('input,select,textarea').focusin(function(e) {
		$(this).addClass('user-input');
		if($(this).val() == $(this).attr('default')) {
			$(this).val('');
		}
	});
	$('input,select,textarea').focusout(function(e) {
		var newVal = $.trim($(this).val());
		if(newVal == '' || newVal == $(this).attr('default')) {
			$(this).removeClass('user-input');
			$(this).val($(this).attr('default'));
		} else {
			$(this).removeClass('missing-value');
		}
	});
	
	$('input,select,textarea').each(function() {
		if($(this).attr('default')){
			$(this).val($(this).attr('default'));
		}
	});
	
	$('#connect-form').submit(function(e){
		var newVal,readyToGo=true;
		
		e.stopPropagation();
		e.preventDefault();
		
		$('#connect-form input[type="text"]').removeClass('missing-value');
		$('#connect-form input[type="text"]').each(function() {
			newVal = $.trim($(this).val());
			if(newVal == '' || newVal == $(this).attr('default')) {
				$(this).addClass('missing-value');
				$(this).val($(this).attr('default'));
				readyToGo = false;
			}
		});
		
		if(readyToGo){
			$.ajax({
				url: $(this).attr('action'),
				type: 'POST',
				data: $(this).serialize(),
				success: function(response) {
					if(response.status == 'success') {						
						$('#connect-content').fadeOut(200,function() {
							$('#connect-submit').fadeIn(200);
						});
					} else {
						$('#connect-box .error-message')
							.html(response.message)
							.fadeIn(200);
					}
				},
				error: function(response) {
				}
			});
		} else {
			$('#connect-box .error-message')
				.html("Please complete the required fields.")
				.fadeIn(200);
		}
	});
	
	$('#submit').click(function(e) {
		e.stopPropagation();
		
		$('#errorMessages').html('');
		$('.formItem').each(function() {
			$(this).css('background-color', '#fff');
		});
		
		messages = '';
		msgs = '';
		$('.formItem input, .formItem textarea').each(function() {
			$this = $(this);
			
			if( $this.attr('required') &&
				! $this.attr('value') ) {
				label = $($this.parent().children('label')[0]).html();
				
				msgs += (label ? label : $this.attr('name')) + '<br />';
				$this.parent().css('background-color', '#dbb');
			}
		});
		
		if(msgs != '') {
			messages = 'The following required fields were missing values:<br />' + msgs + '<br />';
		}			
		
		msgs = '';
		$('.formItem input, .formItem textarea').each(function() {
			$this = $(this);
			
			if($this.attr('verify')) {
				id = $this.attr('verify');
				
				$target = $('#'+id);
				val1 = $this.attr('value').replace(/^\s+|\s+$/g,"");
				val2 = $target.attr('value').replace(/^\s+|\s+$/g,"");
				
				if(val1 != val2) {
					label = $($target.parent().children('label')[0]).html();
					
					msgs += (label ? label : $this.attr('name')) + '<br />';
					$this.parent().css('background-color', '#dbb');
					$target.parent().css('background-color', '#dbb');
				}
			}
		});
		
		if(msgs != '') {
			messages += 'The following fields must be properly verified:<br />' + msgs;
		}			
		
		if(messages != '') {
			$('#errorMessages').html(messages);
			$('#errorMessages').show();
			
			e.preventDefault();
			return false;
		} else {
			$('#errorMessages').hide();
		}
		
		return true;
	});

});
