
// Returns an object correctly based on browser type
function getObject(name)
{
  var ns4 = (document.layers) ? true : false;
  var w3c = (document.getElementById) ? true : false;
  var ie4 = (document.all) ? true : false;

  if (ns4) return eval('document.' + name);
  if (w3c) return document.getElementById(name);
  if (ie4) return eval('document.all.' + name);
  return false;
}

// Sets the page height in the default CP look 'n feel
function setPageHeight()
{
  var myHeight  = 0;
  var container = document.getElementById('container');
  var boxes     = document.getElementById('sideBar');
  var content   = document.getElementById('content');
  var navHeight = 120;

  if (typeof(window.innerHeight) == 'number') {
    // Non-IE
    myHeight = window.innerHeight;
  }
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    // IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  }
  else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    // IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  if (myHeight - navHeight > 757) {
    navHeight = myHeight + navHeight - 757;
  }
  container.style.height = (myHeight - navHeight) + 'px';
  boxes.style.height     = ((myHeight - navHeight) + 4) + 'px';
  content.style.height   = (myHeight - navHeight) + 'px';
}

// XMLHttpRequest Stuff
var req;

var fField;

function loadXMLDoc(url, fillField)
{
  fField = fillField;
  // Internet Explorer
  try { req = new ActiveXObject('Msxml2.XMLHTTP'); }
  catch(e) {
    try { req = new ActiveXObject('Microsoft.XMLHTTP'); }
    catch(oc) { req = null; }
  }

  // Mozailla/Safari
  if (!req && typeof XMLHttpRequest != 'undefined') { req = new XMLHttpRequest(); }

  // Call the processChange() function when the page has loaded
  if (req != null) {
    req.onreadystatechange = processChange(fillField);
    req.open('GET', url, true);
    req.send(null);
  }
}

function processChange() {
  // The page has loaded and the HTTP status code is 200 OK
  if (req.readyState == 4 && req.status == 200) {
    // Write the contents of this URL to the searchResult layer
    getObject(fField).innerHTML = req.responseText;
  }
}

// End XMLHttpRequest Stuff

function checkMonth(monthField, yearField)
{
  var ourMonthField = getObject(monthField);
  var ourYearField  = getObject(yearField);
  var today         = new Date();
  if ((ourMonthField.options[ourMonthField.selectedIndex].value-1) <= today.getMonth()) {
    if (ourYearField.options[ourYearField.selectedIndex].value <= today.getFullYear()) {
      ourYearField.selectedIndex = ourYearField.selectedIndex+1;
    }
  }
}

function showHide(what)
{
  var theThing = getObject(what);
  var cookieResult = getCookie(what);
  if (cookieResult) {
    var display = cookieResult;
  }
  else {
    var display = theThing.style.display;
  }
  if (display == 'none') {
    theThing.style.display = 'block';
    setCookie(what, 'block', 7);
  }
  else {
    theThing.style.display = 'none';
    setCookie(what, 'none', 7);
  }
}

function restoreShowHide(what)
{
  var cookieResult = getCookie(what);
  if (cookieResult) {
    var theThing = getObject(what);
    if (theThing) {
      theThing.style.display = cookieResult;
    }
  }
}

function setCookie(name, value, days)
{
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = '; expires=' + date.toGMTString();
  }
  else {
    var expires = '';
  }
  document.cookie = name + '=' + value + expires + '; path=/';
}

function getCookie(name)
{
  var cookieName = name + '=';
  var ca = document.cookie.split(';');
  for (var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1, c.length);
      if (c.indexOf(cookieName) == 0) {
			  return c.substring(cookieName.length, c.length);
      }
    }
  }
  return false;
}

function rmCookie(name)
{
  setCookie(name, '', -1);
}

function editorWindow(url)
{
  window.open(url, 'EditorWindow', 'height=600, width=800,alwaysRaised=yes,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,titlebar=yes,toolbar=no');
  return false;
}

function openWindow(url, height, width)
{
  var date = new Date();
  window.open(url, date.getTime(), 'height=' + height + ',width=' + width + ',alwaysRaised=yes,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,titlebar=yes,toolbar=no');
  return false;
}

function showBusyDiv()
{
	var arrayPageSize   = getPageSize();
  var arrayPageScroll = getPageScroll();
  $('busyDiv').style.top  = arrayPageScroll[1] + ((arrayPageSize[3] - 600) / 2) + 'px';
	$('busyDiv').style.display = 'block';
}

function hideBusyDiv()
{
	$('busyDiv').style.display = 'none';
}

function toggleHelp(baseId)
{
  var helpBox  = baseId + 'Box';

  if (getObject(baseId + 'Box').style.display == '') {
    new Effect.SlideUp(baseId + 'Box');
  }
  else {
    new Effect.SlideDown(baseId + 'Box');
  }
}

function getPageScroll()
{
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


