var isdrag = false;
var x, y, tx, ty;
var dobj;

function thQtnPreview () {
	var prv, prvQtn, prvBtn;
	var spec, level, lngpair, lngs, lngt;
	prv = document.getElementById ("previewPane");
	prvQtn = document.getElementById ("prQtn");
	prvBtn = document.getElementById ("prvBtn");
	spec = document.getElementById ("Spec");
	lngs = document.getElementById ("LngSrc");
	lngt = document.getElementById ("LngTrg");
	
	if (prv.style.display == "none" || prv.style.display == "") {
		if (document.getElementById ("Diff0").checked) {level=document.getElementById ("lblD0").innerHTML;}
		else {level=document.getElementById ("lblD1").innerHTML;}
		
		if (lngs.selectedIndex > 0) {lngpair = lngs.options[lngs.selectedIndex].innerHTML + "->";}
		else {lngpair="";}
		lngpair += lngt.options[lngt.selectedIndex].innerHTML;
		
		prvQtn.innerHTML = "<img src='images/panelClose.gif' align=right onclick='thQtnPreview();'>"
		+ "<h3 class=\"thH3\">" + document.getElementById ("SubjHdr").innerHTML + "</h3><p class=\"thP\">"
		+ document.getElementById ("thSubj").value
		+ "</p><h3 class=\"thH3\">" + document.getElementById ("DescHdr").innerHTML + "</h3><p class=\"thP\">"
		+ document.getElementById ("thDesc").value
		+ "</p><h3 class=\"thH3\">" + document.getElementById ("ContHdr").innerHTML + "</h3><p class=\"thP\">"
		+ document.getElementById ("thCont").value
		+ "</p><br><div class=thBold>" + lngpair + "</div>"
		+ "<div class=thBold>" + spec.options[spec.selectedIndex].innerHTML + "</div>"
		+ "<div class=thBold style='margin-bottom:65pt'>" + level + "</div>";

		lngs.style.visibility = "hidden";
		lngt.style.visibility = "hidden";
		spec.style.visibility = "hidden";
		prv.style.display = "block";
		thShowPanel	(prv, prvBtn);
	}
	else {
		prv.style.display = "none";
		lngs.style.visibility = "visible";
		lngt.style.visibility = "visible";
		spec.style.visibility = "visible";
	}
}

function thAnsPreview () {
	var prv = document.getElementById ("previewPane");
	var	prvAns = document.getElementById ("prAns");
	var prvBtn = document.getElementById ("prvBtn");
	
	if (prv.style.display == "none" || prv.style.display == "") {
		prvAns.innerHTML = "<img src='images/panelClose.gif' align=right onclick='thAnsPreview();'>"
		+ "<h3 class=\"thH3\">" + document.getElementById ("SubjHdr").innerHTML + "</h3><p class=\"thP\">"
		+ document.getElementById ("ansSubj").value
		+ "</p><h3 class=\"thH3\">" + document.getElementById ("DescHdr").innerHTML + "</h3><p class=\"thP\">"
		+ document.getElementById ("ansDesc").value
		+ "</p>";
		prv.style.display = "block";
		thShowPanel	(prv, prvBtn);
	}
	else {
		prv.style.display = "none";
	}
}

function thShowPanel (oPanel, oAnchor) {
	// based on http://www.webreference.com/dhtml/column71/2.html
	// panel placed above oAnchor if possible
	var eBody = document.body;
	var nMinSpace = 5;
	var nWindowTopEdge  = eBody.scrollTop;
	var nTopPos  = thGetElementTop (oAnchor) - oPanel.offsetHeight;
	var nWindowHeight   = (window.innerHeight != null)? window.innerHeight : eBody.clientHeight;
	/*
	var nWindowBottomEdge = (nWindowTopEdge + nWindowHeight) - nMinSpace;
	var nElementBottomEdge = nTopPos + oPanel.offsetHeight;
	if (nElementBottomEdge > nWindowBottomEdge) {
		nTopPos -= (nElementBottomEdge - nWindowBottomEdge);
		nTopPos = Math.max (nMinSpace, nTopPos);
	}
	*/
	if (nTopPos < nWindowTopEdge) {
		nTopPos += (nWindowTopEdge - nTopPos) + nMinSpace;
	}

	oPanel.style.top = nTopPos + "px";
	oPanel.style.left = thGetElementLeft (oAnchor) + "px";
	
}

function thGetElementTop (eElement) {
	// based on http://www.webreference.com/dhtml/diner/realpos1/index.html (originaly DL_GetElementTop())
		var nTopPos = eElement.offsetTop;        
		var eParElement = eElement.offsetParent; 
		while (eParElement != null)	{
				nTopPos += eParElement.offsetTop;
				eParElement = eParElement.offsetParent;
		}
		return nTopPos;
}

function thGetElementLeft (eElement) {
	// based on http://www.webreference.com/dhtml/diner/realpos1/index.html (originaly DL_GetElementTop())
		var nLeftPos = eElement.offsetLeft;        
		var eParElement = eElement.offsetParent; 
		while (eParElement != null)	{
				nLeftPos += eParElement.offsetLeft;
				eParElement = eParElement.offsetParent;
		}
		return nLeftPos;
}

function lTrim (sString) {
  while (sString.substring (0,1) == ' ') {
    sString = sString.substring (1, sString.length);
  }
  return sString;
}

function textareaTextCounter (field, cntId, maxSize)
{
  var cnt = document.getElementById (cntId);
  var warnBg = "#fdbdb3";
  var warnAttr = "warn";

  if (cnt) {
    if (!this.cntBg) {
      if (cnt.currentStyle) {this.cntBg = cnt.currentStyle.backgroundColor; /*ie*/}
      else {this.cntBg = ""; /*ff*/}
    }
	  cnt.innerHTML = field.value.length + '/' + maxSize;
	  if (field.value.length > maxSize) {
	    if (cnt.getAttribute (warnAttr) != 1) {
	      cnt.style.backgroundColor = warnBg;
	      cnt.setAttribute (warnAttr, 1);
	    }
	  }
	  else {
	    if (cnt.getAttribute (warnAttr) == 1) {
	      cnt.style.backgroundColor = this.cntBg;
	      cnt.removeAttribute (warnAttr);
	    }
	  }
	}
}

/* Drag */
function drag (e) {
  if(!e) var e = window.event;
  if (isdrag) {

    if (!e.preventDefault) {
      // This is the IE version for handling a strange
      // problem when you quickly move the mouse
      // out of the window and let go of the button.
      if (e.button == 0) {
        dragStop(e);
        return;
      }
    }

    dobj.style.left = tx + e.clientX - x + "px";
    dobj.style.top  = ty + e.clientY - y + "px";

    if (e.preventDefault) {
      e.preventDefault();
    }
    else {
      e.cancelBubble = true;
      return false;
    }

    //return false;
  }
}

function dragStart (e) {
  if(!e) var e = window.event;
  
  isdrag = true;
  if (this.parentElement)
    dobj = this.parentElement.parentElement.parentElement.parentElement.parentElement;
  else
    dobj = this.parentNode.parentNode.parentNode.parentNode.parentNode;

  tx = parseInt(dobj.style.left + 0);
  ty = parseInt(dobj.style.top + 0);
  
  x = e.clientX;
  y = e.clientY;
  document.onmousemove = drag;

  return false;
}

function dragStop (e) {
  isdrag = false;
}

//document.onmouseup=new Function("isdrag=false;");

function showDiacritics(cid, btn) {
  var c = document.getElementById(cid);
  var b = document.getElementById(btn);
  if (c && b) {
    if (c.innerHTML == "") {
      c.innerHTML = "<div id=\"diaInset\">" + getDiacritics(14) + "</div>";
    }
    if (c.style.display != "block") {
      c.style.display = "block";
      b.src = "/tcutils/images/cls.gif";
    }
    else {
      c.style.display = "none";
      b.src = "/tcutils/images/exp.gif";
    }
  }
}
