$(document).ready(function(){
    fillForm();
    $('#flatform').validate();
    $('#houseform').validate(); 
    $('#plotsform').validate();
    $('#premisesform').validate(); 
    $('#quickform').validate();         
    $('#idform').validate();
});

var ajax = new Array();

// Get Townships and Microdistricts
function getTownshipsList(sel)
{
    var municipalityId = sel.options[sel.selectedIndex].value;
    
    document.getElementById('city').options.length = 1;    // Empty township select box
    if(municipalityId.length>0){
        var index = ajax.length;
        ajax[index] = new sack();
        
        ajax[index].requestFile = '/search/townships/id/'+municipalityId+'?estate_type='+$('#estate_type').val();    // Specifying which file to get
        ajax[index].onCompletion = function(){ createTownships(index) };    // Specify function that will be executed after file has been found
        ajax[index].runAJAX();        // Execute AJAX function
    }
}

function createTownships(index)
{
    var obj = document.getElementById('city');
    eval(ajax[index].response);    // Executing the response from Ajax as Javascript code
    
    getMicrodistrictsList(obj);
}

// Get Microdistricts
function getMicrodistrictsList(township_obj){
    
    var townshipId = township_obj.options[township_obj.selectedIndex].value;
    
    document.getElementById('block').options.length = 1;
    if(townshipId.length>0){
        var index = ajax.length;
        ajax[index] = new sack();
        
        ajax[index].requestFile = '/search/microdistricts/id/'+townshipId+'?estate_type='+$('#estate_type').val();    // Specifying which file to get
        ajax[index].onCompletion = function(){ createMicrodistricts(index) };    // Specify function that will be executed after file has been found
        ajax[index].runAJAX();        // Execute AJAX function
    }
}

function createMicrodistricts(index)
{
    if(ajax[index].response == ""){
        document.getElementById('block').length = 1;
        document.getElementById('block').disabled = true;
        
        var township_obj = document.getElementById('city');
        getTownshipStreetsList(township_obj);
    } else {
        document.getElementById('block').disabled = false;
        var obj = document.getElementById('block');
        eval(ajax[index].response);    // Executing the response from Ajax as Javascript code
        
        getMicrodistrictStreetsList(obj);
    }
}

function getTownshipStreetsList(township_obj){
    
    var townshipId = township_obj.options[township_obj.selectedIndex].value;
    
    document.getElementById('street').options.length = 1;    // Empty township select box
    if(townshipId.length>0){
        var index = ajax.length;
        ajax[index] = new sack();
        
        ajax[index].requestFile = '/search/streetst/id/'+townshipId+'?estate_type='+$('#estate_type').val();    // Specifying which file to get
        ajax[index].onCompletion = function(){ createStreets(index) };    // Specify function that will be executed after file has been found
        ajax[index].runAJAX();        // Execute AJAX function
    }
}

function createStreets(index)
{
    if(ajax[index].response == ""){
        document.getElementById('street').length=1;
        document.getElementById('street').disabled = true;
    } else {
        document.getElementById('street').disabled = false;
        var obj = document.getElementById('street');
        eval(ajax[index].response);
    }
}

function getMicrodistrictStreetsList(microdistrict_obj){
    
    var microdistrictId = microdistrict_obj.options[microdistrict_obj.selectedIndex].value;
    
    //document.getElementById('street').options.length = 1;    // Empty township select box
    if(microdistrictId.length>0){
        var index = ajax.length;
        ajax[index] = new sack();
        
        ajax[index].requestFile = '/search/streetsm/id/'+microdistrictId+'?estate_type='+$('#estate_type').val();    // Specifying which file to get
        ajax[index].onCompletion = function(){ createStreets(index) };    // Specify function that will be executed after file has been found
        ajax[index].runAJAX();        // Execute AJAX function
    }
}
function filterByBroker()
{
    
    var loc = location.search.substring(1, location.search.length);
    var test = window.location.href;
    var broker = $('#brokers_select').val(); 
    var newloc = '';
      var params = loc.split("&");
      for (i=0; i<params.length;i++) {
          param_name = params[i].substring(0,params[i].indexOf('='));
            
             if(param_name == 'broker')
             {
                 var value = params[i].substring(params[i].indexOf('=')+1);
                 if(broker != '')
                 {
                    newloc = test.replace('?broker='+value,'')+'?broker='+broker;
                 } else {
                     newloc = test.replace('?broker='+value,'');
                 }
                    break;
             } 
             if(param_name == 'sale_rent') {                                      
                 if(broker != '')
                 {  
                    
                     for (i=0; i<params.length;i++) {
                        
                         param_name = params[i].substring(0,params[i].indexOf('='));
                         
                         if(param_name == 'broker')
                         {
                            var value = params[i].substring(params[i].indexOf('=')+1); 
                            
                            newloc = test.replace('&broker='+value,'')+'&broker='+broker;
                            var br = true;
                            break;  
                         }
                             
                     }
                     if(!br) {
                        newloc = test+'&broker='+broker;      
                     
                     } 
                    
                 } else {
                     newloc = test.replace('&broker='+value,'');
                 }
                 break;   
             } else {
                newloc = test+'?broker='+broker;
             }
                    
      }                         
      window.location = newloc;
}  

function emptyField(el, value)
{
    if (el.value == value)
    {
        el.value = '';
    }
}

function checkField(el, value)
{
    if (el.value == '')
    {
        el.value = value;
    }
}
function clearValues()
{
    
    $('.field').each(function(){
        if($(this).val() == 'iki' || $(this).val() == 'nuo' ) {
            $(this).attr('value','');
        }                                 
    });
    
}
function checkIdValue()
{
    if( $('#add_id').val() == 'ID paieška')
    {
        $('#add_id').attr('value','');
    }    
}
function fillForm() 
{
    $('.from').each(function(){
        if($(this).val() == '' ) {
            $(this).attr('value','nuo');
        }                                 
    });
    
    $('.to').each(function(){
        if($(this).val() == '' ) {
            $(this).attr('value','iki');
        }                                 
    });
    
}


