function getSelectedText() { 
        if (window.getSelection) { 
                return window.getSelection().toString(); 
        } 
        else if (document.getSelection) { 
                return document.getSelection(); 
        } 
        else if (document.selection) { 
                // this is specifically for IE 
                return document.selection.createRange().text; 
        } 
}

function go(property_id) {
    window.location = '/'+lang+ "/property/"+property_id;
}

function plus_over(obj) {
    $(obj).find("img").attr("src",'/media/plus_red.gif');
};

function plus_out(obj) {
    $(obj).find("img").attr("src",'/media/plus_grey.gif');
};

function att_over(obj) {
    $(obj).find("img").attr("src",'/media/plus_attention.gif');
};

function att_out(obj) {
    $(obj).find("img").attr("src",'/media/plus_attention_grey.gif');
};

function report_error() {

    var text = err1;
    var popup_title = '<strong>'+popup_error_text+'</strong>';
    $('#popup-question').find('.popup-header').html(popup_title);
    
    show_popup('#popup-question', text, function(){ /* callback function */

        $.post("/en/report/", {  
                text: text, 
                page: document.location.href,
                message: $('textarea#id_error_message').val() 
            },
            function(data){
                $('.finish-tip').show();
                $(this).oneTime(1000, function() {
                  $.modal.close();
                  $('.finish-tip').hide();
                });
            });
        
        });
    $('textarea#id_error_message').focus();


};

function report_violation() {

    var text = err2;
    var popup_title = '<strong>'+popup_violation_text+'</strong>';
    $('#popup-question').find('.popup-header').html(popup_title);
    
    show_popup('#popup-question', text, function(){ /* callback function */

        $.post("/en/report/", {  
                text: text, 
                usernamex: username,
                useremailx: useremail,
                page: document.location.href,
                message: $('textarea#id_error_message').val(),
                type: 'violation' 
            },
            function(data){
                $('.finish-tip').show();
                $(this).oneTime(1000, function() {
                  $.modal.close();
                  $('.finish-tip').hide();
                });
            });
        
        });
    $('textarea#id_error_message').focus();
    

};

function contact_owner() {

    if (ownerlanguage==''){
        var text = ownernativelangname; 
        }
    else {
        var text = ownerlanguage;
    }
    var popup_title = '<strong>'+popup_contact_text+'</strong>';
    $('#popup-question-owner').find('.popup-header').html(popup_title);
    
    show_popup('#popup-question-owner', text, function(){ /* callback function */

        if ($('input#captchaid').val() != "12WX12"){
           exit;
        }
        $.post("/en/contact_owner/", {  
                usernamex: userfullname,
                captcha: $('input#captchaid').val(),
                useremailx: useremail,
                userphone: userphone,
                usercellphone: usercellphone,
                useremailowner: useremailowner,
                ownernativelang: ownernativelang,
                page: document.location.href,
                message: $('textarea#id_error_message-owner').val(),
                type: 'contact_owner' 
            },
            function(data){
                $('.finish-tip').show();
                $(this).oneTime(1000, function() {
                  $.modal.close();
                  $('.finish-tip').hide();
                });
            });
        
        });
    $('textarea#id_error_message-owner').focus();

};

function contact_agent() {

    if (ownerlanguage==''){
        var text = ownernativelangname; 
        }
    else {
        var text = ownerlanguage ;
    }
    var popup_title = '<strong>'+popup_contact_text+'</strong>';
    $('#popup-question-owner').find('.popup-header').html(popup_title);
    
    show_popup('#popup-question-owner', text, function(){ /* callback function */

        $.post("/en/contact_owner/", {  
                usernamex: userfullname,
                useremailx: useremail,
                userphone: userphone,
                usercellphone: usercellphone,
                useremailagent: useremailagent,
                ownernativelang: ownernativelang,
                page: document.location.href,
                message: $('textarea#id_error_message-owner').val(),
                type: 'contact_agent' 
            },
            function(data){
                $('.finish-tip').show();
                $(this).oneTime(1000, function() {
                  $.modal.close();
                  $('.finish-tip').hide();
                });
            });
        
        });
    $('textarea#id_error_message-owner').focus();
    

};

function plus_click(obj, property_id) {
    $.get(obj.href, function(data){
      if (data=='1'){
        show_popup2('#popup-favs','', function(){ /* callback function */
            });
      }
    });    
    $(obj).find("img").hide();
    return false;
};


function confirm_delete(property_id, obj)
{
  $('#popup-confirm-delete').find('.agree-button input').unbind("click").click(function(){
      $.get(obj.href);
      $.modal.close();
      $('#myrow_'+property_id).fadeOut("slow");
  } );
  $('#popup-confirm-delete').modal( {close : false, maxHeight:400, maxWidth:600, overlayClose:true} );
}

$(document).ready(function(){ 

  if ( $("select#id_type1").val() == 1 || $("select#id_type1").val() == 3 || $("select#id_type1").val() == 24 || $("select#id_type1").val() == 25 || $("select#id_type1").val() == 26 || $("select#id_type1").val() == 27 || $("select#id_type1").val() == 67) {
    $("select#id_type2").parent().parent().show();
  }

  $("select#id_type1").change(function(){
    
    if ( $("select#id_type1").val() == 1 || $("select#id_type1").val() == 3 || $("select#id_type1").val() == 24 || $("select#id_type1").val() == 25 || $("select#id_type1").val() == 26 || $("select#id_type1").val() == 27 || $("select#id_type1").val() == 67 ) {
      $("select#id_type2").parent().parent().show();
      if ( $(this).val() ) {
          $("select#id_type2").html('<option value="" selected="selected">...loading</option>');
          $.getJSON('/'+lang+"/catalog/subtype/",{id: $(this).val(), ajax: 'true'}, function(j){
            var options = '<option value="" selected="selected">---------</option>';
            for (var i = 0; i < j.length; i++) {
              options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
            }
            $("select#id_type2").html(options);
          })
      } else {
      }
      
    } else {
      var options = '<option value="" selected="selected">---------</option>';
      $("select#id_type2").html(options);
      $("select#id_type2").parent().parent().hide();
    }
 

  })

   

$("#from_price, #to_price, #from_area, #to_area").focus(function() {
	if( this.value == this.defaultValue ) {
		this.value = "";
	}
}).blur(function() {
	if( !this.value.length ) {
		this.value = this.defaultValue;
	}
});

    
    if ($("select#id_offer_type").val() == '') {
            $("#id_rent_price_per_day").parent().parent().hide();
            $("#id_rent_price_per_day").parent().parent().next('tr').hide();
            
            $("#id_rent_price_per_week").parent().parent().hide();
            $("#id_rent_price_per_month").parent().parent().hide();
            $("#id_rent_price_per_year").parent().parent().hide();
            $("#id_sale_price").parent().parent().hide();
    }
    
    
    if ($("select#id_offer_type").val() == 1) {
            $("#id_rent_price_per_day").parent().parent().hide();
            $("#id_rent_price_per_day").parent().parent().next('tr').hide();
            
            $("#id_rent_price_per_week").parent().parent().hide();
            $("#id_rent_price_per_month").parent().parent().hide();
            $("#id_rent_price_per_year").parent().parent().hide();
    }
 
    if ($("select#id_offer_type").val() == 0) {
            $("#id_sale_price").parent().parent().hide();
    }
 
    
    $("select#id_offer_type").change(function() 
    { 
        var offer_type = $(this).val();
        
        //alert(offer_type);
        if (offer_type == 1) {
            $("#id_sale_price").parent().parent().show();
            $("#id_rent_price_per_day").parent().parent().hide();
            $("#id_rent_price_per_day").parent().parent().next('tr').hide();
            
            $("#id_rent_price_per_week").parent().parent().hide();
            $("#id_rent_price_per_month").parent().parent().hide();
            $("#id_rent_price_per_year").parent().parent().hide();
        } else if (offer_type == '') {
            $("#id_rent_price_per_day").parent().parent().hide();
            $("#id_rent_price_per_day").parent().parent().next('tr').hide();
            
            $("#id_rent_price_per_week").parent().parent().hide();
            $("#id_rent_price_per_month").parent().parent().hide();
            $("#id_rent_price_per_year").parent().parent().hide();
            $("#id_sale_price").parent().parent().hide();        
        } else if (offer_type == 0) {
            $("#id_rent_price_per_day").parent().parent().show();
            $("#id_rent_price_per_day").parent().parent().next('tr').show();
            
            $("#id_rent_price_per_week").parent().parent().show();
            $("#id_rent_price_per_month").parent().parent().show();
            $("#id_rent_price_per_year").parent().parent().show();
            $("#id_sale_price").parent().parent().hide();
        }

    });
    


  /* Quick search */
  $("select#id_qsearch-country").change(function(){
    if ( $(this).val() ) {
        var ac = $("#id_custom_city")[0].autocompleter;
        ac.setExtraParams( { country: $("select#id_qsearch-country").val() } );
        ac.flushCache();
        $("#id_custom_city").val('');
        $('#custom_city_block').hide();

        $("select#id_qsearch-region").html('<option value="" selected="selected">...loading</option>');
        $("select#id_qsearch-city").html('<option value="" selected="selected">...loading</option>');
        $.getJSON("/catalog/qsearch_both/",{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '<option value="" selected="selected">---------</option>';
          for (var i = 0; i < j[0].length; i++) {
            options += '<option value="' + j[0][i].optionValue + '">' + j[0][i].optionDisplay + '</option>';
          }
          $("select#id_qsearch-region").html(options);
          
          var options = '<option value="" selected="selected">---------</option>';
          for (var i = 0; i < j[1].length; i++) {
            options += '<option value="' + j[1][i].optionValue + '">' + j[1][i].optionDisplay + '</option>';
          }
          options += '<option value="-1">'+other+'</option>';
          $("select#id_qsearch-city").html(options);          
          
        });
    
    }
  })
  

  $("select#id_qsearch-region").change(function(){
    if ( $(this).val() ) {
        var ac = $("#id_custom_city")[0].autocompleter;
        ac.setExtraParams( { 
            country: $("select#id_qsearch-country").val(),
            region: $("select#id_qsearch-region").val()
         } );
        ac.flushCache();
    
        $("select#id_qsearch-city").html('<option value="" selected="selected">...loading</option>');
        $.getJSON("/catalog/qsearch/",{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '<option value="" selected="selected">---------</option>';
          for (var i = 0; i < j.length; i++) {
            options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
          }
          options += '<option value="-1">'+other+'</option>';
          $("select#id_qsearch-city").html(options);
        })
    } else {
        var ac = $("#id_custom_city")[0].autocompleter;
        ac.setExtraParams( { 
            country: $("select#id_qsearch-country").val()
         } );
        ac.flushCache();    
    }
  })
  
  $("select#id_qsearch-city").change(function(){
    if ( $(this).val() ) {      
        if($(this).val() ==-1){
            $('#id_custom_city').val('');
            $('#custom_city_block').show();
            $('#id_custom_city').focus();
        } else {
            $('#custom_city_block').hide();
        }
        $.getJSON("/catalog/get_region/",{id: $(this).val(), ajax: 'true'}, function(j){
            $('select#id_qsearch-region').val(j.region);
        });
    }
  });  
  /* end of Quick search */


  $("select#id_country").change(function(){
    if ( $(this).val() ) {
        var ac = $("#id_custom_city2")[0].autocompleter;
        ac.setExtraParams( { country: $("select#id_country").val() } );
        ac.flushCache();
        $("#id_custom_city2").val('');
        $('#id_custom_city2').hide();

        $("select#id_region").html('<option value="" selected="selected">...loading</option>');
        $("select#id_city").html('<option value="" selected="selected">...loading</option>');
        $.getJSON("/catalog/qsearch_both/",{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '<option value="" selected="selected">---------</option>';
          for (var i = 0; i < j[0].length; i++) {
            options += '<option value="' + j[0][i].optionValue + '">' + j[0][i].optionDisplay + '</option>';
          }
          $("select#id_region").html(options);
          
          var options = '<option value="" selected="selected">---------</option>';
          for (var i = 0; i < j[1].length; i++) {
            options += '<option value="' + j[1][i].optionValue + '">' + j[1][i].optionDisplay + '</option>';
          }
          options += '<option value="-1">'+other+'</option>';
          $("select#id_city").html(options);          
          
        });
    
    }
  })
  

  $("select#id_region").change(function(){
    if ( $(this).val() ) {
        var ac = $("#id_custom_city2")[0].autocompleter;
        ac.setExtraParams( { 
            country: $("select#id_country").val(),
            region: $("select#id_region").val()
         } );
        ac.flushCache();
    
        $("select#id_city").html('<option value="" selected="selected">...loading</option>');
        $.getJSON("/catalog/qsearch/",{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '<option value="" selected="selected">---------</option>';
          for (var i = 0; i < j.length; i++) {
            options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
          }
          options += '<option value="-1">'+other+'</option>';
          $("select#id_city").html(options);
        })
    } else {
        var ac = $("#id_custom_city2")[0].autocompleter;
        ac.setExtraParams( { 
            country: $("select#id_country").val()
         } );
        ac.flushCache();    
    }
  })

  $("select#id_city").change(function(){
    if ( $(this).val() ) {      
        if($(this).val() ==-1){
            $('#id_custom_city2').val('');
            $('#id_custom_city2').show();
            $('#id_custom_city2').focus();
        } else {
            $('#id_custom_city2').hide();
        }
        $.getJSON("/catalog/get_region/",{id: $(this).val(), ajax: 'true'}, function(j){
            $('select#id_region').val(j.region);
        });
    }
  });  

  $("#add_language_select").click(function(){
      $("#id_languages").clone(true).insertAfter(this);
      return false;
  });





$("div.scrollable").scrollable({
clickable: false,
prevPage: '#prevPage', 
nextPage: '#nextPage',
size: 31,
keyboard: false
});

$("#prevPage, #nextPage").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
    
    
$('input#from_price, input#to_price, input#from_area, input#to_area, input#id_area, input#id_sale_price, input#id_rent_price_per_day, input#id_rent_price_per_week, input#id_rent_price_per_month, input#id_rent_price_per_year').bind('keypress', function(e) {
//alert(e.which);
 	return ( e.which!=13 && e.which!=46 && e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
})    

/* int dynamic attrs */
/* see list at http://rew1.com/en/test/numeric/ */
$('input#attr_9, input#attr_11, input#attr_15, input#attr_16, input#attr_18, input#attr_19, input#attr_22, input#attr_26, input#attr_38, input#attr_41, input#attr_42, input#attr_49, input#attr_51, input#attr_52, input#attr_61, input#attr_62, input#attr_65, input#attr_67, input#attr_76, input#attr_77, input#attr_78, input#attr_79, input#attr_80, input#attr_81, input#attr_82, input#attr_83, input#attr_84, input#attr_91, input#attr_92, input#attr_95, input#attr_99, input#attr_100, input#attr_111, input#attr_112, input#attr_115, input#attr_116, input#attr_117, input#attr_126, input#attr_134, input#attr_136, input#attr_144, input#attr_145, input#attr_146, input#attr_148, input#attr_157, input#attr_158').bind('keypress', function(e) { 
	return ( e.which!=13 && e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
})    

$('input#id_map_zoom, input#id_apartment_number').bind('keypress', function(e) { 
	return ( e.which!=13 && e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
})    


$('input#id_postal_code').bind('keypress', function(e) {
	return ( e.which!=45 && e.which!=13 && e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
})    


/* multi int dynamic attrs */
/* see list at http://rew1.com/en/test/numeric/ */
$('input#attr_17').bind('keypress', function(e) { 
	return ( e.which!=13 && e.which!=44 && e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
})  

/* float dynamic attrs */
/* see list at http://rew1.com/en/test/numeric/ */
$('input#attr_20, input#attr_58, input#attr_69, input#attr_118, input#attr_121, input#attr_122, input#attr_125, input#attr_131, input#attr_161').bind('keypress', function(e) { 
	return ( e.which!=13 && e.which!=46 && e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
}) 




/* phone fields */
/* see list at http://rew1.com/en/test/numeric/ */
/*
$('input#id_cell_phone_number, input#id_phone_number').bind('keypress', function(e) { 
  alert(e.which);
	return ( e.which!=32 && e.which!=45 && e.which!=41 && e.which!=40 &&   e.which!=13 && e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
}) 
*/

$(document).keypress(function(e){

    if (((e.keyCode == 13) || (e.keyCode == 10)) && (e.ctrlKey == true)) {
        text = getSelectedText();
        usernamex = username;
        useremailx = useremail;
        if (text.length > 0) {
        //alert( text );
        var popup_title = '<strong>'+popup_error_text+'</strong>';
        $('#popup-question').find('.popup-header').html(popup_title);

        show_popup('#popup-question', text, function(){ /* callback function */

            $.post("/en/report/", {  
                    text: text,
                    usernamex: usernamex,
                    useremailx: useremailx,
                    page: document.location.href,
                    message: $('textarea#id_error_message').val() 
                },
                function(data){
                    $('.finish-tip').show();
                    $(this).oneTime(1000, function() {
                      $.modal.close();
                      $('.finish-tip').hide();
                    });
                });
            
            });
        $('textarea#id_error_message').focus();
        
        }        
    };
});




    $("input#search-type-2").click(function() 
    { 
        $("#rent_price_type").show();
        $("#rent_price_type_pusher").show();

    });

    $("input#search-type-1").click(function() 
    { 
        $("#rent_price_type").hide();
        $("#rent_price_type_pusher").hide();

    });



    $("form#register_form").submit(function() 
    { 
      if($("input#terms").is(':checked')){
      } else {
           
        show_popup2('#popup-terms','', function(){ /* callback function */
          
            });

          return false;      
      }
    });


      $("#id_custom_city").autocomplete("/catalog/ajax_suggest/", 
        {
        delay:10,
        minChars:3,
        matchSubset:1,
        autoFill:false,
        maxItemsToShow:10,
        lineSeparator: '\n',
        cellSeparator: '|',
        onItemSelect:selectItem,
        formatItem: formatItem,
        extraParams: { 
          country: $("select#id_qsearch-t_country").val() 
          }
        }
      );
      
      $("#id_custom_city2").autocomplete("/catalog/ajax_suggest/", 
        {
        delay:10,
        minChars:3,
        matchSubset:1,
        autoFill:false,
        maxItemsToShow:10,
        lineSeparator: '\n',
        cellSeparator: '|',
        onItemSelect:selectItem2,
        formatItem: formatItem,
        extraParams: { 
          country: $("select#id_t_country").val() 
          }
        }
      );      




function selectItem(li) {
    $('select#id_qsearch-t_region').val( li.extra[2] );
    $("select#id_qsearch-t_city").html('<option value="" selected="selected">...loading</option>');
    $.getJSON("/catalog/tqs_get_cities/",{id: li.extra[2], ajax: 'true', required_city: li.extra[1], lang: lang}, function(j){
      var options = '<option value="" selected="selected">---------</option>';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      options += '<option value="-1">'+other+'</option>';
      $("select#id_qsearch-t_city").html(options);
      $("select#id_qsearch-t_city").val(li.extra[1]);
      
      var ac = $("#id_custom_city")[0].autocompleter;
      ac.setExtraParams( { 
          country: $("select#id_qsearch-t_country").val(),
          region: $("select#id_qsearch-t_region").val()
       } );
      ac.flushCache();
      
    })
}

function selectItem2(li) {
    $('select#id_t_region').val( li.extra[2] );
    $("select#id_t_city").html('<option value="" selected="selected">...loading</option>');
    $.getJSON("/catalog/tqs_get_cities/",{id: li.extra[2], ajax: 'true', required_city: li.extra[1], lang: lang}, function(j){
      var options = '<option value="" selected="selected">---------</option>';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      options += '<option value="-1">'+other+'</option>';
      $("select#id_t_city").html(options);
      $("select#id_t_city").val(li.extra[1]);
      
      var ac = $("#id_custom_city2")[0].autocompleter;
      ac.setExtraParams( { 
          country: $("select#id_t_country").val(),
          region: $("select#id_t_region").val()
       } );
      ac.flushCache();
      
    })
}



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



$("input#id_area, input#id_sale_price").blur( function() {
  var area = $("input#id_area").val();
  var price = $("input#id_sale_price").val();
  if (area > 0 && price > 0) {
  $('input#id_square_meter_price').val( price/area );
  }  
});


    $(".expandable").click(function() 
    { 
        $(this).hide();
        $(this).next("fieldset").slideDown("slow");
        return false;
    });

    
}); 

