
function setElementOpacity(elem, nOpacity)
{
  var opacityProp = getOpacityProperty();

  if (!elem || !opacityProp) return; // Если не существует элемент с указанным id или браузер не поддерживает ни один из известных функции способов управления прозрачностью
  
  if (opacityProp=="filter")  // Internet Exploder 5.5+
  {
    nOpacity *= 100;
	
    // Если уже установлена прозрачность, то меняем её через коллекцию filters, иначе добавляем прозрачность через style.filter
    var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
    if (oAlpha) oAlpha.opacity = nOpacity;
    else 
	elem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";	 // Для того чтобы не затереть другие фильтры используем "+="
  }
  else // Другие браузеры
    elem.style[opacityProp] = nOpacity;
}

function getOpacityProperty()
{
  if (typeof document.body.style.opacity == 'string') // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9)
    return 'opacity';
  else if (typeof document.body.style.MozOpacity == 'string') // Mozilla 1.6 и младше, Firefox 0.8 
    return 'MozOpacity';
  else if (typeof document.body.style.KhtmlOpacity == 'string') // Konqueror 3.1, Safari 1.1
    return 'KhtmlOpacity';
  else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) // Internet Exploder 5.5+
    return 'filter';

  return false; //нет прозрачности
}

function HideAllBody(nOpacity, type)
{
	var type = type || "";
	setElementOpacity(document.getElementById("OverDiv" + type), 1-nOpacity);
	document.getElementById("OverDiv" + type).style.display = "block";
	document.getElementById("OverDiv" + type).style.height = document.getElementById("mainDiv").clientHeight+"px";
}


ShowPopupTip = function(image, description, align)
{
	var width = 350;
	var dx = 300;
	var dy = 200;
	var p = GetAbsolutePos(image);
	var bigDiv = document.getElementById("PopupDiv");
	var toolFrame = document.getElementById("PopupFrame");
//	var toolTipContent = document.getElementById("ModalTipContent");
	var toolTip = document.getElementById("PopupTipContent");
	
	switch (align)
	{
		case 'Left' : var shift = - width;break;
		case 'Center' : var shift = - width/2;break;
		case 'Right' : var shift = 0;break;
		case 'DependOnImagePosition':
			var shift;
			if (p.x < 260) shift = 0;
			else shift = - width/2;
			break;
		default : var shift = - width;break;
	}
	bigDiv.style.display = "none";
	toolTip.innerHTML = description;
	
	bigDiv.style.top = p.y - dy + "px";
	bigDiv.style.left = p.x + shift - dx + "px";

	bigDiv.style.display = "inline";
	toolFrame.style.height = toolTip.clientHeight;
}

function ClosePopupDiv()
{
	document.getElementById("PopupDiv").style.display = 'none';
}

ShowModalTip = function(type)
{
	var type = type || "";
	HideAllBody(0.3, type);
	var w = 350;
	var h = 200;
	var x = Math.floor((screen.width-w-10)/2);
	var y = Math.floor((screen.height-h-150)/2);
	
	var bigDiv = document.getElementById("ModalDiv" + type);
	var toolFrame = document.getElementById("ModalFrame" + type);
//	var toolTipContent = document.getElementById("ModalTipContent");
	var toolTip = document.getElementById("ModalTip" + type);

	bigDiv.style.display = "none";
//	toolTipContent.innerHTML = description;
	
	bigDiv.style.top = y+"px";
	bigDiv.style.left = x+"px";

	bigDiv.style.display = "inline";
	toolFrame.style.height = toolTip.clientHeight+"px";
	toolFrame.style.width = toolTip.clientWidth+2+"px";
	document.body.onclick = function(){ if(!document.getElementById("CancelMainNews")) return false};
}

CloseModalTip = function (toShowToolTip, type)
{
	var type = type || "";
	var bigDiv = document.getElementById("ModalDiv" + type);
	bigDiv.style.display = "none";
	document.body.onclick = function(){};
	document.getElementById("OverDiv" + type).style.display = "none";
}

StartSearch = function(form)
{
	str = form.elements.SearchString["value"];
	if (!CheckSearchString(str))
	{
		alert("Строка поиска должна содержать хотя бы одно слово, состоящее больше чем из 3 символов");
		return false;
	}
	ShowModalTip();
	var req = new JsHttpRequest();
	req.onreadystatechange = function()
	{
		if (this.readyState == 4)
		{
			document.location.href = this.responseJS.RedirectUrl;
		}
	}
	req.caching = false;
	req.open('POST', projectPath+'do_prepare_search_result.php', true);
	req.send({Form: form});
}

function ShowMainNews()
{
	var newsid = document.getElementById("MainNewsID");
	if (newsid != null) ShowModalTip("News");
}

function ShowTableOrderConfirm()
{
	ShowModalTip("Order");
}

function CloseNews()
{
	var newsid = document.getElementById("CancelMainNews").checked ? document.getElementById('MainNewsID').value : 0;
	window.location.href="home.php?CancelMainNews=" + newsid;
}

// возвращает cookie если есть или undefined
function getCookie(name) {
	var matches = document.cookie.match(new RegExp(
	  "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
	))
	return matches ? decodeURIComponent(matches[1]) : undefined 
}

// уcтанавливает cookie
function setCookie(name, value, props) {
	props = props || {}
	var exp = props.expires
	if (typeof exp == "number" && exp) {
		var d = new Date()
		d.setTime(d.getTime() + exp*1000)
		exp = props.expires = d
	}
	if(exp && exp.toUTCString) { props.expires = exp.toUTCString() }

	value = encodeURIComponent(value)
	var updatedCookie = name + "=" + value
	for(var propName in props){
		updatedCookie += "; " + propName
		var propValue = props[propName]
		if(propValue !== true){ updatedCookie += "=" + propValue }
	}
	document.cookie = updatedCookie

}

// удаляет cookie
function deleteCookie(name) {
	setCookie(name, null, { expires: -1 })
}


function ShowVote()
{
	if (getCookie("CancelVote")) return false;
	document.getElementById("popupvote").style.display = "block";
}

function CloseVote()
{
	var popupvote = document.getElementById("popupvote").style.display = "none";
	var currDate = new Date();
	currDate.setMinutes(currDate.getMinutes() + 20);
	setCookie("CancelVote", "1", { expires: 600});
}

CreateOrder = function()
{
	ShowModalTip();
	var req = new JsHttpRequest();
	req.onreadystatechange = function()
	{
		if (this.readyState == 4)
		{
//			alert(this.responseText);
			document.location.href = this.responseJS.RedirectUrl;
		}
	}
	req.caching = false;
	req.open('POST', projectPath+'do_crete_order.php', true);
	var form = document.forms["order_form"];
	req.send({Form: form});
}

CloseModalDiv = function()
{
	CloseModalTip();
	//call function to cancel started process
}

CheckSearchString = function(str)
{
	var ar = str.split(" ");
	for (var i = 0; i < ar.length; i++)
		if (ar[i].length >= 3)
			return true;
	return false;
}

