var Profile;
if (!Profile)
{
	Profile = {};
	
	Profile.setEventHandler = function(element, event_type, handler)
	{
		if (typeof element == 'string') element = document.getElementById(element);
		if (element.addEventListener)
		{
			element.addEventListener(event_type, handler, false);
		}
		else if (element.attachEvent)
		{
			element.attachEvent('on'+event_type, handler);
		}
		else
		{
			eval('element.on'+event_type+' = '+handler);
		}
	}
	
	Profile.init = function()
	{
		DHTMLSelect.init();
		Profile.setEventHandler('country', 'keyup', loadCountry);
		Profile.setEventHandler('city', 'keyup', loadCity);
	}
	
	if (window.addEventListener)
	{
		window.addEventListener("load", Profile.init, false);
	}
	else if (window.attachEvent)
	{
		window.attachEvent("onload", Profile.init);
	}
	else
	{
		window.onload = Profile.init;
	}
}


function onAskChange()
{
	var ask_type = $('how_ask').options[$('how_ask').selectedIndex].value;
	if (ask_type == 'n')
	{
		$('block_nickname').style.display = '';
	}
	else
	{
		$('block_nickname').style.display = 'none';
	}
}

var timer_value = 500;
var timer_period = 50;
var timer = timer_value;
var t_event;
var t_link;
var t_layer;
var t_handler;
var t_keyCode;
var show = 0;

function selectTimer()
{
	if (timer >= timer_period)
	{
		timer -= timer_period;
	}
	else
	{
		if (timer == 0 && show == 1)
		{
			var n_event = new Object();
			n_event.keyCode = t_keyCode;
			DHTMLSelect.showSelect(n_event, 1, t_link, t_layer, t_handler);
			show = 0;
		}
	}
}

var select_timer = setInterval("selectTimer()", timer_period);

function loadCountry_old(event, layer, link)
{
	t_event = event;
	t_keyCode = event.keyCode;
	t_link = link;
	t_layer = layer;
	t_handler = 'showCountry';
	timer = timer_value;
	show = 1;
}
function loadCountry(e)
{
	if (document.getElementById('country') && document.getElementById('select_country'))
	{
		if (!e) e = window.event;
		var word = document.getElementById('country').value;
		t_event = e;
		t_keyCode = e.keyCode;
		t_link = '/ajax.php?service=country&word='+word;
		t_layer = 'select_country';
		t_handler = 'showCountry';
		timer = timer_value;
		show = 1;
	}
}

function showCountry(layer, str)
{
	//alert($('country_id').value);
	$(layer).style.display='block';
	// id обнуляем, чтобы если название только введено текстом, он не мешался
	$('country_id').value = '';
	$(layer).style.height = '200px';
	if (str == '')
	{
		$(layer).innerHTML = '<div id="list_country">Не найдено ни одной подходящей страны</div>';
	}
	else
	{
		try
		{
			var c = eval("("+str+")");
			var str = '<div id="list_country">';
			for (var i = 0; i < c['country_id'].length; i++)
			{
				name_esc = c['country_name'][i].replace(/\'/g, "\\'");
				str += '<div class="ajax_list_item" onClick="setCountry('+c['country_id'][i]+', \''+name_esc+'\');return false"><a href="">'+c['country_name'][i]+'</a></div>\n';
			}
			str += '</div>';

			$(layer).innerHTML = str;
		}
		catch(e)
		{
			alert(e.fileName+':'+e.lineNumber+' '+e.name+' '+e.message);
			return true;
		}
	}
    // Уменьшаем высоту окошка, если мало результатов
	var list_height = $('list_country').offsetHeight;
	if (list_height < 200)
	{
		$(layer).style.height = (list_height+5)+'px';
	}
	else
	{
		$(layer).style.height = '200px';
	}
}

function setCountry(id, name)
{
	$('country_id').value = id;
	$('country').value = name;
	$('select_country').style.visibility = 'hidden';
	$('select_country').style.display = 'none';
}
/////////////
function loadCity(e)
{
	if (document.getElementById('city') && document.getElementById('select_city'))
	{
		if (!e) e = window.event;
		var word = document.getElementById('city').value;
		var country_id = document.getElementById('country_id').value;
		var link = '/ajax.php?service=city&word='+word+'&country_id='+country_id;
		var layer = 'select_city';

		if (link.match(/&country_id=$/) || link.match(/&country_id=0$/))
		{
			var rus_names = [
				'Россия',
				'РФ',
				'Российская федерация',
				'Раша',
				'СССР',
				'Советский Союз'
			];
			var name = $('country').value;
			var id = '';
			for (var i=0; i<rus_names.length; i++)
			{
				var reg = new RegExp("^"+rus_names[i]+"$", "i");
				if (reg.exec(name))
				{
					id = 910;
					$('country_id').value = 910;
					break;
				}
			}
			link = link.replace(/&country_id=$/, '&country_id='+id);
		}
		t_event = e;
		t_keyCode = e.keyCode;
		t_link = link;
		t_layer = layer;
		t_handler = 'showCity';
		timer = timer_value;
		show = 1;
	}
}

function showCity(layer, str)
{
	$(layer).style.display='block';
	$(layer).style.height = '200px';
	//alert($('country_id'));
	if (str == '')
	{
		$(layer).innerHTML = '<div id="list_city">Не найдено ни одного подходящего города</div>';
	}
	else
	{
		try
		{
			var c = eval("("+str+")");
			var str = '<div id="list_city">';
			for (var i = 0; i < c['city_id'].length; i++)
			{
				str += '<div class="ajax_list_item" onClick="setCity('+c['city_id'][i]+', \''+c['city_name'][i]+'\');return false"><a href="">'+c['city_name'][i]+'</a></div>';
			}
			 str += '</div>';
			 //alert(str);
			 $(layer).innerHTML = str;
		}
		catch(e)
		{
			alert(e.name);
		}
	}
	// Уменьшаем высоту окошка, если мало результатов
	var list_height = $('list_city').offsetHeight;
	if (list_height < 200)
	{
		$(layer).style.height = (list_height+5)+'px';
	}
	else
	{
		$(layer).style.height = '200px';
	}
}

function setCity(id, name)
{
	$('city_id').value = id;
	$('city').value = name;
	$('select_city').style.visibility = 'hidden';
	$('select_city').style.display = 'none';
}

var old_inputs = new Array();

function checkForm()
{
	errors = new Array();
	inputs = new Array();
	$('error_messages').innerHTML = '';
	
	
	for (var i=0; i < old_inputs.length; i++)
	{
		$(old_inputs[i]).className = 'big_input';
	}
	
	if ($('firstname').value == '')
	{
		errors.push('Вы не ввели Ваше имя');
		inputs.push('firstname');
	}
	if ($('secondname').value == '')
	{
		errors.push('Вы не ввели Вашу фамилию');
		inputs.push('secondname');
	}
	if ($('country').value == '')
	{
		errors.push('Вы не выбрали страну');
		inputs.push('country');
	}
	if ($('city').value == '')
	{
		errors.push('Вы не выбрали город');
		inputs.push('city');
	}
	if (!$('sex_m').checked && !$('sex_w').checked)
	{
		errors.push('Вы не выбрали Ваш пол');
		inputs.push('sex');
	}
	
	if (errors.length == 0)
	{
		$('profile_fill').submit();
		return true;
	}
	else
	{
		displayErrors();
		return false;
	}

}

function displayErrors()
{
	var error_str = '';
	for (var i=0; i < errors.length; i++)
	{
		error_str += '<div>'+errors[i]+'</div>';
	}
	$('error_messages').innerHTML = error_str;
	for (i=0; i < inputs.length; i++)
	{
		$(inputs[i]).className = 'input_bad';
	}
	old_inputs = inputs;
}



