var isIE		= (navigator.appName.indexOf('Internet Explorer') != -1);
var isOpera		= (navigator.userAgent.indexOf("Opera")>-1);

var cache		= new Array();
var isBlocked 		= false;

function onIframeLoading(id, loadingShow)
{
	if (loadingShow)
	{
		var loading = document.getElementById(id + '_loading');
		loading.style.display = 'block';
		var done = document.getElementById(id + '_done');
		done.style.display = 'none';
	}
	if (isIE)
	{
		try
		{
			var iframe =  document.frames[id+'_frame'].document.getElementById('good');
			var result = iframe.getAttribute('name');
			var loading = document.getElementById(id + '_loading');
			loading.style.display = 'none';
			var done = document.getElementById(id + '_done');
			done.style.display = 'block';
			var tmp = document.getElementById(id + '_tmp');
			tmp.value = result;
			var name = iframe.innerHTML;
			var show = document.getElementById(id + '_show');
			show.innerHTML = name;
			iframe.id = '0o0o0o';
			if ($('#'+id+'_error').length) $('#'+id+'_error').fadeOut();
			if ($('#'+id+'_preview').length) $('#'+id+'_preview').fadeOut();
		} catch (e)
		{
			window.setTimeout('onIframeLoading("'+id+'")', 200);
		}
	}
	else
	{
		try
		{
			var iframe = top.document.getElementById(id+'_frame').contentDocument.getElementById('good');
			var result = iframe.getAttribute('name');
			var loading = document.getElementById(id + '_loading');
			loading.style.display = 'none';
			var done = document.getElementById(id + '_done');
			done.style.display = 'block';
			var tmp = document.getElementById(id + '_tmp');
			tmp.value = result;
			var name = iframe.innerHTML;
			var show = document.getElementById(id + '_show');
			show.innerHTML = name;
			iframe.id = '0o0o0o';
			if ($('#'+id+'_error').length) $('#'+id+'_error').fadeOut();
			if ($('#'+id+'_preview').length) $('#'+id+'_preview').fadeOut();
		} catch (e)
		{
			window.setTimeout('onIframeLoading("'+id+'")', 200);
		}
	}
}

function clearElem(id)
{
	if ($('#'+id)) $('#'+id).fadeOut(300);
}

function formToUrl(obj)
{
	var elem = obj.elements;
	var url = '';

	for (var i=0; i<elem.length; i++)
	{
		if (elem[i] && elem[i].name)
		{
			if (elem[i].type == 'radio' || elem[i].type == 'checkbox')
			{
				if (elem[i].checked) url = url + elem[i].name + '=' + encodeURIComponent(elem[i].value) + '&';
			}
			else url = url + elem[i].name + '=' + encodeURIComponent(elem[i].value) + '&';
		}
	}

	return url;
}

function post(request_url, request_query, history)
{
	history = history ? false : true;
	url = request_url.replace('.html', '.xml');
	if (request_url)
	{
		if (!isBlocked)
		{
			isBlocked = true;
			$('#blocking').stop().css({opacity:0, width:'100%', height:function(){return $('#allPage').height() + 'px'; }, position:'absolute', top:'0px', left:'0px', display:'block'}).animate({opacity:0.5}, 300, function(){
				if ($('#loading').length)
				{
					$('#loading').css({
						left: function() {return (($('html > body')[0].clientWidth / 2 - 65) + $(document).scrollLeft()) + 'px';},
						top: function() {return (($('html > body')[0].clientHeight / 2 - 23) + $(document).scrollTop()) + 'px';},
						position: 'absolute',
						display: 'block'
					});

					$(window).scroll(function(){
						$('#loading').css({
							left: function() {return (($('html > body')[0].clientWidth / 2 - 65) + $(document).scrollLeft()) + 'px';},
							top: function() {return (($('html > body')[0].clientHeight / 2 - 23) + $(document).scrollTop()) + 'px';}
						});
					});
				}

				$.ajax({
					async: true,
					beforeSend: function() {},
					cache: true,
					complete: function(xmlDoc, result) {},
					contentType: 'application/x-www-form-urlencoded',
					data: request_query,
					dataType: 'xml',
					error: function() {alert('Request error'); unblock();},
					global: true,
					ifModified: false,
					processData: true,
					success: function(xmlDoc, result) {
						if (!checkErrors(xmlDoc))
						{
							if ($('index > function', xmlDoc).length)
							{
								goCallBack(xmlDoc);
								return;
							}
							if ($('index > redirect', xmlDoc).length)
							{
								location.href = $('index > redirect', xmlDoc).text();
								return;
							}
							getXSL(xmlDoc, history);
						}
					},
					type: 'POST',
					url: request_url
				});
			});
		}
		else
		{
			$.ajax({
				async: false,
				beforeSend: function() {},
				cache: true,
				complete: function(xmlDoc, result) {},
				contentType: 'application/x-www-form-urlencoded',
				data: request_query,
				dataType: 'xml',
				error: function() {alert('Request error'); unblock();},
				global: true,
				ifModified: false,
				processData: true,
				success: function(xmlDoc, result) {
					if (!checkErrors(xmlDoc))
					{
						if ($('index > function', xmlDoc).length)
						{
							goCallBack(xmlDoc);
							return;
						}
						if ($('index > redirect', xmlDoc).length)
						{
							location.href = $('index > redirect', xmlDoc).text();
							return;
						}
						getXSL(xmlDoc, history);
					}
				},
				type: 'POST',
				url: request_url
			});
		}

		return false;
	}
	else
	{
		return true;
	}
}

function get(request_url, history)
{
	url = request_url.replace('.html', '.xml');

	if (url)
	{
		if (!isBlocked) block();

		$.ajax({
			async: true,
			beforeSend: function() {},
			cache: true,
			complete: function(xmlDoc, result) {},
			contentType: 'application/x-www-form-urlencoded',
			data: '',
			dataType: 'xml',
			error: function() {alert('Request error'); unblock();},
			global: true,
			ifModified: false,
			processData: true,
			success: function(xmlDoc, result) {
				if (!checkErrors(xmlDoc))
				{
					if ($('index > function', xmlDoc).length)
					{
						goCallBack(xmlDoc);
						return;
					}
					if ($('index > redirect', xmlDoc).length)
					{
						location.href = $('index > redirect', xmlDoc).text();
						return;
					}
					getXSL(xmlDoc, history, url);
				}
			},
			type: 'GET',
			url: url
		});

		return false;
	}
	else
	{
		return true;
	}
}

function checkErrors(xmlDoc)
{
	if ($('index > errors > error', xmlDoc).length && !($('index > errors > error', xmlDoc).length == 1 && $('index > errors > error[name="dontunblock"]', xmlDoc).length == 1))
	{
		var formName = $('index', xmlDoc).attr('name');
		var errorItems = $('index > errors > error', xmlDoc);

		for(var i=0; i<errorItems.length; i++)
		{
			var name	= $(errorItems[i]).attr('name');
			var pclass	= $(errorItems[i]).attr('class');
			var text	= $(errorItems[i]).text();
			var id		= formName + '_' + name + '_error';
			
			if ($('#' + formName + '_' + name + '_tmp').length)
			{
				$('#' + formName + '_' + name + '_tmp').val('');
				$('#' + formName + '_' + name + '_show').val('');
				$('#' + formName + '_' + name + '_done').hide();
			}

			var elem	= document.getElementById(id);
			$('#'+id).text(text);

			if (!pclass) pclass = 'error';
			$('#'+id).addClass(pclass).fadeIn(300);

			if (i == 0 && $('#'+id).length)
			{
				var obj = $('#'+id).offset();
				if (!$('index > errors > error[name="dontunblock"]', xmlDoc).length)
				{
					if (obj)
					{
						unblock('noScroll');
						var top = 0;
						if (obj.top > 60) top = obj.top - 60;
						$('html,body').scrollTop(top);
					}
					else
					{
						unblock();
					}
				}
			}
		}

		if (!($('#blocking').length))
		{
			var el = $('<div class="blocking" id="blocking"><!-- --></div>');
			$('html > body').append(el);
		}

		$('#blocking').css({width:'100%', height:function(){return $('#allPage').height() + 'px'; }, position:'absolute', top:'0px', left:'0px'});

		return true;
	}
	else
	{
		return false;
	}
}

function goCallBack(xmlDoc)
{
	if ($('index > function', xmlDoc).length)
	{
		var functionName = $('index > function', xmlDoc).attr('name');
		eval(functionName+'(xmlDoc)');
		return true;
	}
	else
	{
		return false;
	}
}

function getXSL(xmlDoc, history, url)
{
	var request_xsl = $('index > data > xsl', xmlDoc).text();

	$.ajax({
		async: true,
		beforeSend: function() {},
		cache: true,
		complete: function(xslDoc, result) {},
		contentType: 'application/x-www-form-urlencoded',
		data: '',
		dataType: 'xml',
		error: function() {alert('Request error'); unblock();},
		global: true,
		ifModified: false,
		processData: true,
		success: function(xslDoc, result) {
			transformData(xmlDoc, xslDoc, url, history);
		},
		type: 'GET',
		url: request_xsl
	});
}

function transformData(xmlDoc, xslDoc, url, history)
{
	var newId = $('index > data > id', xmlDoc).text();
	var oldId = '';

	if (!(newId && $('#'+newId).length))
	{
		$('index > data > siblilng-id', xmlDoc).each(function() {
			if ($('#'+$(this).text()).length) oldId = $(this).text();
		});
	}
	else
	{
		oldId = newId;
	}

	if (oldId) oldId = '#'+oldId;

	var baseXml = '';
	if ($('index > data > base-xml', xmlDoc).length) var baseXml = $('index > data > base-xml', xmlDoc).text();

	if (!oldId || !$(oldId).length)
	{
		get(baseXml);
		return;
	}

	if (!history && $('index > data > nav-id', xmlDoc).length)
	{
		try { dhtmlHistory.add($('index > data > nav-id', xmlDoc).text(), url); } catch (e) {}
	}

	if (window.ActiveXObject) {
		var el = $('index > body', xmlDoc)[0];
		var newText = el.transformNode(xslDoc);
	} else {
		var processor = new XSLTProcessor();
		processor.importStylesheet(xslDoc);

		var newDocument = processor.transformToDocument(xmlDoc);
		var el = $('index > body', newDocument)[0];
		var newText = new XMLSerializer().serializeToString(el);
	}

	$(oldId).html(newText).attr('id', newId);
	go_decoding();

	if (isBlocked) unblock();

	onLoad(true);
}

function textareaPrepare()
{
	var textA = document.body.getElementsByTagName("TEXTAREA");
	for (var i=0; i<textA.length; i++)
	{
		var textareav = textA.item(i).value;
		if (textareav == '--!--' || textareav == '<!---->' || textareav == '<!--x-->') textA.item(i).value = '';
	}
}

function historyChange(newLocation, historyData)
{
	if (historyData)
	{
		get(historyData, true);
	}
}

function onLoad(nohistory)
{
	if (!nohistory)
	{
		try{
			window.historyStorage.init();
			window.dhtmlHistory.create();
		} catch (e) {}

		try{
			dhtmlHistory.initialize();
			dhtmlHistory.addListener(historyChange);

			if (dhtmlHistory.isFirstLoad()){}
		} catch (e) {}
	}

	go_decoding();
	textareaPrepare();

	var jsSrc = '';
	$('div[name="embeded_script"]').each(function(){
		jsSrc += $(this).html();
	});
	if (jsSrc) try {eval(jsSrc)} catch (e) {alert("Error in embeded script:" + e + '  (' + e.description + ')')};

	$('form div[name="file_ajax"]').each(function(){
		$(this).css('display', 'inline');
	});

	$('form div[name="file_red"]').each(function(){
		$(this).css('display', 'none').empty();
	});

	if (!($('#blocking').length))
	{
		var el = $('<div class="blocking" id="blocking"><!-- --></div>');
		$('html > body').append(el);
	}

	$('#blocking').css({width:'100%', height:function(){return $('#allPage').height() + 'px'; }, position:'absolute', top:'0px', left:'0px'});
}

function block(noLoading)
{
	isBlocked = true;
	
	$('#blocking').stop().css({opacity:0, width:'100%', height:function(){return $('#allPage').height() + 'px'; }, position:'absolute', top:'0px', left:'0px', display:'block'}).animate({opacity:0.5}, 300, function(){
		if (!noLoading && $('#loading').length)
		{
			$('#loading').css({
				left: function() {return (($('html > body')[0].clientWidth / 2 - 65) + $(document).scrollLeft()) + 'px';},
				top: function() {return (($('html > body')[0].clientHeight / 2 - 23) + $(document).scrollTop()) + 'px';},
				position: 'absolute',
				display: 'block'
			});

			$(window).scroll(function(){
				$('#loading').css({
					left: function() {return (($('html > body')[0].clientWidth / 2 - 65) + $(document).scrollLeft()) + 'px';},
					top: function() {return (($('html > body')[0].clientHeight / 2 - 23) + $(document).scrollTop()) + 'px';}
				});
			});
		}
	});
}

function unblock(noScrollTop)
{
	if ($('#loading').length) $('#loading').css('display', 'none');
	$(window).scroll(function(){});

	$('#blocking').stop().animate({opacity:0}, 300, function(){$(this).css({display:'none'}); isBlocked = false;});
	if (!noScrollTop) $('html,body').scrollTop(0);
}

function show(id)
{
	if (id.constructor == Array)
	{
		for (var i=0; i<id.length; i++)
			if ($('#'+id[i]).length) $('#'+id[i]).css('display', 'block');
	}
	else
	{
			if ($('#'+id).length) $('#'+id).css('display', 'block');
	}
}

function hide(id)
{
	if (id.constructor == Array)
	{
		for (var i=0; i<id.length; i++)
			if ($('#'+id[i]).length) $('#'+id[i]).css('display', 'none');
	}
	else
	{
			if ($('#'+id).length) $('#'+id).css('display', 'none');
	}
}

function callEditor(container)
{
	block(true);
	wc = $('#wyswyg_container');

	$(wc).addClass('wyswyg_show');

	$(wc).css({
		left: function() {return (($('html > body')[0].clientWidth / 2 - $(wc).width() / 2) + $(document).scrollLeft()) + 'px';},
		top: function() {return (($('html > body')[0].clientHeight / 2 - $(wc).height() / 2) + $(document).scrollTop()) + 'px';}
	});

	$(window).scroll(function(){
		$(wc).css({
			left: function() {return (($('html > body')[0].clientWidth / 2 - $(wc).width() / 2) + $(document).scrollLeft()) + 'px';},
			top: function() {return (($('html > body')[0].clientHeight / 2 - $(wc).height() / 2) + $(document).scrollTop()) + 'px';}
		});
	});

	var oEditor = FCKeditorAPI.GetInstance('wyswyg_gate') ;

	oEditor.SetHTML($(container).html());
	curEditedElement = container.id.replace('div_', '');
}

function saveEditorChanges(act)
{
	var oEditor = top.FCKeditorAPI.GetInstance('wyswyg_gate') ;
	var container = document.getElementById('div_'+curEditedElement);
	var valueContainer = document.getElementById('hd_'+curEditedElement);

	if(act == 'yes')
	{
		valueContainer.value = oEditor.GetXHTML();
		container.innerHTML = valueContainer.value;
		oEditor.SetHTML('');
	}
	else oEditor.SetHTML('');

	curEditedElement = null;
	document.getElementById('wyswyg_container').className = 'wyswyg_hidden';
	$(window).scroll(function(){});
	unblock(true);
}

function setDo(id, operation)
{
	$('#'+id).val(operation);
}

function selectAllLink(name, selAllTitle, deselAllTitle)
{
	var ch	= $('#chall_' + name)[0];

	$('span[name="chall_' + name + '_div"]').each(function(){
		$(this).html(selAllTitle);
	});

	if (ch.checked) ch.checked = false;
		else ch.checked = true;

	selectAll(name, ch.checked);
}

function selectAll (form_name, checked)
{
	$('form[name="'+form_name+'"] input[type="checkbox"]').each(function(){
		this.checked = checked;
	});
}

function formClear(obj)
{
	var elem = obj.elements;
	for (var i=0; i<elem.length; i++)
	{
		if (elem[i] && elem[i].name && elem[i].name != 'SELECT' && elem[i].type != 'button' && elem[i].type != 'hidden' && elem[i].type != 'select-one' && elem[i].type != 'radio')
		{
			if (elem[i].type == 'checkbox')
			{
				elem[i].checked = false;
			}
			else
			{
				elem[i].value = '';
			}
		}

		if (elem[i].length) elem[i].selectedIndex = 0;
	}
}

function h(id)
{
	document.getElementById(id).style.display='none';
}

function s(id)
{
	document.getElementById(id).style.display='block';
}

$.ajaxSetup({timeout:30000});
scriptsLoaded++;
