﻿
if (document.all && window.attachEvent) {
    window.attachEvent("onload", init);
}
function init() {
    if (document.getElementById("bedrooms") != null) {
        var bedrooms = document.getElementById("bedrooms").value;

        if (bedrooms != null && bedrooms.length > 0) {
            var id = "bedrooms" + bedrooms;
            document.getElementById(id).className = "selected";
        }
    }
    if (document.getElementById("bathrooms") != null) {
        var bathrooms = document.getElementById("bathrooms").value;

        if (bathrooms != null && bathrooms.length > 0) {
            var id = "bathrooms" + bathrooms;
            document.getElementById(id).className = "selected";
        }
    }
}

function selector_Click(parentID, item) {
    var parent = document.getElementById(parentID);

    if (parent != null) {
        var items = parent.getElementsByTagName("a");

        for (var i = 0; i < items.length; i++) {
            items[i].className = "";
        }

        item.className = "selected";
    }
}
function bedroomsSelector_Click(item, value) {
    selector_Click("bedroomsSelector", item);

    var bedrooms = document.getElementById("bedrooms");

    if (bedrooms != null) {
        bedrooms.value = value;
    }
}

function bathroomsSelector_Click(item, value) {
    selector_Click("bathroomsSelector", item);

    var bathrooms = document.getElementById("bathrooms");

    if (bathrooms != null) {
        bathrooms.value = value;
    }
}

var locations = new Array();

function toggleVisibility(containerID, toggleSwitchID) {
    var container = document.getElementById(containerID);
    var toggle = document.getElementById(toggleSwitchID);

    if (container.style.display == "none") {
        container.style.display = "block";
        toggle.src = "http://mysitedb.ultraitsolutions.com/images/minus.gif";
    }
    else {
        container.style.display = "none";
        toggle.src = "http://mysitedb.ultraitsolutions.com/images/plus.gif";
    }
}

function selectTownsByRegion(regionName) {
    var townCheckBoxes = document.getElementsByName(regionName);

    var townElementName = 'TownElement' + regionName;
    var townElements = getElementsByAttribute(document.body, 'div', 'rel', regionName);

    var parentName = regionName + 'RegionCheckBox'
    var parent = document.getElementById(parentName);

    // alert("parentName: " + parentName);
    // alert("TownElement: " + townElementName);

    if (parent.checked == true) {
        // alert("Check the towns");
        for (i = 0; i < townCheckBoxes.length; i++) {
            var locationID = townCheckBoxes[i].getAttribute('rel');
            //alert("Town" + i);
            //alert("Number of Towns: " + townCheckBoxes.length);
            //alert("Number of Elements: " + townElements.length);
            if (townElements[i].className.indexOf("selected") != -1) {
                // do nothing
            }
            else {
                // show as selected and add to the town list
                townElements[i].className = "level3 levelselected";
                townCheckBoxes[i].checked = true;
                addLocation(locationID);
            }
        }
    }
    else {
        // alert("Uncheck the towns");
        for (i = 0; i < townCheckBoxes.length; i++) {
            var locationID = townCheckBoxes[i].getAttribute('rel');
            if (townElements[i].className.indexOf("selected") != -1) {
                // show as selected and add to the town list
                townElements[i].className = "level3";
                townCheckBoxes[i].checked = false;
                // remove location
                removeLocation(locationID);
            }
            else {
                // do nothing
            }
        }
    }
}


function town_Click(locationElementID, locationCheckBoxID, locationID) {
    var locationElement = document.getElementById(locationElementID);
    var locationCheckBox = document.getElementById(locationCheckBoxID);

    if (locationElement.className.indexOf("selected") != -1) {
        locationElement.className = "level3";
        locationCheckBox.checked = false;
        removeLocation(locationID);
    }
    else {
        locationElement.className = "level3 levelselected";
        locationCheckBox.checked = true;
        addLocation(locationID);
    }
}

function addLocation(locationID) {
    var found = false;

    for (var i = 0; i < locations.length; i++) {
        if (locations[i] == locationID) {
            found = true;
        }
    }

    if (!found) {
        locations[locations.length] = locationID;
    }

    updateField();
}

function removeLocation(locationID) {
    for (var i = 0; i < locations.length; i++) {
        if (locations[i] == locationID) {
            locations[i] = "";
        }
    }

    updateField();
}

function updateField() {
    var locationsField = document.getElementById("locations");

    locationsField.value = "";

    for (var i = 0; i < locations.length; i++) {
        if (locations[i].length > 0) {
            locationsField.value = locationsField.value + locations[i] + ",";
        }
    }
}


function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue) {
    var arrElements = (strTagName == "*" && document.all) ? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined") ? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    var oCurrent;
    var oAttribute;
    for (var i = 0; i < arrElements.length; i++) {
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute(strAttributeName);
        if (typeof oAttribute == "string" && oAttribute.length > 0) {
            if (typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))) {
                arrReturnElements.push(oCurrent);
            }
        }
    }
    return arrReturnElements;
}
