$(document).ready(function() {
	var USD = $("span#USD").text();
	var EUR = $("span#EUR").text();
	var RUB = $("span#RUB").text();

	$("table.TourDataActive").each(function () {
		var priceMenu = document.createElement('select');
		var option1 = document.createElement('option');
		var option2 = document.createElement('option');
		var option3 = document.createElement('option');
		var option4 = document.createElement('option');

		option1.setAttribute('value', '0');
		option1_text = document.createTextNode('UAH');
		option1.appendChild(option1_text);

		option2.setAttribute('value', '1');
		option2_text = document.createTextNode('USD');
		option2.appendChild(option2_text);

		option3.setAttribute('value', '2');
		option3_text = document.createTextNode('EUR');
		option3.appendChild(option3_text);

		option4.setAttribute('value', '3');
		option4_text = document.createTextNode('RUB');
		option4.appendChild(option4_text);

		priceMenu.appendChild(option1);
		priceMenu.appendChild(option2);
		priceMenu.appendChild(option3);
		priceMenu.appendChild(option4);

		priceMenu.className = "priceMenu";

		$(this).wrap("<div class=\"priceBlock\" />");

		var tableParent = $(this).parent();
		tableParent.prepend(priceMenu);

		$("select.priceMenu").change(function () {
			var table = $(this).next();
			var value = $(this).children("option:selected").text();
			$(table).find("p.priceCell").each(function() {
				var rawPrice = $(this).children(".rawPrice").text();
				var actualPrice = $(this).children(".actualPrice").text();
				var i = rawPrice.indexOf("/");
				if(value == 'USD') {
					if (i >=0)
					{
						var prices = rawPrice.split("/");
						$(this).children(".actualPrice").text(Math.floor(prices[0]/USD) + '/' + Math.floor(prices[1]/USD));
					}else{
						$(this).children(".actualPrice").text(Math.floor(rawPrice/USD));
					}
				}
				if(value == 'EUR') {
					if (i >=0)
					{
						var prices = rawPrice.split("/");
						$(this).children(".actualPrice").text(Math.floor(prices[0]/EUR) + '/' + Math.floor(prices[1]/EUR));
					}else{
						$(this).children(".actualPrice").text(Math.floor(rawPrice/EUR));
					}
				}
				if(value == 'RUB') {
					if (i < 0)
					{
						$(this).children(".actualPrice").text(Math.floor(rawPrice/RUB));
					}else{
						var prices = rawPrice.split("/");
						$(this).children(".actualPrice").text(Math.floor(prices[0]/RUB) + '/' + Math.floor(prices[1]/RUB));
					}
				}
				if(value == 'UAH') {
					
						$(this).children(".actualPrice").text(rawPrice);
					
				}		
			});
			
		});
	});

	$("table.TourDataActive td p").each(function () {
		var cellData = $(this).text();
		if(cellData.match(/^[\d\/]+$/)) {
			$(this).addClass("priceCell");
			$(this).html("<span class=\"actualPrice\">" + $(this).text() + "</span><span class=\"rawPrice\">" + $(this).text() + "</span>");
		}
	});
});

function messageBox(MessageText)
{
 Message=window.open("", "Message", "toolbar=no, resizable=no, scrollbars=no, width=280, height=140");
 Message.document.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=WINDOWS-1251" /><title>Системное сообщение</title><link rel="stylesheet" href="/stylePopUp.css" type="text/css"></head>');
 Message.document.write('<body bgcolor="#FFFFFF"><table style="width: 100%; height: 100%;"><tr><td style="width: 100%; height: 100%; vertical-align: middle; font-family: arial, sans-serif; font-size: 80%; color: #333333;"><p style="text-align: center;">' + MessageText + '<br /><br /><a href="" onClick="window.close(); return false;" style="color: #008bda;"">Закрыть окно</a></p></td></tr></table><script language="JavaScript">var TimerRunning=true; TimeID=setTimeout("window.close()", 5000);</script></body></html>');
 Message.focus();
}


function OpenPhoto(src, title, width, height)
{
 open(src, title, "toolbar=no, resizable=yes, scrollbars=no, width="+(width+16)+", height="+(height+24));
}

function OpenPhotoH(src, title)
{
 open(src, title, "toolbar=no, resizable=yes, scrollbars=no, width=496, height=316");
}

function OpenPhotoV(src, title)
{
 open(src, title, "toolbar=no, resizable=yes, scrollbars=no, width=316, height=496");
}

function toggleOff() {
 Controls = document.CompareForm.elements;
 ControlsCount = Controls.length;
 for (var i=1; i<ControlsCount; i++)
 {
  if (Controls[i].checked == false)
   Controls[i].disabled = true;
 }
};


function toggleOn() {
 Controls = document.CompareForm.elements;
 ControlsCount = Controls.length;
 for (var i=1; i<ControlsCount; i++)
 {
  if (Controls[i].checked == false)
   Controls[i].disabled = false;
 }
};


function subscriptionFormCheck(form)
{
 ErrorFlag=0;
 if (form.EMail.value=='')
 {
  alert('Введіть адресу Вашої електронної пошти!');
  return false;
 }
 if (form.Password.value=='')
 {
  alert('Введіть Ваш пароль!');
  return false;
 }
 if (form.PasswordAgain.value=='')
 {
  alert('Повторіть введення паролю!');
  return false;
 }
 else
 {
  if (form.Password.value != form.PasswordAgain.value)
  {
   alert('Введені паролі не співпадають!');
   return false;
  }
 }
 return true;
}


function tourSelectFormCheck(form)
{
 ErrorFlag=0;
 if ((form.Country.value=='') || (form.TourStart.value=='') || (form.TourEnd.value=='') || (form.TourDuration.value=='') || (form.HotelCategory.value=='') || ((form.Tourists.value=='') && (form.TouristsOther.value=='')) || (form.Meals.value=='') || (form.Name.value=='') || (form.Phone.value==''))
 {
  alert("Заполните все обязательные поля!");
  return false;
 }
 return true;
}


function askQuestionFormCheck(form)
{
 ErrorFlag=0;
 if ((form.Name.value=='') || (form.PhoneEMail.value=='') || (form.Question.value==''))
 {
  alert("Заполните все обязательные поля!");
  return false;
 }
 return true;
}


function feedbackFormCheck(form)
{
 ErrorFlag=0;
 if ((form.Name.value=='') || (form.EMail.value=='') || (form.Message.value==''))
 {
  alert("Заповніть всі обов'язкові поля!");
  return false;
 }
 return true;
}
