//
//  File:     iec_main.js
//
//  Additional header information is found after the 
//             section for editable site messages.
//
// Instructions for display of a site message:
//
//  1. Put your message between the quotes in the line(s) below.  You have 
//     a maximum of 3 lines.  Do not use the quote character (") in your text 
//     (see IT support if you need to do this).  
//     
//     EXAMPLE with MESSAGE:
//     var line1 = "SITE INFO: Database searches will be unavailable on ";
//     var line2 = "2004.09.01 from 17:30 to 18:00 CET due to maintenance.";
//     var line3 = "IEC Web Store (http://webstore.iec.ch) is not affected.";
//     
//  2. Save the file.  
//
//  3. Test the message in site1.  First close all open browser sessions, 
//     since the message will only be shown once per browser session 
//     (ie. it will not show again if you refresh).  Open a new browser 
//     session and enter the url ( http://site1.iec.ch ).  
//
//     If OK, upload the file with Dreamweaver to the iectest site, and
//     and test the url ( http://iectest.iec.ch ), exiting and restarting
//     browsers as before.  
//
//     If OK, upload the file to the www site by uploading the "jscript"
//     directory using the site copy facility of http://webadmin.iec.ch.
//     Make final test with http://www.iec.ch (again, having exited and
//     restarted browsers).
//
//  4. To remove the message, repeat the above, changing the text to 
//     empty lines (e.g. var line1 = "";) -- do not delete the lines entirely.
//     
//     EXAMPLE no MESSAGE:
//     var line1 = "";
//     var line2 = "";
//     var line3 = "";
//     
//  5. Do not make any changes to the file below these 3 lines.
//

var line1 = "";
var line2 = "";
var line3 = "";

//////////////////////////////////////////////////////////////////////
//
//  DO NOT MAKE ANY CHANGES BEYOND THIS POINT !!! 
//
//  File:  iec_main.js
//
//  Purpose:  JavaScripts common to all IEC pages combined in a 
//            single file.  Top portion of file may be edited to 
//            display site messages.
//
//  Created from individual files by LBM and SWC, 2007-06
//
//  Modifications:
//
//
//


var browser_model = '';
var cadebug = '';
var ca_normal_css = '';
var ca_print_css  = '';
var emf_win = null;
var ia_normal_css = -1;
var ia_print_css  = -1;
var show_debug = 0;
var win = null;
var _jsver = 0;

//  
//  Copyright date calculator
//
function cpyear() { 
  var time = new Date();
  time.getDate();
  var year = time.getYear();
  if (year < 2000) { year = year + 1900; }
  document.write(year); 
}  // end of function cpyear()

//
//  Function to unscramble an email address; changes link in an
//              anchor into a "mailto:" link
//
function mailsend(mail,p1,p2,p3,p4) { 
  mail.href = "mailto:" + p2 + p3 + "\@" + p4 + p1; 
  return true; 
}  // end function mailsend()

//
//  Function to test browser compatibility and/or display a site message
//
function ns4cookie() {
  var message_string = '';
  if(document.all) { ; }
  else if (document.getElementById) { ; }
  else {
    message_string = "Your browser will not support all of the features on "
                     + "this site.\nWe suggest that you upgrade or change "
                     + "your browser.\n[Reason: browser DOM not supported].";
  }
  if( _jsver == 1.0 && !message_string ) {
    message_string = "Your browser will not support all of the features on "
                     + "this site.\nWe suggest that you upgrade or change "
                     + "your browser.\n[Reason: JavaScript version lower "
                     + "than 1.2].";
  }

  var sitemsg = _make_site_msg();
  if( sitemsg != "" ) {
    message_string = sitemsg + "\n\n" + message_string;
  }

  if( message_string ) {
    var iwrite = 0;
    var start = document.cookie.indexOf("jsmsg=");
    var ia = start + 6;
    if((!start) && ("jsmsg" != document.cookie.substring(0,5))) { iwrite = 1; }
    if(start == -1) { iwrite = 1; }
    if(iwrite) {
      document.cookie = 'jsmsg=done;path=/;domain=www.iec.ch';
      alert(message_string);
    }
  }
  return;
}  // end of function ns4cookie()

//
// function to pop-up for email to a friend page
//
function emf_form( anchor_name,                   
                   new_window_name,
                   new_window_width,
                   new_window_height,
                   new_window_scroll,
                   new_window_position
                 ) {

  // set default values (i.e. values not in the onClick call)
  if( !anchor_name ) { anchor_name = "pagetitle"; }
  if( !new_window_name ) { new_window_name = 'emf_to_friend'; }
  if( !new_window_width ) { new_window_width = 700; }
  if( !new_window_height ) { new_window_height = 600; }
  if( !new_window_scroll ) { new_window_scroll = 'yes'; }
  if( !new_window_position ) { new_window_position = 'center'; }

  var astring;    // debugging alerts
  astring = "Defaults: \n"
          + "anchor_name = " + anchor_name + "\n"
          + "new_window_name = " + new_window_name + "\n"
          + "new_window_width = " + new_window_width + "\n"
          + "new_window_height = " + new_window_height + "\n"
          + "new_window_scroll = " + new_window_scroll + "\n"
          + "new_window_position = " + new_window_position + "\n"
          ;
  // alert(astring);
  astring = "";

  var LeftPosition = 0; 
  var TopPosition = 20;
  
  if(new_window_position == "random") {
    LeftPosition = (screen.availWidth) 
                   ? Math.floor(Math.random() 
                     * (screen.availWidth - new_window_width))
                   :400;
    TopPosition = (screen.availHeight)
                  ? Math.floor(Math.random() 
                    * ((screen.availHeight-new_window_height) - 75))
                  :50;
  }
  else if(new_window_position == "center") {
    LeftPosition = (screen.availWidth)
                   ? (screen.availWidth - new_window_width)/2
                   :400;
    TopPosition = (screen.availHeight)
                  ? (screen.availHeight-new_window_height)/2
                  :50;
  }
  else if(new_window_position == "default") {
    LeftPosition = 400;
    TopPosition = 50
  }
  else {
    LeftPosition = 0;
    TopPosition = 20;
  }

  var nw_settings = 'width=' + new_window_width 
                  + ',height=' + new_window_height 
                  + ',top=' + TopPosition
                  + ',left=' + LeftPosition
                  + ',scrollbars='+ new_window_scroll
                  + ',location=no'
                  + ',directories=no'
                  + ',status=no'
                  + ',menubar=no'
                  + ',toolbar=no'
                  + ',resizable=no';

  var title = '';
  var pageref = '';

  var anm = anchor_name.match(/^#/);
  if( anm != null ) { 
    anchor_name = anchor_name.substr(1);
    pageref = '%23' + anchor_name;
  }  

  astring = "Use url + #reference for link\n"
          + "anchor_name is now: " + anchor_name + "\n"
          + "pageref is now: " + pageref;
  // alert(astring);
  astring = "";

  var aref = document.anchors;
  var anchor_number = -1;
  for ( var ia = 0; ia < aref.length; ia++ ) {
    if( aref[ia].name == anchor_name ) { anchor_number = ia; }
  }

  if( anchor_number < 0 ) {
    title = document.title;
    // now remove everything from the first '>'
    pageref = '';
  }
  else { 
    aref = document.anchors[anchor_number];
    if( aref.text ) { title = aref.text; }
    else if ( aref.innerText ) { title = aref.innerText; }
    else { title = document.title; }
  }

  var pqs = '';
  if( window.pseudo_query_string ) {
    pqs = window.pseudo_query_string;
  }

  var docurl = document.URL;
  var window_url = '/cgi-bin/emf_form.pl?title=' + title;
  astring = window_url + "\n";

  if( docurl.match('cgi-bin') != null ) {
    var iquestion = docurl.indexOf('?');
    if( iquestion < 0 ) {
      // POST
      window_url = window_url + '&docurl=' + docurl + '&pqs=' + pqs;
      astring = astring + "dynamic/POST\n&docurl = " + docurl;
    }
    else {
      // GET -- split at '?'
      window_url = window_url + '&docurl=' + docurl.substr(0,iquestion)
                 + '&pqs=' + pqs;
      astring = astring + "dynamic/GET\n&docurl = " 
                        + docurl.substr(0,iquestion);
    }
    astring = astring + "\n&pqs = " + pqs;
  }
  else {
    window_url = window_url + '&docurl=' + docurl + pageref;
    astring = astring + " (static page)\n&docurl = " + docurl + pageref;
    window_url = window_url + '&docurl=' + docurl;
    astring = astring + " (static page)\n&docurl = " + docurl;
  }

  astring = astring + "\n"
                    + "window_url = " + window_url + "\n"
                    + "new_window_name = " + new_window_name + "\n"
                    + "nw_settings = " + nw_settings + "\n"
                    + "Title: " + title ;
  if(emf_win == null) { ; }
  else { emf_win.close(); }
  
  emf_win = window.open(window_url,new_window_name,nw_settings);
  if(emf_win.focus) { emf_win.focus(); }

  return false;
}  // end of function emf_form()

//
//  function escape_4chars - needed escapes so pop-up URLs will work
//
function escape_4chars(instr) {
  var ca;
  ca = instr.replace(/_/g,"_5f");
  ca = ca.replace(/\?/g,"_3f");
  ca = ca.replace(/\&/g,"_26");
  ca = ca.replace(/\#/g,"_23");
  ca = ca.replace(/ /g,"_20");
  return ca;
}  // end of function escape_4chars()

//
//  switch to printer friendly view
//
function printerFriendlyView() {
  if( ia_normal_css < 0 ) { determine_browser(); }
  if( ia_normal_css > 900 ) { return; }
  cadebug = 'Switching to print view' + "\n\n";

  if( browser_model == "MSIE" ) {
    document.styleSheets[ia_normal_css].href = ca_print_css;
    cadebug = cadebug + 'using MSIE object' + "\n";
  }
  else if( browser_model == "Safari" ) {
    ca_normal_css.disabled = true;
    ca_print_css.disabled = false;
    cadebug = cadebug + 'using Safari object' + "\n";
  }
  else {
    document.styleSheets[ia_normal_css].disabled = true;
    document.styleSheets[ia_print_css].disabled = false;
    cadebug = cadebug + 'using Mozilla object' + "\n";
  }  
  if(show_debug == 1) { alert(cadebug); }
  cadebug = '';
} // end of function printerFriendlyView()

//
// function normalView()
//
function normalView() {
  if( ia_normal_css < 0 ) { determine_browser(); }
  if (ia_normal_css > 900 ) { return; }
  cadebug = 'Switching to normal view' + "\n\n";

  if( browser_model == "MSIE" ) {
    document.styleSheets[ia_normal_css].href = ca_normal_css;
    cadebug = cadebug + 'using MSIE object' + "\n"; 
  }
  else if( browser_model == "Safari" ) {
    ca_print_css.disabled = true;
    ca_normal_css.disabled = false;
    cadebug = cadebug + 'using Safari object' + "\n";
  }
  else {
    document.styleSheets[ia_print_css].disabled = true;
    document.styleSheets[ia_normal_css].disabled = false;
    cadebug = cadebug + 'using Mozilla object' + "\n";
  }
  if(show_debug == 1) { alert(cadebug); }
  cadebug = '';
}  // end of function normalView

//
//  function loadCSSchanger() -- alternate name for determin_browser()
//
function loadCSSchanger() {
  determine_browser();
}  //  end of function loadCSSchanger()

//
//  function determine_browser()
//
function determine_browser() {
  var browser_string;
  if( navigator.appName ) {
    browser_string = navigator.appName;
    if( navigator.appVersion ) {
      browser_string = browser_string + "  " + navigator.appVersion;
    }
    cadebug = cadebug + "browser_string = " + browser_string + "\n";
  }

  if( browser_string.indexOf("Safari") > -1 ) {
    browser_model = "Safari";
  }
  else if( browser_string.indexOf("Opera") > -1 ) {
    browser_model = "Opera";
  }
  else if( browser_string.indexOf("MSIE") > -1 ) {
    browser_model = "MSIE";
  }

  if( browser_model == 'Safari' ) {
    // find name of normal_view.css and print_view.css for Safari
    var link_obj; 
    var link_title;
    for ( var ia = 0; 
              ( link_obj = document.getElementsByTagName("link")[ia] );
              ia++
        ) {
      link_title = link_obj.getAttribute("title");
      cadebug = cadebug + "ia = " + ia + "   link_title = " + link_title + "\n";
      if( ia_normal_css < 0 && link_title == 'normal_view' ) {
        cadebug = cadebug + "setting normal view" + "\n";
        ia_normal_css = ia;
        ca_normal_css = link_obj;
      }
      else if ( ia_print_css < 0 && link_title == 'printer_view' ) {
        cadebug = cadebug + "setting print view" + "\n";
        ia_print_css = ia;
        ca_print_css = link_obj;
      }
    }
  }

  else {
    // find index and string for normal_view.css and print_view.css
    //      (except Safari)
    for ( var ia = 0; ia < document.styleSheets.length; ia++ ) {
      ca = document.styleSheets[ia].href;
      cadebug = cadebug + 'style sheet ' + ia + ' is ' + ca + "\n";
      if( ia_normal_css < 0 && 
          ( regex_obj = ca ? ca.match(/normal_view\.css/) : null ) != null ) { 
        ia_normal_css = ia; 
        ca_normal_css = ca;
      }
      if( ia_print_css < 0 &&
          ( regex_obj = ca ? ca.match(/printer_view\.css/) : null ) != null ) { 
        ia_print_css = ia; 
        ca_print_css = ca;
      }
    }    

    cadebug = cadebug + "\n" + 'ia_normal_css = ' + ia_normal_css 
                      + '   ia_print_css = ' + ia_print_css + "\n";
    cadebug = cadebug + "condition ia_print_css < 0 && ia_normal_css > -1\n";
    if(show_debug == 1) { alert(cadebug); }
    cadebug = ''; 
  }

  cadebug = cadebug + "\n" + 'ia_normal_css = ' + ia_normal_css 
                           + '   ia_print_css = ' + ia_print_css + "\n"
                           + 'browser_model = ' + browser_model;
  if(show_debug == 1) { alert(cadebug); }
  cadebug = '';

  // set to impossibility to avoid looping
  if( ia_normal_css < 0 ) { ia_normal_css = 999; }
  if( ia_print_css  < 0 ) { ia_normal_css  = 999; }

  if( browser_model == 'Opera' || browser_model == 'Safari' ) {
    // go through both views -- required for proper initialization in Opera
    printerFriendlyView();
    normalView();
  }

  return;
}  // end of function determine_browser()

//
//  function NewWindow()
//
function NewWindow(mypage,myname,w,h,scroll,pos) {
  if( pos=="random" ) {
    LeftPosition = (screen.availWidth) 
                 ? Math.floor(Math.random()*(screen.availWidth-w)) 
                 : 400;
    TopPosition = (screen.availHeight)
                ? Math.floor(Math.random()*((screen.availHeight-h)-75))
                : 50;
  }
  if( pos=="center" ) {
    LeftPosition = (screen.availWidth)
                 ? (screen.availWidth-w)/2
                 : 400;
    TopPosition = (screen.availHeight)
                ? (screen.availHeight-h)/2
                : 50;
  }
  if( pos=="default" ) {
    LeftPosition = 400;
    TopPosition=50;
  }
  else if( (pos != "center" && pos != "random" && pos!="default") 
           || pos == null ) {
    LeftPosition = 0;
    TopPosition = 20;
  }
  settings = 'width=' + w 
             + ',height=' + h
             + ',top=' + TopPosition
             + ',left=' + LeftPosition
             + ',scrollbars=' +scroll
             + ',location=no'
             + ',directories=no'
             + ',status=no'
             + ',menubar=no'
             + ',toolbar=no'
             + ',resizable=no';
  win = window.open(mypage,myname,settings);
  if( win.focus ) { win.focus(); }
}  // end of function NewWindow

//
//  function url_change()
//
function url_change() {
  var lang_select = document.forms["electropedia"].elements["criteria"].options;

  var search_term = document.forms["electropedia"].elements["value"].value;
  var search_type = '';
  var search_lang = '';

  for ( var ia = 0; ia < lang_select.length; ia++ ) {
    if( lang_select[ia].selected ) {
      search_lang = lang_select[ia].value;
    }
  }

  if( search_lang == 'IEV_No' ) {
    search_type = 'IEVref';
    search_lang = 'SearchViewEN';
  }
  else {
    search_type = 'term';
    if( search_lang == '' ) { search_lang = 'EN'; }
    search_lang = 'SearchView' + search_lang;
  }

  var new_url = 'http://www.electropedia.org/iev/iev.nsf/' 
                + search_lang;
  document.forms["electropedia"].elements["Query"].value =
                  'field ' + search_type + '="' + search_term + '"';

  document.forms["electropedia"].action = new_url;

  return true;
}  // end of function url_change()

//
// Trim function
//
function _jstrim(in_string,trim_flag) {
  if( !trim_flag ) { trim_flag = ''; }
  trim_flag = trim_flag.toLowerCase();

  var out_string = in_string;
  if( out_string ) {
    if( trim_flag != 'r' ) { out_string = out_string.replace(/^\s+/,''); }
    if( trim_flag != 'l' ) { out_string = out_string.replace(/\s+$/,''); }
  }
  return out_string;
} // end of function _jstrim()

//
//  Message preparation function - called by ns4cookie
//
function _make_site_msg() {
  var ca = _jstrim(line1);
  var cb = _jstrim(line2);
  var cc = _jstrim(line3);
  var sitemsg = '';
  if(ca != '') { sitemsg = line1; }
  if(cb != '') { 
    if(sitemsg != '') { sitemsg += "\n" + line2; }
    else { sitemsg = line2; }
  }
  if(cc != '') {
    if(sitemsg != '') { sitemsg += "\n" + line3; }
    else { sitemsg = line3; }
  }
  return sitemsg;
}  // end of function _make_site_msg()


