function onPage(obj, location){
    var page = obj.options[obj.selectedIndex].value;
    window.location = location + '&page=' + page;
}

function f_disable(fields){
    for (i = 0; i < fields.length; i++) {
		var field = $(fields[i]);
        if (field){
            field.disabled = true;
			if(field.hasClassName('required')){
				field.require = true;
				field.removeClassName('required');
			}
		}
    }
}

function f_enable(fields){
    for (i = 0; i < fields.length; i++) {
		var field = $(fields[i]);
        if (field){
            field.disabled = false;
			if(field.require)
				field.addClassName('required');
		}
    }
}

function initTabs(prefix){
    this.prefix = prefix;
    var res = $$('.' + prefix + '_panel');
    if (!res.length) 
        return;
    var html = '';
    panels = [];
	var title = $('title');
	var th;
	if(title.innerHTML){
		title.setStyle({height:'auto'});
		th = title.getHeight();
	}else{
		title.remove();
		th = 0;
	}

    var h = $('body').getHeight() - 64 - th - 54; // 23 -tab control height, 30 - tab padding

    var outerbp = $('outer_bottom_panel');
    if (outerbp) {
        var outer_h = outerbp.getHeight();
        h = h - outer_h;
    }
    for (i = 0; i < res.length; i++) {
		var h1;
        if (res[i].title) 
            html += '<div class="tab" onClick="tab(\'' + i + '\', \'' + prefix + '\')" id="pc' + i + '"><div>' + res[i].title + '</div></div>';
        panels[i] = res[i];
        if (i > 0) 
            res[i].hide();
        
        var inner = res[i].down('.inner_bottom_panel');
        if (inner && inner != '') {
            inner.setStyle({
                position: 'absolute',
                bottom: '20px',
                left: '202px',
                width: $('tb_content').getWidth() + 'px'
            });
			
            res[i].setStyle({
                marginBottom: '41px'
            });
            h1 = h - 41;
        }else{
			h1 = h - 2;
		}
        
        res[i].setStyle({
            height: h1 + 'px',
            padding: '13px'
        });
    }
    if ($(prefix + '_button')) 
        $(prefix + '_button').onclick = function(){
            tab(false, prefix);
        }
    $('tab_control').innerHTML = html + '<div style="clear:left; height:0px"></div>';
    current_tab = 0;
    $('pc0').className = 'tab_active';
}

function tab(nr, prefix){

    if (!nr) {
        nr = parseInt(current_tab) + 1;
    }
    
    if (nr == current_tab) 
        return;
    
    panels[current_tab].toggle();
    $('pc' + current_tab).className = 'tab';
    panels[nr].toggle();
    current_tab = nr;
    $('pc' + nr).className = 'tab_active';
    var button = $(prefix + '_button');
    if (!button) 
        return;
    if (nr == panels.length - 1) {
        button.value = 'Senden';
        button.onclick = function(evt){
            if (wFORMS.formValidation(evt)) {
                document.forms['myform'].submit();
            }
        }
    }
    else {
        button.value = 'weiter';
        button.onclick = function(){
            tab(false, prefix);
        }
    }
}

function initCalendar(){
    var cal = $$('.calendar');
    if (!cal) 
        return;
    var calendar = new CalendarPopup('_calendar');
    calendar.setCssPrefix("TEST");
    for (i = 0; i < cal.length; i++) {
        cal[i].onclick = function(e){
            if (e) 
                var target = e.target;
            else 
                var target = event.srcElement;
            
            if (!target.id) 
                target.id = 'rnd' + Math.ceil(Math.random() * 1000);
            calendar.select(target, target.id, 'dd.MM.yyyy')
        }
    }
    initCalendarMonth();
}

function initCalendarMonth(){

    var cal = $$('.calendarMonth');
    if (!cal) 
        return;
    
    var calen = new Array();
    
    for (i = 0; i < cal.length; i++) {
        if (cal[i].readOnly) 
            continue;
        
        calen[i] = new CalendarPopup('_calendar');
        calen[i].setDisplayType("month");
        calen[i].setReturnMonthFunction('ReturnMonth');
        calen[i].showYearNavigation();
        calen[i].setCssPrefix("TEST");
        // calen[i][i] = cal[i];
        cal[i].no = i;
        cal[i].onclick = function(e){
            if (e) 
                var target = e.target;
            else 
                var target = event.srcElement;
            
            if (!target.id) 
                target.id = 'rnd' + Math.ceil(Math.random() * 1000);
            calen[this.no].showCalendar(target.id);
        }
    }
}

function ReturnMonth(y, m){
    $(input).value = m + "/" + y;
}

function goto(linkas){
	if(!linkas) return;
    window.location = linkas;
}

function createWin(width, title, options){
	
	if($('winclose'))
	$('winclose').onclick();
	
	if(!options)
	var options = {};
	var html = '';
	var container = 'body';
	if(options.modal != false)
		html = '<div id="winmodal"></div>';
		
	if(options.fromEl)
		container = $(options.fromEl).up();
	
    $(container).insert(html+'<div id="winbody" style="position:absolute"><div id="wintitle"><div id="wintitlecontent">' + title + '</div><div id="winclose">X</div><div style="clear:left"></div></div><div id="wincontent"></div></div>');

    var win = $('winbody');
    
    var wintitle = $('wintitle');
    var closeicon = $('winclose');
    closeicon.onclick = function(){
        selectStart();
        var win = $('winbody');
		if(win.fromEl){
			$(win.fromEl).update($('wincontent').down());
		}
        win.update('');
        win.remove();
		if($('winmodal'))
        $('winmodal').remove();
    }
    win.setStyle({
        width: width + 'px'
    });

    win.setStyle({
        left: parseInt(($('body').getWidth() - win.getWidth()) / 2) + 'px',
        top: parseInt((($('body').getHeight() - win.getHeight()) / 3) + document.viewport.getScrollOffsets().top) + 'px'
    });
    //selectStop();
    
    dragGofn = dragGo.bindAsEventListener();
    dragStopfn = dragStop.bindAsEventListener();
    
    Event.observe(wintitle, 'mousedown', startDrag.bindAsEventListener(), true);
	
	if(options.fromEl){
		$('wincontent').update($(options.fromEl).down());
		win.fromEl = options.fromEl;
	}else if(options.html){
		$('wincontent').update(options.html);
	}
}

function loadWin(width, title, url, options){
	
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(transport){
			createWin(width, title, options);
			if(transport.responseText == 0)
				$('wincontent').update('Error loading content. Please check the Internet connection');
			else
				$('wincontent').update(transport.responseText);
				
			if(options.callback)
				options.callback(options);
		}
    });
    
}

function selectStop(){
    document.onselectstart = function(){
        return false;
    }
    document.onmousedown = function(){
        return false;
    }
    document.onselectstart = function(){
        return false;
    }
    document.onmousemove = function(){
        return false;
    }
}

function selectStart(){
    document.onselectstart = function(){
        return true;
    }
    document.onmousedown = function(){
    }
    document.onselectstart = function(){
    }
    document.onmousemove = function(){
    }
}

function startDrag(event){
    dragObj = $('winbody');
    $('wincontent').hide();
    dragObj.setOpacity(0.5);
    
    cursorStartX = Event.pointerX(event);
    cursorStartY = Event.pointerY(event);
    elementStartX = parseInt(dragObj.getStyle('left'), 10);
    elementStartY = parseInt(dragObj.getStyle('top'), 10);
    
    Event.observe($('body'), 'mousemove', dragGofn);
    Event.observe(dragObj, 'mouseup', dragStopfn);
}

function dragGo(event){
    if (dragObj) {
        var x, y;
        x = Event.pointerX(event);
        y = Event.pointerY(event);
        dragObj.style.left = (x - cursorStartX + elementStartX) + "px";
        dragObj.style.top = (y - cursorStartY + elementStartY) + "px";
    }
    Event.stop(event);
}

function dragStop(event){
    $('wincontent').show();
    dragObj.setOpacity(1);
    // Stop capturing mousemove and mouseup events.
    Event.stopObserving(dragObj, 'mousemove', dragGofn);
    Event.stopObserving(dragObj, 'mouseup', dragStopfn);
    dragObj = null;
    Event.stop(event);
}

function showMessage(width, title, content){
	createWin(width, title);
	$('wincontent').update(content);
}

function autoSum(fields, result, prefix){
	var sum = 0;
	if(!prefix)
	prefix = '';
	for(var i=0; i<fields.length; i++){
		var f = $(fields[i]);
		if(!f) continue;
		
		var value = parseInt(f.value);
		if(value)
		sum += value;
		if(!f.fields){
			f.fields = fields;
			f.result = result;
			f.pref = prefix;
			f.onblur = function(){
				autoSum(this.fields, this.result, this.pref);
			}
		}
	}
	$(result).innerHTML = sum+' '+prefix;
	$('p19').value = sum;
}

