/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length
 */
function core_md5(x, len)
{
	/* append padding */
	x[len >> 5] |= 0x80 << ((len) % 32);
	x[(((len + 64) >>> 9) << 4) + 14] = len;

	var a =  1732584193;
	var b = -271733879;
	var c = -1732584194;
	var d =  271733878;

	for(var i = 0; i < x.length; i += 16)
	{
		var olda = a;
		var oldb = b;
		var oldc = c;
		var oldd = d;

		a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
		d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
		c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
		b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
		a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
		d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
		c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
		b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
		a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
		d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
		c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
		b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
		a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
		d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
		c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
		b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

		a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
		d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
		c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
		b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
		a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
		d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
		c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
		b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
		a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
		d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
		c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
		b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
		a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
		d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
		c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
		b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

		a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
		d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
		c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
		b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
		a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
		d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
		c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
		b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
		a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
		d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
		c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
		b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
		a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
		d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
		c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
		b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

		a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
		d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
		c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
		b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
		a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
		d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
		c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
		b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
		a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
		d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
		c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
		b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
		a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
		d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
		c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
		b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

		a = safe_add(a, olda);
		b = safe_add(b, oldb);
		c = safe_add(c, oldc);
		d = safe_add(d, oldd);
	}
	return Array(a, b, c, d);

}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md5_cmn(q, a, b, x, s, t)
{
	return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
	return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
	return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
	return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
	return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
	var lsw = (x & 0xFFFF) + (y & 0xFFFF);
	var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
	return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function bit_rol(num, cnt)
{
	return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
 */
function str2binl(str)
{
	var bin = Array();
	var mask = (1 << chrsz) - 1;
	for(var i = 0; i < str.length * chrsz; i += chrsz)
		bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
	return bin;
}

/*
 * Convert an array of little-endian words to a hex string.
 */
function binl2hex(binarray)
{
	var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
	var str = "";
	for(var i = 0; i < binarray.length * 4; i++)
	{
		str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
					 hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
	}
	return str;
}


function jscss(a,o,c1,c2)
{
  switch (a) {
  
    case 'swap':
      o.className=!jscss('check',o,c1) ? o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    
    case 'add':
      if(!jscss('check',o,c1)) {
      	o.className+=o.className ? ' '+c1 : c1;
      }
    break;
    
    case 'remove':
      var rep=o.className.match(' '+c1) ? ' '+c1 : c1;
      o.className=o.className.replace(rep,'');
    break;
    
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}


function fuckYouSpambot(username, domain) {
    document.write('<a href="mailto:'+username+'@'+domain+'">'+username+'@'+domain+'</a>');
}
function disable(element) {
	element.disabled = true;
   	jscss('add',element,'disabled');
}

function enable(element) {
	element.disabled = false;
   	jscss('remove',element,'disabled');
}

function enableIf(element, when) {
	(when) ? enable(element) : disable(element);
}

function disableAll(elements) {
	for( var i=0; i<elements.length; i++)
		disable(elements[i]);
}

function enableAll(elements) {
	for( var i=0; i<elements.length; i++)
		enable(elements[i]);
}

function enableAllIf(elements, when) {
	for( var i=0; i<elements.length; i++)
		(when) ? enable(elements[i]) : disable(elements[i]);
}

function display( element ) {
	element.style.display = 'inline';
}

function hide( element ) {
	element.style.display = 'none';
}

function displayIf( element, when ) {
	( when ) ? display( element ) : hide( element );
}

var toValidate = new Array();

function validateAndSubmit( myform ) {
	validation_array = (toValidate[myform.name]) ? toValidate[myform.name] : new Array();
	if( validate( document.forms[myform.name], toValidate[myform.name]) ) {
    		document.forms[myform.name].submit();
	}
}

function validate(sender,myarray) {

	var err_msg = new Array('Folgende Fehler sind aufgetreten:\n');
	var error = false;
	
	if(myarray[0]=="*") {
		return confirm("Element löschen?");
	}
	
	else{
	for (i=0;i<myarray.length;i++) {
	  field = document.forms[sender.name].elements[myarray[i][0]];
	
	/* Block 1 überprüft Felder, die ausgefüllt sein müssen */
	  if (myarray[i][1].indexOf('e')>-1) {
	   	
		if (!field.value && !field.disabled) {
	      
		 error = true;
	      err_msg.push(myarray[i][2]);
	    }
		
	  }
	
	/* Block 2 überprüft, ob die Emailadresse formal richtig ist */
	 if (myarray[i][1].indexOf('m')>-1) {
	    if (field.value) {
	      var usr = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
	      var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
	      var regex = "^"+usr+"\@"+domain+"$";
	      var myrxp = new RegExp(regex);
	      var check = (myrxp.test(field.value));
	        if (check!=true) {
	          error=true;
	          err_msg.push(field.value+" "+myarray[i][2]);
	        }
	      }
	    }
		
		/* Block 3 überprüft Felder, deren Wert eine Zahl sein muss */
	  if (myarray[i][1].indexOf('n')>-1) {
	    var num_error = false;
	    if(field.value) {
	      var myvalue = field.value;
	      var num = myvalue.match(/[^0-9,\.]/gi)
	      var dot = myvalue.match(/\./g);
	      var com = myvalue.match(/,/g);
	      if (num!=null)  num_error = true;
	      else if ((dot!=null)&&(dot.length>1))  num_error = true;
	      else if ((com!=null)&&(com.length>1))  num_error = true;
	      else if ((com!=null)&&(dot!=null))     num_error = true;
	      
	    }
	    if (num_error==true) {
	        error = true;
	        err_msg.push(myvalue+" "+myarray[i][2]);
	    }
	  }
	
	
	/* Block 4 überprüft ob eine postleitzahl vorliegt*/
	 if (myarray[i][1].indexOf('p')>-1) {
	    var plz_error = false;
	    if(field.value) {
	      var myvalue = field.value;
	      var num = myvalue.match(/[^0-9]/g);
			if (num!=null) 	      plz_error = true;
			else if (myvalue.length>5) plz_error=true;
		}
		
	  
	    if (plz_error==true) {
	        error = true;
	        err_msg.push(myvalue+" "+myarray[i][2]);
	    }
	  }
	/* Block 5 Überprüft ob eine alphanumerische Eingabe vorliegt */
	 if (myarray[i][1].indexOf('a')>-1) {
	    var alnum_error = false;
	    if (field.value) {
			var myvalue = field.value;
			var num = myvalue.match(/[^0-9a-zA-Z\s]/gi);
	      	if (num!=null) alnum_error = true;
	    }
	    if (alnum_error==true) {
			error = true;
			err_msg.push(myvalue+" "+myarray[i][2]);
	    }			 	
	  }
	  /* Block 6 Überprüft ob ein port vorliegt */
	 if (myarray[i][1].indexOf('o')>-1) {
	    var port_error = false;
	    if (field.value) {
			var myvalue = field.value;
			var num = myvalue.match(/[^0-9]/g);
			if (num!=null) port_error = true;
			else if ((myvalue < 0) || (myvalue > 65535)) port_error=true;
	    }
	    if (port_error==true) {
			error = true;
			err_msg.push(myvalue+" "+myarray[i][2]);
	    }			 	
	  }
	  
	 /* Block 7 Überprüft ob eine ip vorliegt */
	 if (myarray[i][1].indexOf('z')>-1) {
	    var ip_error = false;
	    if (field.value) {
			var myvalue = field.value;
			var ip = myvalue.match(/\b(([0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})\b/g);
			var temp = myvalue.match(/[9]/g);
			if((temp != null) &&( temp.length > 6)) ip_error = true;
			else if ((ip==null) || (ip.length > 1 )) ip_error = true;
		}
	    if (ip_error==true) {
			error = true;
			err_msg.push(myvalue+" "+myarray[i][2]);
	    }			 	
	  }
	   /* Block 8 Überprüft ob eine name vorliegt */
	 if (myarray[i][1].indexOf('c')>-1) {
	    var name_error = false;
	    if (field.value) {
			var myvalue = field.value;
			var name = myvalue.match(/[^a-zA-ZäöüßÄÖÜ\-\s]/g);
			var mi = myvalue.match(/[^\-\s]/g);
			var ws = myvalue.match(/[\s]/g);
			if ((ws != null) && (ws.length > 4)) name_error = true;
			else if(mi == null) name_error=true;
			else if (name!=null) name_error = true;
		}
	    if (name_error==true) {
			error = true;
			err_msg.push(myvalue+" "+myarray[i][2]);
	    }			 	
	  }
	   /* Block 9 Überprüft ob eine preis vorliegt */
	 if (myarray[i][1].indexOf('x')>-1) {
	    var price_error = false;
	    if (field.value) {
			var myvalue = field.value;
			var price = myvalue.match(/euro/gi);
			var price2 = myvalue.match(/[^0-9euro\s,\.]/gi);
			var eur = myvalue.match(/\/g);
	     	var dot = myvalue.match(/\./g);
	     	var com = myvalue.match(/,/g);
	     	
			if((price != null) && (price.length > 1)) price_error=true;
			else if(price2 != null) price_error = true;
			else if((eur!=null) && (eur.length > 1)) price_error=true;
			else if((price!=null) && (eur!=null)) price_error=true;
			else if ((dot!=null)&&(dot.length>1)) price_error = true;
	     	else if ((com!=null)&&(com.length>1)) price_error = true;
	      	else if ((com!=null)&&(dot!=null))    price_error = true;
		}
	    if (price_error==true) {
			error = true;
			err_msg.push(myvalue+" "+myarray[i][2]);
	    }			 	
	  }
	    /* Block 10 Überprüft select  */
	 if (myarray[i][1].indexOf('s')>-1) {
		var select_error = true;
		if((field.selectedIndex > -1) && (field.options[field.selectedIndex].text))
			select_error = false;
		
	    if (select_error == true) {
			error = true;
			err_msg.push(myarray[i][2]);
	    }			 	
	 }
	   /* Block 11 Überprüft radiobuttons */
	 if (myarray[i][1].indexOf('r')>-1) {
		var radio_error = true;
		for(var k=0;k<field.length;k++) {
			if (field[k].checked) {
				radio_error = false;
				break;
			}
		}
	    if (radio_error) {
			error = true;
			err_msg.push(myarray[i][2]);
	    }			 	
	 }
	
	  /* Block 12 Überprüft telefonnummern */
	 if (myarray[i][1].indexOf('t')>-1) {
		var tel_error = false;
	    if(field.value) {
	      var myvalue = field.value;
	      var num = myvalue.match(/[^0-9\-\/\s]/g);
		  if (num!=null) tel_error = true;
		}
		if (tel_error==true) {
			error = true;
			err_msg.push(field.value+" "+myarray[i][2]);
	    }
	}
	/* Block 13 Überprüft ob eine strasse + hausnummer vorliegt */
	 if (myarray[i][1].indexOf('w')>-1) {
	    var alnum_error = false;
	    if (field.value) {
			var myvalue = field.value;
			var num = myvalue.match(/[^0-9a-zA-ZßüäöÜÄÖ\.\-\s]/gi);
	      	if (num!=null) alnum_error = true;
	    }
	    if (alnum_error==true) {
			error = true;
			err_msg.push(myvalue+" "+myarray[i][2]);
	    }			 	
	  }
	
	}
	/* im Fehlerfall werden hier die gesammelten Fehlermeldungen verarbeitet und angezeigt. Wenn das
	Formular ohne Beanstandung ist, wird es übertragen */
	  if (error) {
	    err_msg = err_msg.join('\n\xB7 ');
		alert(err_msg);
		return false;
	  }
	  else {
	    return true;
	  }
	}
}



var dwin = null;

function debug(msg) {
	if ((dwin == null) || (dwin.closed)) {
		dwin = window.open("","debugconsole","scrollbars=yes,resizable=yes,height=100,width=300");
		dwin.document.open("text/html", "replace");
	}
	dwin.document.writeln(''+msg);
	dwin.scrollTo(0,10000);
	dwin.focus();
	// dwin.document.close();  // uncomment this if you want to see only last message , not all the previous messages
}


