var libUtils=true;

isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
isOp5 = isNS6 && (navigator.userAgent.indexOf("Opera") != -1);
isKr2 = isIE5 && (navigator.userAgent.indexOf("Konqueror") != -1);
isNS5 = isNS6 && (navigator.userAgent.indexOf("; 0.8)") != -1);
if( isOp5 || isNS5 ) { isNS6 = false };
if( isKr2 ) { isIE5 = false };

isIE=(isIE4 || isIE5);
isMoz=(isNS6 || isNS5);
isNS=(isMoz || isNS4);


function getElement( id ) {
     if( document.getElementById ) {
	  return document.getElementById( id );
     }
     if( isIE4 ) {
	  return document.all[id];
     }
}

function isAlpha( c ) {
     return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

function isNum( c ) {
     return c >= '0' && c <= '9';
}

function isAlNum( c ) {
     return isAlpha(c) || isNum(c);
}

function isPunct( c ) {
     if(c == '.') return true;
     if(c == "'") return true;
     if(c == ',') return true;
     if(c == '!') return true;
     if(c == '?') return true;
     if(c == ':') return true;
     if(c == ';') return true;
     if(c == '-') return true;
     if(c == '_') return true;
     if(c == '+') return true;
     if(c == '@') return true;
     if(c == "(") return true;
     if(c == ")") return true;
     if(c == "/") return true;
     if(c == "%") return true;
     return false;
}


function isOkInAttr( c ) {
     return isAlNum(c) || isPunct(c) || c==' ';
}


function isValidXmlId( s, name, lastOnly ) {
     var start = (lastOnly && s.length) ? s.length-1 : 0;

     if( !lastOnly && s.length==0 ) {
	  _vERROR = '"' +name+ '" must be specified.';
	  return false;
     }
	  
     for( i=start; i<s.length; i++ ) {
	  c = s.charAt(i);
//	  alert(i +': '+ c);
	  if( i==0 && !isAlpha(c) ) {
	       _vERROR = '"' +name+ '" must begin with a letter, not "' +c+ '".';
	       return false;
	  }
	  if( !(isAlNum(c) || c=='_') ) {
	       _vERROR = 'Character "' +c+ '" not permitted in "' +name+ '" (only letters, digits, and "_" allowed).';
	       return false;
	  }
     }
     return true;
}

// These functions are from FAQTS.COM. Sadly they're only good for
// IE. The workaround is the "clipBoard" form field for NS & Opera
function storeCaret (field) {
     if (field.createTextRange) {
	  field.caretPos = document.selection.createRange().duplicate();
     }
}

function insertAtCaret (field, text) {
     if (field.createTextRange && field.caretPos) {
	  var caretPos = field.caretPos;
	  caretPos.text =
	       caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
	       text + ' ' : text;
     } else if( document.forms[0].clipBoard ) {
	  document.forms[0].clipBoard.value = text;
     }
}

function wrapAroundSelection( pre, post ) {
     field = document.forms[0].content;
//     alert(pre + post);
     if (field.createTextRange && field.caretPos) {
	  var cp = field.caretPos;
	  hasSpc  = cp.text.charAt(cp.text.length - 1) == ' ';
	  text    = hasSpc ? cp.text.substring(0,cp.text.length-1) : cp.text;
	  cp.text = pre + text + post + (hasSpc ? ' ' : '');
     } else if( document.forms[0].clipBoard ) {
	  document.forms[0].clipBoard.value = pre + post;
     }
}



function sniff( obj, w )
{
     if( !w ) {
	  w=window.open();
     }
     var log = w.document;

     var prop;
     var val;
     log.write('<b>Attribute listing:</b><br>\n');
     log.write('<table border="1">')
     for( i in obj ) {
          log.write('<tr valign="top"><th>' +i+ '</th><td>' + entify(obj[i]) + "</td></tr>\n");
     }
     log.write('</table>');
     log.close();
}

function entify( o ) {
     if( o ) {
	  s = new String(o);
	  s.replace('<', '&amp;amp;lt;');
	  s.replace('>', '&amp;amp;gt;');
	  return s;
     }
     return o;
}

function selectOptionWithValue( selectList, value ) {
     foundIt = false;

     // Netscape 4 and Opera need special treatment:
     if( ! (isNS4 || isOp5) ) {
          selectList.value = value;
          return true;
     }

     // Bizarrely, when the index starts at 0, Netscape (4.05/NT) gets
     // the value of the option at index 0 wrong!
     for( i=selectList.length-1; i>=0; i-- ) {
          if( selectList[i].value == value ) {
               if( isNS4 ) {
                    selectList.selectedIndex = i;
               } else if ( isOp5 ) {
                    selectList.value = i;
               }
               foundIt = true;
               break;
          }
     }
     return foundIt;
}


function stripSpace( s ) {
     i = 0;
     j = s.length - 1 ;
     while( s.charAt(i) == ' ' ) { i++ };
     while( s.charAt(j) == ' ' ) { j-- };
     return s.substring(i, j+1);
}
