﻿
$('category').onchange = function() {
    var cTab = $('category').options[$('category').selectedIndex].value;
    if (cTab == "") { return; }
    SwitchTab(cTab);
};

$('searchterm').onchange = function() {
    var cTab = $('category').options[$('category').selectedIndex].value;
    if (cTab == "" && $F('searchterm') == "") {
        DisableSearch();
    } else {
        EnableSearch();
    }
};

function noAlpha(obj) {
    var reg = /[^0-9]/g;
    obj.value = obj.value.replace(reg, "");
}

function selectText(what) {
    var obj = $(what);
    obj = Element.extend(obj);
    obj.select();
}

function configureInputElements() {
    // Find the input elements that exist within the filtering span
    // container and have either the level or cost class
    // and disallow all characters except for numbers
    $$('input').each(function(s) {
        var input = Element.extend(s);
        var id = input.readAttribute('id');
        if (id != 'searchterm') {
            Event.observe(input, 'keyup', function() {
                noAlpha(input);
            });
        }
    });
//    if (isiPad) {
//        document.getElementById('resultsTableBody').style.height = '352px'
//        document.getElementById('results').style.height = '468px'
//        document.getElementById('resultsBody').style.height = '434px'
//        document.getElementById('resultsBottom').style.top = '444px'
//        document.getElementById('footer').style.height = '683px'
//    }
}

Event.observe(window, 'load', configureInputElements);
Event.observe(window, 'load', PopulateFilterSelects);
Event.observe(window, 'resize', function () {
    if (isiPad) {
        document.getElementById('main').style.left = '2px';
    }
    else if (document.body.clientWidth)
        document.getElementById('main').style.left = Math.floor((document.body.clientWidth - 1016) / 2) + 'px';
    else
        document.getElementById('main').style.left = Math.floor((document.width - 1016) / 2) + 'px';
});
Event.observe($('searchterm'), 'keydown', function (event) {
    var e = event;
    var keynum = 0;
    if (window.event) {
        keynum = e.keyCode;
    } else if (e.which) {
        keynum = e.which;
    }
    if (keynum == 13) {
        if (Prototype.Browser.WebKit) {
            e.stop();
        }
        var cTab = $('category').options[$('category').selectedIndex].value;
        ExecuteSearch(cTab);
    }
});

Event.observe($('searchterm'), 'keyup', function () {
    var cTab = $('category').options[$('category').selectedIndex].value;
    if (cTab == "" && $F('searchterm') == "") {
        DisableSearch();
    } else {
        EnableSearch();
    }
});

Event.observe($('nameonly'), 'click', function () {
    var state = $('nameonly').checked;
    if (state) {
        window.nameOnly = true;
    } else {
        window.nameOnly = false;
    }
});

Event.observe($('resetForm'), 'click', function () {
    ResetAll();
});

function EnableSearch() {
    var obj = $('searchbutton');
    obj.setStyle({ cursor: 'pointer' });
    obj.onclick = function() {
        var cTab = $('category').options[$('category').selectedIndex].value;
        ExecuteSearch(cTab);
    };
    obj.src = 'images/search_button.png';
}

function DisableSearch() {
    var obj = $('searchbutton');
    obj.setStyle({ cursor: 'default' });
    obj.onclick = "";
    obj.src = 'images/search_button_d.png';
}

function EnableFilter() {
    var obj = $('filter_apply');
    obj.setStyle({ cursor: 'pointer' });
    $('filter_apply').onclick = function() {
        ApplyFilter();
    };
    obj.src = 'images/filter_apply.png';

    obj = $('filter_reset');
    obj.setStyle({ cursor: 'pointer' });
    $('filter_reset').onclick = function() {
        ResetFilter();
    };
    obj.src = 'images/filter_reset.png';

}

function DisableFilter() {
    var obj = $('filter_apply');
    obj.setStyle({ cursor: 'default' });
    obj.onclick = "";
    obj.src = 'images/filter_apply_d.png';

    obj = $('filter_reset');
    obj.setStyle({ cursor: 'default' });
    obj.onclick = "";
    obj.src = 'images/filter_reset_d.png';

}

