// array containing key's to skip a check for in forms
var keyCodeExceptions = new Array(9,13,16,17,18,19,20,27,33,34,35,36,37,38,39,40,46);

function swapModelOptions(selectfrom, selectto)
{
    var selectedbrand, selectedmodel, obj, option;

    if (selectfrom.selectedIndex) {

        selectedbrand = selectfrom.options[selectfrom.selectedIndex].value;
        selectedmodel = selectto.options[selectto.selectedIndex].value;

        if (selectto && selectedbrand) {

            selectedbrand = selectedbrand.replace(' ', '_').replace('-', '_');

            obj = eval(selectedbrand);

            if (typeof(obj) == 'object') {

                removeChildren(selectto);

                txt = document.createTextNode(all);
                option = document.createElement('option');
                option.setAttribute('value', '');
                option.appendChild(txt);
                selectto.appendChild(option);

                for (var i = 0; i < obj.length; i++) {

                    txt = document.createTextNode(obj[i]);
                    option = document.createElement('option');

                    option.setAttribute('value', obj[i]);

                    if (selectedmodel && (option.getAttribute('value') == selectedmodel)) {
                        option.setAttribute('selected', 'selected');
                    }

                    option.appendChild(txt);
                    selectto.appendChild(option);
                }
            }
        }
    } else {

        removeChildNodes(selectto);

        txt = document.createTextNode(all);
        option = document.createElement('option');
        option.setAttribute('value', '');
        option.appendChild(txt);
        selectto.appendChild(option);

        var h = 0;
        modelArray = new Array();

        // first fill the modelArray
        for (var i = 0; i < brands.length; i++) {
            for (var j = 0; j < brands[i].length; j++) {
                modelArray[h++] = brands[i][j];
            }
        }
        // then sort it
        modelArray.sort();
        // then display it
        for (var k = 0; k < modelArray.length; k++) {

            txt = document.createTextNode(modelArray[k]);
            option = document.createElement('option');

            option.setAttribute('value', modelArray[k]);
            option.appendChild(txt);
            selectto.appendChild(option);
        }
    }
}

function removeChildNodes(target)
{
    while (target.options.length > 0) {
        // remove options from target element
        target.options[0] = null;
    }
}

function isValidBPM(bpm)
{
    // only accept digits
    var refno= new RegExp("^\\d{1,}$");

    return bpm.match(refno);
}

function calculatetaxvalue(inputid, fueltypeid, outputid)
{
    var taxvalue;
    var input = document.getElementById(inputid);
    var fueltype = document.getElementById(fueltypeid);
    var output = document.getElementById(outputid);

    if (input && fueltype && output) {

        var bpm = input.value;

        if (bpm) {

            if (isValidBPM(bpm)) {

                bpm = parseFloat(bpm);

                taxvalue = (fueltype.options[fueltype.selectedIndex].value == 3) ? Math.round((((bpm - 328) / 0.452) * 1.19) + bpm) : Math.round((((bpm + 1540) / 0.452) * 1.19) + bpm);
                output.firstChild.nodeValue = taxvalue;

            } else {
                alert('U mag alleen cijfers invoeren');
            }

        } else {
            alert('Vul eerst het BPM-bedrag in');
        }
    }
}

function applyKeyUpEvent(id, callback)
{
    var el = document.getElementById(id);

    if (el) {
        if (window.addEventListener) {
            el.addEventListener('keyup', callback, false);
        } else {
            el.attachEvent('onkeyup', callback);
        }
    }
}

function moveOnIfFieldCompleted(e)
{
    var input = e['srcElement'] || e['target'];
    var keycode = e.keyCode || e.which;

    // check if no exception key is pressed
    if (keycode) {
        for (var i = 0; i < keyCodeExceptions.length; i++) {
            if (keycode == keyCodeExceptions[i]) {
                var nocheck = true;
            }
        }

        if (typeof(nocheck) == 'undefined') {

            // maxlength is 2
            if (input.value.length == 2) {

                // locate next form element
                if (input.form.elements) {

                    for (var i = 0; i < input.form.elements.length; i++) {

                        // we found the current input
                        if (input.form.elements[i] == input) {

                            // move on to the next input, and focus it
                            var nextinput = input.form.elements[++i];
                            if (typeof(nextinput) != 'undefined') nextinput.focus();

                            break;
                        }
                    }
                }
            }
        }
    }
}

function onToggle(e)
{
    if (e) {
        var target = getTarget(e);
        toggle(target);
    }
}

function toggle(node)
{
    if (node) {
        var sibling = node.nextSibling;
        while (sibling) {
            if (sibling.nodeType == 1) {
                if (sibling.nodeName.toLowerCase() == 'dt') {
                    break;
                } else {
                    if (sibling.style.display == 'undefined' || sibling.style.display == 'none') {
                        sibling.style.display = 'block';
                        node.style.backgroundImage = 'url(/images/a_3hoek_down.gif)';
                    } else {
                        sibling.style.display = 'none';
                        node.style.backgroundImage = 'url(/images/a_3hoek.gif)';
                    }
                }
            }
            sibling = sibling.nextSibling;
        }
    }
}

function toggleOptions()
{
    var options = document.getElementById('options');
    options.style.display = (options.style.display == 'undefined' || options.style.display == 'none') ? 'block' : 'none';
}

function closeList(id)
{
    var e = document.getElementById(id);

    if (e) {
        for (var i = 0; i < e.childNodes.length; i++) {
            if (e.childNodes[i].nodeName.toLowerCase() == 'dt') {
                toggle(e.childNodes[i]);
            }
        }
    }
}

function printWindow()
{
    window.print();
}

function onSwapImg(e)
{
    var target = getTarget(e);
    swapImg(target.properties.element_id, target.properties.img_src);
}

function swapImg(id, newSrc) {
    if (id != '' && newSrc != '') {
        e = document.getElementById(id);
        if (e) {
            e.setAttribute('src', newSrc);
        }
    }
}
function getCookie(name)
{
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) return null;
    var end = document.cookie.indexOf(";", len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}

function initBillboard()
{
    if (billboardArgs) {
        var id = billboardArgs[0].id;

        // check if cookie has been set
        var cookie = getCookie('hide_' + id);
        if (cookie) return;

        if (id) {

            var node = document.getElementById(id);
            billboardArgs.push(node);
            openBillboard(node.parentNode);
            swfobject.createSWF.apply(swfobject, billboardArgs);
        }
    }
}

function openBillboard(node)
{
    if (node) {
        node.className = node.className.replace(/nodisplay/, '');
    }
}

function closeBillboard(id)
{
    var node = document.getElementById(id);
    if (node) {
        node.parentNode.className += ' nodisplay';
    }
}

function closeBillboardForever(id)
{
    closeBillboard(id);

    // set expiration date for cookie
    var date = new Date();
    date.setDate((date.getDate() + 30)); // set to 30 days

    // set cookie
    var cookiestring = 'hide_' + id + '=1; expires=' + date;
    document.cookie = cookiestring;
}

function displayTab(tabname)
{
    var tab = document.getElementById('overview-tab-content');
    tab.style.display = 'none';
    var tab = document.getElementById('description-tab-content');
    tab.style.display = 'none';
    var tab = document.getElementById('specifications-tab-content');
    tab.style.display = 'none';
    var tab = document.getElementById('photos-tab-content');
    tab.style.display = 'none';

    var tab = document.getElementById(tabname+'-content');
    tab.style.display = 'block';
    setSelectedTab(tabname);
}

function setSelectedTab(tabname)
{
    var tab = document.getElementById('overview-tab');
    tab.className = '';
    var tab = document.getElementById('description-tab');
    tab.className = '';
    var tab = document.getElementById('specifications-tab');
    tab.className = '';
    var tab = document.getElementById('photos-tab');
    tab.className = '';

    var tab = document.getElementById(tabname);
    tab.className = 'selected';
}

function displaySearchform(selectedSearchformId, disabledSearchformId, selectedTabId, disabledTabId)
{
    if (document.getElementById(selectedSearchformId)) {
        var selectedSearchform = document.getElementById(selectedSearchformId);
        selectedSearchform.style.display = 'block';
    }
    if (document.getElementById(selectedTabId)) {
        var selectedTab = document.getElementById(selectedTabId);
        selectedTab.className = 'selected';
    }

    if (document.getElementById(disabledSearchformId)) {
        var disabledSearchform = document.getElementById(disabledSearchformId);
        disabledSearchform.style.display = 'none';
    }
    if (document.getElementById(disabledTabId)) {
        var disabledTab = document.getElementById(disabledTabId);
        disabledTab.className = '';
    }
}
