/*
 * FIX for firefox-bug that throws security exceptions on some input elements that have autocompletion turned on
 */

function fixAutoComplete(){
  var inputs = document.getElementsByTagName("input");
  for(var i = 0; i < inputs.length; i++){
    var input = inputs[i];
    input.setAttribute('autocomplete', 'off');
  }
}

/*
 * Copies the onchange-event of all checkboxes/radiobuttons on a form to its onclick-event 
 */
function fixOnChange(){	
	var inputs = document.getElementsByTagName("input");
	for(var i = 0; i < inputs.length; i++){
		var input = inputs[i];
		// Is this input a checkbox or a radiobutton?
		if(input.type == 'checkbox' || input.type == 'radio'){
			input.onclick = input.onchange;
		}
	}
  fixAutoComplete();
}


/*
 * Links an input element to a checkbox/radiobutton:
 * - when the checkbox/radio is not checked, the input is disabled
 * - when the input becomes clicked, the checkbox becomes checked 
 */
function linkElements(checkID, inputID){
	var check = document.getElementById(checkID);
	var input = document.getElementById(inputID);
	appendOnChange(check, "linkState(this, document.getElementById('" + inputID + "'));");
	appendOnFocus(input, "if(!document.getElementById('" + checkID + "').checked){document.getElementById('" + checkID + "').checked = true; document.getElementById('" + checkID + "').onchange();}");

	addEvent( check, 'focus', function() { input.focus(); } );
	addEvent( check, 'click', function() { input.focus(); } );
	
	if(check.type == 'radio'){
		var group = document.getElementsByName(check.name);
		for(var i = 0; i < group.length; i++){
			if(group[i] != check){
				appendOnChange(group[i], "linkState(document.getElementById('" + checkID + "'), document.getElementById('" + inputID + "'));");
			}
		}
	}
   
	linkState(check, input);
}


/*
 * See linkElements, but for select options
 */
function linkSelectElements(optionID, inputID){
	var option = document.getElementById(optionID);
	var input = document.getElementById(inputID);
	appendOnChange(option.parentNode, "linkSelectState(document.getElementById('" + optionID + "'), document.getElementById('" + inputID + "'));");
	appendOnFocus(input, "if(!document.getElementById('" + checkID + "').selected){document.getElementById('" + checkID + "').selected = true; document.getElementById('" + checkID + "').onchange();}");
	
	linkSelectState(option, input);
}


/*
 * Appends an action to the onChange event of a control
 */
function appendOnChange(input, action){
	if((input.changeActionsArray instanceof Array) == false){
		input.changeActionsArray = new Array();
	}

	input.changeActionsArray[input.changeActionsArray.length] = action;
	input.onchange = function(){
		for(var i = 0; i < this.changeActionsArray.length; i++){
			eval(this.changeActionsArray[i]);
		}
	}
}

/*
 * Appends an action to the onClick event of a control
 */
function appendOnClick(input, action){
	if((input.clickActionsArray instanceof Array) == false){
		input.clickActionsArray = new Array();
	}

	input.clickActionsArray[input.clickActionsArray.length] = action;
	input.onclick = function(){
		for(var i = 0; i < this.clickActionsArray.length; i++){
			eval(this.clickActionsArray[i]);
		}
	}
}


/*
 * Appends an action to the onClick event of a control
 */
function appendOnFocus(input, action){
	if((input.focusActionsArray instanceof Array) == false){
		input.focusActionsArray = new Array();
	}

	input.focusActionsArray[input.focusActionsArray.length] = action;
	input.onfocus = function(){
		for(var i = 0; i < this.focusActionsArray.length; i++){
			eval(this.focusActionsArray[i]);
		}
	}
}


/*
 * Sets the 'disabled' state of a control to the opposite of the 'checked' state of a checkbox.
 */
function linkState(check, input){
	try{
	  if(check.checked){
      input.disabled = false;
		  input.select();  
	  }else{
      input.enabled = true;
    }
  }catch(e){
  }
}

/*
 * Links the 'disabled' state of a control to the opposite of the 'selected' state of a select option
 */
function linkSelectState(option, input){
	try{
	  if(option.selected){
      input.disabled = false;
		  input.select();
	  }else{
      input.enabled = true;
    }
  }catch(e){
  }
}

/*
 * Check date format
 */
function validDate(val) {
  // valide datum opmaak is:
  // DD-MM-YYYY
  var valid = true;
  var month = day = year = 0;

  var elems = val.split('-');
  if (elems.length != 3)
    elems = val.split('.'); 
  if (elems.length != 3)
    elems = val.split('/'); 

  if (elems.length == 3) {
    if (val.length != 10)
      return false;
    //DD-MM-YYYY
    day = parseInt(elems[0],10);
    month=parseInt(elems[1],10);
    year =parseInt(elems[2],10);
  } else
    return false;

  valid = !isNaN(month) && (month > 0) && (month < 13) &&
          !isNaN(day) && (day > 0) && (day < 32) &&
          !isNaN(year);

  return valid;
}

/*
 * Date format conversion function
 */
String.prototype.convertDate = function () {
    d = new Date();
    separators = new Array();
    separators[0] = "-";
    separators[1] = "/";

    for ( sepKey in separators ) {
      var sep = separators[sepKey];
      splittedDate = this.split(sep);
      
      if ( splittedDate.length==2 ) {
        splittedDate[2] = d.getFullYear().toString();
      }
      if ( splittedDate.length>1 && splittedDate.length <= 3 ) {
          // Put a 0 in front of a single number: 06-5 --> 06-05
          for ( var i=0; i<splittedDate.length; i++ ) {
            if ( splittedDate[i].length==1 )
              splittedDate[i] = "0"+splittedDate[i];
          }
          
          // DD-MM-YY, DD-MM-YYYY...
          if ( !splittedDate[2] || splittedDate[2].length>=3 ) return splittedDate.join("-");
          return splittedDate[0]+"-"+splittedDate[1]+"-20"+splittedDate[2];
      }
    }
    // No seperators found...
    if ( this.length >= 6 ) {
        // Perhaps DDMMYY, DDMMYYYY
        readableDate = this.substr(0,2)+"-"+this.substr(2,2)+"-"+this.substr(4,this.length);
        return readableDate.convertDate(); // convert YY -> YYYY
    }
    return this.replace("/","-");
}

function cancelBubble(e) {
  if (!e) {
    var e = window.event
  }
  e.cancelBubble = true
  if (e.stopPropagation) {
    e.stopPropagation()

  }
}

function addEvent(obj, evType, fn) {
  if (obj.addEventListener) {
    obj.addEventListener(evType, fn, false)
    return true
  } else if (obj.attachEvent) {
    obj.attachEvent("on"+evType, fn)
    return true
  } else {
    return false
  }
}