(function($){
	$.fn.validate = function(options) {
		var defaults = {
			renderValid: function(){},
			renderInvalid: function(){},
			highlightError: function(){}
		};
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			var obj = $(this);
			var actionUrl = obj.attr("action");
			var currentErrorField = '';			
			
			$("input[type=text], textarea", obj).blur(function(){
				var field = $(this);
				var fieldName = field.attr('name');
				var fieldValue = field.val();
				
				$.post(
					actionUrl + '?nmode=validate&nport=' + fieldName, 
					fieldName + "=" + fieldValue, 
					function(data){
						if(data[0]){ // invalid
							if(!field.hasClass("nephtaliInvalid")){
								field.addClass("nephtaliInvalid");
							}
							options.renderInvalid(field, data[0][1]);
							if(currentErrorField.length == 0){
								currentErrorField = fieldName;
								options.highlightError(field);
							}
						}else{       // valid
							options.renderValid(field);
						}
					}, 
					"json"
				);
			}).focus(function(){
				if($(this).hasClass("nephtaliInvalid")){
					options.highlightError($(this));
					currentErrorField = $(this).attr("name");
				}
			});
		});
	};
})(jQuery);

$(function(){
	$('#project_budget input.share_item, #project_budget input.grant_item,').blur(function(){
		if ($.trim($(this).val()).length == 0)
		{
			$(this).val('0');
		}																																										 
		
		total_request = 0;
		total_cost_share = 0;
		
		$("#project_budget input.grant_item").each(function () {
				var amount = ($(this).val().length > 0) ? parseInt($(this).val()) : 0;
				total_request += amount;
		});	
		$('#total_request').val(total_request);
		
		$("#project_budget input.share_item").each(function () {
				var amount = ($(this).val().length > 0) ? parseInt($(this).val()) : 0;
				total_cost_share += amount;
		});	
		$('#total_cost_share').val(total_cost_share);
		
		if (total_cost_share < total_request)
		{
			$('#total_cost_share').css({'background-color':'#fbb'});
			var error_text = '<br /><strong class="status_alert">The grant request cannot exceed the cost share.  You can reduce the grant or add hours to the cost share column.</strong>';
			$('#error_feedback').html(error_text);
			$('#submit_app').hide();
		}else if (total_request > max_cost)
		{
			$('#total_request').css({'background-color':'#bfb'});
			$('#total_request').css({'background-color':'#fbb'});
			var error_text = '<br /><strong class="status_alert">The grant request cannot exceed $' + max_cost + '</strong>';
			$('#error_feedback').html(error_text);
			$('#submit_app').hide();
		}
		else
		{
			$('#error_feedback').html("");
			$('#total_request').css({'background-color':'#bfb'});
			$('#total_cost_share').css({'background-color':'#bfb'});
			$('#submit_app').show();
		}
		
		$('#project_staff_total_cost').val(totalRow('project_staff'));
		$('#planner_total_cost').val(totalRow('planner'));
		$('#personnel_total_cost').val(totalRow('personnel'));
		$('#humanities_professional_total_cost').val(totalRow('humanities_professional'));
		$('#travel_total_cost').val(totalRow('travel'));
		$('#promotion_total_cost').val(totalRow('promotion'));
		$('#printing_total_cost').val(totalRow('printing'));
		$('#postage_total_cost').val(totalRow('postage'));
		$('#telephone_total_cost').val(totalRow('telephone'));
		$('#supplies_total_cost').val(totalRow('supplies'));
		$('#rental_total_cost').val(totalRow('rental'));
		$('#other_total_cost').val(totalRow('other'));
		$('#total_cost').val(totalRow('total'));

	});

					 
					 
					 
	$("input,textarea").focus(function(){
		$(this).parent().addClass("focused");
	}).blur(function(){
		$(this).parent().removeClass("focused");
	});
	$("form.submission").validate({
		renderInvalid: function(field, errorMsg){
			$("#" + field.attr('name') + "Div").remove();
			field.after("<div class='invalid' id='" + field.attr('name') + "Div'>&nbsp;<div class='displayContainer'><div class='messageWrapper'><div class='errorText'>" + errorMsg + "</div></div><div class='bottom'></div></div></div>").parent().addClass("invalid");
			$("#" + field.attr("name") + "Div div.displayContainer").hide();
		},
		renderValid: function(field){
			$("#" + field.attr('name') + "Div").remove();
			field.after("<div class='valid' id='" + field.attr('name') + "Div'>&nbsp;</div>").parent().removeClass("invalid").addClass("valid");
			$("#" + field.attr("name") + "Div  div.displayContainer").hide();
		},
		highlightError: function(field){
			// hide all error messages
			$("div.invalid div.displayContainer").hide();
			// unhide current message
			$("#" + field.attr("name") + "Div div.displayContainer").show();
		}
	});
});


// JavaScript Document
function totalRow(rowName){
	var text1 = $('#' + rowName + '_request').val();
	var text2 = $('#' + rowName + '_cost_share').val();
	text1 = $.trim(text1);
	text2 = $.trim(text2);
	var val1 = (text1.length > 0) ? (parseInt(text1)) : 0;
	var val2 = (text2.length > 0) ? (parseInt(text2)) : 0;
	return val1 + val2;
}

$(function(){
	// quick grants 500, planning 1000
	var max_cost = 500;
	var total_request = 0;
	var total_cost_share = 0;
	var total_cost = 0;
	
	$('.total_item').css({'background-color':'#bfb'});
					 
	$('#grant_type').change(function(){
		$("#grant_type option:selected").each(function () {
					var text = $(this).text();
					if (text == "Planning") {
						$('#planning_fieldset').show();
						max_cost = 1000;
						$('#max_cost').html(max_cost);
					} else {
						$('#planning_fieldset').hide();
						max_cost = 500;
						$('#max_cost').html(max_cost);
					}
		});							
	}).change();
	
	$('#professional_listing').change(function(){
		$("#professional_listing option:selected").each(function () {
					var text = $(this).text();
					if (text == "Unlisted (must manually enter info)") {
						$('#professional_info').show();
					} else {
						$('#professional_info').hide();
					}
		});							
	}).change();
	
	$('#planning_fieldset').hide();
	
	$("#professional_listing option:selected").each(function () {
				var text = $(this).text();
				if (text == "Unlisted (must manually enter info)") {
					$('#professional_info').show();
				} else {
					$('#professional_info').hide();
				}
	});							
	
}); 