var xPos, yPos;

// find out if ie runs in quirks mode
//
var docEl = (
	typeof document.compatMode != "undefined" && 
	document.compatMode        != "BackCompat"
	)? "documentElement" : "body";

// register event
// capture it for nc 4x (ok it's a dino)
//
function init_mousemove() {
    if(document.layers) document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove =	storeMousePos;
}

function storeMousePos(e) {
    xPos    =  e? e.pageX : window.event.x;
	yPos    =  e? e.pageY : window.event.y;

	
	if (document.all && !document.captureEvents) {
	    xPos    += document[docEl].scrollLeft;
	    yPos    += document[docEl].scrollTop;
    }
    
    if (document.layers) routeEvent(e);
}

function showGlossar (id, show) {
	if (!document.getElementById(id))
		return;
	
	document.getElementById(id).style.left = xPos+5+"px";
	document.getElementById(id).style.top = yPos+5+"px";
	
	if (show) {
		document.getElementById(id).style.display = "";
		
		if (document.all) {
			var sel = document.getElementsByTagName("select");
			for (var x = 0; x < sel.length; x++)
				sel[x].style.visibility="hidden";
		}
	} else {
		document.getElementById(id).style.display = "none";
		
		if (document.all) {
			var sel = document.getElementsByTagName("select");
			for (var x = 0; x < sel.length; x++)
				sel[x].style.visibility="";
		}
	}
}




setlinktext = true;
function setLinkText(text, iname) {
	if (!setlinktext)
		return;
	var obj = document.getElementsByName(iname)[0];
	obj.value = replaceSpecialSigns(text);
}


settitletext = true;
function setTitleText(text, iname) {
	if (!settitletext)
		return;
	var obj = document.getElementsByName(iname)[0];
	obj.value = text;
}

function replaceSpecialSigns(text) {
	text = text.replace(/ä/g, "ae");
	text = text.replace("ö", "oe");
	text = text.replace("ü", "ue");
	text = text.replace("Ä", "Ae");
	text = text.replace("Ö", "Oe");
	text = text.replace("Ü", "Ue");
	text = text.replace("ß", "ss");
	text = text.replace(/[^a-z0-9A-Z]/g, "-");
	return text;
}




/**
* go to top if in frame loaded :D
*/
var gotop = (top == self);
if (!gotop && self.name != "ucont") top.location.href = self.location.href; 

/**
* hover effect
*
* @param object cell
* @param string classname
*/
function mHoverCell(obj, classname) {
	obj.className = classname;
	obj.setAttribute("class", classname);
}

/**
* proof select field for disabled (IE FIX!!!)
*/
function proofSelect(obj) {
	var def = false;
	for (var x = 0; x < obj.length; x++) {
		if (obj.options[x].selected==true && obj.options[x].disabled==true) {
			def = true;
			alert("Auswahl nicht möglich!");
		}
	}
	
	if (def) {
		for (var x = 0; x < obj.length; x++) {
			if (obj.options[x].defaultSelected==true) {
				obj.options[x].selected=true;
			}
		}
	}
	
	return !def;
}

/**
* proofs the length of textarea fields
*
* @param object textarea
* @param number length
*/
function proofTxtLength(obj, length) {
	if (obj.value.length > length) {
		obj.value = obj.value.substr(0, length);
	}
	
	if (obj.value.length == length) {
		obj.style.backgroundColor = "#FFCCCC";
	} else {
		obj.style.backgroundColor = "";
	}
}

/**
* shows the rest of charachters in textarea fields
*
* @param object textarea
* @param object target
* @param number length
*/
function showTxtRest(obj, target, length) {
	proofTxtLength(obj, length);
	target.innerHTML = length - obj.value.length;
}

/**
* numcheck of inputfield
*
* @param object input
*/
function numcheck(obj) {
	var num = obj.value;
	var new_num = "";
	for (var x = 0; x < num.length; x++)
		new_num += (num[x].match(/[0-9]/) ? num[x] : "");
	
	if (new_num != obj.value)
		obj.value = new_num;
}

/**
* pricecheck of inputfield
*
* @param object input
*/
function pricecheck(obj) {
	var num = obj.value;
	// replace , with .
	num = num.replace(",", ".");
	// replace signs not in range
	num = num.replace(/[^0-9.]*/, "");
	// proof float part
	var pos;
	pos = num.indexOf(".");
	if (pos != -1) {
		var append = num.substr(pos, num.length-pos);
		// remove any signs not numeric in append an trim length
		append = append.replace(/[^0-9]*/, "");
		
		if (append.length > 2)
			append = append.substr(0,2);
		
		// rebuild num
		num = num.substr(0,pos)+"."+append;
	}
	obj.value = num;
}

/**
* generate random String (password eg.)
*/
function RandomKey(id, length) {
	// get object
	var obj = document.getElementById(id);
	
	//Passwort generieren
	var abc = new Array("a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"); // 23 Elemente
	
	var x = '', rand = 1, passwd = '';
	for(x=0;x<length;x++)
	{
		rand = 1;
		while(rand > 0.22)
		{
			rand = Math.random();
			//alert(rand);
		}
		rand = rand*100;
		(x < 2 || (x >= 4 && x <6)) ? passwd += abc[Math.round(rand)].toLowerCase() : passwd += abc[Math.round(rand)].toUpperCase();
		
	}
	
	obj.value = passwd;
	return false;
}

/**
* build username by email or first and second name
*/
function buildUsername(type, field1_id, field2_id, target_id) {
	switch (type) {
		case 'email':
			var src = document.getElementById(field1_id);
			var target = document.getElementById(target_id);
			if (src.value.length == 0)
				alert("Es wurde keine eMail-Adresse eingegeben!");
			target.value = src.value;
			break;
		case 'name':
			var src1 = document.getElementById(field1_id);
			var src2 = document.getElementById(field2_id);
			var target = document.getElementById(target_id);
			var name = "";
			if (src1.value.length > 0)
				name = src1.value.substr(0,1);
			name += src2.value;
			name = name.toLowerCase();
			
			if (name.length == 0)
				alert("Es wurde weder ein Vor- noch ein Nachname eingegeben!");
			
			target.value = name;	
			break;
	}
	
	return false;
}

/**
* open holder by id
*
* @param string id
*/
function openHolder(id) {
	var obj = document.getElementById(id);
	
	if (!obj)
		return false;
	
	obj.style.display = "";
	
	return false;
}

/**
* close holder by id
*
* @param string id
*/
function closeHolder(id) {
	var obj = document.getElementById(id);
	
	if (!obj)
		return false;
	
	obj.style.display = "none";
	
	return false;
}

/**
* open/close holder by id
*
* @param string id
*/
function opencloseHolder(id) {
	var obj = document.getElementById(id);
	
	if (!obj)
		return;
	
	if (obj.style.display == "")
		obj.style.display = "none";
	else
		obj.style.display = "";
	
	return;
}

/**
* check all checkboxes in holder
*
* @param object mainobject
* @param string id
*/
function checkAll(main, id) {
	var obj = document.getElementById(id);
	
	if (!obj)
		return false;
	
	var elements = obj.getElementsByTagName("input");
	
	// checked true or false?
	var status = false;
	if (main.checked) {
		status = true;
	}
	
	for (var x = 0; x < elements.length; x++) {
		if (elements[x].type == 'checkbox' && !elements[x].disabled)
			elements[x].checked = status;
	}
}

/**
* check all checkboxes in holder
*
* @param string id
*/
function checkAll2(id, status) {
	var obj = document.getElementById(id);
	
	if (!obj)
		return false;
	
	var elements = obj.getElementsByTagName("input");
	
	for (var x = 0; x < elements.length; x++) {
		if (elements[x].type == 'checkbox' && !elements[x].disabled)
			elements[x].checked = status;
	}
}



/**
* blending for message and error texts
*/
function blending () {
	// config
	var mclass = "message_head";
	var eclass = "error_head";
	var mcolor1 = "#009900";
	var mcolor2 = "#005100";
	var ecolor1 = "#ff3300";
	var ecolor2 = "#863530";
	var speed = 12;
	var count = 3; // number of blendings
	
	// find message boxes
	var divs = document.getElementsByTagName("div");
	for (var x = 0; x < divs.length; x++) {
		if (divs[x].className == mclass) {
			// set random id
			divs[x].setAttribute("id", "__blending__m__"+x);
			_runMessageBlending("__blending__m__"+x, mcolor1, mcolor2, speed, count, 0);
		}
	}
	// find error boxes
	var divs = document.getElementsByTagName("div");
	for (var x = 0; x < divs.length; x++) {
		if (divs[x].className == eclass) {
			// set random id
			divs[x].setAttribute("id", "__blending__e__"+x);
			_runMessageBlending("__blending__e__"+x, ecolor1, ecolor2, speed, count, 0);
		}
	}
}

function _runMessageBlending(id, color1, color2, speed, count, i) {
	var div = document.getElementById(id);
	if(div.style.backgroundColor=="") {
		div.style.backgroundColor = color1;
		window.setTimeout("_runMessageBlending('"+id+"', '"+color1+"', '"+color2+"', "+speed+", "+count+", "+i+")", 3*speed);
	} else {
		// ge hex color
		var bg = div.style.backgroundColor;
		
		// to dec array
		var _color1 = new Array();
		if (document.all)
			_color1 = [xd(bg.substr(1,2)), xd(bg.substr(3,2)), xd(bg.substr(5,2))];
		else {
			bg = bg.replace("rgb(", "");
			bg = bg.replace(")", "");
			bg = bg.replace(" ", "");
			_color1 = bg.split(",");
			//_color1 = [xd(bg.substr(1,2)), xd(bg.substr(3,2)), xd(bg.substr(5,2))];
		}
		_color1[0] = parseInt(_color1[0]);
		_color1[1] = parseInt(_color1[1]);
		_color1[2] = parseInt(_color1[2]);
		
		var _color2 = new Array();
		_color2 = [xd(color2.substr(1,2)), xd(color2.substr(3,2)), xd(color2.substr(5,2))];
		//document.getElementById("__test__").value = _color2;

		// diffenrences
		var red = (_color2[0] > _color1[0] ? _color2[0] - _color1[0] : _color1[0] - _color2[0]);
		var yel = (_color2[1] > _color1[1] ? _color2[1] - _color1[1] : _color1[1] - _color2[1]);
		var blu = (_color2[2] > _color1[2] ? _color2[2] - _color1[2] : _color1[2] - _color2[2]);
		
		// build steps
		var s_red = parseInt(red / speed < 3 ? (_color2[0] > _color1[0] ? _color2[0] - _color1[0] : _color1[0] - _color2[0]) : Math.floor(red / speed));
		var s_yel = parseInt(yel / speed < 3 ? (_color2[1] > _color1[1] ? _color2[1] - _color1[1] : _color1[1] - _color2[1]) : Math.floor(yel / speed));
		var s_blu = parseInt(blu / speed < 3 ? (_color2[2] > _color1[2] ? _color2[2] - _color1[2] : _color1[2] - _color2[2]) : Math.floor(blu / speed));
		// build new color
		//document.getElementById("__test__").value = _color1[1] + parseInt(s_yel);
		_color1[0] = (_color2[0] > _color1[0] ? _color1[0] + s_red : _color1[0] - s_red);
		_color1[1] = (_color2[1] > _color1[1] ? _color1[1] + s_yel : _color1[1] - s_yel);
		_color1[2] = (_color2[2] > _color1[2] ? _color1[2] + s_blu : _color1[2] - s_blu);
		
		// set new color
		bg = "#"+dx(_color1[0])+""+dx(_color1[1])+""+dx(_color1[2]);
		//document.getElementById("__test__").value = _color1[0]+" "+_color1[1]+" "+_color1[2];
		div.style.backgroundColor= bg;
		
		if (bg == color2) {
			div.style.backgroundColor = color1;
			++i;
			//window.setTimeout("_runMessageBlending('"+id+"', '"+color1+"', '"+color2+"', "+speed+")", 100*speed);
		}
		//else
		
		if (i<count)
			window.setTimeout("_runMessageBlending('"+id+"', '"+color1+"', '"+color2+"', "+speed+", "+count+", "+i+")", 10*speed);
	}
}

function dx (d) {
  max = Math.pow(16,8);
  
  if (d > max) {
    return false;
    return;
  }
  if (d < 0) {
    return false;
  }
  var z = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
    "A", "B", "C", "D", "E", "F");
  var x = "";
  var i = 1, v = d, r = 0;
  while (v > 15) {
    v = Math.floor(v / 16);
    i++;
  }
  v = d;
  for (j=i; j >= 1; j--) {
    x = x + z[Math.floor(v / Math.pow(16, j-1))];
    v = v - (Math.floor(v / Math.pow(16, j-1)) * Math.pow(16, j-1));
  }
  return zerofill(x, 2);
}

function xd (x) {
  max = 8;
  
  if (x.length > max) {
    return false;
  }
  var e = new Array();
  var z = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
    "A", "B", "C", "D", "E", "F");
  var d = 0, chk = 0;
  x = x.toUpperCase();

  for(i=0; i < x.length; i++) {
    for (j=0; j <= 16; j++) {
      if (x.substring(i, i+1) == z[j]) {
        chk = 1;
        e[i] = j;
      }
    }
    if (chk == 0) {
    return false;
    }
  }
  for (i=0; i < x.length; i++)
    d = d + e[i] * Math.pow(16, x.length-i-1)
  return d;
}

function zerofill (str, length) {
	while (str.length < length)
		str = "0"+str;
	return str;
}