function getRefToDiv(divID,oDoc) {
  if( document.getElementById ) {
    return document.getElementById(divID); }
  if( document.all ) {
    return document.all[divID]; }
  if( !oDoc ) { oDoc = document; }
  if( document.layers ) {
    if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
      //repeatedly run through all child layers
      for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
        //on success, return that layer, else return nothing
        y = getRefToDiv(divID,oDoc.layers[x].document); }
    return y; } }
  return false;
}

function changeDisplay( elementId, setTo ) {
   var theElement;
	 theElement = getRefToDiv(elementId);
  if( !theElement ) {
    /* The page has not loaded, or the browser claims to
    support document.getElementById or document.all but
    cannot actually use either */
    return;
  }
  //Reference the style ...
  if( theElement.style ) { theElement = theElement.style; }
  if( typeof( theElement.display ) == 'undefined' ) {
    //The browser does not allow us to change the display style
    //Alert something sensible (not what I have here ...)
    //window.alert( 'Your browser does not support this' );
    return;
  }
  //Change the display style
  theElement.display = setTo;
}

function hideElement( elementId , dontHideSelects) {
  if (!dontHideSelects) dontHideSelects=0;
  changeDisplay( elementId, 'none' );
	if (dontHideSelects!=1) showSelects();
	return;
}

function showElement( elementId , dontHideSelects) {
  if (!dontHideSelects) dontHideSelects=0;
  changeDisplay( elementId, 'block' );
	if (dontHideSelects!=1) hideSelects();
	return;
}

function showElement2( elementId ) {
  changeDisplay( elementId, 'inline' );
	return;
}

function changeEnabled( elementId, setTo ) {
   var theElement;
	 theElement = getRefToDiv(elementId);
  if( !theElement ) {
    return;
  }
  //if( theElement.style ) { theElement = theElement.style; }
  if( typeof( theElement.disabled ) == 'undefined' ) {
    return;
  }
  theElement.disabled = setTo;
	//document.form1.description.value = theElement.disabled;
}

function enableElement( elementId ) {
  changeEnabled( elementId, false );
	return;
}

function disableElement( elementId ) {
  changeEnabled( elementId, true );
	return;
}

// hide all selects 
function hideSelects(){
 selects = document.getElementsByTagName("select");
 for(i=0;i<selects.length;i++){
  selects[i].style.visibility="hidden";
 }
}

// show all selects 
function showSelects(){
 selects = document.getElementsByTagName("select");
 for(i=0;i<selects.length;i++){
  selects[i].style.visibility="visible";
 }
}

function redirect(pageURL){
 window.location = pageURL;
}

function changeDisplaySelect( elementId, flag ) {
  if (flag==1) showElement( elementId , 1);
	else if (flag==0) hideElement( elementId , 1);
	return;
}

function changeDisplayStraight( elementId, flag ) {
  if (flag!=0) showElement( elementId , 1);
	else hideElement( elementId , 1);
	return;
}

function changeDisplayReverse( elementId, flag ) {
  if (flag==0) showElement( elementId , 1);
	else hideElement( elementId , 1);
	return;
}

function changeDisplayThreshold( elementId, flag, threshold ) {
  if (!threshold) threshold=1;
  if (flag>=threshold) showElement( elementId , 1);
	else hideElement( elementId , 1);
	return;
}

function changeDisplayThresholdReverse( elementId, flag, threshold ) {
  //if (!threshold) threshold=1;
  if (flag>=threshold) hideElement( elementId , 1);
	else if (flag>0) showElement( elementId , 1);
	return;
}

function LinkedSelect(from,to,optionArray) {
       var fromCode = from.options[from.selectedIndex].value;
       to.options.length = 0;
       for (i = 0; i < optionArray.length; i++) {
         if (optionArray[i][0] == fromCode) {
           to.options[to.options.length] = 
                new Option(optionArray[i][1], optionArray[i][2], optionArray[i][3], optionArray[i][3]);
         }
       } 
}

function refreshForm(command) {
  getRefToDiv("c").value = command;
  document.forms[0].submit();
}

function setImgTarget( targetname ) {
  imgElem=getRefToDiv("imgtarget");
	imgElem.value=targetname;
	targetfield=targetname;
}

function insertTags(myField, startTag, endTag) {
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = startTag + sel.text + endTag;
}
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ startTag 
+ myField.value.substring(startPos, endPos)
+ endTag
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
myField.focus();
}

function insertList(myField, listType) {
var i, buf, brstr='';
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
for (i=0; i<sel.text.length; i++) {
    buf = sel.text.charAt(i);
		if (buf!='\n') brstr = brstr + buf;
		else brstr = brstr + buf + 'li>';
};
sel.text = '<' + listType + 'l>\n<li>' + brstr + '\n</' + listType + 'l>';
}
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
var tempstr = myField.value.substring(startPos, endPos);
for (i=0; i<tempstr.length; i++) {
		buf = tempstr.charAt(i);
		if (buf!='\n') brstr = brstr + buf;
		else brstr = brstr + buf + '<li>';
};

myField.value = myField.value.substring(0, startPos)
+ '<' + listType + 'l>\n<li>' + brstr + '\n</' + listType + 'l>'
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
myField.focus();
}
