function validateEmail(e) {
    var valid = WebLegs.DataValidator.IsValidEmail(e.value);
    showValidity(e, valid);
    return valid;
}

function validatePhone(e) {
    if (e.value === "") {
        clearValidity(e);
        return true;
    }
    var valid = WebLegs.DataValidator.IsPhone(e.value);
    showValidity(e, valid);
    return valid;
}

function validateName(e) {
    var valid = WebLegs.DataValidator.IsAlpha(e.value);
    showValidity(e, valid);
    return valid;
}

function showValidity(e, b) {
    var id = "v_" + e.name;
    e = document.getElementById(id);
    if (b) {
        e.style.backgroundImage = "url(images/icons/valid.png)";
    }
    else {
        e.style.backgroundImage = "url(images/icons/invalid.png)";
    }    
}

function clearValidity(e) {
    var id = "v_" + e.name;
    e = document.getElementById(id);
    e.style.backgroundImage = "none";
}

function validateContactForm() {
    var fieldIds = ["cf_fn","cf_ln","cf_em","cf_hp","cf_wp","cf_cp"];
    var fieldValidation = ["a","a","e","p","p","p"];
    var errors = 0;
    for (var i = 0; i < fieldIds.length ; i++) {
        var e = document.getElementById(fieldIds[i]);
        var valid;
        switch (fieldValidation[i]) {
            case "a" :
                valid = validateName(e);
                break;
            case "e" :
                valid = validateEmail(e);
                break;
            case "p" :
                valid = validatePhone(e);
        }
        if (!valid) {
            errors++;
        }
    }
    var element = document.getElementById("cf_feedback");
    if (errors == 0) {
        element.style.color = "#009900";
        element.innerHTML = "Your message is being processed.";
        document.getElementById("contact_form").reset();
        return true;
    }
    else {
        element.style.color = "#990000";
        var errorMessage = "Your message has not been sent.<br />Please correct the invalid data.";
        element.innerHTML = errorMessage;
        return false;
    }
}

function resetRegionalMap() {
    switchRegionalMap("regional_map");
}

function switchRegionalMap(s) {
    var i = document.getElementById("regional_map");
    i.src = imgArray[s].src;
}

var imgArray = new Array();
function preloadImages() {
    addImage("regional_map");
    addImage("s");
    addImage("se");
    addImage("sw");
    addImage("n");
    addImage("ne");
    addImage("nw");
    addImage("ec");
    addImage("wc");
}

function addImage(s) {
    imgArray[s] = new Image();
    imgArray[s].src = "images/maps/" + s + ".jpg";
}


function setLinks() {
    if (!document.getElementsByTagName) return false;
    var links = document.getElementsByTagName("a");
    if (links.length == 0) return false;
    for (var i = 0; i < links.length; i++) {
        var relation = links[i].getAttribute("rel");
        if (relation == "external") {
            links[i].onclick = function() {
                return !window.open(this.href);
            }
        }
    }
}


function getSelectedIndex(id) {
    var myselect=document.getElementById(id);
    for (var i=0; i<myselect.options.length; i++){
        if (myselect.options[i].selected==true){
            return i;
        }
    }
    return 0;
}

function submitSort(search) {
    if  (search == null) {
        search = "";
    }
    var sort = document.getElementById("sort").options[getSelectedIndex("sort")].value;
    var d = document.getElementById("sort_direction").options[getSelectedIndex("sort_direction")].value;
    window.location = 'search_results.php?' + search + "&sort=" + sort + "&d=" + d;
}

function submitAdminSort(search) {
    if  (search == null) {
        search = "";
    }
    var sort = document.getElementById("sort").options[getSelectedIndex("sort")].value;
    var d = document.getElementById("sort_direction").options[getSelectedIndex("sort_direction")].value;
    window.location = 'admin_listings.php?' + search + "&sort=" + sort + "&d=" + d;
}

function submitQuickSearch() {
    var search_criteria = "";
    var lt = document.getElementById("qs_lt").options[getSelectedIndex("qs_lt")].value;
    if (lt != "") {
        search_criteria += "&lot=" + lt;
    }
    var a = document.getElementById("qs_a").options[getSelectedIndex("qs_a")].value;
    if (a != "") {
        search_criteria += "&a=" + a;
    }
    var p = document.getElementById("qs_p").options[getSelectedIndex("qs_p")].value;
    if (p != "") {
        search_criteria += "&p=" + p;
    }
    window.location = 'search_results.php?' +  search_criteria;
}

function submitAdvancedSearch() {
    var search_criteria = "";
    var lt = document.getElementById("qs_lt").options[getSelectedIndex("qs_lt")].value;
    if (lt != "") {
        search_criteria += "&lot=" + lt;
    }
    var a = document.getElementById("qs_a").options[getSelectedIndex("qs_a")].value;
    if (a != "") {
        search_criteria += "&a=" + a;
    }
    var p = document.getElementById("qs_p").options[getSelectedIndex("qs_p")].value;
    if (p != "") {
        search_criteria += "&p=" + p;
    }
    var m = document.getElementById("qs_m").options[getSelectedIndex("qs_m")].value;
    if (m != "") {
        search_criteria += "&m=" + m;
    }
    var ss = document.getElementById("qs_ss").options[getSelectedIndex("qs_ss")].value;
    if (ss != "") {
        search_criteria += "&ss=" + ss;
    }
    var r = document.getElementById("qs_r").options[getSelectedIndex("qs_r")].value;
    if (r != "") {
        search_criteria += "&region=" + r;
    }
    var sd = document.getElementById("qs_sd").options[getSelectedIndex("qs_sd")].value;
    if (sd != "") {
        if (sd == "-1") {
            search_criteria += "&ind=yes";
        }
        else if (sd == "-2") {
            search_criteria += "&ind=no";
        }
        else {
            search_criteria += "&sd=" + sd;
        }
    }
    var ld = "&ld=";
    var lds = document.getElementById("lot_descriptions");
    var checkboxes = lds.getElementsByTagName("input");
    for (var box in checkboxes) {
        if (checkboxes[box].checked == 1) {
            if (ld != "&ld=") {
                ld += ",";
            }
            ld += checkboxes[box].name.substring(4);
        }
    }
    if (ld != "&ld=") {
        search_criteria += ld;
    }
    
    window.location = 'search_results.php?' +  search_criteria;
}

function geocode() {
    var button = document.getElementById("gen");
    button.value = "Geocoding...";
    button.disabled = true;
    geocoder = new google.maps.Geocoder();
    if (geocoder) {
        var address = document.getElementById("sa").value + ", " + document.getElementById("c").value + " WI " + document.getElementById("z").value;
        geocoder.geocode( {
            'address': address
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                LatLng = results[0].geometry.location
                document.getElementById("latlng").value = LatLng;
            }
            else {
                alert("Could not geocode the address.");
            }
            button.value = "Geocode Street Address";
            button.disabled = false
        });
    }
    else {
        alert ("Sorry, could not find Google javascript code.")
    }

}

