/**-------- From common.js ----*/
function createPopUp(url, name, optionString) {
	if (!name){
		name = "newwnd";
	}
	if (!optionString){
		optionString = "toolbar=no,location=no,directories=no,scrollbars=yes,resizable=yes,height=500,width=730,screenX=4,screenY=4,top=4,left=4";
	}		
	win = window.open(url, name, optionString);
	win.focus();
}

/**------- From dhtmlUtil.js ---*/
function DisplayPopup(popName, featureList) {
	var self = this;
	this.init(popName, featureList);
}

DisplayPopup.prototype = {
	init: function(popName, featureList) {
		this.popName = (!popName) ? "newWin" : popName;
		this.featureList = (!featureList) ? "" : featureList;
		this.newWin = "";
	},
	
	show: function(url) {
		this.newWin = window.open(url, this.popName, this.featureList);
		this.focus();
	},
	
	close: function() {
		this.newWin.close();
	},
	
	focus: function() {
		this.newWin.focus();
	}
}	
/* --------------------------------- */

/*---------- Helper functions from dojo.dom, dojo.html namespaces which were removed for .9+  ------*/
function isEmpty(obj) {
	if (obj != null && obj != "" && ((typeof obj != "undefined") || obj != "undefined" || obj != undefined)) {
		return false;
	}
	else {
		return true;
	}
}

function getTextContent (node, text) {
 if (arguments.length>1) {
    var _document = dojo.doc();
    dojo.dom.replaceChildren(node, _document.createTextNode(text));
    return text;  //  string
  } else {
    if(node.textContent != undefined){ //FF 1.5
      return node.textContent;  //  string
    }
    var _result = "";
    if (node == null) { return _result; }
    for (var i = 0; i < node.childNodes.length; i++) {
      switch (node.childNodes[i].nodeType) {
        case 1: // ELEMENT_NODE
        case 5: // ENTITY_REFERENCE_NODE
          _result += getTextContent(node.childNodes[i]);
          break;
        case 3: // TEXT_NODE
        case 2: // ATTRIBUTE_NODE
        case 4: // CDATA_SECTION_NODE
          _result += node.childNodes[i].nodeValue;
          break;
        default:
          break;
      }
    }
    return _result;  //  string
  }
}

function removeChildren(node) {
	if(node) {
		while (node.hasChildNodes()) {
			node.removeChild(node.firstChild);
		}
	}
}

function addClass(node, classStr) {
 if (hasClass(node, classStr)) {
    return false;
  }
  classStr = (_getClass(node) + " " + classStr).replace(/^\s+|\s+$/g,"");
  return setClass(node, classStr);  //  boolean
}

function setClass(node, classStr) {
  node = dojo.byId(node);
  var cs = new String(classStr);
  try{
    if(typeof node.className == "string"){
      node.className = cs;
    }else if(node.setAttribute){
      node.setAttribute("class", classStr);
      node.className = cs;
    }else{
      return false;
    }
  }catch(e){
    console.debug("setClass() failed", e);
  }
  return true;
}

function hasClass(node, classname) {
 return (new RegExp('(^|\\s+)'+classname+'(\\s+|$)')).test(_getClass(node))  //  boolean
}

function _getClass(node) {
 node = dojo.byId(node);
  if(!node){ return ""; }
  var cs = "";
  if(node.className){
    cs = node.className;
  }else if(hasAttribute(node, "class")){
    cs = getAttribute(node, "class");
  }
  return cs.replace(/^\s+|\s+$/g, "");  //  string
}

function getAttribute (node, attr) {
  node = dojo.byId(node);
  // FIXME: need to add support for attr-specific accessors
  if((!node)||(!node.getAttribute)){
    // if(attr !== 'nwType'){
    //  alert("getAttr of '" + attr + "' with bad node"); 
    // }
    return null;
  }
  var ta = typeof attr == 'string' ? attr : new String(attr);
  // first try the approach most likely to succeed
  var v = node.getAttribute(ta.toUpperCase());
  if((v)&&(typeof v == 'string')&&(v!="")){ 
    return v;  //  string 
  }
  // try returning the attributes value, if we couldn't get it as a string
  if(v && v.value){ 
    return v.value;  //  string 
  }
  // this should work on Opera 7, but it's a little on the crashy side
  if((node.getAttributeNode)&&(node.getAttributeNode(ta))){
    return (node.getAttributeNode(ta)).value;  //  string
  }else if(node.getAttribute(ta)){
    return node.getAttribute(ta);  //  string
  }else if(node.getAttribute(ta.toLowerCase())){
    return node.getAttribute(ta.toLowerCase());  //  string
  }
  return null;  //  string
}

function hasAttribute(node, attr) {
 return getAttribute(dojo.byId(node), attr) ? true : false;  //  boolean
}

function removeClass(node, classStr, allowPartialMatches) {
 try{
    if (!allowPartialMatches) {
      var newcs = _getClass(node).replace(new RegExp('(^|\\s+)'+classStr+'(\\s+|$)'), "$1$2");
    } else {
      var newcs = _getClass(node).replace(classStr,'');
    }
    setClass(node, newcs);
  }catch(e){
    console.debug("removeClass() failed", e);
  }
  return true;  //  boolean
}

function isNumeric(value) {
 return (!isNaN(value) 
    && isFinite(value) 
    && (value != null) 
    && !isBoolean(value) 
    && !dojo.isArray(value) 
    && !/^\s*$/.test(value)
  );  //  boolean
}

function isBoolean(it) {
 return (it instanceof Boolean || typeof it == "boolean"); // Boolean
}
