/**
 * @author Denis Zarubin (den@design.ru)
 * @copyright Art.Lebedev Studio (http://www.artlebedev.ru)
 */

var x_city, x_zipcode, iPlaceSelectedId;

var aPlaceOptions = aAreaOptions = aAreaOptionsFull = aCityOptions = aZipOptions = [];
var bInvalidZipcode = false;
var counter = 0;



$(function(){
	
	var data_url = '/credit/acquire/data/';
	
	if (typeof(xForm1) != 'undefined') {
		var xArea = xForm1.getWidgetById('area');
	
		$('form input').focus(function(){
			$(this).parent().parent().addClass('focus');
		});
		$('form input').blur(function(){
			$(this).parent().parent().removeClass('focus');
		});
		
		
		
		
		$.getScript(data_url, function(){
			this.ePlace_list = $('#place_list').get(0);
			if(typeof this.ePlace_list != 'undefined'){
				this.ePlace_list.options.length = 0;
				for (var i = 0; i < aPlaceOptions.length; i++) {
					eOption = new Option(aPlaceOptions[i][1], aPlaceOptions[i][0]);
					this.ePlace_list.options[this.ePlace_list.options.length] = eOption;
				}
				var xPlace = xForm1.getWidgetById('place');
				xPlace.initOptions();
				xPlace.updateOptions(true);
				xPlace.hideOptions();
			}
			
		})
		
		$('#place').keyup(function(){
			change_place()
		});
		$('#place').blur(function(){
			change_place()
		});
		/*
		$('#place_list').change(function(){
			change_place()
		});
		$('#place_list').click(function(){
			change_place()
		});
		*/

		
		
		$('#area').keyup(function(){
			setTimeout(change_area,1)
		});
		
		$('#area').blur(function(){
			setTimeout(change_area,1)
		});
		
		/*
		$('#area_list').click(function(){
			$('#dalert').html($('#dalert').html() + '<br>#area_list .click')
			setTimeout(change_area,10)
		});
		
		$('#area_list').change(function(){
			$('#dalert').html($('#dalert').html() + '<br>#area_list .change')
			setTimeout(change_area,100)
		});
		*/
		
		var zip_value_prev =''; 
		update_postal_code = function(){
			var zip_value = $('#postal_code').val();
			var x_zipcode = xForm1.getWidgetById('postal_code');
			var eZip_list = $('#postal_code_list').get(0);
			if (zip_value != zip_value_prev) {
				$('#r_postal_code .r_message').css({display: 'none'});
				if (zip_value.length < 3) {
					
					eZip_list.options.length = 0; 
					x_zipcode.initOptions();
					x_zipcode.updateOptions(true);
				}
				if (zip_value.length == 3) {
					zip_value = $('#postal_code').val();
					$.getScript(data_url + '?zipcode=' + zip_value, function(){
						eZip_list.options.length = 0;
						for (var i = 0; i < aZipOptions.length; i++) {
							eOption = new Option(aZipOptions[i], aZipOptions[i]);
							eZip_list.options[eZip_list.options.length] = eOption;
						}
						x_zipcode.initOptions();
						x_zipcode.updateOptions(true);
						
					})
					
					
				}
				if (zip_value.length == 6) {
					load_address_by_zipcode(zip_value)
				}
			}
			zip_value_prev = zip_value;
		}
		$('#postal_code').keyup(function(){
			update_postal_code()
		});
		$('#postal_code').blur(function(){
			update_postal_code()
		});
		$('#postal_code_list').change(function(){
			update_postal_code()
		});
		$('#postal_code_list').click(function(){
			update_postal_code()
		});
		

		
		
		
		filter_options = function(filter_value, sId, sOptionList, callback){
			var oSelect = $('#' + sId);
			oSelect.html(sOptionList);
			var x = filter_value;
			
			$('option', oSelect).each(function(){
				if (new RegExp('^(' + x + ')(.+)', 'i').test($(this).html()) ||
				new RegExp('\\s' + x, 'ig').test($(this).html()) ||
				new RegExp('\\(' + x, 'ig').test($(this).html()) ||
				typeof(x) == 'undefined') {
				//$(this).show();
				}
				else {
					$(this).remove();
				}
			})
			if (oSelect.attr('selectedIndex') == -1 || $('option', oSelect).get(oSelect.attr('selectedIndex')).style.display == 'none') {
				oSelect.get(0).selectedIndex = -1;
				callback();
			}
		};
		
		hide_all = function(){
			$('#r_area').hide();
			if(typeof(xArea) != 'undefined') {
						xArea.bRequired=false;
			}
			$('#r_area').removeClass('required');
			
			$('#r_city').hide();
			$('#r_street').hide();
			$('#r_street').val('');
			$('#r_building').hide();
			$('#r_corpus').hide();
			$('#r_stroenie').hide();
			$('#r_flat').hide();
			$('#r_dormitory').hide();
		};
		
		hide_all();
		
		var sPlace_prev = '';
		var oPlace = $('#place');
		
		change_place = function(call_back){
			
			if (sPlace_prev != oPlace.val()) {
			
				sPlace_prev = oPlace.val()
			
				$('#area').val('');
				$('#city').val('');
				this.valid = false;
				
				for (var i = 0; i < aPlaceOptions.length; i++) {
					if (aPlaceOptions[i][1] == oPlace.val()) {
						this.valid = true;
						this.isCity = aPlaceOptions[i][2];
						iPlaceSelectedId = aPlaceOptions[i][0];
						break;
					}
				}
				
				this.ePlace_list = $('#place_list').get(0);
				
				if (this.valid) {
					if (this.isCity) {
						$('#r_area').hide();
						xArea.bRequired=false;
						$('#r_area').removeClass('required');
						
						$('#r_city').hide();
						$('#r_street').show();
						$('#r_building').show();
						$('#r_corpus').show();
						$('#r_stroenie').show();
						$('#r_flat').show();
						$('#r_dormitory').show();
						if (typeof call_back == 'function') {
							call_back();
						}
						else {
							$('#street').focus();
						}
					}
					else {
						$('#r_area').show();
						xArea.bRequired=true;
						$('#r_area').addClass('required');
						$('#r_city').hide();
						$('#r_street').hide();
						$('#r_building').hide();
						$('#r_corpus').hide();
						$('#r_stroenie').hide();
						$('#r_flat').hide();
						$('#r_dormitory').hide();
						if (!aAreaOptionsFull[iPlaceSelectedId]) {
							$.getScript(data_url + '?area_parent_id=' + iPlaceSelectedId, function(){
								aAreaOptionsFull[iPlaceSelectedId] = aAreaOptions;
								change_area_list(call_back, aAreaOptionsFull[iPlaceSelectedId])
							})
						}
						else {
							aAreaOptions = aAreaOptionsFull[iPlaceSelectedId];
							change_area_list(call_back, aAreaOptionsFull[iPlaceSelectedId])
						}
					}
				}
				else {
					hide_all();
				}
				
				if (this.ePlace_list.length == 0 && typeof call_back != 'function') {
					$('#r_place > dd > .r_message').css({
						display: 'block'
					});
					
					if (bInvalidZipcode) {
						$('#r_postal_code > dd > .r_message').css({
							display: 'none'
						});
					}
				}
				else {
					$('#r_place > dd > .r_message').css({
						display: 'none'
					});
					if (bInvalidZipcode) {
						$('#r_postal_code > dd > .r_message').css({
							display: 'block'
						});
					}
				}
			}
		};

		change_area_list = function(call_back, aAreaOptions){
			this.eArea_list = $('#area_list').get(0);
			this.eArea_list.options.length = 0;
			
			for (var i = 0; i < aAreaOptions.length; i++) {
				eOption = new Option(aAreaOptions[i][1], aAreaOptions[i][0]);
				this.eArea_list.options[this.eArea_list.options.length] = eOption;
			}
			xArea.initOptions();
			xArea.updateOptions(true);
			xArea.hideOptions();
			if (typeof call_back == 'function') {
				call_back();
			}
			else {
				$('#area').focus();
			}
		}

		
		var sArea_prev = '';
		var oArea = $('#area');
		
		change_area = function(call_back){
			if($('#area').val() != '' ){
				xArea.bRequired=false;
				$('#r_area').removeClass('required');
			} else {
				xArea.bRequired=true;
				$('#r_area').addClass('required');
			}
			if (sArea_prev != oArea.val()) {
				sArea_prev = oArea.val();
				$('#city').val('');
				
				this.valid = false;
				for (var i = 0; i < aAreaOptions.length; i++) {
					if (aAreaOptions[i][1] == oArea.val()) {
						this.valid = true;
						this.isCity = aAreaOptions[i][2];
						iAreaSelectedId = aAreaOptions[i][0];
						break;
					}
				}
				//$('#dalert').html($('#dalert').html() + '<br>aAreaOptions.length=' + aAreaOptions.length + '#oArea.val()=' + oArea.val())
				this.eArea_list = $('#area_list').get(0);
				
				if (this.valid) {
					if (this.isCity) {
						$('#r_city').hide();
						$('#r_street').show();
						$('#r_building').show();
						$('#r_corpus').show();
						$('#r_stroenie').show();
						$('#r_flat').show();
						$('#r_dormitory').show();
						if (typeof call_back == 'function') {
							call_back();
						}
						else {
							$('#street').focus();
						}
					}
					else {
						$('#r_city').show();
						$('#r_street').show();
						$('#r_building').show();
						$('#r_corpus').show();
						$('#r_stroenie').show();
						$('#r_flat').show();
						$('#r_dormitory').show();
						$.getScript(data_url + '?city_parent_id=' + iAreaSelectedId, function(){
							this.eCity_list = $('#city_list').get(0);
							this.eCity_list.options.length = 0;
							for (var i = 0; i < aCityOptions.length; i++) {
								eOption = new Option(aCityOptions[i][1], aCityOptions[i][0]);
								this.eCity_list.options[this.eCity_list.options.length] = eOption;
							}
							var xCity = xForm1.getWidgetById('city');
							xCity.initOptions();
							xCity.updateOptions(true);
							xCity.hideOptions();
							if (typeof call_back == 'function') {
								call_back();
							}
							else {
								$('#city').focus();
							}
						})
					}
				}
				else {
					$('#r_city').hide();
					$('#r_street').hide();
					$('#r_street').val('');
					$('#r_building').hide();
					$('#r_corpus').hide();
					$('#r_stroenie').hide();
					$('#r_flat').hide();
					$('#r_dormitory').hide();
				}
				if (this.eArea_list.length == 0 && oArea.val() != '' && typeof call_back != 'function') {
					$('#r_area > dd > .r_message').css({
						display: 'block'
					});
				}
				else {
					$('#r_area > dd > .r_message').css({
						display: 'none'
					});
				}
				
			}
		}
		
		
		
		load_address_by_zipcode = function(zipcode){
			$.getScript(data_url + '?zipcode=' + zipcode, function(){
				
			})
		}
	}

});

