var 
  childWindows = new Object;

var agt=navigator.userAgent.toLowerCase();

//BROWSER VERSION ***
//IE5 returns 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
// If you want to allow spoofing, take out the tests for opera and webtv.
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
            && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
            && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly      = (is_nav && ((agt.indexOf(";nav") != -1) ||
                      (agt.indexOf("; nav") != -1)) );
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_gecko = (agt.indexOf('gecko') != -1);

var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5")==-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);


function closeChildren() {
  var 
    win,
    winName;

  for (winName in childWindows) {
    win = childWindows[winName];
    if (win != null) {
      win.close();
      childWindows[winName] = null;
    }
  }
}



//lose event instance so it doesn't bubble up and trigger parents .. browser specific
function disableEvent(e) {
  //alert("DISABLE: X1");
  if (is_ie) {
    //alert("DISABLE: IE: " + window.event.type);
    window.event.cancelBubble = true
  } else {
    //alert("DISABLE: NS IN: " + e);
    //alert("DISABLE: NS: " + e.event);
    //e.preventBubble();
    e.stopPropagation();
    //alert("DISABLE: NS OUT: " + e.event);
  };
  //alert("DISABLE: X2");
}



//NB: className is a String not an array of Objects hence coding
function flipflop(div) {
  //test("Starting FlipFlopping: " + div.id);
  if (div.style.display=='') div.style.display='block'; else div.style.display=''; 
  //test("Finished FlipFlopping: " + div.id);

  //if (div.className.indexOf("invisible") >= 0) {
  //  div.className = div.className.replace("invisible", "visible");
  //} else {
  //  div.className = div.className.replace("visible", "invisible");
  //}
}


//flip my (following) internal div (IE) 
function flipflopchildIE(div) {
  var 
    child;
    //childTag,
    //count;

  if (is_nav) {
    //alert("IS NAV"); 
    return;
  };

  child = div.getElementsByTagName('div')[0]; //my first DIV
  //test("FlipFlop: " + child.id + " .. child of " + div.id);
  //if (is_nav) {alert("NAV")} else {alert("NOT NAV")};
  //if (is_ie) {alert("IE")} else {alert("NOT IE")};

  flipflop(child);
  disableEvent(div); 
  return;
}



//flip my (following) internal div (using W3C events)
function flipflopchildW3C(evt) {
var 
  lchild, 
  lparent,
  ltarget; 

ltarget=evt.target;
//test("FlipFlop2.T: " + ltarget + " : " + ltarget.id + " : " + ltarget.tagName);
lparent=ltarget.parentNode;
//test("FlipFlop2.P: " + lparent + " : " + lparent.id + " : " + lparent.tagName);
if ((ltarget.tagName != "DIV") && (ltarget.tagName != "LI")) {
  ltarget=ltarget.parentNode;
} 
lchild = ltarget.getElementsByTagName('div')[0]; //my first DIV
//test("FlipFlop2.C: " + lchild + " : " + lchild.id + " : " + lchild.tagName);
flipflop(lchild);
evt.stopPropagation();
}


//first visit?
function GetCookie(name) {
  var arg=name+"=";
  var alen=arg.length;
  var clen=document.cookie.length;
  var i=0;
  while (i<clen) {
    var j=i+alen;
    if (document.cookie.substring(i,j)==arg)
      return "here";
    i=document.cookie.indexOf(" ",i)+1;
    if (i==0) break;
  }
  return null;
}



function init() {
  window.innerWidth += 1;   //fudge for NS
  //alert("INIT ENTER");
  if (is_nav) {
    //alert("INIT ENTER NAV");
    //document.getElementById('answers').addEventListener('click',    flipflopchildW3C, false)
    //document.getElementById('aboutus').addEventListener('click',    flipflopchildW3C, false)
    //document.getElementById('whatis').addEventListener('click',     flipflopchildW3C, false)
    //document.getElementById('howdoesit').addEventListener('click',  flipflopchildW3C, false)
    //document.getElementById('benefits').addEventListener('click',   flipflopchildW3C, false)
    //document.getElementById('suitable').addEventListener('click',   flipflopchildW3C, false)
    //document.getElementById('risks').addEventListener('click',      flipflopchildW3C, false)
    //document.getElementById('painful').addEventListener('click',    flipflopchildW3C, false)
    //document.getElementById('bothonsameday').addEventListener('click', flipflopchildW3C, false)
    //document.getElementById('howlong').addEventListener('click',    flipflopchildW3C, false)
    //document.getElementById('howsoon').addEventListener('click',    flipflopchildW3C, false)
    //document.getElementById('cost').addEventListener('click',       flipflopchildW3C, false)
    //document.getElementById('contact').addEventListener('click',    flipflopchildW3C, false)
    //document.getElementById('mail').addEventListener('click',       flipflopchildW3C, false)
    //document.getElementById('disclaimer').addEventListener('click', flipflopchildW3C, false)
  } else {
    //alert("INIT ENTER IE");
    document.all.disclaimer.onclick=flipflopchild;
  };
  //alert("INIT EXIT");
}




function makeVisible(div) {
  disableEvent(div); 
  //loop up
  var myDiv = div;
  while (myDiv != null) {
    if (myDiv.className.indexOf("invisible") >= 0) {
      myDiv.className = myDiv.className.replace("invisible", "visible");
    } else {
    }
    myDiv = myDiv.parentNode;
  }
}


//make the named div the only visible one
function openDiv(div) {
  var 
    child,
    count;

  disableEvent(div); 
  //alert("openDiv: Start1: " + div.tagName + ":  " + div.id);  
  //alert("openDiv: Start1a: " + window.document.all.length);//_form.elements.length);  
  for (count = 0; count < document.all.length; count++) {
    child = document.all[count];
    if (child.tagName.toUpperCase() != "DIV") continue;
    //alert("openDiv: Start2(" + count + "): " + child.tagName + ": " + child.id + ": " + child.className);  
    if (child.id == div.id) {
      //alert("openDiv: Start2(matches)");  
      child.className = child.className.replace("invisible", "visible");
    } else {
      //alert("openDiv: Start2(doesnt match)");  
      if (child.className.indexOf("invisible") == -1) {
        child.className = child.className.replace("visible", "invisible");
      }
    }
  }
}


function openWindow(url, windowName) {
  var  
    displayWindow; 

  disableEvent(); 
  displayWindow = window.open(url, windowName, "width=400,height=400,resizable");
  childWindows[windowName] = displayWindow;
}



//display a Glossary item
function showGlossaryItem(anItem) {
  var 
    lowerItem,
    message;

  disableEvent(); 
  lowerItem = anItem.toLowerCase();
  if (lowerItem == "lasik") {
    message = "Laser Assisted in-situ Keratomileusis now the most common form of laser treatment. It involves an additional surgical step to make a hinged flap of cornea using an instrument called a microkeratome.  This eliminates the need for the removal of the surface layer of cells of the cornea so that visual recovery is enhanced.  Following the application of the laser treatment, the flap is replaced to its original position.  Clear vision is possible the next day with little discomfort enabling the rapid return to normal activities."
  } else if (lowerItem == "prk") {
    message = "Photorefractive Keratectomy uses a laser to treat the surface of the cornea. With PRK your vision will be clear within the first two to three days.  As the cornea heals, its new shape becomes smoother, enabling the vision to improve over time.  Within days after surgery you will notice that your distance vision without glasses has greatly improved and will continue to do so over subsequent weeks."
  } else {
    message = "Unknown glossary item"
  };
  showGlossaryMessage(anItem, message);
}


//display a Glossary message
function showGlossaryMessage(aTitle, aMessage) {
  var 
    aDiv,
    displayWindow, 
    divs,
    doc;  

  displayWindow = window.open("", "glossarywindow", "width=400,height=200,resizable");
  doc = displayWindow.document;
  doc.title = aTitle; 
  doc.open();
  doc.writeln('<HTML><HEAD><TITLE>' + aTitle + '</TITLE></HEAD>');
  doc.writeln('<BODY style="background-color:#DDEEEE;color:#006666;font-size:12pt;" onblur="close();"></BODY>');
  doc.writeln('<div style="font-size:120%;font-weight=bold;">' + aTitle + '</div>' +
              '<div><p/>' + aMessage + '</div>');
  doc.writeln('</BODY></HTML>');
  doc.close(); //flush to screen
}



function test(comment) {
  alert('DEBUG: ' + comment);
}

