
function autoFill(inputId,defVal){
	var chosenField = $("input#" + inputId);

	chosenField.focus(function(){
		if(chosenField.attr("value") == defVal){
			chosenField.attr("value","");
		}	
	});

	chosenField.blur(function(){
		if(chosenField.attr("value") == ""){
			chosenField.attr("value",defVal);
		}												 
	});
}

function limitChars(textclass, limit, outputclass){
 var text = $('.'+textclass).val(); 
 var textlength = text.length;
 if(textlength > limit){
		$('.'+outputclass+' span').html('0');
	 $('.'+textclass).val(text.substr(0,limit));
	 return false;
 } else {
	 $('.'+outputclass+' span').html((limit - textlength));
	 return true;
 }
}

$(document).ready(function() {

	autoFill("caseNo","number");
	autoFill("cbName","name");
	autoFill("cbPhone","phone");
	autoFill("cbEmail","email");
	
	$('.limit').keyup(function(){
		limitChars('limit', 1000, 'limitDisplay');
	})
	
	$(".otherHidden").css("display","none"); 
	$("#whereFound").change(function(){
		var selected = $("#whereFound option:selected")
		var selectedVal = selected.val();
		if(selectedVal == "Other"){
			$(".otherHidden").slideDown();	
		}else{
			$(".otherHidden").slideUp(function(){
			});
		}
	});
	if(!$("#rentArrears").is(':checked')){
		$("#rentArrearsSection").css("display","none"); 
		$("#rentDueAmount").removeClass("required");
		$("#rentDueTotal").removeClass("required");
	}
	$("#rentArrears").click(function(){
		if($(this).is(':checked')){
			$("#rentArrearsSection").slideDown();
			$("#rentDueAmount").addClass("required");
			$("#rentDueTotal").addClass("required");
		}else{
			$("#rentArrearsSection").slideUp();
			$("#rentDueAmount").removeClass("required");
			$("#rentDueTotal").removeClass("required");
		}
	});
	
	if(!$("#rentDueMonthly").is(':checked')){
		$(".rentDueDay").css("display","none"); 
	}
	$(".rentDue").click(function(){
		if($("#rentDueMonthly").is(':checked')){
			$(".rentDueDay").slideDown();
		}else{
			$(".rentDueDay").slideUp();
		}
	});
	
	if($("select#notice option:selected").val() == "No"){
		$("#noticeConditional").hide();
		$("#mainInfo").hide();
		$("#noNotice").show();								
	}
	
	$("select#notice").change(function(){
		if($("select#notice option:selected").val() != "Yes"){
			$("#noticeConditional").fadeOut();
			$("#mainInfo").fadeOut(function(){
				$("#noNotice").fadeIn();								
			});
		}else{
			$("#noNotice").fadeOut(function(){
				$("#noticeConditional").fadeIn();
				$("#mainInfo").fadeIn();												
			});	
		}
	});
	
	// Handle rent arrears table
	$("a#addRow").click(function(){
		var numRows = $("table#rentArrears tr").length;
		$("table#rentArrears").append("<tr><td><input name='date-"+numRows+"' class='input' id='date-"+numRows+"' type='text' /></td><td><span>&pound;</span><input name='amount-"+numRows+"' class='input number' id='amount-"+numRows+"' type='text' /></td><td><input name='dateReceived-"+numRows+"' class='input' id='dateReceived-"+numRows+"' type='text' /></td><td><span>&pound;</span><input name='amountReceived-"+numRows+"' class='input number' id='amountReceived-"+numRows+"' type='text' /></td><td><span>&pound;</span><input name='accumulative-"+numRows+"' class='input number' id='accumulative-"+numRows+"' type='text' /></td></tr>");
	});
	
	// Handle news excerpts
	/*
	$(".newsContent").excerpt();
	$(".newsEntry p.showMore a").click(function(){
		$(this).parent()												
	});
	*/
	
	
	$("#callbackForm").validate();
	$("#trackcaseForm").validate();
	$("#contactForm").validate();
	$("#councilLogin").validate();
	$("#councilInquiry").validate();
	$("#orderForm").validate();
	$("#step1Form").validate();
	$("#step2Form").validate();
	$("#benefitCampaign").validate();
	$("#LAPack").validate();
	
});