//var $ = jQuery.noConflict();

$(document).ready(function(){
	
	$('#streetname').live('keyup',function(event){
		$('#streetlist').css('display','block');
		list(this,event);
	});
	
	
	$('#tabs').tabs();
	$('.tip').cluetip({
		activation: 'click',
	    arrows: true,
	    fx: {             
	      open: 'fadeIn',
	      openSpeed:  500
	    }
	  });
	  $('.help').cluetip({
	  	activation: 'click',
	    arrows: true,
	    width: 500,
	    sticky:true,
	    closePosition:'title',
	    cluetipClass: 'jtip',
	    closeText: '<img src="../images/cross.png" alt="close" />',
	    fx: {             
	      open: 'fadeIn',
	      openSpeed:  500
	    }
	  });
	  /*$('a').each(function() {
	    var thisHash = this.hash;
	    $(this).cluetip({
	       ajaxProcess: function(data) {
	        var $div = $('<div></div>').append(data).find(thisHash);
	          return $div;
	      }
	    
	    });
	  });  
	*/
	
	$(".ui-widget-overlay").live("click", function() {  $("#popup").dialog("close"); } );
	  
	$(".taxpopup").live('click',function(){
		$("#popup").html("<br /><br />Loading<br /><img src='../images/ajax-barloader.gif'>");
		var title = $(this).attr('title');
		//var program = $(this).attr('program');
		var width = parseInt($(this).attr('width'));
		var height = parseInt($(this).attr('height'));
		var program = "index.php";
		if (typeof width == 'undefined' || isNaN(width)){
			width="1000";
		}
		if (typeof height == 'undefined' || isNaN(height)){
			var height="600";
		}
		    		
		var optionsclient = {
			modal: true,
    		title: title,
    		width: width,
    		height: height,
    		buttons: { 
    			    "Print": function() { $("#taxdetail").jqprint(); },
    			    Close: function() { $('#popup').dialog("close"); }
    			  }
		};
		var href=$(this).attr('href');
		processHTML(href,'popup');
		$("#popup").dialog(optionsclient);
		$('#popup').data('title.dialog', title); 
		$('#popup').data('width.dialog',  width); 
		$('#popup').data('height.dialog', height);
		$("#popup").dialog("open");
		return false;	
	}); 
	
	$('.taxdetail').live('click',function(){
		var href = $(this).attr('href');
		processHTML(href,'taxdetail');
		return false;
	});
	
	$('#taxdata a.link').live('click',function(){
		var href=$(this).attr('href');
		var querystring = href.slice(href.indexOf('?')+1);
		$.get('taxdata.php',querystring,processData);
		return false;
	});
	$('#SearchMethod').livequery('change',function(){
		var selected = $("#SearchMethod option:selected"); 
		var queryString = "search=yes&searchtype="+selected.val();
		$.ajax({
			url: "searchtype.php",
			type: "post",
			data: queryString,
			dataType: 'html',
			error: function(){
    			alert('Error loading Data');
			},
			success: function(data){
    			$('#searchForm').html(data);
			}
		});
	});
	
	$('.checklen').livequery('keyup',function(){
		var id = $(this).attr('id');
		var len = $(this).val().length;
		var v = $(this).val();
		switch(id){
			case 'pnmun':
				if (len == 3){
					$('#pntown').focus();
				}
				break;
			case 'pntown':
				if (len == 2){
					$('#pnrang').select();
					$('#pnrang').focus();
				}
				break;
			case 'pnrang':
				if (len == 2){
					$('#pnsect').focus();
				}
				break;
			case 'pnsect':
				if (len == 2){
					$('#pnqtr').focus();
				}
				break;
			case 'pnqtr':
				if (len == 2){
					$('#pnid').focus();
				}
				break;	
		}
	});
	
});
function processData(data){
	$("#taxdata").fadeOut('slow', function(){
			$("#taxdata").html(data).fadeIn('slow');
	});
}
function jprocesshtml(program,target,aoption){
   $.post(program,{},function(data){
        if (aoption=='true') {
            $(target).fadeOut('slow',function(){
                $(target).html(data).fadeIn('slow');
            });
        }else{
            $(target).html(data);
        }
    });
 }
var errFunc = function(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
	};

/*function processHtml(program,target){
	var resp = new Ajax.Request(program,{
			onSuccess:	function(t) {
   	 		Element.update(target, t.responseText);
        },
	  		onFailure:errFunc});
}*/
/*function processForm(program,form,target){
	var resp = new Ajax.Request(program,
		 {parameters: $(form).serialize(true),
		 onSuccess:	function(t) {
       	 	Element.update(target, t.responseText);
       	 	}});
}*/
function list(field, event){
  var keyID = event.which||event.keyCode;
  if(keyID != 9){
  	  displayBelowThisObject = field;	
	  var x = displayBelowThisObject.offsetLeft;
	  var y = displayBelowThisObject.offsetTop + displayBelowThisObject.offsetHeight;
	  // deal with elements inside tables and such
	  var parent = displayBelowThisObject;
	  while (parent.offsetParent) {
	    parent = parent.offsetParent;
	    x += parent.offsetLeft;
	    y += parent.offsetTop ;
	  }
	   stringx = "x="+x+"&y="+y;
	   var str = $('#streetname').val();
	  processHTML('lookup.php?'+stringx+'&streetname='+str ,'streetlist');
	  
  }
}
function closediv(field){
	$('#'+field).html('');
}
function checkMun(){
	var letters = document.getElementById('pnmun').value.length +1;
	if (letters <= 3){
		$('#pnmun').focus();
	}else{
		$('#pntown').focus();
	}
} 
function checkTown(){
	var letters = document.parcel_form.pntown.value.length +1;
	if (letters <= 2){
		$('#pntown').focus();
	}else{
		$('#pnrang').focus();
	}
} 
function checkRange(){
var letters = document.parcel_form.pnrang.value.length +1;
	if (letters <= 2){
		$('#pnrang').focus();
	}else{
		$('#pnsect').focus();
	}
} 
function checkSection(){
	var letters = document.parcel_form.pnsect.value.length +1;
	if (letters <= 2){
		$('#pnsect').focus();
	}else{
		$('#pnqtr').focus();
	}
}
function checkQuarter(){
var letters = document.parcel_form.pnqtr.value.length +1;
	if (letters <= 2){
		$('#pnqtr').focus();
	}else{
		$('#pnid').focus();
	}
} 
function checkId(){
	var letters = document.parcel_form.pnid.value.length +1;
	if (letters <= 3){
		$('#pnid').focus();
	}else{
		$('#Query').focus();
	}
} 
function checkPtown()
{
	var letters = document.partial_form.pntown.value.length +1;
	if (letters <= 2){
		$('#pntown').focus();
	}else{
		$('#pnrang').focus();
	}
} 
function checkPrange(){
	var letters = document.partial_form.pnrang.value.length +1;
	if (letters <= 2){
		$('#pnrang').focus();
	}else{
		$('#pnsect').focus();
	}
} 
function checkPsection(){
	var letters = document.partial_form.pnsect.value.length +1;
	if (letters <= 2){
		$('#pnsect').focus();
	}else{
		$('#pnqtr').focus();
	}
}
function checkPquarter(){
	var letters = document.partial_form.pnqtr.value.length +1;
	if (letters <= 2){
		$('#pnqtr').focus();
	}else{
		$('#pnid').focus();
	}
} 
function checkPid(){
	var letters = document.partial_form.pnid.value.length +1;
	if (letters <= 3){
		$('#pnid').focus();
	}else{
		$('#Query').focus();
	}
}
function checktMun(){
	var letters = document.tparcel_form.pnmun.value.length +1;
	if (letters <= 3){
		$('#pnmun').focus();
	}else{
		$('#Query').focus();
	}
} 
function checkKey(fieldValue) {
	var valid = "0123456789";
	for (var i=0; i < fieldValue.length; i++) {
	temp = "" + fieldValue.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			var len = fieldValue.length - 1;
			fieldValue = fieldValue.substring(0 , len);
		}
	}
	return fieldValue;
}
function validate(field) {
	var valid = "0123456789";
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1")
			ok = "no";
	}
	if (ok == "no") {
		alert("Invalid entry! Must be a Number!");
		field.focus();
		field.select();
	  }
}

function closeStreetList() {
	$('#streetlist').html('');
}

