
var selectedBranch = '';
var branchHint = 'Please type the branch or area name.';

$(document).ready(function() {

	
	//validate form
	$('#landlordForm').validate({
		rules: {
			first_name: {
				required: true,
				minlength: 3
			},
			surname: {
				required: true,
				minlength: 3
			},
			contact_tel: {
				required: true,
				minlength: 10
			},
			email: {
				required: true,
				email: true,
				minlength: 5
			},
			branch: {
				required: true
			},
			address: {
				required: true,
				minlength: 3
			}
		},
		messages: {
			first_name: {
				required: '<br>Please fill in your first name.',
				minlength: $.format('<br>At least {0} characters required!')
			},
			surname: {
				required: '<br>Please fill in your last name.',
				minlength: $.format('<br>At least {0} characters required!')
			},
			contact_tel: {
				required: '<br>Please fill in your contact number.',
				minlength: $.format('<br>At least {0} characters required!')
			},
			email: {
				required: '<br>Please fill in your email address.',
				minlength: $.format('<br>At least {0} characters required!')
			},
			branch: {
				required: '<br>Please select a branch to contact'
				
			},
			address: {
				required: '<br>Please fill in your address.',
				minlength: $.format('<br>At least {0} characters required!')
			}
		}
	});
	
	
	
	$("#branch").autocomplete(
		branchData,
		{
			delay:1,
			minChars:1,
			max: 50,
			cacheLength:50,
			matchSubset: true,
			matchContains: true,
			selectFirst:true,
			delay:1,
			formatItem: function(row, i, max) {
				return row.name;
			},
			formatMatch: function(row, i, max) {
				return row.name;
			},
			formatResult: function(row) {
				return row.name;
			}
		}
	);

	$('#branch').result(function(event, data, formatted) {
            $('#branchId').attr("value",data.id);
            selectedBranch = data.name;
    });
	
	function defaultBranchHint(){
		if ( isset( $('#branch').fieldValue()[0] ) && ( $('#branch').fieldValue()[0] == '')  ) {
			$('#branch').attr('value',branchHint);
		}	
	}//end defaultAreaAutocompleteHint
	
	
	function clearBranchHint(){
		if ( isset( $('#branch').fieldValue()[0] ) && ( trim($('#branch').fieldValue()[0]) == branchHint ) ) {
			$('#branch').attr('value','');
		}
			
	}//end clearLocationHint
	

	$('#branch').focus(function(){ clearBranchHint() });	
	$('#branch').blur(function(){ defaultBranchHint() });
	
	defaultBranchHint();


});


