function qsSelect(status)
{
	if(status == 'sell')
	{
		$('qs_sell').checked = true;
		$('qs_price_sell_container').style.display = '';
		$('qs_price_rent_container').style.display = 'none';
	}
	else
	{
		$('qs_rent').checked = true;
		$('qs_price_sell_container').style.display = 'none';
		$('qs_price_rent_container').style.display = '';
	}
}


function newsletterSubmit(url)
{
	var msg = $('newsletter_msg');

	msg.update('Loading...');

	new Ajax.Updater(msg, root_directory + lang + '/ajax/newsletter_add_filter/', {
		method: 'post',
		parameters: url
	});

	return false;
}


function QShowSubregion(id, subId)
{
	if(id)
	{
		$('qs_sub_region_container').innerHTML = 'Loading...';
		var parameters = 'id=' + id + '&sub_id=' + (subId ? subId : 0) + '&place=quick_search';
		new Ajax.Updater('qs_sub_region_container', root_directory + lang + '/ajax/sub_regions/' + id, {
			method: 'post',
			parameters: parameters
		});
	}
}


function SShowSubregion(id, regions)
{
	if(id)
	{
		MSSearch.reset();
		$('s_sub_regions_container').innerHTML = 'Loading..';

		var parameters = 'id=' + id + '&sub_id=&place=search' + (regions ? regions : '');
		new Ajax.Updater('s_sub_regions_container', root_directory + lang + '/ajax/sub_regions/' + id, {
			method: 'post',
			parameters: parameters,
			onComplete: function() {
				MSSearch.transfer('from_to');
			}
		});
	}
}


function openClose(id)
{
	var momentStyle = $(id).style.display;
	$$('#open_close .open_close_text').each(function(node){
		node.hide();
	});
	$(id).style.display = (momentStyle == 'block') ? 'none' : 'block';
	return false;
}


// MultipleSelects
MultipleSelects = function(fromId, toId, fromToId, toFromId)
{
	this.fromId = fromId;
	this.toId = toId;
	this.empty = $$('#' + this.toId + ' option')[0];
	this.form = $(fromId).form.onsubmit = this.submit.bind(this);

	$(fromToId).onclick = this.transfer.bind(this, 'from_to');
	$(toFromId).onclick = this.transfer.bind(this, 'to_from');
}

MultipleSelects.prototype.transfer = function(action)
{
	var action = action;
	$$('#' + (action == 'from_to' ? this.fromId : this.toId) + ' option').each(function(node) {
		if(node.selected)
		{
			node.selected = false;
			node.remove();
			$(action == 'to_from' ? this.fromId : this.toId).appendChild(node);
		}
	}.bind(this));

	if(!$$('#' + this.toId + ' option')[0])
	{
		$(this.toId).appendChild(this.empty);
	}
	else if($$('#' + this.toId + ' option')[0] == this.empty && $$('#' + this.toId + ' option')[1])
	{
		$(this.toId).removeChild(this.empty);
	}
}

MultipleSelects.prototype.reset = function()
{
	$$('#' + this.toId + ' option').each(function(node) { node.remove(); });
	$(this.toId).appendChild(this.empty);
}

MultipleSelects.prototype.submit = function()
{
	$$('#' + this.toId + ' option').each(function(node) { node.selected = true; });

	if($$('#' + this.toId + ' option')[0])
	{
		$(this.toId).removeChild(this.empty);
	}
}






function writeSwf(element, swfFile, width, height, bgColor, parameters, flashId)
{
	var htmlCode = '';
	var flashVars = '';
	var embedColorCode = (bgColor) ? 'bgcolor="' + bgColor + '"' : 'wmode="transparent"';


	if(parameters)
	{
		for(var i in parameters)
		{
			if(typeof parameters[i] != 'string')
			{
				alert('Parameter: ' + i + ' must by string');
			}
			else
			{
				parameters[i] = parameters[i].replace(/&amp;/gi, '%26');
				parameters[i] = parameters[i].replace(/&/gi, '%26');

				flashVars += i + '=' + parameters[i] + '&';
			}
		}

		if(flashVars)
		{
			flashVars = flashVars.substr(0, flashVars.length - 1);
		}
	}

	htmlCode += '<object ' + (flashId ? 'id="' + flashId + '"' : '') + ' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '">';
		htmlCode += '<param name="movie" value="' + swfFile + '" />';
		htmlCode += '<param name="quality" value="high" />';
		htmlCode += '<param name="flashvars" value="' + flashVars + '" />';
		htmlCode += '<param name="AllowScriptAccess" value="always" />';

		if(bgColor)
		{
			htmlCode += '<param name="bgcolor" value="' + bgColor + '" />';
		}
		else
		{
			htmlCode += '<param name="wmode" value="transparent" />';
		}

		htmlCode += '<embed ' + (flashId ? 'name="' + flashId + '"' : '') + embedColorCode + ' allowscriptaccess="someDomain" flashvars="' + flashVars + '" src="' + swfFile + '" quality="high" width="' + width + '" height="' + height + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	htmlCode += '</object>';

	element.innerHTML = htmlCode;
}
