/**
On a current project, where accessibility is essential, I have integrated a stylesheet switcher on two pages where a mouseover affect on an image montage displays an introduction to each page in a separate box below the montage. The montage is structurally defined as a definition list, since it is a visual representation of links to pages on the site, with a brief description of each page. The stylesheet switcher allows users to switch the view to an actual list representation, if, for example, they are using only the keyboard. For CSS-enabled browsers without Javascript, the switcher loads a fresh page with the list display, and for non-display browsers the montage is just represented as a list from the outset.

While, I hope this will be effective under most conditions, I found a frustrating problem with supplementing the onclick event with an onkeypress event for keyboard users in Mozilla and co. When tabbing through the links on the page, as soon as the user tabs off the switcher (containing the event handler) the key press event is triggered. Well, of course, this is the correct behaviour, but Inte$net Exploder actually only triggers the event when the Return key is pressed, which is misleading to the developer, and incorrect interpretation.

The solution was to integrate a filter function that is called by the onkeypress event and this in turn only calls the style switcher function if the key pressed was Return

Here is the code:


function checkKeyPressed(evt, func, params)
{
  evt = (evt) ? evt : (window.event) ? event : null;
  if (evt)
  {
    var charCode = (evt.charCode) ? evt.charCode :
                   ((evt.keyCode) ? evt.keyCode :
                   ((evt.which) ? evt.which : 0));
    if (charCode == 13) func(params);
  }    
}

Where 13 is the ASCII value for the Return key. As well as passing the event as argument, I passed the function to be called and its parameters, to make the function generic. The scenario is just as relevant if using the event handlers to open a new window from a link (although this is strongly discouraged). The resulting anchor is as follows:


<a href="#" onclick="setStyleSheet('sheet'); return false;"
onkeypress="checkKeyPressed(event, setStyleSheet, 'sheet');"
>Switch to List View</a>

It is important that a false value is not returned in the key press event since this would prevent the user from tabbing beyond the style switcher link. The DHTML cookbook suggests the key detection in the filter should work in Netscape and Exploder back to v.4. It resolved the issue for this project.

*/
function checkKeyPressed(evt, func, params)
{
  evt = (evt) ? evt : (window.event) ? event : null;
  if (evt)
  {
    var charCode = (evt.charCode) ? evt.charCode :
                   ((evt.keyCode) ? evt.keyCode :
                   ((evt.which) ? evt.which : 0));
    if (charCode == 13) func(params);
  }    
}

////////

function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

///////////////////////////////


function isArray(obj) {
   if(!obj)	
	  return false;
	  
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

/**
* email kontrol eder. do�ru bir giri�se true, yanl��sa false d�ner
*/

function isValidEmail(emailAdayi){
	
	var reg_email = /^[a-zA-Z0-9]+[_a-zA-Z0-9-]*(\.[_a-z0-9-]+)*@.+\..+$/;
	
	if(emailAdayi.value == ''){
		return false;
	}
	
	if(!reg_email.test(emailAdayi)){
		return false;
	}
	
	if(emailAdayi.charAt(emailAdayi.length-1)=='.'){
		return false;
	}
	
	return true;
}//

/**
* bir stringin bir string par�ac���n� bar�nd�r�p bar�nd�rmad���n� kontrol eder
* @PARAMS;
* needle :  aranacak olan string
* haystack : i�erisinde arama yap�lacak olan string
* @RETURN;
* varsa boolean true, yoksa false d�ner
*/
function str_contains(needle, haystack){
	if(haystack.indexOf(needle) == -1) return false;
	else return true;
}//end function str_contains()

/** bir stringin, bir dizi i�erisindeki b�t�n string par�alar�n� bar�nd�r�p bar�nd�rmad���n� kontrol eder
* @PARAMS;
* needleArray : i�erisindeki string par�alar� aranacak olan array
* haystack : i�erisinde aram yap�lacak olan string
*
* @RETURN;
*  needleArray bir dizi de�ilse 1 d�ner.
*  needleArray'in eleman say�s� 0 ise 2 d�ner
*  needleArray i�indeki par�alardan biri yoksa boolean false, hepsi varsa true d�ner
*/

function str_containsAll(needleArray, haystack){
	if(!isArray(needleArray)) return 1;
	
	var arrLen = needleArray.length;
	if(arrLen == 0) return 2;
	
	var i=0;
	
	for(i=0; i<arrLen; i++){
		if(haystack.indexOf(needleArray[i]) == -1) return false;
	}//end for
	
	return true;
}//end function str_containsAll()

/**
* bir dizinin i�indeki herhangi bir eleman�n bir stringde ge�ip ge�medi�ini kontrol eder
* @PARAMS;
* needleArray : i�erisindeki string par�alar� aranacak olan array
* haystack : i�erisinde aram yap�lacak olan string
*
* @RETURN;
*  needleArray bir dizi de�ilse 1 d�ner.
*  needleArray'in eleman say�s� 0 ise 2 d�ner
*  needleArray i�indeki par�alardan herhangi biri varsa boolean false, hi�biri yoksa true d�ner
*/

function str_containsNone(needleArray, haystack){
	if(!isArray(needleArray)) return 1;
	
	var arrLen = needleArray.length;
	if(arrLen == 0) return 2;
	
	var i=0;
	
	for(i=0; i<arrLen; i++){
		if(haystack.indexOf(needleArray[i]) != -1) return false;
	}//end for
	
	return true;
}//end function str_containsNone()


/**
*
* Javascript trim, ltrim, rtrim
* http://www.webtoolkit.info/
*
*
**/

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function resetForm(form_id){
	var form = document.getElementById(form_id);
	var len = form.length;
	var i=0;
	
	for(i=0; i<len; i++){
		if(form[i].type != 'submit'){
			form[i].value = '';
		}
	}
}

/**
form elemanlar�n� disabled yapar
*/
function disableForm(form_id){
	var form = document.getElementById(form_id);
	var i=0;
	
	for(i = 0; i<form.length; i++){
		
		form[i].disabled = true;
	}
}//end function disableForm()

/**
form elemanlar�n� enabled yapar
*/
function enableForm(form_id){
	var form = document.getElementById(form_id);
	var i=0;
	
	for(i = 0; i<form.length; i++){
		
		form[i].disabled = false;
	}
}//end function enableForm()


function show_div (div_id) {
    document.getElementById(formid).style.display="block";
}


/** form do�rulama fonksiyonlar�  **/
function box_isEmpty(box_id,message){
	var box = document.getElementById(box_id);
	if(trim(box.value) == ''){
		alert(message);
		box.focus();
		return true;
	}
	
	return false;
}//end function box_isEmpty();

/** form do�rulama fonksiyonlar�  **/
function box_isEmpty2(box_id,message){
	var box = document.getElementById(box_id);
	if(trim(box.value) == ''){
		box.focus();
		return true;
	}
	
	return false;
}//end function box_isEmpty();

function box_isNumeric(box_id,message_ifempty,message_ifnotnumeric){
	var box = document.getElementById(box_id);
	
	if(box.value == ''){
		alert(message_ifempty);
		box.focus();
		return false;
	}
	
	if(box.value.replace(/0/g,'') != parseInt(box.value.replace(/0/g,''))){
		alert(message_ifnotnumeric);
		box.focus();
		return false;
	}
	
	return true;
}//end function box_isNumeric();

function box_isNumericIdentity(box_id,message, defaultLength, defaultLength_message){
	var box = document.getElementById(box_id);
	
	if(box.value.replace(/0/g,'') != parseInt(box.value.replace(/0/g,''))){
		alert(message);
		box.focus();
		return false;
	}
	
	if(defaultLength){
		if(trim(box.value).length != defaultLength){
			alert(defaultLength_message);
			box.focus();
			return false;
		}
	}
	
	return true;
}//end function box_isNumericIdentity()


function box_isValidPassword(passBox_id, passRepeatBox_id, message_1, message_2){
	var box1 = document.getElementById(passBox_id);
	
	if(trim(box1.value) == ''){
		alert(message_1);
		box1.focus();
		return false;
	}
	
	if(box1.value != document.getElementById(passRepeatBox_id).value){
		alert(message_2);
		document.getElementById(passRepeatBox_id).focus();
		return false;
	}
	
	return true;
}//end function box_isValidPassword()


function box_isEmail(box_id, message){
	var box = document.getElementById(box_id);
	
	if(!isValidEmail(box.value)){
        alert(message);
		box.focus();
		return false;
	}
	
	return true;
}//end function box_isEmail();
function box_isEmail2(box_id, message){
	var box = document.getElementById(box_id);
	
	if(!isValidEmail(box.value)){
		box.focus();
		return false;
	}
	
	return true;
}//end function box_isEmail();


var charset = "ABC�DEFG�HI�JKLMNO�PQRS�TU�VWXYZabc�defg�hi�jklmno�pqrs�tu�vwxyz ,.";
var ad_charset = "ABC�DEFG�HI�JKLMNO�PQRS�TU�VWXYZabc�defg�hi�jklmno�pqrs�tu�vwxyz ";

var numcharset = "0123456789";
var tel_charset = "0123456789 ";

function kontrol(target) {
	oldvalue = target.value;
	StrLen = oldvalue.length;
	var tempy = '';
	
	for (a=0; a < StrLen; a++) {
	
	if (charset.indexOf(oldvalue.charAt(a)) != -1) tempy += oldvalue.charAt(a);
	}
	if (oldvalue != tempy) target.value = tempy;
}


function num_kontrol(target) {
	oldvalue = target.value;
	StrLen = oldvalue.length;
	var tempy = '';
	
	for (a=0; a < StrLen; a++) {
	if (numcharset.indexOf(oldvalue.charAt(a)) != -1) tempy += oldvalue.charAt(a);
	}
	if (oldvalue != tempy) target.value = tempy;
}

function tel_kontrol(target) {
	oldvalue = target.value;
	StrLen = oldvalue.length;
	var tempy = '';
	
	for (a=0; a < StrLen; a++) {
	if (tel_charset.indexOf(oldvalue.charAt(a)) != -1) tempy += oldvalue.charAt(a);
	}
	if (oldvalue != tempy) target.value = tempy;
}

function ad_kontrol(target){
	var oldvalue = target.value;
	var StrLen = oldvalue.length;
	var tempy = '';
	
	for (a=0; a < StrLen; a++) {
	
	if (ad_charset.indexOf(oldvalue.charAt(a)) != -1) tempy += oldvalue.charAt(a);
	}
	if (oldvalue != tempy) target.value = tempy;
}//end function ad_kontrol()

function goLink (nereye) {
	document.location.href=nereye;
}

function BuyukHarf(e) {
  var nesne = e.target ? e.target : e.srcElement;
    var basilantus = e.charCode == undefined ? e.keyCode : e.charCode;
    var str = String.fromCharCode(basilantus);
    if ((basilantus < 97 || basilantus > 122) && !isTRChar(basilantus))
        return true;
    if (basilantus == 105)
        str = '\u0130';
    if (nesne.createTextRange) {
        e.keyCode = str.toUpperCase().charCodeAt(0);
        return true;
    }
    else {
        var startpos = nesne.selectionStart;
        var endpos = nesne.selectionEnd;
        nesne.value = nesne.value.substr(0, startpos) + str.toUpperCase() + nesne.value.substr(endpos);
        nesne.setSelectionRange(startpos + 1, startpos + 1);
        return false;
    }
}

function isTRChar(key) {
    var trchar = [231, 246, 252, 287, 305, 351];
    for (var i = 0; i < trchar.length; i++) {
        if (trchar[i] == key)
            return true;
    }
    return false;
}

function BuyukHarfBlur(e, clear) {
    var nesne = e.target ? e.target : e.srcElement;
    var val = nesne.value;
    val = val.replace(/i/g, "\u0130").replace(/^\s+|\s+$/g, "");
    if (clear) val = val.replace(/\s{2,}/g, " ");
    nesne.value = val.toUpperCase();
}

