function markItem( oInput ) {
    return _menu.markItem( oInput );
}

function getFirstChildElm( oElm, sTag ) {
    var elms = getChildElements( oElm, sTag, 1 );
    
    if( elms.length > 0 )
        return elms[0];
        
    return null;
}

function getChildElements( oElm, sTag, max ) {
    var elms = new Array();
    
    if( oElm ) {
        var cn = oElm.childNodes;
        
        for( var i=0; i < cn.length; i++ ) {
            var elm = cn[i];
                       
            if( elm.nodeType == 1 ) {
                if( String(sTag) != "undefined" && String(sTag) != "" && String(sTag) != "null" ) {
                    if( String(elm.nodeName).toLowerCase() == String(sTag).toLowerCase() )
                        elms.push( elm );
                } else
                    elms.push( elm );
            }
            
            if( !isNaN(max) && max != "" )
                if( elms.length >= max )
                    break;
        }
    }
    
    return elms;    
}

function getAllMarkedItems( menu_elm ) {
    var marked_items = new Array();
    
    if( menu_elm ) {
        var inputs = menu_elm.getElementsByTagName("input");
                        
        for( var i=0; i < inputs.length; i++ ) {
            var oInput = inputs[i];
            var oParent = oInput.parentNode;
            var input_type = String(oInput.type).toLowerCase();
            
            if( oParent && input_type == "checkbox" || input_type == "radio" ) {
                if( oInput.checked )
                    marked_items.push( oParent );
            }
        }
    }
    
    return marked_items;
}

function checkMarkedItems( menu_elm ) {
    if( menu_elm ) {
        var inputs = menu_elm.getElementsByTagName("input");
                        
        for( var i=0; i < inputs.length; i++ )
            markItem( inputs[i] );
    }
}

function checkAllMarkedItems( e ) {            
    checkMarkedItems( document.getElementById("menu") );
}

function debug( str ) {
    document.getElementById("debug").style["dispaly"] = "none";
    document.getElementById("debug").innerHTML += str +"<br />";
}

function changeStyleSheet( css_filename ) {
    if( css_filename ) {
        try {
            var header = document.getElementsByTagName("head")[0];
            var styleNode = header.getElementsByTagName("style")[0];
            var styleObj = ( document.styleSheets )? document.styleSheets[0] : null;
                if( !( styleObj.cssText ) ) styleObj = null;
                
            var innerStyle = ( styleObj !== null )? styleObj.cssText : styleNode.innerHTML;
                innerStyle = String(innerStyle).replace("css/fvr_front.css", "css/"+ css_filename);
           
            if( styleObj === null ) {
                styleNode.innerHTML = innerStyle;
            } else {
                styleObj.cssText = innerStyle;
            }
            
            /*ex = new Object();
            ex.message = styleObj.cssText;
            throw ex;*/
        } catch(ex) {
            alert("Error in switching stylesheet:\r\n\t"+ ex.message);
        }
        
    }
}

function changeClass( oElm, classname ) {
    if( oElm )
        oElm.className = classname;
}

function all_att( elm ) {
    var str = "element unspecified!";
    
    if( elm ) {
        str = new Array();
        
        for( i in elm ) {
            var temp = i +": ";
           
            try {
                temp += String(elm[i]);
            } catch(ex) {
                temp += "?";
            }
           
            str.push( temp );
        }
        str = "-|[ "+ str.join(" ]|-  -|[ ") +" ]|-";
    }
    
    return str;
}

function open_close_folder( oFolder ) {
    if( oFolder ) {
        var isOpen = ( String( oFolder.className ).match(/\bopenfolder\b/) !== null );
        
        if( isOpen )
            oFolder.className = String(oFolder.className).replace( "openfolder", "folder" );// +" folder";            
        else
            oFolder.className = String(oFolder.className).replace( "folder", "openfolder" );// +" openfolder"; 
    }
}

function trim( val ) {
	return String(val).replace(/^[\s|\t]+/m, "").replace(/[\s|\t]+$/m, "");
}

function string_ok( val ) {
	val = trim(val);
	return (val != "undefined" && val != "null" && val != "");
}

function object_ok( obj ) {
	return (string_ok(obj) && typeof(obj) == "object");
}

function is_node( elm ) {
	return (object_ok(elm) && string_ok(elm.nodeType));	
}

function markInput( input_field, err ) {
	setStyle( input_field, "border", ( ( err )? "1px solid #ff0000" : "" ) )
	//input_field.style["border"] = ( err )? "1px solid #ff0000" : "";
}

function setStyle( elm, style_elm, style_val ) {	
	if( is_node(elm) && string_ok(style_elm) ) {
		style_elm = trim(style_elm).toLowerCase();
		var oStyle = elm.style;
		//alert( oStyle.cssText )
		var css = "";
		
		//alert( oStyle.getPropertyCSSValue() )
		
		if( object_ok( oStyle ) )
			css = oStyle.cssText;
		
		var inline_style = style_elm +": "+ style_val +";"
			if( !string_ok( style_val ) )
				inline_style = "";
		
		switch( style_elm ) {
			case "border":
				css = trim(css).replace( eval("/border([-](left|right|top|bottom)([-](color|style|width))?)?[^:]*[:][^;]*[;]?/ig"), "" );
				break;
			case "background":
				css = trim(css).replace( eval("/([-]moz[-])?background([-](attachment|repeat|position|color|image))?[^:]*[:][^;]*[;]?/ig"), "" );
				break;
			default:
				css = trim(css).replace( eval("/"+ style_elm +"[^:]*[:][^;]*[;]?/ig"), "" );
				break;
		}
		
		css = trim(css);
		
		if( string_ok(style_val) ) {
			if( string_ok(css) ) {
				if( css.lastIndexOf(";") < (css.length -1) )
					css += ";";
				
				css += " ";
			}
			
			css += style_elm +": "+ style_val +";";
		}
		
		if( object_ok( oStyle ) )
			elm.style.cssText = css;
	}
}

function check6digit( field ) {
	return (_validator.is_numbers_only(field.value) && trim(field.value).length == 6);
}

function check4digit( field ) {
	return (_validator.is_numbers_only(field.value) && trim(field.value).length == 4);
}

function validate_input( field ) {
	if( !_input_handler.validate( field ) )
		errors.push( field["err_msg"] );
}

function show( elm_id ) {
	var elm = document.getElementById( elm_id );
	
	if( object_ok(elm) )
		elm.className = trim(elm.className).replace( /\s?\bhidden\b/, "" );
}

function hide( elm_id ) {
	var elm = document.getElementById( elm_id );
	
	if( object_ok(elm) && ( trim(elm.className).match(/\s?\bhidden\b/) === null ) )
		elm.className += " hidden";
}

function showHide( elm_id, checkbox ) {
	if( object_ok(checkbox) ) {
		if( checkbox.checked )
			show( elm_id );
		else
			hide( elm_id );
	}
}

function getCheckedRadio( radio_col ) {
	var cr = null;
			
	if( object_ok(radio_col) ) {		
		for( var i=0; i < radio_col.length && cr === null; i++ ) {
			if( radio_col[i].checked )
				cr = radio_col[i];
		}
	}
	
	return cr;	
}

function anyRadioChecked( radio_col ) {
	return ( getCheckedRadio( radio_col ) !== null );
}

function markAllRadios( radio_col, err ) {
	for( var i=0; i < radio_col.length; i++ )
		markInput( radio_col[i], err );
}

function markRadios( radio_col, err ) {
	markInput( radio_col[0].parentNode, err );
}

function markCheckbox( checkbox, err ) {
	markInput( aFields[ checkbox[0] ].parentNode, err );
}

function markAllCheckbox( checkbox, err ) {
	for( var i=0; i < checkbox.length; i++ )
		markInput( aFields[ checkbox[i] ], err );
}

function validate_radio( radio_col, err_msg ) {
	if( !anyRadioChecked( radio_col ) && string_ok(err_msg) ) {
		errors.push( err_msg );
		markRadios( radio_col, true );
	} else
		markRadios( radio_col, false );
}

var _movehtml = {
    destinationhtml:'',
    destinationid:'',

    transfer:function( source, destination ) {

        if ( source && destination ) {
            var sourceelement = document.getElementById( source );
            var destinationelement = document.getElementById( destination );

            if ( sourceelement && destinationelement ) {
                this.destinationhtml = destinationelement.innerHTML;
                this.destinationid = destination;
                destinationelement.innerHTML = sourceelement.innerHTML;
            }
        }

    },

    restore:function() {
        if ( this.destinationid ) {
            var destinationelement = document.getElementById( this.destinationid );
            
            if ( destinationelement ) {
                destinationelement.innerHTML = this.destinationhtml;               
            }
        }
    }

}

var _input_handler = {
	init : function( input_field, def_val, validation_func, error_msg, error_mode ) {
		this.initialize( input_field, def_val, validation_func, error_msg, error_mode );
	},
	
	initialize : function( input_field, def_val, validation_func, error_msg, error_mode ) {		
		if( object_ok(input_field) ) {
			/*
				ERROR MODES:
					0: return whether there was an error (true/false) - error message can be read in 'err_msg' on object
					1: color text red in wrong field
					2: alert error
					3>: mode 0 + 1 + 2
					10: Field is required, so content cannot be empty
					11: mode 1 + 10
					12: mode 2 + 10
					13: mode 3 + 10
			*/
			
			
			error_mode = parseInt(error_mode);
				if( isNaN( error_mode ) || error_mode < 0 ) error_mode = 0;
			
			if( string_ok(def_val) ) {			
				input_field["def_val"] = trim(def_val);
				input_field.value = input_field["def_val"];
				input_field.onfocus = this.clear;
				input_field.onblur = this.restore;
			}
			
			if( string_ok( validation_func ) ) {
				input_field["validate_func"] = validation_func;
				input_field["err_mode"] = error_mode;
				input_field["err_msg"] = ( string_ok(error_msg) )? error_msg : "Indtastningen var ikke korrekt";
			}
		}
	},
	
	clear : function() {
		if( object_ok(this) && string_ok(this["def_val"]) ) {
			if( this["def_val"] == trim(this.value) )
				this.value = "";
		}
	},
	
	restore : function() {
		if( object_ok(this) && string_ok(this["def_val"]) ) {
			if( !string_ok(this.value) && string_ok(this["def_val"]) ) {
				this.value = trim(this["def_val"]);
			} else {
				_input_handler.validate( this );
			}
		}
	},
	
	validate : function( input_field ) {
		
		try {
			var has_error = false;
			
			var val = trim(input_field.value);
				if( string_ok(input_field["def_val"]) && trim(input_field["def_val"]) == val )
					val = "";
					
			var is_requred = false;
			var err = input_field["err_msg"];
			var mode = input_field["err_mode"]; // see initialize for modes
			
			if( mode >= 10 ) {
				is_requred = true;
				mode -= 10;
			}
			
			if( !string_ok( val ) ) {
				if( is_requred )
					has_error = true;
			} else {
				if( string_ok(input_field["validate_func"]) )
					has_error = ( !(input_field["validate_func"]( input_field )) );	
			}
			
			if( mode >= 2 ) {
				if( string_ok( err ) && has_error ) alert( err );
				mode -= 2;
			}
			
			markInput( input_field, ( mode >= 1 && has_error ) );
			
			if( mode >= 1 ) mode -= 1;
							
			return (!has_error);
		} catch(ex) {
			alert("validate failed:\n"+ ex.message);
		}
		
	}
}
function trapEnterKey(btnID, event) {
  btn = findObj(btnID);
  if (document.all) {
    if (event.keyCode == 13) {
      event.returnValue = false;
      event.cancel = true;
      btn.click();
    }
  }
  else if (document.getElementById) {
    if (event.which == 13) {
      event.returnValue = false;
      event.cancel = true;
      btn.focus();
      btn.click();
    }
  }
  else if(document.layers) {
    if(event.which == 13) {
      event.returnValue = false;
      event.cancel = true;
      btn.focus();
      btn.click();
    }
  }
}


function trapEnterKeyToRedirect(url, event) {
	if (document.all) {
		if (event.keyCode == 13) {
			event.returnValue = false;
			event.cancel = true;
			window.location.href=url;
		}
	}
	else if (document.getElementById) {
		if (event.which == 13) {
			event.returnValue = false;
			event.cancel = true;
			window.location.href=url;
		}
	}
	else if(document.layers) {
		if(event.which == 13) {
			event.returnValue = false;
			event.cancel = true;
			window.location.href=url;
		}
	}
}

function findObj(n, d) { 
  var p,i,x;  
  if(!d) {
    d=document; 
  }
  if((p=n.indexOf("?")) > 0 && parent.frames.length) {
    d = parent.frames[n.substring(p+1)].document; 
    n = n.substring(0,p);
  }
  if(!(x=d[n])&& d.all) {
    x=d.all[n]; 
  }
  for (i=0;!x && i < d.forms.length;i++) {
    x=d.forms[i][n];
  }
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) {
    x=findObj(n,d.layers[i].document);
  }
  if(!x && d.getElementById) {
    x=d.getElementById(n); 
  }
  return x;
}


