// *** Copyright (c)2002 Jim Massey. All rights reserved  ***********
// *** massey@stlouis-shopper.com **************
// *** No Warranty - certified to work with nothing ***
// *** developed to work with mozilla kiosk **** 

window.addEventListener("load", collapse_VKboard, false);
window.document.addEventListener("click", item_got_focus, true);
current_VK_foucs = "";
VK_focus_value = "";

// *** A case block may be better here. There are some bad hacks here *****
function item_got_focus() {
	var j = 0;
	var current_focus = document.commandDispatcher.focusedElement;
	if ( current_focus == "[object XULElement]" ) { return }
	dump("FOCUSED1: " + current_focus +"\n");
        if (current_focus == null 
	     || current_focus.type != "text" 
	        || current_focus.type != "textarea"  ) {
           current_VK_focus = null;
           VK_focus_value = "";
        }
    // *** Test for various input types we can use **
	//dump("FOCUSED2: " + current_focus.type +"\n");
	var focus_value = current_focus.value;
	if (current_focus.type == "text" 
	     || current_focus.type == "textarea" 
                || current_focus.type == "password") {
	  VK_focus_value = focus_value;
	  current_VK_focus = current_focus;
	}
//var ed = document.getElementsByTagName("toolbox");
//var ed = document.getElementById('VKboard1');
//dump("*** TOOLBOX COLLAPSED: " + ed.getAttribute("moz-collapsed") + " **\n");
//dump("*** TOOLBOX COLLAPSED: " + document.commandDispatcher.advanceFocus() + " **\n");
//for (var list in ed) {
//    ++j;
//    dump(list+ "*ENUM*" + j + "\n");
//}

}

function item_got_focusB() {
	var j = 0;
	var current_focus = document.commandDispatcher.focusedElement;

}

// *** I can't find a good use for this but it is interesting ****
function VK_advance_focus() {
	document.commandDispatcher.rewindFocus();
	current_VK_focus = document.commandDispatcher.focusedElement;
	VK_focus_value = document.commandDispatcher.focusedElement;
	dump("### REWIND FOCUS: " + document.commandDispatcher.focusedElement + " ***\n");
}

// **** Handle Vkeystrokes from normal VKeys ****
function VKhandlefocus(VKvalue) {
dump("**** ITEM GOT FOCUS: " + current_VK_focus.name + " **\n");
  if(document.getElementById("VK_shift").getAttribute("label") == "shift ON") {
	VKvalue = VKvalue.toUpperCase();
	//dump("**** UPPERCASE: " + VKvalue.toUpperCase() + "  ****\n");
  }
	dump("**** ITEM FOCUS VALUE: " + VKvalue + " **\n");
	current_VK_focus.value = VK_focus_value + VKvalue;
	VK_focus_value = VK_focus_value + VKvalue;
	
}

// **** Delete entire contents of focused field ****
function VKhandledelete() {
	current_VK_focus.value = "";
	VK_focus_value = "";
	
}

// **** Delete the Last char from focused field ****
function VKbackspace() {
var i = 0;
var temp_len = VK_focus_value.length;
//dump("*** BACKSPC LENGTH: " + temp_len + " **\n");
var temp_string = "";
 for(i = 0; i < temp_len - 1; i++) {
	temp_string = temp_string + VK_focus_value.substr(i, 1);
	//dump("**** SUBStr : " + temp_string + " **\n");
 }
	current_VK_focus.value = temp_string;
	VK_focus_value = temp_string;

}

// **** Toggles the capslock flag - label - for cap letters
function capslock() {
	var temp = document.getElementById("VK_shift");
 	if (temp.getAttribute("label") == "shift/caps") {
		 temp.setAttribute("label", "shift ON");
	} else {
		 temp.setAttribute("label", "shift/caps");
 }

}

// **** Collapse the VKboard ****
function collapse_VKboard() {
      	var temp = document.getElementById('VKboard_box');
      	var tempB = document.getElementById('VKboard1');
	var tempC = document.getElementById('VKnumpad');
	var tempD = document.getElementById('VKspecialpad');
	if (tempB.getAttribute("moz-collapsed") != "true" ||
		tempB.getAttribute("collapsed") != "true") {
      	     temp.collapseToolbar(tempB);
	     //tempB.setAttribute("collapsed", "true");
	}
	tempC.setAttribute("moz-collapsed", "false");
	tempC.setAttribute("collapsed", "false");
	tempD.setAttribute("moz-collapsed", "true");
	tempD.setAttribute("collapsed", "true");
	//temp.collapseToolbar(tempC);
	//temp.collapseToolbar(tempD);
      	//temp.createCollapsedGrippy(tempB);
      	//temp.setAttribute("moz-collapsed", "true");
      	//document.persist(tempB, "moz-collapsed");
      	//temp.expandToolbar();
      	dump("** ONLOAD VKboard : " + temp.collapseToolbar + " **\n");
}

// *** Toggles the special keypads ***
function VKtoggle_pad() {
	var temp = document.getElementById('VKboard_box');
	var tempC = document.getElementById('VKnumpad');
	var tempD = document.getElementById('VKspecialpad');
	if (tempC.getAttribute("moz-collapsed") == "true" ||
	          tempC.setAttribute("collapsed", "true")) {
	    tempD.setAttribute("moz-collapsed", "true");
		tempD.setAttribute("collapsed", "true");
	    tempC.setAttribute("moz-collapsed", "false");
		tempC.setAttribute("collapsed", "false");
	} else {
	    //temp.collapseToolbar(tempC);
	    tempC.setAttribute("moz-collapsed", "true");
		tempC.setAttribute("collapsed", "true");
	    tempD.setAttribute("moz-collapsed", "false");
		tempD.setAttribute("collapsed", "false");
	}
}

// **** Function to reset VKboard *****
function VKboard_reset() {
	collapse_VKboard();
	document.getElementById("VK_shift").setAttribute("label", "shift/caps");
	current_VK_focus = null;
        VK_focus_value = "";
}