// JavaScript for WebCHR
// Martin Kaeser, 2007

// redirect browser to main page if only program frame was loaded
function redirect()
{
  if (top.location == location)
    window.location = "../." + location.search
}

// redirect program frame to take parameter (load) from main URL
function deep_link()
{
  if(window.location.search != "")
    top.program.location = "./cgi-bin/program.cgi" + window.location.search
}

// Scroll console window to the bottom
function scroll_down()
{
  var ta_console = document.getElementById("ta_console")
  ta_console.focus()
  var text = ta_console.value
  ta_console.value = text
  ta_console.scrollTop = ta_console.scrollHeight
}

// get height of window's client area
function get_body_height()
{
  var bh=0, dh=0
  if(document.body && document.body.clientHeight)
    bh = document.body.clientHeight
  if(document.documentElement && document.documentElement.clientHeight)
    dh = document.documentElement.clientHeight
  // don't know how to get desired value - depends on browser
  var h = (Math.min(bh,dh)>0) ? Math.min(bh,dh) : Math.max(bh,dh)
  return h
}

// show/hide code
function toggle_code()
{
  var h_show_code = document.getElementById("h_show_code")
  if (h_show_code.value == "0")
    h_show_code.value = "1"
  else
    h_show_code.value = "0"
  disp_code()
}

// show or hide code indicated by hidden input 'h_show_code'
function disp_code()
{
  var body_height = get_body_height()
	var div_top = document.getElementById("div_top")
	var div_bottom = document.getElementById("div_bottom")
  var h_show_code = document.getElementById("h_show_code")
	var a_show_code = document.getElementById("a_show_code")
	var p_info = document.getElementById("p_info")
	var ta_code = document.getElementById("ta_code")
	var ta_console = document.getElementById("ta_console")
  
  //var h1 = 20; var h2 = 12
  var h1 = 20; var h2 = 32
  
  // set elements to invisible or not, change text
  // set size of textarea(s) according to window height and element heights
	if (h_show_code.value == "1")
	{
		a_show_code.innerHTML = "Hide program code"
		p_info.style.display = "none"
    p_code.style.display = ""
    
    var h3 = h1+div_top.offsetHeight+div_bottom.offsetHeight
    var ta_height = (body_height-h3)/2 - h2
    ta_code.style.height = ta_height+"px"
    ta_console.style.height = ta_height+"px"
	}
	else
	{
		a_show_code.innerHTML = "Display program code"
    p_code.style.display = "none"
    p_info.style.display = ""

    var h3 = h1+div_top.offsetHeight+div_bottom.offsetHeight
    var ta_height = (body_height-h3) - h2
    ta_console.style.height = ta_height+"px"
	}
}

// clear console textarea
function clear_console()
{
	document.getElementById("ta_console").value = "| ?- "	
  document.getElementById("s_query").selectedIndex = 0
  scroll_down()
}

// triggered when user selects a predefined query
function select_query()
{
	var s_query = document.getElementById("s_query")
	var ta_console = document.getElementById("ta_console")
	var startpos = query_startpos()
	ta_console.value = ta_console.value.substr(0,startpos) + " " // remove current query
	if (s_query.selectedIndex > 0)
		 ta_console.value += s_query.options[s_query.selectedIndex].text // ..and add new one
  scroll_down()
}

// find start pos of current query in console textarea
function query_startpos()
{
	var ta_console = document.getElementById("ta_console")
	var tmp = "\n" + ta_console.value
	var pos = tmp.lastIndexOf("\n| ?-")
	if (pos < 0)
	{
    // no query? - initialise!
		ta_console.value = "| ?-"
		return 4
	}
	return pos+4
}
