(function($){
	jQuery.fn.delayThis = function(millis,callBack){
		var object = $(this);
		
		return window.setTimeout(function() {
			if(typeof callBack == 'function'){
				callBack.call(this);
			}
			
			return object;
		}, millis);
	}
})(jQuery);

(function($) {
  jQuery.fn.validate = function(callback) {
		var object = this;
		var panel = this;

	$(".required").removeClass('error');
	var req_fields = panel.find(".required[value='']");
	var req_checked = panel.find(".required:checkbox");
	var hasError = false;
	
	if (req_fields.length > 0) {
		$("fieldset", this).children(".error").fadeIn();
		req_fields.addClass('error');
		req_fields[0].focus();
		hasError = true;
		return false;
	}
	$(".group", panel).each(function() {
		if(! $("input:radio:checked",this).length > 0) {
			$(this).parents("fieldset").children(".error").fadeIn();
			hasError = true;
			return false;
		}
	});

	if(req_checked.length > 0) {
		req_checked.each(function() {		
			if(this.checked != true) {
				$(this).parents("fieldset").children(".error").fadeIn();
				hasError = true;
				return false;
			}
		});
	}

	if(hasError == false) {
		$("div.error").fadeOut();
		if(typeof callback == 'function'){
			callback.call(this);
		}
		return true;
	}
  }
})(jQuery);

function combineFields(name) {
	var textVal = "";
	var textareaVal = "";
	var value = "";
	var checked = $("input[name^=" + name + "]:checked");
	var size = $("input[name^=" + name + "][value!='']:text").size();
	$("input[name^=" + name + "][value!='']:text").each(function(idx) {
		textVal += $(this).val();
      if (idx != size-1) { 
          textVal += " - ";
      }
	});
	textareaVal = $("textarea[name^=" + name + "][value!='']").val();
	
	if(checked.size() != 0) {

			if(textVal != "") {
				value = checked.val() + ", "  + textVal;
			} else if(textareaVal != "") {
				value = checked.val() + ", "  + textareaVal;
			}
	} else if(checked.size() == 0) {
		value = textVal;	
	}
	$('input[name=' + name + '_c]').val(value);

}

$(document).ready(function() {
	
	$("#job-app .submit").click(function(){
		var hasError = true;
		if($("input[name='agree_agreement']").is(":checked")) {
			hasError = false;
		} else {
			$(".step8 .error").fadeIn();
			hasError = true;
		}
		
		if($("input[name='agree_guarantee']").is(":checked")) {
			hasError = false;
		} else {
			$(".step8 .error").fadeIn();
			hasError = true;
		}
		
		if(hasError == false) {
			$("form#application").submit();	
		}
	});
	$("#job-app .continue").click(function(){
		// Combine fields
		$("input[name$='_c']").each(function() {
			var name = $(this).attr('name');
			combineFields(name.substring(0,name.length-2));
		});
		$(this).parent().parent().validate(function() {
			$(this).fadeOut(function() {
				var panel = $(this).next();
				$("#saving").show().delayThis(500, function() {
					$("#saving").fadeOut(function() {
						panel.fadeIn();
					});
				});
			});
		});
	});
	
	$("#job-app .back").click(function(){ 
		$(this).parent().parent().fadeOut(function() {
			var panel = $(this).prev();
			$("#saving").show().delayThis(500, function() {
				$("#saving").fadeOut(function() {
					panel.fadeIn();
				});
			});
		});
	});
	
	var i = 1;
	$("#attach").click(function(){
		i++;
		$(this).before("<label for=\"file" + i + "\" class=\"file\"></label><input class=\"file\" type=\"file\" name=\"file" + i + "\" id=\"file" + i + "\"><br />")
	});
});
