	
$(document).ready(function() {
  
	// attach an event to the search button
	$('#search-now-button').click(function() {
	  getSearchResults();
	});
	
  	//numeric only inputs
  	$('#size').keydown(maskInput);
  	$('#persons').keydown(maskInput);
  
});

function getSearchResults() {

	var serviced = $('#serviced').val();
	if(serviced == 'none')
		serviced = '';
	var size = $('#size').val();
	var persons = $('#persons').val();
	var numres = 0;
	
	$('#results-list').html('');
	
	if(serviced.length==0 && size.length==0 && persons.length==0) {

		$.each(offices, function(i, item) {
			numres++;	
			addResult(item.Name,item.URL,item.desc);	
		});

	}else{

		$.each(offices, function(i, item) {
			
			var match = true;
			
			if(serviced.length > 0) {
				if(serviced != item.serviced)
					match = false;				
			}
			
			if(size.length > 0) {	
				var isize = parseInt(size);
				var lsize = parseInt(item.lsize);
				var usize = parseInt(item.usize);
				if(isize<lsize || isize>usize)
					match = false;
			}

			if(persons.length > 0) {	
				var ipersons = parseInt(persons);
				var lpersons = parseInt(item.lpersons);
				var upersons = parseInt(item.upersons);
				if(ipersons<lpersons || ipersons>upersons)
					match = false;
			}
			
			if(match) {
				numres++;
				addResult(item.Name,item.URL,item.desc);
			}

		});

	}
	
	$('#numres').html(numres);
	
	if(numres > 0) {
		$('#search-results').removeClass('displayNone');
		$('#no-search-results').addClass('displayNone');
	}else{
		$('#search-results').addClass('displayNone');
		$('#no-search-results').removeClass('displayNone');		
	}

}

function addResult(name,link,desc) {
	$("#results-list").append('<li class="result-item"><a href="'+link+'"><h3>'+name+'</h3></a><div>'+desc+'</div></li>');
}

function maskInput() {
	var key_code = window.event.keyCode;
	var oElement = window.event.srcElement;
	if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
			if ((key_code > 47 && key_code < 58) ||
					  (key_code > 95 && key_code < 106)) {
							if (key_code > 95) 
									key_code -= (95-47);
					   oElement.value = oElement.value;
			} else if(key_code == 8) {
					oElement.value = oElement.value;
			} else if(key_code != 9) {
					event.returnValue = false;
			}    
	}
}		

var offices = [ 
 	{	
 		"Name": "106 Hope Street, Glasgow",
 		"desc" : "899 - 1174 sq ft",
 		"URL": "/offices/106-hope-street.html", 
 		"serviced": "no", 
 		"lsize": "899", 
 		"usize": "1174", 
 		"lpersons": "10",
 		"upersons": "15"
 	}, 
 	{
 		"Name": "111 Union Street, Glasgow", 
 		"desc" : "107 - 715 sq ft",
 		"URL": "/offices/111-union-street.html", 
 		"serviced": "yes", 
 		"lsize": "107",
 		"usize": "715", 
 		"lpersons": "1",
 		"upersons": "10"
 	},
 	{
 		"Name": "342 Argyle Street, Glasgow",
 		"desc" : "1200 sq ft", 
 		"URL": "/offices/342-argyle-street.html", 
 		"serviced": "no", 
 		"lsize": "1200", 
 		"usize": "1200",
 		"lpersons": "15",
 		"upersons": "15"
 	}, 
 	{
 		"Name": "Baltic Chambers, 50 Wellington Street, Glasgow",
 		"desc" : "225 - 3000 sq ft", 
 		"URL": "/offices/baltic-chambers.html", 
 		"serviced": "yes", 
 		"lsize": "225",
 		"usize": "3000", 
 		"lpersons": "1",
 		"upersons": "35"
 	}, 
 	{	
 		"Name": "Central Chambers, 93 Hope Street, Glasgow",
 		"desc" : "120 - 3000 sq ft", 
 		"URL": "/offices/central-chambers.html", 
 		"serviced": "yes", 
 		"lsize": "120",
 		"usize": "3000",
 		"lpersons": "1",
 		"upersons": "35"
 	}, 
	{
		"Name": "Ingram House, 227 Ingram Street, Glasgow",
		"desc" : "486 - 1200 sq ft", 
		"URL": "/offices/ingram-house.html", 
		"serviced": 
		"no", 
		"lsize": "486",
		"usize": "1200",
		"lpersons": "4",
		"upersons": "14"
	},
 	{
 		"Name": "Turnberry House, 175 West George Street, Glasgow",
 		"desc" : "340 - 2100 sq ft", 
 		"URL": "/offices/turnberry-house.html", 
 		"serviced": "yes", 
 		"lsize": "340",
 		"usize": "2100", 
 		"lpersons": "3",
 		"upersons": "25"
 	}
];

