/**
 * Function initialize the Html-Editor TinyMCE
 *
 * @author Daniel Aslan
 */
function initTinyMCE(lang, content_css) {
	if (tinyMCE) {
		tinyMCE.init({
			mode : "textareas",
			theme : "advanced",
			browsers : "msie,gecko",
			plugins : "table,emotions,contextmenu",
			table_styles : "Header 1=header1;Header 2=header2;Header 3=header3",
			table_cell_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Cell=tableCel1",
			table_row_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1",
			table_cell_limit : 100,
			table_row_limit : 5,
			table_col_limit : 5,
			theme_advanced_buttons1 : "newdocument,separator,bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,fontselect,fontsizeselect,separator,bullist,numlist,outdent,indent,sub,sup,separator,charmap,emotions,forecolor,backcolor,separator,undo,redo,removeformat,separator,table,link,unlink",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_path : false,
			language : lang,
			content_css : content_css,
			force_br_newlines : true,
			force_p_newlines : false,
			extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
		});
	}
}

/**
 * Function returns a reference to the VKC frameset containing
 * window identified by name "MAINFRAMESET"
 *
 * @author Hardy Höfer
 */
function getMainFrameset() {
  	var win = self;

  	while ( win ) {
    	if (win.name == 'MAINFRAMESET') {
        	return win;
    	}

    	if (win == top) {
        	return null;
    	}

    	win = win.parent;
  	}

  	return null;
}

/**
 * Function returns a reference to the VKC frameset containing
 * window identified by name "NAVIGATORFRAMESET"
 *
 * @author Hardy Höfer
 */
function getNavigatorFrameset() {
  	var win = self;

  	while ( win ) {
    	if (win.name == 'NAVIGATORFRAMESET') {
        	return win;
    	}

    	if (win == top) {
        	return null;
    	}

    	win = win.parent;
  	}

  	return null;
}

/**
 * Function returns a reference to the VKC
 * window identified by name "MAINVIEW"
 *
 * @author Hardy Höfer
 */
function getMainView() {	
    var f = getMainFrameset();

    if ( !f ) {
        return null;
    }

    if ( !f.MAINVIEW ) {
		return null;
    }     
    
	// Opera workaround
	if (window.opera) {
		if (f.MAINVIEW.parent && f.MAINVIEW.parent.name == 'NAVIGATORFRAMESET') {
			return f.MAINVIEW.parent;
		}
	}
		
    return f.MAINVIEW;
}

/**
 * Function returns a reference to the VKC
 * window identified by name "TABVIEW"
 *
 * @author Hardy Höfer
 */
function getTabView() {
    var f = getMainFrameset();

    if ( !f ) {
        return null;
    }

    if ( !f.TABVIEW ) {
        return null;
    }        

    return f.TABVIEW;
}

/**
 * Function returns a reference to the VKC
 * window identified by name "COPYRIGHTVIEW"
 *
 * @author Daniel Aslan
 */
function getCopyrightView() {
    var f = getMainFrameset();

    if (!f) {
        return null;
    }

    if (!f.COPYRIGHTVIEW) {
        return null;
    }        

    return f.COPYRIGHTVIEW;
}

/**
 * Function returns a reference to the VKC
 * window identified by name "TREEVIEW"
 *
 * @author Hardy Höfer
 */
function getTreeView() {
    var f = null;
    
    if (window.opera) {
        f = getNavigatorFrameset();
    } else {
        f = getMainView();
    }

    if ( !f ) {
        return null;
    }

    if ( !f.TREEVIEW ) {
        return null;
    }        

    return f.TREEVIEW;
}

/**
 * Function returns a reference to the VKC frameset containing
 * window identified by name "NAVIGATORFRAMESET"
 *
 * @author Hardy Höfer
 */
function getClusterFrameset() {
  	var win = self;

  	while ( win ) {
    	if (win.name == 'CLUSTERFRAMESET') {
        	return win;
    	}

    	if (win == top) {
        	return null;
    	}

    	win = win.parent;
  	}

  	return null;
}

/**
 * Function removes white spaces from the front and end 
 * of a string.
 *
 * @author Hardy Höfer
 * @return null, if given string is null or not defined
 *         empty string, if given string is empty or contains white spaces only
 *         - the given string without preceeding and succeeding blanks
 */
function trim( s ) {
 	if (!s || s == null) {
    	return null;
    }

  	var trimmed = "";
  	var maxIdx = s.length - 1;
  	var i = 0;
  	var k = 0;

  	for (i=0; i<=maxIdx; i++) {
    	if ( s.charAt(i) != ' ' && s.charAt(i) != '\n' && s.charAt(i) != '\r' && s.charAt(i) != '\t' )
      		break;
  	}

  	if (i <= maxIdx) {
    	for (k=maxIdx; k>i; k--) {
	      	if ( s.charAt(k) != ' ' && s.charAt(k) != '\n' && s.charAt(i) != '\r' && s.charAt(k) != '\t' )
	        	break;
    	}

    	trimmed = s.substring(i, k+1);
  	}

  	return trimmed;  	
}

/**
 * Function checks whether the argument is a valid integer value
 *
 * @author Hardy Höfer
 */
function isInteger( str ) {
    if ( !str || !str.length || str.length==0 ) 
        return false;

    var i  = 0;
    var ch = "";

    for ( var i=0 ; i<str.length ; i++ ) {
        ch = str.charAt( i );

        if ( ch=='0' || ch=='1' || ch=='2' || ch=='3' || ch=='4' || 
             ch=='5' || ch=='6' || ch=='7' || ch=='8' || ch=='9' ) {
             continue;
        } else if ( i==0 && ( ch=='+' || ch=='-' ) ) {
             continue;
        } else {
            return false;
        }
    }

    return true;
}


/**
 * Submits a form, changes
 * a buttons text and disables it.
 *
 * @author Andreas Koop
 */
function submit(form, button, text){
	button.value=text;
	button.disabled=true;
	form.submit();
}

/**
 * Opens a new screen-centered window.
 * Used for popup-windows with no scrollbars and addressbar.
 *
 * @author vchris
 */
function openCentered(width, height, url, name) {
    var leftPosition = screen.width/2  - width/2;
    var topPosition  = screen.height/2 - height/2;
    var settings ='height=' + height + ', width=' + width + ', top=' + topPosition + ', left=' + leftPosition;
    settings += ',scrollbars=no,resizable=yes';
    windowOpen = window.open( url, name, settings );
    windowOpen.focus();							  

    return windowOpen;
}

/**
 * Opens a new screen-centered window.
 * Used for popup-windows with scrollbars but without addressbar.
 *
 */
function openWindow(width, height, url, name) {
    var settings ='height=' + height + ', width=' + width;
    settings += ',scrollbars=yes,resizable=yes';
    windowOpen = window.open( url, name, settings );
    windowOpen.focus();
}

function disableAllButtons(elem) {
	if (elem != null && elem.type && elem.type == 'button') {
		var forms = document.forms;
		var index = null;
		
		for (var i = 0; i < forms.length; i++) {
			var elements = forms[i].elements;
			
			for (var j = 0; j < elements.length; j++) {
				if (elements[j].type == 'button') {
		    		elements[j].disabled = true;
				}
				
				if (elements[j].name == elem.name) {
					index = i;
				}
			}
		}
	
		if (index != null) {
			if (elem.form.action && elem.name && elem.name != '') {
				if (elem.form.action.indexOf('?') == -1) {
					elem.form.action = elem.form.action + '?' + elem.name + '=true';
				} else {
					elem.form.action = elem.form.action + '&' + elem.name + '=true';
				}
			}
		}
	}
}

function disableAllButtonsAndSubmit(elem) {
	if (elem != null && elem.type && elem.type == 'button') {
		var forms = document.forms;
		var index = null;
		
		for (var i = 0; i < forms.length; i++) {
			var elements = forms[i].elements;
			
			for (var j = 0; j < elements.length; j++) {
				if (elements[j].type == 'button') {
		    		elements[j].disabled = true;
				}
				
				if (elements[j].name == elem.name) {
					index = i;
				}
			}
		}
	
		if (index != null) {
			if (elem.form.action && elem.name && elem.name != '') {
				if (elem.form.action.indexOf('?') == -1) {
					elem.form.action = elem.form.action + '?' + elem.name + '=true';
				} else {
					elem.form.action = elem.form.action + '&' + elem.name + '=true';
				}

				elem.form.submit();
			}
		}
	}
}

function preventSubmitByEnter() {
  	return !(window.event && window.event.keyCode == 13);
}