function extend_string()
{
	if ('undefined' == typeof String.prototype.ltrim)
	{
	  String.prototype.ltrim = function() {
	    return this.replace(/^\s+/, '');
	  }
	}
	
	if ('undefined' == typeof String.prototype.rtrim)
	{
	  String.prototype.rtrim = function() {
	    return this.replace(/\s+$/, '');
	  }
	}
	
	if ('undefined' == typeof String.prototype.trim)
	{
	  String.prototype.trim = function() {
	    return this.replace(/^\s+/, '').replace(/\s+$/, '');
	  }
	}		
}

function strHasData(s, minLength)
{
	if(s == null)
		return false;
	
	extend_string();
	s = s.trim();
	return s.length >= minLength;
}

function checkEmail(s)
{
	if( ! strHasData(s, 5)  )
		return false;
	var i = s.indexOf('@');
	var i2 = s.indexOf('.');
	return (i >= 0) && (i2 >= 0);		
}

// !!!  в name нельзя пробелы и лучше только латиница, т.к. это имя, а не заголовок
function popupWin(name, uri, wdt, hgt)
{
	var posCode = ''
	if ( (screen.height < 481) && (hgt > 400) )
		{ hgt = 400 }
	var posX = Math.round((screen.width - wdt) / 2)
	var posY = Math.round((screen.height - hgt) / 2) - 35
	posCode = ",left="+posX+",top="+posY
//	var popupedWin = window.open(uri, name, "status=yes,menubar=yes,toolbar=yes,resizable=yes,scrollbars=yes,location=yes,width="+wdt+",height="+hgt+posCode)
	var popupedWin = window.open(uri, name, "status=yes,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,location=no,width="+wdt+",height="+hgt+posCode)
	popupedWin.focus();
	return popupedWin;
}

function showWin(name, uri, left, top, wdt, hgt)
{
	var posCode = ",left="+left+",top="+top;
	var popupedWin = window.open(uri, name, "status=yes,menubar=yes,toolbar=yes,resizable=yes,scrollbars=yes,location=no,width="+wdt+",height="+hgt+posCode)
	popupedWin.focus();
	return popupedWin;
}

// if state exist then define that need to do (show or hide) 
// state = 1 -> show, state = 0 -> hide, state = undefined -> switch visibility
function showHide(elId, state)
{
	var el = document.getElementById(elId);
	if (el == null)
	{
		//alert("Элемент не найден - " + elId);
		return;
	}
	
	if( state != null)
	{
		if(state)
			el.style.display = "block";
		else
			el.style.display = "none";
	}
	else
	{
		if(el.style.display == "none")
			el.style.display = "block";
		else
			el.style.display = "none";
	}
}

var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera')  > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);
	 
function getDocumentHeight() {
  return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}
 
function getViewportHeight() {
  return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}


/// Открывает "модально" div
function openModalWindow(elHolderId, elWndId)
{
	var el;
	var w, h, x, y, h1;
	
	el = document.getElementById(elHolderId);
	if(el == null)
		return;

	el.style.display = 'block';

	h1 = getDocumentHeight();
	if(window.innerWidth)
	{
		x = window.pageXOffset;
		y = window.pageYOffset;
		w = window.innerWidth;
		h = window.innerHeight;
	}
	else
	{
		with(document.body)
		{
			x = scrollLeft;
			y = scrollTop;
			w = clientWidth;
			h = clientHeight;
		}
	}
	el.style.left = 0;
	el.style.top = 0;
	el.style.width = w + x + 'px'; 
	el.style.height = h1 + y + 'px';
	WCH.Apply(elHolderId)
	
	el = document.getElementById(elWndId);
	if(el == null)
		return;

	el.style.display = 'block';
	el.style.left = x + ( parseInt(w*0.5) ) - ( parseInt(el.clientWidth*0.5) ) + 'px';
	el.style.top = y + ( parseInt(h*0.5) ) - ( parseInt(el.clientHeight*0.5) ) + 'px';
}

function closeModalWindow(elHolderId, elWndId)
{
	el = document.getElementById(elHolderId);
	if(el == null)
		return;
	el.style.display = 'none';
	WCH.Discard(elHolderId)

	el = document.getElementById(elWndId);
	if(el == null)
		return;
	el.style.display = 'none';
}

function openModalWindowEx(elWndId)
{
	var elId = "idModalWindowEx";
	var el = getTmpDiv(elId);
	el.className = "modal_window_holder";
	openModalWindow(elId, elWndId);
}

function closeModalWindowEx(elWndId)
{
	var elId = "idModalWindowEx";
	closeModalWindow(elId, elWndId);
}

/// Ищет ближайшую родительскую форму
function findParentForm(el)
{
	if(el == null)
		return null;
	var pnode = el.parentNode;
	while (pnode != null)
	{
		if(pnode.nodeName.toUpperCase() == 'FORM')
			return pnode;
		pnode = pnode.parentNode;
	}
	return null;
}

function submitParentForm(el)
{
	var fm = findParentForm(el);
	if ( fm == null )
	{
		alert("Ошибка. Форма не найдена.");
		return;
	}
	fm.submit();
}


/// Сабмитит форму в которой находится activeElement, после выдачи предупреждения, при условии что пользователь дал согласие
/// Внимание! используется стиль: modal_window_holder
function submitAfterConfirm(activeElement, msg)
{
	var fm = findParentForm(activeElement);
	if ( fm == null )
	{
		alert("Ошибка. Форма не найдена.");
		return;
	}
	
	var elId = "submitAfterConfirmModal";
	var el = getTmpDiv(elId);
	el.className = "modal_window_holder";
	
	openModalWindow(elId);
	if (confirm(msg))
	{
		fm.submit();
		return;
	}
	closeModalWindow(elId);
}

function getTmpDiv(divId)
{
	var el = document.getElementById(divId);
	if(el != null)
		return el;
	
	el = document.createElement("DIV");
	document.body.appendChild(el);
	el.id = divId;
	return el;
}

function setClass(elId, className)
{
	var el = document.getElementById(elId);
	if(el == null)
	{
		alert('Element not found by ID: '.elId);
		return;
	}
	el.className = className;
}

///переключить класс
function switchClass(elId, className1, className2)
{
	var el = document.getElementById(elId);
	if(el == null)
	{
		alert('Element not found by ID: '.elId);
		return;
	}
	if( el.className == className1)
		el.className = className2;
	else
		el.className = className1;
}
