old_image = null;
function chandeImage(id, path) {
  old_image = document.getElementById('image_'+id).src;
  document.getElementById('image_'+id).src = "/images.php?type=carmodels&name="+path;
  }

function restoreImage(id) {
  document.getElementById('image_'+id).src = old_image;
  }

function loadOther(xet, div, action) {
  check = false;
  arr = new Array();
  arr['action'] = xet;
  target = div;
  if (document.getElementById(div).offsetHeight) {
    document.getElementById(div).style.height = document.getElementById(div).offsetHeight+"px";
    }
  document.getElementById(div).innerHTML = "<img src='/img/admin/searching.gif' alt='' />";
  bajax.call('/ajax.php', exemplo, arr)
  }

function exemplo(x) {
  document.getElementById(target).innerHTML = x;
  document.getElementById(target).style.height = "auto";
  check = true;
  }

function showChar(id, param) {
  if (param == 1) {
    loadOther('/catalogue/complect/'+id+'.html', 'char_container');
    }
  else if (param == 2) {
    loadOther('/catalogue/base/'+id+'.html', 'char_container');
    }
  else if (param == 3) {
    loadOther('/catalogue/opt/'+id+'.html', 'char_container');
    }
  }

function add_opt(obj) {
  var opt_price = Number(document.getElementById('opt_price').innerHTML);
  var summary = Number(document.getElementById('summary').innerHTML);
  reg = /^add_opt_/;
  if (obj.checked) {
	document.getElementById('opt_price').innerHTML = opt_price + Number(obj.name.replace(reg, ""));
	}
  else {
	document.getElementById('opt_price').innerHTML = opt_price - Number(obj.name.replace(reg, ""));
	}
  document.getElementById('summary').innerHTML = Number(document.getElementById('opt_price').innerHTML) + Number(document.getElementById('base_price').innerHTML);
  }

function changeSel(id) {
  var tbl = document.getElementById('compare_tbl');
  var tds = tbl.getElementsByTagName('td');
  var reg = /^holder_/;
  var itm = null; 
  for (i = 0; i < tds.length; i++) {
    if (reg.test(tds[i].id)) {
      if (tds[i].id == "holder_"+id) {
        itm = tds[i].getElementsByTagName('a');
        for (j = 0; j < itm.length; j++) {
          itm[j].style.fontWeight = "bold";
          }
        }
      else {
        itm = tds[i].getElementsByTagName('a');
        for (j = 0; j < itm.length; j++) {
          itm[j].style.fontWeight = "normal";
          }
        }
      }
    }
  }

function compChar(id1, id2, param) {
  if (param == 1) {
    loadOther('/catalogue/compopt/'+id1+'/'+id2, 'char_container');
    }
  else if (param == 2) {
    loadOther('/catalogue/compchar/'+id1+'/'+id2, 'char_container');
    }
  }


var current_field = null;
function updateList(obj, list_id) {
  img = obj.parentNode.getElementsByTagName('img');
  img[0].src= "/images.php?type=carmodels&name="+images[obj.value];
  current_field = list_id;

  arr = new Array();
  arr['action'] = "/catalogue/listbuild/"+obj.value;
  var select = document.getElementById(list_id);
  var selectChilds = select.childNodes.length;
  for (i = 0; i < selectChilds; i++) {
    select.removeChild(select.firstChild);
    }
  var new_option = document.createElement('option');
  new_option.setAttribute('value', "0");
  new_option.appendChild(document.createTextNode("Ждите..."));
  select.disabled = true;
  select.appendChild(new_option);
  bajax.call('/ajax.php', fill_list, arr, true);
  }

function fill_list(x) {
  var select = document.getElementById(current_field);
  select.disabled = false;
  var selectChilds = select.childNodes.length;
  for (i = 0; i < selectChilds; i++) {
    select.removeChild(select.firstChild);
    }
  var data = x.getElementsByTagName('option');
  for (i = 0; i < data.length; i++) {
    var new_option = document.createElement('option');
    new_option.setAttribute('value', data[i].getAttribute('val'));
    new_option.appendChild(document.createTextNode(data[i].firstChild.data));
    select.appendChild(new_option);
    }
  }

function changeImg(id, img, imgLrg, trg) {
  document.getElementById(id).style.visibility = "hidden";
  document.getElementById(id).src = "/images.php?type="+trg+"&name="+img;
  document.getElementById('loader').parentNode.style.visibility = "visible";
  img_large = imgLrg;
  }

function showImg(obj) {
  obj.style.visibility = "visible";
  document.getElementById('loader').parentNode.style.visibility = "hidden";
  }

function zoomImage() {
  document.getElementById('gray_bg').style.height = document.getElementById('bodyId').offsetHeight+"px";
  document.getElementById('content_loader').style.height = document.getElementById('bodyId').offsetHeight+"px";
  document.getElementById('content_loader').innerHTML = "<div style='text-align: center; width: 23px; margin: 330px auto auto auto; height: 23px; background: url(/img/bg_red_load.gif)'><img src='/img/searching.gif' alt='' style='margin: 5px;' /></div>";
  document.getElementById('gray_bg').style.display = "block";
  document.getElementById('content_loader').style.display = "block";
  document.getElementById('img_large').src="/images.php?type=gallery&name="+img_large;
  }

function showBlock(src) {
  if (!/empty\.gif/.test(src)) {
    document.getElementById('content_loader').style.display = "none";
    document.getElementById('loaded_image').style.display = "block";
    }
  }

function closeWin(lnk) {
  lnk.style.display = "none";
  document.getElementById('gray_bg').style.display = "none";
  }


var timeout = null;
var inc = 0;
var counter = 1;
var obj_mem = null;
function moveImg(obj, id) {
  window.clearTimeout(timeout);
  inc = 0;
  counter = 1;
  obj.style.visibility = "hidden";
  obj_mem = obj;
  if (id == 1) {
    timeout = window.setTimeout("minusOne("+counter+")", 0);
    }
  else {
    timeout = window.setTimeout("plusOne("+counter+")", 0);
    }
  }

function plusOne(counter, inct) {
  window.clearTimeout(timeout);
  if (counter < 60) {
     if (counter >= 30) {
       inc = inc + 10;
       }
     if (counter+5 > 60) {
       document.getElementById('scroll').scrollTop += 60-counter+1;
       obj_mem.style.visibility = "visible";
       return;
       }
     else {
       counter+=5;
       document.getElementById('scroll').scrollTop += 5;
       }
     timeout = window.setTimeout("plusOne("+counter+", "+inc+")", inc);
     }
  }

function minusOne(counter, inct) {
  window.clearTimeout(timeout);
  if (counter < 60) {
     if (counter >= 30) {
       inc = inc + 10;
       }
     if (counter+5 > 60) {
       document.getElementById('scroll').scrollTop -= 60-counter+1;
       obj_mem.style.visibility = "visible";
       return;
       }
     else {
       counter+=5;
       document.getElementById('scroll').scrollTop -= 5;
       }
     timeout = window.setTimeout("minusOne("+counter+", "+inc+")", inc);
     }
  }

function checkComp(obj) {
  var vals = obj.form.getElementsByTagName('input');
  for (i = 0; i < vals.length; i++) {
    if (vals[i].type == 'radio' && vals[i].checked) {
      obj.form.submit();
      return;
      }
    }
  alert("Выберите версию для сравнения");
  }

function formPreview(id) {
  document.getElementById(id).action = "/to/preview.html";
  document.getElementById(id).submit();
  }

function countDays(is_tr) {
  if (!is_tr) {
    pasteDate();
    return;
    }
  var checkInDate = document.getElementById('checkInDate');
  var checkOutDate = document.getElementById('checkOutDate');
  var checkInDateVal = checkInDate.value;
  var checkInMonthYear = document.getElementById('checkInMonthYear');
  if (checkInMonthYear.value != "-1") {
    var year = Number(checkInMonthYear.value.substr(-4, 4));
    var month = Number(checkInMonthYear.value.substr(0, checkInMonthYear.value.length-4));
    var days_in_month = coutDaysImMonth(month, year);

    var selectChilds = checkInDate.childNodes.length;
    for (i = 0; i < selectChilds; i++) {
      checkInDate.removeChild(checkInDate.firstChild);
      }
    var selectChilds = checkOutDate.childNodes.length;
    for (i = 0; i < selectChilds; i++) {
      checkOutDate.removeChild(checkOutDate.firstChild);
      }

    if (checkInDateVal > days_in_month) {
      checkInDateVal = days_in_month;
      }
    for (i = 0; i <= days_in_month; i++) {
      var new_option = document.createElement('option');
      if (i == 0) {
        new_option.setAttribute('value', "-1");
        new_option.appendChild(document.createTextNode(""));
        }
      else {
        new_option.setAttribute('value', i);
        if (checkInDateVal == i) {
          new_option.setAttribute('selected', 'selected');
          }
        new_option.appendChild(document.createTextNode(i));
        }
      checkInDate.appendChild(new_option);
    }

    if (month + 3 > 11) {
      var days_in_next_month = coutDaysImMonth(month + 3 - 11, year+1);
      }
    else {
      var days_in_next_month = coutDaysImMonth(month+3, year);
      }

    for (i = 0; i <= days_in_next_month; i++) {
      var new_option = document.createElement('option');
      if (i == 0) {
        new_option.setAttribute('value', "-1");
        new_option.appendChild(document.createTextNode(""));
        }
      else {
        new_option.setAttribute('value', i);
        new_option.appendChild(document.createTextNode(i));
        }
      checkOutDate.appendChild(new_option);
      }
    }
  pasteDate();
  }

var nights = 1;
function pasteDate() {
  var checkInDate = document.getElementById('checkInDate');
  var checkOutDate = document.getElementById('checkOutDate');
  var checkInDateVal = checkInDate.value;
  var checkInMonthYear = document.getElementById('checkInMonthYear');
  var checkOutMonthYear = document.getElementById('checkOutMonthYear');
  if (checkInMonthYear.value != "-1" && checkInDate.value != "-1") {
    var options = checkOutDate.getElementsByTagName('option');
    var check = true;
    for (i = 0; i < options.length; i++) {
      if (options[i].value == Number(checkInDate.value) + nights) {
        options[i].selected = "true";
        check = false;
        break;
        }
      }
    var optionsY = checkOutMonthYear.getElementsByTagName('option');
    for (j = 0; j < optionsY.length; j++) {
      if (optionsY[j].value == Number(checkInMonthYear.value)) {
        optionsY[j].selected = "true";
        break;
        }
      }
    if (check) {
      var year = Number(checkInMonthYear.value.substr(-4, 4));
      var month = Number(checkInMonthYear.value.substr(0, checkInMonthYear.value.length-4));
      var days_more = nights - (coutDaysImMonth(month, year) - Number(checkInDate.value));
      var count = 0;
      while (days_more > 0) {
        if (month == 11) {
          var nxt = coutDaysImMonth(0, year+1);
          }
        else {
          var nxt = coutDaysImMonth(month+1, year);
          }

        if (days_more - nxt < 0) {
          var fin = days_more;
          }
        days_more -= nxt;
        count++;
        }

      options[fin].selected = "true";
      optionsY[j+count].selected = "true";
      }
    }
  }

function coutDaysImMonth(month, year) {
   var c = new Date(year, month, 1);
   if (month == 11) {
     b = new Date(year+1, 0, 1);
     }
   else {
     b = new Date(year, month+1, 1);
     }
   return (b-c)/3600/24/1000;
  }

function countNights() {
  var days_1 = document.getElementById('checkInDate').value;
  var days_2 = document.getElementById('checkOutDate').value;
  var checkInMonthYear = document.getElementById('checkInMonthYear');
  var checkOutMonthYear = document.getElementById('checkOutMonthYear');
  var year_1 = Number(checkInMonthYear.value.substr(-4, 4));
  var month_1 = Number(checkInMonthYear.value.substr(0, checkInMonthYear.value.length-4));
  var year_2 = Number(checkOutMonthYear.value.substr(-4, 4));
  var month_2 = Number(checkOutMonthYear.value.substr(0, checkOutMonthYear.value.length-4));
  var date_1 = new Date(year_1, month_1, days_1);
  var date_2 = new Date(year_2, month_2, days_2);
  var nights = (date_2 - date_1)/3600/24/1000;
  if (nights > 0 && nights <= 99) {
    var options = document.getElementById('numOfNights').getElementsByTagName('option');
    for (i = 0; i < options.length; i++) {
      if (options[i].value == nights) {
        options[i].selected = "true";
        break;
        }
      }
    }
  }

var img_loaded = 0;
var images_array = new Array();
var tmout_main = null;
var tmout = null;
var opacity_max = 1;
var opacity_min = 0;
var num2 = null;
function imgLoaded() {
  img_loaded++;
  if (img_loaded == count_images) {
    document.getElementById('image_holder').removeChild(document.getElementById('waiting_img').parentNode);
    images_array = document.getElementById('image_holder').getElementsByTagName('img');
    images_array[0].style.visibility = "visible";
    tmout_main = window.setTimeout("slideshow(0)", 4000);
    }
  }

function slideshow(num) {
  window.clearTimeout(tmout_main);
  opacity_max = 1;
  opacity_min = 0;
  num2 = num;
  fadeInOut(num2);
  if (typeof(images_array[num+1])  != "undefined") {
    num++;
    }
  else {
    num = 0;
    }
  tmout_main = setTimeout("slideshow("+num+")", 6000);
  }

function fadeInOut(num2) {
  window.clearTimeout(tmout);
  if (opacity_max >= 0 && opacity_min <= 1) {
    opacity_max -= 0.05;
    opacity_min += 0.05;
    if (typeof(images_array[num2+1]) != "undefined") {
      var nxt_num = num2+1;
      }
    else {
      var nxt_num = 0;
      }    
    if (typeof(images_array[num2].style.filter)  != "undefined") {
      images_array[num2].style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+ opacity_max*100 +")";
      images_array[nxt_num].style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+ opacity_min*100 +")";
      }
    if (typeof(images_array[num2].style.MozOpacity)  != "undefined") {
      images_array[num2].style.MozOpacity = opacity_max;
      images_array[nxt_num].style.MozOpacity = opacity_min;
      }
    if (typeof(images_array[num2].style.opacity)  != "undefined") {
      images_array[num2].style.opacity = opacity_max;
      images_array[nxt_num].style.opacity = opacity_min;
      }
    images_array[nxt_num].style.visibility = "visible";
    tmout = setTimeout("fadeInOut("+num2+")", 100);
    }
  else {
    if (navigator.appName != "Microsoft Internet Explorer") {
      images_array[num2].style.visibility = "hidden";
      }
    }
  }

function changeClass(val, lng) {
  window.open("/"+lng+"/rooms/"+val+".html", '_self');
  }

function manPac(obj) {
  if (obj.nextSibling.style.display == 'block') {
    obj.nextSibling.style.display = 'none';
    }
  else {
    obj.nextSibling.style.display = 'block';
    }
  }