//
// Copyright (c) 2007 Denis Zubo | http://www.zubo.ru/
//

/*****************************
**   Common Functions
******************************/

function gebid(id) {
  return document.getElementById(id);
}
///

// возвращает сам элемент или его ближайшего родителя, с именем класса classStr
function getSelfOrParentByClass(oElem, classStr){
	var limit = 10;
	while(limit > 0 && oElem){
		if(matchClass(oElem, classStr)) return oElem;
		limit -= 1;
		oElem = oElem.parentNode;
	}
}
///

// возвращает сам элемент или его ближайшего родителя, с именем тега tagStr
function getSelfOrParentByTag(oElem, tagStr){
	var limit = 10;
	while(limit > 0 && oElem){
		if(oElem.tagName == tagStr) return oElem;
		oElem = oElem.parentNode;
	}
}

/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
    Add-ons by Denis Zubo, http://www.zubo.ru  (now you can use strClassName='*')
*/
// Функция всегда возвращает массив. Если ничего не найдено, то пустой.
// Выбирает всех детей с заданным именем тега и классом.
// strTagName == "*" - выбираем элементы без ограничения на имя тега
// strClassName == "*" - выбираем элементы без ограничения на класс
// strClassName == "" - выбираем элементы с пустым или не заданным классом
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = (strClassName == "*") ? '' : new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
	  oElement = arrElements[i];
	  if((oRegExp.length = 0) || oRegExp.test(oElement.className)){
	  	arrReturnElements.push(oElement);
	  }
	}
	return (arrReturnElements)
}
///


// Аналог getElementsByClassName(oElm, strTagName, strClassName), но возвращает не массив, а первый элемент, удовл. условиям
function getElementByClassName(oElm, strTagName, strClassName){
	var oElem, oElems;
	oElems = getElementsByClassName(oElm, strTagName, strClassName);
	if(oElems.length) oElem = oElems[0];
	return oElem;
}
///

/*****************************
**   Common visual effects methods
******************************/

// скрывает первый элемент, показывает второй
function ToggleDisplay(id1, id2) {
	var oElem1 = gebid(id1);
	var oElem2 = gebid(id2);
	if(oElem1 && oElem2){
		oElem1.style.display = 'none';
		oElem2.style.display = 'block';
	}
}
///

function addHtmlToElem(oElem, some_html){
	if(oElem) { oElem.innerHTML += some_html ? some_html : ""; }
}
///

function setHtmlToElem(oElem, some_html){
	if(oElem) { oElem.innerHTML = some_html ? some_html : ""; }
}
///

/*****************************
**   Common class methods
**
**   Copyright (c) Art. Lebedev Studio | http://www.artlebedev.ru/
**   Author - Leechy | leechy@design.ru
**
******************************/

function switchClass( objNode, strCurrClass, strNewClass ) {
	if ( matchClass( objNode, strNewClass ) ) replaceClass( objNode, strCurrClass, strNewClass );
		else replaceClass( objNode, strNewClass, strCurrClass );
}

function removeClass( objNode, strCurrClass ) {
	replaceClass( objNode, '', strCurrClass );
}

function addClass( objNode, strNewClass ) {
	replaceClass( objNode, strNewClass, '' );
}

function replaceClass( objNode, strNewClass, strCurrClass ) {
	var strOldClass = strNewClass;
	if ( strCurrClass && strCurrClass.length ){
		strCurrClass = strCurrClass.replace( /\s+(\S)/g, '|$1' );
		if ( strOldClass.length ) strOldClass += '|';
		strOldClass += strCurrClass;
	}
	objNode.className = objNode.className.replace( new RegExp('(^|\\s+)(' + strOldClass + ')($|\\s+)', 'g'), '$1' );
	objNode.className += ( (objNode.className.length)? ' ' : '' ) + strNewClass;
}

function matchClass( objNode, strCurrClass ) {
	return ( objNode && objNode.className.length && objNode.className.match( new RegExp('(^|\\s+)(' + strCurrClass + ')($|\\s+)') ) );
}

/*****************************
**   PopupWindow
******************************/

function getDims(winWidth, winHeight){
	var dims = new Object();
	dims.widthStr = '';
	dims.heightStr = '';
	dims.scrollbars = false;
	if (winWidth){
		dims.width = winWidth;
		if (screen.width < dims.width + 50){
			dims.width = screen.width - 50;
			dims.scrollbars = true;
		}
		dims.widthStr = ',width=' + dims.width;
	}

	if (winHeight){
		dims.height = winHeight;
		if (screen.height < dims.height + 100){
			dims.height = screen.height - 100;
			dims.scrollbars = true;
		}
		dims.heightStr = ',height=' + dims.height;
	}
	dims.scrollbarsStr = (dims.scrollbars)? ',scrollbars=yes' : ',scrollbars=no';
	dims.posX = Math.round((screen.width - dims.width) / 2);
	dims.posY = Math.round((screen.height - winHeight) / 2);
	dims.posCode = (document.all)? ',left=' + dims.posX + ',top=' + dims.posY : ',screenX=' + dims.posX + ',screenY=' + dims.posY;
	return dims;
}

function popupImg(imgSrc, winName, imgWidth, imgHeight, winTitle){
	winWidth = (imgWidth)? imgWidth + 20 : null;
	winHeight = (imgHeight)? imgHeight + 20 : null;
	var dims = getDims(winWidth, winHeight);
	var popupWin = window.open('', winName, 'menubar=no,toolbar=no,resizable=yes,status=yes' + dims.scrollbarsStr + dims.widthStr + dims.heightStr + dims.posCode);
	if (popupWin){
		popupWin.document.open();
		popupWin.document.write('<html><head><title>' + winTitle + '</title></head><body bgcolor="white" style="margin: 10px 10px; padding: 0px;">')
		popupWin.document.write('<img src="' + imgSrc + '" width="' + imgWidth + '" height="' + imgHeight + '" />')
		popupWin.document.write('</body></html>')
		popupWin.document.close();
		popupWin.focus();
	}
	return false;
}
///

function printDebug(text){
	var oElem = gebid('debug');
	if(oElem) oElem.innerHTML = text ? text : "";
}
///



/*****************************
**   /
******************************/




function showsubsc()
{
 document.getElementById('subscr').style.display=(document.getElementById('subscr').style.display != 'block' ? 'block' : 'none');
}

function show_hide(elmnt)
{
 document.getElementById(elmnt).style.display=(document.getElementById(elmnt).style.display != 'block' ? 'block' : 'none');
}

function showdate()
{
  today=new Date();// Build an array initializer
  function isnArray()
  {
    argnr=isnArray.arguments.length
    for (var i=0;i<argnr;i++)
    {
      this[i+1] = isnArray.arguments[i];
    }
  }
  // And months and day arrays
  var isnMonths=new   isnArray("Января","Февраля","Марта","Апреля","Мая","Июня","Июля","Августа","Сентября","Октября","Ноября","Декабря");
  var thisyear = today.getYear(); // varies in JavaScript and JScript
  var thisyear = 2007; // varies in JavaScript and JScript

  var isnDays=new   isnArray("Воскресенье","Понедельник","Вторник","Среда","Четверг","Пятница","Суббота");
  var thisday = today.getDay(); // varies in JavaScript and JScript

  document.write(" "+isnDays[today.getDay()+1]+",  "+today.getDate()+" "+isnMonths[today.getMonth()+1]+"  "+thisyear+" г.")
}

function ChangeImg() {
 if(document.images) {
  eval("document."+ChangeImg.arguments[0]+
  ".src=('"+ChangeImg.arguments[1]+"')");
 }
}

function showmenu(elmnt)
{
document.getElementById(elmnt).style.visibility="visible"
}
function hidemenu(elmnt)
{
document.getElementById(elmnt).style.visibility="hidden"
}

v=0;

function ShowMap(path,w,h)
{
	if (v!=0) v.close();
	h+=30;
	v=window.open ('', 'view', 'scrolling=no,height='+h+',width='+w);
	with (v.document)
	{
	    open ();
	       write('<html><head><title>Карта проезда</title></head><body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0 ><table cellspacing="0" cellpadding="0" border="0"><tr><td align><img src="'+path+'" border=0></td></tr><tr><td align="center" valign="middle" height="30"><a href="javascript:;" onClick="window.close()">[ закрыть окно ]</a></td></tr></table></body></html>');
	    close ();
	}
	return false;
}

v=0;
function ShowPhoto(path,w,h,name)
{
  if (v!=0) v.close();
  h+=30;
  v=window.open ('', 'view', 'scrolling=no,height='+h+',width='+w);
  with (v.document)
{
 open ();
  write('<html><head><title>'+name+'</title></head><body topmargin=0 leftmargin=0 marginheight=0 marginwidth=0     bgcolor="#f0eeff"><table cellspacing="0" cellpadding="0" border="0"><tr><td align><img src="'+path+'"     border=0></td></tr><tr><td align="center" valign="middle" height="30"><a href="javascript:;" onClick="window.close()"><font color="000000">[ закрыть окно ]</font></a></td></tr></table></body></html>');
  close ();
}
	return false;
}

function popup(fileUrl, winW, winH, winN, scrollB) {
var winWidth = (winW)? winW : 200;
var winHeight = (winH)? winH : 200;
var winName = (winN)? winN : 'popupWin';
var scrollBars = (scrollB)? scrollB : 'auto'
posX = Math.round((screen.width - winWidth) / 2);
posY = Math.round((screen.height - winHeight) / 2);
posCode = "left="+posX+",top="+posY;
var popupWin = window.open(fileUrl, winName,"menubar=no,toolbar=no,scrollbars=" + scrollBars + ",status=yes,width=" + winWidth + ",height=" + winHeight + "," + posCode);
if (popupWin) popupWin.focus();
}

function makedate()
{
  document.forms.booking.dd.value = document.forms.booking.outDay.value +"/"+ document.forms.booking.outMonth.value +"/"+document.forms.booking.outYear.value
  document.forms.booking.ad.value = document.forms.booking.inDay.value +"/"+ document.forms.booking.inMonth.value +"/"+document.forms.booking.inYear.value
  if (document.forms.booking.dccode.value!='') { document.forms.booking.dc.value = document.forms.booking.dccode.value; }
  if (document.forms.booking.accode.value!='') { document.forms.booking.ac.value = document.forms.booking.accode.value; }
  document.forms.booking.inYear.value=''
  document.forms.booking.inDay.value=''
  document.forms.booking.inMonth.value=''
  document.forms.booking.outYear.value=''
  document.forms.booking.outDay.value=''
  document.forms.booking.outMonth.value=''
}