// *******************************
// ***   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 LINKS   ***
// *******************************



function ShowAltLink34(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 = linkID.replace(' ', '_');
         // 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 + '" tabindex=-1 title="' + sLinks[x] + '." href="javascript:DoSomething(\'' + letter + '\');" >' + linkText + '</a></li>');
   }
   document.write('</ul>');
   return;
}



// *******************************
// ***   CLOSE WINDOW   ***
// *******************************



function CloseWindowNow34(bRefresh) {
   // REFRESH CALLER...
   if (window.opener) {
		if (window.opener.RefreshMe34) {
			window.opener.RefreshMe34(bRefresh);
		}
	}
   window.close();
   return;
}
function CloseWindowSoon34(iSeconds, bRefresh) {
   iSeconds = iSeconds * 1000
   setTimeout('CloseWindowNow34(' + bRefresh + ')', iSeconds);
   return;
}
function CloseAndResubmitCallerForm() {
   if (window.opener) {
		if (window.opener.document.frmMain) {
			window.opener.document.frmMain.submit();
		}
	}
   window.close();
   return;
}



// *******************************
// ***   COOKIES   ***
// *******************************



// COOKIE FUNCTIONS...
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=.BrickFair.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 ClickOnlyHref34(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 FocusFirstHref34() {
   // REQUIRES THE DOCUMENT TO HAVE HREF LINKS NUMBERED
   // LIKE href1, href2, href3, etc.

   var h1 = document.getElementById('href1');
   if (h1) {
      h1.focus();
   }
   return;
}
function FocusObj34(inID) {
   var h1 = document.getElementById(inID);
   if (h1) {
      h1.focus();
      if (h1.select) {
         h1.select();
         return true;
      }
   }
   return false;
}



// *******************************
// ***   INNER HTML   ***
// *******************************



function SetInnerHtml34(inObj, inStr) {
   if ('*' + inObj.id + '*' == '*undefined*') {
      inObj = document.getElementById(inObj);
   }
   if (!inObj) {
      return false;
   }
   inObj.innerHTML = inStr;
   return true;
}
function AddInnerHtml34(inObj, inStr) {
	SetInnerHtml34(inObj, GetInnerHtml34(inObj) + inStr);
	return;
}
function GetInnerHtml34(inObj) {
   if ((inObj != '[object]') && (inObj != '[object HTMLDivElement]')) {
      inObj = document.getElementById(inObj);
   }
   if (!inObj) {
      return '[Error]';
   }
   return inObj.innerHTML;
}

function ReadHtmlTagStr34(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 PopUpWindow34(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 + ',left=10;top=10';
   }
   if (inWindowName == null) {
      inWindowName = 'Help_Popup';
   }
   self.open(inFile, inWindowName, settings);
   return;
}

function PopDownWindow34(inPage) {
   var callerDoc = window.opener;
   if (callerDoc) {
      callerDoc.location = inPage;
	}
   CloseWindowNow34(false);
   return;
}



// *******************************
// ***   SHOW HIDE   ***
// *******************************



// 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 ShowHide34(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 ShowHideB(object, YesNo) {
   var IsVis = YesNo ? '' : '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 IsVisible34(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   ***
// *******************************



function SizeWindowToBody34() {
   var WinUp;
   var WinAcross;
   var DocUp;
   var DocAcross;
   var ScrollAcross;
   var ScrollUp;
   var TotLoopCount;
   var MAXLOOPCOUNT = 70;
   var INCREASEBY = 18;

   // FOCUS THIS WINDOW...
   window.focus();

   // MAKE SMALLER...
   window.resizeTo(450, 300);

   // MAKE WIDER UNTIL DONE...
   TotLoopCount = 0;
   WinAcross = window.screen.width;
   ScrollAcross = window.document.body.scrollWidth;
   do {
      TotLoopCount = TotLoopCount + 1;
      window.resizeBy(INCREASEBY, 0);
      DocAcross = window.document.body.clientWidth;
   }
   while ((DocAcross < WinAcross) && (DocAcross < ScrollAcross) && (TotLoopCount < MAXLOOPCOUNT))

   // MAKE TALLER UNTIL DONE...
   TotLoopCount = 0;
   WinUp = window.screen.height - 120;
   ScrollUp = window.document.body.scrollHeight;
   do {
      TotLoopCount = TotLoopCount + 1;
      window.resizeBy(0, INCREASEBY);
      DocUp = window.document.body.clientHeight;
   }
   while ((DocUp < WinUp) && (DocUp < ScrollUp) && (TotLoopCount < MAXLOOPCOUNT))

   // ONE LAST TIME FOR SAFETY...   
   window.resizeBy(INCREASEBY, INCREASEBY);

   return;
}



// *******************************
// ***   SWAP IMAGE   ***
// *******************************



function SwapImage34(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;
}


// *******************************
// ***   REFRESH ME   ***
// *******************************

function RefreshMe34(bRefresh) {
	if (bRefresh == true) {
		document.location = document.location;
	}
	return;
}


// *******************************
// ***   FAUX POP-UP   ***
// *******************************


function ShowBox(evt, boxName, objFocus) {
	// VARS...
	var x, y;
	var box = document.getElementById(boxName);
	var IE = false;

	// BROWSER SNIFFER...
	if (navigator.appName == "Microsoft Internet Explorer") {
		IE = true;
	}
	
	if (evt == null) {
		x = 100;
		y = 100;
	} else if (IE) {
		x = evt.clientX + document.body.scrollLeft;
		y = evt.clientY + document.body.scrollTop;
	} else {
		x = evt.pageX;
		y = evt.pageY;
	}
	
	// ADJUST...
	x = x - 15;
	y = y - 45;
	if (x < 0) x = 15;
	if (y < 0) y = 15;

	// SET LOCATION...
	box.style.left = x + 'px';
	box.style.top = y + 'px';

	// SHOW IT...
	ShowHide34(boxName, true);
	FocusObj34(objFocus);
	return;
}




function TryAutoLogon() {
	var id = getCookie('AfolID', 0);
	window.status = 'AfolID: ' + id;
	if (id != 0) {
		PopUpWindow34('/pop/AutoLogon34.asp?AfolID=' + id);
	}
	return;
}




// **********************
// ***   OTHER CRAP   ***
// **********************



function ListObjAttributes(inObj) {
	// VARS...
	var s = '';
	var c = 0;
	var max = 10;
	var att;

	for(att in inObj) {
		if (inObj[att] != null) {
			s = s + att + ' = ' + inObj[att] + '\n';
		}
		c = c + 1;
		if (c == max) {
			if (!confirm(s)) { return; }
			s = '';
			c = 0;
		}
	}
	return;
}
