﻿var SiteName = "www.tavrida.ru";
var ArticleIllustrationsWin = null;

//var IsIE = (window.ActiveXObject) ? true : false;

function Trim (txt)
{
	var el = ("object" == typeof(txt)) ? txt : null ;
	var res = (null == el) ? txt : el.value ;
	var re = /^\s+/ ;
	res = res.replace(re, "") ;
	re = /\s+$/ ;
	res = res.replace(re, "") ;
	if ( el != null ) el.value = res ;
	return res ;	
}
function SpacesToOne (txt)
{
	var el = ("object" == typeof(txt)) ? txt : null ;
	var res = (null == el) ? txt : el.value ;
	var re = /\s{2,}/g ;
	res = res.replace(re, " ") ;
	if ( el != null ) el.value = res ;
	return res ;
}
function DropSpaces (txt)
{
	var el = ("object" == typeof(txt)) ? txt : null ;
	var res = (null == el) ? txt : el.value ;
	var re = /\s+/g ;
	res = res.replace(re, "") ;
	if ( el != null ) el.value = res ;
	return res ;
}
function NormSpaces (txt)
{
	var el = ("object" == typeof(txt)) ? txt : null ;
	var res = (null == el) ? txt : el.value ;
	res = Trim(res) ;
	res = SpacesToOne(res) ;
	if ( el != null ) el.value = res ;
	return res ;
}
function EmailCheck (txt)
{
	var el = ("object" == typeof(txt)) ? txt : null ;
	var res = (null == el) ? txt : el.value ;
	res = Trim(res).toLowerCase() ;
	if ( el != null ) el.value = res ;	
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/ ;
	if ( ! res.match(re) ) res = "" ;
	return res ;
}
function ToXml (input)
{
	var res, re ;
	re = /&/g ;	
    res = input.replace(re, "&amp;") ;    
    re = /</g ;
    res = res.replace(re, "&lt;") ;    
    re = />/g ;
    res = res.replace(re, "&gt;") ;    
    re = /\"/g ;
    res = res.replace(re, "&quot;") ;    
    re = /\'/g ;
    res = res.replace(re, "&apos;") ;    
    return res ;	
}
function FromXml (input)
{
	var res, re ;
	re = /&amp;/g ;
	res = input.replace(re, "&") ;
	re = /&lt;/g ;
	res = res.replace(re, "<") ;
	re = /&gt;/g ;
	res = res.replace(re, ">") ;
	re = /&quot;/g ;
	res = res.replace(re, "\"") ;
	re = /&apos;/g;
	res = res.replace(re, "'") ;
	return res ;
}
function GetInt (input)
{	
	var intVal = parseInt(input, 10);
	if ( Number.NaN == intVal || ! IntCheck(input) )
		intVal = null;
	return intVal;
}
function GetFloat (input)
{		
	var floatVal = parseFloat(input.replace(",", ".")) ;
	if ( Number.NaN == floatVal || ! DecimalCheck(input) )
		floatVal = null;
	return floatVal;
}
function IntCheck (input_string, maxlength)
{
	var res = true ;
	var input = Trim(input_string) ;
	var re = /^[-\+]?\d+$/ ;
	if ( ! input.match(re) )
		res = false ;
	else if ( maxlength && maxlength < input.length )		
		res = false ;
	return res ;
}
function DecimalCheck (input_string)
{
	var res = true ;
	var input = Trim(input_string) ;
	var re = /^[-\+]?\d+([\.\,]{1}\d+)?$/ ;
	if ( ! input.match(re) ) res = false ;
	return res ;
}
function EnterToClick (evt, elOrId)
{
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if ( 13 == charCode )
	{
		var el = ("string" == typeof(elOrId)) ? document.getElementById(elOrId) : elOrId;
		el.click();
		return false;	
	}
	return true;
}
function WriteTextNode (elOrId, text)
{
	var el = ("string" == typeof(elOrId)) ? document.getElementById(elOrId) : elOrId;
	while ( el.hasChildNodes() )
		el.removeChild(el.firstChild);
	el.appendChild(document.createTextNode(text));
}
function CoordinateSelectWithInput (evt, elOrId)
{
	evt = (evt) ? evt : event;
	var target = (evt.target) ? evt.target : evt.srcElement;	
	if ( "SELECT" == target.tagName )
	{
		if ( target.selectedIndex != 0 )
		{
			var input = ("object" == typeof(elOrId)) ? elOrId : document.getElementById(elOrId);
			input.value = "";
		}
	}
	else
	{
		if ( Trim(target.value).length != 0 )
		{
			var select = ("object" == typeof(elOrId)) ? elOrId : document.getElementById(elOrId);
			select.selectedIndex = 0;
		}
	}
}

function InitializeRequestHandlerForCancelConcurrent (sender, args)
{	
	if ( Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack() )
	{
		args.set_cancel(true);
		alert("Выполняется прошлая операция.");
	}
}

function EndRequestHandlerForErrorAlert (sender, args)
{
	if ( args.get_error() != null )
	{	
		var msg = args.get_error().message;
		var colonIndex = msg.indexOf(":");
		if ( 0 <= colonIndex ) msg = msg.substring(colonIndex + 2, msg.length);
		
		if ( args.get_response().get_statusCode() != "200" )		
		{
			if ( "500" == args.get_response().get_statusCode() )
				msg = "Приносим наши извинения, на сервере произошла ошибка.";
			else
				msg = "Произошла непредвиденная ошибка.\n\n" + msg + "\n\nПопробуйте повторить попытку.";
		}
			
		args.set_errorHandled(true);
		alert(msg);
	}
}

function WriteYoutubeVideo (playerSrc, playerWidth)
{
	width = (null == playerWidth) ? 420 : parseInt(playerWidth, 10);	
	if ( typeof(playerSrc) !== "string" || isNaN(width) )
		return;	
	
	var height = Math.ceil(width * 0.8235);
	var src = Trim(playerSrc);	
	if ( src.charAt(src.length - 1) !== "&" )
		src += "&";
	if ( src.indexOf("hl=ru_RU") < 0 )
		src += "hl=ru_RU&";
	if ( src.indexOf("fs=1") < 0 )
		src += "fs=1&";		
	src += "rel=0&border=1&color1=0x007360&color2=0x5e9c92";
	
	document.write("<object width=\"" + width.toString() + "\" height=\"" + height.toString() + "\">");
	document.write("<param name=\"movie\" value=\"" + src + "\"></param>");
	document.write("<param name=\"allowFullScreen\" value=\"true\"></param>");
	document.write("<embed src=\"" + src + "\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"" + width.toString() + "\" height=\"" + height.toString() + "\"></embed>");	
	document.write("</object>");
}

// --------------------------------------

var ChildWinsArray = new Object();
ChildWinsArray.Array = new Array();
ChildWinsArray.Add = function (win)
{
	this.Array[this.Array.length] = win;	
}
ChildWinsArray.Close = function()
{
	if ( 0 < this.Array.length )
		for ( var i = 0; i < this.Array.length; i++ )
			if ( ! this.Array[i].closed )
				this.Array[i].close();
}

function OpenWin (url, width, height, MustStayAfterParentOnunload)
{
	var MustStay = (MustStayAfterParentOnunload) ? MustStayAfterParentOnunload : false;
	var win = window.open(url, "", "width=" + width + ",height=" + height+ ",resizable=yes");
	if ( ! MustStay )
		ChildWinsArray.Add(win);
	return win;
}
function OpenScrollWin (url, width, height, MustStayAfterParentOnunload)
{
	var MustStay = (MustStayAfterParentOnunload) ? MustStayAfterParentOnunload : false;
	var win = window.open(url, "", "width=" + width + ",height=" + height+ ",resizable=yes,scrollbars=yes");
	if ( ! MustStay )
		ChildWinsArray.Add(win);
	return win;
}

function OpenArticleIllustrationsWin (url)
{
	if ( ! ArticleIllustrationsWin || ArticleIllustrationsWin.closed )
	{
		ArticleIllustrationsWin = OpenWin(url, 600, 600);
		ArticleIllustrationsWin.focus();
	}
	else
	{
		ArticleIllustrationsWin.location.href = url;
		ArticleIllustrationsWin.focus();
	}
}

// --------------------------------------

window.onload = function ()
{
	window.defaultStatus = SiteName;	
	if ( typeof(telPageOnload) != "undefined" )
		telPageOnload();
}
window.onunload = function ()
{
	ChildWinsArray.Close();	
	if ( typeof(telPageOnunload) != "undefined" )
		telPageOnunload();	
}