function strReplace(s, r, w)
{
	return s.split(r).join(w);
}

function addslashes(str) 
{
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}
function stripslashes(str) 
{
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}

function trim(str)
{ 
	str = str.replace(/&nbsp;/g, '');
	return str.replace(/(^\s+)|(\s+$)/g, '');
}



       
function countdown(year, month, day, hour, minute, format)
{
	Today = new Date();
	Todays_Year = Today.getFullYear();
	Todays_Month = Today.getMonth();
	
	Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(), Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();                                 
	Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();

	Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
	 
	if(Time_Left < 0)
	{
		Time_Left = 0;
	}
	
	switch(format)
	{
		case 0:
			//SS
			getObj("countdown").innerHTML = Time_Left;
			break;
		case 1:
			//DD:HH:MM:SS
			days = Math.floor(Time_Left / (60 * 60 * 24));
			Time_Left %= (60 * 60 * 24);
			hours = Math.floor(Time_Left / (60 * 60));
			Time_Left %= (60 * 60);
			minutes = Math.floor(Time_Left / 60);
			Time_Left %= 60;
			seconds = Time_Left;
			
			getObj("countdown").innerHTML = days + " : " + hours + " : " + minutes + " : " + seconds;
			break;
		case 2:
			//HHHH:MM:SS
			hours = Math.floor(Time_Left / (60 * 60));
			Time_Left %= (60 * 60);
			minutes = Math.floor(Time_Left / 60);
			Time_Left %= 60;
			seconds = Time_Left;
			
			var str = ((hours < 10) ? "0" + hours : hours) + ":" + ((minutes < 10) ? "0" + minutes : minutes) + ":" + ((seconds < 10) ? "0" + seconds : seconds);
			getObj("countdown").innerHTML = str;
			break;
		default: 
			getObj("countdown").innerHTML = Time_Left;
	}
		   
	setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
}



function getObj(nid)
{
	if (document.getElementById(nid))
	{
		return document.getElementById(nid);
	}
	else
	{
		return false;
	}
}

function getObjValue(nid)
{
	var ret = "";
	if (document.getElementById(nid).type == "checkbox")
	{
		if (document.getElementById(nid).checked == true)
		{
			ret = document.getElementById(nid).value;
		}
	}
	else
	{
		ret = document.getElementById(nid).value;
	}
	return ret;
}

function getValFromUrl(url, varname)
{
	var vars = new Array();
	vars = url.split("&");
	var ret = "error";

	for (i = 0; i < vars.length; i++)
	{
		var value = new Array();
		value = vars[i].split("=");
		if (value[0] == varname)
		{
			ret = value[1];
		}
	}
	return ret;	
}


function makeRequest(url) 
{
	var http_request = false;

	if(document.all)
	{
		var yOffset = document.documentElement.scrollTop;
	}
	else
	{
		var yOffset = window.pageYOffset;
	}

	if (window.XMLHttpRequest) 
	{ // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType('text/html');
			// Читайте ниже об этой строке
		}
	} 
	else if (window.ActiveXObject) 
	{ // IE
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
			}
		}
	}

	if (!http_request) 
	{
		alert('Unfortunatly your browser doesn\'t support XMLHTTP.');
		return false;
	}

	http_request.onreadystatechange = function() 
	{ 
		if (http_request.readyState == 4)
		{
         if (http_request.status == 200)
			{	
				if (getValFromUrl(http_request.responseText, "answer") == "search")
				{
					if (getValFromUrl(http_request.responseText, "error") != "")		//Возникли ошибки
					{
						var newHTML = getValFromUrl(http_request.responseText, "error");
						newHTML = strReplace(newHTML, "::ravno::", "=");
						newHTML = strReplace(newHTML, "::amp::", "&");
						alert(newHTML);
					}
					else
					{
						document.location = "/search/";
					}
				}
			}
			else
			{
				alert('Connection wasn\'t successful.');
			}
		}
	};


	//************** ОБРАБАТЫВАЕМ КЛИКИ
	var args = "";
	for (var i = 0; i < arguments.length; i++)
	{
		args += "&p" + i + "=" + arguments[i];
	}
	
	http_request.open('POST', "/getAnswer.php", true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Connection", "close");	
	http_request.send("q=" + url + args);
}


//*****************************************
function numericCheck(e, obj, max)
{
	var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);
	if (e.ctrlKey || e.altKey || key < 32 || key == 60 || key == 62)
	{
    	return true;
	}
	
	key = String.fromCharCode(key);
	
	if (obj.value.length < max)
	{ 
		//return /[\d+\.+\,+\-]/.test(key);
		return /[\d]/.test(key);
	}
	else
	{
		return false;
	}	
}




function toggleFizTable()
{
	var rows = document.getElementsByTagName("tr");
	for (i = 0; i < rows.length; i++)
	{
		if (rows[i].getAttribute("rel") == "fiz_table")
		{
			if (document.all)
			{
				rows[i].style.display = (rows[i].style.display != "none") ? "none" : "block";
			}
			else
			{
				rows[i].style.display = (rows[i].style.display == "table-row") ? "none" : "table-row";
			}
		}
	}
}


function openFaqForm()
{
		Lightview.show({
			href: '/form1.php',
			rel: 'iframe',
			title : 'Ваш вопрос',
			options: {
				width:535,
				height: 324,
				autosize: false,
				topclose: false,
				closeButton: false,
				radius: 1,
				border: 1
				
			}
		});

}


var currentPic = 0;
function showNext()
{
	currentPic++;
	
	var pics = document.getElementsByTagName("input");
	var n = 0;
	for (i = 0; i < pics.length; i++)
	{
		if (pics[i].getAttribute("rel") == "pictures")
		{
			n++;
		}
	}
	
	if (currentPic >= n)
	{
		currentPic = 0;
	}
	
	getObj("picture").src = getObjValue("pic" + currentPic);
}

function showPrev()
{
	currentPic--;
	
	var pics = document.getElementsByTagName("input");
	var n = 0;
	for (i = 0; i < pics.length; i++)
	{
		if (pics[i].getAttribute("rel") == "pictures")
		{
			n++;
		}
	}
	
	if (currentPic < 0)
	{
		currentPic = n - 1;
	}
	
	getObj("picture").src = getObjValue("pic" + currentPic);
}



function insertGallery(id, width, height, title)
{
	document.write("<div id=\"" + id + "\"></div>");
	
	
	var n = 1;
	var images = Array();
	var links = Array();
	for (var i = 4; i < arguments.length; i++)
	{
		if (n > 0)
		{
			images.push(arguments[i]);
		}
		else
		{
			links.push(arguments[i]);
		}
		n *= -1;
	}
	
	var galleryTitle = document.createElement("div");
	var galleryBody = document.createElement("div");
	var galleryPages = document.createElement("div");
	var galleryNext = document.createElement("div");
	var galleryMoreinfo = document.createElement("div");
	var galleryLeft = document.createElement("div");
	var galleryRight = document.createElement("div");
	
	galleryTitle.className = "deco_block_2";
	galleryTitle.innerHTML = title;
	
	galleryBody.style.width = width + "px";
	galleryBody.style.height = height + "px";
	galleryBody.style.background = "#cccccc";
	//galleryBody.style.backgroundImage = "url(/style/ajax-loader.gif)";
	galleryBody.style.backgroundRepeat = "no-repeat";
	//galleryBody.style.backgroundPosition = "center center";
	galleryBody.setAttribute("id", "image_" + id);
	galleryBody.setAttribute("pagesTotal", (arguments.length - 4) / 2);
	galleryBody.setAttribute("currentPage", 0);
	
	galleryPages.className = "deco_block_3";
	galleryPages.style.borderTop = "1px solid #999999";
	
	galleryNext.style.backgroundImage = "url()";
	galleryNext.style.height = height + "px";
	galleryNext.style.cursor = "hand";
	galleryNext.style.cursor = "pointer";
	galleryNext.style.width = "53px";
	galleryNext.style.marginLeft = width - 53 + "px";
	galleryNext.style.backgroundRepeat = "no-repeat";
	galleryNext.style.display = "block";	
	
	
	galleryLeft.style.backgroundImage = "url(/style/gallery_left.png)";
	galleryLeft.style.height =  56 + "px";
	galleryLeft.style.cursor = "hand";
	galleryLeft.style.cursor = "pointer";
	galleryLeft.style.width = "82px";
	galleryLeft.style.marginLeft = width - 180 + "px";
	galleryLeft.style.marginTop = -14 + "px";
	galleryLeft.style.backgroundPosition = "0px 0px";
	galleryLeft.style.backgroundRepeat = "no-repeat";
	galleryLeft.style.display = "block";
	galleryLeft.style.position = "absolute";
	
	
	galleryRight.style.backgroundImage = "url(/style/gallery_right.png)";
	galleryRight.style.height =  56 + "px";
	galleryRight.style.cursor = "hand";
	galleryRight.style.cursor = "pointer";
	galleryRight.style.width = "82px";
	galleryRight.style.marginLeft = width - 98 + "px";
	galleryRight.style.marginTop = -14 + "px";
	galleryRight.style.backgroundPosition = "0px 0px";
	galleryRight.style.backgroundRepeat = "no-repeat";
	galleryRight.style.display = "block";
	galleryRight.style.position = "absolute";
	
	
	galleryMoreinfo.style.backgroundImage = "";
	galleryMoreinfo.style.backgroundRepeat = "no-repeat";
	galleryMoreinfo.style.backgroundPosition = "center center";
	galleryMoreinfo.style.height = height + "px";
	galleryMoreinfo.style.cursor = "hand";
	galleryMoreinfo.style.clear = "none";
	galleryMoreinfo.style.cursor = "pointer";
	galleryMoreinfo.style.width = width - 80 + "px";
	galleryMoreinfo.style.position = "absolute";
	
	galleryPages.appendChild(galleryLeft);
	galleryPages.appendChild(galleryRight);
	
	galleryBody.appendChild(galleryMoreinfo);
	galleryBody.appendChild(galleryNext);
	
	
	
	
	galleryNext.onmouseover = function()
	{
		this.style.backgroundImage = "url(/style/gallery_hover.png)";		
	}
	galleryNext.onmouseout = function()
	{
		this.style.backgroundImage = "url()";
	}
	galleryNext.onclick = function()
	{
		var currentPage = getObj("image_" + id).getAttribute("currentPage");
		if (currentPage * 1 + 1 >= getObj("image_" + id).getAttribute("pagesTotal"))
		{
			nextPage = 0;
		}
		else
		{
			nextPage = currentPage * 1 + 1;
		}
		
		galleryBody.setAttribute("currentPage", nextPage);
		
		var pages = document.getElementsByTagName("a");
		var n = 0;
		for (i = 0; i < pages.length; i++)
		{
			if (pages[i].getAttribute("rel") == "page" + id)
			{
				if (n == nextPage)
				{
					pages[i].className = "galleryPage_active";
				}
				else
				{
					pages[i].className = "galleryPage";
				}
				n++;
			}
		}
			
		getObj("image_" + id).style.backgroundImage = "url(" + images[nextPage] + ")";
	}
	
	
	
	
	galleryRight.onclick = function()
	{
		var currentPage = getObj("image_" + id).getAttribute("currentPage");
		if (currentPage * 1 + 1 >= getObj("image_" + id).getAttribute("pagesTotal"))
		{
			nextPage = 0;
		}
		else
		{
			nextPage = currentPage * 1 + 1;
		}
		
		galleryBody.setAttribute("currentPage", nextPage);
		
		var pages = document.getElementsByTagName("a");
		var n = 0;
		for (i = 0; i < pages.length; i++)
		{
			if (pages[i].getAttribute("rel") == "page" + id)
			{
				if (n == nextPage)
				{
					pages[i].className = "galleryPage_active";
				}
				else
				{
					pages[i].className = "galleryPage";
				}
				n++;
			}
		}
			
		getObj("image_" + id).style.backgroundImage = "url(" + images[nextPage] + ")";
	}
	
	
	galleryLeft.onclick = function()
	{
		var currentPage = getObj("image_" + id).getAttribute("currentPage");
		if (currentPage * 1 - 1 < 0)
		{
			nextPage = getObj("image_" + id).getAttribute("pagesTotal") - 1;
		}
		else
		{
			nextPage = currentPage * 1 - 1;
		}
		
		galleryBody.setAttribute("currentPage", nextPage);
		
		var pages = document.getElementsByTagName("a");
		var n = 0;
		for (i = 0; i < pages.length; i++)
		{
			if (pages[i].getAttribute("rel") == "page" + id)
			{
				if (n == nextPage)
				{
					pages[i].className = "galleryPage_active";
				}
				else
				{
					pages[i].className = "galleryPage";
				}
				n++;
			}
		}
			
		getObj("image_" + id).style.backgroundImage = "url(" + images[nextPage] + ")";
	}
	
	
	
	galleryMoreinfo.onmouseover = function()
	{
		var currentPage = getObj("image_" + id).getAttribute("currentPage");
		if (links[currentPage] != "")
		{
			this.style.backgroundImage = "url(/style/gallery_moreinfo.png)";
		}
	}
	galleryMoreinfo.onmouseout = function()
	{
		this.style.backgroundImage = "url()";
	}
	galleryMoreinfo.onclick = function()
	{
		var currentPage = getObj("image_" + id).getAttribute("currentPage");
		if (links[currentPage] != "")
		{
			document.location = links[currentPage];
		}
	}
	
	
	
	for (var i = 0; i < (arguments.length - 4) / 2; i++)
	{
		var page = document.createElement("a");
		page.className = "galleryPage";
		page.setAttribute("index", i);
		page.setAttribute("rel", "page" + id);
		page.innerHTML = i + 1;
		page.setAttribute("href", "");
		page.onclick = function()
		{
			//getObj("image_" + id).style.backgroundImage = "url(/style/ajax-loader.gif)";
			var pages = document.getElementsByTagName("a");
			for (i = 0; i < pages.length; i++)
			{
				if (pages[i].getAttribute("rel") == "page" + id)
				{
					pages[i].className = "galleryPage";
				}
			}
			getObj("image_" + id).style.backgroundImage = "url(" + images[this.getAttribute("index")] + ")";
			getObj("image_" + id).setAttribute("currentPage", this.getAttribute("index"));
			this.className = "galleryPage_active";
			
			if (links[this.getAttribute("index")] != "")
			{
				
			}
			
			return false;
		}
		
		if (i == 0)
		{
			page.className = "galleryPage_active";			
		}
		
		galleryPages.appendChild(page);
	}
	
	
	
	getObj(id).appendChild(galleryTitle);
	getObj(id).appendChild(galleryBody);
	getObj(id).appendChild(galleryPages);
	
	getObj("image_" + id).style.backgroundImage = "url(" + images[0] + ")";
}
