// Global variables
var isshift=0;
var iscaps=0;
var isaltgr=0;

/*
  Enable or disable CAPS LOCK
*/
function caps_lock() {
   if (iscaps==0) {
	iscaps=1;
	isshift=0;
	isaltgr=0;
	document.getElementById('caps1').value="CAPS";
	document.getElementById('caps2').value="CAPS";
	var the3rdcap = document.getElementById('caps3');
	if (the3rdcap) the3rdcap.value="CAPS";
	hide_numbers();
   }
   else	{
	iscaps=0;
	document.getElementById('caps1').value="";
	document.getElementById('caps2').value="";
	var the3rdcap = document.getElementById('caps3');
	if (the3rdcap) the3rdcap.value="";
	show_numbers();
   }
}

/*
  Enable SHIFT for the next keystroke
*/
function shift_key() {
   isshift=1;
   iscaps=0;
   isaltgr=0;

   document.getElementById('caps1').value="SHIFT";
   document.getElementById('caps2').value="SHIFT";
   var the3rdcap = document.getElementById('caps3');
   if (the3rdcap) the3rdcap.value="SHIFT";
   
   hide_numbers();
}

/*
  Enable ALTR-GR for the next keystroke
*/
function altgr_key() {
   isaltgr=1;
   iscaps=0;
   isshift=0;

   document.getElementById('caps1').value="ALTGR";
   document.getElementById('caps2').value="ALTGR";
   var the3rdcap = document.getElementById('caps3');
   if (the3rdcap) the3rdcap.value="ALTGR";

   show_altgr();
}

/*
  Sets the global variable containing the name of the input field to modify
*/
function select_field(field1, field2, field3, field4) {
  target_field = field1;
  var wanted_field = document.getElementById(field1);
  wanted_field.focus();
  wanted_field.style.backgroundColor="#FFFFFF";
  var wanted_radio_img = document.getElementById("radio_"+field1);
  wanted_radio_img.src="/pxpadmin/icons/radio_checked.gif";

  var other_field = document.getElementById(field2);
  other_field.style.backgroundColor="#E6E7E6";
  var other_radio_img = document.getElementById("radio_"+field2);
  other_radio_img.src="/pxpadmin/icons/radio_unchecked.gif";

  var other_field_bis = document.getElementById(field3);
  if (other_field_bis) {
    other_field_bis.style.backgroundColor="#E6E7E6";
  }
  var other_radio_img_bis = document.getElementById("radio_"+field3);
  if (other_radio_img_bis) {
    other_radio_img_bis.src="/pxpadmin/icons/radio_unchecked.gif";
  }

  var other_field_ter = document.getElementById(field4);
  if (other_field_ter) {
    other_field_ter.style.backgroundColor="#E6E7E6";
  }
  var other_radio_img_ter = document.getElementById("radio_"+field4);
  if (other_radio_img_ter) {
    other_radio_img_ter.src="/pxpadmin/icons/radio_unchecked.gif";
  }
}

/*
  Writes a character, testing CAPS LOCK, SHIFT and ALT-GR
*/
function write_std(sChar) {

   var value_to_modify=document.getElementById(target_field).value;
   if (iscaps==1) {
	var sChar = sChar.toUpperCase();
	document.getElementById(target_field).value=value_to_modify + sChar;
	changeKeyboardPosition();
   }
   else if (isshift==1)	{
	var sChar = sChar.toUpperCase();
	isshift=0;
	document.getElementById('caps1').value="";
	document.getElementById('caps2').value="";
	var the3rdcap = document.getElementById('caps3');
	if (the3rdcap) the3rdcap.value="";
	document.getElementById(target_field).value=value_to_modify + sChar;
	changeKeyboardPosition();
	show_numbers();
   }
   else if (isaltgr==1)	{
	isaltgr=0;
	document.getElementById('caps1').value="";
	document.getElementById('caps2').value="";
	var the3rdcap = document.getElementById('caps3');
	if (the3rdcap) the3rdcap.value="";
	document.getElementById(target_field).value=value_to_modify + sChar;
	changeKeyboardPosition();
	show_numbers();
   }
   else	{
	document.getElementById(target_field).value=value_to_modify + sChar;
	changeKeyboardPosition();
   }
}

/*
  Writes a character that is not shiftable (number, punctuation, etc.)
*/
function write_car(sChar) {

   var value_to_modify=document.getElementById(target_field).value;
   document.getElementById(target_field).value=value_to_modify + sChar;
   if (isshift==1) {
	isshift=0;
	document.getElementById('caps1').value="";
	document.getElementById('caps2').value="";
	var the3rdcap = document.getElementById('caps3');
	if (the3rdcap) the3rdcap.value="";
	show_numbers();
   }
   else if (isaltgr==1) {
	isaltgr=0;
	document.getElementById('caps1').value="";
	document.getElementById('caps2').value="";
	var the3rdcap = document.getElementById('caps3');
	if (the3rdcap) the3rdcap.value="";
	show_numbers();
   }
   changeKeyboardPosition();
}

function back_space() {
   var value_to_modify=document.getElementById(target_field).value;
   var new_value_to_modify = value_to_modify.slice(0,-1);
   document.getElementById(target_field).value=new_value_to_modify;
}


/*
  To change keyboard position
*/
function changeKeyboardPosition() {
   if (hasToMoveKeyboard) {
	var divx = Math.random();
	divx= 0 + ( divx * ( 150 - 0 ) );
	divx = Math.round(divx);
	var divy = Math.random();
	divy= -15 + ( divy * ( 45 - -75 ) );
	divy = Math.round(divy);
  
	var posKeyboard = document.getElementById('divKeyboard');
	posKeyboard.style.left = divx + "px";
	posKeyboard.style.top = divy + "px";
   }
}

/*
  Hide the normal keyboard
*/
function hide_numbers() {
   var numkeys = document.getElementById('numbers');
   var specialkeys = document.getElementById('special');
   var altgrkeys = document.getElementById('altgr');
  
   numkeys.style.display = "none";
   specialkeys.style.display = "";
   if (altgrkeys) altgrkeys.style.display = "none";
}

/*
  Show the normal keyboard
*/
function show_numbers() {
   var numkeys = document.getElementById('numbers');
   var specialkeys = document.getElementById('special');
   var altgrkeys = document.getElementById('altgr');

   numkeys.style.display = "";
   specialkeys.style.display = "none";
   if (altgrkeys) altgrkeys.style.display = "none";
}

/*
  Show the Altr-Gr keyboard
*/
function show_altgr() {
   var numkeys = document.getElementById('numbers');
   var specialkeys = document.getElementById('special');
   var altgrkeys = document.getElementById('altgr');

   numkeys.style.display = "none";
   specialkeys.style.display = "none";
   if (altgrkeys) altgrkeys.style.display = "";
}

