/*
_________                __                 
\_   ___ \  ____   _____/  |______  ___  ___
/    \  \/ /  _ \ /    \   __\__  \ \  \/  /
\     \___(  <_> )   |  \  |  / __ \_>    < 
 \______  /\____/|___|  /__| (____  /__/\_ \
        \/            \/          \/      \/
		Contact Center - http://www.contax.com.br

  $Teclado virtual. CTXTVCLI.JS, 24/11/2006: 12:07:00
  Release por Rodrigo M. Roque (rmatuck@contax.com.br)
  
  Changelog:
  24/11/2006 - Adicionado compatibilidade para funcionar com o browser netscape, opera e firefox. (rmatuck)
*/

// variáveis globais
var VIRK_LAYOUT = "en";
var VIRK_NOKBD = false;

if ( !window.VIRK_PATH )
	window.VIRK_PATH = "js/";

// tamanho do teclado
var VIRK_SIZE = [286,93];
var theVirk = null;

// instancia o objeto
ctrlAttachEvent (window, "load", function () { if (!theVirk) {theVirk = new Virk ();} });

// função de esconder ou exibir o teclado. 0 = false e 1 = true
function kbdShowHide (fShow) {
   if (!theVirk)
      theVirk = new Virk ();
    
	fShow ? theVirk.show () : theVirk.hide ();
}

function Virk () 
{
	this.elem = document.getElementById ("virk");
	
	if (this.elem && this.elem.tagName == "IFRAME") {
		this.wnd = window.frames ["virk"];
		this.show = virkFrameShow;
		this.hide = virkFrameHide;
		
		if ( this.elem.style.visibility != "hidden" && this.elem.style.display != "none" )
			this.show ();
	}
	else {
		this.show = virkDialogShow;
		this.hide = virkDialogHide;
	}
   
	this.attachOnFocus ();
	
	if (VIRK_NOKBD) {
		this.blockKbd ();
		this.show ();
	}
}

function virkDialogShow () 
{
	if (this.wnd && !this.wnd.closed)
		return;
   
	var href = window.VIRK_PATH + "teclado.html";
	
	if (VIRK_LAYOUT)
		href += "?layout=" + VIRK_LAYOUT;
	
	if (window.showModelessDialog) {
		var features = "dialogWidth:" + (VIRK_SIZE [0] + 6) + "px;dialogHeight:" + (VIRK_SIZE [1] + 25) + "px"
			+ ";scroll:no;help:no;status=no;";
		
		this.wnd = window.showModelessDialog (href, { opener: window }, features);
   }
	else {   
		var features = "dependent=yes,width=" + VIRK_SIZE [0] + ",height=" + VIRK_SIZE [1]
			+ ",scroll=no,help=no,status=no,directories=no,menubar=no,resizable=no";
		this.wnd = window.open (href, "Virk", features);
	}
}

function virkDialogHide () 
{
	if (this.wnd && !this.wnd.closed)
		this.wnd.close ();
	
	this.wnd = null;
}

function virkFrameShow () 
{
	this.elem.style.visibility = "visible";
	
	if (this.wnd && this.wnd.virkCtrl) {
		if (VIRK_LAYOUT) {
			this.wnd.virkCtrl.setLayout (VIRK_LAYOUT);
			VIRK_LAYOUT = "";
		}
		
		this.wnd.virkCtrl.attach (window);
	}
}

function virkFrameHide () 
{
	this.elem.style.visibility = "hidden";
	
	if (this.wnd && this.wnd.virkCtrl)
		this.wnd.virkCtrl.detach ();
}

Virk.prototype.blockKbd = function () {
   for (var i = 0; i < document.forms.length; ++i) {
      var ctrls = document.forms [i].elements;
      for (var j = 0; j < ctrls.length; ++j) {
         ctrls [j].onkeypress = function () { return false; }
         ctrls [j].onclick = function () { theVirk.show (); }
      }
   }
}

// Rotina para verificar todos os objetos do formulário. Objetivo é identificar os
// objetos que são do tipo INPUT
Virk.prototype.attachOnFocus = function (wnd) 
{
	try {
		wnd = wnd ? wnd : window;
		var forms = wnd.document.forms;
		
		for (var i = 0; i < forms.length; ++i) 
		{
			var ctrls = forms [i].elements;
			
			for (var j = 0; j < ctrls.length; ++j) 
			{
				if (isEditable (ctrls [j]))
					ctrlAttachEvent (ctrls [j], "focus", virkOnFocus);
			}
		}
 
		var frames = wnd.document.getElementsByTagName ("IFRAME");
		for (var i = 0; i < frames.length; ++i)
			ctrlAttachEvent (frames [i], "load", virkOnLoadFrame);
		
		for (var i = 0; i < wnd.frames.length; ++i)
			this.attachOnFocus (wnd.frames [i]);
	}
	catch (e) {
	}
}

// rotina para obter layout
Virk.prototype.getLayout = function () 
{
	if (!this.wnd || this.wnd.closed)
		return "en";
	
	return this.wnd.virkCtrl.getLayout ();
}

function virkOnLoadFrame () {
	theVirk.attachOnFocus ();
}

function virkOnFocus (e) {
	theVirk.activeCtrl = e.srcElement || e.target;
}

// verifica se o campo corresponde do tipo
function isEditable (ctrl) {
	return ctrl.isContentEditable
		|| ctrl.tagName == "INPUT" && ( ctrl.type == "text" || ctrl.type == "password" )
		|| ctrl.tagName == "TEXTAREA";
}

// 
function ctrlAttachEvent (ctrl, type, listener) 
{

	if (ctrl.addEventListener) {
		ctrl.addEventListener (type, listener, false);
	}
	else if (ctrl.attachEvent) {
		ctrl.detachEvent ("on" + type, listener);
		ctrl.attachEvent ("on" + type, listener);
	}
}