function moveOption(moveFrom, moveTo, toDo, errorMessage) {
  var selectFrom = eval('document.compose.'+moveFrom);
  var selectTo = eval('document.compose.'+moveTo);
  var selectedIndex = selectFrom.options.selectedIndex;
  var container;

  if (toDo=='add') {
    container=eval('document.compose.' + toDo + capitalize(moveTo));
  }

  if (toDo=='remove') {
    container=eval('document.compose.' + toDo + capitalize(moveFrom));
  }

  if (selectedIndex == -1) {
    alert(errorMessage);
  } else {
    for (i=0; i<selectFrom.options.length; i ++) {
      if(selectFrom.options[i].selected) {
        var name = selectFrom.options[i].text;
        var ID = selectFrom.options[i].value;
        
        selectFrom.options[i] = null;
        selectTo.options[selectTo.options.length]= new Option(name,ID);
        i = i-1;
        
        if(toDo == 'add' || toDo == 'remove') {
          container.value = container.value + name + '^' + ID + ':';
        }
        
        sortSelect(selectTo);
      }
    }
  }
}

// sort function - ascending (case-insensitive)
function sortFuncAsc(record1, record2) {
    var value1 = record1.optText.toLowerCase();
    var value2 = record2.optText.toLowerCase();
    if (value1 > value2) return(1);
    if (value1 < value2) return(-1);
    return(0);
}

// sort function - descending (case-insensitive)
function sortFuncDesc(record1, record2) {
    var value1 = record1.optText.toLowerCase();
    var value2 = record2.optText.toLowerCase();
    if (value1 > value2) return(-1);
    if (value1 < value2) return(1);
    return(0);
}

function sortSelect(selectToSort, ascendingOrder) {
    if (arguments.length == 1) ascendingOrder = true;    // default to ascending sort

    // copy options into an array
    var myOptions = [];
    for (var loop=0; loop<selectToSort.options.length; loop++) {
        myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
    }

    // sort array
    if (ascendingOrder) {
        myOptions.sort(sortFuncAsc);
    } else {
        myOptions.sort(sortFuncDesc);
    }

    // copy sorted options from array back to select box
    selectToSort.options.length = 0;
    for (var loop=0; loop<myOptions.length; loop++) {
        var optObj = document.createElement('option');
        optObj.text = myOptions[loop].optText;
        optObj.value = myOptions[loop].optValue;
        selectToSort.options.add(optObj);
    }
}

function capitalize(word) {
    // the first character
    ch = word.substring(0,1);

    // the rest of the value
    rest = word.substring(1);

    // convert first character to uppercase
    up = ch.toUpperCase();

    // concatenate the uppercase with the rest
    word = up + rest;
    
    return word;
}

function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
} 

function attach(nextAction) {		
	attachmentAction 	= document.getElementsByName("attachmentAction")[0];
	action 				= document.getElementsByName("action")[0];
	
    attachmentAction.value = 'add';
    action.value = nextAction;
    
    if (attachment_check()) {
		avoidDown4Upload();
        uniqSubmit();
    }
}

function attachDelete(nextAction) {
	attachmentAction 	= document.getElementsByName("attachmentAction")[0];
	action 				= document.getElementsByName("action")[0];
	
    attachmentAction.value = 'delete';
    action.value = nextAction;
    
    uniqSubmit();
}

function avoidDown4Upload() {    
	document.onmousedown = document.onkeydown = function () {
		alert('Bitte warte, bis der Upload abgeschlossen ist!');
	}
}

function attachment_check() {
	attachmentAction 	= document.getElementsByName("attachmentAction")[0];
	file_upload 		= document.getElementsByName("file_upload")[0];
		
    if (attachmentAction.value) {
        if (attachmentAction.value == 'add') {
            if (file_upload.value == '') {
                alert('Bitte füge eine Datei ein!');
                file_upload.focus();
                return false;
            } else {
                return true;
            }
        }
    }
}

function uniqSubmit() {
	document.getElementsByName("compose")[0].submit();
}

document.getElementsByClassName = function (needle) {
	var my_array = document.getElementsByTagName("*");
	var retvalue = new Array();
	var i;
	var j;
	
	for (i = 0, j = 0; i < my_array.length; i++) {
		var c = " " + my_array[i].className + " ";
		
		if (c.indexOf(" " + needle + " ") != -1) {
		  retvalue[j++] = my_array[i];
		}
	}
	
	return retvalue;
}

addEvent(window, 'load', function() {
	var counts = document.getElementsByClassName('maxLength');
	var i, count, matches, countHolder;
	
	for (i = 0; i < counts.length; i++) {
		count = counts[i];			
				
		matches = count.className.match(/max_([0-9]+)/);
		count.maxVal = RegExp.$1;
		count.holder = document.getElementById('maxLengthCount');		
		
		if (count.holder) {						
			var difference = count.maxVal - count.value.length;	
					
			setElementValue(count.holder, difference);
			
			count.onkeyup = function() {
				if (this.value.length > this.maxVal) {
					this.value = this.value.substring(0, this.maxVal);
				}
	
				var difference = this.maxVal - this.value.length;
				
				setElementValue(this.holder, difference);		
			}
		}
	}	
});

function setElementValue(element, value) {
    element.innerHTML = value;
}

// ------------- Change there values to match with css colors

addEvent(window, 'load', function() {
	var input, textarea;
	var inputs = document.getElementsByTagName('input');
	for (var i = 0; (input = inputs[i]); i++) {
		if(	input.type != 'submit' && input.type != 'button' && input.type != 'radio' &&
			input.type != 'file' && input.type != 'checkbox') {
			addEvent(input, 'focus', oninputfocus);
			addEvent(input, 'blur', oninputblur);
		}
	}
	
	var textareas = document.getElementsByTagName('textarea');
	for (var i = 0; (textarea = textareas[i]); i++) {
		addEvent(textarea, 'focus', oninputfocus);
		addEvent(textarea, 'blur', oninputblur);
	}
});

function oninputfocus(e) {
	/* Cookie-cutter code to find the source of the event */
	if (typeof e == 'undefined') {
		var e = window.event;
	}
	var source;
	if (typeof e.target != 'undefined') {
		source = e.target;
	} else if (typeof e.srcElement != 'undefined') {
		source = e.srcElement;
	} else {
		return;
	}
	
	/* End cookie-cutter code */
	source.style.border='1px solid #fd0';
}

function oninputblur(e) {
	/* Cookie-cutter code to find the source of the event */
	if (typeof e == 'undefined') {
		var e = window.event;
	}
	var source;
	if (typeof e.target != 'undefined') {
		source = e.target;
	} else if (typeof e.srcElement != 'undefined') {
		source = e.srcElement;
	} else {
		return;
	}
	
	/* End cookie-cutter code */
	source.style.border='1px solid transparent';
	source.style.borderColor='#cca #775 #775 #cca';
}