/*
 *
 *Utility functions
 **/
function get_left(ele){
    return ele.cumulativeOffset()[0];
    
    /*
    if (ele.offsetParent)
        return ele.offsetLeft + get_left(ele.offsetParent);
    else
        return ele.offsetLeft;*/
}

function get_top(ele){
    return ele.cumulativeOffset()[1];
    /*if (ele.offsetParent)
        return (ele.offsetTop + get_top(ele.offsetParent));
    else
        return (ele.offsetTop);*/
}

function toggle_checkboxes(element, prefix, form){
    f = form || element.form;
    inputs = f.getInputs('checkbox').reject(function(i){return i.name.indexOf(prefix)!=0 });
    inputs.each(function(i){ i.checked = element.checked });
}

function togggle_single_checkbox(element, c_id){
    check_box = $(c_id);
    check_box.checked = element.checked
}

/*
 *Open an image in a popup window
 **/
function image_popup(domElement, url){ 
  
    var myImage = new Image();
    myImage.name =url;
    myImage.onload = open_window;
    myImage.src = url;
  
    /*/var x = get_left(domElement) - (width/2);
  if (x<0){ x=0}
  var y = get_left(domElement) - (height/2);
  if (y<0){ y=0}*/
}

function open_window(){    
    
    x = this.width+20;
    y = this.height+20;
    imgwindow= window.open(this.name, "imgwindow","menubar=0,toolbar=0,resizable=0,location=0,status=0,scrollbars=0,width="+x+",height="+y);  
  
    dx = 800 - this.width;
    if (dx<0) dx=0;
    dy = 600 - this.height;
    if (dy<0) dy=0;
  
    imgwindow.moveTo(dx,dy);
}

function print_window(urll, x, y, dx,dy){
    printwindow= window.open(urll, "printwindow","menubar=1,toolbar=0,resizable=0,location=0,status=0,scrollbars=1,width="+x+",height="+y);  
    printwindow.moveTo(dx,dy);  
}


/*
 *Functions for layered menus
 **/
  
/*Array containing all layers together with their properties like lifetime, etc..*/  
var active_links = [];
var layers = $H({});
//layers that contain links of pages currently contained in the breadcrumb
var breadcrumb_layers = $H({});
var delta_t = 100; //interval between 2 scheduled runs;
  
function set_old_link_inactive(level)  {
    if (active_links[level]){
        $(active_links[level]).className=$(active_links[level]).className.replace(" active_level"+level,"");
    }
}
  
function set_link_active(id, level)  {
    
    set_old_link_inactive(level);
    
    active_links[level] = id;            
    $(id).className=$(id).className+" "+"active_level"+level;    
}
  
function register_layer(id,level)  {
    layers.set(id, {"id":id, "lifetime":0, "level":level});
}

function register_breadcrumb_layer(id,level)  {
    breadcrumb_layers.set(id, {"id":id, "level":level});
}

function show_layer(id, lifetime, parent_id,delta_x, delta_y,level)  {
    parent_element = $(parent_id);
    //alert(delta_y);        
    $(id).style.left = (get_left(parent_element)+delta_x+1+parent_element.getWidth())+"px";
    $(id).style.top = (get_top(parent_element)+delta_y-1)+"px";
    
    if (level==2){        
        $(id).style.left = (delta_x+parent_element.getWidth())+"px";
        
        dy = delta_y-114;
        //if (get_top(parent_element)+dy <0) dy=-114;
        
        $(id).style.top = (get_top(parent_element)+dy)+"px";
    }       
    
    hide_all_layers(level);
    layers.get(id)["lifetime"] = lifetime
    //$(id).show();
    $(id).style.visibility ="visible";            
    
}  

function keep_alive_layer(id, lifetime)  {
    if (layers.get(id)["lifetime"]< lifetime){
        layers.get(id)["lifetime"] = lifetime;
    }
}

function hide_layer(id, lifetime)  {
    layers.get(id)["lifetime"] = lifetime;        
}

function hide_all_layers(level){
    layers.each(function(pair) {        
        layer = pair.value;   
        if (layer.level==level){
            layer.lifetime = 0;
            //$(layer.id).hide();        
            $(layer.id).style.visibility ="hidden";
            //alert(pair.key + ' = "' + pair.value + '"');
        }
    });    
}

//shows layers that ocntain the pages currently contained in the breadcrumb 
function show_breadcrumb_layers(level){
    breadcrumb_layers.each(function(pair) {        
        layer = pair.value;     
        if (layer.level==level){                    
            $(layer.id).style.visibility ="visible";            
        }
    });    
}

//hides layers that ocntain the pages currently contained in the breadcrumb 
function hide_breadcrumb_layers(level){
    breadcrumb_layers.each(function(pair) {        
        layer = pair.value;           
        if (layer.level==level){                    
            $(layer.id).style.visibility ="hidden";            
        }
    });    
}

//hide "expired layers"  
function runner(){    
    
    atleast_one_layer_visible = false;
    
    layers_to_hide = [];
    layers_to_hide_count = 0;
    
    layers.each(function(pair) {        
        layer = pair.value;
        if (layer.lifetime > 0){
            atleast_one_layer_visible = true;
            layer.lifetime -= delta_t;
            if (layer.lifetime <= 0){
                layer.lifetime = 0;
                //$(layer.id).hide();
                //just mark the layers to be hidden
                layers_to_hide[layers_to_hide_count] = layer.id;
                layers_to_hide_count++;
                //$(layer.id).style.visibility ="hidden";
            }
        }
        //alert(pair.key + ' = "' + pair.value + '"');
    });    
    
    if (!atleast_one_layer_visible){
        set_old_link_inactive(0);              
        show_breadcrumb_layers(1);       
        show_breadcrumb_layers(2);       
    }else{
        //hide_breadcrumb_layers();
    }
    
    //now hide the layers that have to be hidden
    for (k=0; k<layers_to_hide_count; k++){
        $(layers_to_hide[k]).style.visibility ="hidden";
    }
    
    
}

setInterval(runner, delta_t);

function process_onload_tasks(){
}

function isValidEmail(str) {
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


function checkFormEntries(form){    
    
    returnvalue = true;
    
    $$('#'+form.id+' .mandatory_input').each(function(mandatory_field){
        if (returnvalue){
            if ($(mandatory_field).name.indexOf("email")>0){
                if (!isValidEmail($(mandatory_field).value)){
                    $(mandatory_field).style.borderColor = "red";
                    alert('Bitte geben Sie eine gültige Email-Adresse ein');
                    $(mandatory_field).focus();
                    returnvalue = false;
                }
            }else{            
                if (($(mandatory_field).value+'').length <=0){
                    $(mandatory_field).style.borderColor = "red";
                    alert('Bitte geben Sie einen gültigen Wert für folgendes Feld ein: '+$(mandatory_field).name.replace(":",""));
                    $(mandatory_field).focus();
                    returnvalue = false;
                }
            }
        }
                
    });        
    return returnvalue;
}

