var PatternEmail = new RegExp ("^([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9\.\-]+)\.([a-zA-Z]{2,4})$");

function showTooltip(id) {
	var oDiv = document.getElementById(id);
	var self = oDiv.parentNode;
	self.className = 'active';
	oDiv.style.display = 'block';
}

function hideTooltip(id) {
	var oDiv = document.getElementById(id);
	var self = oDiv.parentNode;
	self.className = '';
	oDiv.style.display = 'none';
}

function checkSubscribe(oForm)
{
	if (oForm.name.value == '')
	{
		alert('Не введено Имя');
		oForm.name.focus();
		return false;
	}
	if (oForm.email.value == '')
	{
		alert('Не введен адрес электронной почты');
		oForm.email.focus();
		return false;
	}
	
	if(!PatternEmail.test(oForm.email.value))
	{
		alert('Некорректный адрес электронной почты');
		oForm.email.focus();
		return false;
	}
	return true;
}

function checkUnSubscribe(oForm)
{
	if (oForm.email.value == '')
	{
		alert('Не введен адрес электронной почты');
		oForm.email.focus();
		return false;
	}
	
	if(!PatternEmail.test(oForm.email.value))
	{
		alert('Некорректный адрес электронной почты');
		oForm.email.focus();
		return false;
	}
	return true;
}

function showBranch(ID)
{
	var oNode = document.getElementById(ID);
	oNode.style.display =
		oNode.style.display == 'none' ? 'block' : 'none';
	if (oNode.style.display == 'none')
		document.getElementById('i'+ID).style.backgroundImage = 'url(/gfx/row-close.gif)';
	else document.getElementById('i'+ID).style.backgroundImage = 'url(/gfx/row-open.gif)'
}

function commentAnswer(id)
{
	var oForm = document.forms.commentForm;
	oForm.ParentID.value = id;
	document.location = '#commentForm';
	return false;
}

function checkReviewForm(oForm)
{
	if (oForm.name.value == '')
	{
		alert('Необходимо ввести имя!');
		oForm.name.focus();
		return false;
	}
	if (oForm.message.value == '')
	{
		alert('Необходимо ввести сообщение!');
		oForm.message.focus();
		return false;
	}
	return true;
}
function checkReferForm(oForm)
{
	if (oForm.name.value == '')
	{
		alert('Необходимо ввести имя!');
		oForm.name.focus();
		return false;
	}
	if (oForm.email.value == '')
	{
		alert('Необходимо ввести E-mail!');
		oForm.email.focus();
		return false;
	}
	return true;
}

function checkQuestForm(oForm)
{
	if (oForm.name.value == '' && oForm.name.disabled == false)
	{
		alert('Необходимо ввести имя!');
		oForm.name.focus();
		return false;
	}
	if (
		(oForm.email.value == '' && oForm.email.disabled == false) &&
		(oForm.phone.value == '' && oForm.phone.disabled == false)
	)
	{
		alert('Необходимо заполнить контактную информацию!');
		oForm.email.focus();
		return false;
	}
	if (oForm.question.value == '')
	{
		alert('Необходимо ввести вопрос!');
		oForm.question.focus();
		return false;
	}
}

function checkSuggestForm(oForm)
{
	if (oForm.name.value == '' && oForm.name.disabled == false)
	{
		alert('Необходимо ввести имя!');
		oForm.name.focus();
		return false;
	}
	if (oForm.company.value == '' && oForm.company.disabled == false)
	{
		alert('Необходимо ввести название компании!');
		oForm.comapny.focus();
		return false;
	}
	if (
		(oForm.email.value == '' && oForm.email.disabled == false) &&
		(oForm.phone.value == '' && oForm.phone.disabled == false)
	)
	{
		alert('Необходимо заполнить контактную информацию!');
		oForm.email.focus();
		return false;
	}
	if (oForm.theme.value == '')
	{
		alert('Необходимо ввести тему мероприятия!');
		oForm.theme.focus();
		return false;
	}
	return true;
}

function switchTab(sTab)
{
	if (typeof(sTab) == 'string')
	{
		scrollTo(0, 0);
	}
	else
	{
		sTab = this.id;
	}
	for (var i in jTabs)
	{
		var oTabEl = G('#'+i);
		var oTabCont = G('#'+jTabs[i]);
		if (!oTabEl || !oTabCont)
			continue;
		if (i == sTab)
		{
			oTabEl.setClass('sactive');
			oTabCont.show();
		}
		else
		{
			oTabEl.setClass('');
			oTabCont.hide();
		}
	}
	return false;
}

function checkUserEN(oForm)
{
	if (oForm.ClientCompany.value == '')
	{
		alert('Please enter the name of the company name');
		oForm.ClientCompany.focus();
		return false;
	}
	if (oForm.ClientAddr.value == '')
	{
		alert('Please enter your address');
		oForm.ClientAddr.focus();
		return false;
	}
	if (oForm.ClientTown.value == '')
	{
		alert('Please entrt your city');
		oForm.ClientTown.focus();
		return false;
	}
	if (oForm.ClientZP.value == '')
	{
		alert('Please enter your zip code');
		oForm.ClientZP.focus();
		return false;
	}
	if (oForm.ClientCountry.value == '')
	{
		alert('Please enter your country');
		oForm.ClientCountry.focus();
		return false;
	}
	if (oForm.ClientName.value == '')
	{
		alert('Please enter your name');
		oForm.ClientName.focus();
		return false;
	}
	if (oForm.ClientEmail.value == '')
	{
		alert('Please enter your E-mail');
		oForm.ClientEmail.focus();
		return false;
	}
	if (oForm.ClientPhone.value == '')
	{
		alert('Please enter your phone number');
		oForm.ClientPhone.focus();
		return false;
	}
	if (oForm.ClientFax.value == '')
	{
		alert('Please enter your fax');
		oForm.ClientFax.focus();
		return false;
	}
	return false;
}

function checkUser(oForm, lang)
{
	var jErrors = jOrderFormMessages[lang];

	if (oForm.FormPairAddr.value == '')
	{
		alert(jErrors.addr);
		oForm.FormPairAddr.focus();
		return false;
	}
	if (oForm.FormPairNameC.value == '')
	{
		alert(jErrors.pnc);
		oForm.FormPairNameC.focus();
		return false;
	}
	if (oForm.FormPairPhone.value == '')
	{
		alert(jErrors.fpp);
		oForm.FormPairPhone.focus();
		return false;
	}
	if (oForm.FormPairFax.value == '')
	{
		alert(jErrors.fpf);
		oForm.FormPairFax.focus();
		return false;
	}

	return true;
}

function checkOrderEN(oForm)
{
	var oTPairs = G('#OrderPairsInfo').tBodies[0].rows;
	var count = oTPairs.length;
	var flag = false;

	var aInput = G('#OrderPairsInfo').getElementsByTagName('INPUT');
	var cnt = aInput.length;
	for (var i=0; i<cnt; i++)
	{
		if (aInput[i].value == '')
		{
			alert('Required fields are not filld');
			aInput[i].focus();
			return false;
		}
	}

	/*for (var i=1; i<count; i++)
	{
		var oInput = oTPairs[i].cells[0].getElementsByTagName('INPUT')[0];
		if (oInput.value != '')
		{
			flag = true;
			break;
		}
	}
	if (flag == false)
	{
		alert('You have to input any employer!');
		G('#OrderCountSelect').focus();
		return false;
	}*/

	if (!oForm.FormPairCompany.disabled && oForm.FormPairCompany.value == '')
	{
		alert('Please enter the name of the company');
		oForm.FormPairCompany.focus();
		return false;
	}
	if (!oForm.FormPairAddr.disabled && oForm.FormPairAddr.value == '')
	{
		alert('Please enter your address');
		oForm.FormPairAddr.focus();
		return false;
	}
	if (!oForm.FormPairTown.disabled && oForm.FormPairTown.value == '')
	{
		alert('Please enter your city');
		oForm.FormPairTown.focus();
		return false;
	}
	if (!oForm.FormPairZP.disabled && oForm.FormPairZP.value == '')
	{
		alert('Please enter your ZIP code');
		oForm.FormPairZP.focus();
		return false;
	}
	if (!oForm.FormPairCountry.dsiabled && oForm.FormPairCountry.value == '')
	{
		alert('Please enter your country');
		oForm.FormPairCountry.focus();
		return false;
	}
	if (!oForm.FormPairNameC.disabled && oForm.FormPairNameC.value == '')
	{
		alert('Please enter the name of the contact person');
		oForm.FormPairNameC.focus();
		return false;
	}
	if (!oForm.FormPairEmail.disabled && oForm.FormPairEmail.value == '')
	{
		alert('Please enter contact person’s  E-mail');
		oForm.FormPairEmail.focus();
		return false;
	}
	if(!oForm.FormPairEmail.disabled)
	{
		if(!PatternEmail.test(oForm.FormPairEmail.value))
		{
			alert('Please enter correct E-mail');
			oForm.FormPairEmail.focus();
			return false;
		}
	}
	
	
	if (!oForm.FormPairPhone.disabled && oForm.FormPairPhone.value == '')
	{
		alert('Please enter contact person’s  telephone number');
		oForm.FormPairPhone.focus();
		return false;
	}
	if (!oForm.FormPairFax.disabled && oForm.FormPairFax.value == '')
	{
		alert('Please enter contact person’s  fax number');
		oForm.FormPairFax.focus();
		return false;
	}
	return true;
}

function checkOrder(oForm, lang)
{
	var jErrors = jOrderFormMessages[lang];
	var oTPairs = G('#OrderPairsInfo').tBodies[0].rows;
	var count = oTPairs.length;
	var flag = false;

	var aInput = G('#OrderPairsInfo').getElementsByTagName('INPUT');
	var cnt = aInput.length;
	for (var i=0; i<cnt; i++)
	{
		if (aInput[i].value == '')
		{
			alert('Не заполнены обязательные поля');
			aInput[i].focus();
			return false;
		}
	}
	/*for (var i=1; i<count; i++)
	{
		var oInput = oTPairs[i].cells[0].getElementsByTagName('INPUT')[0];
		if (oInput.value != '')
		{
			flag = true;
			break;
		}
	}
	if (flag == false)
	{
		alert(jErrors.empl);
		G('#OrderCountSelect').focus();
		return false;
	}*/
	if (oForm.FormPairCompany.value == '' && !oForm.FormPairCompany.disabled)
	{
		alert(jErrors.compn);
		oForm.FormPairCompany.focus();
		return false;
	}
	if (oForm.FormPairAddr.value == '' && !oForm.FormPairAddr.disabled)
	{
		alert(jErrors.addr);
		oForm.FormPairAddr.focus();
		return false;
	}
	if (oForm.OrderCity.value == 0 && !oForm.OrderCity.disabled)
	{
		alert(jErrors.fci);
		oForm.OrderCity.focus();
		return false;
	}
	if (oForm.FormPairNameC.value == '' && !oForm.FormPairNameC.disabled)
	{
		alert(jErrors.pnc);
		oForm.FormPairNameC.focus();
		return false;
	}
	if (oForm.FormPairPhone.value == '' && !oForm.FormPairPhone.disabled)
	{
		alert(jErrors.fpp);
		oForm.FormPairPhone.focus();
		return false;
	}
	if (oForm.FormPairFax.value == '' && !oForm.FormPairFax.disabled)
	{
		alert(jErrors.fpf);
		oForm.FormPairFax.focus();
		return false;
	}
	if (oForm.FormPairEmail.value == '' && !oForm.FormPairEmail.disabled)
	{
		alert(jErrors.fpe);
		oForm.FormPairEmail.focus();
		return false;
	}
	
	if(!oForm.FormPairEmail.disabled)
	{
		if(!PatternEmail.test(oForm.FormPairEmail.value))
		{
			alert(jErrors.fpewrong);
			oForm.FormPairEmail.focus();
			return false;
		}
	}

	if (oForm.OrderSureInput.checked == false)
	{
		alert(jErrors.foc);
		oForm.OrderSureInput.focus();
		return false;
	}
	return true;
}

function switchSubmit(oInput)
{
	G('#OrderSubmit').disabled = !oInput.checked;
}

var jOrderFormMessages = {
	ru:{
		empl:'Необходимо ввести хотя бы одного сотрудника!',
		compn:'Необходимо ввести наименование организации!',
		addr:'Необходимо ввести юридический адрес!',
		pnc:'Необходимо ввести контактное лицо',
		fpp:'Необходимо ввести телефон контактного лица',
		fpf:'Необходимо ввести факс',
		fpe:'Необходимо ввести E-mail',
		fpewrong:'Некорректное значение поля E-mail',
		fpwd:'Необходимо ввести пароль для входа в личный кабинет',
		fci:'Необходимо выбрать город',
		fcomp:'Необходимо ввести наименование организации',
		foc:'Для регистрации необходимо ваше согласие с условиями договора в пункте "С условиями договора согласен :"'
	},
	en:{
		empl:'Please enter any emploer',
		compn:'Please enter your name of your company',
		addr:'Please enter your address',
		pnc:'Please enter your name',
		fpp:'Please enter your phone',
		fpf:'Please enter your fax',
		fpe:'Please enter your E-mail',
		fpewrong:'E-mail is not valid',
		fpwd:'Please enter your password',
		fci:'Please enter your city',
		fcomp:'Please enter your name of your company',
		foc:'To register you must agree to the terms of the contract'
	}
}

function OpenWindow(url, w, h, s)
{
	s = s || 0;
	window.open(url, '', 'toolbar=0,menubar=0,location=0,status=0,scrollbars='+s+',resizable=0,height='+h+',width='+w+',left=50,top=50');
}

function flip(item)
{
	if (item.style.display == 'none')
	{
		item.style.display = 'inline';
	}
	else
	{
		item.style.display = 'none';
	}
}

function scrolled()
{
	parent.leftFrame.document.body.scrollTop=document.body.scrollTop;
	parent.rightFrame.document.body.scrollTop=document.body.scrollTop;
}

function G(selector, container)
{
	container = container || document;
	var oNode = null;
	if (selector.substr(0, 1) == '#')
		oNode = document.getElementById(selector.substr(1)) || new Object();
	else if (selector == '*')
		return container.getElementsByTagName('*');
	else if (/^[a-z0-9]*\.[a-z0-9_-]+$/i.test(selector))
	{
		var aPart = selector.split('.');
		var aNodes = [];
		var sTagName = aPart[0] == '' ? '*' : aPart[0].toUpperCase();
		var aTNodes = container.getElementByTagName(sTagName);
		var count = aTNodes.length;
		if (count == 0) return false;
		for (var i=0; i<count; i++)
		{
			if (aTNodes[i].className == aPart[1])
				aNodes.push(aTNodes[i]);
		}
		return aNodes;
	}
	else if (/^[a-z0-9]+$/i.test(selector))
	{
		return container.getElementsByTagName(selector.toUpperCase()) || false;
	}
	else return false;

	if (!!oNode && typeof(oNode) == 'object')
	{
		oNode.hide = function()
		{
			this.style.display = 'none';
			return this;
		}

		oNode.show = function()
		{
			this.style.display = '';
			return this;
		}
		oNode.setClass = function(sClass)
		{
			this.className = sClass;
			return this;
		}
	}
	return oNode;
}

function setDiscount()
{
	var oDSelect = G('#OrderCountSelect');
	for (var i in jOPDiscount)
	{
		oDSelect.options.add(new Option(jOPDiscount[i].num, jOPDiscount[i].num));
	}
	G('#OrderTotalPrice').value = '';
}

function calculate()
{
	if (G('#OrderCitySelect').value == 0)
		return false;
	var iOrderCount = G('#OrderCountSelect').value;
	var fResultPrice = 0;
	var oTPTable = G('#OrderPairsInfo').tBodies[0];
	var iAdditionalDiscount = G('#OrderCitySelect').value == 1 ? 0 : OrderOCDiscount;
	for (var i=0; i<iOrderCount; i++)
	{
		var oInput = G('input', oTPTable.rows[i+1].cells[0])[0];
		if (oInput.value != '')
		{
			fResultPrice += OrderPPrice*(1 - (jOPDiscount['n'+(i+1)].discount + iAdditionalDiscount)/100);
		}
	}
	G('#OrderTotalPrice').value = fResultPrice == 0 ? '' : fResultPrice;
}

var oPRowTpl = null;
var oTPContainer = null;

function setPairs(append)
{
	oPRowTpl = G('#PairRowTemplate').tBodies[0].rows[0];
	oTPContainer = G('#OrderPairsInfo').tBodies[0];
	G('#OrderCountSelect').onchange = setPairsOC;
	if (!!append)
	setPairsOC();
}
function setPairsEn(append)
{
	oPRowTpl = G('#PairRowTemplate').tBodies[0].rows[0];
	oTPContainer = G('#OrderPairsInfo').tBodies[0];
	G('#OrderCountSelect').onchange = setPairsOCEn;
	if (!!append)
	setPairsOCEn();
}

function setPairsOC()
{
	var iNum = G('#OrderCountSelect').value;
	var count = oTPContainer.rows.length;
	for (var i=count-1; i>0; i--)
	{
		oTPContainer.removeChild(oTPContainer.rows[i]);
	}
	for (i=0; i<iNum; i++)
	{
		var oTmpRow = oPRowTpl.cloneNode(true);
		var aInput = G('input', oTmpRow);
		aInput[0].setAttribute('name', 'OrderPPairName1['+(i+1)+']');
		aInput[0].onblur = calculate;

		aInput[1].setAttribute('name', 'OrderPPairName2['+(i+1)+']');
		aInput[1].onblur = calculate;

		aInput[2].setAttribute('name', 'OrderPPairName3['+(i+1)+']');
		aInput[2].onblur = calculate;

		aInput[3].setAttribute('name', 'OrderPPairPost['+(i+1)+']');

		G('td', oTmpRow)[4].innerHTML = jOPDiscount['n'+(i+1)].discount+'%';
		//oTmpRow.cells[4].innerHTML = jOPDiscount['n'+(i+1)].discount+'%';
		oTPContainer.appendChild(oTmpRow);
		oTmpRow = null;
	}
	G('#OrderTotalPrice').value = '';
}

function setPairsOCEn()
{
	var iNum = G('#OrderCountSelect').value;
	var count = oTPContainer.rows.length;
	for (var i=count-1; i>0; i--)
	{
		oTPContainer.removeChild(oTPContainer.rows[i]);
	}
	for (i=0; i<iNum; i++)
	{
		var oTmpRow = oPRowTpl.cloneNode(true);
		var aInput = G('input', oTmpRow);
		aInput[0].setAttribute('name', 'OrderPPairName1['+(i+1)+']');
		aInput[0].onblur = calculate;

		aInput[1].setAttribute('name', 'OrderPPairName2['+(i+1)+']');
		aInput[1].onblur = calculate;

		aInput[2].setAttribute('name', 'OrderPPairPost['+(i+1)+']');
		aInput[2].onblur = calculate;

		G('select', oTmpRow)[0].setAttribute('name', 'OrderPPairTreat['+(i+1)+']');
		oTPContainer.appendChild(oTmpRow);
		oTmpRow = null;
	}
	G('#OrderTotalPrice').value = '';
}

// snow

/*
var no = 35; // namber of snowflakes
var speed = 6; // speed of snowflake
var snowflake = "/gfx/1.gif";

var dx, xp, yp;
var am, stx, sty;
var i, doc_width = 800, doc_height = 600;

dx = new Array();
xp = new Array();
yp = new Array();
am = new Array();
stx = new Array();
sty = new Array();
for (i = 0; i < no; ++ i) {
	dx[i] = 0;
	xp[i] = Math.random()*(doc_width-50);
	yp[i] = Math.random()*doc_height;
	am[i] = Math.random()*20;
	stx[i] = 0.02 + Math.random()/10;
	sty[i] = 0.7 + Math.random();
	if (i == 0) {
		document.write("<div id=\"dot"+ i +"\" style=\"POSITION: ");
		document.write("absolute; Z-INDEX: "+ i +"; VISIBILITY: ");
		document.write("visible; TOP: 15px; LEFT: 15px;\"><img src=\"");
		document.write(snowflake + "\" border=\"0\"></div>");
	} else {
		document.write("<div id=\"dot"+ i +"\" style=\"POSITION: ");
		document.write("absolute; Z-INDEX: "+ i +"; VISIBILITY: ");
		document.write("visible; TOP: 15px; LEFT: 15px;\"><img src=\"");
		document.write(snowflake + "\" border=\"0\"></div>");
	}
}

function snowIE() {
	for (i = 0; i < no; ++ i) {
		yp[i] += sty[i];
		if (yp[i] > doc_height-50) {
			xp[i] = Math.random()*(doc_width-am[i]-30);
			yp[i] = 0;
			stx[i] = 0.02 + Math.random()/10;
			sty[i] = 0.7 + Math.random();
			//doc_width = self.innerWidth;
			//doc_height = self.innerHeight;
		}
		dx[i] += stx[i];
		document.getElementById("dot"+i).style.top = yp[i];
		document.getElementById("dot"+i).style.left = xp[i] + am[i]*Math.sin(dx[i]);
	}
	setTimeout("snowIE()", speed);
}

window.onload = function()
{
	doc_width = document.body.clientWidth;
	doc_height = document.body.clientHeight;
	snowIE();
}*/