
// ********
// *** AJAX
// ********

var AjaxResponseHandler;
var AjaxReqObj;

function xmlPost(url, toSend, responseHandler) {
   AjaxResponseHandler = responseHandler;
   xmlOpen("POST", url, toSend);
}
function xmlGet(url, responseHandler) {
   AjaxResponseHandler = responseHandler;
   xmlOpen("GET", url, null);
}
function xmlOpen(method, url, toSend) {
    if (window.XMLHttpRequest) {
        // browser has native support for XMLHttpRequest object
        AjaxReqObj = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // try XMLHTTP ActiveX (Internet Explorer) version
        AjaxReqObj = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if(AjaxReqObj) {
        AjaxReqObj.onreadystatechange = KeepTrying;
        AjaxReqObj.open(method, url, true);
        AjaxReqObj.setRequestHeader("content-type","application/x-www-form-urlencoded");
        AjaxReqObj.send(toSend);
    } else {
        alert('Your browser does not seem to support XMLHttpRequest.');
    }
}
function KeepTrying() {
   //alert('readyState = ' + AjaxReqObj.readyState + '\nstatus = ' + AjaxReqObj.status);
   if ((AjaxReqObj.readyState == 4) && (AjaxReqObj.status == 200)) {
      if (AjaxResponseHandler) {
         AjaxResponseHandler();
      }
   }
   return;
}
function getNode(parent, tagName) {
    var i;
    var max = parent.childNodes.length;

    // Check each child node
    for(i = 0; i < max; i++) {
        if(parent.childNodes[i].tagName) {
            if(parent.childNodes[i].tagName.toUpperCase() == tagName.toUpperCase()) {
                // We found a matching child node; return it.
                return parent.childNodes[i];
            }
        }
    }
    // One was not found; return null
    return null;
}
function getNodesWithKey(parent, tagName, key) {
    var i;
    var cellNodes = parent.getElementsByTagName(tagName);
    var max = cellNodes.length;

    // Check each cell node for the specified value for
    // the 'key' attribute
    for(i = 0; i < max; i++) {
        if(cellNodes[i].getAttribute('key') == key) {
            // We found a matching cell node; return it.
            return cellNodes[i];
        }
    }
    // One was not found; return null
    return null;
}

// ************
// *** ALT KEYS
// ************

function ShowAltLink45(sText) {
   var sLinks = sText.split(',');
   var pos;
   var letter;
   var linkText;
   var linkID;
   
   document.write('<ul class="AltKeys">');

   // FOR EACH LINK...
   for (var x = 0; x < sLinks.length; x++) {

      // DEFAULT...
      letter = '';
      linkText = sLinks[x];

      // FIND DOT...
      pos = sLinks[x].indexOf('.');

      if (pos > -1) {

         // LINK LETTER...
         letter = sLinks[x].charAt(pos + 1);

         // CLEAN LINK...
         linkText = sLinks[x].substring(0, pos) + '<u>' + letter + '</u>'
                  + sLinks[x].substring(pos + 2, sLinks[x].length);
         linkID = 'href_' + sLinks[x].substring(0, pos) + letter
                + sLinks[x].substring(pos + 2, sLinks[x].length);

         // linkID = href_Save, for example
         sLinks[x] = sLinks[x].substring(0, pos)
                   + sLinks[x].substring(pos + 1, sLinks[x].length);
      }

      // SHOW AND TELL...
      letter = letter.toUpperCase();
      document.write('<li><a id=' + linkID + ' accesskey="' + letter
                   + '" xtabindex=-1 title="' + sLinks[x]
                   + '." href="javascript:DoSomething(\'' + letter + '\');" >'
                   + linkText + '</a></li>');
   }
   document.write('</ul>');
   return;
}

// ****************
// *** CLOSE WINDOW
// ****************

function CloseWindowNow45(bRefresh) {
   // REFRESH CALLER...
   if (window.opener) {
		if (window.opener.RefreshMe45) {
			window.opener.RefreshMe45(bRefresh);
		}
	}
   window.close();
   return;
}
function CloseWindowSoon45(iSeconds, bRefresh) {
   iSeconds = iSeconds * 1000
   setTimeout('CloseWindowNow45(' + bRefresh + ')', iSeconds);
   return;
}

// ***********
// *** COOKIES
// ***********

function setCookie(NameOfCookie, value, expiredays) {
   var ExpireDate = new Date();
   var NewCookie;
   if (!(expiredays >= -1)) { expiredays = 120; }
   ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
   NewCookie = NameOfCookie + '=' + escape(value);
   NewCookie = NewCookie + '; expires=' + ExpireDate.toGMTString();
   NewCookie = NewCookie + '; domain=.DownFox.com';
   NewCookie = NewCookie + '; path=/';
   document.cookie = NewCookie;
   return;
}
function getCookie(NameOfCookie, inDefault) {
   var begin;
   var end;
   
   if (document.cookie.length > 0) {
      begin = document.cookie.indexOf(NameOfCookie+"=");
      if (begin != -1) {
         begin += NameOfCookie.length+1;
         end = document.cookie.indexOf(";", begin);
         if (end == -1) {
            end = document.cookie.length;
         }
         return unescape(document.cookie.substring(begin, end));
      }
   }
   return inDefault;
}
function deleteCookie(NameOfCookie) {
   setCookie(NameOfCookie, 0, -1);
   return;
}
function testCookie() {
   setCookie('Testing', 2, 1);
   var answer = getCookie('Testing', -1);
   deleteCookie('Testing');
   return (answer == 2);
}

// *************
// *** FOCUS OBJ
// *************

function ClickOnlyHref45(Clear1, Clear2) {
   // REQUIRES THE DOCUMENT TO HAVE HREF LINKS NUMBERED
   // LIKE href1, href2, href3, etc.

   var h1 = document.getElementById('href1');
   var h2 = document.getElementById('href2');

   // IF HREF1 EXISTS BUT HREF2 DOES NOT, THEN CLICK HREF1...
   if ((h1) && !(h2)) {
      // Check for .Click() function because FireFox doesn't have that(?)
      if (eval(h1.click)) { h1.click(); }
      return;
   }

   // OTHERWISE, CLEAR OUT THE OTHER PAGES...
   // Do this 1st, so that Clear2 is always the Frame3 window, if needed...
   if (!Clear2) {
      Clear2 = Clear1;
      Clear1 = null;
   }

   var wind;
   var loc;
   if (Clear1) {
      wind = eval('parent.' + Clear1);
      loc = new String(wind.location);
      if (loc.indexOf('BlankWithLogo.htm') < 0) {
         wind.location = '../BlankWithLogo.htm';
      }
   }
   if (Clear2) {
      wind = eval('parent.' + Clear2);
      loc = new String(wind.location);
      if (loc.indexOf('Blank.htm') < 0) {
         wind.location = '../Blank.htm';
      }
   }

   return;
}
function FocusFirstHref45() {
   // REQUIRES THE DOCUMENT TO HAVE HREF LINKS NUMBERED
   // LIKE href1, href2, href3, etc.

   var h1 = document.getElementById('href1');
   if (h1) {
      h1.focus();
   }
   return;
}
function FocusObj45(inID) {
   var h1 = document.getElementById(inID);
   if (h1) {
      h1.focus();
      if (h1.select) {
         h1.select();
         return true;
      }
   }
   return false;
}

// **************
// *** INNER HTML
// **************

function SetInnerHtml45(inObj, inStr) {
   if ('*' + inObj.id + '*' == '*undefined*') {
      inObj = document.getElementById(inObj);
   }
   if (!inObj) {
      return false;
   }
   inObj.innerHTML = inStr;
   return true;
}
function AddInnerHtml45(inObj, inStr) {
	SetInnerHtml45(inObj, GetInnerHtml45(inObj) + inStr);
	return;
}
function GetInnerHtml45(inObj) {
   if ((inObj != '[object]') && (inObj != '[object HTMLDivElement]')) {
      inObj = document.getElementById(inObj);
   }
   if (!inObj) {
      return '[Error]';
   }
   return inObj.innerHTML;
}
function ReadHtmlTagStr45(inTag, inText) {
   var Tag1 = '<'  + inTag + '>';
   var Tag2 = '</' + inTag + '>';
   var xx1 = inText.indexOf(Tag1) + Tag1.length;
   var xx2 = inText.indexOf(Tag2);
   if ((xx1 == 0) || (xx2 == 0) || (xx2 < xx1)) {
      return '';
   }
   return inText.substring(xx1, xx2);
}

// ****************
// *** POPUP WINDOW
// ****************

function PopUpWindow45(inFile, bShowStatus, inWindowName, inWidth, inHeight) {
   // Must -- I mean MUST -- use RELATIVE directory in order for this to work!  Cannot use "http://www..."
   if (!inWidth)  { inWidth  = 0; }
   if (!inHeight) { inHeight = 0; }
   if (inWidth  == 0) { inWidth  = 300; }
   if (inHeight == 0) { inHeight = 300; }

   var settings;
   if (bShowStatus) {
      settings = '';
   } else {
      settings = 'scrollbars=yes,toolbar=no,directories=no,menubar=no,resizable=yes,dependent=yes,width=' + inWidth + ',height=' + inHeight;
   }
   if (inWindowName == null) {
      inWindowName = 'Help_Popup';
   }
   self.open(inFile, inWindowName, settings);
   return;
}
function PopDownWindow45(inPage) {
   var callerDoc = window.opener;
   if (!callerDoc) return;

   callerDoc.location = inPage;
   CloseWindowNow45(false);
   return;
}

// **************
// *** REFRESH ME
// **************

function RefreshMe45(bRefresh) {
   if (bRefresh) {
      if (getCookie('bPermitRefresh', 1) == 1) {
         document.location = document.location;
      }
   }
   return;
}

// ************
// *** SHOWHIDE
// ************

// All the damn options...
var isNS4 = (document.layers) ? true : false;
var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;
var isNS = (isNS4 || isNS6);
var isIE = (isIE4 || isIE5);

function ShowHide45(object, YesNo) {
   var IsVis = YesNo ? 'block' : 'none';
   if (isNS4) {
      document.layers[object].display = IsVis;
   } else if (isIE4) {
      document.all[object].style.display = IsVis;
   } else if (isNS6 || isIE5 || isFirefox()) {
      var elm = document.getElementById(object);
      if (elm) {
         elm.style.display = IsVis;
      }
   }
   return;
}
function IsVisible45(object) {
   // Do it...
   if (isNS4) {
      return (document.layers[object].display != 'none');
   } else if (isIE4) {
      return (document.all[object].style.display != 'none');
   } else if (isNS6 || isIE5) {
      var elm = document.getElementById(object);
      return (elm.style.display != 'none');
   }
   return false;
}

// ***********************
// *** SIZE WINDOW TO BODY
// ***********************

function SizeWindowToBody45() {
   // VARS...
   var f, g;

   // SMALLER...
   window.resizeTo(450, 300);
   
   // GET WINDOW SIZE...
   f = document.documentElement.scrollWidth + 80;
   g = document.documentElement.scrollHeight + 80;
   
   // CHECK AGAINST WINDOW SIZE...
   if (f > (window.screen.width - 20)) {
      f = window.screen.width - 20;
   }
   if (g > (window.screen.height - 100)) {
      g = window.screen.height - 100;
   }

   // SIZE IT...
   window.resizeTo(f, g);
   return;
}

// **************
// *** SWAP IMAGE
// **************

function SwapImage45(Button) {
   if (Button.src.lastIndexOf('-Off') > 0) {
      Button.src = Button.src.substring(0, Button.src.lastIndexOf('-O')) + '-On.gif';
   } else {
      Button.src = Button.src.substring(0, Button.src.lastIndexOf('-O')) + '-Off.gif';
   }
   return;
}

// *******************
// *** JAVASCRIPT HELP
// *******************

function ListAttributes45(inObjID) {
	var list = document.getElementById(inObjID).attributes;
	var s = '';
	for(var i = 0; i < list.length; i++) {
		s = s + list[i].nodeName; // + ' = ' + list[i] + '\n'
	}
	alert(s);
	return;
}


// **************
// *** FAUX STUFF
// **************


function FauxDropdownShowC(evt, inURLc) {
	// SHOWS FAUX POP-UP WINDOW.
	// CALLS AJAX PAGE TO RETRIEVE DATA (BASED ON URL), BUT DOES NOT ATTACH TO ANY INPUT TEXT BOX.  SIMPLY SHOWS POP-UP.
	
	// DETEMINE DATA-RECIPIENT...
	var menu = document.getElementById('divFauxDropdown');

	// CLEAR IT...
	SetInnerHtml45('divFauxDropdown', '');
	
	// GET LIST...
	var url = '/ajax/' + inURLc;
	window.status = url;
	xmlGet(url, FauxDropdownResult);
	
	// POSITION...
	var x = GetEventX(evt);
	var y = GetEventY(evt);
	
	// SET LOCATION BASED ON DATA-RECIPIENT...
	var box = document.getElementById('divFauxDropdown');
	box.style.left = x + 'px';
	box.style.top = y + 'px';

	// SHOW IT...
	ShowHide45('divFauxDropdown', true);
	return;
}
function FauxDropdownResult() {
	// FILL TEXT...
	var ttt = ReadHtmlTagStr45('RESULT', AjaxReqObj.responseText);
	if (ttt == '') {
		ShowHide45('divFauxDropdown', false);
		return;
	}
	SetInnerHtml45('divFauxDropdown', ttt);
	return;
}
function GetEventX(evt) {
	// VARS...
	var IE = false;

	// BROWSER SNIFFER...
	if (navigator.appName == "Microsoft Internet Explorer") {
		IE = true;
	}
	
	if (evt == null) {
		return 100;
	} else if (IE) {
		return evt.clientX + document.body.scrollLeft;
	} else {
		return evt.pageX;
	}
}
function GetEventY(evt) {
	// VARS...
	var IE = false;

	// BROWSER SNIFFER...
	if (navigator.appName == "Microsoft Internet Explorer") {
		IE = true;
	}
	
	if (evt == null) {
		return 100;
	} else if (IE) {
		return evt.clientY + document.body.scrollTop;
	} else {
		return evt.pageY;
	}
}
