/*
general functions for Litecad
Copyright (c) 2003 Ylab, Ontwerpbureau voor interactieve media, www.ylab.nl
Author: Yohan Creemers
version 1.0
*/
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]();}}
//window.onresize = function() {var m = (document.body.scrollWidth-760)/2; document.body.style.margin = "5px " + m + "px"; window.status=m;}

//Convert id into object
function id2object(el){
  if (typeof(el)=="string"){el = document.getElementById(el);}
  return el;
}
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++){obj.methods[handler][i].call(obj);}
}

//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 addBlurEvent(idInput, idBtn, isEmail){
  inp = document.getElementById(idInput);
  if(inp){
    inp.onblur = isEmail ?
	    function(){
	  	  var at = this.value.indexOf('@');
	  		var invalidEmail = ( (at < 2) || (this.value.indexOf('.',at+1) < 4) );
	    	setEnabled(!invalidEmail, idBtn);
	    }
			:
			function(){
	    	setEnabled(this.value, idBtn);
	    };
    inp.onblur();
  }
}
function browse(s){
  if(s){openWindow('http://'+s, 'organisation');}
}
function mailto(s){
  if(s){location.href = 'mailto:'+s;}
}

function showForms(){
  for (var i=0; i<document.forms.length; i++){
    document.forms[i].style.display='block';
  }
}
function setTimeZone(inp){
  if(inp){
    inp.value = (new Date()).getTimezoneOffset();
  }
}

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);
}
//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";
  }
  window.status = code;
  alert(code);
}
function debugConfirm(){
  if (!debugging){return;}
  var code = "";
  for(var i=0; i < arguments.length; i++) {
    code += arguments[i] + "\n";
  }
  window.status = code;
  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;
  }
}