function teamHighscore(teamId, season) {
	var asynchronous = new Asynchronous();
	asynchronous.complete = function( status, statusText, responseText, responseXML) {
	    document.getElementById('highscore-container').innerHTML = responseText;
	}
	asynchronous.get('team-highscore.do?teamId=' + teamId + '&season=' + season);
}

function displayErrors( errorMessage ){
	if ( errorMessage == null || errorMessage == '' ){
		document.getElementById('errors').innerHTML = '';
		hideById('errors-display');
	}else{
		document.getElementById('errors').innerHTML = errorMessage;
		showById('errors-display');
	}
}

function displayMessageYesNo( headline, content, yesAction, noAction ){
	if ( content == null || content == '' ){
		document.getElementById('message-yes-no--content').innerHTML = '';
		hideById('message-yes-no');
	}else{
		if ( headline == null || headline == '' ){
			document.getElementById('message-yes-no--headline').style.visable = false;
			document.getElementById('message-yes-no--headline').style.backgroundColor = 'transparent';
			document.getElementById('message-yes-no--headline').style.visable = false;
		}else{
			document.getElementById('message-yes-no--headline').innerHTML = headline;
		}
		document.getElementById('message-yes-no--content').innerHTML = content;
		document.getElementById('message-yes-no--yes').onclick = yesAction;
		document.getElementById('message-yes-no--no').onclick = noAction;
		showById('message-yes-no');
	}
}

function loadLocation( location ){
	self.location.href=location;
}

function showById( id ){
	document.getElementById( id ).style.display = 'inline';
}

function showSelf( self ){
	self.style.display = 'inline';
}
	
function hideById( id ){
	document.getElementById( id ).style.display = 'none';
}

function hideSelf( self ){
	self.style.display = 'none';
}


// TODO: find out why this only works when put in jsp 
// TODO: localize grouping char 
var GROUPING_CHAR = ".";
var JST_NUMBER_CODES = "0123456789abcdefghi`";

/* We ignore the following characters on mask:
45 - insert, 46 - del (not on opera or konqueror), 35 - end, 36 - home, 33 - pgup, 
34 - pgdown, 37 - left, 39 - right, 38 - up, 40 - down,
127 - del on konqueror, 4098 shift + tab on konqueror */
var JST_IGNORED_KEY_CODES = [45, 35, 36, 33, 34, 37, 39, 38, 40, 127, 4098];
if (navigator.userAgent.toLowerCase().indexOf("opera") < 0 && navigator.userAgent.toLowerCase().indexOf("khtml") < 0) {
    JST_IGNORED_KEY_CODES[JST_IGNORED_KEY_CODES.length] = 46;
}
//All other with keyCode < 32 are also ignored
for (var i = 0; i < 32; i++) {
    JST_IGNORED_KEY_CODES[JST_IGNORED_KEY_CODES.length] = i;
}


function onlySpecified(string, possible) {
    string = String(string);
    possible = String(possible);
    for (var i = 0; i < string.length; i++) {
        if (possible.indexOf(string.charAt(i)) == -1) {
            return false;
        }
    }
    return true;
}

function preventDefault(event) {
    if (event == null && window.event) {
        event = window.event;
    }
    if (event != null) {
        if (event.preventDefault != null) {
            event.preventDefault();
        } else if (event.returnValue !== null) {
            event.returnValue = false;
        }
    }
    return false;
}

function preventEnter(evt) {
  	var evt = (evt) ? evt : ((window.event) ? window.event : null);
  	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  	if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
  	else {return true;}
} 

function indexOf(object, array, startingAt) {
    if ((object == null) || !(array instanceof Array)) {
        return -1;
    }
    if (startingAt == null) {
        startingAt = 0;
    }
    for (var i = startingAt; i < array.length; i++) {
        if (array[i] == object) {
            return i;
        }
    }
    return -1;
}

function typedCode(event) {
    var code = 0;
    if (event == null && window.event) {
        event = window.event;
    }
    if (event != null) {
        if (event.which) {
            code = event.which;
        } else if (event.keyCode) {
            code = event.keyCode;
        }
    }
    return code;
}

function replaceAll(string, find, replace) {
    return String(string).split(find).join(replace);
}

function useGroupingDown(event, element) {
	if (window.event) {
    event = window.event;
  }
   
  var code = typedCode(event);
  if (indexOf(code, JST_IGNORED_KEY_CODES) >= 0) {
  	return true;
  }
  var typedChar = String.fromCharCode(code);
    
  if (!onlySpecified(typedChar, JST_NUMBER_CODES )) {
  	return preventDefault(event);  	  	
  }
   
  return true;	
}

function applyGrouping(element) {
	element.value = applyGroupingVar( element.value );
	return true;
}

function applyGroupingVar(content) {
	content = replaceAll(content, GROUPING_CHAR, "");
	var result = "";
	for (var i = content.length-1; i >= 0; i--) {
		if ((content.length+2 - i) % 3 == 0) {
			result = GROUPING_CHAR + result;
		}
	  result = content.charAt(i) + result;
	}
	result = result.substr(0,result.length-1);
	return result;
}

function stripGrouping(element) {
  element.value = stripGroupingVar(element.value);
  return true;
}

function stripGroupingVar(content) {
	content = content.replace(/\D/,'');
	return replaceAll(content, GROUPING_CHAR, ""); 
}
