window.__COUNTRY_CITY= {

	countryId: null,

	cityId: null,

	json: null,

	getCityList: function(countryId) {

		this.countryId = countryId.toInt();

		var this_closure = this;

		//this_closure.emptySelectList($('city'));
		$('city').empty();

		if(this.countryId) {

			var json_options = {
				url : '/js/city_list/'+this.countryId,
				onRequest : function() {
					$('city').setProperty('disabled', true);
					$('loading_city').removeClass('hidden');
				},
				onComplete : function(json_response) {
					if(json_response) {
						var i=0;
						for(x in json_response) {
							var content = json_response[x];
							var value = x;
							if(['string', 'number'].contains(typeof(content)) && ['string', 'number'].contains(typeof(value))) {
								$('city').options[i++] = new Option(content,value);
							}
						}
						if($('city').options.length!=0) {
							$('city').removeProperty('disabled');

							$('country').removeClass('inp_big_red');
							$('city').removeClass('inp_big_red');
						}
						else {
							$('city').options[0] = new Option($('city_empty').get('html'), 0);

							$('country').addClass('inp_big_red');
							$('city').addClass('inp_big_red');
						}
					}
					$('loading_city').addClass('hidden');
				}
			};

			if(this.json && $defined(this.json.cancel)) {
				this.json.cancel();
			}

			this.json = new Request.JSON(json_options).get({
				'DEBUG_MODE': 0,
				'rand': Math.random()
			});
		}

		return false;
	},

	emptySelectList: function(list) {
		if (!list) {return;}
		if (!list.options.length) {return(list);}
		for (var i=0;i<list.options.length;i++) {
			list[i] = null;
		}
		this.emptySelectList(list);
	}

}

window.__NICK_CHECKER = {

	json: null,

	exec : function(inputField) {

		var c = this;

		var nickPattern = inputField.value.trim();

		var json_options = {
			
			url : '/js/check_free_nick/',
			onRequest : function() {
			
				c.showMe($('img_load'));
				c.hideMe($('nick_msg_free'));
				c.hideMe($('nick_msg_busy'));
				c.hideMe($('error_msg_desc'));
				c.hideMe($('nick_ok_img'));
				$('__ERROR__').getParent().addClass('hidden');
			},
			onComplete : function(json_response) {

				c.hideMe($('img_load'));

				if(json_response && json_response.error_html!=='') { // error
			
					inputField.addClass('input_error');
					//$('__ERROR__').set('html', json_response.error_html);
					//$('__ERROR__').getParent().removeClass('hidden');

					var error_desc = [];
					for(x in json_response.errors) {
						error_desc.include(json_response.errors[x]);
						if(x != 2) {
							c.showMe($('nick_msg_busy'));
						}
					}
					$('error_msg_desc').set('html', error_desc.join('<br />'));
					$('error_msg_desc').removeClass('hidden');
				}
				else { // ok
			
					inputField.removeClass('input_error');
					inputField.removeClass('inp_big_red');

					$('error_msg_desc').addClass('hidden');

					c.showMe($('nick_msg_free'));
					c.showMe($('nick_ok_img'));
				}
			}
			
		};

		if(this.json && $defined(this.json.cancel)) {
			this.json.cancel();
		}

		this.json = new Request.JSON(json_options).get({
			"nick": nickPattern, 
			"DEBUG_MODE": 0,
			"rand": Math.random()
		});
		
		return false;
	},


	showMe: function(elem) {
		if(elem) {
			elem.removeClass('hidden');
		}
	},

	hideMe: function(elem) {
		if(elem) {
			elem.addClass('hidden');
		}
	}
};


function SecretQSelect(secret_q, secret_q_user) {
	if(secret_q==secret_q_user) {
		$('secret_q_alt_tr').setStyle('display', '');
		$('secret_q').focus();
	}
	else {
		$('secret_q_alt_tr').setStyle('display', 'none');
	}
	return false;
}

function ShowPasswordStrength(password, password_min, password_max) {

	var password_delta = 2;

	password_min = password_min.toInt();
	password_max = password_max.toInt();

	$('password_strength_low').setStyle('display', 'none');
	$('password_strength_medium').setStyle('display', 'none');
	$('password_strength_high').setStyle('display', 'none');

	$('password_short_length').setStyle('display', 'none');

	if(password.length==0) {
		// pass
	}
	else if(password.length < password_min) {
		// off limit
		$('password_short_length').setStyle('display', 'block');
	}
	else if(password.length > password_max) {
		// off limit
	}
	else if(password.length < (password_min+password_delta*1)) {
		$('password_strength_low').setStyle('display', 'block');
	}
	else if(password.length < (password_min+password_delta*2)) {
		$('password_strength_medium').setStyle('display', 'block');
	}
	else if(password.length < (password_min+password_delta*3) || password.length <= password_max) {
		$('password_strength_high').setStyle('display', 'block');
	}

	return false;
}

function ReloadCaptcha() {

	var json_options = {
		url : '/js/reload_complex_captcha/',
		onRequest : function() { },
		onComplete : function(json_response) {
			if(json_response.captcha_img) {
				$('captcha_img').set('html', json_response.captcha_img);
			}
		}
	};
	new Request.JSON(json_options).get({
		'DEBUG_MODE': 0,
		'rand': Math.random()
	});
	return false;
}

window.__CONF_PASS = {

	id_pass : 'password',
	id_conf : 'password_conf',
	id_error : 'conf_pass_not_match',

	id_pass_o : null,
	id_conf_o : null,
	id_error_o : null,

	init : function() {

		this.id_pass_o = $(this.id_pass);
		this.id_conf_o = $(this.id_conf);
		this.id_error_o = $(this.id_error);

		if(this.ok()) {
			var that = this;
			this.id_conf_o.addEvent('blur', function(event){
				that.checkEqual(event);
			});
			this.id_conf_o.addEvent('keyup', function(event){
				that.checkEqual(event);
			});
		}
	},
	ok : function() {
		return $defined(this.id_pass_o) && $defined(this.id_conf_o) && $defined(this.id_error_o);
	},
	checkEqual : function(event) {
		var bool = this.id_pass_o.value && this.id_conf_o.value!=this.id_pass_o.value;
		this.proceedError(bool);
		return bool;
	},
	proceedError : function(bool) {
		if(bool) {
			this.showError();
		}
		else {
			this.hideError();
		}
	},
	showError : function() {
		this.id_error_o.setStyle('display', '');
	},
	hideError : function() {
		this.id_error_o.setStyle('display', 'none');
	}
};