﻿/***********************************************/
//  Copyright 2011 BIRDVIEW Technologies, Inc.
//  author: Haig Bedrosian
//
//  2011/06/10  Inception
/***********************************************/

var cbNode, cbOffset;

function AddCity(cityID) {
    elSel = document.getElementById(cityID);

    // make sure we found a checkbox
    if (elSel != null) {
        // toggle the checkbox
        elSel.checked = (elSel.checked ^ true);

        // scroll the element into view (rather than jumping right to it)
        scrollIntoView(elSel);
    }

/*
    elSel = document.getElementById("select");

    // loop through all elements in the list
    for (i = elSel.length - 1; i >= 0; i--) {

        // find the matching element
        if (elSel.elements[i].value == city) {

            // toggle the checkbox
            elSel.elements[i].checked = (elSel.elements[i].checked ^ true);

            // scroll the element into view (rather than jumping right to it)
            scrollIntoView(elSel.elements[i]);
            break;
        }
    }
*/
}

function checkAllAreas() {
    elSel = document.getElementById("select");

    // loop through all elements in the list
    for (i = elSel.length - 1; i >= 0; i--) {
        // activate the element
        elSel.elements[i].checked = true;
    }
}

function uncheckAllAreas() {
    elSel = document.getElementById("select");

    // loop through all elements in the list
    for (i = elSel.length - 1; i >= 0; i--) {
        // deactivate the element
        elSel.elements[i].checked = false;
    }
}

function scrollIntoView(node) {
    cbNode = document.getElementById("area_checkbox");
    var parentCHeight = cbNode.clientHeight;
    var parentSHeight = cbNode.scrollHeight;
  
    // determine if scrolling is necessary
    if (parentSHeight > parentCHeight) {
        var nodeHeight = node.clientHeight;
        var nodeOffset = node.offsetTop;

        // determine how far to scroll
        var scrollOffset = nodeOffset + (nodeHeight / 3) - (parentCHeight / 3);

        // constrain the scrolling target by the min/max positions
        if (scrollOffset >= (parentSHeight - parentCHeight)) { scrollOffset = parentSHeight - parentCHeight; }
        else if (scrollOffset < 0) { scrollOffset = 0; }

        // jump to the target
        //parent.scrollTop = scrollOffset;
        cbOffset = scrollOffset; cbIncrement = 0;

        // scroll to the target
        animateScroll();
    }
}

function animateScroll() {
    // target position is an approximation, so if we are within the size of the next increment, just end at the target position
    if (Math.abs(cbOffset - cbNode.scrollTop) <= 20) { cbNode.scrollTop = cbOffset; return; }

    // increment the scrolling position
    cbNode.scrollTop += (cbOffset < cbNode.scrollTop) ? -20 : 20;

    // call this function again after 1ms
    setTimeout("animateScroll()", 1);
}

function doAreaQuickSearch() {
    var pTypeindex = document.getElementById('type').selectedIndex;
    var pType = document.getElementById('type')[pTypeindex].value;

    var MinPriceindex = document.getElementById('min').selectedIndex;
    var MinPrice = document.getElementById('min')[MinPriceindex].value;

    var MaxPriceindex = document.getElementById('max').selectedIndex;
    var MaxPrice = document.getElementById('max')[MaxPriceindex].value;

    var bedsindex = document.getElementById('beds').selectedIndex;
    var bed = document.getElementById('beds')[bedsindex].value;

    var bathsindex = document.getElementById('baths').selectedIndex;
    var bth = document.getElementById('baths')[bathsindex].value;

    var cityList = getCheckboxValues(document.asearch.cities)

    window.location.href = "/homesrch.htm?action=search&scope=ALL&state=IL&bedrooms=" + bed + "&bathrooms=" + bth +"&minprice=" + MinPrice + "&maxprice=" + MaxPrice + "&hometypes[]=" + pType + "&city=" + cityList;
}

function getCheckboxValues(checkBoxArray) {
    var values = new Array();
    for (i = 0; i < checkBoxArray.length; i++){
        if (checkBoxArray[i].checked) { values[values.length] = checkBoxArray[i].value; }
    }
    return values;
}

