window.addEvent('domready', domready);

function initPortalnews() {
    myObjects.portalnews = $('portalnews');
    if (myObjects.portalnews){
        myLocation.portalnews_url = base_url + '/feed_news.php?domain=' + myLocation.portalnews_domain;
        myObjects.ajaxPortalnews = new Ajax(myLocation.portalnews_url, {method: 'get', onComplete: function(){ajaxPortalnewsComplete();}});
        myObjects.ajaxPortalnews.request();
    }
}

function ajaxPortalnewsComplete(){
    var months = new Object();
    months.Jan = '01';
    months.Feb = '02';
    months.Mar = '03';
    months.Apr = '04';
    months.May = '05';
    months.Jun = '06';
    months.Jul = '07';
    months.Aug = '08';
    months.Sep = '09';
    months.Oct = '10';
    months.Nov = '11';
    months.Dec = '12';
    if (window.ie){
        var xmlObj = new ActiveXObject('Msxml2.DOMDocument.3.0');
        xmlObj.loadXML(myObjects.ajaxPortalnews.response.text);
    }else{
        var xmlObj = myObjects.ajaxPortalnews.response.xml;
    }
    var items = xmlObj.getElementsByTagName('item');
    if (items.length > 0){
        myObjects.portalnews.ul = myObjects.portalnews.appendChild(new Element('ul', {'class':'overview'}));
    }
    for (i=0;i<items.length;i++){
        try {
            var item = items[i];
            var itemTitle = item.getElementsByTagName('title')[0].childNodes[0].nodeValue;
            var itemLink = item.getElementsByTagName('link')[0].childNodes[0].nodeValue;
            try {
                var itemPubDate = item.getElementsByTagName('pubDate')[0].childNodes[0].nodeValue;
                var dateString = itemPubDate.split(',')[1].trim();
                dateArray = dateString.split(' ');
                day = dateArray[0];
                month = months[dateArray[1]];
                year = dateArray[2];
                dateString = day + '.' + month + '.' + year
                var itemDateHtml = '<span class="date">' + dateString + '</span>';
            } catch(e) {
                var itemDateHtml = '';
            }
            try {var itemDescription = item.getElementsByTagName('description')[0].childNodes[0].nodeValue;}catch(e){var itemDescription = null;}
            
            var entity = new Element('li', {'class':'entity'}).injectInside(myObjects.portalnews.ul);
            var entityHeadline = new Element('h3').injectInside(entity);
            entityHeadline.innerHTML = '<a href="' + itemLink + '">' + itemDateHtml + itemTitle + '</a>';
            if (itemDescription){
                var entityDescription = new Element('p', {'class':'description'}).injectInside(entity);
                entityDescription.innerHTML = itemDescription;
            }
        } catch (e){}
    }
}

function resize() {
    if (windowdomready){
        if (myObjects.quicklinks){
            var toggleOut = false;
            myObjects.quicklinks.size = myObjects.quicklinks.getSize();
            addInt = 19;
            if (window.ie){
                addInt = 23;
            }
            if (myObjects.teaserMap){
                myObjects.teaserMap.coordinates = myObjects.teaserMap.getCoordinates();
                myObjects.teaserMapContent.coordinates = myObjects.teaserMapContent.getCoordinates();
                if (myObjects.teaserMapContent.coordinates['height'] > myObjects.teaserMap.coordinates['height']){
                    toggleOut = true;
                }else{
                    myObjects.teaserMap.map.coordinates = myObjects.teaserMap.map.getCoordinates();
                    if (myObjects.quicklinks.out && (myObjects.teaserMap.coordinates['height'] - myObjects.teaserMap.map.coordinates['height'] - myObjects.quicklinks.size['scrollSize']['y'] - addInt) <= 0){
                        toggleOut = true;
                    }
                }
            }
            if (myObjects.teaserLocation){
                myObjects.teaserLocation.coordinates = myObjects.teaserLocation.getCoordinates();
                myObjects.teaserLocationContent.coordinates = myObjects.teaserLocationContent.getCoordinates();
                if (myObjects.teaserLocationContent.coordinates['height'] > myObjects.teaserLocation.coordinates['height']){
                    toggleOut = true;
                }else{
                    if (myObjects.quicklinks.out && (myObjects.teaserLocation.coordinates['height'] - myObjects.teaserLocationContent.coordinates['height'] - myObjects.quicklinks.size['scrollSize']['y'] - addInt) <= 0){
                        toggleOut = true;
                    }
                }
            }
            if (toggleOut){
                myObjects.containerQuicklinks.headline.addClickHandler();
                toggleQuicklinks('out');
            }else{
                myObjects.containerQuicklinks.headline.removeClickHandler();
                toggleQuicklinks('in');
            }
        }
        
        initReceiverLinks();
        initJobLinks();
        
        initPortalnews();
    }
}

function domready() {
    windowdomready = true;
    myObjects = new Object();
    initNav();
    initTeaser();
    initQuicklinks();
    initTitleimages();
    initTips();
    initLocationslist();
    initMap();
    initForm();
    window.addEvent('resize', resize);
    resize();
}


function initNav() {
    myObjects.nav = $('nav');
    myObjects.nav.items = $$('#nav li ul');
    myObjects.nav.items.each(function(item){
        new initNavItemMenu(item);
    });
    myObjects.quicknav = $('quicknav');
    if (myObjects.quicknav){
        myObjects.quicknav.items = $$('#quicknav li ul');
        myObjects.quicknav.items.each(function(item){
            new initNavItemMenu(item);
        });
    }
}

function initNavItemMenu(navItemMenu) {
    var navItemMenuChildren, navItem, navItemSlideFx, navItemSlideObj, navItemCoords , navItemA, e, fxDuration;
    if (navItemMenu){
        navItemMenuChildren = navItemMenu.getChildren();
        fxDuration = navItemMenuChildren.length * 70;
        navItemSlideFx = new Fx.Slide(navItemMenu, {duration:fxDuration});
        navItemMenu.style.visibility = 'visible';
        navItemSlideObj = navItemMenu.getParent();
        navItem = navItemSlideObj.getParent();
        navItemA = $(navItem.getChildren()[0]);
        navItem.addEvent('mouseenter', function(event){
            e = new Event(event);
            navItemA.addClass('hover');
            navItemSlideFx.stop();
            navItemCoords = navItemMenu.getCoordinates();
            navItemSlideObj.style.width = navItemCoords.width + 'px';
            navItemSlideFx.slideIn();
            e.stop();
        });
        navItem.addEvent('mouseleave', function(event){
            e = new Event(event);
            navItemA.removeClass('hover');
            navItemSlideFx.stop();
            navItemSlideFx.slideOut();
            e.stop();
        });
    }
}

function initTeaser() {
    myObjects.teaser = $('teaser');
    initTeaserMap();
    initTeaserLocation();
}

function initTeaserMap() {
    myObjects.teaserMap = $('teaser_map');
    if (myObjects.teaserMap){
        myObjects.teaserMapContent = $('teaser_map_content');
        myObjects.teaserMap.headline = $('teaser_headline_map');
        myObjects.teaser_mapLocations = $('teaser_mapLocations');
        new initTeaserMapMenu(myObjects.teaser_mapLocations);
        myObjects.teaserMap.map = myObjects.teaserMap.getElement('img.teaser_map_map');
        myObjects.teaserMap.effect = new Fx.Elements([myObjects.teaserMap,myObjects.teaserMap.headline]);
        myObjects.teaserMap.mapBack = $('map_back');
        if (myObjects.teaserMap.mapBack){
            myObjects.teaserMap.mapBack.onclick = function(){switchTeaser('location');return false;}
        }
        myObjects.teaserMap.mapInfos = new Object();
        myObjects.teaserMap.current_mapInfo = null;
        myObjects.teaserMap.icons = $$('#teaser_map img.icon_on_teaserMap');
        myObjects.teaserMap.tips = new Tips(myObjects.teaserMap.icons, {'offsets':{'x':17,'y':-2}})
        myObjects.teaserMap.icons.each(function(item){
            new initTeaserMapIcon(item);
        });
    }
}

function initTeaserMapMenu(mapItemMenu) {
    var mapItemMenuChildren, mapItem, mapItemSlideFx, mapItemSlideObj, mapItemA, e, fxDuration;
    if (mapItemMenu){
        mapItemMenuChildren = mapItemMenu.getChildren();
        fxDuration = mapItemMenuChildren.length * 70;
        mapItemSlideFx = new Fx.Slide(mapItemMenu, {duration:fxDuration, onComplete:function(){mapItemSlideFxComplete();}});
        mapItemSlideFx.hide();
        mapItemSlideFx.slideIn = function(mode){
            this[mode || this.options.mode]();
            this.start([this.element.getStyle(this.margin).toInt(), this.wrapper.getStyle(this.layout).toInt()], [0, myObjects.teaserMap.offsetHeight-2]);
        }
        mapItemSlideObj = mapItemMenu.getParent();
        mapItem = mapItemSlideObj.getParent();
        mapItemA = $(mapItem.getChildren()[0]);
        mapItem.onclick = function(){clickIn();};
        function clickIn() {
            mapItemA.addClass('hover');
            mapItemSlideFx.stop();
            mapItemSlideObj.setStyle('border', '1px solid #004731');
            mapItemSlideObj.setStyle('borderTop', '0');
            mapItemSlideFx.slideIn();
            mapItem.onclick = function(){clickOut();};
        }
        function clickOut() {
            mapItemA.removeClass('hover');
            mapItemSlideFx.stop();
            mapItemSlideFx.slideOut();
            mapItem.onclick = function(){clickIn();};
        }
        function mapItemSlideFxComplete() {
            if (mapItemA.hasClass('hover')){
                
            }else{
                mapItemSlideObj.setStyle('border', '0');
            }
        }
    }
}

function initTeaserMapIcon(icon) {
    var id = getId(icon);
    myObjects.teaserMap.mapInfos[id] = $('teaserMapInfo_' + id);
    myObjects.teaserMap.mapInfos[id].effects().set({'opacity':[0]});
    myObjects.teaserMap.mapInfos[id].style.visiblity = 'visible';
    document.body.appendChild(myObjects.teaserMap.mapInfos[id]);
    $('teaserMapInfo_close_' + id).addEvent('click', hide);
    icon.addEvent('click', function(event) {show(event);});
    function show(event) {
        var e = new Event(event);
        if (myObjects.teaserMap.current_mapInfo) {
            if (myObjects.teaserMap.current_mapInfo == myObjects.teaserMap.mapInfos[id]){
                hide();
                return;
            }else{
                hide();
            }
        }
        myObjects.teaserMap.current_mapInfo = myObjects.teaserMap.mapInfos[id];
        myObjects.teaserMap.current_mapInfo.style.display = 'block';
        myObjects.teaserMap.current_mapInfo.style.top = (e.page.y - myObjects.teaserMap.current_mapInfo.offsetHeight) + 'px';
        myObjects.teaserMap.current_mapInfo.style.left = (e.page.x - 330) + 'px';
        myObjects.teaserMap.current_mapInfo.effects({duration:400}).start({'opacity':[1]});
    }
    function hide() {
        myObjects.teaserMap.current_mapInfo.effects({duration:400}).start({'opacity':[0]});
        myObjects.teaserMap.current_mapInfo = null;
    }
    function getId(icon) {
        return icon.id.split('_').getLast();
    }
}

function initTeaserLocation() {
    myObjects.teaserLocation = $('teaser_location');
    if (myObjects.teaserLocation){
        myObjects.teaserLocationContent = $('teaser_location_content');
        myObjects.teaserLocation.headline = $('teaser_headline_location');
        myObjects.teaserLocation.effect = new Fx.Elements([myObjects.teaserLocation, myObjects.teaserLocation.headline]);
        myObjects.map_small = $('map_small');
        if (myObjects.map_small){
            myObjects.map_small.onclick = function(){switchTeaser('map');return false;}
            myObjects.teaserLocation.not_responsibles = $$('#teaser_location_content .not_responsible');
            myObjects.teaserLocation.not_responsibles.each(function(item){
                item.effects({duration:600}).start({'opacity':[0.4]});
                item.addEvent('mouseenter', function(){item.effects({duration:600}).start({'opacity':[1]})});
                item.addEvent('mouseleave', function(){item.effects({duration:600}).start({'opacity':[0.4]})});
            });
        }
    }
}

function switchTeaser(what) {
    myObjects.teaserLocation.effect.stop();
    myObjects.teaserMap.effect.stop();
    if (what=='map'){
        myObjects.teaserLocation.effect.start({'0':{'opacity':[0]},'1':{'opacity':[0]}});
        myObjects.teaserMap.effect.start({'0':{'opacity':[1]},'1':{'opacity':[1]}});
    }else{
        myObjects.teaserMap.effect.start({'0':{'opacity':[0]},'1':{'opacity':[0]}});
        myObjects.teaserLocation.effect.start({'0':{'opacity':[1]},'1':{'opacity':[1]}});
    }
}

function initQuicklinks() {
    myObjects.containerQuicklinks = $('container_quicklinks');
    if (myObjects.containerQuicklinks) {
        myObjects.containerQuicklinks.headline = myObjects.containerQuicklinks.getElement('h1');
        myObjects.quicklinks = $('quicklinks');
        myObjects.quicklinks.out = false;
        myObjects.quicklinks.toggleEffect = new Fx.Elements([myObjects.containerQuicklinks,myObjects.quicklinks,myObjects.teaser]);
        myObjects.containerQuicklinks.headline.addClickHandler = function(){
            myObjects.containerQuicklinks.headline.style.cursor = 'pointer';
            myObjects.containerQuicklinks.headline.onclick = function() {
                toggleQuicklinks();
            };
        }
        myObjects.containerQuicklinks.headline.removeClickHandler = function(){
            myObjects.containerQuicklinks.headline.onclick = '';
            myObjects.containerQuicklinks.headline.style.cursor = '';
        }
    }
}

function toggleQuicklinks(what) {
    myObjects.containerQuicklinks.headline.coordinates = myObjects.containerQuicklinks.headline.getCoordinates();
    myObjects.quicklinks.size = myObjects.quicklinks.getSize();
    if (!what){
        if (myObjects.quicklinks.out){what='in';}else{what='out';}
    }
    if (what=='out'){
        if (!myObjects.quicklinks.out){
            myObjects.quicklinks.out = true;
            var addInt = 0;
            if (window.ie6){
                var addInt = 4;
            }
            myObjects.quicklinks.toggleEffect.start({'0':{'height':[myObjects.containerQuicklinks.headline.coordinates['height']]},'1':{'opacity':[0]},'2':{'bottom':[myObjects.containerQuicklinks.headline.coordinates['height'] + addInt]}});
        }
    }else{
        if (myObjects.quicklinks.out){
            myObjects.quicklinks.out = false;
            var addInt = 9;
            if (window.ie6){
                var addInt = 13;
            }
            myObjects.quicklinks.toggleEffect.start({'0':{'height':[myObjects.containerQuicklinks.headline.coordinates['height'] + myObjects.quicklinks.size['scrollSize']['y'] + 9]},'1':{'opacity':[1]},'2':{'bottom':[myObjects.containerQuicklinks.headline.coordinates['height'] + myObjects.quicklinks.size['scrollSize']['y'] + addInt]}});
        }
    }
}

function initTitleimages() {
    myObjects.containerTitleimagesSmall = $('container_titleimages_small');
    myObjects.containerTitleimages = $('container_titleimages');
    myObjects.titleimages = myObjects.containerTitleimages.getElements('img');
    myObjects.titleimages[0].loaded = true;
    myObjects.current_titleimage_index = 0;
    myObjects.containerTitleimagesSmall.getElements('a').each(function(item){
        if (myObjects.titleimages.filter(function(myItem, index){return myItem.src == item.href;}).length == 0){
            var myImage = new Asset.image(item.href);
            myImage.style.visibility = 'hidden';
            myObjects.titleimages.push(myImage);
            myImage.injectInside(myObjects.containerTitleimages);
            myImage.loaded = false;
            myImage.onload = function(){this.loaded = true;this.effects().set({'opacity':[0]});};
        }
    });
    window.setTimeout(rotateTitleimages,8000);
}

function rotateTitleimages(){
    if (myObjects.titleimages.length>1){
        var next_titleimage_index = myObjects.current_titleimage_index + 1;
        if (next_titleimage_index > myObjects.titleimages.length-1){
            next_titleimage_index = 0;
        }
        if (myObjects.titleimages[next_titleimage_index].loaded){
            myObjects.titleimages[myObjects.current_titleimage_index].effects({duration:1000}).start({'opacity':[0]});
            myObjects.current_titleimage_index = next_titleimage_index;
            myObjects.titleimages[myObjects.current_titleimage_index].effects({duration:1000}).start({'opacity':[1]});
            window.setTimeout(rotateTitleimages,8000);
        }else{
            window.setTimeout(rotateTitleimages,1000);
        }
    }
}

function initTips(){
    myObjects.definitionLinks = $$('a.definitionlink');
    myObjects.definitionTips = new Tips(myObjects.definitionLinks,
        {
            initialize:function() {
                this.toolTip.effects({wait:false}).set({'opacity':[0]});
            },
            onShow:function(toolTip) {
                toolTip.effects().stop();
                toolTip.effects({duration:200, wait:false}).start({'opacity':[1]});
            },
            onHide:function(toolTip) {
                toolTip.effects({duration:200, wait:false, onComplete:function(toolTip){
                    toolTip.setStyles({
                            'display': 'none'
                    });
                }}).start({'opacity':[0]});
            },
            className:'definitioninside',
            fixed:true
        }
    );
    myObjects.definitionTips.position = function(element) {
        this.toolTip.setStyles({'display':'block', 'top':0, 'left':0, 'width':'auto','height':'auto'});
        element.position = element.getPosition();
        
        this.toolTip.leftSided = (window.getWidth() - element.position.x) < element.position.x;
        this.toolTip.maxWidth = this.toolTip.leftSided ? (element.position.p - element.offsetWidth - 50) : (window.getWidth() - element.position.x - element.position.offsetWidth - 50);
        this.toolTip.coordinates = this.toolTip.getCoordinates();
        if (this.toolTip.coordinates.width > this.toolTip.maxWidth){
            this.toolTip.setStyle('width', this.toolTip.maxWidth + 'px');
            this.toolTip.coordinates = this.toolTip.getCoordinates();
        }
        
        if (element.position.y + this.toolTip.coordinates.height + this.options.offsets.y > window.getHeight()){
            this.toolTip.setTop = window.getHeight() - (this.toolTip.coordinates.height + this.options.offsets.y);
        }else{
            this.toolTip.setTop = element.position.y;
        }
        
        this.toolTip.setStyles({
                'left': element.position.x + element.offsetWidth + this.options.offsets.x,
                'top': this.toolTip.setTop
        });
    };
    myObjects.definitionLinks.each(function(item){
        item.onclick = function(){return false;};
    });
}

function initLocationslist() {
    myObjects.showLocationslist = $('show_locationslist');
    myObjects.locationslist = $('locationslist');
    if (myObjects.locationslist){
        document.body.appendChild(myObjects.locationslist);
        myObjects.locationslist.style.display = 'block';
        myObjects.locationslist.effects({}).set({'opacity':[0]});
        myObjects.locationslist.getElement('a.close').onclick = function() {hideLocationslist(); return false;};
        myObjects.showLocationslist.onclick = function(){showLocationslist(); return false;};
    }
}

function showLocationslist() {
    myObjects.locationslist.effects({duration:600}).start({'opacity':[1]});
}

function hideLocationslist() {
    myObjects.locationslist.effects({duration:600}).start({'opacity':[0]});
}

function initMap() {
    myObjects.map = $$('div.map');
    if (myObjects.map.length > 0){
        myObjects.map = myObjects.map[0];
        myObjects.map.map = myObjects.map.getElement('img.map_map');
        myObjects.map.mapInfos = new Object();
        myObjects.map.current_mapInfo = null;
        myObjects.map.icons = myObjects.map.getElements('img.icon_on_map');
        myObjects.map.tips = new Tips(myObjects.map.icons, {'offsets':{'x':17,'y':-2}})
        myObjects.map.icons.each(function(item){
            new initMapIcon(item);
        });
    }
}
        
function initMapIcon(icon) {
    var id = getId(icon);
    myObjects.map.mapInfos[id] = $('mapInfo_' + id);
    myObjects.map.mapInfos[id].effects().set({'opacity':[0]});
    myObjects.map.mapInfos[id].style.visiblity = 'visible';
    document.body.appendChild(myObjects.map.mapInfos[id]);
    $('mapInfo_close_' + id).addEvent('click', hide);
    icon.addEvent('click', function(event) {show(event);});
    function show(event) {
        var e = new Event(event);
        if (myObjects.map.current_mapInfo) {
            if (myObjects.map.current_mapInfo == myObjects.map.mapInfos[id]){
                hide();
                return;
            }else{
                hide();
            }
        }
        myObjects.map.current_mapInfo = myObjects.map.mapInfos[id];
        myObjects.map.current_mapInfo.style.display = 'block';
        myObjects.map.current_mapInfo.style.top = (e.page.y - myObjects.map.current_mapInfo.offsetHeight - 20) + 'px';
        myObjects.map.current_mapInfo.style.left = e.page.x + 'px';
        myObjects.map.current_mapInfo.effects({duration:600}).start({'opacity':[1]});
    }
    function hide() {
        myObjects.map.current_mapInfo.effects({duration:600}).start({'opacity':[0]});
        myObjects.map.current_mapInfo = null;
    }
    function getId(icon) {
        return icon.id.split('_').getLast();
    }
}

function initReceiverLinks() {
    myObjects.receiverLinks = $$('a.receiver');
    myObjects.receiverLinks.each(function(item){
        item.href += '?receiver=' + encodeURIComponent(item.title);
    });
}

function initJobLinks() {
    myObjects.jobLinks = $$('a.jobLink');
    myObjects.jobLinks.each(function(item){
        item.href += '?job=' + encodeURIComponent(item.title);
    });
}


function initForm() {
    myObjects.fld_receiver = $('fld_receiver');
    if (myObjects.fld_receiver){
        var urlstring,querystring,querystring_items,receiver_value;
        var urlstring = location.href;
        if (urlstring.indexOf('?')>=0){
            querystring = urlstring.split('?')[1];
            if (querystring.indexOf('receiver')>=0){
                if (querystring.indexOf('&amp;')>=0){
                    querystring_items = querystring.split('&amp;');
                    for (var i=0;i<querystring_values.length;i++){
                        if (querystring_values[i].indexOf('=')>=0){
                            var querystring_item = querystring_values[i].split('=');
                            if (querystring_item[0]=='receiver'){
                                receiver_value = querystring_item[1];
                            }
                        }
                    }
                } else {
                    receiver_value = querystring.split('=')[1];
                }
                if (receiver_value.length>0){
                    myObjects.fld_receiver.value = decodeURIComponent(receiver_value);
                }
            }
        }
    }
    
    myObjects.fld_job = $('fld_job');
    if (myObjects.fld_job){
        var urlstring,querystring,querystring_items,job_value;
        var urlstring = location.href;
        if (urlstring.indexOf('?')>=0){
            querystring = urlstring.split('?')[1];
            if (querystring.indexOf('job')>=0){
                if (querystring.indexOf('&amp;')>=0){
                    querystring_items = querystring.split('&amp;');
                    for (var i=0;i<querystring_values.length;i++){
                        if (querystring_values[i].indexOf('=')>=0){
                            var querystring_item = querystring_values[i].split('=');
                            if (querystring_item[0]=='job'){
                                job_value = querystring_item[1];
                            }
                        }
                    }
                } else {
                    job_value = querystring.split('=')[1];
                }
                if (job_value.length>0){
                    myObjects.fld_job.value = decodeURIComponent(job_value);
                }
            }
        }
    }
}

function getAdr(prefix, postfix, text) {
        document.write('<a href="mailto:' + prefix + '@' + postfix + '">' + (text ? text.replace(/&quot;/g, '"').replace(/%EMAIL%/, prefix + '@' + postfix) : prefix + '@' + postfix) + '</a>');
}

function swapImage(element, newimage) {
        var oldsrc = element.src
        element.src = newimage
        if (!element.onmouseout)
                element.onmouseout = function (event) { swapImage(this, oldsrc); };
}

//checkEmail - needed for forms
function checkEmail(val) {
        if (val) {
                var usr = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
                var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
                var regex = "^"+usr+"\@"+domain+"$";
                var myrxp = new RegExp(regex);
                var check = (myrxp.test(val));
                if (check!=true) {
                        return false;
                }
                else {
                        return true;
                }
        }
}

/*
validates formfields if they have a value or not
to check for other options do the following
specialfields = new Object();
specialfields.fieldname = new Object();
specialfields.fieldname.check1 = 'function_to_call,error_message';
specialfields.fieldname.check2 = 'second_function_to_call,second_error_message';
specialfields.another_fieldname = new Object();
specialfields.another_fieldname.check1 = 'function_to_call,error_message';
*/
function validateForm(form,specialfields) {
        var errors = new Array();
        var fields = form.getElementsByTagName('label');
        for (i = 0; i < fields.length; i++) {
                var span = fields[i].getElementsByTagName('span')[0];
                if (span) {
                        var label = span.firstChild.data;
                        label = label.trim();
                        // if there is a '*' in the label - this indicates the inputfield has to be filled
                        if (label.charAt(label.length - 1) == '*') {
                                label = label.substring(0, label.length - 1).trim();
                                // get the inputfield
                                var obj_input = fields[i].getElementsByTagName('input');
                                if (!obj_input[0])
                                        obj_input = fields[i].getElementsByTagName('select');
                                if (!obj_input[0])
                                        obj_input = fields[i].getElementsByTagName('textarea');

                                // if there is an inputfield
                                if (obj_input && obj_input[0]) {
                                        input = obj_input[0];
                                        error = false;
                                        
                                        // check if the inputfield has a value
                                        if (!input.value || input.value.trim().length==0) {
                                                error = true;
                                                if (input.tagName=='SELECT'){
                                                    errors.push(label + ' ' + langstrings['form_errormessage_part_select']);
                                                }
                                                else{
                                                    errors.push(label + ' ' + langstrings['form_errormessage_part']);
                                                }
                                        }
                                        
                                        // check the inputfield for special things (email, ...)
                                        if (!error && specialfields[input.name]){
                                                specialfield = specialfields[input.name];
                                                for (check in specialfield){
                                                        check_function = specialfield[check].split(',')[0];
                                                        check_message = specialfield[check].split(',')[1];
                                                        if (!eval(check_function)(input.value)){
                                                                error = true;
                                                                errors.push(label + ' ' + check_message);
                                                        }
                                                }
                                        }

                                        // on error give the label the className 'error' otherwise delete the className 'error' (if exists)
                                        if (error) {
                                                className = fields[i].className;
                                                if (className.length>0){
                                                        className = className + ' ';
                                                }
                                                fields[i].className = className + 'error';
                                        } else {
                                        className = fields[i].className;
                                                if (className.indexOf('error')>-1){
                                                                className = className.replace(' error', '');
                                                                className = className.replace('error', '');
                                                                fields[i].className = className;
                                                }
                                        }
                                }
                        }
                }
        }

        return errors;
}

function showFormErrors (errors) {
        error_message = '';
        for (i=0;i<errors.length;i++){
                error_message += errors[i] + '\n';
        }
        alert(error_message);
}

function hideSelectBoxes(){
    selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) { selects[i].style.visibility = "hidden";}
}

function showSelectBoxes(){
    selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) { selects[i].style.visibility = "visible";}
}