function searchResultString()
{
    var ad_category = 1;

    var type_code = "1,2";
                
    // Property Type
    //var property_type   = "31,32";
                
    // Price
    var price           = "";
    var price_period    = "";
                
    var free_text = document.this_page.free_text.value;
    free_text     = escape(format_free_text(free_text));
                
    // Build Search Parameters
    var search_params  = new Array();
    search_params[search_params.length] = "search_type=property";
    search_params[search_params.length] = "cat=" + ad_category;
    //search_params[search_params.length] = "type=" + property_type;
    search_params[search_params.length] = "free_text=" + free_text;
    search_params[search_params.length] = "type_code=" + type_code;
            
    str_params = search_params.join("&");

    str_params = "search-result.asp?" + str_params;

    return str_params;

}


function Enter_search()
{ 

    var str_params = searchResultString();

    document.this_page.action = str_params;
    document.this_page.submit();
}

function search_sale()
{
    var str_params = searchResultString();
    
    relocate(str_params, "self");
}

function search_rent()
{
    // Buy
    var ad_category = 2;

    var type_code = "1,2";
                
    // Property Type
    //var property_type   = "31,32";
                
    // Price
    var price           = "";
    var price_period    = "";
                
    var free_text = document.this_page.free_text.value;
    free_text     = escape(format_free_text(free_text));
                
                
    // Build Search Parameters
    var search_params  = new Array();
    search_params[search_params.length] = "search_type=property";
    search_params[search_params.length] = "cat=" + ad_category;
    //search_params[search_params.length] = "type=" + property_type;
    search_params[search_params.length] = "free_text=" + free_text;
    search_params[search_params.length] = "type_code=" + type_code;
                
    str_params = search_params.join("&");

    relocate("search-result.asp?" + str_params, "self");
}

            
function relocate(url, target)
{
    eval(format_str("%s.location.href=url", target));
    return false;
}

            
function format_free_text(free_text)
{
    if (free_text != "" && typeof free_text != "undefined")
    {
    // Convert + to &plus;
        var re = new RegExp("\[+\]", "gi");
        free_text = free_text.replace(re, "&plus;");
    
        // Convert " to &quot;
        var re = new RegExp('\["\]', "gi");
        free_text = free_text.replace(re, "&quot;");
        
        // Convert ' to &quot;
        var re = new RegExp("\['\]", "gi");
        free_text = free_text.replace(re, "&rsquo;");
    }

    return free_text;
}

function show_navigation_links(id, from_row, to_row, rowcount, ads_on_list, page_index, search_params)
{
    // typeCast parameters
    from_row    = parseInt(from_row);
    to_row      = parseInt(to_row);
    rowcount    = parseInt(rowcount);
    ads_on_list = parseInt(ads_on_list);
    page_index  = parseInt(page_index);

    var rowcount_to_show = to_row - from_row + 1;

    if (id == 1)
    {
        var info_string = "";

        if (0 < rowcount)
        {
            info_string = "<p>Your search returned " + String(rowcount) + " propert" + (rowcount_to_show == 1 ? "y" : "ies") + ".</p>";
            info_string += "<p id='nav_info'>Showing <strong>" + String(from_row) + "-" + String(to_row) + "</strong> of <strong>" + String(rowcount) + "</strong></p>";
        }
        else
        {
            info_string = "<p><b>We have found 0 properties</b> for your selected criteria.</p>";
            info_string += "<p id='nav_info'></p>";
        }

        try
        {
            document.getElementById("search_result_info").innerHTML = info_string;
        }
        catch(e)
        {
        }
    }

    if (from_row > 0 && to_row > 0)
    {
        // Remove list page from search_params
        var re = new RegExp();
        re = /&list_page=(\d+)/g;
        search_params = String(search_params).replace(re, "");
        
        // Keep original list_index
        var list_page = page_index;
        
        // Get page to relocate to in list navigation
        var page_href = "search-result.asp?" + search_params;
        
        // Get  first page index
        var first_page = 0;
        
        // Get last page index
        var last_page = parseInt(rowcount/ads_on_list) - 1;
        if (parseInt(rowcount%ads_on_list)/ads_on_list > 0) { last_page++; }
        
        // Get prev page
        var prev_index = ( parseInt(page_index)-1 >= first_page )? parseInt(page_index)-1 : first_page;
        
        // Get next page
        var next_index = ( parseInt(page_index)+1 <= last_page )?  parseInt(page_index)+1 : last_page;
        
        var selected_nav_to = "";
        var nav_to_index = new Array();
        var nav_to_pages = new Array();
        
        for (var i=0; i < 5; i++)
        {
            // Build link from/to
            var list_index = page_index - (page_index % 5);
            var link_from   = (parseInt(list_index) * parseInt(ads_on_list)) + parseInt((i * ads_on_list)) + 1;
            var link_to     = (parseInt(link_from)) + parseInt(ads_on_list) - 1;
            var nav_index   = parseInt(page_index) + i;
            
            // Set Max link_to
            if (link_to > rowcount)
            {
                link_to = rowcount;
            }
            
            // Get selected nav_to
            if ( (link_from == from_row) && (link_to == to_row) )
            {
                selected_nav_to = i;
            }
            
            // Set Max link_from
            if ( (link_from > rowcount) || (link_to < ads_on_list) )
            {
                // Clear nav_to links
                nav_to_index[i] = "";
                nav_to_pages[i] = "";
            }
            else
            {
                // Build nav_to links
                nav_to_index[i] = parseInt(list_index) + i;
                nav_to_pages[i] = nav_to_index[i] + 1;
            }
        }
        
        // Display Navigation HTML
        var html_output = "";

        if (prev_index < page_index)
        {
            html_output += "<p class='p_page'><strong><a href='" + page_href + "&list_page=" + prev_index + "'><< Prev</a></strong></p>";
        }
        // ----------------------------------
        if (nav_to_pages[0] != "")
        {
            html_output += "<ul class='page'>";
            for (var i = 0; i < 5; i++)
            {
                if (nav_to_pages[i] != "")
                {
                    var selected = (page_index == nav_to_pages[i] - 1 ? ' class="selected"' : '');
                    html_output += "<li"+ selected + "><a href='" + page_href + "&list_page=" + nav_to_index[i] + "'>" + nav_to_pages[i] + "</a></li>";
                }
            }
            html_output += "</ul>";
        }
        // ----------------------------------
        if (page_index < next_index)
        {
            html_output += "<p class='n_page'><strong><a href='" + page_href + "&list_page=" + next_index + "'>Next >></a></strong></p>";
        }
            
        if (id == 1)
        {
            document.getElementById("navigation_section_top").innerHTML     = html_output;
        }
    }
}


function validate_not_blank(field, name)
{
    // strip all spaces at start and end of line
    var regx_output = new String(field.value);
    var re1 = new RegExp("^[ ]*|([ ]*)\$", 'g');
    field.value = regx_output.replace(re1, "");
    
    if (field.value == "")
    {
        alert(format_str('The "%s" field cannot be blank.', name));
        focus_field(field);
        return false;
    }
    return true;
}





function validate_email_address(field, name, allow_name_in_address, display_error)
{
    if (typeof display_error == "undefined")
    {
        display_error = true;
    }
    
    // Get field value
    var fieldval = field.value;
    
    // Remove spaces before and after email
    fieldval = fieldval.replace(/^\\s*/, "");
    fieldval = fieldval.replace(/\\s*\$/, "");
    
    // Update field
    field.value = fieldval;
    
    // If "name <email>" format is available - get email address
    if (allow_name_in_address)
    {
        var a = fieldval.indexOf("<");
        var b = fieldval.indexOf(">");
        if (a < b)
        {
            fieldval = fieldval.substring(a + 1, b);
        }
    }
    
    // Initialize
    var msg     = "";
    var last_at = fieldval.lastIndexOf("\@");
    var before  = fieldval.substr(0, last_at);
    var after   = fieldval.substr(last_at + 1, fieldval.length - last_at - 1);
    var reg;
    
    // CHECK: the @ character must exist
    if (last_at < 0)
    {
        msg = "There is no @ in your e-mail address.";
    }
    
    // CHECK: there must be at least one character preceeding and one character following @
    if (!msg)
    {
        if ((before.length < 1) || (after.length < 1))
        {
            msg = "Your e-mail address must contain at least 1 character in front of @.";
        }
    }
    
    // CHECK: characters below 32 are illegal
    if (!msg)
    {
        for (var i = 0; i < fieldval.length; i++)
        {
            var code = fieldval.charCodeAt(i);
            if (code < 32)
            {
                msg = "You have used an invalid character in your email address.";
                break;
            }
        }
    }
    
    // CHECK: the ".@" combination is illegal
    if (!msg)
    {
        if (before.substr(before.length - 1, 1) == ".")
        {
            msg = "Du har skrevet et ulovligt tegn før @.";
        }
    }
    
    // CHECK: the "@." combination is illegal
    if (!msg)
    {
        if (after.substr(0, 1) == ".")
        {
            msg = "A full stop cannot be the first character after @.";
        }
    }
    
    // CHECK: at least one "." is required in the host section
    if (!msg)
    {
        if (after.indexOf(".") < 0)
        {
            msg = "There is no full stop in your e-mail address.";
        }
    }
    
    // CHECK: "." cannot be the last character
    if (!msg)
    {
        if (after.lastIndexOf(".") == (after.length - 1))
        {
            msg = "A full stop cannot be the last character in an e-mail address.";
        }
    }
    
    // CHECK: the "-." combination is illegal following @
    if (!msg)
    {
        reg = /\\-{1}\\.{1}/;
        if (reg.test(after))
        {
            msg = "A dash  (-) cannot be placed in front of a full stop (.)";
        }
    }
    
    // CHECK: "-" cannot be the last character
    if (!msg)
    {
        if (after.lastIndexOf("-") == (after.length - 1))
        {
            msg = "A dash (-) cannot be the last character in an e-mail address.";
        }
    }
    
    // CHECK: certain characters preceeding @ must be escaped
    if (!msg)
    {
        // 1) remove all escaped characters
        // 2) all remaining special characters were not escaped - which is illegal
        before = before.replace(/\\\\{1}.{1}/g, "");
        reg = /[\\s<>()\\[\\]\\\\,;:@\\"]{1,}/;
        if (reg.test(before))
        {
            msg = "You have used a character that cannot be used in front of the @.";
        }
    }
    
    // CHECK: the characters following @ are restricted
    // ASCII Characters below 32, not allowed
    if (!msg)
    {
        reg = new RegExp("^[\\x00-\\x20]*\$");
        if (reg.test(after))
        {
            msg = "You have used a character that cannot be used after the @.";
        }
    }
    
    // If the address is illegal
    if ((msg) && (display_error = true))
    {
        alert(format_str("The e-mail address '%s' is incorrect:\n%s\n\nE-mail addresses are usually formatted in the following way:\n - name.surname@company.co.uk\n - username@internetserviceprovider.com\n - initials@serviceprovider.com\n   etc.", name, msg));
        focus_field(field);
        return false;
    }
    
    // The address seems to be in order
    return true;
}

function show_images(property_id, index)
{
    var top = 50;
    var left = 50;
    var width = 700;
    var height = 500;
    window.open("show-images.asp?property_id=" + property_id + "&index=" + index, "", "toolbar=no,directories=no,menubar=no,scrollbars=yes,status=no,resizable=1,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height);
}

function show_contact_agent(search_criteria)
{
    var width = 780;
    var height = 590;
    var top = 50;
    var left = 50;
    window.open("contact-estate-agent.asp?"+search_criteria, "", "toolbar=no,directories=no,menubar=no,scrollbars=yes,status=yes,resizable=1,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height);
}

function show_properties_by_email(search_criteria)
{
    var width = 780;
    var height = 510;
    var top = 50;
    var left = 50;
    window.open("property-mail-agent.asp?"+search_criteria, "", "toolbar=no,directories=no,menubar=no,scrollbars=yes,status=yes,resizable=1,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height);
}


function search_letting_property()
{
    var obj_res = getQueryString();
    if (obj_res.result)
    {
        relocate("search-result.asp?" + obj_res.search_params, "self");
    }
}

function search_sale_property()
{
    var obj_res = getQueryString();
    if (obj_res.result)
    {
        relocate("search-result.asp?" + obj_res.search_params, "self");
    }
}

function save_property_mail_agent()
{
    var include_mail_agent_params = 1;
    var obj_res = getQueryString(include_mail_agent_params);
    if (obj_res.result)
    {
        var objFrame = document.getElementById("iframe_save_property_mail_agent");

        var url = objFrame.contentWindow.document.getElementById("url");
        url.value = "save-property-mail-agent.asp?" + obj_res.search_params;

        objFrame.contentWindow.save_mail_agent();
    }
}

function showMessage(msg)
{
    alert(msg);
}

sas_tmstp=Math.round(Math.random()*10000000000);sas_masterflag=1;
function SmartAdServer(sas_pageid,sas_formatid,sas_target) {
 if (sas_masterflag==1) {sas_masterflag=0;sas_master='M';} else {sas_master='S';};
 document.write('<scr'+'ipt src="http://ads.maroonspider.com/call/pubj/' + sas_pageid + '/' + sas_formatid + '/' + sas_master + '/' + sas_tmstp + '/' + escape(sas_target) + '?"></scr'+'ipt>');
}