// PARTNER SEARCH WIDGET
// Location field hint variables
var primelocation_hint_1 = 'Enter place name or area';
var primelocation_hint_2 = 'Enter street or postcode';
var primelocation_example_1 = 'e.g. Oxford or NW3';
var primelocation_example_2 = 'e.g. Acacia Avenue or TW19 5NW';

function add_tracking(eventid, eventtype, query) {

    var oImg=document.createElement("img");
        oImg.setAttribute('src', 'http://beacon.krxd.net/event.gif?event_id=' + eventid + '&event_type=' + eventtype + '&pub_id=73d63640-00aa-4b5a-8fd2-7d80afb25a19' + ( query || "" ) );
        oImg.setAttribute('height', '0');
        oImg.setAttribute('width', '0');
        document.getElementById('primelocation_search').appendChild(oImg);

}

// Location hint change function
function primelocation_change_hint(hint, example) {

    if ((document.getElementById('primelocation_location').value == primelocation_hint_1) || (document.getElementById('primelocation_location').value == primelocation_hint_2) || (document.getElementById('primelocation_location').value === '')) {

        document.getElementById('primelocation_location').value = hint;
        document.getElementById('primelocation_example').innerHTML = example;
        document.getElementById('primelocation_location').style.color = '#999999';

    }

}

// Location field onkeydown function
function primelocation_location_function(el) {

    if ((document.getElementById('primelocation_location').value == primelocation_hint_1) || (document.getElementById('primelocation_location').value == primelocation_hint_2)) {

        document.getElementById('primelocation_location').value = '';
        document.getElementById('primelocation_location').style.color = '#000000';

    }

}

function primelocation_location_field(el) {

    if (el) {
        var query = "&location=" + el.value,
            eventid,
            section = document.getElementById('primelocation_section_type');

        if ( section.value == 'for-sale' )     eventid = 'Ig8d00FS';
        if ( section.value == 'to-rent' )      eventid = 'Ig8h1r2C';
        if ( section.value == 'house-prices' ) eventid = 'Ig8oBJzd';
        if ( section.value == 'new-homes-bar' )    eventid = 'IpJj6rV9';

        add_tracking(eventid, 'fd', query);
    }

}

function primelocation_price_change(el) {

    if ( el.value != '' ) {
        var query = '&' + ( el.name == "price_min" ? "min_price" : "max_price" ) + '=' + el.value,
            eventid,
            section = document.getElementById('primelocation_section_type');

        if ( section.value == 'for-sale' )    eventid = el.name == "price_min" ? "Ig8b5jXS" : "Ig8coY33";
        if ( section.value == 'to-rent' )     eventid = el.name == "price_min" ? "Ig8gABYS" : "Ig8gnxJI";
        if ( section.value == 'new-homes-bar' )   eventid = el.name == "price_min" ? "IpJkOrff" : "IpJkdvOj";

        add_tracking(eventid, 'cact', query);
    }

}

function  primelocation_submit_button() {

    var query,
        eventid,
        section = document.getElementById('primelocation_section_type');

    if ( section.value == 'for-sale' )     eventid = 'Ig8fPUbt';
    if ( section.value == 'to-rent' )      eventid = 'Ig8iwQDt';
    if ( section.value == 'house-prices' ) eventid = 'Ig8m4Aeo';
    if ( section.value == 'new-homes-bar' )    eventid = 'IpJlB16n';

    if ( section.value == 'house-prices' ) {
        query = '&location=' + document.getElementById('primelocation_location').value;
        add_tracking(eventid, 'clk', query);
        return;
    }

    query = '&min_price=' + document.getElementById('primelocation_sale_price_min').value +
            '&max_price=' + document.getElementById('primelocation_sale_price_max').value +
            '&location='  + document.getElementById('primelocation_location').value;

    add_tracking(eventid, 'clk', query);

}

function primelocation_tabs_function(tab) {
    var sale_obj = document.getElementById('primelocation_type_sale');
    var rent_obj = document.getElementById('primelocation_type_rent');
    var newhomes_obj = document.getElementById('primelocation_type_newhomes');
    var prices_obj = document.getElementById('primelocation_type_prices');

    var section_obj = document.getElementById('primelocation_section_type');

    if (tab == 'sale') {

        if (sale_obj !== null && sale_obj.className.indexOf('current') < 0) sale_obj.className += " current";
        if (rent_obj !== null && rent_obj.className.indexOf('none') < 0) rent_obj.className = "primelocation_type";
        if (newhomes_obj !== null && newhomes_obj.className.indexOf('none') < 0) newhomes_obj.className = "primelocation_type";
        if (prices_obj !== null && prices_obj.className.indexOf('none') < 0) prices_obj.className = "primelocation_type";

        add_tracking('Ig8bCXK2', 'clk');

        section_obj.value = "for-sale";

    } else if (tab == 'rent') {

        if (rent_obj !== null && rent_obj.className.indexOf('current') < 0) rent_obj.className += " current";
        if (sale_obj !== null && sale_obj.className.indexOf('none') < 0) sale_obj.className = "primelocation_type";
        if (newhomes_obj !== null && newhomes_obj.className.indexOf('none') < 0) newhomes_obj.className = "primelocation_type";
        if (prices_obj !== null && prices_obj.className.indexOf('none') < 0) prices_obj.className = "primelocation_type";

        add_tracking('Ig8foWbz', 'clk');

        section_obj.value = 'to-rent';

    } else if (tab == 'prices') {

        if (prices_obj !== null && prices_obj.className.indexOf('current') < 0) prices_obj.className += " current";
        if (sale_obj !== null && sale_obj.className.indexOf('none') < 0) sale_obj.className = "primelocation_type";
        if (rent_obj !== null && rent_obj.className.indexOf('none') < 0) rent_obj.className = "primelocation_type";
        if (newhomes_obj !== null && newhomes_obj.className.indexOf('none') < 0) newhomes_obj.className = "primelocation_type";

        add_tracking('Ig8nmJcY', 'clk');

        section_obj.value = 'house-prices';

    } else {

        if (newhomes_obj !== null && newhomes_obj.className.indexOf('current') < 0) newhomes_obj.className += " current";
        if (sale_obj !== null && sale_obj.className.indexOf('none') < 0) sale_obj.className = "primelocation_type";
        if (rent_obj !== null && rent_obj.className.indexOf('none') < 0) rent_obj.className = "primelocation_type";
        if (prices_obj !== null && prices_obj.className.indexOf('none') < 0) prices_obj.className = "primelocation_type";

        add_tracking('IpJifJZE', 'kx_clk');

        section_obj.value = 'new-homes-bar';

    }

    var menu_items = new Array();
    var children = document.getElementById('primelocation_sections').childNodes;

    for (var i = 0; i < children.length; i++) {

        if (children[i].nodeName == 'DIV') {
            menu_items.push(children[i]);
        }

    }

    if (menu_items[(menu_items.length - 1)].className.indexOf('last') < 0) menu_items[(menu_items.length - 1)].className += ' last';

}

// For sale option function
function primelocation_sale_function() {
    primelocation_tabs_function('sale');
    document.getElementById('primelocation_toggle_sale_price_min').style.display = 'block';
    document.getElementById('primelocation_toggle_sale_price_max').style.display = 'block';
    document.getElementById('primelocation_toggle_rent_price_min').style.display = 'none';
    document.getElementById('primelocation_toggle_rent_price_max').style.display = 'none';
    document.getElementById('primelocation_sale_price_min').disabled = '';
    document.getElementById('primelocation_sale_price_max').disabled = '';
    document.getElementById('primelocation_rent_price_min').disabled = 'true';
    document.getElementById('primelocation_rent_price_max').disabled = 'true';
    document.getElementById('primelocation_beds_min').disabled = '';
    document.getElementById('primelocation_property_type').disabled = '';
    document.getElementById('primelocation_attributes').style.display = 'block';
    primelocation_change_hint(primelocation_hint_1, primelocation_example_1);

}

// To rent option function
function primelocation_rent_function() {
    primelocation_tabs_function('rent');
    document.getElementById('primelocation_toggle_sale_price_min').style.display = 'none';
    document.getElementById('primelocation_toggle_sale_price_max').style.display = 'none';
    document.getElementById('primelocation_toggle_rent_price_min').style.display = 'block';
    document.getElementById('primelocation_toggle_rent_price_max').style.display = 'block';
    document.getElementById('primelocation_sale_price_min').disabled = 'true';
    document.getElementById('primelocation_sale_price_max').disabled = 'true';
    document.getElementById('primelocation_rent_price_min').disabled = '';
    document.getElementById('primelocation_rent_price_max').disabled = '';
    document.getElementById('primelocation_beds_min').disabled = '';
    document.getElementById('primelocation_property_type').disabled = '';
    document.getElementById('primelocation_attributes').style.display = 'block';
    primelocation_change_hint(primelocation_hint_1, primelocation_example_1);

}

// Home value or House price option function
function primelocation_prices_function() {
    primelocation_tabs_function('prices');
    document.getElementById('primelocation_attributes').style.display = 'none';
    document.getElementById('primelocation_sale_price_min').disabled = 'true';
    document.getElementById('primelocation_sale_price_max').disabled = 'true';
    document.getElementById('primelocation_rent_price_min').disabled = 'true';
    document.getElementById('primelocation_rent_price_max').disabled = 'true';
    document.getElementById('primelocation_beds_min').disabled = 'true';
    document.getElementById('primelocation_property_type').disabled = 'true';
    primelocation_change_hint(primelocation_hint_2, primelocation_example_2);
}

function primelocation_newhomes_function() {
    primelocation_tabs_function('newhomes');
    document.getElementById('primelocation_toggle_sale_price_min').style.display = 'block';
    document.getElementById('primelocation_toggle_sale_price_max').style.display = 'block';
    document.getElementById('primelocation_toggle_rent_price_min').style.display = 'none';
    document.getElementById('primelocation_toggle_rent_price_max').style.display = 'none';
    document.getElementById('primelocation_sale_price_min').disabled = '';
    document.getElementById('primelocation_sale_price_max').disabled = '';
    document.getElementById('primelocation_rent_price_min').disabled = 'true';
    document.getElementById('primelocation_rent_price_max').disabled = 'true';
    document.getElementById('primelocation_beds_min').disabled = '';
    document.getElementById('primelocation_property_type').disabled = '';
    document.getElementById('primelocation_attributes').style.display = 'block';
    primelocation_change_hint(primelocation_hint_1, primelocation_example_1);
}

// Onload function
if (document.getElementById('primelocation_newhomes') && document.getElementById('primelocation_newhomes').checked === true) {

    primelocation_newhomes_function();

} else if (document.getElementById('primelocation_prices') && document.getElementById('primelocation_prices').checked === true) {

    primelocation_prices_function();

} else if (document.getElementById('primelocation_rent') && document.getElementById('primelocation_rent').checked === true) {

    primelocation_rent_function();

} else {

    primelocation_sale_function();

}
