﻿function SuburbLookup() { }

SuburbLookup.showpop = function () {
	$("#divSuburbDialog").dialog({ resizable:false, modal: true, buttons: { "Ok": function() { $("#ppsPostcodeBox").val($('#divSuburbs input:radio:checked').val()); $(this).dialog("close"); $("#btnppsSelectPostcode").click(); } } } );
	$("#divSuburbDialog").keydown(function (event) {
	    if (event.keyCode == '13') {
            event.preventDefault();
            $("#ppsPostcodeBox").val($('#divSuburbs input:radio:checked').val()); 
            $(this).dialog("close"); 
            $("#btnppsSelectPostcode").click();
        }
	});
}

SuburbLookup.DisplayError = function (errorDisplay,message) {
//	errorDisplay.html(message);
//    setTimeout(function() { errorDisplay.html(""); },5000);
	alert(message);
}



SuburbLookup.SendAjax = function (urlMethod, jsonData, returnFunction, errorDisplay) {
	$.ajax({
		type: "POST",
		contentType: "application/json; charset=utf-8",
		url: urlMethod,
		data: jsonData,
		dataType: "json",
		success: function(msg) {
			if (msg != null) {
				var err = returnFunction(msg,errorDisplay);
			}
		},
		error: function(xhr, status, error) {
			// Boil the ASP.NET AJAX error down to JSON.
			var err = eval("(" + xhr.responseText + ")");
 
			// Display the specific error raised by the server
			SuburbLookup.DisplayError(errorDisplay,err.Message);
		}
	});
}

SuburbLookup.LookupSuburb = function (suburb,errorDisplay)
{
	errorDisplay.html("");
	$("#divSuburbs").html("");
	//Ajax
	var urlMethod = "/PackagePricing.asmx/getPostcodesForSuburbName";
	var jsonData = '{suburb : \''+suburb+'\'}';
	SuburbLookup.SendAjax(urlMethod, jsonData, SuburbLookup.HandleReply,errorDisplay);
}

SuburbLookup.HandleReply = function (msg,errorDisplay) {
	var listItems = "";
	var items = "";
	if(msg.d.length > 0) {
		$.each(msg.d, function(key, val) {
			items += "<br /><input type='radio' name='option_layout' value='"+val+"' style='margin-bottom:5px'>"+val+"</input>";
		});
		$("#divSuburbs").html(items);
		SuburbLookup.showpop();
	}
	else {
        PostcodePriceSetter.GetPackagePricing($("#ppsPostcodeBox").val(),errorDisplay);
	}
}

