/**
** Capture mouse coordinates
**/
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
    if (document.layers) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
     	mouseButton = e.which;
    } else if (document.all) {
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
    
}
/**
** Name: swap_image
** Arguments: image_id - the id of image holder of wich to change image
**			  image_url - the new image to change to
**
** Function: Switched the url for a given image 
** 
**/
function swap_image(image_id, image_url, type){
	var frame_holder = parent.frames['top_frame'];
	
	switch(type){

		case 1:	// Swap this image and reset all images with similar id's (type = 101)
			var image_array;
				var clicked_postfix = "_on";
				var off_postfix = "_off";
				var on_postfix = "_omo";
				var image_array = document.images;
		break;

	}
//			alert("id: " + image_id + ", img_type: " + image_type + ", image_path: " + image_path)
						
			var similar_id = image_url;
			var current_image;
			var org_image = image_id;
			var image_type = image_id.src.substring(image_id.src.length-4,image_id.src.length);
			var path_length = image_id.src.search(similar_id);
			var image_path = image_id.src.substring(0,path_length);
			var current_image_name;
			
			// Loop over all images and hide the ones that match the name
			for(var i = 0; i < image_array.length; i++) {
				current_image = image_array[i];

//				alert("Testar bild: " + current_image.id);
				
				// See if the current image matches the id
				if(current_image.id.search(similar_id) != -1){
					current_image_name = current_image.src;
					// Remove the ending from the filemane
					current_image_name = current_image_name.substring(0,(current_image_name.length-image_type.length))
					
					// Remove the current postfix from the filename
					if(current_image_name.search(clicked_postfix) != -1){
						current_image_name = current_image_name.substring(0,(current_image_name.length - clicked_postfix.length));
					} else if(current_image_name.search(on_postfix) != -1){
						current_image_name = current_image_name.substring(0,(current_image_name.length - on_postfix.length));
					} else if(current_image_name.search(off_postfix) != -1) {
						current_image_name = current_image_name.substring(0,(current_image_name.length - off_postfix.length));
					}
					// Set the new filename
					if(current_image == org_image){ // Clicked postfix for the clicked image
						current_image.src = current_image_name+clicked_postfix+image_type;
					} else { // Off postfix for the others
						current_image.src = current_image_name+off_postfix+image_type;
					}
				} // End if(current_image.id.search(similar_id) != -1){
			} // End for(var i = 0; i < document.images.length; i++) {
}
/**
** Name: image_omo(image, type)
** Arguments: image - Reference to the image
**			  type - image for passinga additional parameters
**
** Function: Switches name for an image. Called for OnMouseOver and OnMouseOut for an image
** 
**/
function image_omo(image, type){

	var image_src = image.src;
	var image_type = image_src.substring((image_src.length -4),image_src.length);
	var image_name = image_src.substring(0,image_src.length-image_type.length);
	
	switch(type){
		case 0:
			var off_postfix = "_off";
			var on_postfix = "_omo";
			var clicked_postfix = "_on";
		break;
		
		default:
			var off_postfix = "_off";
			var on_postfix = "_on";
			var clicked_postfix = "_selected";
	}
	if(image_name.search(off_postfix) !=-1){
		image_name = image_name.substring(0, (image_name.length - off_postfix.length));
		image_name += on_postfix + image_type;
	} else if (image_name.search(on_postfix) !=-1){
		image_name = image_name.substring(0, (image_name.length - on_postfix.length));
		image_name += off_postfix + image_type;
	}
	// Do not use onMouseOver of the image is clicked on
	if(image.src.search(clicked_postfix) == -1){
		image.src =  image_name;
	}
}

/**
** Name: open_popup
** Arguments: page - the content of the popup window
**			  w - the width of the window
**			  h - the heigt of the window
**			  type - variable containing additional arguments
**
** Function: opens a pop up page centerd on the screen with the given content.
** 
**/
function open_popup(page, w, h, type) 
{
	w = 1 * w;
	h = 1 * h;
	
	
	var options;
	switch(type){
		case 0: // FAQ
			options = "scrollbars=0,resizable=1";
		break;
		case 1: // Table order
			options = "scrollbars=1,resizable=1";
		break;
	}

	x = window.screen.availWidth;
	y = window.screen.availHeight;
	
	w = w - 13;
	h = h - 30;
	
	x = 1 * x;
	y = 1 * y;
	
	y = 0.2 * y;
//	x = (x - w)/2;
	x= 100;
		
	windowInstance = window.open(page, "",config='width='+w+',height='+h+','+options+',top='+y+',left='+x)
}
/**
** Name: find_first_table
** Arguments: none
**
** Function: Finds the first table that has a width defined in pixels
** 
**/
function resize_popup() 
{
	var element = document.getElementsByTagName("table");
	for (var i = 0; i < element.length; i++){
	//	alert("Element, id: "+element[i].id+", name: "+element[i].name+", type: "+element[i].tagName+", width: "+element[i].width);
		if(element[i].width.search("%") == -1 ){ // % width is not accepted
			width = element[i].width * 1 + 5;
			//width += 5;
			height = element[i].offsetHeight + 80;
			if(height > 500){
				height = 500;
			}
			resize_self(width, height);
		}
	}
}
/**
** Name: resize_self
** Arguments: int width - the width to resize to
**			  int height - the height to resize to
**
** Function: Resizes the current window to the given width and height
** 
**/
function resize_self(width, height){
	height = height * 1;
	width = width * 1;
	self.resizeTo((width+13),(height+45)); // Make some extra room for the close link				
}
/**
** Name: reload_frame
** Arguments: frame_name - name of the frame to reload
** 
** Function: Reloads a specific frame
** 
**/
function reload_frame(frame_name) {
	
    if (document.images){
        parent.parent.frames[frame_name].location.reload();
    } else {
        parent.parent.frames[frame_name].location.href = parent.frames[frame_name].location.href;
    }
}
/**
** Name: load_frame
** Arguments: frame_name - name of the frame where contents should be loaded
** 			  loacation - the location that should be loaded
** 
** Function: Reloads a specific location in a given frame
** 
**/
function load_frame(frame_name, location){
//	alert("Ska ladda frame: "+frame_name+"\nlocation:"+location+"\ni: "+parent.frames[frame_name].name+"\nsom nu är: "+parent.frames[frame_name].location.href);
	parent.frames[frame_name].location.href = location;
	return 1;
}
/**
* Name: 		validate_form
* Description:	checks all required fields (with '-r' n the name) and highlights the ones that are not completed
* Variables:	form_element: The form element
* Returne:		true if all fields are completed. Otherwise false.
*
**/
function validate_form(form_element){

	var form_elements = form_element.elements;
	var form_completed = true;
	var first_password = "";
	var alert_message = "";
	if(!form_elements){
		return false;
	}
	
	var num_form_elements = form_elements.length;
	for(var i = 0; i < num_form_elements; i++){
		
//		alert("kollar : "+form_elements[i].name + " som är: "+form_elements[i].type+"\när text: "+form_elements[i].type.search("text"));

		if(form_elements[i].type.search("text") == 0){
			form_elements[i].className = 'black_10_border'; // Reset the className	
		}
		

                  
		if(form_elements[i].name.search("-r") >= 0 && form_elements[i].value == ""){
			form_elements[i].className = 'alert';
			form_completed = false;
			alert_message = "Vänligen fyll i alla obligatoriska fält.\n";
		}
		// Check radiobuttons
		if(form_elements[i].name.search("-r") >= 0 && form_elements[i].type == "radio"){
			var radio_button = document.forms[0].elements[form_elements[i].name];
			// Loop over all buttons and check if anyone is selected.
			var radio_completed = false;
			for(var j = 0; j < radio_button.length; j++){
				if(radio_button[j].checked){
					radio_completed = true;
				}
			}
			if(!radio_completed){
				form_completed = false;
				alert_message = "Please complete all required fields.\n";
			}
			radio_completed = "";
		}
		
		
		// Password match check
		if(form_elements[i].name.search("pwd") >= 0){
			if(first_password.length == 0){
				first_password = form_elements[i].value;
				
			} else {
				if(first_password != form_elements[i].value){
					alert_message = "Your passwords doesn't match!";
					form_elements[i].className = 'alert';
					form_elements[(i-1)].className = 'alert';
					form_completed = false;
				}
			}			
		}

	}
	if(form_completed){
		return true;
	} else {
		alert(alert_message)
		return false;	
	}
	
}

/*******************************/
function get_cell_highlight_class(cell_index, row) {

  var cell_class = row.cells[cell_index].className;
  // Don't append active if the cell is already active, selected or locked
  if (cell_class.search("_omo") == -1 && cell_class.search("_selected") == -1 && cell_class.search("_locked") == -1) {
    cell_class = cell_class + "_omo";
  }
//  	alert("Sätter OMO class: "+cell_class);
	return cell_class;
}

function get_cell_lowlight_class(cell_index, row) {

  cell_class = row.cells[cell_index].className;
  // Don't deactivate a cell if it isn't "_active" 
  if (cell_class.search("_omo") != -1) {
    cell_class = cell_class.substring(0,cell_class.length-4);
  }
	return cell_class;
}

function highlight_row(row, type)
{
//var message ="Rader i tabellen: " + table.rows.length + "\n" + "Celler i raden: " + table.rows[row.rowIndex].cells.length + "\n"; 
//var message = "Radindex: " + cell.parentNode.rowIndex + "\n" + "Cellindex: " + cell.cellIndex;
//var message = "Radindex: " + row.rowIndex + "\n";
//var message = "Tabellid: " + row.parentNode.parentNode.id + "\nTabellnamn: " + row.parentNode.parentNode.name;
//var message = "Row: " + row + "\nType: " + type;
//alert(message);

  // Get the container for the table 
	var table = getTableContainer(row);
	var style_class;
	if (!table) {
		type = -1;
	}

	switch(type){
		case 0:
		
			//  alert("Längd: " +row.rowIndex +"\nKlassnamn: "+table.rows[row.rowIndex].cells[1].className);
			for(var i = 0; i < row.cells.length; i++){
				table.rows[row.rowIndex].cells[i].className = get_cell_highlight_class(i, row);
				style_class = get_cell_highlight_class(i, row);
			//			alert("Är: " + table.rows[row.rowIndex].cells[i].className + "\nVill byta till: "+get_cell_highlight_class(i, row));
			}
//			alert("Highlight: "+style_class);
		break
		case 1:
			// Fix for first line in each module wich has an extra cell
			var start_index;
			if(row.cells.length == 5){
				start_index = 1;
			} else {
				start_index = 0;
			}
			for(var i = start_index; i < row.cells.length; i++){
				table.rows[row.rowIndex].cells[i].className = get_cell_highlight_class(i, row);
			}
		break
		
		case 2:
			//Highlight all rows for a module
			row = row.parentNode; // Convert the the cell to a row
			var start_index;
			var num_rows = 5;
			
			for(var j = row.rowIndex; j < (row.rowIndex + num_rows); j++)	{
				for(var i = 0; i < table.rows[j].cells.length; i++){
					table.rows[j].cells[i].className = get_cell_highlight_class(i, table.rows[j]);
				}
			}
		break
	} // End case
} // End highlight_row

function lowlight_row(row, type)
{
	// Get the container for the table 
	var table = getTableContainer(row);
	var style_class;

	switch(type){
		case 0:
		for(var i = 0; i < row.cells.length; i++){
			table.rows[row.rowIndex].cells[i].className = get_cell_lowlight_class(i, row);
			style_class = get_cell_lowlight_class(i, row);
		}
//		alert("Lowlight: "+style_class);
	break
	case 1:
		// Fix for first line in each module wich has an extra cell
		var start_index;
		if(row.cells.length == 5){
			start_index = 1;
		} else {
			start_index = 0;
		}
		for(var i = start_index; i < row.cells.length; i++){
			table.rows[row.rowIndex].cells[i].className = get_cell_lowlight_class(i, row);
		}
	break
	case 2:
		//Lowlight all rows for a module
		row = row.parentNode; // Convert the the cell to a row
		var start_index;
		var num_rows = 5;
		
		for(var j = row.rowIndex; j < (row.rowIndex + num_rows); j++)	{
			for(var i = 0; i < table.rows[j].cells.length; i++){
				table.rows[j].cells[i].className = get_cell_lowlight_class(i, table.rows[j]);
			}
		}
	
	break
	} // End case
} // End lowlight_row

function select_option(object_name, option_value){
	
//	alert("test3:\n "+parent.parent.id);
//	alert("test2:\n "+top.top_frame.document.forms[0][object_name].name);
//	box = parent.parent.top.document.forms[0][object_name];
	box = top.top_frame.document.forms[0][object_name];

	for(i = 0; i < box.options.length; i++){
		if(box.options[i].value == option_value){
			box.selectedIndex = i;
		}
//		alert("kollar: "+box.options[i].value +"=="+ option_value)
	}
	// Submit the form if a fund is submitted to load the book dropdown
	if(object_name == "menu_fund"){
		top.top_frame.document.forms[0].submit();
//		parent.parent.top.document.forms[0].submit();  // Not used
	}
	
}

/**
** Disables or enables input field depending on if current checkbox is clicked or not
**
** checkbox - handler for the current checkbox (this)
** textfield_name - name of the textfield to control
**/
function control_order_field(checkbox, textfield_name){
	
	textfield = document.getElementById(textfield_name);
	if(checkbox.checked == true){
		textfield.disabled = false;
		textfield.value = get_max_order();
	} else {
		textfield.disabled = true;
		textfield.value = "";
		
	}
}

function set_order_field_value(textfield_name, calc_type){
	
	textfield = document.getElementById(textfield_name);
	old_value = parseInt(textfield.value);
	if(calc_type == '+'){
		new_value = old_value + 1;
	} else {
		new_value = old_value - 1;
	}
	// Loop over all fields to fiend the field that matches the new value
	for(i = 0; i< document.forms[0].elements.length; i++){
	
	if(document.forms[0].elements[i].name.search("_order") != -1){
		if(parseInt(document.forms[0].elements[i].value) == parseInt(new_value)){
			 document.forms[0].elements[i].value = old_value;
		}
	}
	
	if(textfield.disabled == false){
		textfield.value = new_value;
	}
}

}
/**
** Reloads the frame that called a pop-up
**/
function reload_parent(){
	opener.location.reload();
}

/**
** Closes the current window
**/
function close_self(){
	window.close();
}
/**
** Loops through the order input boxes and returns the greates written value +1
**
**
**/
function get_max_order(){
	var max_value = 0;
	
	var string;
	// Loop over all dd_ fields to get the max value
	for(i = 0; i< document.forms[0].elements.length; i++){
		
		if(document.forms[0].elements[i].name.search("_order") != -1){
			if(parseInt(document.forms[0].elements[i].value) > parseInt(max_value)){
				 max_value = parseInt(document.forms[0].elements[i].value);
				string += max_value+", "; 
			}
		}
	}
	return ((max_value*1)+1);
//	alert("Returnerar: "+max_value);
}
/**
** Toggle the visiblity of the submenu by resizing the previuos row
**/
function toggle_submenu_DEPRICATED(mode){
	table = document.getElementById('menu_container');
	// Display submenu by changeing height of the two cells
	if(mode == 1){
//		table.rows[1].style.display = '';
		table.rows[0].cells[0].height=52;
		table.rows[0].cells[1].height=52;
		
	}else{
		// Hide submenu
//		table.rows[1].style.display = 'none';
		table.rows[0].cells[0].height=118;
		table.rows[0].cells[1].height=118;
	}
}
function toggle_submenu(mode){
	table = document.getElementById('submenu_container');
	// Display submenu by setting the visiblity for the table
	if(mode == 1){
		table.style.display = '';
		
	}else{
		// Hide submenu
		table.style.display = 'none';
	}
//	 Sätt meny bilderna
//	set_image("menu_image_0", "img/tab_overview_off.gif");
//	set_image("menu_image_1", "img/tab_funds_on.gif");
//	set_image("menu_image_2", "img/tab_settings_off.gif");
}
// Replaces the , with a . in a string
function replace_comma(me){
	me.value = me.value.replace(",",".");
}
function update_cashtrans_comment(value_field){
	var comment_field = document.getElementById('info_txt-r');
	if(comment_field){
		comment_field.innerHTML += " Value: "+value_field.value;
	}
	
}

/**
** Sets a given image to a given url
**
**/
function set_image(image_id, image_url){
	var image_holder = document.getElementById(image_id);
	image_holder.src = image_url;
	return 1;
}
/**
** Name: display_tooltip()
** Arguments: text - The text to display in the tooltip
**			  type - variable for passing additional parameters
**
** Function: Displays a tooltip box with the given text
** 
**/
function display_tooltip(content, type)
{
var tooltip_element = document.getElementById("tooltip_element");
var vertScroll = document.body.scrollTop;
var startHeight = yMousePos - 0 + vertScroll;
var yDisplacement = 0;
var element_width = 0;

	switch(type){
		// cash trans list
		case 0:
			element_width = 350;
			tooltip_element.style.width = element_width;
		break;
	}
	// Only display the tooltp if it has content
	if(content.length > 1){
		tooltip_element.style.left = xMousePos - 15 - element_width;
		tooltip_element.innerHTML = content;
		/* Move tooltip if it spans over the bottom of the page.
		tooltip is not visible over frame boreder, hence disabled.
		 
				yDisplacement = (startHeight + tooltip_element.offsetHeight) - document.body.clientHeight;
				if(yDisplacement < 0){
					yDisplacement = 0;
				}
		*/	
		tooltip_element.style.top  = startHeight - yDisplacement;  // - 30 so toolTip is positioned above text.
		tooltip_element.style.display = "";
	}
}
/**
** Name: hide_tooltip()
** Arguments: type - variable for passinga additional parameters
**
** Function: Hides the tooltip element
** 
**/
function hide_tooltip(type){

	var tooltip_element = document.getElementById("tooltip_element");
	tooltip_element.style.display = "none";
	tooltip_element.innerHTML = "";
	tooltip_element.style.top = -100;
	tooltip_element.style.left = -100;
}
/**
** Name: set_additional_object
** Arguments: object - string containing the object_type
**
** Function: Sets the object type for when adding addition objects from the auto_form
**
**/
function set_additional_object(object){
	form_element = document.getElementById("additional_object");
	form_element.value = object;
	return 1;
}

/**
** Name: reload_image()
** Arguments: image_name - id of the image to reload
**		   
** Function: Forces the browsre to reload an image and not to show the cache version. Gets the source of the file from the given one
**/
function reload_image(image_name){
    var now = new Date();
	var image_element = document.getElementById(image_name);

    if (document.images) {
        image_element.src = image_element.src+'?' + now.getTime();
    }
}