﻿var f = document.demandForm;
function e(eName) {
 return document.getElementById(eName);
}
function isVis(eName) {
 var v = e(eName).style.display;
 if(v == 'none')
  return false;
 return true;
}

function trySendForm() {
 sendForm();
 return false;
}
function clearForm() {
 f.reset();
}
function sendForm() {
 if(!is_string(f.object.value)) {
  alert("Необходимо указать Объект");
  f.object.focus();
  return void(0);
 }
 if(!is_string(f.region.value)) {
  alert("Необходимо указать Регион");
  f.region.focus();
  return void(0);
 }
 if(isVis('districtF')) {
  if(!is_string(f.district.value)) {
   alert("Необходимо указать Район");
   f.district.focus();
   return void(0);
  }
 }
 else if(isVis('districtsF')) {
  var oneSel = false;
  for(i = 0; i < e('districtsElement').options.length; i++) {
   if(e('districtsElement').options[i].selected) {
    oneSel = true;
    break;
   }
  }
  if(!oneSel) {
   alert("Необходимо выбрать хотя бы один Район");
   return void(0);
  }
 }
 else {
  alert("Необходимо указать Район...");
  return void(0);
 }
 if(isVis('mkadR') && !is_int(f.mkadDistance.value, true, true)) {
  alert("Необходимо указать Расстояние от МКАД");
  f.mkadDistance.focus();
  return void(0);
 }
 if(isVis('cityR') && !is_string(f.city.value)) {
  alert("Необходимо указать Населённый пункт");
  f.city.focus();
  return void(0);
 }
 if(isVis('homeAreaR') && !is_int(f.homeArea.value, true, true)) {
  alert("Необходимо указать Площадь дома");
  f.homeArea.focus();
  return void(0);
 }
 if(isVis('yearR') && !is_int(f.year.value, true, true)) {
  alert("Необходимо указать Год постройки");
  f.year.focus();
  return void(0);
 }
 if(isVis('floorR') && !is_int(f.floor.value, true, true)) {
  alert("Неоходимо указать Этажность");
  f.floor.focus();
  return void(0);
 }
 if(isVis('materialR') && !is_string(f.material.value)) {
  alert("Необходимо указать Материал");
  f.material.focus();
  return void(0);
 }
 if(isVis('roofR') && !is_string(f.roof.value)) {
  alert("Необходимо указать Кровлю");
  f.roof.focus();
  return void(0);
 }
 if(isVis('landAreaR') && !is_int(f.landArea.value, true, true)) {
  alert("Необходимо указать Площадь участка");
  f.landArea.focus();
  return void(0);
 }
 if(isVis('landWidthR') && !is_int(f.landWidth.value, true, true)) {
  alert("Необходимо указать Ширину участка");
  f.landWidth.focus();
  return void(0);
 }
 if(isVis('landHeightR') && !is_int(f.landHeight.value, true, true)) {
  alert("Необходимо указать Длину участка");
  f.landHeight.focus();
  return void(0);
 }
 if(isVis('waterR') && !is_string(f.water.value)) {
  alert("Необходимо указать Питьевую воду");
  f.water.focus();
  return void(0);
 }
 if(isVis('heatingR') && !is_string(f.heating.value)) {
  alert("Необходимо указать Отопление");
  f.heating.focus();
  return void(0);
 }
 if(isVis('buildingsR') && !is_string(f.buildings.value)) {
  alert("Необходимо указать Дополнительные постройки");
  f.buildings.focus();
  return void(0);
 }
 if(isVis('approachR') && isVis('asphaltDistanceF') && !is_real(f.asphaltDistance.value, true)) {
  alert("Неоходимо указать Расстояние до асфальта");
  f.asphaltDistance.focus();
  return void(0);
 }
 if(isVis('reservoirR')) {
  if(!is_string(f.reservoir.value)) {
   alert("Необходимо указать Водоём");
   f.reservoir.focus();
   return void(0);
  }
  if(isVis('reservoirFName') && !is_string(f.reservoirName.value)) {
   alert("Необходимо указать Название водоёма");
   f.reservoirName.focus();
   return void(0);
  }
 }
 if(isVis('reservoirDistanceR') && !is_int(f.reservoirDistance.value, true, true)) {
  alert("Необходимо указать Расстояние до водоёма");
  f.reservoirDistance.focus();
  return void(0);
 }
 if(isVis('forestDistanceR') && !is_int(f.forestDistance.value, true, true)) {
  alert("Необходимо указать Расстояние до леса");
  f.forestDistance.focus();
  return void(0);
 }
 if(isVis('categoryR') && !is_string(f.category.value)) {
  alert("Необходимо указать Категорию земель");
  f.category.focus();
  return void(0);
 }
 if(isVis('usageR') && !is_string(f.usage.value)) {
  alert("Необходимо указать Разрешенное использование");
  f.usage.focus();
  return void(0);
 }
 if(!is_int(f.price.value, true, true)) {
  alert("Необходимо указать Цену");
  f.price.focus();
  return void(0);
 }
 if(!is_string(f.name.value)) {
  alert("Необходимо указать Имя");
  f.name.focus();
  return void(0);
 }
 if(!is_string(f.phone.value)) {
  alert("Необходимо указать Контактный телефон");
  f.phone.focus();
  return void(0);
 }
 if(!is_email(f.email.value)) {
  alert("Необходимо указать E-mail");
  f.email.focus();
  return void(0);
 }
 f.submit();
}
function iniForm() {
 changeRegionIDDemand();
 changeMaterialType();
 changeRoofType();
 changeWaterType();
 changeHeatingType();
 changeBuildingsType();
 changeApproach();
 changeReservoirType();
 changeCategoryType();
 changeUsageType();
 changeObjectType();
}
function ccForm() {
 var d1 = f.operationType.options[f.operationType.selectedIndex].value*1;
 var d2 = f.objectType.options[f.objectType.selectedIndex].value*1;
 var ds = 10*d1 + d2;
// alert("D1: " + d1 + " / D2: " + d2 + " / DS: " + ds);
/*
 d1: 1 - купить, 2 - продать
 d2: 1 - дом, 2 - земельный участок, 3 - КН, 4 - другое
 ds: 11 - купить дом, 12 - купить земельный участок, 13 - купить КН, 14 - купить другое
     21 - продать дом, 22 - продать земельный участок, 23 - продать КН, 24 - продать другое
*/
 e('demand1R').style.display = (d1 == 1)? '': 'none';
 e('demand2R').style.display = (d1 == 2)? '': 'none';
 e('mkadR').style.display = ((d1 == 1) || (f.regionID.options[f.regionID.selectedIndex].value == '72'))? '': 'none';
 e('mskDistR').style.display = ((d1 == 2) && (f.regionID.options[f.regionID.selectedIndex].value == '72'))? '': 'none';
// e('mkadR').style.display = ((d1 == 1) || ((d1 == 2) && (72)))? '': 'none';
 e('cityR').style.display = (d1 != 1)? '': 'none';
 e('homeAreaR').style.display = (ds == 21)? '': 'none';
 e('yearR').style.display = (ds == 21)? '': 'none';
 e('floorR').style.display = (ds == 21)? '': 'none';
 e('materialR').style.display = (d2 == 1)? '': 'none';
 e('roofR').style.display = (ds == 21)? '': 'none';
 e('landAreaR').style.display = (d2 < 3)? '': 'none';
 e('landWidthR').style.display = ((d1 == 2) && (d2 < 3))? '': 'none';
 e('landHeightR').style.display = ((d1 == 2) && (d2 < 3))? '': 'none';
 e('landShapeR').style.display = ((d1 == 2) && (d2 < 3))? '': 'none';
 e('electricityR').style.display = (d2 < 3)? '': 'none';
 e('waterR').style.display = (d2 < 3)? '': 'none';
 e('gasR').style.display = (d2 < 3)? '': 'none';
 e('heatingR').style.display = (d2 == 1)? '': 'none';
 e('toiletR').style.display = (d2 == 1)? '': 'none';
 e('buildingsR').style.display = (d2 < 3)? '': 'none';
 e('approachR').style.display = (d2 < 3)? '': 'none';
 e('reservoirR').style.display = (d2 < 3)? '': 'none';
 if(d2 < 3)
  changeReservoirType();
 else
  e('reservoirDistanceR').style.display = 'none';
 e('forestDistanceR').style.display = (d2 < 3)? '': 'none';
 e('residenceR').style.display = (d2 == 1)? '': 'none';
 e('categoryR').style.display = ((ds == 12) || (ds == 21) || (ds == 22))? '': 'none';
 e('usageR').style.display = ((ds == 12) || (ds == 21) || (ds == 22))? '': 'none';
 e('photosR').style.display = (d1 == 2)? '': 'none';
 e('filesR').style.display = (d1 == 2)? '': 'none';
}

function changeOperationType() {
 ccForm();
}

function changeObjectType() {
 var objectF = e('objectF');
 if(f.objectType.options[f.objectType.selectedIndex].value != 4) {
  objectF.style.display = 'none';
  f.object.value = f.objectType.options[f.objectType.selectedIndex].text;
 }
 else { // другой
  f.object.value = "";
  objectF.style.display = '';
 }
 ccForm();
}

function changeRegionIDDemand() {
 var districts = e('districtsElement');
 var districtsF = e('districtsF');
 var districtF = e('districtF');
 var regionF = e('regionF');
 var d1 = f.operationType.options[f.operationType.selectedIndex].value*1;
 if(f.regionID.options[f.regionID.selectedIndex].value != '0') {
  regionF.style.display = 'none';
  f.region.value = f.regionID.options[f.regionID.selectedIndex].text;
  districtF.style.display = 'none';
  eval("var selGroup = districts2_" + f.regionID.options[f.regionID.selectedIndex].value + ";");
  districts.options.length = selGroup.length;
  for(i = 0; i < selGroup.length; i++) {
   districts.options[i].value = selGroup[i];
   districts.options[i].text  = selGroup[i];
   if(districts2_selected && (districts2_selected[0] == f.regionID.options[f.regionID.selectedIndex].value)) {
    for(j = 1; j < districts2_selected.length; j++)
     if(districts.options[i].value == districts2_selected[j])
      districts.options[i].selected = true;
   }
  }
  if(districts2_selected && (districts2_selected[0] == f.regionID.options[f.regionID.selectedIndex].value)) {
   districts2_selected = null;
  }
  districtsF.style.display = '';
 }
 else { // другой
  f.region.value = "";
  regionF.style.display = "";
  districtsF.style.display = 'none';
  f.district.value = "";
  districtF.style.display = "";
 }
 e('mkadR').style.display = ((d1 == 1) || (f.regionID.options[f.regionID.selectedIndex].value == '72'))? '': 'none';
 e('mskDistR').style.display = ((d1 == 2) && (f.regionID.options[f.regionID.selectedIndex].value == '72'))? '': 'none';
 if((d1 == 2) && (f.regionID.options[f.regionID.selectedIndex].value == '72'))
  f.mkadSimile.selectedIndex = 1;
}

function changeMaterialType() {
 var materialF = e('materialF');
 if(f.materialType.options[f.materialType.selectedIndex].value != '0') {
  materialF.style.display = 'none';
  f.material.value = f.materialType.options[f.materialType.selectedIndex].text;
 }
 else { // другой
  f.material.value = "";
  materialF.style.display = '';
 }
}

function changeRoofType() {
 var roofF = e('roofF');
 if(f.roofType.options[f.roofType.selectedIndex].value != '0') {
  roofF.style.display = 'none';
  f.roof.value = f.roofType.options[f.roofType.selectedIndex].text;
 }
 else { // другой
  f.roof.value = "";
  roofF.style.display = '';
 }
}

function changeWaterType() {
 var waterF = e('waterF');
 if(f.waterType.options[f.waterType.selectedIndex].value != '0') {
  waterF.style.display = 'none';
  f.water.value = f.waterType.options[f.waterType.selectedIndex].text;
 }
 else { // другой
  f.water.value = "";
  waterF.style.display = '';
 }
}

function changeHeatingType() {
 var heatingF = e('heatingF');
 if(f.heatingType.options[f.heatingType.selectedIndex].value != '0') {
  heatingF.style.display = 'none';
  f.heating.value = f.heatingType.options[f.heatingType.selectedIndex].text;
 }
 else { // другой
  f.heating.value = "";
  heatingF.style.display = '';
 }
}

function changeBuildingsType() {
 var buildingsF = e('buildingsF');
 if(f.buildingsType.options[f.buildingsType.selectedIndex].value != '0') {
  buildingsF.style.display = 'none';
  f.buildings.value = f.buildingsType.options[f.buildingsType.selectedIndex].text;
 }
 else { // другой
  f.buildings.value = "";
  buildingsF.style.display = '';
 }
}

function changeApproach() {
 e('asphaltDistanceF').style.display = (f.approach.options[f.approach.selectedIndex].value != "асфальт")? '': 'none';
}

function changeReservoirType() {
 var reservoirF = e('reservoirF');
 var reservoirFName = e('reservoirFName');
 var reservoirDistanceR = e('reservoirDistanceR');

 f.reservoir.value = (f.reservoirType.options[f.reservoirType.selectedIndex].value != 0)? f.reservoirType.options[f.reservoirType.selectedIndex].text: '';
 reservoirF.style.display = (f.reservoirType.options[f.reservoirType.selectedIndex].value != 0)? 'none': '';
 reservoirFName.style.display = (f.reservoirType.options[f.reservoirType.selectedIndex].value >= 0)? 'none': '';
 reservoirDistanceR.style.display = (f.reservoirType.options[f.reservoirType.selectedIndex].value == 3)? 'none': '';
}

function changeCategoryType() {
 var categoryF = e('categoryF');
 if(f.categoryType.options[f.categoryType.selectedIndex].value != '0') {
  categoryF.style.display = 'none';
  f.category.value = f.categoryType.options[f.categoryType.selectedIndex].text;
 }
 else { // другой
  f.category.value = "";
  categoryF.style.display = '';
 }
}

function changeUsageType() {
 var usageF = e('usageF');
 if(f.usageType.options[f.usageType.selectedIndex].value != '0') {
  usageF.style.display = 'none';
  f.usage.value = f.usageType.options[f.usageType.selectedIndex].text;
 }
 else { // другой
  f.usage.value = "";
  usageF.style.display = '';
 }
}
