/*
General functions for Yotta
Copyright (c) 2003-2008 Ylab, www.ylab.nl
Author: Yohan Creemers
*/
var debugging = true;
var isIE = (navigator.appName.indexOf("Internet Explorer") != -1);
//todo
var languageCode = "nl";

//window.onerror = reportError;
function reportError(msg, url, line){
  var txt = (languageCode == "nl") ? "Er is een fout opgetreden. Meld dit a.u.b. aan de webmaster." : "An error occured. Please contact webmaster.";
  window.status = txt + " | " + line + ": " + msg;
  return true;
}
var loadFunctions = new Array();
function addLoadFunction(f) {loadFunctions[loadFunctions.length] = new Function(f);}
window.onload   = function(){for (var i=0; i<loadFunctions.length; i++){loadFunctions[i]();}}

//Convert id into object
function id2object(el){
  if(typeof(el)=="string"){el = document.getElementById(el);}
  return el;
}
/* quick getElement reference */
function $byId(){
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++){
		var element = id2object(arguments[i]);
		if(arguments.length == 1){
			return element;
		}
		elements.push(element);
	}
	return elements;
}

function addEvent(obj, handler, f){
  if(!obj){return;}
  //prepare method storage
  if(!obj.methods){obj.methods = new Array();}
  if(!obj.methods[handler]){obj.methods[handler] = new Array();}
  //the generic method wich will call the actual methods
  var parentMethod = function(){callMethodsForEvent(this, handler);}
  if(!obj[handler]){
    obj[handler] = parentMethod;
  }
  else if(obj[handler].toString() != parentMethod.toString()){
    //store existing method
    obj.methods[handler].push(obj[handler]);
    obj[handler] = parentMethod;
  }
  //store new method
  if(typeof(f) != "function"){f = new Function(f);}
  obj.methods[handler].push(f);
}

function callMethodsForEvent(obj, handler){
  if(!obj.methods || !obj.methods[handler]){return;}
  for (var i=0; i<obj.methods[handler].length; i++){
    if(obj.methods[handler][i].apply){
      obj.methods[handler][i].apply(obj);
    }
    else{
      //for ie5.0 jscript5.0
      eval(obj.methods[handler][i]);
    }
  }
}

//Set focus on first input element
function setFirstFieldFocus(container){
  if(container.tagName == "FORM"){
    var list = container.elements;
  }
  else{
    var list = container.getElementsByTagName("input");
  }
  for (var i=0; i< list.length; i++){
    try{
      if(list[i].type == "button"){continue;}
      list[i].focus();
      if(list[i].select){list[i].select();}
      break;
    }
    catch (exception){}
  }
}

//enable or disable elements and their children
function setEnabled(on){
  for (var i=1; i<arguments.length; i++){
    var obj = id2object(arguments[i]);
    if(!obj){return;}
    obj.disabled = !on;
    if(on){
    	removeClass(obj, 'disabled');
    }
    else{
    	addClass(obj, 'disabled');
    }
    if(obj.hasChildNodes()){
      for (var j=0; j<obj.childNodes.length; j++){
        if(obj.childNodes[j].nodeType == 1){setEnabled(on, obj.childNodes[j]);}
      }
    }
  }
}

function addClass(obj, strClass){
	if(!obj){return;}
	if(!obj.className){
		obj.className = strClass;
	}
	else if(!obj.className.match(strClass)){
		obj.className += ' ' + strClass;
	}
}

function removeClass(obj, strClass){
	if(!obj || !obj.className){return;}
	 obj.className = obj.className.replace(strClass, '');
}

//set a style property or fail gracefully
function setStyle(objectId, prop, value){
  var obj = document.getElementById(objectId);
  if((obj) && (obj.style[prop] != value)){
    obj.style[prop] = value;
  }
}

function getCookie(name){
  //Function to return the value of the cookie specified by "name".
  //returns: String object containing the cookie value, or null if the cookie does not exist.

  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen){
    var j = i + alen;
    if(document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if(i == 0) break;
  }
  return null;
  function getCookieVal(offset){
    //Internal function to return the decoded value of a cookie
    var endstr = document.cookie.indexOf (";", offset);

    if(endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
  }
}

function setCookie(name, value){
  //Function to create or update a cookie for calling document.
  document.cookie = name + "=" + escape (value);
}

function xmlGetValues(xmlDoc, tag){
  var values = xmlDoc.getElementsByTagName(tag)
  if(values){
    if(values.length == 1){
      return values[0].hasChildNodes() ? values[0].firstChild.data : null;
    }
    if(values.length > 1){
      var results = new Array();
      for (var i =0; i<values.length; i++){
        results[i] = values[i].hasChildNodes() ? values[i].firstChild.data : null;
      }
      return results;
    }
  }
  return null;
}

function $_GET(){
  var q = location.search;
	if(q.length > 1){
	  q = q.substring(1);
	}
	else{
	  return null;
	}
	var pairs = q.split("&");
	for(var i=0; i < pairs.length; i++) {
		this[pairs[i].split("=")[0]] = pairs[i].split("=")[1];
	}
	return this;
}

function scrollRowIntoView(pageref, param){
	var scrTop = getCookie(pageref);
	var get = $_GET();

	if(scrTop){
		//set scrollTop
		if(document & document.documentElement){
			document.documentElement.scrollTop = scrTop;
		}
		//delete scrollTop stored in Cookie
		setCookie(pageref, '');
	}
	if(get && get[param]){
		var row = id2object(get[param]);
		if(row){
			//hilite row
			row.className = 'listview-hilite';
			if(!scrTop){
				//no scrollTop stored in Cookie, scroll row into view
				row.scrollIntoView();
			}
		}
	}
}

//DEBUGGING
function devAlert(){
  if(!debugging){return;}
  var code = "Deze functie is nog niet geïmplementeerd.\n";
  for(var i=0; i < arguments.length; i++){
    code += arguments[i] + "\n";
  }
  alert(code);
}

function debugAlert(){
  if(!debugging){return;}
  var code = "";
  for(var i=0; i < arguments.length; i++){
    code += arguments[i] + "\n";
  }
  code += '\nKlik op Annuleren om verdere meldingen te onderdrukken.';
  debugging = confirm(code);
}

function debugConfirm(){
  if(!debugging){return;}
  var code = "";
  for(var i=0; i < arguments.length; i++){
    code += arguments[i] + "\n";
  }
  return confirm(code);
}

if(!Array.prototype.push){
  Array.prototype.push = function(){
    for(var i=0; i<arguments.length; i++){
      this[this.length] = arguments[i];
    }
    return this.length;
  }
}

if(!Array.prototype.in_array){
  Array.prototype.in_array = function(element){
  	var i;

    for (i=0; i<this.length; i++){
      if (this[i] == element){
        return true;
      }
    }
    return false;
  };
}
