// JavaScript Document
function showHiddenElem(id) {
	elem = document.getElementById(id+'_');
	if(!elem) return false;
	if(elem.style.display == 'block') elem.style.display = 'none';
	else elem.style.display = 'block';
}


/**********************************************************/
function checkSelectForm(){
	if($('country').selectedIndex > 0 && $('city').selectedIndex >= 0){
		return true;
	}else {
		alert('Вы не выбрали страну или город вылета');
		return false;
	}
}

function getCity(elem){
	if(elem.selectedIndex > 0){
		
		var select = document.getElementById('city');
		
		$A(select.options).each(function(item) {
			$(item.id).remove();
		});
		$('city').disable();
		var option = document.createElement('OPTION');
		select.appendChild(option);
		option.innerHTML = 'Пожалуйста, подождите...';
		option.id = 'please_wait';
		
		var options = {
			method: 'post',
			parameters: {value: elem.value },
			
			onComplete: function(transport) {
				var data = transport.responseText.evalJSON();
				if(data) $('city').enable();
				//$('please_wait').remove();

				$A(select.options).each(function(item) {
					$(item).remove();
				});

				/*var option = document.createElement('OPTION');
				select.appendChild(option);
				option.id = 'to';
				option.innerHTML = '-----------------------------';*/

				for(var i = 0; i < data.length; i++) {
					var option = document.createElement('OPTION');
					select.appendChild(option);
					option.value = data[i]['id'];
					option.id = data[i]['id'];
					option.innerHTML = data[i]['name'];
				}
		  },
		  
		  onException: function(ajax, error) {
		  }
		};
		
		var a = new Ajax.Request(site_url + 'getCity/', options);
	}
}
/**********************************************************/

/**** Каталог отелей ****************/

function getContent(){
	if($('country_catalog').selectedIndex <= 0){
		alert('Вы не выбрали страну');
		return false;
	}
	else if($('city_catalog').selectedIndex < 0){
		alert('Вы не выбрали город');
		return false;
	}
	else if($('stars_').selectedIndex < 0){
		alert('Вы не выбрали количество звезд');
		return false;
	}
	else {
		content(1);
	}
}

function content(page){
	var url = site_url + 'get_content/';
	var pars = {'country': $('country_catalog').value, 'city' : $('city_catalog').value, 'stars' : $('stars_').value, 'page' : page };
    new Ajax.Updater(
    	{success: 'hotel_content'}, 
    	url, 
    	{method: 'POST', parameters: pars, onLoading: showDiv, onComplete: hideDiv}
    );
}

function showDiv(originalRequest){
	$('loading_').style.display = 'block';
}
function hideDiv(originalRequest){
	$('loading_').style.display = 'none';
}

function go_page(page){
	content(page);
}


function getCity_catalog(elem){
	if(elem.selectedIndex > 0){
		
		var select = document.getElementById('city_catalog');
		var select_stars = document.getElementById('stars_');
		
		//очистка
		$A($('city_catalog').options).each(function(item) {
	  		$(item).remove();
		});
		$A($('stars_').options).each(function(item) {
			$(item).remove();
		});
		
		$('city_catalog').disable();
		var option = document.createElement('OPTION');
			select.appendChild(option);
			option.innerHTML = 'Пожалуйста, подождите...';
			option.id = 'please_wait';
		
		new Ajax.Request(site_url + 'catalog_city/', {

		  method: 'post',
		  parameters: {'value' : elem.value },
		  
		  onSuccess: function(transport) {
		  	var data = transport.responseText.evalJSON();
		  	if(data) $('city_catalog').enable();
		  	//$('please_wait').remove();
		  	
		  	$A(select.options).each(function(item) {
		  		$(item.id).remove();
			});
			$A($('stars_').options).each(function(item) {
				$(item.id).remove();
			});
		  	
		  	/*var option = document.createElement('OPTION');
			select.appendChild(option);
			option.id = 'to';
			option.innerHTML = '-----------------------------';*/
			
			for(var i = 0; i < data.length; i++){
				var option = document.createElement('OPTION');
				select.appendChild(option);
				option.value = data[i]['id'];
				option.id = data[i]['id'];
				option.innerHTML = data[i]['name'];
			}
			if($('stars_').options.length <= 0){
				/*var option = document.createElement('OPTION');
				select_stars.appendChild(option);
				option.id = 'to';
				option.innerHTML = '--';*/
				
				//звезды
				var option = document.createElement('OPTION');
				select_stars.appendChild(option);
				option.id = 'all';
				option.value = 'all';
				option.innerHTML = 'Все звезды';
				for(var i = 2; i <= 5; i++){
					var option = document.createElement('OPTION');
					select_stars.appendChild(option);
					option.value = i;
					option.id = i;
					option.innerHTML = i;
				}
				
				
			}
		  }
		});
	}
}
/**** Каталог отелей ****************/


/**** Каталог туров ****************/

function getContent_tyrns(){
	if($('country_turns').selectedIndex < 0){
		alert('Вы не выбрали тип тура');
		return false;
	}
	else if($('type').selectedIndex <= 0){
		alert('Вы не выбрали страну');
		return false;
	}
	else content_tyrns(1);
}

function content_tyrns(page, tip){
	var url = site_url + 'get_html_tyrns/';
	if(page == 1) tip = '';
	
	
	if($('city_resort') != null){
		var pars = {'type' : $('type').value, 'country_turns' : $('country_turns').value, 'event' : $('country_turns').options[$('country_turns').selectedIndex].id ,'page' : page, 'tip': tip, 'city_resort' : $('city_resort').value };
	}else{
		var pars = {'type' : $('type').value, 'country_turns' : $('country_turns').value, 'event' : $('country_turns').options[$('country_turns').selectedIndex].id ,'page' : page, 'tip': tip };
	}
    new Ajax.Updater(
    	{success: 'tyrns_content'}, 
    	url, 
    	{method: 'POST', parameters: pars, onLoading: showDiv, onComplete: hideDiv}
    );
}

/*function showDiv(originalRequest){
	$('loading_').style.display = 'block';
}
function hideDiv(originalRequest){
	$('loading_').style.display = 'none';
}

/*function go_pages(page, tip){
	content_tyrns(page, );
}*/


function getType(elem, events){
	if(elem.selectedIndex > 0){
		
		var select = document.getElementById('country_turns');
		
		//очистка
		$A($('country_turns').options).each(function(item) {
	  		$(item).remove();
		});
		$('country_turns').disable();
		
		var option = document.createElement('OPTION');
		select.appendChild(option);
		option.innerHTML = 'Пожалуйста, подождите...';
		option.id = 'please_wait';
		
		new Ajax.Request(site_url + 'get_country/', {

		  method: 'post',
		  parameters: {'value' : elem.value },
		  
		  onSuccess: function(transport) {
		  	var data = transport.responseText.evalJSON();
//		  	if(data) 
		  	$('country_turns').enable();
		  	
		  	var tip = data['tip'];
		  	var city_resort = data['city_resort'];
		  	
		  	if(tip){
			  	$A(select.options).each(function(item) {
			  		$(item).remove();
				});
			  	
				var option = document.createElement('OPTION');
				select.appendChild(option);
				option.id = 'all';
				option.value = 'all';
				option.innerHTML = 'Все';
				
				for(var i = 0; i < tip.length; i++){
					var option = document.createElement('OPTION');
					select.appendChild(option);
					option.value = tip[i]['id'];
					if(tip[i]['event']) option.id = 'event';
					else option.id = tip[i]['id'];
					option.innerHTML = tip[i]['type'];
					//if(data[i]['event']) option.className = 'event';
				}
		  	}
		  	if(city_resort){
				$A($('city_resort').options).each(function(item) {
			  		$(item).remove();
				});
				
				var option = document.createElement('OPTION');
				$('city_resort').appendChild(option);
				option.id = 'all';
				option.value = 'all';
				option.innerHTML = 'Все';
				
				for(var i = 0; i < city_resort.length; i++){
					var option = document.createElement('OPTION');
					$('city_resort').appendChild(option);
					option.value = city_resort[i]['id'];
					option.id = city_resort[i]['id'];
					option.innerHTML = city_resort[i]['name'];
				}
		  	}
		  }
		});
	}
}
/**** Каталог туров ****************/

function is_valid(tirag)
{
	if(!isFinite(tirag)) {
		return false;
	}
	if(!tirag){
		return false;
	}
	if(tirag < 0)	{
		return false;
	}
	if(tirag%1 != 0) {
		return false;
	}
	else return true;
}

function showAnswer(elem, id){
	var div = document.getElementById(id);
	if(elem.id == 0){
		div.style.display = 'block';
		elem.id = 1;
		elem.innerHTML = 'Скрыть ответ';
	}
	else{
		div.style.display = 'none';
		elem.id = 0;
		elem.innerHTML = 'Показать ответ';
	}
}

function showPrim(id){
	$('prim'+id).style.display = 'block';
	
}
function hideElem(id){
	$('prim'+id).style.display = 'none';
}


function showPrimAvia(id){
	$('Avia'+id).style.display = 'block';
	
}
function hideElemAvia(id){
	$('Avia'+id).style.display = 'none';
}

function checkFaqForm() {
	var required = {
		"fio"		:	"Вы не ввели Ваше имя",
		"qw"		:	"Вы не ввели вопрос",
		"code"		:	"Вы не ввели контрольный номер либо ввели его неверно"
	};
	for (var i in required) {
		var el = document.getElementById(i);
		if(el.id == 'code' && el.value && !isFinite(el.value))
		{
			alert(required['code']);
			el.focus();
			return false;
		};
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkSubscribeForm(){
	if(!$F('email') || !checkEmail($F('email'))) {
		alert('Не введен или введен некорректный e-mail');
		return false;
	}
	else return true;
}

function checkEmail(email) {
	if(!email) return email;
	if(!email.match(/^[\w\.\-]+@[\w\.\-]+\.[a-zA-Z]{2,}$/i, "")) return false;
	return true;
}

function checkFeedbackForm(){
	var required = {
		"name"		:	"Вы не ввели Ваше ФИО",
		"email"		:	"Не введен или введен некорректный e-mail",
		"msg"		:	"Вы не ввели сообщение"
	};

	for (i in required) {
		var el = document.getElementById(i);
		if(el.id == 'email' && el.value && !checkEmail(el.value)){
			alert(required['email']);
			el.focus();
			return false;
		};
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkFeedbackFormS(){
	
	var required = {
		"names"		:	"Вы не ввели Ваше ФИО",
		"emails"	:	"Не введен или введен некорректный e-mail",
		"msgs"		:	"Вы не ввели сообщение"
	};

	for (i in required) {
		var el = document.getElementById(i);
		if(el.id == 'emails' && el.value && !checkEmail(el.value)){
			alert(required['emails']);
			el.focus();
			return false;
		};
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkClientForm(){
	var required = {
		"fase"		:	"Вы не ввели контактное лицо",
		"date"		:	"Вы не ввели даты поездки",
		"napravl"	:	"Вы не ввели направление",
		"cat_hotel"	:	"Вы не ввели категорию отеля",
		"kol"		:	"Вы не ввели количество туристов",
		"budjet"	:	"Вы не ввели бюджет",
		"msg"		:	"Вы не ввели примечание",
		"backlink"	:	"Вы не ввели координаты для обратной связи"
	};

	for (i in required) {
		var el = document.getElementById(i);
		if(el.id == 'napravl' && el.selectedIndex <= 0){
			alert(required['napravl']);
			el.focus();
			return false;
		};
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}

function checkStrSearch() {
	var str = document.getElementById('srch');
	if((str.value.length) < 3) {
		alert('Длина поисковой фразы должна быть больше 3 символов');
		return false;
	} 
	else return true;
}

function checkAuthForm(){
	var required = {
		"name"		:	"Вы не ввели название агентства",
		"ur_adr"		:	"Вы не ввели юридический адрес",
		"fiz_adr"		:	"Вы не ввели фактический адрес",
		"tel"		:	"Вы не ввели Телефон/факс",
//		"site"		:	"Вы не ввели cайт",
		"fio_dir"		:	"Вы не ввели ФИО директора",
		"fio_buh"		:	"Вы не ввели ФИО бухгалтера",
		"inn"		:	"Вы не ввели ИНН предприятия",
		"ogrn"		:	"Вы не ввели ОГРН предприятия",
		"number_vs"		:	"Вы не ввели номер свидетельства регистрации",
		"number_uch"		:	"Вы не ввели номер свидетельства постановки на учет в налоговом органе",
		"kpp"		:	"Вы не ввели КПП",
		"rs"		:	"Вы не ввели Р/с",
		"ks"		:	"Вы не ввели К/с",
		"bik"		:	"Вы не ввели БИК",
		"bank"		:	"Вы не ввели банк",
		"email"		:	"Не введен или введен некорректный e-mail"
	};
	for (i in required) {
		var el = document.getElementById(i);
		if(el.id == 'email' && el.value && !checkEmail(el.value)){
			alert(required['email']);
			el.focus();
			return false;
		};
		/*if (el.id == 'pass1' && !el.value){
			alert(required['pass1']);
			el.focus();
			return false;
		}
		if(el.id == 'pass1' && el.value && el.value.length < 4)
		{
			alert('Длина пароля меньше 4 символов');
			el.focus();
			return false;
		};
		if (el.id == 'pass2' && !el.value){
			alert(required['pass2']);
			el.focus();
			return false;
		}
		if(el.id == 'pass2' && el.value)
		{
			var pass1 = document.getElementById('pass1');
			if(pass1.value != el.value){
				el.focus();
				alert('Пароль и подтверждение пароля не совпадают');
				return false;
			}
		};*/
		if (!el.value) {
			alert(required[i]);
			el.focus();
			return false;
		}
	}
	return true;
}