var usesDhtml   = 0;
var usesLayers  = 0;
var usesAll     = 0;
var usesID      = 0;
var isNs4       = false;
var whitespace  = " \t\n\r";
var defaultEmptyOK = false;
var s_name = 'Please enter your name in this field!';
var s_contact = 'Please enter your email address or phone number in this field!';
var s_street = 'Address incomplete: Please enter your street in this field!';
var s_town = 'Address incomplete: Please enter your town in this field!';
var s_postcode = 'Address incomplete: Please enter your post code in this field!';
var s_county = 'Address incomplete: Please enter your county in this field!';
var s_comments = 'Please enter your message to us!';
var s_when_move = 'Please tell us approximately when you intend to purchase a new property.';



function isEmpty(s) {                        // Check whether string s is empty.
    return ((s == null) || (s.length == 0))
}

function isWhitespace (s) {     // Returns true if string s is empty or whitespace characters only.
    var i;
    if (isEmpty(s)) return true;    // Is s empty?

    // Search through string's characters until non-whitespace character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);    // Check that current character isn't whitespace.
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;         // All characters are whitespace.
}

function init_page() {
    if ( document.getElementById &&
         (navigator.appName.indexOf('Opera') == '-1') ) {    // IE.
        usesID = 1;                                         // Should be opera 5+ too but doesn't seem to be happy.
        usesDhtml = 1;
    } else {
        if ( document.all) {
            usesAll = 1;
            usesDhtml = 1;
        } else {
            browserVersion = parseInt( navigator.appVersion);
            if (( navigator.appName.indexOf('Netscape') != '-1') && (browserVersion == 4 )) {
                usesLayers    = 1;
                usesDhtml     = 1;
                isNs4         = true;
            }
        }
    }
}

function GetStyle( objectId ) {
    if ( usesID ) {
        if (document.getElementById(objectId) != null) return ( document.getElementById(objectId).style );
    } else {
        if ( usesAll ) {
            return ( document.all[objectId].style );
        } else {
            if ( usesLayers ) {
                return ( document.layers[objectId] );
            }
        }
    }
}
function getObj(name) {
    if (document.getElementById) {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
    } else if (document.all) {
        this.obj = document.all[name];
        this.style = document.all[name].style;
    }  else if (document.layers)  {
        this.obj = getObjNN4(document,name);
        this.style = this.obj;
    }
}



function getObjNN4(obj,name) {
    var x = obj.layers;
    var foundLayer;
    for (var i=0;i<x.length;i++) {
        if (x[i].id == name)
            foundLayer = x[i];
        else if (x[i].layers.length)
            var tmp = getObjNN4(x[i],name);
        if (tmp) foundLayer = tmp;
    }
    return foundLayer;
}

function select_county(county) {    // raises county map image + ticks box
    var map = 'map' + county;
    var tickbox = 'limit_county_' + county;
    var mapstyle = GetStyle(map);

    mapstyle.zIndex = 33;
    if ( usesID ) document.getElementById(tickbox).checked = true; // IE + NN6+
    else if ( usesAll ) document.all[tickbox].checked = true;
    else if ( usesLayers ) document.countyform.elements[tickbox].checked = true;
}

function select_county_text(county) {    // raises county map image + ticks box
    var tickbox = 'limit_county_' + county;

    if ( usesID ) document.getElementById(tickbox).checked = true; // IE + NN6+
    else if ( usesAll ) document.all[tickbox].checked = true;
    else if ( usesLayers ) document.countyform.elements[tickbox].checked = true;
}

function unselect_county(county) {    // raises county map image + ticks box
    var map = 'map' + county;
    var tickbox = 'limit_county_' + county;
    var mapstyle = GetStyle(map);

    mapstyle.zIndex = 1;
    if ( usesID ) document.getElementById(tickbox).checked = false;
    else if ( usesAll ) document.all[tickbox].checked = false;
    else if ( usesLayers ) document.countyform.elements[tickbox].checked = false;
}

function unselect_county_text(county) {    // raises county map image + ticks box
    var tickbox = 'limit_county_' + county;

    if ( usesID ) document.getElementById(tickbox).checked = false;
    else if ( usesAll ) document.all[tickbox].checked = false;
    else if ( usesLayers ) document.countyform.elements[tickbox].checked = false;
}

function toggle_county(county) {    // raises or lowers county map image  + ticks / unticks box
    // in: county = name_short

    var map = 'map' + county;
    var tickbox = 'limit_county_' + county;
    var mapstyle = GetStyle(map);

    if (mapstyle != null) { // if map exists:

        if (mapstyle.zIndex < 32) {             // Select (raise) county
            select_county(county);
            if (county == 'powys') {
                select_county('knighton');
                select_county('brecon');
                select_county('radnor');
                select_county('montgom');
            }
            if (county == 'shrops') select_county('knighton');
            if (county == 'radnor') select_county('knighton');
        } else {
            unselect_county(county);            // Un-select (lower)
            if (county == 'knighton') unselect_county('powys');
            if (county == 'brecon')   unselect_county('powys');
            if (county == 'radnor')   unselect_county('powys');
            if (county == 'montgom')  unselect_county('powys');
        }
    }
    // new: Ensure town name layer stays on top:
    //
    var map = 'map_towns';
    var mapstyle = GetStyle(map);
    if (mapstyle != null) {
        mapstyle.zIndex = 34;
    }
}

function county_ticked(county) {    // returns true/false if requested county is ticked
    var tickbox = 'limit_county_' + county;
alert(county + ';' + tickbox);
    if ( usesID )
         return document.getElementById(tickbox).checked; // IE + NN6+
    else if ( usesAll )
         return document.all[tickbox].checked;
    else if ( usesLayers )
         return document.countyform.elements[tickbox].checked;
    else return false;
}

function toggle_county_text(county) {    // raises or lowers county map image  + ticks / unticks box
    // in: county = name_short

    var tickbox = 'limit_county_' + county;

alert('toggle_county_text');

    if (county_ticked(county)) {
        unselect_county_text(county);
        if (county == 'knighton' || county == 'brecon' ||
            county == 'radnor'   || county == 'montgom')
            unselect_county('powys');
    } else {
        select_county_text(county);
        if (county == 'powys') {
             select_county('knighton');
             select_county('brecon');
             select_county('radnor');
             select_county('montgom');
        }
        if (county == 'shrops' || county == 'radnor')
            select_county('knighton');
    }


}

function check_for_number(value) {                                            // Zahl prüfen
    if ( parseInt(value,10) != value ) {
        return false; // NaN
    } else {
        if ( value < 0 ) return false;          // < 0
    }
    return true;                                // is a Number
}

function check_format_numberfield(field) {                                            // Zahlenfeld prüfen
    if (check_for_number(field.value) == false) {
        alert("Please enter a valid number (>= 0).");
        field.focus(); return false;
    }
    return true;
}
function isEmail (s) {
    if (isEmpty(s))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);

    if (isWhitespace(s)) return false;                  // is s whitespace?

    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@")) {    // look for @
        i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != ".")) {    // look for .
        i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function tighten(s) {
    return s.replace(/ /ig, "");
}

function check_format_email(field) {
    if (isWhitespace(field.value)) {
        alert("Please enter your EMail address.");
        field.focus();
        return false;
    } else {
        if (!isEmail(field.value)) {
            alert("Please enter a valid EMail address.");
            field.focus();
            return false;
        } else {
            return true;
        }
    }
}
function check_format_empty(field, msg) {
    if ( field.value.length == 0 ){
        alert(msg);
        field.focus();
        return false;
    }
    return true;
}

function check_dropbox_selected(field, msg) {
    if (field.options[field.options.selectedIndex].value.length == 0) {
        alert(msg);
        field.focus();
        return false;
    } else {
        return true;
    }
}

function checkSearchForm(thisform) {
//    if (check_format_numberfield(document.getElementById(['limit_to[land_min]'])) == false) return false;
//    if (check_format_numberfield(document.getElementById(['limit_to[land_max]'])) == false) return false;
    if (check_format_numberfield(document.getElementById(['limit_to[price_min]'])) == false) return false;
    if (check_format_numberfield(document.getElementById(['limit_to[price_max]'])) == false) return false;
    return true;
}

function checkMailingRegForm(thisform) {
    if (!check_format_email(thisform.email)) return false;               // email
    if (!check_format_empty(thisform.name, s_name)) return false;    // realname
    if (!check_dropbox_selected(thisform.when_move, s_when_move)) return false;    // when_move (dropbox)
    return confirm('Please double-check that there are no mistakes in your email address "' + thisform.email.value + '". Click OK to continue or cancel to amend it!');
//    return true;
}


function checkContactForm(thisform) {
    if (!check_format_empty(thisform.name, s_name)) return false;
    if (!check_format_empty(thisform.comments, s_comments)) return false;
    if (!check_format_empty(thisform.contact, s_contact)) return false;
    // realname
    // email
    // pw & pw2
    // when_move (dropbox)
    return true;
}

function checkNotepadSendForm(thisform) {
    if (!check_format_empty(thisform.name, s_name)) return false;
    if (!check_format_empty(thisform.address_street, s_street)) return false;
    if (!check_format_empty(thisform.address_town, s_town)) return false;
    if (!check_format_empty(thisform.address_postcode, s_postcode)) return false;
    if (!check_format_empty(thisform.address_county, s_county)) return false;

    if (!check_format_email(thisform.email)) return false;               // email

    if (!check_dropbox_selected(thisform.when_move, s_when_move)) return false;

    return true;
}



init_page();
