/* A bunch of Javascript for adding features to Canalplan interface
pages */

function New_XMLHTTP() {
  var xmlhttp=null
  // first for all but IE, else for IE
  if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest()
  } else if (window.ActiveXObject) {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  if (xmlhttp == null) {
    alert("Sorry, your browser can't do this");
    return false;
  }
  return xmlhttp;
}

// Sets a radio button to a particular state
function SetRadio(field,setto) {
  var x = document.forms['canalform'].elements[field];
  for(i=0; i<x.length; i++) {
    if(x[i].value == setto) {
      x[i].checked = true;
    } else {
      x[i].checked = false;
    }
  }
}

// toggles a checkbox when you click on the text
function SetCheckbox(field) {
  var x = document.forms['canalform'].elements[field];
  x.checked = !(x.checked);
}

// called on clicking on a map type box
function MapCheckbox(wh) {
  if(document.forms['canalform'].elements[wh].checked == true) {
    document.forms['canalform'].elements['maincheck_map'].checked = true;
  }      
}

// a combination of the above for map box text
function MapChecktext(field) {
  var x = document.forms['canalform'].elements[field];
  x.checked = !(x.checked);
  if(x.checked == true)
    document.forms['canalform'].elements['maincheck_map'].checked = true;
}

// toggle a checkbox
function ToggleBox(field) {
  var toswap = document.forms['canalform'].elements[field];
  toswap.checked = !toswap.checked;
}

/* hacked out of dhtmlgoodies - original copyrights as follows
(C) www.dhtmlgoodies.com, November 2005

This is a script from www.dhtmlgoodies.com. You will find this and a
lot of other scripts at our website.

Terms of use:
You are free to use this script as long as the copyright message is
kept intact. However, you may not redistribute, sell or repost it
without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland
*/

function Smooth_Slide(outer, inner, intoview) {
    var obj =document.getElementById(outer);
    var contentObj = document.getElementById(inner);
    var stepsize = 10;
    if(intoview == false) 
	stepsize = -stepsize;
    height = obj.clientHeight;
    if(height == 0) height = obj.offsetHeight;
    height = height + stepsize;
    rerunFunction = true;
    if(height > contentObj.offsetHeight){
	height = contentObj.offsetHeight;
	rerunFunction = false;
    }
    if(height <= 1) {
	height = 0;
	rerunFunction = false;
    }
    obj.style.height = height + 'px';
    var topPos = height - contentObj.offsetHeight;
    if(topPos > 0) topPos=0;
    contentObj.style.top = topPos + 'px';
    if(rerunFunction) {
	setTimeout('Smooth_Slide("'+outer+'","'+inner+'",'+intoview+')',10);
    }
}

function Toggle_Submenu(item) {
    a = $('submenu_'+item);
    b = $('edit_arrow_'+item);
    if(b.src.indexOf("tinyup") != -1) {
	Smooth_Slide('submenu_'+item,'submenu_content_'+item,false);
	b.src = b.src.replace('tinyup.png','tinydown.png');
    } else {
	Smooth_Slide('submenu_'+item,'submenu_content_'+item,true);
	b.src = b.src.replace('tinydown.png','tinyup.png');
    }
}

// produce a simple pop-up window

function pop_up(contents) {
  var newwindow = window.open(contents,"Whatsnew","scrollbars=yes");
  newwindow.focus();
}

/* Hide/expose blocks of text etc.  We don't need to test for capability, as
code to call these is only generated when getElementById exists */

function ToggleShow(item) {
  var thisone = document.getElementById(item).style;
  if(thisone.display == "block")
    thisone.display = "none";
  else
    thisone.display = "block";
}

function ShowBlock(item) {
  var thisone = document.getElementById(item).style.display = "block";
}

function HideBlock(item) {
  var thisone = document.getElementById(item).style.display = "none";
}

/* Used to compress blocks on the options screen */

function OptionsBlock(thing) {
  if(document.forms['canalform'].elements['opt_'+thing].value == "hide") {  // turning on
    document.getElementById(thing+"-off").style.display = "none";
    document.getElementById(thing+"-on").style.display = "block";
    document.canalform['opt_'+thing].value = "show";
  } else {
    document.getElementById(thing+"-off").style.display = "block";
    document.getElementById(thing+"-on").style.display = "none";
    document.canalform['opt_'+thing].value = "hide";
  }
}

function synchronise_hpd(altered,other) {
  document.forms['canalform'].elements[other].value = document.forms['canalform'].elements[altered].value;
  return false;
}

/* I bet this is never used!
// used for expanding on waterways when loading rings
function ringdetails(numb) {
  document.forms['canalform'].elements['_submit'].value = 'Show Ring Details';
  document.forms['canalform'].elements['detailsofwhat'].value = numb;
  document.forms['canalform'].elements['PL_START'][0].click();
}
*/

// produce the pop calendar
function PopUpCal(yy,mm,dd) {
  var fm = document.forms['canalform'].elements;
  var yv = fm[yy].options[fm[yy].selectedIndex].text;
  var mv = fm[mm].options[fm[mm].selectedIndex].text;
  var dv = fm[dd].options[fm[dd].selectedIndex].text;
  var dt = dv+'/'+mv+'/'+yv;
  var win = window.open("../cgi-bin/showcal.cgi?t="+dt+";y="+yy+";m="+mm+";d="+dd,"popcal","width=200,height=170,toolbar=no,menubar=no,scrollbars=0,resizable=no");
}

// set one item in a selection
function setone(what,to) {
  for(i=0;i<what.length;i++) {
    if(what.options[i].text == to)
      what.selectedIndex = i;
  }
}

/* ----------------  Code for drop-down menus for places ----------------- */

// global menu variable for use in mouse events
var pm_stuff = {
  currentmenu: null,
  which_place: null,
  lastcurrentmenu: null,
  timer: null
}

function PlaceMenu(pc,what,flg) {
  var menu = document.getElementById(what)
  var inbox = document.getElementById(pc);
  if(flg) {    // turning it on
    var x =0, y = 0;               // work out where to show it
    clearTimeout(pm_stuff.timer);
    y = inbox.offsetHeight;
    while(inbox.offsetParent) {
      x = x + inbox.offsetLeft;
      y = y + inbox.offsetTop;
      inbox = inbox.offsetParent;
    }
    var mm = menu;
    var mx = 0;
    while(mm.parentNode && mm.parentNode.offsetLeft) {
				mx += mm.parentNode.offsetLeft;
				mm = mm.parentNode;
    }
    x -= mx;
    menu.style.left= x+"px";
    menu.style.top = y+"px";
    Smooth_Slide(what,what+'_contents',true);
    pm_stuff.which_place = pc;
  } else {
    pm_stuff.currentmenu = what;
    pm_stuff.lastcurrentmenu = what;
    pm_stuff.timer = setTimeout(HideMenu, 1000);;
  }
}

// called by timer
function HideMenu() {
  if(pm_stuff.currentmenu)
      Smooth_Slide(pm_stuff.currentmenu,pm_stuff.currentmenu+'_contents',false);
}

function KillMenu() {
    if(pm_stuff.lastcurrentmenu) {
				Smooth_Slide(pm_stuff.lastcurrentmenu,pm_stuff.lastcurrentmenu+'_contents',false);
    }
}

function KeepMenu(what,flg) {
    var menu = document.getElementById(what);
		if(flg) {
				pm_stuff.currentmenu = null;     // prevent it vanishing
		} else {   // it will vanish soon
				pm_stuff.currentmenu = what;
				pm_stuff.lastcurrentmenu = what;
				pm_stuff.timer = setTimeout(HideMenu, 1000);;
		}
}

// if this gets too big, break it out into a menu.js file

function Menu_Function(menu, item) {
    switch(menu) {
    case 'newroute':
	switch(item) {
	case '0':
	    place_clearinter();
	    break;
	case '1':
	    place_tidyup();
	    break;
	case '2':
	    place_add_a_row();
	    break;
	case '3':
	    place_reverse();
	    break;
	default:
	    alert(menu+'::'+item+' -- not implemented');
	    break;
	}
	break;
    default:
	alert("Error - unimplemented split-menu");
	break;
    }
    KillMenu();
}

function Show_Help(box,type,module,file) {
    if(type=='plugin') {
	var fn = '../plugins/gazetteer/'+module+'/'+file;
	var bid = $('plugin_help_'+box);
	var bic = $('plugin_icon_'+box);
	if(bid.innerHTML) {               // only needs loading once
	    if(bid.style.display == 'block') {
		bid.style.display = 'none';
		bic.src = bic.src.replace('tinyup.png','tinydown.png');
	    } else {
		bid.style.display = 'block';
		bic.src = bic.src.replace('tinydown.png','tinyup.png');
	    }
	} else {
	    var xm = New_XMLHTTP();
	    xm.onreadystatechange=function() {
		if (xm.readyState==4) {
		    if (xm.status==200) {
			bid.innerHTML = xm.responseText;
			bid.style.display = 'block';
			bic.src = bic.src.replace('tinydown.png','tinyup.png');
		    }
		}
	    };
	    xm.open("GET",fn,true);
	    xm.send(null);
	}
    }
}

// produces a message as a pop-up box
function Any_Messages() {
    if(typeof(cp_info.error_message) != "undefined" && cp_info.error_message != "") {
	var q = document.getElementById('workspace');
	if(q == null) {
	    var wksp = document.createElement('div');
	    wksp.style.display = 'none';
	    wksp.id = 'workspace';
	    document.body.appendChild(wksp);
	    q = wksp;
	}
	var xmlhttp = XMLHTTP_Item();
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
		    q.innerHTML = xmlhttp.responseText;
		    q.innerHTML+='<div style="align:center; width:10%; margin-left:auto; margin-right:auto; padding-top:0.5em"><button type="button" onclick="Kill_Popup(\'any\')">OK</button></div>';
		    Popup('workspace','','ErrorMessage');
		}  
	    }
	};
	xmlhttp.open("GET", "message.cgi?lang="+cp_info.language+"&what="+cp_info.error_message, true);
	xmlhttp.send(null);
    }
}

function Doemall(todo) {
    if(Do_Once(todo) == false)
	setTimeout(function() {Doemall(todo)},500);   // try again in 1/10 of a second
}
