/** DHML API ---- */
var DHTML = (document.getElementById || document.all || document.layers);


function getObj(name){
  var obj;
  var style;
  if (document.getElementById)  {
  	this.obj = document.getElementById(name);
  	if(!this.obj) return;
	this.style = document.getElementById(name).style;
  }
  else if (document.all)  {
	this.obj = document.all[name];
	if(!this.obj) return;
	this.style = document.all[name].style;
  }  else if (document.layers)  {
	this.obj = getObjNN4(document,name);
	this.style = this.obj;
  }
}

function getSimpleObj(name){
  var obj;
  if (document.getElementById)  {
  	this.obj = document.getElementById(name);
  }  else if (document.all)  {
	this.obj = document.all[name];
  }  else if (document.layers)  {
	this.obj = getObjNN4(document,name);
  }
}


function getOpenerObj (name) {

	var obj;
	var style;
	if(document.getElementById) {
		this.obj = opener.document.getElementById(name);
		this.style = opener.document.getElementById(name).style;
	} else if (document.all) {
		this.obj = opener.document.all[name];
		this.style = opener.document.all[name].style;
	} else if (document.layers) {
		this.obj = getObjNN4(opener.document,name);
		this.style = this.obj;
	}
}

function getObjNN4(obj,name){
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}


function invi(elem,flag)
{
	if (!DHTML) return;
	var x = new getObj(elem);
	x.style.visibility = (flag) ? 'hidden' : 'visible'
}

function changeCol(elem,col) {
	if (!DHTML) return;
	var x = new getObj(elem);
	x.style.color = col;
}

/** DHML API END ---- */


/** farbe bei editierten feldern ändern. */
function setRed(layername){
	if(!DHTML) return;
	invi("afEditBottom",false);	
	changeCol(layername,'#f00');
}

function editWin(action,width,height) {
	var win = winOpen(action,width,height);
	win.focus();
}

function winOpen(url, breite, hoehe) {
      // Errechnet Koordinaten, um das Popup zentriert zu platzieren
      links = (screen.width/2)-(breite/2);
      oben = (screen.height/2)-(hoehe/2);
     return  window.open(url,'popup',"height="+hoehe+",width="+breite+",status = yes,toolbar = no,menubar = no,location = no,resizable = yes,titlebar = no,scrollbars = yes,fullscreen = no,top ="+oben+",left ="+links);
}

function resizeMe(width,height) {
	oldW = document.body.clientWidth;
	oldH = document.body.clientHeight;
	if(oldW < width && oldH < height) {
		this.window.self.resizeTo(width,height);
	} 
}

/** wird vom form2mail-popup gebraucht. */
function reloadParent() {
	opener.blindForm.submit();
}

/** wird vom cosePopUp gebraucht. */
function closePopUp() {
	window.close();
}   

function UriParams() {
	var uri = document.location.search.substr(1,document.location.search.length);
	var params = uri.split('&');
	var myParams = new Array();
	var i=0;
	var hash;
	var str1,str2;
	for(i=0;i<params.length;i++) {
		hash = params[i].split('=');
		str1 = hash[0];
		str2 = hash[1];
		myParams[str1] = str2;
	}
	return myParams;
}


/** COLORSELECTOR ROUTINES */
function _CloseOnEsc() {
  if (event.keyCode == 27) { window.close(); return; }
}

function Init() {                                                       // run on page load

  var uriParams = UriParams();
  var formID = uriParams['formID'];
  var openerObj = new getOpenerObj(formID);
  if(openerObj.obj != undefined) {
  	var color = openerObj.obj.value;
  	color = color.substr(1,color.length);
  	color = ValidateColor(color) || '000000';
  	View(color);                                                          // set default color
  }
}

function View(color) {                  // preview color
	var ColorPreview = new getObj("ColorPreview");
	if(ColorPreview.style != undefined) {
		ColorPreview.style.backgroundColor = '#' + color;
		var cHex = new getObj("ColorHex");
		cHex.obj.value = '#' + color;	
	} else {
		
		ColorPreview = new getObjNN4(document,'ColorHex');
		ColorPreview.value='#' + color;
	
	}
}

function Set(string) {                   // select color
  var color = ValidateColor(string);
  if (color == null) { alert("Invalid color code: " + string); }        // invalid color
  else {                                                                // valid color
    View(color);                          // show selected color
    var uriParams = UriParams();
  	var formID = uriParams['formID'];
  	var openerObj = new getOpenerObj(formID);
  	var colorObj = new getOpenerObj('col_'+formID);
  	if(openerObj.obj != undefined) {
  		
  		openerObj.obj.value = '#'+color;
  		colorObj.style.backgroundColor = '#'+color;
  		window.close();
  	} else {
  		openerObj.value='#'+color;
  		window.close();
  	}

  }
}


function ValidateColor(string) {                // return valid color code
  string = string || '';
  string = string + "";
  string = string.toUpperCase();
  var chars = '0123456789ABCDEF';
  var out   = '';

  for (var i=0; i<string.length; i++) {             // remove invalid color chars
    var schar = string.charAt(i);
    if (chars.indexOf(schar) != -1) { out += schar; }
  }

  if (out.length != 6) { return null; }            // check length
  return out;
}
/* COLORSELECTOR ROUTINES */

/** wird vom form2mail-popup gebraucht. */
function reloadParent() {
	if(!opener.blindForm) {
		//alert('parent does nothing...');
		// firefox hack!
		if(!opener.document.forms.blindForm) {
			// can't help it
		} else {
			opener.document.forms.blindForm.submit();
		}
		// this works!	opener.submitBlindForm();
	} else {
		//alert('parent has got the blind form');
		//alert(opener.blindForm);
		opener.blindForm.submit();
	}
}

/** wird vom cosePopUp gebraucht. */
function closePopUp() {
	//alert('closing window...');
	window.close();
}   


function URLEncode( plaintext ){
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
};

function URLDecode( encoded){
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};
