var clickFlag = false;

function checkForm() {

	if(clickFlag == true) return false;
	
	var txt = '';
	
	if(document.main.person.value == '') {
	
		txt = txt + ' — Контактное лицо\n';
		document.main.person.style.border = "2px solid red";
		}
	else
		document.main.person.style.border = "1px solid gray";
		

	if(document.main.phone.value == '') {
		
		txt = txt + ' — Телефон\n';
		document.main.phone.style.border = "2px solid red";
		}
	else
		document.main.phone.style.border = "1px solid gray";	
		

	if(document.main.email.value == '') {
		
		txt = txt + ' — E-mail\n';
		document.main.email.style.border = "2px solid red";
		}
	else
		document.main.email.style.border = "1px solid gray";
		
	avia = '';
	for(i = 0; i < document.main.from.length; i++) {
		
		document.main.from[i].style.border = "1px solid gray";
		document.main.to[i].style.border = "1px solid gray";
		document.main.num[i].style.border = "1px solid gray";

		if(document.main.from[i].value == '' &&
		   document.main.to[i].value == '' &&
		   (document.main.num[i].value == '' || document.main.num[i].value == 0)) {
		   
		   if(avia == '') avia = false;
		   }
		else {
			
			if(avia == '') avia = true;
			if(document.main.from[i].value == '') {
				
				txt = txt + ' — Рейс #' + (i+1) + ': Город или аэропорт вылета\n';
				document.main.from[i].style.border = "2px solid red";
				}

			if(document.main.to[i].value == '') {
				
				txt = txt + ' — Рейс #' + (i+1) + ': Город или аэропорт назаначения\n';
				document.main.to[i].style.border = "2px solid red";
				}

			if(document.main.num[i].value == '' || document.main.num[i].value == 0) {
				
				txt = txt + ' — Рейс #' + (i+1) + ': Количество пассажиров\n';
				document.main.num[i].style.border = "2px solid red";
				}
			}   
		}
		
	if(!avia) {
		
		txt = txt + ' — Информацию хотя бы об одном рейсе\n';
		
		document.main.from[0].style.border = "2px solid red";
		document.main.to[0].style.border = "2px solid red";
		document.main.num[0].style.border = "2px solid red";
		} 	
		
		
	if(txt != '') {
		
		txt = "Заполните, пожалуйста, следующие поля:\n\n" + txt + '\nСпасибо.';
		window.alert(txt);	
		return false;
		}
		
	clickFlag = true;
	
	return true;	
	}
	
	

function OWin(file, w, h) {
	
	var prm = "width="+w+",height="+h;
	win = window.open("./page_win.php?var="+file,"avia", prm);
	win.focus();
	
	return false;
	}	


//
// Author Denis Zubo | http://www.zubo.ru/
//

var oTRs = new Array();
var oDIVs = new Array();

var W3CDOM = (document.createElement && document.getElementsByTagName);

/*****************************
**   Main menu
******************************/

function MenuInit(id) {
	if (!W3CDOM) return;
	if (oElem = gebid(id)) {
		oTRs = getElementsByClassName(oElem, 'tr', '*');
		for (var i=0; i < oTRs.length; i++) {
			// выбираем строки, в которых две ссылки (картинка и текстовая)
			if ((oAs = getElementsByClassName(oTRs[i], 'a', '*')) && (oAs.length == 2)) {
				oAs[0].number = i;
				oAs[0].onmouseover = mouseGoesOver;
				oAs[0].onmouseout = mouseGoesOut;
				oAs[0].onmousedown = mouseGoesDown;
				oAs[0].onmouseup = mouseGoesUp;
				oAs[1].number = i;
				oAs[1].onmouseover = mouseGoesOver;
				oAs[1].onmouseout = mouseGoesOut;
				oAs[1].onmousedown = mouseGoesDown;
				oAs[1].onmouseup = mouseGoesUp;
			}
		}
	}
}

function mouseGoesOver() {
	addClass(oTRs[this.number], 'hover');
}

function mouseGoesOut() {
	removeClass(oTRs[this.number], 'hover');
	removeClass(oTRs[this.number], 'down');
}

function mouseGoesDown() {
	addClass(oTRs[this.number], 'down');
}

function mouseGoesUp() {
	removeClass(oTRs[this.number], 'down');
}

/*****************************
**   Main menu
******************************/

function RightMenuInit(id){
	if (!W3CDOM) return;
	var oDIV, aHeaders, aB;
	if (oElem = gebid(id)) {
		oDIVs = getElementsByClassName(oElem, 'div', 'item');
		for (var i=0; i < oDIVs.length; i++) {
			oDIV = oDIVs[i];
			// закрываем все разделы, кроме помеченных классом 'selected'
			if( !matchClass(oDIV, 'selected') ){
				addClass(oDIV, 'closed');
			}
			if ( (aHeaders = getElementsByClassName(oDIV, 'div', 'header')) && (aHeaders.length == 1) ){
				if ( (aB = aHeaders[0].getElementsByTagName('b')) && (aB.length > 0) ){
					aB[0].number = i;
					aB[0].onclick = mouseGoesClickRM;
					aB[0].onmouseover = mouseGoesOverRM;
					aB[0].onmouseout = mouseGoesOutRM;
				}
			}
		}
	}
}

function mouseGoesClickRM() {
	if( matchClass(oDIVs[this.number], 'closed') ){
		removeClass(oDIVs[this.number], 'closed');
	}
	else {
		addClass(oDIVs[this.number], 'closed');
	}
}

function mouseGoesOverRM() {
	addClass(oDIVs[this.number], 'hover');
}

function mouseGoesOutRM() {
	removeClass(oDIVs[this.number], 'hover');
}

/*****************************
**   Common Functions
******************************/

function gebid(id) {
  return document.getElementById(id);
}
///

// возвращает сам элемент или его ближайшего родителя, с именем класса classStr
function getSelfOrParentByClass(oElem, classStr){
	var limit = 10;
	while(limit > 0 && oElem){
		if(oElem.className == 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 = (strTagName == "*") ? '' : 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)
}
///

/*****************************
**   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);
	popupwin.resizeTo(dims.width + 12, dims.height + 62)

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;
}

/*****************************
**   Common class methods
******************************/
// Author Leechy, http://www.artlebedev.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+)') ) );
}

