// onBodyLoad

var plzJson = [];

jQuery(function() {

	jQuery("#user").focus( function() {
		$('#user').unbind('focus');
		$('#user').val('');
	});

	jQuery("#pass").focus( function() {
		$('#pass').unbind('focus');
		$('#pass').val('');
		document.getElementById('pass').type = 'password';
	});

	jQuery("#plzFinder").focus( function() {
		jQuery("#plzFinder").unbind('focus');
		jQuery("#plzFinder").val('');
		jQuery("#plzFinder").autocomplete({
			minLength: 5, 
			source: function(req, add){  

				    // pass request to server
					jQuery.getJSON("ajaxPlzFinder.php?callback=?", req, function(data) {  
			   	
						 // create array for response objects
						 var suggestions = [];

						 // process response
					 	$.each(data, function(i, val){ 
						 	if(val.ort!=null&&val.ort!=""){
						 		suggestions.push(val.plz+', '+val.ort);  
						 	}
					  	});  
					        
					 	plzJson = data;
					  	// pass array to callback
					  	add(suggestions);  
					});
			},
			select: function(e, ui) {  
				 //create formatted friend  
				 var ort = ui.item.value;
				 adjustPlzValues(findPlzByOrt(ort));
			}
		});

	});
});

function adjustPlzValues(plz){
	jQuery("#plzId").val(plz.id);
}

function findPlzByOrt(ort){
	var val = null;
	jQuery.each(plzJson, function(){  	
			if(this.plz+', '+this.ort==ort){
				val = this;
				return this;
			} 
	});  
	return val;
}

function isFormValid(){
	var ret = jQuery("#plzId").val() != null && jQuery("#plzId").val() != "";
	return ret;
}

