$(document).ready(function() {
    $('.datepicker').datepicker({ dateFormat: 'dd.mm.yy' });
    
    /** Add a trigger to reload the town listing if the user selects a region from the dropdown **/
    /**
    $("select#listingregionid").change(function(){
        $.getJSON("/openbritain.net/ajax/propertysearchfields.php",{id: $(this).val(), region: 'true', ajax: 'true'}, function(j){
            var options = '<option value="">Any town</option>';
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
            }
            $("select#addressline3").html(options);
        })
    });
    **/
    
    /** This enables the 'show/hide' advanced search options functionality **/
    $('#moreoptions').click(function() {
        $('#propsearchadv').slideToggle('slow');
        if($(this).html()=="More options...") {
            $('#moreoptions').html('Less options...');
        } else {
            $('#moreoptions').html('More options...');
        }
    });
    
    /** AJAX-ify the search form **/
    var options = { 
        target:        '#propertysearchresults',
        success:       showMainSearchResponse
    };
    
    $('#propsimplesearch').submit(function() { 
        $(this).ajaxSubmit(options);
        return false; 
    });
    
    /** Hide the search form if we're on the search results page **/
    if(POSTED==1) {
        //alert("yay");
        $('#searchform').css('visibility','hidden');
        $('#propsimplesearch').submit();
    } 
    
}); 

/** Load the search results into the results div **/
function showMainSearchResponse(responseText, statusText)  {
    $('#propertysearchresults').css('display','block');
    $('#searchform').css('display','none');
    $('#propertysearchresults').html(responseText);
    window.scroll(0,0);
}

