$(document).ready(function () {
  $(".lgnMsWarn div b").click(function () {
    var days = parseInt($(this).attr("tc:hidedays"), 10);
    createCookie("tcmw", "hide", days);
    $(this).parent().parent().hide();
  });
  // Help links.
  $("b.tcHelp").click(function () {
    HelpWindow("/tcterms/help/?ctg=" 
      + $(this).attr("tc:ctg") + "&HelpID=" + $(this).attr("tc:id"));
  });
});

function highlight()
{
 var oObj = event.srcElement;
 oObj.style.color = '#ffffff' ;
}

function lolight()
{
 var oObj = event.srcElement;
  oObj.style.color = '#000000' ;
  oObj.style.textDecoration = "none" ;
}

function LogOut_onclick(CafeURL) {
location.href=CafeURL + '/Logout.asp'
}

function HelpWindow(PageName){
	newwin=window.open(PageName,"Help", "height=600, width=480, toolbar=no, menubar=no, scrollbars=yes,resizable=yes");
}

function NewWindow(PageName, WindowName){
	newwin=window.open(PageName, WindowName, "height=600, width=540, toolbar=no, menubar=no, scrollbars=yes, resizable=yes");
}

function JumpTo(url,confmsg)
{
	var theResponse = window.confirm(confmsg);
	if (theResponse) 
	{
		window.location=url
	}
}

function JumpToURL(url)
{
	window.location=url
}

//three spell checking functions for use with ieSpell:
//put the following on the button place:
//	<script language="JavaScript" type="text/javascript">checkspells();</script>

function checkspells() {
	try {
		var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
		document.write("&nbsp;<input type='button' name='btnSpell' value='Check Spelling' onclick='checkspell()' />");
		}
	catch(exception) {
	if (is_ie&&is_win) {
	    document.write("&nbsp;<input type='button' name='btnSpell' value='Get ieSpell' onclick='checkspell()' />");
		}
	}
}

function forumcheckspells() {
	try {
		var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
		document.write("&nbsp;<img alt='Spellcheck' src='http://www.translatorscafe.com/cafe/megabbs/images/spellcheck.gif' align='top' name='spellcheck' onclick='checkspell()' />");
		}
	catch(exception) {
	if (is_ie&&is_win) {
	    document.write("&nbsp;<img alt='Spellcheck' src='http://www.translatorscafe.com/cafe/megabbs/images/GetIESpell.gif' align='top' name='GetIESpell' onclick='checkspell()' />");
		}
	}
}

function checkspell() {
	try {
		var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
		tmpis.CheckAllLinkedDocuments(document);
	}
	catch(exception) {
		if (is_ie&&is_win) {
			window.open("http://www.iespell.com/download.php","DownLoad");
		}
	}
}

/* ie-activex fix taken from http://activecontent.blogspot.com/ */
var bo_ns_id = 0;

function ieFixStart()
{
  if(isIE())
  {
    document.write('<div id="bo_ns_id_' + bo_ns_id + '"><!-- ');
  }
}
function ieFixEnd()
{
  if(isIE())
  {
    document.write('</div>');
    var theObject = document.getElementById("bo_ns_id_" + bo_ns_id++);
    var theCode = theObject.innerHTML;
    theCode = theCode.substring(4 ,9+theCode.indexOf("</object>"));
    document.write(theCode);
  }
}
function isIE()
{
  // only for Win IE 6+
  // But not in Windows 98, Me, NT 4.0, 2000
  var strBrwsr= navigator.userAgent.toLowerCase();
  if(strBrwsr.indexOf("msie") > -1 && strBrwsr.indexOf("mac") < 0)
  {
    if(parseInt(strBrwsr.charAt(strBrwsr.indexOf("msie")+5)) < 6)
    {
      return false;
    }
    if(strBrwsr.indexOf("win98") > -1 || strBrwsr.indexOf("win 9x 4.90") > -1 
      || strBrwsr.indexOf("winnt4.0") > -1 || strBrwsr.indexOf("windows nt 5.0") > -1)
    {
      return false;
    }
    return true;
  }
  else
  {
    return false;
  }
}
/* end of ie activex fix */

function hideElement (id) {
	var o = document.getElementById (id);
	if (o) {
		o.style.display = "none";
	}
}

window.onload = function () {
  document.body.onclick = uiHideMenu;
}

var gUiMenu = null;

function uiHideMenu () {
  if (gUiMenu) {
    gUiMenu.style.display = "none";
  }  
}

function uiSelectLng (link, e, isRtl) {

  if (!e) var e = window.event;
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
  uiHideMenu ();

  var mnu = document.getElementById ("uilSelector");
  
  if (mnu) {
    mnu.style.display = "block";
    var top = uiGetElementTop (link) + link.offsetHeight;
    var left;
    if (isRtl) {
      left = uiGetElementLeft(link) - (mnu.offsetWidth - link.offsetWidth) + 20;
    }
    else {
      left = uiGetElementLeft (link);
    }
    mnu.style.top = top + "px";
    mnu.style.left = left + "px";
    gUiMenu = mnu;
  }
}

/* Set the el position above or under the a. */
function uiSetPosition(el, a, isRtl, eh, ew, ah, aw) {

  var eBody = document.body;
  var nWindowHeight = (window.innerHeight != null) ? window.innerHeight : eBody.clientHeight;
  var nWindowWidth = (window.innerWidth != null) ? window.innerWidth : eBody.clientWidth;
  var left;
  var top = uiGetElementTop(a) - eh - 3;
  if (top < 0) { top = uiGetElementTop(a) + ah; }
  if (isRtl) {
    left = uiGetElementLeft(a) - (ew - aw);
  }
  else {
    left = uiGetElementLeft(a);
  }
  el.style.top = top + "px";
  el.style.left = left + "px";

}

function uiGetElementTop (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 uiGetElementLeft (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 tcLoadXml (url, fptr) {
  // branch for native XMLHttpRequest object
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.onreadystatechange = fptr;
    req.open ("GET", url, true);
    req.send(null);
  // branch for IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
      req.onreadystatechange = fptr;
      req.open("GET", url, true);
      req.send();
    }
  }
}

// args: 
//  url,
//  success = pointer to fn (resultText)
//  error = pointer to fn (statusCode)
function tcAjax (args) {
  var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

  if (xhr) {
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) { // complete
        if (!xhr.status || (xhr.status >= 200 && xhr.status < 300)
          || xhr.status == 304 || xhr.status == 1223) {
          if (args.success) {
            args.success(xhr.responseText);
          }
        }
        else {
          if (args.error) {
            args.error(xhr.status);
          }
        }
      }
    };
    xhr.open("GET", args.url, true);
    xhr.send()
  }

}

function pausecomp(Delay) 
{
	// Delay in milliseconds
	var date = new Date();
	var CurDate = null;

	do { CurDate = new Date(); } 
	while(CurDate - date < Delay);

}

/* Text area change height on text entering. */
function tcSetTextAreaAutoHeight(jsSelect) {

  var t = $(jsSelect);
  if (typeof (t) == "undefined" || t == null) { return; }
  if (t.length > 0) {
    var s = t.after("<div class=\"ctlcopy\" style=\"width:" + t.width().toString() + "px;\"></div>");
    s.css("font-family", t.css("font-family"));
    s.css("font-size", t.css("font-size"));
    s.css("line-height", t.css("line-height"));

    $(jsSelect).keyup(
      function () {
        var v = $(this).val();
        var s = $(this).parent().find(".ctlcopy");
        $(s).html(tcToHtml(v) + "...");
        var h = Math.max(15, $(s).height()) + 5;
        $(this).height(h);
      }
    );
  }
}

function tcToHtml(a) {
  if (!a.toString || typeof (a) == "undefined" || a === null) return "";
  var sc = new Array({ c: /&/g, r: "&amp;" }, { c: /"/g, r: "&quot;" }, { c: /'/g, r: "&#039;" },
    { c: /</g, r: "&lt;" }, { c: />/g, r: "&gt;" }, { c: / /g, r: "&nbsp;&shy;" },
    { c: /\n/g, r: "<br />" });
  var i;
  var s = a.toString();
  for (i = 0; i < sc.length; i++) {
    s = s.replace(sc[i].c, sc[i].r)
  }
  return s;
} 



/* Dynamic image loading. */
function tcInitializeDynImages(jqSelect) {
  $(document).ready(function () {
    tcShowDynImage(jqSelect);
  });
  $(window).scroll(function () {
    tcShowDynImage(jqSelect);
  });
}

function tcShowDynImage(jqSelect) {
  $(jqSelect).each(function () {
    if (tcIsScrolledIntoView($(this)) && $(this).attr("tc:shown") != "1") {
      $(this).attr("src", $(this).attr("tc:s"));
      $(this).attr("tc:shown", "1");
    }
  });
}

function tcIsScrolledIntoView(elem) {
  var docViewTop = $(window).scrollTop();
  var docViewBottom = docViewTop + $(window).height();
  var elemTop = $(elem).offset().top;
  var elemBottom = elemTop + $(elem).height();
  return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

/* String concatenation by Lasse Reichstein Nielsen. */
function StringBuilder() {
  this.buffer = [];
}

StringBuilder.prototype.append = function append(string) {
  this.buffer.push(string);
  return this;
};

StringBuilder.prototype.toString = function toString() {
  return this.buffer.join("");
};

/* Text insertion */
var tcTextCursor = 0;
var tcTextField;
var tcTextCounter = new TextCounterFunction ();

function TextCounterFunction() {
  this.pointer = null;
  this.outputId = "";
  this.limit = 0;
}

// Track cursor position
function textPosition(o, pt, cntId, cntMax) {
  tcTextField = o;
  tcTextCounter.pointer = pt;
  tcTextCounter.outputId = cntId;
  tcTextCounter.limit = cntMax;
  if (!document.selection) { tcTextCursor = o.selectionStart; }
}

function textInsert(text) {

  if (!tcTextField) { return; }

  // Except Opera
  if (navigator.userAgent.indexOf('Opera') == -1) { tcTextField.focus(); }

  // IE, Opera
  if (document.selection) {
    document.selection.createRange().text = text;
  }

  // FireFox, Mozilla
  else {
    // Split text on cursor position
    var strFirst = tcTextField.value.substr(0, tcTextCursor);
    var strEnd = tcTextField.value.substr(tcTextCursor, tcTextField.value.length);
    var num = 0;
    if (strFirst == '') { num = 1; }
    // Insert the text
    tcTextField.value = strFirst + text + strEnd;
    // New cursor position
    tcTextCursor = strFirst.length + text.length + num;
    // Set cursor after the text
    tcTextField.selectionStart = tcTextField.selectionEnd = tcTextCursor;
  }

  if (tcTextCounter.pointer) {
    tcTextCounter.pointer (tcTextField, tcTextCounter.outputId, tcTextCounter.limit);
  }
}

/*
  Cookie functions.
  Based on: http://www.quirksmode.org/js/cookies.html
 */
function createCookie(name, value, days) {
  var expires;
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
  }
  else { expires = ""; }
  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') { c = c.substring(1, c.length); }
    if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length, c.length); }
  }
  return null;
}

function eraseCookie(name) {
  createCookie (name, "", -1);
}

