// ---------------- HANDLE KEYDOWN and KEYUP FOR WHOLE PAGE ----------------
var shiftdown = false;
var controldown = false;
function keydownhandler(event) {
	if (get_keycode(event) == 16) {
		shiftdown = true;
	} else if (get_keycode(event) == 17) {
		controldown = true;
	}	
}
function keyuphandler(event) {
	if (get_keycode(event) == 16) {
		shiftdown = false;
	} else if (get_keycode(event) == 17) {
		controldown = false;
	}
}
Event.observe(document, 'keydown', keydownhandler.bindAsEventListener(self));
Event.observe(document, 'keyup', keyuphandler.bindAsEventListener(self));




// --------------------- UTILITY FUNCTIONS -----------------
function get_keycode(event) {
  code = 0;
  if (event.which) {
  	code = event.which;
  } else { 
  	code = event.keyCode;
  }
  return code;
}

// only works properly in firefox
function get_key(event) {
  code = get_keycode(event);
  return String.fromCharCode(code);
}

function user_feedback(field) {
	field.setStyle({ background: '#ccc'});
	window.setTimeout("reset_user_feedback('" + field.id + "')", 100);
}

function reset_user_feedback(fieldid) {
	$(fieldid).setStyle({ background: 'url(/images/inputwidebg.gif)'});	
}

// check characters if event.which is availible - otherwise codes
function supress_characters(event, field, characters, codes, shiftcodes) {
  ok = true;
  if (controldown) {
  	return;
  }
  
  if (event.which) {
  	  if (characters.indexOf(get_key(event)) > -1) {	  	  
		ok = false;
	  }
  } else {
  	  code = get_keycode(event);
	  if (shiftdown) {
	  	for(i = 0; i < shiftcodes.length; i++) {
			if (shiftcodes[i] == code) {
				ok = false;
			}
		}
	  } else {
	  	for(i = 0; i < codes.length; i++) {
			if (codes[i] == code) {
				ok = false;
			}
		}
	  }
  }	  
  
  if (ok) {
	reset_user_feedback(field.id);
  } else {
	Event.stop(event);
	user_feedback(field);  
  }
}

	  
// ------------------------------ IE KEYCODES ------------------------------
var symbolchars_requireshift = "172,33,34,163,36,37,94,38,42,40,41,95,43,123,125,126,64,58,63,62,60,124,60,62".split(",");
var symbolchars_requireshift_without_poundcommafullstop = "172,33,34,163,37,94,38,42,40,41,95,43,123,125,126,64,58,63,62,60,124,60,62".split(",");
var symbolchars_requireshift_without_plusbracket = "172,33,34,163,36,37,94,38,42,95,123,125,126,64,58,63,62,60,124,60,62".split(",");
var symbolchars_withoutshift = "96,45,61,91,93,35,39,59,47,46,44,92".split(",");
var symbolchars_withoutshift_without_commafullstop = "96,45,61,91,93,35,39,59,47,92".split(",");
var azchars = "65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122".split(",");
var numchars = "49,50,51,52,53,54,55,56,57,48".split(",");

// ----------- FUNCTIONS FOR SUPRESSING DIFFERENT TYPES OF FIELDS -----------
// String.fromCharCode(172) = ¬
// String.fromCharCode(163) = £
function supress_for_currency(event, field) {
	var iecodes = symbolchars_withoutshift_without_commafullstop.concat(azchars);
	var ieshiftcodes = symbolchars_requireshift_without_poundcommafullstop;

	supress_characters(event, field, String.fromCharCode(172) + "!\"$%^&;*()_+`-=|\\[]{}:;@~#?/'><abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", iecodes, ieshiftcodes);
}

function supress_for_postcode(event, field) {
	var iecodes = symbolchars_withoutshift;
	var ieshiftcodes = symbolchars_requireshift;
	supress_characters(event, field, String.fromCharCode(172) + String.fromCharCode(163) + ",.!\"$%^&*()_+`-=|\\[]{}:;@~#?/'><", iecodes, ieshiftcodes);
}

function supress_for_alpha(event, field) {
	var iecodes = symbolchars_withoutshift;
	var ieshiftcodes = symbolchars_requireshift;
	supress_characters(event, field, String.fromCharCode(172) + String.fromCharCode(163) + ",.!\"$%^&*()_+=|\\[]{}:;@~#?/1234567890><", iecodes, ieshiftcodes);
}

function supress_for_phone(event, field) {
	var iecodes = azchars.concat(symbolchars_withoutshift);
	var ieshiftcodes = symbolchars_requireshift_without_plusbracket;
	supress_characters(event, field, String.fromCharCode(172) + String.fromCharCode(163) + "!\"$%^&;*_`-=|\\[]{}:;@~#?/'><abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", iecodes, ieshiftcodes);
}

