/*
 * TaxInquiry.js - Site specific script file
 */

/*
 * Create the TaxInquiry namespace
 */
var TaxInquiry;
if (typeof TaxInquiry == 'undefined') {
	TaxInquiry = {};
}

/*
 * Code to be run when the document is ready
 */
TaxInquiry.onready = function() {
	/*
	 * Set up Throbber plugin
	 */
    $("#search")
	.click(function() {
		if( !$("#throbber-container").length > 0 ) { 
			$("#resultDiv").html("<center><h3>Searching...</h3><br/><div id='throbber-container'></div></center>"); 
		} else {
			$("#throbber-container").show();
		}
		$("#throbber-container").css({
            'margin-left': 'auto',
            'margin-right': 'auto'
		})
	})
    .throbber({
        image: "/common/images/dauphin_loader.gif",
        ajax: true,
        parent: "#throbber-container"
    });
    
    /*
     * Set up datepicker
     */
	$(".date_input").livequery(function(){
    	$(this).datepicker();
	});

    $(".pagenav")
	.livequery( 'click', function() {
		if( !$("#throbber-container").length > 0 ) { 
			$("#resultDiv").html("<center><h3>Please wait...</h3><br/><div id='throbber-container'></div></center>"); 
		} else {
			$("#throbber-container").show();
		}
		$("#throbber-container").css({
            'margin-left': 'auto',
            'margin-right': 'auto'
		})
        $.throbberShow({
            image: "/common/images/dauphin_loader.gif",
            ajax: true,
            parent: "#throbber-container"
        })
	});

	/*
	 * Results Dialog box
	 */
	$("#resultDivDlg").dialog({
		dialogClass: "resultsDlg", 
		draggable: false, 
		modal: true, 
		resizable: false, 
		width: 'auto',
		title: "Search Results",
		autoOpen: false,
		open: function() { $("#resultWait").dialog( "close" );}
	});
}

/*
 * Run the search!
 */
TaxInquiry.doSearch = function() {
	document.criteria.rm.value = 'search_ajax';
	$("#errors").html("");
	$("#messages").html("");
	
	if( document.getElementById( 'parcel_search' ).checked ) {
		document.criteria.rm.value = 'search_process';
		document.criteria.submit();
	}
	else {
		$.post('/propertyinquiry.pl', DEVNET.formToJSON( '#criteria' ), function(data){ TaxInquiry.handleJSON(data, '#resultDiv');}, 'json');
	}
}

TaxInquiry.viewResults = function() {
	if(!$("#resultWait").length > 0) {
		$("body").append("<div id='resultWait' class='waitDlg'><center><h3>Please wait...</h3><br/><img src='/common/images/dauphin_loader.gif' /></center></div>");
		$("#resultWait").dialog({
			draggable: false, 
			modal: true, 
			resizable: false,
			width: 240,
			height: 'auto'
		});
	} else {
		$("#resultWait").dialog("open");
	}
	$.getJSON('/propertyinquiry.pl?rm=results&ajax=1&', function(data){ 
		if(TaxInquiry.handleJSON(data, '#resultDiv') != -1)
			$("#resultDivDlg").dialog("open");
	});
}

/*
 * Code to be run after the page loads
 */
TaxInquiry.onload = function() {
	return;
}

/*
 * Respond to JSON received from server
 */
TaxInquiry.handleJSON = function(data, contentDiv) {
	$.each(data.errors, function(i, err) {
		var html = '<span class="red"><b>' + err + '</b></span><br />'
		$("#errors").append(html);
	});
	$.each(data.messages, function(i, msg) {
		var html = '<span class="blue"><b>' + msg + '</b></span><br />'
		$("#messages").append(html);
	});
	if( data.replace_content == 1) {
		$("#content").html( data.content );
	}
	else {
		$(contentDiv).html( data.content );
	}
}

// Default enter key to form submit
$(function() {  
	$("#criteria").keypress(function (e) {  
		if(( e.which && e.which == 13 ) || ( e.keyCode && e.keyCode == 13 )) {  
		$('#search').click();  
			return false;  
		}
		else {  
			return true;  
		}  
	});  
});

