var DelayedSearchArray = new Object();

function printCurrentDate()
{
  var mydate=new Date();
  var year=mydate.getYear();
  if (year < 1000)
    year+=1900;

  var day=mydate.getDay();
  var month=mydate.getMonth();
  var daym=mydate.getDate();

  if ((daym > 20) || (daym < 10)) {
    if ((daym % 10) == 1) {
      daym = "" + daym + "st";
    } else if ((daym % 10) == 2) {
      daym = "" + daym + "nd";
    } else if ((daym % 10) == 3) {
      daym = "" + daym + "rd";
    } else {
      daym = "" + daym + "th";
    }
  } else {
    daym = "" + daym + "th";
  }

  var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
  var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  document.write("<b>"+dayarray[day]+", "+montharray[month]+" "+daym+", "+year+"</b>");
}

function closePopup(delay) {
  if (delay == undefined) {
    delay=0;
  } else {
    // Delay is in milliseconds.
    delay *= 1000;
  }

  if (delay) {
    setTimeout('window.close();',delay);
  } else {
    window.close();
  }
}

function resizePopup(width,height,center) {
  if (width == undefined) {
    width=screen.availWidth;
  } else if ((width > 0) && (width < 1)) {
    width *= screen.availWidth;
  }

  if (height == undefined) {
    height=screen.availHeight;
  } else if ((height > 0) && (height < 1)) {
    height *= screen.availHeight;
  }

  window.resizeTo(width,height);

  if ((center == undefined) ||
       center)
  {
    var bottom = screen.availHeight;
    var right = screen.availWidth;
    var pos_x = parseInt((right - width)/2) ;
    var pos_y = parseInt((bottom - height)/2);

    window.moveTo(pos_x,pos_y);
  }
}

function openPopup(link,windowname,width,height,scrollbars) {
  if (!window.focus) { return false; }

  if (width == undefined) {
    width=screen.availWidth;
  } else if ((width > 0) && (width < 1)) {
    width *= screen.availWidth;
  }

  if (height == undefined) {
    height=screen.availHeight;
  } else if ((height > 0) && (height < 1)) {
    height *= screen.availHeight;
  }

  if (scrollbars == undefined) {
    scrollbars='yes';
  }

  if (typeof(scrollbars) == 'boolean') {
    if (scrollbars) {
      scrollbars='yes';
    } else {
      scrollbars='no';
    }
  }

  var bottom = screen.availHeight;
  var right = screen.availWidth;
  var pos_x = parseInt((right - width)/2) ;
  var pos_y = parseInt((bottom - height)/2);

  var href;
  if (typeof(link) == 'string') {
    href = link;
  } else {
    href = link.href;
  }

  var options='width='+width+',height='+height+',scrollbars='+
    scrollbars+',left='+pos_x+',top='+pos_y;

  var new_window = window.open(href,windowname,options);
  new_window.focus();
  new_window.leftmargin = 0;

  return false;
}

/**
 * The following was coded to take care of little idiosyncracies
 * allowed and disallowed in cross window communication.
 * Specifically, Mozilla / Netscape allow a child window to
 * refresh their parent, regardless of whether or not the
 * parent is secure or insecure, with the child being secure.
 * In IE and Opera, this is considered a security exception.
 *
 * The correct behavior is? But who cares.  The method I came up
 * with to get around this is to create a listener that will
 * get called routinely (every .5 seconds), to see if a child
 * window has closed.  If it has, the parent will update itself.
 *
 * This specific functionality is obtained implictly by calling
 * openPopupRefreshUponClose(...).
 */
var childInterval;
var children = new Array();

function refreshUponClose() {
  for(var i=0; i < children.length; i++) {
    var child = children.shift();

    var closed = true;

    try {
      closed = child.closed;
    } catch(e) {
       /* Ignore, as this is what happens in Opera when the
          window has been closed, but was marked secure.
        */
    }

    if (closed) {
      var searchStr = getSearchStr();
      if (searchStr != '') {
	window.location.search = searchStr;
      } else {
	window.location.reload(true);
      }
    } else {
      children.push(child);
    }
  }
}

function openPopupRefreshUponClose(link,windowname,width,height,scrollbars) {
  if (!window.focus) { return false; }

  if (width == undefined) {
    width=screen.availWidth;
  } else if ((width > 0) && (width < 1)) {
    width *= screen.availWidth;
  }

  if (height == undefined) {
    height=screen.availHeight;
  } else if ((height > 0) && (height < 1)) {
    height *= screen.availHeight;
  }

  if (scrollbars == undefined) {
    scrollbars='yes';
  }

  if (typeof(scrollbars) == 'boolean') {
    if (scrollbars) {
      scrollbars='yes';
    } else {
      scrollbars='no';
    }
  }

  var bottom = screen.availHeight;
  var right = screen.availWidth;
  var pos_x = parseInt((right - 5 - width)/2) ;
  var pos_y = parseInt((bottom + 60 - height)/2);

  var href;
  if (typeof(link) == 'string') {
    href = link;
  } else {
    href = link.href;
  }

  var options='width='+width+',height='+height+',scrollbars='+
    scrollbars+',left='+pos_x+',top='+pos_y;

  var new_window = window.open(href,windowname,options);

  children.push(new_window);
  new_window.focus();

  if (childInterval == undefined) {
    childInterval = window.setInterval('refreshUponClose()',500);
  }

  return false;
}

function openParent(link) {
  if (!window.focus || !window.opener) {
    return true;
  }

  var href;
  if (typeof(link) == 'string') {
    href = link;
  } else {
    href = link.href;
  }

  window.opener.focus();
  window.opener.location.href = href;

  return false;
}

function refreshParent() {
  if (!window.focus || !window.opener) {
    return true;
  }

  window.opener.focus();

  var searchStr = getSearchStr();
  if (searchStr != '') {
    window.location.search = searchStr;
  } else {
    window.location.reload(true);
  }

  return false;
}

function redirectSelf(includeSearch) {
  newHREF = window.location.protocol + '//' + window.location.host + window.location.pathname;

  if ((includeSearch != undefined) && includeSearch) {
    newHREF += window.location.search;
  }

  window.location.href = newHREF;
}

function redirectLocal(uri) {

  new_url = 'http://' + window.location.host + window.location.pathname + uri;

  window.location.href = new_url;
  return false;
}

function setCookie(name,value,duration,path,domain) {
  value=name + '=' + escape(value) + ';';

  if (duration != undefined) {
    value += ' expires=';
    if (typeof(duration) != 'object') {
      increment = duration;
      duration = new Date();
      duration.setTime(duration.getTime() + increment * 1000);
    }

    value += duration.toGMTString() + ';';
  }

  if (path == undefined) {
    path = '/';
  }

  value += ' path=' + path + ';';

  if (domain == undefined) {
    domainName = document.domain;
    offset = domainName.lastIndexOf('.');
    if (offset != -1) {
      offset = domainName.lastIndexOf('.',offset-1);
    }
    if (offset == -1) {
      offset = 0;
    }
    domain = domainName.substr(offset);
  }

  value += ' domain=' + domain + ';';

  document.cookie=value;
}

function delCookie(name,path,domain) {
  setCookie(name,'',-1000,path,domain);
}

function selectOption(selectObj,value,multiple) {
  var options = selectObj.options;
  var matched = 0;
  var values = [];

  if (value instanceof Array) {
    values = value;
  } else {
    values = [ value ];
  }

  for(var i=0; i < values.length; i++) {
    if (values[i] instanceof Object) {
      values[i] = values[i].value;
    }
  }

  for(var i=0; i < options.length; i++) {
    options[i].selected = false;

    for(var j=0; j < values.length; j++) {
      if (values[j] == options[i].text) {
	 options[i].selected = true;
	 matched++;
      }
    }
  }
}

function selectOptionValue(selectObj,value,multiple) {
  var options = selectObj.options;

  if (!multiple) {
    value = [ value ];
  }

  for(var i=0; i < options.length; i++) {
    options[i].selected = false;

    for(var j=0; j < value.length; j++) {
      if (value[j] == options[i].value) {
	 options[i].selected = true;
      }
    }
  }
}

function getSearchStr() {
  var result = '';

  var searchArr = getSearchArray()
  var delayedArr = getDelayedSearchArray()

  for(var i in searchArr) {
    if (result == '') {
       result += '?';
    } else {
       result += '&';
    }

    result += '' + i + '=' + searchArr[i];
  }

  for(var i in delayedArr) {
    if (result == '') {
       result += '?';
    } else {
       result += '&';
    }

    result += '' + i + '=' + delayedArr[i];
  }

  return result;
}

function getSearchArray() {
  var searchKey = document.location.search;
  searchKey = searchKey.substring(1,searchKey.length);

  var searchKeyValues = searchKey.split('&');
  var result = new Object();

  if (searchKey != '') {
    for(var i=0; i < searchKeyValues.length; i++) {
      var eq = searchKeyValues[i].indexOf('=');
      var key=null, value=null;

      if (eq >= 0) {
	key = searchKeyValues[i].substring(key,eq);
	value = searchKeyValues[i].substring(eq+1,searchKeyValues[i].length);
      } else {
	key = searchKeyValues[i];
      }

      result[key] = value;
    }
  }

  return result;
}

function getDelayedSearchArray() {
  return DelayedSearchArray;
}

//-----------------------------------------------------------------------------
// PNG Support on IE
//-----------------------------------------------------------------------------
// Support PNG Alpha aliasing on IE.

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
    document.writeln('<style type="text/css">img, input.image { visibility:hidden; } </style>');
    window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

    for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
        if (itsAllGood && img.src.match(/\.png$/i) != null) {
            fnFixPng(img);
            img.attachEvent("onpropertychange", fnPropertyChanged);
        }
        img.style.visibility = "visible";
    }

    var nl = document.getElementsByTagName("INPUT");
    for (var i = nl.length - 1, e = null; (e = nl[i]); i--) {
        if (e.className && e.className.match(/\bimage\b/i) != null) {
            if (e.src.match(/\.png$/i) != null) {
                fnFixPng(e);
                e.attachEvent("onpropertychange", fnPropertyChanged);
            }
            e.style.visibility = "visible";
        }
    }
}

function fnPropertyChanged() {
    if (window.event.propertyName == "src") {
        var el = window.event.srcElement;
        if (!el.src.match(/x\.gif$/i)) {
            el.filters.item(0).src = el.src;
            el.src = "/images/x.gif";
        }
    }
}

function dbg(o) {
    var s = "";
    var i = 0;
    for (var p in o) {
        s += p + ": " + o[p] + "\n";
        if (++i % 10 == 0) {
            alert(s);
            s = "";
        }
    }
    alert(s);
}

function fnFixPng(img) {
    var src = img.src;
    img.style.width = img.width + "px";
    img.style.height = img.height + "px";
    img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
    img.src = "/images/x.gif";
}

//-----------------------------------------------------------------------------
// PNG Support on IE
//-----------------------------------------------------------------------------
