/**
 * InfoDay javascript file
 * @version $Revision: 1.1.2.3 $
 */

/**
 * Initializes infoday form
 * Appends two select boxes
 * @return void
 */
function initInfodayForm()
{
    var el = document.getElementById('fldlocation_id');
    var dd;
    if (el) {
        dd = el;
        while (dd.tagName.toString().toLowerCase() != 'dd') dd = dd.parentNode;

        // add 'location' form element:
//        location_dt = document.createElement('dt');
//        location_label = document.createElement('label');
//        location_label.setAttribute('for', 'fldlocation');
//        location_label.appendChild(document.createTextNode(window.texts['InfoDayLocation']));
//        location_dt.appendChild(location_label);
//        location_select = document.createElement('select');
//        location_select.setAttribute('name', 'location');
//        location_select.setAttribute('id', 'fldlocation');
//        location_select.onchange = function () { refreshInfoDayForm(document.getElementById('fldlocation')); }
//        option = document.createElement('option');
//        option.setAttribute('value', '');
//        option.appendChild(document.createTextNode(window.texts['InfoDayNoPreference']));
//        location_select.appendChild(option);
//        location_dd = document.createElement('dd');
//        location_dd.appendChild(location_select);
//
//        dd.parentNode.insertBefore(location_dd, dd.nextSibling);
//        dd.parentNode.insertBefore(location_dt, dd.nextSibling);

        // add 'infoday' form element:
        infoday_dt = document.createElement('dt');
        infoday_label = document.createElement('label');
        infoday_label.setAttribute('for', 'fldinfoday_id');
        infoday_label.appendChild(document.createTextNode(window.texts['InfoDayDate']));
        infoday_dt.appendChild(infoday_label);
        infoday_select = document.createElement('select');
        infoday_select.setAttribute('name', 'infoday_id');
        infoday_select.setAttribute('id', 'fldinfoday_id');

        infoday_dd = document.createElement('dd');
        infoday_dd.appendChild(infoday_select);

        dd.parentNode.insertBefore(infoday_dd, dd.nextSibling);
        dd.parentNode.insertBefore(infoday_dt, dd.nextSibling);

        el.onchange = function () { refreshInfoDayForm(document.getElementById('fldlocation_id')); }
        refreshInfoDayForm(el);

//        var url = window.location;
//
//        if (url != '') {
//            var querystring = url.search.substr(1, url.search.length);
//            var queryparts = querystring.split('&');
//            for (var i = 0; i < queryparts.length; i++) {
//                var querypart = queryparts[i].split('=');
//                if (querypart.length == 2) {
//                    if (querypart[0] == 'location') {
//                        var location = querypart[1].toLowerCase();
//
//                        var el = document.getElementById('fldlocation');
//
//                        for (var j = 0; j < el.options.length; j++) {
//                            if (el.options[j].value.toLowerCase() == location) {
//                                el.selectedIndex = j;
//
//                                refreshInfoDayForm(el);
//                                break;
//                            }
//                        }
//
//                        break;
//                    }
//                }
//            }
//        }

    }

}

/**
 * Refreshes the form, updates options etc
 * @param DOMNode The select node that had its selection changed
 * @return void
 */
function refreshInfoDayForm(sel)
{
    if (sel.getAttribute('name') == 'location_id') {
        rebuildInfoDayOptions();
    }
    // check if submit button must be enabled/disabled

    el = document.getElementById('fldinfoday_id');
    if (el) {
        inputs = el.form.getElementsByTagName('input');
        for (var i = 0; i < inputs.length; i++) {
            input = inputs.item(i);
            if (input.getAttribute('type') == 'submit') {
                if ((el.options.length > 0) && (el.options[0].value != '')) {
                    input.removeAttribute('disabled');
                } else {
                    input.setAttribute('disabled', 'disabled');
                }
                break;
            }
        }
    }
}

/**
 * Rebuild the location options
 * @return void
 */
function rebuildLocationOptions()
{
    var curvalue = '';

    var el = document.getElementById('fldlocation');
    if (el) {

        // remember current selection
        curvalue = el.options[el.selectedIndex].value;

        // filter value
        ptitle = document.getElementById('fldptitle').options[document.getElementById('fldptitle').selectedIndex].value;

        // delete current options (but the first)
        while (el.options.length > 1) el.options[el.options.length - 1] = null;

        var locations = new Array();
        var exists;
        for(var i = 0; i < window.infodays.length; i++) {
            if (window.infodays[i].continent == ptitle) {
                tmp_location = window.infodays[i].location;
                exists = false;
                for(var j = 0; j < locations.length; j++) {
                    if (locations[j] == tmp_location) {
                        exists = true;
                        break;
                    }
                }
                if (exists == false) {
                    locations.push(tmp_location);
                }
            }
        }

        locations.sort();
        for (var i = 0; i < locations.length; i++) {
            option = document.createElement('option');
            option.setAttribute('value', locations[i]);
            if (curvalue == locations[i]) option.setAttribute('selected', 'selected');
            option.appendChild(document.createTextNode(locations[i]));
            el.appendChild(option);
        }
    }
}

/**
 * Rebuild the infoday options
 * @return void
 */
function rebuildInfoDayOptions()
{
    var curvalue = 0;
    var el = document.getElementById('fldinfoday_id');
    if (el) {

        // remember current selection
        if (el.selectedIndex > -1) {
            curvalue = el.options[el.selectedIndex].value;
        }

        // delete current options
        while (el.options.length > 0) el.options[el.options.length - 1] = null;

        // filter values
//        ptitle = document.getElementById('fldptitle').options[document.getElementById('fldptitle').selectedIndex].value;
        curlocation = document.getElementById('fldlocation_id').options[document.getElementById('fldlocation_id').selectedIndex].value;

        var items = new Object();
        for(var i = 0; i < window.infodays.length; i++) {
            if ((curlocation == window.infodays[i].location_id)) {
                items[window.infodays[i].date] = window.infodays[i].date_title;
            }
        }

        var i = 0;
        for (e in items) {
            i = 1;
            option = document.createElement('option');
            option.setAttribute('value', e);
            option.appendChild(document.createTextNode(items[e]));
            el.appendChild(option);
        }
        if (i == 0) {
            option = document.createElement('option');
            option.setAttribute('value', '');
            option.appendChild(document.createTextNode(window.texts['InfoDayNoDatesFound'])); // TODO move to language array
            el.appendChild(option);
        }
    }
}

/**
 * InfoDay class constructor
 * @param String location_id The location_id
 * @param String date The date
 * @param String date_title The date title
 * @return void
 */
function InfoDay(location_id, date, date_title)
{
    this.location_id = location_id;
    this.date = date;
    this.date_title = date_title;
}
