function GetSecondsTxt(sSecondsNum)
{

	sSecondsNum = String(sSecondsNum);
	var sSecondsText = "секунд";
	var nSecondsNumLength = sSecondsNum.length;
	
	if ( sSecondsNum.charAt(nSecondsNumLength - 2) != "1" )
	{
		if (sSecondsNum.charAt(nSecondsNumLength - 1) == "2" || sSecondsNum.charAt(nSecondsNumLength - 1) == "3" || sSecondsNum.charAt(nSecondsNumLength - 1) == "4"){
			sSecondsText = "секунды";
		}else if (sSecondsNum.charAt(nSecondsNumLength - 1) == "1"){
			sSecondsText = "секунду";
		}
	}
	return sSecondsText;
}

function countDown()
{
	var currentTime = parseInt(document.getElementById('timer').innerHTML);
	
	if( currentTime > 1 ) {
		document.getElementById('timer').innerHTML = currentTime - 1;
		document.getElementById('seconds').innerHTML = GetSecondsTxt(currentTime-1);

    	setTimeout('countDown()',1000);
	}
	else
	{
		document.getElementById('timer').innerHTML = "";
		document.getElementById('seconds').innerHTML = "...";
	}
}

function getWorkspaceSize()
{
	return {
		"x":document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth,
		"y":document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight
	};
}

function getScrollSize()
{
	return {
		"x":self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft),
		"y":self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop)
	};
}

function getElementPosition(oElement)
{
    var w = oElement.offsetWidth;
    var h = oElement.offsetHeight;
	
    var l = 0;
    var t = 0;
	
    while (oElement)
    {
        l += oElement.offsetLeft;
        t += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }

    return {"left":l, "top":t, "width": w, "height":h};
}

function mousePageXY(e)
{
  var x = 0, y = 0;

  if (!e) e = window.event;

  if (e.pageX || e.pageY)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }

  return {"x":x, "y":y};
}

function preventDefaultReaction(objEvent)
{
	var objEvent = objEvent || window.event;

	if ( objEvent.preventDefault ) objEvent.preventDefault();
	objEvent.returnValue = false;
}

var oMenu =
{
	// исполняется ли в данный момент запрос к серверу
	bProcessing: false,
	
	// div для вывода сообщения на экран
	awaitNotice: false,
	
	// div для вывода сообщения на экран
	divNotice: false,
	
	// время отображения сообщения на экране
	iShowNoticeTime: 3000,
	
	init: function()
	{
		var allElements = document.getElementsByTagName('*');
		
		if ( allElements.length == 0 )
		{
			allElements = document.all;
		}
		
		this.censusform = false;
		for ( var i = 0; i < allElements.length; i++ )
		{
			if ( allElements[i].id.length > 0 )
			{
				if ( allElements[i].id.length > 0 )
				{
					this[allElements[i].id] = allElements[i];
					
					/*if ( 'censusform' == allElements[i].id )
					{
						allElements[i].onsubmit = function(e)
						{
							this.action = window.location;
							oMenu.sendRequest(this, showResponse);
							preventDefaultReaction(e);
						}
					}*/
				}
			}
		}
		
		this.divNotice = document.createElement('div');
		this.divNotice.style.display = 'none';
		this.divNotice.style.visibility = 'hidden';
		this.divNotice.style.width = '400px';
		this.divNotice.style.padding = '25px';
		this.divNotice.style.backgroundRepeat = 'no-repeat';
		this.divNotice.style.backgroundPosition = '5px 5px';
		this.divNotice.style.fontSize = '12px';
		this.divNotice.style.textAlign = 'center';
		this.divNotice.style.position = 'absolute';
		
		this.awaitNotice = document.createElement('div');
		this.awaitNotice.style.display = 'none';
		this.awaitNotice.style.visibility = 'hidden';
		this.awaitNotice.style.width = '400px';
		this.awaitNotice.style.padding = '25px';
		this.awaitNotice.style.backgroundRepeat = 'no-repeat';
		this.awaitNotice.style.backgroundPosition = '5px 5px';
		this.awaitNotice.style.fontSize = '12px';
		this.awaitNotice.style.textAlign = 'center';
		this.awaitNotice.style.position = 'absolute';
		
		document.body.appendChild(this.awaitNotice);
		document.body.appendChild(this.divNotice);
	},
	
	// выводим сообщение на экран
	showNotice: function(sType, sMessage)
	{
		this.divNotice.className = sType;
		this.divNotice.innerHTML = sMessage;
		this.divNotice.style.display = 'block';
		
		var aWorkSpaceSize = getWorkspaceSize();
		var aScrollSize = getScrollSize();
		
		this.divNotice.style.left = aScrollSize['x']+((aWorkSpaceSize['x'] - this.divNotice.offsetWidth)/2)+'px';
		this.divNotice.style.top = aScrollSize['y']+((aWorkSpaceSize['y'] - this.divNotice.offsetHeight)/2)+'px';		
		
		this.divNotice.style.visibility = 'visible';
		
		setTimeout
		(
		 	function()
			{
				oMenu.divNotice.style.visibility = 'hidden';
			},
			oMenu.iShowNoticeTime
		);
	},
	
	// выводим сообщение на экран
	displayNotice: function(sType, sMessage)
	{
		this.awaitNotice.className = sType;
		this.awaitNotice.innerHTML = sMessage;
		this.awaitNotice.style.display = 'block';
		
		var aWorkSpaceSize = getWorkspaceSize();
		var aScrollSize = getScrollSize();
		
		this.awaitNotice.style.width = '400px';
		
		this.awaitNotice.style.left = aScrollSize['x']+((aWorkSpaceSize['x'] - this.awaitNotice.offsetWidth)/2)+'px';
		this.awaitNotice.style.top = aScrollSize['y']+((aWorkSpaceSize['y'] - this.awaitNotice.offsetHeight)/2)+'px';		
		
		this.awaitNotice.style.visibility = 'visible';
	},
	
	// выводим сообщение на экран
	hideNotice: function()
	{
		this.awaitNotice.style.visibility = 'hidden';
	},
	
	// сообщаем, что в данный момент выполняется соединение
	setProcessing: function(bProcessing)
	{
		this.bProcessing = bProcessing;

		if ( bProcessing )
		{
			this.displayNotice('info', 'Подождите...');
		}
		else
		{
			this.hideNotice();
		}
	},
	
	sendQuestion: function()
	{
		this.sendRequest(this.faqForm, showResponse);
		
		return false;
	},
	
	sendProgramQuery: function()
	{
		this.sendRequest(this.progForm, showResponse);
		
		return false;
	},
	
	sendPressQuery: function()
	{
		if
		(
		 	selectedYear != this.pressForm.year.options[this.pressForm.year.selectedIndex].value
			||
			selectedMonth != this.pressForm.month.options[this.pressForm.month.selectedIndex].value
		)
		{
			this.sendRequest(this.pressForm, showPressResponse);
		}
		
		return false;
	},
	
	sendPartQuery: function()
	{
		if
		(
		 	selectedCountry != oMenu.partForm.country.options[oMenu.partForm.country.selectedIndex].value
			||
			selectedRegion != oMenu.partForm.region.options[oMenu.partForm.region.selectedIndex].value
		)
		{
			this.sendRequest(this.partForm, showPartResponse);
		}
		
		return false;
	},
	
	getRequestBody: function(oForm)
	{
		var aParams = new Array();		
		aParams.push(encodeURIComponent('js')+'='+encodeURIComponent(1));
	
		for ( var i = 0; i < oForm.elements.length; i++ )
		{
			if
			(
				(oForm.elements[i].type != 'radio' && oForm.elements[i].type != 'checkbox')
				||
				(oForm.elements[i].type == 'radio' && oForm.elements[i].checked)
				||
				(oForm.elements[i].type == 'checkbox' && oForm.elements[i].checked)
			)
			{
				aParams.push(encodeURIComponent(oForm.elements[i].name)+'='+encodeURIComponent(oForm.elements[i].value));
			}
		}
	
		return aParams.join('&');
	},
	
	sendRequest: function(oForm, fnCallback)
	{
		if ( this.bProcessing ) return;
		
		try
		{
			this.setProcessing(true);
			var oXmlHttp = zXmlHttp.createRequest();

			oXmlHttp.open('post', oForm.action, true);
			oXmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			oXmlHttp.onreadystatechange = function()
			{
				try
				{
					if ( oXmlHttp.readyState == 4 )
					{
						if ( oXmlHttp.status == 200 )
						{
							fnCallback(oXmlHttp.responseText);
							oMenu.setProcessing(false);
						}
						else
						{
							throw new Error('Ошибка соединения с сервером. Запрос: '+sAction);
						}
					}
				}
				catch ( oException )
				{
					oMenu.setProcessing(false);
					oMenu.showNotice('err', oException.message);
				}
			}
			oXmlHttp.send(this.getRequestBody(oForm));
		}
		catch ( oException )
		{
			oMenu.setProcessing(false);
			oMenu.showNotice('err', oException.message);
		}
	}
}

function showResponse(sResponseText)
{
	if ( sResponseText == -1 )
	{
		oMenu.showNotice('info', '<font color="#FF0000"><b>!!!!</b></font><br />\nПожалуйста, подождите...');
		setTimeout(function(){location.replace('/');}, oMenu.iShowNoticeTime);
		
		return false;
	}
	
	oMenu.showNotice('info', sResponseText);
	
	return false;
}

function showPressResponse(sResponseText)
{
	arrResponse = eval(sResponseText);
	
	if ( selectedYear != oMenu.pressForm.year.options[oMenu.pressForm.year.selectedIndex].value )
	{
		months = arrResponse[1];
		
		for (var i=oMenu.pressForm.month.options.length-1; i >= 0; i--)
		{
			oMenu.pressForm.month.remove(i);
		}
		
		for ( var j=0; j<months.length; j++ )
		{
			oOption = document.createElement("option");
			oOption.appendChild(document.createTextNode(ruMonths[(months[j]-1)]));
			oOption.setAttribute("value", months[j]);
			
			oMenu.pressForm.month.appendChild(oOption);
		}
		
		selectedYear = oMenu.pressForm.year.options[oMenu.pressForm.year.selectedIndex].value;
		
		articles = arrResponse[2];
		links = '';
		for ( var x=0; x<articles.length; x++ )
		{
			links += '<a href="piece'+arrResponse[0]+'page'+articles[x].id+'.php">'+articles[x].date+': '+articles[x].name+'</a>\n';
		}
		
		oMenu.pressArticles.innerHTML = links;
		
		selectedMonth = oMenu.pressForm.month.options[oMenu.pressForm.month.selectedIndex].value;
		oMenu.pressForm.showArticles.style.visibility = 'hidden';
	}
	else if ( selectedMonth != oMenu.pressForm.month.options[oMenu.pressForm.month.selectedIndex].value )
	{
		articles = arrResponse[2];
		links = '';
		for ( var x=0; x<articles.length; x++ )
		{
			links += '<a href="piece'+arrResponse[0]+'page'+articles[x].id+'.php">'+articles[x].date+' '+selectedYear+': '+articles[x].name+'</a>\n';
		}
		
		oMenu.pressArticles.innerHTML = links;
		
		selectedMonth = oMenu.pressForm.month.options[oMenu.pressForm.month.selectedIndex].value;
		oMenu.pressForm.showArticles.style.visibility = 'hidden';
	}

	return false;
}

function showPartResponse(sResponseText)
{
	arrResponse = eval(sResponseText);
	
	regions = arrResponse[0];
	partners = arrResponse[1];
	
	if ( selectedCountry != oMenu.partForm.country.options[oMenu.partForm.country.selectedIndex].value )
	{
		for (var i=oMenu.partForm.region.options.length-1; i >= 0; i--)
		{
			oMenu.partForm.region.remove(i);
		}
		
		for ( var j=0; j<regions.length; j++ )
		{
			oOption = document.createElement("option");
			oOption.appendChild(document.createTextNode(regions[j].name));
			oOption.setAttribute("value", regions[j].id);
			
			oMenu.partForm.region.appendChild(oOption);
		}
		
		selectedCountry = oMenu.partForm.country.options[oMenu.partForm.country.selectedIndex].value;
		
		oMenu.displayPartners.innerHTML = '';
		for ( var x=0; x<partners.length; x++ )
		{
			oDiv = document.createElement("div");
			oDiv.innerHTML = '<h4>'+partners[x].name+'</h4>\n'+partners[x].text;
			oMenu.displayPartners.appendChild(oDiv);
		}
		
		selectedRegion = oMenu.partForm.region.options[oMenu.partForm.region.selectedIndex].value;
		oMenu.partForm.showPartners.style.visibility = 'hidden';
	}
	else if ( selectedRegion != oMenu.partForm.region.options[oMenu.partForm.region.selectedIndex].value )
	{
		oMenu.displayPartners.innerHTML = '';
		for ( var x=0; x<partners.length; x++ )
		{
			oDiv = document.createElement("div");
			oDiv.innerHTML = '<h4>'+partners[x].name+'</h4>\n'+partners[x].text;
			oMenu.displayPartners.appendChild(oDiv);
		}
		
		selectedRegion = oMenu.partForm.region.options[oMenu.partForm.region.selectedIndex].value;
		oMenu.partForm.showPartners.style.visibility = 'hidden';
	}

	return false;
}

window.onload = function()
{
	theObjects = document.getElementsByTagName("object");
	for (var i = 0; i < theObjects.length; i++)
	{
		theObjects[i].outerHTML = theObjects[i].outerHTML;
	}
	
	oMenu.init();

	if ( 'object' == typeof(oMenu.censusform) && oMenu.censusform.formType.value == SESSION.formType )
	{
		for ( var i in SESSION )
		{
			if ( 'string' == typeof(SESSION[i]) )
			{
				if
				(
				 	'object' == typeof(oMenu.censusform.elements[i])
					&&
					(
					 	'text' == oMenu.censusform.elements[i].type
						||
						'file' == oMenu.censusform.elements[i].type
						||
						'textarea' == oMenu.censusform.elements[i].type
					)
					&&
					0 < SESSION[i].length
				)
				{
					oMenu.censusform.elements[i].value = SESSION[i];
				}
				else if ( 'object' == typeof(oMenu.censusform.elements[i]) && 'object' == typeof(oMenu.censusform.elements[i].options) )
				{
					intLength = oMenu.censusform.elements[i].options.length;
					for ( var j=0; j < intLength; j++ )
					{
						if ( oMenu.censusform.elements[i].options[j].value == SESSION[i] )
						{
							oMenu.censusform.elements[i].options[j].selected = true;
						}
					}
				}
			}
			else if ( 0 < SESSION[i].length && 'object' == typeof(oMenu.censusform.elements[i+'[]']) && 'checkbox' == oMenu.censusform.elements[i+'[]'][0].type )
			{
				intLength = SESSION[i].length;
				chbLength = oMenu.censusform.elements[i+'[]'].length;
				for ( var x = 0; x < chbLength; x++ )
				{
					for ( var z = 0; z < intLength; z++ )
					{
						if ( SESSION[i][z] == oMenu.censusform.elements[i+'[]'][x].value )
						{
							oMenu.censusform.elements[i+'[]'][x].checked = true;
						}
					}
				}
			}
			else if ( 0 < SESSION[i].length )
			{
				intLength = SESSION[i].length;
				for ( var x = 0; x < intLength; x++ )
				{
					if ( 0 < SESSION[i][x].length && oMenu.censusform.elements[i+'[]']  )
					{
						oMenu.censusform.elements[i+'[]'][x].value = SESSION[i][x];
					}
				}
			}
		}
		
		if ( 'object' == typeof(oMenu.censusform.data9) && 'function' == typeof(showData10) )
		{
			showData10(oMenu.censusform.data9);
		}
		
		if ( 'object' == typeof(oMenu.censusform['data11[]']) && 'function' == typeof(showData12) )
		{
			showData12(oMenu.censusform['data11[]'][7]);
		}
		
		if ( 'object' == typeof(oMenu.censusform['data11']) && 'function' == typeof(showData12) )
		{
			showData12(oMenu.censusform['data11']);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data13) && 'function' == typeof(showData14) )
		{
			showData14(oMenu.censusform.data13);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data14) && 'function' == typeof(showData15) )
		{
			showData15(oMenu.censusform.data14);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data17) && 'function' == typeof(showData18) )
		{
			showData18(oMenu.censusform.data17);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data19) && 'function' == typeof(showData20) )
		{
			showData20(oMenu.censusform.data19);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data21) && 'function' == typeof(showData22) )
		{
			showData22(oMenu.censusform.data21);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data23) && 'function' == typeof(showData24) )
		{
			showData24(oMenu.censusform.data23);
		}

		if ( 'object' == typeof(oMenu.censusform.data24) && 'function' == typeof(showData25) )
		{
			showData25(oMenu.censusform.data24);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data25) && 'function' == typeof(showData26) )
		{
			showData26(oMenu.censusform.data25);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data26) && 'function' == typeof(showData27) )
		{
			showData27(oMenu.censusform.data26);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data27) && 'function' == typeof(showData28) )
		{
			showData28(oMenu.censusform.data27);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data28) && 'function' == typeof(showData29) )
		{
			showData29(oMenu.censusform.data28);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data30) && 'function' == typeof(showData31) )
		{
			showData31(oMenu.censusform.data30);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data31) && 'function' == typeof(showData32) )
		{
			showData32(oMenu.censusform.data31);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data33) && 'function' == typeof(showData34) )
		{
			showData34(oMenu.censusform.data33);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data34) && 'function' == typeof(showData35) )
		{
			showData35(oMenu.censusform.data34);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data36) && 'function' == typeof(showData37) )
		{
			showData37(oMenu.censusform.data36);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data37) && 'function' == typeof(showData38) )
		{
			showData38(oMenu.censusform.data37);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data39) && 'function' == typeof(showData40) )
		{
			showData40(oMenu.censusform.data39);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data41) && 'function' == typeof(showData42) )
		{
			showData42(oMenu.censusform.data41);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data43) && 'function' == typeof(showData44) )
		{
			showData44(oMenu.censusform.data43);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data46) && 'function' == typeof(showData47) )
		{
			showData47(oMenu.censusform.data46);
		}
		
		if ( 'object' == typeof(oMenu.censusform['data48[]']) && 'function' == typeof(showData49) )
		{
			showData49(oMenu.censusform['data48[]'][4]);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data50) && 'function' == typeof(showData51) )
		{
			showData51(oMenu.censusform.data50);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data51) && 'function' == typeof(showData52) )
		{
			showData52(oMenu.censusform.data51);
		}
		
		if ( 'object' == typeof(oMenu.censusform.data29) && 'function' == typeof(showData55) )
		{
			showData55(oMenu.censusform.data29);
		}
	}
}


