// Time-stamp: <2006-02-28 13:03:08 mambur>

function formLock(){
  $('#sForm select').attr('disabled', 'disabled');
  $('#code').removeAttr('disabled');
  $('#sForm input.submit').removeAttr('disabled');
}

$(document).ready(function(){

  $("#p_Date1").mask("99/99/9999");
  $("#p_Date2").mask("99/99/9999");
  /*
  $("#search").click(function(){
    if ($("#streetlst").val() == ""  && $("input[name='p_Price1']").val() == "" && $("input[name='p_Price2']").val() == "") {
      alert("Помилка, не всі обов'язкові поля заповнені");
      return false;
    }
  });
  */
  $("#code").focus(function(){
    $('#search').val('Шукати за кодом');
  });

  $("#sForm input:not(#code, #search)").focus(function(){
    $('#search').val('Пошук');
    $('#code').val('');
  });

  $("#map p a.mass").click(function(){
    $(this).toggleClass('selected');
    selector = "#map p." + $(this).attr('id') + " a:not(a.mass)";
    if ($(this).hasClass('selected')) {
      $(selector).addClass('selected');
    } else {
      $(selector).removeClass('selected');
    }
  });
  
  $("#map p a:not(a.mass)").click(function(){
    $(this).toggleClass('selected');
  });
  setTimeout("autocomp();",500);
});

function autocomp ()
  {
    try {
      $("input[id='streetlst']").autocomplete("/x/search_ua.php", {
        minChars: 3,
        max: 20,
        /*mustMatch: true,*/
        /*autoFill: true,*/
        multiple: true,
        matchSubset: 1,
        matchContains: 1,
        cacheLength: 10,
        formatItem: formatItem,
        selectOnly: 1
      });
      $("input[id='TownCode']").autocomplete("/x/searchR_ua.php", {
        minChars: 3,
        max: 20,
        /*mustMatch: true,*/
        /*autoFill: true,*/
        multiple: true,
        matchSubset: 1,
        matchContains: 1,
        cacheLength: 10,
        formatItem: formatItem,
        selectOnly: 1
      });
    } 
    catch (err) {
      //alert(err);
    }
    // Для Києва
    var reg = 0;
    if ($("input[id='p_Region']").val() != 1) reg = $("input[id='p_Region']").val();
    if (reg) {
      cnt = explode(reg, ",");
      cnt = cnt.length;
      $("#map p a").each(function(){
        if (reg.search ($(this).attr('id').substr(1, $(this).attr('id').length-1)) > -1)
          $(this).addClass('selected');
      });
      if (cnt == 1)
        text = 'Вибрано ' + cnt + ' район';
      if ((cnt > 1) && (cnt <= 4))
        text = 'Вибрано ' + cnt + ' райони';
      if (cnt >= 5)
        text = 'Вибрано ' + cnt + ' районів';
      $("#geoSelection").text(text);      
    }
    // Для районов области
    var dist = 0;
    if ($("input[id='p_StDistr']").val() != 1) dist = $("input[id='p_StDistr']").val();
    if (dist) {
      cnt = explode(dist, ",");
      cnt = cnt.length;
      $("#map p a").each(function(){
        if (dist.search ($(this).attr('id').substr(1, 7)) > -1)
          $(this).attr('class','selected');
      });
      if (cnt == 1)
        text = 'Вибрано ' + cnt + ' район';
      if ((cnt > 1) && (cnt <= 4))
        text = 'Вибрано ' + cnt + ' райони';
      if (cnt >= 5)
        text = 'Вибрано ' + cnt + ' районів';
      $("#geoSelection").text(text);      
    }
  }
function explode( item,delimiter ) {
tempArray=new Array(1);
var Count=0;
var tempString=new String(item);

while (tempString.indexOf(delimiter)>0) {
tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
Count=Count+1
}

tempArray[Count]=tempString;
return tempArray;
}
/*
 * Установка для переключателя срока оплаты (месяц/сутки)
 */
currentRentTerm = 'day';

function formatItem(row){
  return row[0] + "<br/><i>" + row[1] + "</i>";
}

function toggleExp(){
  $('form').toggleClass('collapsed');
  
  if ($('form').attr('class') == 'collapsed') {
    $('tr.ruler a').text('Детальніше');
  }
  else {
    $('tr.ruler a').text('Сховати');
  }
}

function toggleTerm(){
  if (currentRentTerm == 'day') {
    $('#rentTerm').text('місяць');
    currentRentTerm = 'month';
  }
  else {
    $('#rentTerm').text('доба');
    currentRentTerm = 'day';
  }
}

function formUnlock(){
  $('input').removeAttr('disabled');
  $('select').removeAttr('disabled');
  //$('#code').attr('value', '');
}

function mapToggle(){
  if ($('#map').css('display') == 'none') {
    $('td.geo iframe').toggle();
    $('#map').fadeIn();
  }
  else {
    $('td.geo iframe').toggle();
    $('#map').fadeOut();
  }
}

function resetIT(){
	$('#frm').clearForm();
}

function mapItemReset(){
  $('#map a').removeClass('selected');
}

function mapSubmit(){
  list = "";
  cnt = 0;
  var text = "Вибрано всі райони";
  $("#map p a").each(function(){
    if ($(this).hasClass('selected')) {
      cnt++;
      list = list + $(this).attr('id').substr(1, $(this).attr('id').length-1) + ",";
    }
    return true;
  });
  $("#p_Region").val(list.substr(0, list.length - 1));
  $("#p_StDistr").val(list.substr(0, list.length - 1));
  if (cnt == 1)
    text = 'Вибрано ' + cnt + ' район';
  if ((cnt > 1) && (cnt <= 4))
    text = 'Вибрано ' + cnt + ' района';
  if (cnt >= 5)
    text = 'Вибрано ' + cnt + ' районів';
  $("#geoSelection").text(text);
  mapToggle();
}

function changePhone(nameOf){
  if ($('input#' + nameOf).val() == "0") {
    $('input#' + nameOf).val('1');
    return true;
  }
  if ($('input#' + nameOf).val() == "1") {
    $('input#' + nameOf).val('0');
    return true;
  }
}

function trim(string){
  return string.replace(/(^\s+)|(\s+$)/g, "");
}
/*
function searchCode(){
  r = $('input#code').val();
  location.href = '/realtyshow/saleflat.lisp?id=' + trim(r);
  return false;
}
*/
function OpenEmployee(UserID){
  window.open('/empinfo.php?p_UID=' + UserID, '', 'toolbar=0,menubar=0,location=0,scrollbars=1,resizable=1,width=570,height=300');
  document.title = docTitle;
}

function OpenCatalog(RuType, RealtyID){
  window.open('catalogdet.php?p_RuType=' + RuType + '&p_RealtyID=' + RealtyID, '', 'toolbar=0,menubar=0,location=0,scrollbars=1,resizable=1,width=790,height=550');
  document.title = docTitle;
}

function FlipAllRows(){
  if (isOK) {
    var obj, img;
    var i = 0;
    curRowsDispState = !curRowsDispState;
    var disp = curRowsDispState ? 'block' : 'none';
    var c = ROOT_DIR + 'Theme/' + style_dir + '/pics/' + (curRowsDispState ? 'more_silver_top.gif' : 'more_silver_bottom.gif');
    var cTitle = ROOT_DIR + 'Theme/' + style_dir + '/pics/' + (curRowsDispState ? 'more_blue_top.gif' : 'more_blue_bottom.gif');
    i = 0;
    MM_findObj('RSTitle').src = cTitle;
    while (i < totalRows) {
      obj = MM_findObj('ROW' + i);
      obj.style.display = disp;
      img = MM_findObj('RS' + i);
      img.src = c;
      i++;
    }
  }
  else {
    alert('Not supportet');
  }
  return false;
}

function RowDisplay(RowIndex){
  if (isOK) {
    var obj = MM_findObj('ROW' + RowIndex);
    var disp = 'none';
    var c = 'more_silver_bottom.gif';
    if (obj.style.display == disp) {
      disp = 'block';
      c = 'more_silver_top.gif';
    }
    obj.style.display = disp;
    obj = MM_findObj('RS' + RowIndex);
    obj.src = ROOT_DIR + 'Theme/' + style_dir + '/pics/' + c;
  }
  else {
    alert('Not supportet');
  }
  return false;
}

function menu_show(on){
  if (isOK) {
    var obj = MM_findObj('block_menu');
    if (on == 1) {
      obj.style.display = 'block';
    }
    else {
      obj.style.display = 'none';
    }
  }
  else {
    alert('Not supportet');
  }
  return false;
}

