function changeHandler(){
	
	var fieldId = this.id;
	/* This is a custom added part for neglet fields greater than department in search*/
	if(fieldId >= 15 && $(this).parents(".gps-fields-group").attr("id")=="search-gps-fields"){
		return;
	}
	if(fieldId >= 10 && $(this).parents(".gps-fields-group").attr("id")=="quick_search"){
		return;
	}
	
	var fieldValue = $('option:selected', this).val();

	$(this).parents(".gps-field-container").nextAll().remove();
	if(!fieldValue){
		return;
	}
	
	$.ajax({
		url: '/gps/getChild/',
		data: {
			type: fieldId,
			node: fieldValue,
			ajax: 1
		},
		type: 'POST',
		success: function(msg){
			$(".gps-fields-group").append(msg);
						
			$(".gps-fields-group .gps-field")
				.unbind("change", changeHandler);
			
			$(".gps-fields-group .gps-field")
				.bind("change", changeHandler);
			
			$(".gps-loading").hide();
		},
		beforeSend: function(){
			$(".gps-loading").show();
		}
	});
}

$(document).ready(function(){
	$(".gps-fields-group .gps-field")
	.change(changeHandler);
});


// Gps / ZipCode switch custom for this html code
$(document).ready(function(){
	
	var container = $(".locate-by-zipcode"); 
	var zipcodeElement = $(".zip-locate-zipcode", container);
	var countryElement = $(".zip-locate-country", container);
	
	countryElement.change(function(){
		zipCodeCache = {};
		zipcodeElement.val("");
		if(parseInt($('option:selected', this).val()) > 0){
			zipcodeElement.parents("dl").show();
		}
		else{
			zipcodeElement.parents("dl").hide();
		}
	});
	
	
	// Cache for autocomlpete
	zipCodeCache = {};
	zipcodeElement.autocomplete({
		minLength: 2,
		maxHeight:150,
		source: function(request, response) {
			var switchNode = {
					label:"click here if want to choose<br> your location in another way...",
					value:request.term,
					close:true
				};
			if (zipCodeCache.term == request.term && zipCodeCache.content) {
				
				if(zipCodeCache.content.length > 6){
					$(".ui-autocomplete.ui-menu").css("height", "200px");
					$(".ui-autocomplete.ui-menu").css("overflow-y", "scroll");
				}
				else{
					$(".ui-autocomplete.ui-menu").css("height", "auto");
					$(".ui-autocomplete.ui-menu").css("overflow-y", "visible");
				}
				response(zipCodeCache.content);
				return;
			}
			if (new RegExp(zipCodeCache.term).test(request.term) && 
					zipCodeCache.content && zipCodeCache.content.length < 13
			) {
				var data = $.ui.autocomplete.filter(zipCodeCache.content, request.term);
				
				if(!data.length){
					data = [switchNode];
				}

				if(data.length > 6){
					$(".ui-autocomplete.ui-menu").css("height", "200px");
					$(".ui-autocomplete.ui-menu").css("overflow-y", "scroll");
				}
				else{
					$(".ui-autocomplete.ui-menu").css("height", "auto");
					$(".ui-autocomplete.ui-menu").css("overflow-y", "visible");
				}
				response(data);
				
				return;
			}
			$.ajax({
				url: "/gps/getZipCodes/",
				dataType: "json",
				type: "POST",
				data: {
					countryId:$('option:selected', countryElement).val(),
					term: request.term
				},
				success: function(data) {
					if(data.length){
						data = $.map(data, function(item) {
							return {
								label: item.zip + ", " + item.name,
								value: item.zip,
								id: item.id
							}
						});
					}
					else{
						data = [switchNode];
					}
					
					if(data.length > 6){
						$(".ui-autocomplete.ui-menu").css("height", "200px");
						$(".ui-autocomplete.ui-menu").css("overflow-y", "scroll");
					}
					else{
						$(".ui-autocomplete.ui-menu").css("height", "auto");
						$(".ui-autocomplete.ui-menu").css("overflow-y", "visible");
					}
					
					zipCodeCache.term = request.term;
					zipCodeCache.content = data;
					
					response(data);
				}
			})
		},
		select: function(event, ui) {
			if(ui.item.close === true){
				$(this).autocomplete("close");
				$("#zip-enabled", container).attr("value", "0");
				container.hide();
				container.siblings(".gps-fields-group").eq(0).show();
				$("select#5.gps-field", container.siblings(".gps-fields-group"))
					.val($('option:selected', countryElement).val());
				$("select#5.gps-field", container.siblings(".gps-fields-group"))
					.trigger("change");
			}
			else{
				$(".wgps-node-id", $(".locate-by-zipcode")).attr("value", ui.item.id);
			}
		}
	});
});



// Custom things for last registered users block

$(document).ready(function(){
	var lastRegsGpsFields = "select.gps-field#5, select.gps-field#10";
	
	function getLastRegs(){
		var fieldId = this.id;
		var fieldValue = $('option:selected', this).val();
		
		$.ajax({
			url: '/registration/lastRegs/',
			data: {
				type: fieldId,
				node: fieldValue
			},
			type: 'POST',
			success: function(response){
				$("#last-reg-users").before(response).remove();
			
				$(lastRegsGpsFields, $(".gps-fields-group#reg-gps-fields"))
					.unbind("change", getLastRegs);
				$(lastRegsGpsFields, $(".gps-fields-group#reg-gps-fields"))
					.bind("change", getLastRegs);
				
				$("#last-reg-users .preloader").hide();
			},
			beforeSend: function(){
				$("#last-reg-users .preloader").show();
			}
		});
	}
	
	$(lastRegsGpsFields, $(".gps-fields-group#reg-gps-fields"))
		.change(getLastRegs);
});
