// Index+ CMS [main.js 2.14] ©System Simulation Ltd

function trim(str)
{
  str.replace(/^\s*/,'').replace(/\s*$/,'');
  return str;
}

function set_domain(my_domain)
{
  //alert("Changing document.domain\nfrom "+document.domain+"\nto "+my_domain);
  document.domain = my_domain;
}

function start(new_location)
{
 parent.connectiframe.parent.location = new_location;
}

function bookmark(new_location)
{
 start(new_location);
 alert("Following this refresh, you can use your browser to bookmark this link in the normal way.");
}

function printer_friendly(url)
{
 var pf = window.open(url, '', 'scrollbars=yes,menubar=no');
}

function print_this_page()
{
 window.print();
}

function paste_term_to_target(term, target, multi)
{
//  alert("target = "+target);
  var qterm = term.replace(/'/, "\\'"); // requote single quotes
  if (multi == 'y' || multi == 'Y') {
    var tar;
    eval("tar = "+target);
    if (trim(tar)) {
      var arr = tar.split("\n");
      arr[arr.length] = term;   // don't need quoting here
//      arr.sort(); // to sort or not to sort?
      var newtar = arr.join("\n");
      eval(target+" = newtar");
      opener.focus()
    } else {
      eval(target+" = '\""+qterm+"\"';");
      opener.focus();
    }
  } else {
//    alert("Evaluating\n"+target+" = '"+qterm+"';");
    eval(target+" = '\""+qterm+"\"';");
    opener.focus();
    this.close();
  }
}

function paste_term_to_target_append(term, target, multi)
{
	var qterm = term.replace(/'/, "\\'"); // requote single quotes
	var newtar = eval(target) + "\n" + qterm;
    eval(target+" = newtar");
    opener.focus();
    this.close();
}

function paste_term_to_edit_target(term, target, multi)
{
//  alert("target = "+target);
  var qterm = term.replace(/'/, "\\'"); // requote single quotes
  if (multi == 'y' || multi == 'Y') {
    var tar;
    eval("tar = "+target);
    if (trim(tar)) {
      var arr = tar.split("\n");
      arr[arr.length] = term;   // don't need quoting here
//      arr.sort(); // to sort or not to sort?
      var newtar = arr.join("\n");
      eval(target+" = newtar");
      opener.focus()
    } else {
      eval(target+" = '"+qterm+"';");
      opener.focus();
    }
  } else {
//    alert("Evaluating\n"+target+" = '"+qterm+"';");
    eval(target+" = '"+qterm+"';");
    opener.focus();
    this.close();
  }
}

function paste_term_to_edit_target_thes(term, target, thes_id, thes_id_target, thes_h, thes_h_target, multi)
{
//  alert("target = "+target);
  var qterm = term.replace(/'/, "\\'"); // requote single quotes
  if (multi == 'y' || multi == 'Y') {
    var tar;
    eval("tar = "+target);
    if (trim(tar)) {
      var arr = tar.split("\n");
      arr[arr.length] = term;   // don't need quoting here
//      arr.sort(); // to sort or not to sort?
      var newtar = arr.join("\n");
      eval(target+" = newtar");
      opener.focus()
    } else {
      eval(target+" = '"+qterm+"';");
      opener.focus();
    }
  } else {
//    alert("Evaluating\n"+target+" = '"+qterm+"';");
    eval(target+" = '"+qterm+"';");
    eval(thes_id_target+" = '"+thes_id+"';");
    eval(thes_h_target+" = '"+thes_h+"';");
    opener.focus();
    this.close();
  }
}

function do_paste(qterm, target, multi)
{
   if (opener != null) { // must have a real window to paste to
      var url = document.URL;
      var reg = /^\w+:\/\/\w+\.([\w.]+)/;
      var result = url.match(reg);
      if (!result) {
        reg = /^\w+:\/\/([\w.]+)/;
        result = url.match(reg);
      }
      if (result) {
        try {
          document.write('<a href="#" onClick="paste_term_to_target(\''+qterm+'\',\''+target+'\',\''+multi+'\');">Copy&nbsp;this&nbsp;term&nbsp;to&nbsp;your&nbsp;form</a>');
        }
        catch (e) {}
      }
   }
}

function do_paste_edit(qterm, target, multi)
{
   if (opener != null) { // must have a real window to paste to
      var url = document.URL;
      var reg = /^\w+:\/\/\w+\.([\w.]+)/;
      var result = url.match(reg);
      if (!result) {
        reg = /^\w+:\/\/([\w.]+)/;
        result = url.match(reg);
      }
      if (result) {
        try {
          document.write('<a href="#" onClick="paste_term_to_edit_target(\''+qterm+'\',\''+target+'\',\''+multi+'\');">Copy&nbsp;this&nbsp;term&nbsp;to&nbsp;your&nbsp;form</a>');
        }
        catch (e) {}
      }
   }
}

function do_paste_cs(qterm, target, multi)
{
   if (opener != null) { // must have a real window to paste to
      var url = document.URL;
      var reg = /^\w+:\/\/\w+\.([\w.]+)/;
      var result = url.match(reg);
      if (!result) {
        reg = /^\w+:\/\/([\w.]+)/;
        result = url.match(reg);
      }
      if (result) {
        try {
          document.write('<a href="#" onClick="paste_term_to_target_append(\''+qterm+'\',\''+target+'\',\''+multi+'\');">Copy&nbsp;this&nbsp;term&nbsp;to&nbsp;your&nbsp;form</a>');
        }
        catch (e) {}
      }
   }
}

function do_paste_flat_auth(qterm, target, multi)
{
   if (opener != null) { // must have a real window to paste to
      var url = document.URL;
      var reg = /^\w+:\/\/\w+\.([\w.]+)/;
      var result = url.match(reg);
      if (!result) {
        reg = /^\w+:\/\/([\w.]+)/;
        result = url.match(reg);
      }
      if (result) {
        try {
          document.write('<a href="#" onClick="paste_term_to_target_append(\''+qterm+'\',\''+target+'\',\''+multi+'\');">'+qterm+'</a>');
        }
        catch (e) {}
      }
   }
}

function do_paste_edit_thes(qterm, target, thes_id, thes_id_target, thes_h, thes_h_target, multi)
{
   if (opener != null) { // must have a real window to paste to
      var url = document.URL;
      var reg = /^\w+:\/\/\w+\.([\w.]+)/;
      var result = url.match(reg);
      if (!result) {
        reg = /^\w+:\/\/([\w.]+)/;
        result = url.match(reg);
      }
      if (result) {
        try {
          document.write('<a href="#" onClick="paste_term_to_edit_target_thes(\''+qterm+'\',\''+target+'\',\''+thes_id+'\',\''+thes_id_target+'\',\''+thes_h+'\',\''+thes_h_target+'\',\''+multi+'\');">Copy&nbsp;this&nbsp;term&nbsp;to&nbsp;your&nbsp;form</a>');
        }
        catch (e) {}
      }
   }
}

function copy_term_to_clipboard(term)
{
   var qterm = term.replace(/'/, "\\'"); // requote single quotes
   try {
      window.clipboardData.clearData();
      window.clipboardData.setData("Text", qterm);
      alert("\"" + qterm + "\" copied to clipboard");
   }
   catch (e) {}
}


function do_copy(qterm)
{
   try {
      document.write('<a href="#" onClick="copy_term_to_clipboard(\''+qterm+'\');">Copy&nbsp;this&nbsp;term&nbsp;to&nbsp;your&nbsp;clipboard</a>');
   }
   catch (e) {}
}


function set_context(new_context, item)
{
   try {
      item.name = new_context;
   }
   catch (e) {}
}

function set_default_context(context_true, context_false, item, test)
{
   try {
      if (test == true) {
         item.name = context_true;
      } else {
         item.name = context_false;
      }
   }
   catch (e) {}
}

function get_term(search_url, template_url, input_item_value)
{
   try {
      var url
      if (trim(input_item_value)) {
         url = search_url + input_item_value
      } else {
         url = template_url
      }
      newWimdow = window.open(url)
   }
   catch (e) {}
}

function get_flat_term(search_url, input_item_value)
{
   try {
      var url = search_url
      newWimdow = window.open(url)
   }
   catch (e) {}
}

function get_term_cs(search_url, template_url, input_item_value)
{
   try {
      var url = template_url
      newWimdow = window.open(url)
   }
   catch (e) {}
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

function hideHelp() {
 document.getElementById('helpdisplay').style.position='absolute';
 document.getElementById('helpdisplay').style.visibility='hidden';
 document.getElementById('helpshow').style.position='relative';
 document.getElementById('helpshow').style.visibility='visible';
}

function showHelp() {
 document.getElementById('helpdisplay').style.position='relative';
 document.getElementById('helpdisplay').style.visibility='visible';
 document.getElementById('helpshow').style.position='absolute';
 document.getElementById('helpshow').style.visibility='hidden';
}

function hideHelpById(id) {
 document.getElementById('helpdisplay'+id).style.position='absolute';
 document.getElementById('helpdisplay'+id).style.visibility='hidden';
 document.getElementById('helpshow'+id).style.position='relative';
 document.getElementById('helpshow'+id).style.visibility='visible';
}

function hideHelpById2(id) {
 document.getElementById('helpdisplay'+id).style.position='absolute';
 document.getElementById('helpdisplay'+id).style.visibility='hidden';
 document.getElementById('helpshow'+id).style.visibility='visible';
}

function showHelpById(id) {
 document.getElementById('helpdisplay'+id).style.position='relative';
 document.getElementById('helpdisplay'+id).style.visibility='visible';
 document.getElementById('helpshow'+id).style.position='absolute';
 document.getElementById('helpshow'+id).style.visibility='hidden';
}

function showHelpById2(id) {
 document.getElementById('helpdisplay'+id).style.position='relative';
 document.getElementById('helpdisplay'+id).style.visibility='visible';
 document.getElementById('helpshow'+id).style.visibility='hidden';
}

function showItem(id) {
 document.getElementById(id).style.display='block';
}

function hideItem(id) {
 document.getElementById(id).style.display='none';
}

function showTab(tab) {
 hideItem('tab0captionon');
 hideItem(tab+'captionoff');
 showItem('tab0captionoff');
 showItem(tab+'captionon');
 showItem(tab);
}

function hideTab(tab) {
 hideItem(tab);
 hideItem(tab+'captionon');
 showItem(tab+'captionoff');
}

function showTab0() {
 showItem('tab1');
 showItem('tab2');
 showItem('tab3');
 showItem('tab4');
 hideItem('tab1captionon');
 hideItem('tab2captionon');
 hideItem('tab3captionon');
 hideItem('tab4captionon');
 hideItem('tab0captionoff');
 showItem('tab1captionoff');
 showItem('tab2captionoff');
 showItem('tab3captionoff');
 showItem('tab4captionoff');
 showItem('tab0captionon');
}

function showTab1() {
 hideTab('tab2');
 hideTab('tab3');
 hideTab('tab4');
 showTab('tab1');
}

function showTab2() {
 hideTab('tab1');
 hideTab('tab3');
 hideTab('tab4');
 showTab('tab2');
}

function showTab3() {
 hideTab('tab1');
 hideTab('tab2');
 hideTab('tab4');
 showTab('tab3');
}

function showTab4() {
 hideTab('tab1');
 hideTab('tab2');
 hideTab('tab3');
 showTab('tab4');
}

function initTabs() {
 if (document.getElementById('tabstopdiv')) {
  showItem('tabstopdiv');
  showItem('tabsbottomdiv');
  showItem('tabsdiv');
  showTab1();
 }
}

function onLoadInit() {
 externalLinks();
 initTabs();
}

window.onload = onLoadInit;
