var TERMWEB_ROOT_URL="/tcnewtermweb"

//// General functions ////
function getObj(n,d) {
  var x,i; if(!d) d=document; if(!x && d.getElementById) x=d.getElementById(n);
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  if(!x&&d.links) x=d.links[n];
  if(!x) x=getLink(n,d)
  if(!x) x=getDiv(n,d)   
  return x;
}
function getDiv(n,d) {return getByTag("div",n,d)}
function getLink(n,d) {return getByTag("a",n,d)}
function getByTag(t,n,d) {var x; if(!d) d=document;if(d.getElementsByTagName) x=d.getElementsByTagName(t)[n];return x}
/** Return the form which contains the component c */
function getForm(c) {var x,i,d; d=document; 
  for(i=0;i<d.forms.length;i++)
    if(d.forms[i][c.name]) return d.forms[i]
}
function isValid(comp) {if(comp==null) return false;return comp.getAttribute("valid")}
function setValid(comp, valid) {if(comp) {comp.setAttribute("valid", valid); var c = valid?'':'#ff0000'; comp.style.color=c}}
function showMsg(msg) {window.status = msg}
function bl() {showMsg('')}
function contains(str, c) { return str.indexOf(c) != -1}

//// Flow control
function doAction(n) {var o=getObj("do");o.value=n;getForm(o).submit()}
function redirectTo(url) {self.location.href=url}

//// Functions for rollover images ////
function preloadImg(imgPath, imgs) {
  var d=document;
  var v = new Array("_up","_dw","_ov")
  if(!d.preImg) d.preImg=new Array();  
  var i,j,pos=d.preImg.length
  for(i=0; i<imgs.length; i++) {
    for(j=0;j<v.length;j++) {    
      d.preImg[pos]=new Image()
      d.preImg[pos++].src=imgPath + imgs[i] + v[j]
    }
  }
}

function imgUp(n) {if(!dis(n)) chgSrc(n, "_ov")}
function imgOut(n) {if(!dis(n)) chgSrc(n, "_up")}
function imgOver(n) {if(!dis(n)) chgSrc(n, "_ov")}
function imgDown(n) {if(!dis(n)) chgSrc(n, "_dw")}

function chgSrc(n, x) {
  var i = getObj("Img" + n)
  var s = new String(i.src)
  var suf = s.substr(s.length-4, s.length)
  i.src = s.substr(0, s.length-7)+x+suf
}

function enableButton(n, e) {
  if(e) {
    getObj("Img" + n).removeAttribute("dis")
    getLink("a" + n).setAttribute("href", "#")
    chgSrc(n, "_up")
  }
  else {
    getObj("Img" + n).setAttribute("dis", true)
    getLink("a" + n).removeAttribute("href")
    chgSrc(n, "_na")
  }
}

function dis(n) {return getObj('Img' + n).getAttribute('dis')}

function getIcon(icon, w, h) {
  var s='<img src="' + TERMWEB_ROOT_URL + '/img/' + icon + '"'
  if(w) s+=' width=' + w
  if(h) s+=' height=' + h  
  document.write(s+='>')
}

//// Functions for dialogs ////

/* This refers to the document which opened the dialog.*/
var parentWin
  
function initDialog() {
  if(opener) {
    parentWin=opener
  }
  if(self.dialogArguments) {
    parentWin = dialogArguments
  }
} 

function showDialog(url,w,h) {
  if(!w) {w=300}
  if(!h) (h=160)
  if(window.showModalDialog) {
    h=h+33
    w=w+7
    var prop = 'dialogWidth:' + w + 'px;dialogHeight:' + h + 'px;help:no;scroll:no;status:no'
    window.showModalDialog(url,window,prop);           
  } else {
    showWindow(url, w, h)            
  }    
}

function showWindow(url,w,h) {
  if(!w) {w=300}
  if(!h) (h=160)
  var dw = window.innerWidth;
  var dh = window.innerHeight
  var left = (dw-w)/2+window.screenX
  var top = (dh-h)/2+window.screenY      
  var prop ='width=' + w + ',height=' + h + ',left='+left+',top='+top
  window.open(url,'',prop);    
}

function showImageWindow(url) {
  var sw=self.screen.availWidth
  var wsize=new Array(800,1024,1152,1280,1600)
  var widths=new Array(400,435,485,580,665)
  var heights=new Array(500,575,635,770,880)
  var ww=new Array()
  var wh=new Array()
  for(var i=0;i<wsize.length;i++) {
    ww[wsize[i]]=widths[i]
    wh[wsize[i]]=heights[i]
  }
  var b=top.document.body
  var outw=self.outerWidth
  var outh=self.outerHeight
  var pw=outw?outw:b.offsetWidth
  var ph=outh?outh:b.offsetHeight
  imgW=ww[sw]
  imgH=wh[sw]
  var left=sw-imgW-10
/*  
  if(pw>left) {
    addSize=new SizeObj(top.document)
    parent.parent.window.resizeTo(Math.max(650,left), ph+addSize.h)
  }
*/  
  parent.parent.window.moveTo(0,0)
  var prop ='width='+imgW+',height='+imgH+',left='+left+',top='+top+',resizable=yes'
  window.open(url,'',prop);
}

function SizeObj(d) {
  var w,h
  parent.parent.window.moveTo(0,0)
  parent.parent.window.resizeTo(400,400)
  var outw=self.outerWidth
  var outh=self.outerHeight
  var objw=outw?outw:d.body.offsetWidth
  var objh=outh?outh:d.body.offsetHeight
  this.w=400-d.body.offsetWidth
  this.h=400-d.body.offsetHeight
}
//// Functions for forms ////
function getElementIndex(form, obj) {
  var e = form.elements
  for (var i=0; i<e.length; i++) {
    if (obj == e[i]) return i
  }
  return -1;
} 

function focusNext(obj) {
  var f = getForm(obj)
  var i = getElementIndex(f, obj)    
  var e = f.elements
  var j=i+1;
  if (i == -1 || j >= e.length) return
  e[j].focus();   
}

//// Functions for help ///
function openHelpWindow(url) {
  var prop = "width=600,height=550,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes"
  helpWin = window.open(url,"TermWebHelp",prop)
  helpWin.focus()
}

//// Functions for menus ////
function setMenuItemEnabled(n, e) {
  var c=getLink("mi"+n,getControlFrame().document)
  if(!c) return
  if(e) {
    c.setAttribute("href",c.getAttribute("r"))
    c.className="menuItem"
  } else {
    c.removeAttribute("href")
    c.className="disMenuItem"
  }
}

//// Functions for validating input ////
function numberOnly(e) {if(!isDigit(e.keyCode)) e.returnValue = false}
function trimString(s) {return s.replace(/^(\s+)/,"").replace(/(\s+)$/,"")}
function hasValue(c) {var v=c.value; var s=new String(v); return trimString(s)!=''} 
function isDigit(c) {return isBetween(c, 0, 9)}
function isBetween(c, bot, top) {return c >= (48+bot) && c <= (48+top)}
function checkEmail(s) {var f=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;return f.test(s)}

function getPageOffsetLeft(el) {
  var x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);
  return x;
}

function getPageOffsetTop(el) {
  var y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);
  return y;
}
