/**
 * Javascript functions for the KTH website.
 */

/**
 * Set this to false if you dont want debug
 * info to be shown. Only works in Firefox.
 */
var DEBUG = false;

/**
 *
 * Redirect to the selected link in the form.
 * Used in quickie.
 */
function redirectLinkSelect( form ){

	enterFunction( redirectLinkSelect );
	
	var link = getSelectedValue( form.linkSelect );
	
	debug( "Link is: '" + link + "'" ); 

	redirect( link );
		
	exitFunction( redirectLinkSelect );

}

/**
 *
 * Redirect the current window to a url.
 */
function redirect( url ) {

	debug( "Redirecting current window to: '"  + url + "'." );
	 
	window.location.href = url;

}


/**
 *
 * Get the selected value from an html select box.
 */
function getSelectedValue( select ){

    return select.options[ select.options.selectedIndex ].value;
    
}

/**
 *
 * In Cortina an external link is an complete URL.
 */
function isExternalLink( value ){

	debug( "Value '" + value + "' is ExternalLink : " + isUrl( value ) );
	return isUrl( value );
	
}

/**
 * 
 * Is the passed value an URL, ie contains "http".
 */
function isUrl( value ) {

	if ( value == null){
		
		return false;
		
	}
	
	if( value.length < 5 ){
	
		return false;
		
	} 

	if( value.indexOf( "http" ) > -1 ){
	
		return true;
	
	}
	
	return false;
	
} 

/**
 * Open a new window
 *
 */
function openHelpWindow( url ){

	var newWindow2 = window.open(url,'mywindow','resizable=yes,width=300,height=200,scrollbars=yes,status=no');
	newWindow2.focus( );
	
}

/**
 * Open a new window
 *
 */
function openNewWindow( url ){

	var newWindow = open( url );

}

/**
 *
 * Submits the education choice on the studentwebsite.
 * Later to be used for selecting content in the navigation.
 *
 * NOTE! This method uses an form by the name "rs" (Rember Selection)
 * that has to be present in the page to work. 
 */
function submitEducationChoice( form ){

	enterFunction( submitEducationChoice );
	
	form.rs.value = ( document.rsForm.rs.checked ) ? 'true' : 'false';

	exitFunction( submitEducationChoice );

    return true;
    
}

function help( title, message, language ){

	enterFunction( help );

	var url = '/script/help-www.jsp?t=' + title + '&m=' + message + '&l='+ language;

	debug( "URL: " + url );
	openHelpWindow( url );

	exitFunction( help );	

}

/**
 * Get element by id wrapping browser incompability. 
 * Overloads the document.getElementById
 */
function getElementById(id) {

	if (!document.getElementById){
		if (document.all) {
			document.getElementById = function() {
				
				if (typeof document.all[id] != "undefined") {
					return document.all[id];
				} else {
					return null;
				}
			}

		} else if (document.layers) {			
			document.getElementById = function(){
				if (typeof document[id] != "undefined") {
					return document[id];
				} else {
					return null;
				}
			}
		}
	}
	
	return document.getElementById(id);
}

/**
 * Gets the current browser.
 */
function getBrowser(){
	return navigator.appName
}

/**
 * Gets the browser version as int.
 */
function getBrowserVersion() {

	var result = -1;
	var version = navigator.appVersion;
	
	if (version != null) {
		result = parseInt(version);
	}
	
	return result; 
	
}

/**
 * Set up method for page load.
 */
function setup() {

	enterFunction(setup);
	
	if (isIE7()) {
		columnHeights();
  	}
  	
  	exitFunction(setup);
  	
  	

}
function toogle(id) {
	var element = getElementById(id);
	if (isDisplaying(element)) {		
		element.style.display = 'none';
	} else {
		element.style.display = 'block';
	}
}

function isDisplaying(element){
	if (element == null) {
		return false;
	}
	
	if (element.style.display == null) {
		return true;
	}

	if (element.style.display == "") {
		return true;
	}

	if (element.style.display.toLowerCase() == 'block') {
		return true;
	}
	
	return false;

}


/**
 * Function for always track default sites
 * Local sites should use there own tracking code. 
 */
function trackDefaultSites() {
	
	var site = "" + document.location;
	var siteTracker = null;
	
	// _gat is initialized by the google-analytics.com/ga.js script
	// found in the html head tag if google-analytics is in use.
	if (typeof _gat != "undefined" && _gat != null)  {
		
		if (site.indexOf("www.kth.se") > -1 ) {		
			siteTracker = _gat._getTracker("UA-6683156-1");
		} else if (site.indexOf("campi.kth.se") > -1) {
			siteTracker = _gat._getTracker("UA-7171856-1");
		} else if (site.indexOf("intra.kth.se") > -1) {
			siteTracker = _gat._getTracker("UA-7171805-1");
		}
		
		if (siteTracker != null) {
			siteTracker._trackPageview();
		}
	}
	
	
}

trackDefaultSites();


//********************************************************************************************
//*********************************** DEBUG **************************************************
//********************************************************************************************
//********************************************************************************************
//************ To turn on debugging change var DEBUG_LEVEL at bottom of page *****************
//********************************************************************************************
//********************************************************************************************



/**
 * Debug info to use when entering a function .
 */
function enterFunction( method ){

	if( !isDebugMode( ) ){
		return;
	}
	
	debug( "<p  style='color:gray'>DEBUG: <strong>Entering function:</strong> " + method.name + "</p>");

}

/**
 * Debug info to use when entering a function .
 */
function exitFunction( method ){

	if (!isDebugMode()) {
		return;
	}
	
	debug( "<p  style='color:gray'>DEBUG: <strong>Exiting function:</strong> " + method.name + "</p>");

}


/**
 * Simple debug method for FireFox.
 */
function debug(message){
	
	if (isDebugMode()) {
		
		var display = getDebugView( ).document;
		
		display.write( "<p  style='color:gray'>DEBUG: " + message + "<p>" );		
		
	}
	
}

function info(message){
	
	if (isInfoMode()) {
		
		var display = getDebugView( ).document;
		
		display.write( "<p  style='color:green'>INFO: " + message + "<p>" );		
		
	}
	
}



function warning(message){
	
	if (isWarningMode()) {
		
		var display = getDebugView( ).document;
		
		display.write( "<p  style='color:orange'>WARNING: " + message + "<p>" );		
		
	}
	
}

function fatal(message){
	
	if (isWarningMode()) {
		
		var display = getDebugView( ).document;
		
		display.write( "<p  style='color:black; background-color: red;'>FATAL: " + message + "<p>" );		
		
	}
	
}

/**
 * Simple debug method for FireFox.
 */
function debugValue(label, value){
	
	if (isDebugMode()) {
		
		var display = getDebugView( ).document;
		
		display.write( "<p style='color:gray'>DEBUG: <strong>" + label + "</strong> '" + value + "'<p>" );		
		
	}
	
}

function infoValue(label, value){
	
	if (isInfoMode()) {
		
		var display = getDebugView( ).document;
		
		display.write( "<p style='color:green'>INFO: <strong>" + label + "</strong> '" + value + "'<p>" );		
		
	}
	
}

function warningValue(label, value){
	
	if (isWarningMode()) {
		
		var display = getDebugView( ).document;
		
		display.write( "<p style='color:orange'>WARNING: <strong>" + label + "</strong> '" + value + "'<p>" );		
		
	}
	
}



function fatalValue(label, value){
	
	if (isFatalMode()) {
		
		var display = getDebugView( ).document;
		
		display.write( "<p style='color:black; background-color: red;'>FATAL: <strong>" + label + "</strong> '" + value + "'<p>" );		
		
	}
	
}

/**
 * Get the view to display debug messages in. Normally a window.
 */
var debugView;

function getDebugView( ){

	if (debugView == null){
		debugView = open( 'debug_window', window, 'resizable=yes,width=400,height=250,scrollbars=yes' );
	}
	
	return debugView;
	
}

function debugCleanUp( ){ 

	if( debugView != null ){
		getDebugView( ).close( );
		debugView = null;
	}
	
}

/**
 * 
 * Is this javascript set in debug mode.
 * Normally this is set in DEBUG varible.  
 */

var NO_LOGGING = 100;
var DEBUG = 1;
var INFO = 2;
var WARNING = 3;
var FATAL = 4;

function isDebugMode( ){	
	return DEBUG >= DEBUG_LEVEL;
}

function isInfoMode( ){
	return INFO >= DEBUG_LEVEL;
}

function isWarningMode( ){
	return WARNING >= DEBUG_LEVEL;
}

function isFatalMode( ){
	return FATAL >= DEBUG_LEVEL;
}


/**
 * Set this to false if you dont want debug
 * info to be shown. Only works in Firefox.
 */
var DEBUG_LEVEL = NO_LOGGING;