// JavaScript Document

function OpenWin(FileToOpen,w,h){
      if (w=='' || w < 0 || w==null) w=640;
      if (h=='' || h < 0 || h==null) h=680;
	  if (h >= window.screen.availHeight) h = window.screen.availHeight-100;
	  if (w >= window.screen.availWidth) h = window.screen.availWidth-100;
  	  myWin=open(FileToOpen,"DisplayWidnow", "status=no,toolbar=no,menubar=no,Directories=no,resizable=yes,ScrollBars=yes,location=no,width="+w+",height="+h);
	  myWin.focus();	
}
function OpenWin2(FileToOpen,w,h){
      if (w=='' || w < 0 || w==null) w=650;
      if (h=='' || h < 0 || h==null) h=680;
	  if (h >= window.screen.availHeight) h = window.screen.availHeight-100;
	  if (w >= window.screen.availWidth) h = window.screen.availWidth-100;
  	  myWin=open(FileToOpen,"DisplayWidnow", "status=yes,toolbar=yes,menubar=yes,Directories=yes,resizable=yes,ScrollBars=yes,location=yes,width="+w+",height="+h);
	  myWin.focus();	
}
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=414,height=555');");
}
// was -->

function modifyEnterKey(e) {
  var key;
  if (window.event) {
    if (window.event.keyCode == 13) window.event.keyCode = 9; // IE
  } else if (e && e.which) {
    if (e.which == 13) {
      return false;
    }
  }
  return true;
}

function swapKeys() {
  if (window.event && window.event.keyCode == 13) window.event.keyCode = 9;
}

function changeFormLogic() {
  var x = document.getElementsByTagName("input");
  for (var i = 0; i < x.length; i++) {
    if(x[i].name && x[i].name == "keywords") continue;
    if (x[i].attachEvent) {
      x[i].attachEvent('onkeypress', modifyEnterKey);
      x[i].attachEvent('onkeydown', swapKeys);
    } else {
      x[i].onkeypress = function (e) { return modifyEnterKey(e); }
    }
  }
}


function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=450,height=500,screenX=150,screenY=30,top=30,left=150');
}


if (window.addEventListener)
  window.addEventListener("load", changeFormLogic, false)
else if (window.attachEvent)
  window.attachEvent("onload", changeFormLogic)
else if (window.onload)
  window.onload=changeFormLogic;



function rand(number) {
	return Math.ceil(Math.random()*number);
}

function SetClickTracksCookies()
{
	// This sets the persistent cookie for unique visitors and tracking latent purchases
	var thisCookie = GetCookie("CLICKTRACKS_PERSISTENT");
        var myValue = thisCookie;
	if( thisCookie == null)
	{
		//Setup the random cookie value
		myValue = new Date();
		var randNum = rand(100);		
		myValue = myValue.toGMTString() + "_" + randNum;

		//Setup the expiry date to expire in 2010
		var expiryDate = new Date();
		var date2010 = "Fri, 31 Dec 2030 23:00:00 EST";
		var dt = Date.parse(date2010);

		expiryDate.setTime(dt);

		SetCookie("CLICKTRACKS_PERSISTENT", myValue, expiryDate, "/", window.location.hostname);
	}     

	// This sets the session cookie for maintaining session integrity
	// Set to 15 minutes by default.  Change var minutes value to adjust session length
	
	var minutes = 15;
	var session = GetCookie("CLICKTRACKS_SESSION");
	var scdt = new Date();
	var sdt = new Date(scdt.getMilliseconds + (minutes * 60 * 1000));

        var sessionVal;
        if(session==null){
           sessionVal=myValue + "=" + scdt.toGMTString() + "_" + rand(100);
        }else{
           sessionVal=session;
        }

        SetCookie("CLICKTRACKS_SESSION", sessionVal, sdt, "/", window.location.hostname);
}


// ---------------------------------------------------------------
    //  Cookie Functions - Second Helping  (21-Jan-96)
    //  Written by:  Bill Dortch, hIdaho Design <BDORTCH@NETW.COM>
    //  The following functions are released to the public domain.
    //
    // "Internal" function to return the decoded value of a cookie
    //
    function getCookieVal (offset) {
      var endstr = document.cookie.indexOf (";", offset);
      if (endstr == -1)
        endstr = document.cookie.length;
      return unescape(document.cookie.substring(offset, endstr));
    }

    //
    //  Function to return the value of the cookie specified by "name".
    //    name - String object containing the cookie name.
    //    returns - String object containing the cookie value, or null if
    //      the cookie does not exist.
    //
    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 getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
      }
      return null;
    }

    //
    //  Function to create or update a cookie.
    //    name - String object object containing the cookie name.
    //    value - String object containing the cookie value.  May contain
    //      any valid string characters.
    //    [expires] - Date object containing the expiration data of the cookie.  If
    //      omitted or null, expires the cookie at the end of the current session.
    //    [path] - String object indicating the path for which the cookie is valid.
    //      If omitted or null, uses the path of the calling document.
    //    [domain] - String object indicating the domain for which the cookie is
    //      valid.  If omitted or null, uses the domain of the calling document.
    //    [secure] - Boolean (true/false) value indicating whether cookie transmission
    //      requires a secure channel (HTTPS).  
    //
    //  The first two parameters are required.  The others, if supplied, must
    //  be passed in the order listed above.  To omit an unused optional field,
    //  use null as a place holder.  For example, to call SetCookie using name,
    //  value and path, you would code:
    //
    //      SetCookie ("myCookieName", "myCookieValue", null, "/");
    //
    //  Note that trailing omitted parameters do not require a placeholder.
    //
    //  To set a secure cookie for path "/myPath", that expires after the
    //  current session, you might code:
    //
    //      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
    //
    function SetCookie (name, value) {
      var argv = SetCookie.arguments;
      var argc = SetCookie.arguments.length;
      var expires = (argc > 2) ? argv[2] : null;
      var path = (argc > 3) ? argv[3] : null;
      var domain = (argc > 4) ? argv[4] : null;
      var secure = (argc > 5) ? argv[5] : false;
      document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
    }

    //  Function to delete a cookie. (Sets expiration date to current date/time)
    //    name - String object containing the cookie name
    //
    function DeleteCookie (name) {
      var exp = new Date();
      exp.setTime (exp.getTime() - 1);  // This cookie is history
      var cval = GetCookie (name);
      document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    }

//---------------------------------------------------------------------------------------------

function jumpURL() {
if (this.selectedIndex>0&&this.options[this.selectedIndex].value!="") location.href=this.options[this.selectedIndex].value;
}


//Call the SetClickTracksCookies() function

SetClickTracksCookies();
// AUTO SUGGEST -auto complete library
var element = null;
var popup = null;
var eltname;
var as_x, as_y;
var as_req = false;

function autosuggest_init() {
  var x = findPosX(document.getElementById('keywords'));
  var y = findPosY(document.getElementById('keywords'));
  as_x = x; as_y = y;

  // set autocomplete attr
  if (document.getElementById('keywords').autocomplete) 
   document.getElementById('keywords').autocomplete = "OFF";
  if (document.getElementById('keywords').setAttribute) 
   document.getElementById('keywords').setAttribute("autocomplete", "OFF");

  document.getElementById('keywords').onkeyup = autosuggest_keyup;
  document.getElementById('keywords').onblur = autosuggest_blur;
}

function autosuggest_keyup() {
  var elt = document.getElementById('keywords');
	var value = elt.value;
	if (value == undefined && document.all) {
		value = document.all.keywords.value;
  }
  if (value == undefined)
    value = document.forms['fsearch'].keywords.value;

  var url = "lookup.html?pfx=" + escape(value);
  as_retrieveXML(url);
}

function autosuggest_blur() {
   if (!ignore_blur) hidePopup();
}


function findElementPosX(obj) {
  curleft = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  }//if offsetParent exists
  else if (obj.x)
    curleft += obj.x
  return curleft;
}//findElementPosX

function setSelection(txt) {
  if (document.all) {
    if (txt.indexOf('more results') == -1)
		  document.forms['fsearch'].keywords.value = txt;
    hidePopup();
    document.forms['fsearch'].submit();
/*
    for (var j = 0; j < document.forms.length; j++) {
      if (document.forms[j].keywords)
        document.forms[j].submit();
    }
*/
   } else {
    var element = document.getElementById('keywords');
    if (txt.indexOf('more results') == -1)
      element.value = txt;
    hidePopup();
    element.onblur = autosuggest_blur;
    element.form.submit();
  }
}

function hidePopup() {
  if (popup != null) {
//  popup.style.visibility = "hidden";
    popup.style.display = "none";
//  popup = null;
  }
}

function element() {
  return document.getElementById('keywords');
}

var ignore_blur = 0;

function showPopup(list) {
  ignore_blur = 0;
  hidePopup();
  if (list.length <= 0) return;
  if (popup == null) {
    popup = document.getElementById('popup');
    popup.className = "autosuggest";
//  popup.style.position = "absolute";
    popup.style.zIndex = 999;

//  popup.style.left = as_x;
    popup.style.top = 2 + document.getElementById('keywords').offsetHeight;
    popup.style.width = document.getElementById('keywords').offsetWidth;
    popup.style.border = "solid white 1px;";

    popup.onmouseover = function() {
      ignore_blur = 1;
    }
    popup.onmouseout = function() {
      ignore_blur = 0;
    }
  }
  popup.style.display = "block";
  popup.innerHTML = "";
  for (var j = 0; j < list.length; j++) {
    var item = list[j][0];
//  var a = document.createElement('a');
//  a.href = "javascript:setSelection('" + list[j][0] + "');";

//  list[j][0] = list[j][0].replace(/ /g, "");
//  var tn = document.createTextNode(list[j][0]);
//  a.appendChild(tn);

    popup.innerHTML += "<a href=\"" + 
                       "javascript:setSelection('" + list[j][0] + "');" +
                       "\">" + list[j][0] + "</a>";

//  popup.appendChild(a);
  }
}

function dump(arr) {
  var str = "Array(" + arr.length + "):\n";
  for (var j = 0; j < arr.length; j++) {
    str += "  #" + j + " = " + arr[j] + "\n";
  }
}

var first = 1;

function as_processXML(data) {
 var elements = data.getElementsByTagName('i');

 var list = Array();
 for (var j = 0; j < elements.length; j++) {
   var str = elements[j].getElementsByTagName('p')[0].firstChild.nodeValue;
   list.push(new Array(str));
 }
 showPopup(list); 
}

function as_retrieveXML(url) {
  if (window.XMLHttpRequest) { // Non-IE browsers
    as_req = new XMLHttpRequest();
    as_req.onreadystatechange = as_processStateChange;
    try {
      as_req.open("GET", url, true);
    } catch (e) {
//    alert(e);
      return;
    }
    as_req.send(null);
  } else if (window.ActiveXObject) { // IE
    try {
      as_req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
      try {
        as_req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        as_req = false;
      }
    }
    if (as_req) {
      as_req.onreadystatechange = as_processStateChange;
      as_req.open("GET", url, true);
      as_req.send();
    }
  }
}

function as_processStateChange() {
  if (as_req.readyState == 4) { // Complete
    if (as_req.status == 200) { // OK response
      as_processXML(as_req.responseXML);
    } else {
//    alert("Problem: " + req.status + "/" + req.statusText);
    }
  }
}

function findPosX(obj)
{
  var curleft = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curleft += obj.offsetLeft
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
    curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}

function getPosition(elementId)
{

var element = document.getElementById(elementId);
var left = 0;
var top = 0;
				
if (element != null)
{
    // Try because sometimes errors on offsetParent after DOM changes.
    try
    {
        while (element.offsetParent)
        {
            // While we haven't got the top element in the DOM hierarchy
            // Add the offsetLeft
            left += element.offsetLeft;
            // If my parent scrolls, then subtract the left scroll position
            if (element.offsetParent.scrollLeft) {left -= element.offsetParent.scrollLeft; }
				
            // Add the offsetTop
            top += element.offsetTop;
            // If my parent scrolls, then subtract the top scroll position
            if (element.offsetParent.scrollTop) { top -= element.offsetParent.scrollTop; }
				
            // Grab
            element = element.offsetParent;
        }
    }
    catch (e)
    {
        // Do nothing
    }
				
    // Add the top element left offset and the windows left scroll and subtract the body's client left position.
    left += element.offsetLeft + document.body.scrollLeft - document.body.clientLeft;
				
    // Add the top element topoffset and the windows topscroll and subtract the body's client top position.
    top += element.offsetTop + document.body.scrollTop - document.body.clientTop;
}
return {x:left, y:top};

}
// END AUTO SUGGEST -auto complete library