﻿if (typeof jQuery == 'undefined') {
	alert("No jQuery");
}
else {
}



$(document).ready(function () {
	$('#searchText').keyup(
		function(){ delayedGet(this) ; 
		});

	$.jTemplatesDebugMode(true);

	$("#ChoiceOutput").setTemplateURL("AreaLocatorTemplate.htm");
});



function buttonClick(button) {
	var s = $(button).val().toUpperCase();

	switch( s )
	{
		case "SEARCH":
			search();
			break;
	}
}



function search() {
	var p = "{search:'p'}";

	var s = $('#searchText').val();

	if (s == '') return;

	p = p.replace(/p/,s);

	$.ajax({
		type: "POST",
		url: "jpssws-area-locator.asmx/AreaLocator",
		data: p,
		dataType: 'json',
		contentType: "application/json; charset=utf-8",
		success: function (data)
		{
			apply(data.d);
		},
		error: function (req, status, error) { alert(error); }
	});

}



function apply(data) {
	// process the template
	if (data != null)
		$("#ChoiceOutput").processTemplate(data);
}



function clear() {
	$("#ChoiceOutput").empty();
}



function searchTextChanged(text)
{
	clear();
}



function getDataSeq()
{
	clear();
	search();
}



var delayedGetTimeout =null;

function delayedGet() {
	if (delayedGetTimeout != null) {
		clearTimeout(delayedGetTimeout);
	}

	delayedGetTimeout = setTimeout(getDataSeq, 1000);	
}
