/**
 * Check whether the browser permits us to pop up windows.  Does so
 * by creating and closing a window.
 *
 * @return boolean true iff we can pop up windows
 */
function browserPermitsPopups() {
  newWindow = window.open("", "test", "10,10");
  if (newWindow == null) {
    success = false;
  } else {
    success = true;
    newWindow.close();
  }
  return success;
}

// no handling of window existence here
function safeOpenWindow (windowURL, windowName, specs) {
  newWindow = window.open(windowURL, windowName, specs);
  if (newWindow == null)
    alert("ERROR: Your browser is blocking WISE from opening a new window.  Please add wise.berkeley.edu to the list of sites your browser allows to open popup windows.");
  newWindow.opener = this.window;
  newWindow.focus();
  return newWindow;
}

// based off http://rhea.redhat.com/bboard-archive/acs_design/0003WR.html
function focusToFirstFormElement() {
  // If there is a form in the doc...
  if (document.forms.length > 0) {
    var theElems = document.forms[0].elements
      
      // loop over the elements until finding a non-hidden one
      for (var i = 0; i < theElems.length; i++) {
	var theElem = theElems[i];
	if (theElem.type && theElem.type == "hidden" ) {
	  continue;
	}
	// focus on the first non-hidden element
	theElem.focus();
	break;
      }
  }
}

// from http://www.dustindiaz.com/top-ten-javascript/
function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    if ( tag == null )
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

// from http://www.dustindiaz.com/top-ten-javascript/
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

// from http://www.dustindiaz.com/top-ten-javascript/
function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    }
    else if (elm.attachEvent) {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    }
    else {
        elm['on' + evType] = fn;
    }
    return false;
}

// from http://www.dustindiaz.com/top-ten-javascript/
function toggle(obj) {
    var el = document.getElementById(obj);
    if ( el.style.display != 'none' ) {
        el.style.display = 'none';
    }
    else {
        el.style.display = '';
    }
}

// from http://www.dustindiaz.com/top-ten-javascript/
Array.prototype.inArray = function (value) {
    var i;
    for (i=0; i < this.length; i++) {
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};

// from http://www.dustindiaz.com/top-ten-javascript/
function getCookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
        return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}
    
// from http://www.dustindiaz.com/top-ten-javascript/
function setCookie( name, value, expires, path, domain, secure ) {
    var today = new Date();
    today.setTime( today.getTime() );
    if ( expires ) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name+"="+escape( value ) +
        ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
        ( ( path ) ? ";path=" + path : "" ) +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ( ( secure ) ? ";secure" : "" );
}
    
// from http://www.dustindiaz.com/top-ten-javascript/
function deleteCookie( name, path, domain ) {
    if ( getCookie( name ) ) document.cookie = name + "=" +
            ( ( path ) ? ";path=" + path : "") +
            ( ( domain ) ? ";domain=" + domain : "" ) +
            ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// from http://www.dustindiaz.com/top-ten-javascript/
function $() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}
