$(function(){
	$('#fzip_country').change(function(){FindZip_LoadState($(this).val(),'');});
	$('#btnZipFinder').toggle(
							  function(){$('#cab_fzip2').slideDown('fast',function(){$('#btnZipFinder').html('Close Zip Finder');});},
							  function(){$('#cab_fzip2').slideUp('fast', function(){$('#btnZipFinder').html('+ Find Zip Code');});}
							  );
	FindZip_LoadState($('#fzip_country').val());
});


//```````````````````````````````````````````````````````````
function FindZip_LoadState(parent_id,default_val){
	if ($('#fzip_state').get(0)){$('#fzip_state').remove();}
	if ($('#fzip_city').get(0)){$('#fzip_city').remove();}
	if ($('#fzip_list').get(0)){$('#fzip_list').remove();}
	$('#state_loading').remove();
	$('#cab_fzip').append('<span id="state_loading"><br>loading....</span>');
	$.ajax({
		type: "POST",
		url: js_dir+"ajax_funcs/getstate.php",
		data: "parent_id="+parent_id,
		success: function(json){
			if (json!=''){
				$('#state_loading').remove();
				$('#cab_fzip').append('<select id="fzip_state" style="display: block;"></select>');
				$('#fzip_state').addOption(eval('('+json+')'));
				$('#fzip_state').change(function(){
					FindZip_LoadCity($(this).val());
				});
			}else{
				$('#state_loading').html('<br>failed.');
			}
		}
	});
}
//```````````````````````````````````````````````````````````
function FindZip_LoadCity(parent_id,default_val){
	if ($('#fzip_city').get(0)){$('#fzip_city').remove();}
	if ($('#fzip_list').get(0)){$('#fzip_list').remove();}
	$('#state_loading').remove();
	$('#cab_fzip').append('<span id="city_loading"><br>loading....</span>');
	$.ajax({
		type: "POST",
		url: js_dir+"ajax_funcs/getcity_all.php",
		data: "parent_id="+parent_id,
		success: function(json){
			if (json!=''){
				$('#city_loading').remove();
				$('#cab_fzip').append('<select id="fzip_city" style="display: block;"></select>');
				$('#fzip_city').addOption(eval('('+json+')'));
				$('#fzip_city').change(function(){
					FindZip_LoadZip();
				});
			}else{
				$('#city_loading').html('<br>failed.');
			}
		}
	});
}
//```````````````````````````````````````````````````````````
function FindZip_LoadZip(){
	if ($('#fzip_list').get(0)){$('#fzip_list').remove();}
	$('#state_loading').remove();
	$('#cab_fzip').append('<div id="fzip_list" style="position: relative; float: right; background-color: #EFEFEF;"></div>');
	$.ajax({
		type: "POST",
		url: js_dir+"ajax_funcs/getzip.php",
		data: "state="+$('#fzip_state').val()+'&city='+$('#fzip_city').val(),
		success: function(json){
			if (json!=''){
				objZip=eval('('+json+')');
				for (i=0;i<objZip.bindings.length;i++){
					$('#fzip_list').append('Click on the zip code and it will be entered automatically: <a href="javascript:;" style="position: relative; float:right; margin-left: 5px; font-size: 14px; color: #FF0000;">'+objZip.bindings[i].zip+'</a>');
				}
				$('#fzip_list a').click(function(){
					$('#zip_code').val($(this).html());
					$('#selectBy').attr('checked',true);
					try {switchSearchBy();}catch(err){}
					$('#btnZipFinder').click();
				});
			}else{
				$('#fzip_list').append('<b style="margin-top: 20px; color: #FF0000;">Not found!</b>');
			}
		}
	});
}