/* 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;
    }
  }
  if(field=="opt_dist_fmt")
    change_calculation_unit();
}

// 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;
}

function Toggle_Submenu(item) {
  a = $('submenu_'+item);
  b = $('edit_arrow_'+item);
  if(a.style.display == 'block') {
    a.style.display = 'none';
    b.src = b.src.replace('tinyup.png','tinydown.png');
  } else {
    a.style.display = 'block';
    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;
}

/* Functions to cause the options screen to be regenerated when you
change your calculation units.  First one to be called directly from
the button */ 

function change_calculation_unit(chgto) {
  var x = document.forms['canalform'].elements['opt_dist_fmt'];
  var y = document.forms['canalform'].elements['lastunit'];
  for(var i=0; i<x.length; i++) {
    if(x[i].checked == true) {
      if(x[i].value != y.value) {
	// force a submit
	document.forms['canalform'].elements.submit[0].value="javaopts";
	document.forms['canalform'].elements.submit[0].click();
	return;
      }
    }
  }
}
// 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.display="block";
    menu.style.left= x+"px";
    menu.style.top = y+"px";
    pm_stuff.which_place = pc;
  } else {
    pm_stuff.currentmenu = menu;
    pm_stuff.lastcurrentmenu = menu;
    pm_stuff.timer = setTimeout(HideMenu, 1000);;
  }
}

// called by timer
function HideMenu() {
  if(pm_stuff.currentmenu)
    pm_stuff.currentmenu.style.display = "none";
}

function KillMenu() {
  if(pm_stuff.lastcurrentmenu)
    pm_stuff.lastcurrentmenu.style.display = "none";
}

function KeepMenu(what,flg) {
  var menu = document.getElementById(what);
  if(flg) {
    pm_stuff.currentmenu = null;     // prevent it vanishing
    menu.style.display="block";
  } else {
    menu.style.display="none";
  }
}

// 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)},100);   // try again in 1/10 of a second
}
