/*
 * Wheeler Forms Javascript Library
 *
 * (c)2004-2007 Hexon BV -- www.hexon.cx
 *
 */

var whf_fields = new Array();
var whf_cb = new Array();
var whf_columnviews = new Array();
var whf_masks = new Array();
var whf_preventmultiplesubmit = true;
var whf_doJavaScriptChecks = true;
var whf_submitId = null;

var whe_visibleDiv = -1;
var whe_IE = document.all?true:false;
var whf_defaultErrorStyles = new Array();
var whf_defaultErrorListDivision = '';
var whf_defaultErrorListStyle = 'ul';
var whf_defaultErrorListFormat = '%l %e';
var	whf_formErrorListDivision = whf_defaultErrorListDivision;
var	whf_formErrorListStyle = whf_defaultErrorListStyle;
var	whf_formErrorListFormat = whf_defaultErrorListFormat;
var whf_formValidationCallback = '';
var whf_defaultValidationMethod = 'WHF_ON_SUBMIT';
var whf_inputErrorBackgroundColor = '#ffdddd';
var whf_errorFloatBoxWidth = 200;
var	whf_errListItemCount = Array();
var whf_labelRequired = '';
var whe_exclamationImage = new Image(16,16);
var whe_oldhandler = '';
var whe_handlerRegistered = false;
var whe_divWidth = 0;
var whf_inputHash = '';

// MD5 vars:

    var whe_md5_state = new whe_array(4); 
    var whe_md5_count = new whe_array(2);
	whe_md5_count[0] = 0;
	whe_md5_count[1] = 0;                     
    var whe_md5_buffer = new whe_array(64); 
    var whe_md5_transformBuffer = new whe_array(16); 
    var whe_md5_digestBits = new whe_array(16);

    var whe_md5_S11 = 7;
    var whe_md5_S12 = 12;
    var whe_md5_S13 = 17;
    var whe_md5_S14 = 22;
    var whe_md5_S21 = 5;
    var whe_md5_S22 = 9;
    var whe_md5_S23 = 14;
    var whe_md5_S24 = 20;
    var whe_md5_S31 = 4;
    var whe_md5_S32 = 11;
    var whe_md5_S33 = 16;
    var whe_md5_S34 = 23;
    var whe_md5_S41 = 6;
    var whe_md5_S42 = 10;
    var whe_md5_S43 = 15;
    var whe_md5_S44 = 21;

	var whe_md5_ascii="01234567890123456789012345678901" +
          " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
          "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";


function whf_populate_by_slave(nameM, nameC) {
	objM = document.getElementById(nameM);
	objC = document.getElementById(nameC + '-sel');
	if(objC == null) {
		objC = document.getElementById(nameC);
		objCb = null;
	} else {
		objCb = document.getElementById(nameC);
	}
	if(objM == null || objC == null) {
		return;
	}

	objC.innerHTML = '';
	if(objCb != null) {
		objCb.value = '';
	}

	k = objM.value;

	ids = eval('whf_items_' + nameC + '_id[' + k + ']');
	vals = eval('whf_items_' + nameC + '_val[' + k + ']');

	whf_populate(objC, ids, vals);
}

function whf_populate(id, vals, defaultval, add, vals_associative) {
	if(id == null) {
		return;
	}

	if(vals == null) {
		vals = new Array();
	}

	if(defaultval != null) {
		cbid = document.getElementById(id.id + '-sel');
		if(cbid != null) {
			id.value = defaultval;
			id = cbid;
		}
	}

	if(add == null) {
		add = 1;
	}

	if(vals_associative == null) {
		vals_associative = false;
	}

	id.innerHTML = '';

	defaultfound = false;
	if(defaultval != null) {
		if(vals_associative) {
			if(vals[defaultval]) {
				defaultfound = true;
			} else {
				defaultfound = false;
			}
		} else {
			match = defaultval.toLowerCase();
			for(i in vals) {
				if(vals[i].toLowerCase() == match) {
					defaultfound = true;
				}
			}
		}

		if(!defaultfound && add) {
			var opt = document.createElement('option');
			opt.value = defaultval;			opt.innerHTML = defaultval;
			opt.selected = true;
			id.appendChild(opt);
		}
	}

	for(i in vals) {
		opt = document.createElement('option');
		if(vals_associative) {
			opt.value = i;
		} else {
			opt.value = vals[i];
		}
		opt.innerHTML = vals[i];
		opt.selected = false;
		if(defaultfound) {
			if(vals_associative) {
				if(i == defaultval) {
					opt.selected = true;
				}
			} else {
				if(vals[i].toLowerCase() == match) {
					opt.selected = true;
				}
			}
		}
		id.appendChild(opt);
	}
}

function whf_setDropdownByKey(obj, key) {
	// TODO: Support for comboboxes

	for(i = 0; i < obj.options.length; i++) {
		if(obj.options[i].value == key) {
			obj.selectedIndex = i;
			return;
		}
	}
}

function whf_set(obj, value) {
	obj_sel = document.getElementById(obj.id + '-sel');
	if(obj_sel != null) {
		obj_text = obj;
		obj = obj_sel;
	}

	value = value + '';
	value_escaped = value.replace(/&/, '&amp;');

	match = value.toLowerCase();
	match_escaped = value_escaped.toLowerCase();

	if(obj.options[obj.selectedIndex]) {
		if(obj.options[obj.selectedIndex].selected) {
			obj.options[obj.selectedIndex].selected = false;
		}
	}

	for(i = 0; i < obj.options.length; i++) {
		if(typeof obj.options[i] == 'string') {
			if(obj.options[i].toLowerCase() == match || obj.options[i].toLowerCase() == match_escaped) {
				if(obj_sel != null) {
					obj_text.value = value;
				}
				obj.selectedIndex = i;
				return;
			}
		} else {
			if(obj.options[i] == value || obj.options[i] == value_escaped) {
				if(obj_sel != null) {
					obj_text.value = value;
				}
				obj.selectedIndex = i;
				return;
			}
		}
	}
	for(i = 0; i < obj.options.length; i++) {
		if(obj.options[i].innerHTML.toLowerCase() == match || obj.options[i].innerHTML.toLowerCase() == match_escaped) {
			if(obj_sel != null) {
				obj_text.value = value;
			}
			obj.selectedIndex = i;
			return;
		}
	}

	// not found: add
	var opt = document.createElement('option');
	opt.value = value;
	opt.innerHTML = value;
	opt.selected = true;
	if(obj.options.length > 0) {
		first = obj.options[0];
		obj.insertBefore(opt, first);
		obj.selectedIndex = 0;
	} else {
		obj.appendChild(opt);
		obj.selectedIndex = obj.options.length - 1;
	}
	if(obj_sel != null) {
		obj_text.value = value;
	}
}

function whf_setIfExists(obj, value) {
	obj_sel = document.getElementById(obj.id + '-sel');
	if(obj_sel != null) {
		obj_text = obj;
		obj = obj_sel;
	}

	value_escaped = value.replace(/&/, '&amp;');

	match = value.toLowerCase();
	match_escaped = value_escaped.toLowerCase();

	for(i = 0; i < obj.options.length; i++) {
		if(typeof obj.options[i] == 'string') {
			if(obj.options[i].toLowerCase() == match || obj.options[i].toLowerCase == match_escaped) {
				if(obj_sel != null) {
					obj_text.value = value;
				}
				obj.selectedIndex = i;
				return;
			}
		} else {
			if(obj.options[i].value == value || obj.options[i].value == value_escaped) {
				if(obj_sel != null) {
					obj_text.value = value;
				}
				obj.selectedIndex = i;
				return;
			}
		}
	}
	for(i = 0; i < obj.options.length; i++) {
		if(obj.options[i].innerHTML.toLowerCase() == match || obj.options[i].innerHTML.toLowerCase() == match_escaped) {
			if(obj_sel != null) {
				obj_text.value = value;
			}
			obj.selectedIndex = i;
			return;
		}
	}
}

function whf_registerField(id, required, label) {
	whf_fields[id] = new Array();
	whf_fields[id]['required'] = required;
	whf_fields[id]['min_length'] = null;
	whf_fields[id]['max_length'] = null;
	whf_fields[id]['min'] = null;
	whf_fields[id]['max'] = null;
	whf_fields[id]['allow_past'] = null;
	whf_fields[id]['allow_future'] = null;
	whf_fields[id]['date_field'] = null;
	whf_fields[id]['with_seconds'] = null;
	whf_fields[id]['regexps'] = new Array();
	whf_fields[id]['regexpErrCodes'] = new Array();
	whf_fields[id]['errorScript'] = '';
	whf_fields[id]['valid'] = true;
	whf_fields[id]['label'] = label;
	whf_fields[id]['items'] = null;
	whf_fields[id]['obj'] = document.getElementById(id);

	if (whf_defaultValidationMethod == 'WHF_ON_BLUR' && (whf_fields[id]['validationMethod'] == undefined)){
		whf_fields[id]['validationMethod'] = 'WHF_ON_BLUR';
		whf_addOnBlur(id,'whf_checkField(this.id,false)',false);
	}
}

function whf_registerRadioItem(id, value) {
	if(whf_fields[id]['items'] == null)
		whf_fields[id]['items'] = new Array();
	var field=document.getElementById(id+'_'+value);
	whf_fields[id]['items'].push(field);
}

function whf_setRequired(id, required) {
	whf_fields[id]['required'] = required;

	if(document.getElementById('label'+ id) != undefined) {
		lId = document.getElementById('label'+ id);
	} else {
		return;
	}

	if(required) {
		var found = false;
		for(var i = 0; i < lId.childNodes.length; i++) {
			if(lId.childNodes[i].tagName && lId.childNodes[i].tagName.toLowerCase() == 'span' && lId.childNodes[i].innerHTML == whf_labelRequired) {
				found = true;
			}
		}
		if(!found) {
			var lrSpan = document.createElement('span');
			lrSpan.innerHTML = whf_labelRequired;
			lId.appendChild(lrSpan);
		}
	} else {
		for(var i = 0; i < lId.childNodes.length; i++) {
			if(lId.childNodes[i].tagName && lId.childNodes[i].tagName.toLowerCase() == 'span' && lId.childNodes[i].innerHTML == whf_labelRequired) {
				lId.childNodes[i].parentNode.removeChild(lId.childNodes[i]);
			}
		}
	}
}

function whf_setMinLength(id, value) {
	whf_fields[id]['min_length'] = value;
}

function whf_setMaxLength(id, value) {
	whf_fields[id]['max_length'] = value;
}

function whf_setMin(id, value) {
	whf_fields[id]['min'] = value;
}

function whf_setMax(id, value) {
	whf_fields[id]['max'] = value;
}

function whf_addValidationRegExp(id, regexp, errCode) {
	whf_fields[id]['regexps'].push(regexp);
	whf_fields[id]['regexpErrCodes'].push(errCode);
}

function whf_setAllowPast(id, value) {
	whf_fields[id]['allow_past'] = value;
}

function whf_setAllowFuture(id, value) {
	whf_fields[id]['allow_future'] = value;
}

function whf_linkDateField(id, datef) {
	whf_fields[id]['date_field'] = datef;
}

function whf_setWithSeconds(id, value) {
	whf_fields[id]['with_seconds'] = value;
}

function whf_focus(id) {
	try {
		focusfield(id);
	} catch(e) {
		try {
			document.getElementById(id).focus();
		} catch(e) {
			// Nothing
		}
	}
}

function whf_setLabelRequired(str) {
	whf_labelRequired = str;
}

function whf_setErrorStyle(id,style){
	whf_fields[id]['errorStyle'] = style;
}

function whf_setErrorScript(id,script){
	whf_fields[id]['errorScript'] = script;
}

function whf_setDefaultErrorList(division, style, format) {
	whf_defaultErrorListDivision = division;
	whf_defaultErrorListStyle = style;
	whf_defaultErrorListFormat = format;
}

function whf_setErrorList(id,division,style,format){
	if(id == 'form'){
		whf_formErrorListDivision = division;
		whf_formErrorListStyle = style;
		whf_formErrorListFormat = format;
	} else {
		whf_fields[id]['errorDivision'] = division;
		whf_fields[id]['errorListStyle'] = style
		whf_fields[id]['errorListFormat'] = format;
	}
}

function whf_setExclamationPosition(id,position){
	whf_fields[id]['errorExclamationPosition'] = position;
}


// Error trapping functionality
// Author: Andre de Jong
function whf_showError(id, errHandle){

	// apply error at form level
	if (id == '_whf_form'){
		var isFormError = true;
	} else {
		var isFormError = false;
	}

	if(errHandle == '') {
		errHandle = 'ERR_UNSPECIFIED';
	}

	if(whf_errMsg[errHandle] == undefined) {
		errMsg = 'Unspecified error: '+errHandle;
	} else {
		errMsg = whf_errMsg[errHandle];
	}

	if (isFormError) {
	
		// formbreed 
		for(errType in whf_defaultErrorStyles) {
	
			// ErrorScript:
			if(whf_defaultErrorStyles[errType] == 'errScript') {
				if (whf_defaultErrorScript.length >0){
					eval(whf_defaultErrorScript + '("form","'+errHandle+'");');
				}
				continue;
			}
			// list

			if(whf_defaultErrorStyles[errType] == 'list') {

				// defaults 
				if (!(whf_formErrorListDivision.length>0)){
					whf_formErrorListDivision = whf_defaultErrorListDivision;
				}
				if (!(whf_formErrorListStyle.length>0)){
					whf_formErrorListStyle = whf_defaultErrorListStyle;
				}
				if (!(whf_formErrorListFormat.length>0)){
					whf_formErrorListFormat = whf_defaultErrorListFormat;
				}

				if (document.getElementById(whf_formErrorListDivision) == undefined){
									// DIVISION NOT FOUND!!!! What SHOULD I DO !!TODO!!
					break;
				}
				
				if (whf_formErrorListStyle == 'ol') {
					listDef = 'ol';
					listItemDef = 'li';
				} else if (whf_formErrorListStyle == 'dl') {
					listDef = 'dl';
					listItemDef = 'dd';
				} else if (whf_formErrorListStyle == 'none'){
					listDef = 'span';
					listItemDef = 'span';
				} else {
					listDef = 'ul';
					listItemDef = 'li';
				}

				if (document.getElementById(whf_formErrorListDivision+'_list_'+listDef) == undefined){
					document.getElementById(whf_formErrorListDivision).innerHTML = document.getElementById(whf_formErrorListDivision).innerHTML + "<"+listDef+" id=\""+whf_formErrorListDivision+"_list_"+listDef+"\"></"+listDef+">";
				}

				if (document.getElementById('list_li_form') == undefined){
					document.getElementById(whf_formErrorListDivision+'_list_'+listDef).innerHTML = document.getElementById(whf_formErrorListDivision+'_list_'+listDef).innerHTML + "<"+listItemDef+" id=\"list_li_form\"></"+listItemDef+">\n";
				}

				_errMsg = whf_formErrorListFormat;
				_errMsg = _errMsg.replace(/%l/, '');
				_errMsg = _errMsg.replace(/%e/, errMsg);

				document.getElementById('list_li_form').innerHTML = _errMsg;
	
				document.getElementById('list_li_form').style.visibility = 'inherit';
				document.getElementById('list_li_form').style.display= '';

				if(whf_errListItemCount[listDef] == undefined){
					whf_errListItemCount[listDef] =1; 
				} else {
					whf_errListItemCount[listDef] = whf_errListItemCount[listDef] +1;
				}
	
				continue;
			}
		}

	} else {
		// field specific  

		if(whf_fields[id]['valid'] != true){
			whf_resetError(id, false);
		}

		sId = document.getElementById('input_'+id);
		iId = document.getElementById(id);
		if(document.getElementById('label'+ id) != undefined) {
			lId = document.getElementById('label'+ id);
		} else {
			lId = undefined;
		}
	
		defaultStyle = false;
		if(whf_fields[id]['errorStyle'] == undefined) {
			whf_fields[id]['errorStyle'] = whf_defaultErrorStyles;
			defaultStyle = true;
		}

		for(errType in whf_fields[id]['errorStyle']) {
			// LabelColor:
			if(whf_fields[id]['errorStyle'][errType] == 'labelColor') {
				if ((lId != undefined)){
					if(whf_colorError == null) {
						lId.style.color = 'red';
					} else {
						lId.style.color = whf_colorError;
					}
				}
				continue;
			}	

			// inputColor:
			if(whf_fields[id]['errorStyle'][errType] == 'inputColor') {
				iId.style.backgroundColor = whf_inputErrorBackgroundColor;
				continue;
			}

			// Exclamation
			if(whf_fields[id]['errorStyle'][errType] == 'exclamation') {
				if (whf_fields[id]['errorExclamationPosition'] == undefined){
					whf_fields[id]['errorExclamationPosition'] = whf_defaultErrorExclamationPosition;
				}

				if (document.getElementById('exclamation'+id) == undefined){
	
					excl = document.createElement('span');
					excl.id = 'exclamation'+id;
					excl.innerHTML ='<img id="exclamationimg'+id+'" alt ="!" title=""></img>';
	
					if(whf_fields[id]['errorExclamationPosition'] == 'beforeLabel'){
						if (lId != undefined){
							lId.insertBefore(excl,lId.firstChild);
						}
					} else if (whf_fields[id]['errorExclamationPosition'] == 'beforeInput'){
						sId.insertBefore(excl,sId.firstChild);
					} else if (whf_fields[id]['errorExclamationPosition'] == 'behindInput'){
						sId.appendChild(excl);
					} else { // default = behindLabel
						if (lId != undefined){
							lId.appendChild(excl);
						}
					}
				}

				document.getElementById('exclamationimg'+id).src = whe_exclamationImage.src;
				document.getElementById('exclamation'+id).style.visibility = 'inherit';
				document.getElementById('exclamation'+id).style.display= 'inline';
				continue;
			}


			// FloatingBox:
			if(whf_fields[id]['errorStyle'][errType] == 'floatingBox') {
	
				if (!whe_handlerRegistered){
					if (document.onmousemove!= undefined){
						whe_oldhandler = document.onmousemove;
					}
				
					if(!whe_IE) document.captureEvents(Event.MOUSEMOVE);
					document.onmousemove = whe_getMouseXY;
					whe_handlerRegistered = true;
				}

				if (document.getElementById('errmsg'+id) == null) {
					util_createFloatingDiv('errmsg'+id);
				}

				document.getElementById('errmsg'+id).innerHTML = errMsg;
			
				if (lId != undefined){
					lId.onmouseover = function() { util_showFb(this,'label'); }
					lId.onmouseout = util_hideFb;
				}
			
				if(document.getElementById('exclamation'+id) != undefined){
					document.getElementById('exclamation'+id).onmouseover = function() { util_showFb(this,'exclamation'); }
					document.getElementById('exclamation'+id).onmouseout = util_hideFb;
				}

				continue;
			}
	
			// ErrorScript:
			if(whf_fields[id]['errorStyle'][errType] == 'errScript') {
				if(defaultStyle || whf_fields[id]['errorScript'].length == 0){
					if (whf_defaultErrorScript.length >0){
						eval(whf_defaultErrorScript + '("'+id+'","'+errHandle+'");');
					}
				} else {
					if (whf_fields[id]['errorScript'].length > 0){
						eval(whf_fields[id]['errorScript']+ '("'+id+'","'+errHandle+'");');
					}
				}
				continue;
			}

			// list
			if(whf_fields[id]['errorStyle'][errType] == 'list') {
				
				if (document.getElementById(whf_fields[id]['errorListDivision']) == undefined){
					if (document.getElementById(whf_defaultErrorListDivision) == undefined){
						// DIVISION NOT FOUND!!!! What SHOULD I DO !!TODO!!
						continue;
					}
					whf_fields[id]['errorListDivision'] = whf_defaultErrorListDivision;
				}

				if(whf_fields[id]['errorListFormat'] == undefined){
					whf_fields[id]['errorListFormat'] = whf_defaultErrorListFormat;
				}

				if(whf_fields[id]['errorListStyle'] == undefined){
					whf_fields[id]['errorListStyle'] = whf_defaultErrorListStyle;
				}

				if (whf_fields[id]['errorListStyle'] == 'ol') {
					listDef = 'ol';
					listItemDef = 'li';
				} else if (whf_fields[id]['errorListStyle'] == 'dl') {
					listDef = 'dl';
					listItemDef = 'dd';
				} else if (whf_fields[id]['errorListStyle'] == 'none'){
					listDef = 'span';
					listItemDef = 'span';
				} else {
					listDef = 'ul';
					listItemDef = 'li';
				}

				if (document.getElementById(whf_fields[id]['errorListDivision']+'_list_'+listDef) == undefined){
					document.getElementById(whf_fields[id]['errorListDivision']).innerHTML = document.getElementById(whf_fields[id]['errorListDivision']).innerHTML + "<"+listDef+" id=\""+whf_fields[id]['errorListDivision']+"_list_"+listDef+"\"></"+listDef+">";
				}

				if (document.getElementById('list_li'+id) == undefined){
					document.getElementById(whf_fields[id]['errorListDivision']+'_list_'+listDef).innerHTML = document.getElementById(whf_fields[id]['errorListDivision']+'_list_'+listDef).innerHTML + "<"+listItemDef+" id=\"list_li"+id+"\"></"+listItemDef+">\n";
				}


				_errMsg = whf_fields[id]['errorListFormat'];
				_errMsg = _errMsg.replace(/%l/, whf_fields[id]['label']);
				_errMsg = _errMsg.replace(/%e/, errMsg);
				document.getElementById('list_li'+id).innerHTML = _errMsg;
	
				document.getElementById('list_li'+id).style.visibility = 'inherit';
				document.getElementById('list_li'+id).style.display= '';

				if(whf_errListItemCount[listDef] == undefined){
					whf_errListItemCount[listDef] =1; 
				} else {
					whf_errListItemCount[listDef] = whf_errListItemCount[listDef] +1;
				}
				document.getElementById(whf_fields[id]['errorListDivision']+'_list_'+listDef).style.visibility= 'inherit';
				document.getElementById(whf_fields[id]['errorListDivision']+'_list_'+listDef).style.display= '';
	
				continue;
			}
		}
		whf_fields[id]['valid'] = false;
	} // eo field_specific
}

// id: field for which the error needs to reset,
// isValid: only reset the value, or reset the (optional) callback as well

function whf_resetError(id, isValid) {
	if(whf_fields[id]['valid'] == true){
		return;
	}

	if (whf_fields[id]['errorStyle'] == undefined){
		whf_fields[id]['errorStyle'] = whf_defaultErrorStyles;
	}


	iId = document.getElementById(id);
	if (document.getElementById('label'+ id) != undefined){
		lId = document.getElementById('label'+ id);
	} else {
		lId = undefined;
	}

	for(errType in whf_fields[id]['errorStyle']) {
		if(whf_fields[id]['errorStyle'][errType] == 'labelColor' && lId != undefined) {
			lId.style.color = '';
			continue;
		}

		if(whf_fields[id]['errorStyle'][errType] == 'inputColor') {
			iId.style.backgroundColor = '';
			continue;
		}

		if(whf_fields[id]['errorStyle'][errType] == 'exclamation') {
				document.getElementById('exclamation'+id).style.visibility = 'hidden';
				document.getElementById('exclamation'+id).style.display= 'none';
		}
	

		if(isValid) {
			if (whf_fields[id]['errorStyle'][errType] == 'errScript') {
				if(defaultStyle || whf_fields[id]['errorScript'].length == 0){
					if (whf_defaultErrorScript.length >0){
						eval(whf_defaultErrorScript + '("'+id+'","ERR_NONE");');
					}
				} else {
					if (whf_fields[id]['errorScript'].length > 0){
						eval(whf_fields[id]['errorScript']+ '("'+id+'","ERR_NONE");');
					}
				}
			}
		}

		if(whf_fields[id]['errorStyle'][errType] == 'floatingBox') {
			if ((lId != undefined) && (lId.onmouseover != undefined) && (lId.onmouseout != undefined)){
				lId.onmouseover = function() {};
				lId.onmouseout = function() {};
			}
			continue;
		}

		if(whf_fields[id]['errorStyle'][errType] == 'list') {
			if (document.getElementById('list_li'+id) != undefined){
    			    document.getElementById('list_li'+id).style.visibility = 'hidden';
			    document.getElementById('list_li'+id).style.display= 'none';
			}
			
			if (whf_fields[id]['valid'] == false || whf_fields[id]['valid'] == undefined){
				whf_errListItemCount[listDef] = whf_errListItemCount[listDef] -1;
				if(whf_errListItemCount[listDef] == 0){
					document.getElementById(whf_fields[id]['errorListDivision']+'_list_'+listDef).style.visibility= 'hidden';
					document.getElementById(whf_fields[id]['errorListDivision']+'_list_'+listDef).style.display= 'none';
				}
			}
			continue;
		}


	}
	whf_fields[id]['valid'] = true;
}


function whf_isEmpty(field) {
	if(field.value == '') {
		return true;
	}

	nr = field.getAttribute('maskNr');
	if(nr != null) {
		mask = whf_masks[nr].mask;
		mask = mask.replace(/[#X]/g, ' ');
		if(whf_trim(mask) == whf_trim(field.value)) {
			return true;
		}
	}

	if(field.type == 'checkbox' && field.checked == false){
		return true;
	}


	return false;
}


function setDefaultErrorValidationMethod(method){
	whf_defaultValidationMethod = method;
}

//function whf_checkObj(obj){
//	whf_checkField(obj.id,false);
//}


function whf_checkField(id,checkRequired) {
	field = document.getElementById(id);
	error = false;
	errMessage = '';
	__first =null;
	try {
		if(whf_fields[id]['items']!=null) {
			if(whf_fields[id]['required'] && checkRequired) {
				var i;
				error = true;
				errMessage = 'ERR_EMPTY';
				for(i=0; whf_fields[id]['items'].length>i; i++) {
					if(whf_fields[id]['items'][i].checked) {
						error=false;
						errMessage='';
					}
				}
			}
		} else if(whf_isEmpty(field)) {
			if(whf_fields[id]['required'] && checkRequired) {
				error = true;
				errMessage = 'ERR_EMPTY';
			}
		} else {
			if(!error && whf_fields[id]['min_length'] != null && field.value.length < whf_fields[id]['min_length']) {
				errMessage = 'ERR_MIN_LENGTH';
				error = true;
			}

			if(!error && whf_fields[id]['max_length'] != null && field.value.length > whf_fields[id]['max_length']) {
				errMessage = 'ERR_MAX_LENGTH';
				error = true;
			}

			if(!error && whf_fields[id]['min'] != null && whf_parseFloat(field.value) < whf_fields[id]['min']) {
				errMessage = 'ERR_MIN_VALUE';
				error = true;
			}

			if(!error && whf_fields[id]['max'] != null && whf_parseFloat(field.value) > whf_fields[id]['max']) {
				errMessage = 'ERR_MAX_VALUE';
				error = true;
			}

			if(!error) {
				for(regexp in whf_fields[id]['regexps']) {
					ret = whf_fields[id]['regexps'][regexp].exec(field.value);
					if(!ret) {
						errMessage = whf_fields[id]['regexpErrCodes'][regexp];
						error = true;
					}
				}
			}

			if(!error && whf_fields[id]['allow_past'] != null && whf_fields[id]['allow_past'] == false) {
				var now=new Date();
				var input=field.value.split('-');
				now=new Date(now.getFullYear(), now.getMonth(), now.getDate());
				input=new Date(parseInt(input[2]), parseInt(input[1])-1, parseInt(input[0]));
				if(now.getTime()>input.getTime()) {
					errMessage = 'ERR_PAST';
					error = true;
				}
			}

			if(!error && whf_fields[id]['allow_future'] != null && whf_fields[id]['allow_future'] == false) {
				var now=new Date();
				var input=field.value.split('-');
				now=new Date(now.getFullYear(), now.getMonth(), now.getDate());
				input=new Date(parseInt(input[2]), parseInt(input[1])-1, parseInt(input[0]));
				if(now.getTime()<input.getTime()) {
					errMessage = 'ERR_FUTURE';
					error = true;
				}
			}
			if(!error && whf_fields[id]['date_field'] != null && whf_fields[whf_fields[id]['date_field']]['valid']==true) {
				var datef=whf_fields[whf_fields[id]['date_field']];
				var now=new Date();
				var tinput=field.value.split(':');
				if(datef['allow_past'] != null && datef['allow_past'] == false) {
					var dinput=datef['obj'].value.split('-');
					if(!tinput[2])
						tinput[2]=0;
					var input=new Date(parseInt(dinput[2]), parseInt(dinput[1])-1, parseInt(dinput[0]), parseInt(tinput[0]), parseInt(tinput[1]), parseInt(tinput[2]));
					if(now.getTime()>input.getTime()) {
						errMessage = 'ERR_PAST';
						error = true;
					}
				}
				if(datef['allow_future'] != null && datef['allow_future'] == false) {
					var dinput=datef['obj'].value.split('-');
					if(!tinput[2])
						tinput[2]=0;
					input=new Date(parseInt(dinput[2]), parseInt(dinput[1])-1, parseInt(dinput[0]), parseInt(tinput[0]), parseInt(tinput[1]), parseInt(tinput[2]));
					if(now.getTime()<input.getTime()) {
						errMessage = 'ERR_FUTURE';
						error = true;
					}
				}
			}
			
			// javascript callbacks : 
			// TODO?? //			
		}
	} catch(e) {
		// Nothing
	}
	if(error) {
		whf_showError(id,errMessage);
		__first = id;
	} else {
		whf_resetError(id,true);
	}
	return __first;
}

function whf_submitIntermediate(){

	whf_doJavaScriptChecks = false;
	
	var whf_hiddenField = document.createElement('input');
	whf_hiddenField.type = 'hidden';
	whf_hiddenField.name = 'intermediateSubmit';
	whf_hiddenField.value = '1';

	document.getElementById('whf_form').appendChild(whf_hiddenField);
	document.getElementById('whf_form').submit();
}


function whf_checkForm(obj) {
	btn = null;
	if(whf_submitId != null) {
		btn = document.getElementById(whf_submitId);
	}

	first = null;
	var _first = null;
	if(whf_doJavaScriptChecks) {
		for(id in whf_fields) {
		  _first = whf_checkField(id,true);
			if(first == null){
				first = _first;
			}
		}
	}

	if(first != null) {
		whf_focus(first);
		return false;
	} 

	// do form level javascriptCallbackValidation
	formOk = true;
	if(whf_formValidationCallback != ''){
		exec = "formOk = "+whf_formValidationCallback+"();";
		eval(exec);	
	}
	
	if(formOk != true){
		whf_showError('_whf_form',formOk);
		return false;
	}

	// form is ok
	onsend = obj.getAttribute('onsend');
	if(onsend != '') {
		eval(onsend);
	}
	if(whf_preventmultiplesubmit && btn != null) {
		btn.disabled = true;
	}
	return true;
	
}

function whf_Combobox(id, div_id, defaultValue) {
	this.id = id;
	this.autocompleteQueued = false;
	this.changed = false;

	this.div = document.getElementById(div_id + '-div');
	this.div.setAttribute('cbid', this.id);

	this.sel = document.getElementById(div_id + '-sel');
	this.sel.setAttribute('cbid', this.id);

	this.inp = document.getElementById(div_id);
	this.inp.setAttribute('cbid', this.id);

	this.img = document.getElementById(div_id + '-img');
	this.img.setAttribute('cbid', this.id);

	this.defaultValue = defaultValue;
}

whf_Combobox.prototype.initialize = function() {
	if(this.sel.offsetWidth == 0) {
		window.setTimeout('whf_cb[' + this.id + '].initialize()', 40);
		return;
	}
	this.width = this.sel.offsetWidth;
	this.height = this.sel.offsetHeight;
	this.arrowWidth = this.height * 0.88;

	this.div.style.width = this.width + 'px';
	this.div.style.height = this.height + 'px';
	this.div.onfocus = function() { whf_cb_focus(this); }

	this.sel.style.width = this.width + 'px';
	this.sel.style.clip = 'rect(auto auto auto ' + (this.width - this.arrowWidth) + 'px)';
	this.sel.selectedIndex = -1;
	this.sel.tabIndex = -1;
	if(!this.sel.onchange) {
		this.sel.onchange = function() { whf_cb_change(this); }
	} else {
		var tmp = this.sel.onchange;
		this.sel.onchange = function() { whf_cb_change(this); tmp(); }
	}

	this.inp.style.width = (this.width - this.arrowWidth + 2) + 'px';
	this.inp.onkeydown = function() { whf_cb_keydown(this); }
	this.inp.onkeyup = function() { whf_cb_keyup(this); }
	this.inp.onblur = function() { whf_cb_blur(this); }

	this.img.style.height = '1px';
	this.img.style.width = this.width + 'px';

	if(this.defaultValue != null) {
		this.setValue(this.defaultValue);
	}
}

whf_Combobox.prototype.setValue = function(value) {
	this.inp.value = value;

	value = value.toLowerCase();
	for(i = 0; i < this.sel.options.length; i++) {
		search = this.sel.options[i].innerHTML.toLowerCase();
		if(value == search) {
			this.sel.selectedIndex = i;
		}
	}
	if(this.inp.fireEvent) {
		this.inp.fireEvent('onchange');
	} else if(this.inp.onchange) {
		this.inp.onchange();
	}
}

whf_Combobox.prototype.autocomplete = function() {
	if(!this.autocompleteQueued) {
		return;
	}

	this.autocompleteQueued = false;

	match = this.inp.value.toLowerCase();
	if(match == '') {
		return;
	}

	for(i = 0; i < this.sel.options.length; i++) {
		search = this.sel.options[i].innerHTML.toLowerCase();
		if(search.indexOf(match) == 0) {
			this.inp.value += this.sel.options[i].innerHTML.substring(match.length, search.length);
			this.changed = true;
			var range = this.inp.createTextRange();
			range.moveStart('character', match.length);
			range.select();
			break;
		}
	}
}

whf_Combobox.prototype.setCase = function() {
	match = this.inp.value.toLowerCase();

	if(match == '') {
		return;
	}

	for(i = 0; i < this.sel.options.length; i++) {
		search = this.sel.options[i].innerHTML.toLowerCase();
		if(match == search && this.inp.value != this.sel.options[i].innerHTML) {
			this.inp.value = this.sel.options[i].innerHTML;
			this.changed = true;
			break;
		}
	}
}

whf_Combobox.prototype.sel_stepEntry = function(step) {
	var newidx = this.sel.selectedIndex + step;
	if(newidx < 0) {
		return;
	}
	if(newidx > (this.sel.options.length - 1)) {
		return;
	}
	this.sel.selectedIndex = newidx;
	this.sel_change();
}

whf_Combobox.prototype.sel_change = function() {
	this.inp.value = this.sel.options[this.sel.selectedIndex].innerHTML;
	if(this.inp.fireEvent) {
		this.inp.fireEvent('onchange');
	} else if(this.inp.onchange) {
		this.inp.onchange();
	}
	this.changed = false;
	this.inp.select();
	this.inp.focus();
}

whf_Combobox.prototype.inp_keydown = function() {
	if(event.keyCode == 38) {
		this.sel_stepEntry(-1);
	} else if(event.keyCode == 40) {
		this.sel_stepEntry(1);
	}
}

whf_Combobox.prototype.inp_keyup = function() {
	if(event.keyCode < 49 && event.keyCode != 32) {
		return;
	} else if(event.Control || event.Alt) {
		return;
	}

	this.autocompleteQueued = true;
	window.setTimeout('whf_cb[' + this.id + '].autocomplete()', 50);
}

whf_Combobox.prototype.inp_blur = function() {
	this.setCase();
	if(this.changed) {
		if(this.inp.fireEvent) {
			this.inp.fireEvent('onchange');
		} else if(this.inp.onchange) {
			this.inp.onchange();
		}
		this.changed = false;
	}
}

function whf_cb_init(div_id, defaultValue) {
	id = whf_cb.length;
	whf_cb[id] = new whf_Combobox(id, div_id, defaultValue);
	whf_cb[id].initialize();
}

function whf_cb_focus(sel_obj) {
	id = sel_obj.getAttribute('cbid');
	whf_cb[id].inp.focus();
}

function whf_cb_change(sel_obj) {
	id = sel_obj.getAttribute('cbid');
	whf_cb[id].sel_change();
}

function whf_cb_keydown(inp_obj) {
	id = inp_obj.getAttribute('cbid');
	whf_cb[id].inp_keydown();
}

function whf_cb_keyup(inp_obj) {
	id = inp_obj.getAttribute('cbid');
	whf_cb[id].inp_keyup();
}

function whf_cb_blur(inp_obj) {
	id = inp_obj.getAttribute('cbid');
	whf_cb[id].inp_blur();
}

function shuffleArray(a, b) {
	return(2 * Math.floor(Math.random() * 2)) - 1;
} 

function whf_getPosX(obj) {
	if(obj.offsetParent) {
		return obj.offsetLeft + whf_getPosX(obj.offsetParent);
	} else {
		return obj.offsetLeft;
	}
}
				
function whf_getPosY(obj) {
	if(!obj.offsetParent) {
		return obj.offsetTop;
	} else {
		return obj.offsetTop + whf_getPosY(obj.offsetParent);
	}
}

function whf_trim(s) {
	while(s.substring(0,1) == ' ') {
		s = s.substring(1, s.length);
	}
	while(s.substring(s.length-1, s.length) == ' ') {
		s = s.substring(0, s.length - 1);
	}
	return s;
}

function replaceAll(str, search, repl) {
	while(str.indexOf(search) != -1) {
		str = str.replace(search, repl);
	}
	return str;
}

// @returns a point or comma if a decimal point is found or false when no decimal point is found
function whf_findDecimalPoint(value, forInt) {
	if(typeof(forInt) == undefined) {
		forInt = false;
	}

	value = value+'';
	var indexComma = value.lastIndexOf(',');
	var indexPoint = value.lastIndexOf('.');

	if(indexComma < 0 && indexPoint < 0) {
		return false;
	}

	var dp = '';
	if(indexComma > indexPoint) {
		dp = ',';
	} else {
		dp = '.';
	}

	var parts = value.split(new RegExp(whf_escapeSymbol(dp)));
	if(parts.length > 2) {
		return false;
	} 

	if(!forInt && dp == '.' && /\.[0-9]{3}$/.test(value)) {
		return false;
	} else if(forInt && /[\.,][0-9]{3}$/.test(value)) {
		return false;
	}

	return dp;
}

// @returns a point, comma or space if a thousands separator is found or false when no thousands separator is found
function whf_findThousandsSeparator(value, decimal_point) {
	if(decimal_point === false) {
		if(/-?[0-9]{1,3}(\.[0-9]{3})+/.test(value)) {
			return '.';
		} else if(/-?[0-9]{1,3}(,[0-9]{3})+/.test(value)) {
			return ',';
		} else if(/-?[0-9]{1,3}( [0-9]{3})+/.test(value)) {
			return ' ';
		} else {
			return false;
		}
	} else {
		parts = value.split(decimal_point);
		return whf_findThousandsSeparator(parts[0], false);
	}
}

function whf_escapeSymbol(s) {
	if(s == '.') {
		return '\\.';
	} else {
		return s;
	}
}

function whf_parseInt(value, decimal_point, thousands_separator) {
	whf_parseInteger(value, decimal_point, thousands_separator);
}

function whf_parseInteger(value, decimal_point, thousands_separator) {
	if(typeof(decimal_point) == 'undefined') {
		decimal_point = whf_findDecimalPoint(value, true);
		thousands_separator = whf_findThousandsSeparator(value, decimal_point);
	} else if(typeof(thousands_separator) == 'undefined') {
		thousands_separator = whf_findThousandsSeparator(value, decimal_point);
	}

	if(thousands_separator) {
		while(new RegExp(whf_escapeSymbol(thousands_separator)+'[0-9]{3}').test(value)) {
			value = value.replace(new RegExp(whf_escapeSymbol(thousands_separator)+'([0-9]{3})'), '$1');
		}
	}
	if(decimal_point) {
		value = value.replace(new RegExp(whf_escapeSymbol(decimal_point)), '.');
	}
	if(!value.match(/^-?[0-9]+(\.[0-9]+)?$/)) {
		return false;
	}
	var intval = Math.round(value);
	if(isNaN(intval)) {
		return false;
	}
	return intval;
}

function whf_formatInteger(i, thousands_separator) {
	if(i === false) {
		return '';
	}
	if(typeof(thousands_separator) == 'undefined' || (thousands_separator != '.' && thousands_separator != ',' && thousands_separator != ' ')) {
		thousands_separator = '.';
	}
	i = i +'';
	while(/(-?[0-9]+)([0-9]{3})/.test(i)) {
		i = i.replace(/(-?[0-9]+)([0-9]{3})/, '$1'+ thousands_separator +'$2');
	}
	return i;
}


function whf_toggleInteger(obj, disable, decimal_point, thousands_separator) {
	if(typeof(obj) == 'object') {
		value = obj.value;
	} else {
		value = obj + '';
	}

	var intval = whf_parseInteger(value, decimal_point, thousands_separator);

	var result = '';

	if(disable) {
		if(!intval) {
			result = '';
		} else {
			result = intval;
		}
	} else if(!disable && intval) {
		result = whf_formatInteger(intval, thousands_separator);
	}

	if(typeof(obj) == 'object') {
		if(result != obj.value && result) {
			obj.value = result;
		}
	} else {
		return result;
	}
}

function whf_parseFloat(value, decimal_point, thousands_separator) {
	if(typeof(decimal_point) == 'undefined') {
		decimal_point = whf_findDecimalPoint(value);
		thousands_separator = whf_findThousandsSeparator(value, decimal_point);
	} else if(typeof(thousands_separator) == 'undefined') {
		thousands_separator = whf_findThousandsSeparator(value, decimal_point);
	}

	if(thousands_separator) {
		while(new RegExp(whf_escapeSymbol(thousands_separator)).test(value)) {
			value = value.replace(new RegExp(whf_escapeSymbol(thousands_separator)), '');
		}
	}
	if(decimal_point) {
		value = value.replace(new RegExp(whf_escapeSymbol(decimal_point)), '.');
	}
	if(!value.match(/^-?[0-9]+(\.[0-9]+)?$/)) {
		return false;
	}
	var floatval = parseFloat(value);
	if(isNaN(floatval)) {
		return false;
	}
	return floatval;
}

function whf_formatFloat(value, decimals, decimal_point, thousands_separator) {
	if(typeof(decimal_point) == 'undefined') {
		decimal_point = ',';
	}
	if(typeof(thousands_separator) == 'undefined') {
		thousands_separator = '.';
	}

	if(value === false) {
		return '';
	}

	if(decimal_point == thousands_separator) {
		return '';
	}

	value = value+'';
	var parts = value.split('.');

	if(typeof(parts[1]) == 'undefined') {
		return whf_formatInteger(value, thousands_separator);
	}

	while(/(-?[0-9]+)([0-9]{3})/.test(parts[0])) {
		parts[0] = parts[0].replace(/(-?[0-9]+)([0-9]{3})/, '$1'+ thousands_separator +'$2');
	}
	if(decimals !== 0 && decimals > 0) {
		if(parts[1].length > decimals) {
			var base = parseInt(parts[1].substr(0, decimals));
			var omh_oml = parseInt(parts[1].substr(decimals, 1));
			if(omh_oml >= 5) {
				base++;
			}
			parts[1] = new String(base);
		} else if(parts[1].length < decimals) {
			var over = decimals - parts[1].length;
			for(var i = 0; i < over; i++) {
				parts[1] += '0';
			}
		}
	}
	return parts[0] + decimal_point + parts[1];
}

function whf_toggleFloat(obj, decimals, disable, decimal_point, thousands_separator) {
	if(decimals === 0) {
		return whf_toggleInteger(obj, disable, decimal_point, thousands_separator);
	}
	if(typeof(obj) == 'object') {
		value = obj.value;
	} else {
		value = obj + '';
	}

	floatval = whf_parseFloat(value, decimal_point, thousands_separator);

	var result = '';
	if(disable) {
		if(!floatval) {
			result = '';
		} else {
			//TODO: , vervangen door decimal_separator indien ingevuld
			result = new String(floatval).replace(/\./, ',');;
		}
	} else if(!disable && floatval) {
		result = whf_formatFloat(floatval, decimals, decimal_point, thousands_separator);
	}

	if(typeof(obj) == 'object') {
		if(result != obj.value && result) {
			obj.value = result;
		}
	} else {
		return result;
	}
}

function whf_formatKenteken(obj, disable) {
//document.getElementById('errordivision').innerHTML = document.getElementById('errordivision').innerHTML + "<BR>"+ obj + "\n" + disable;
	if(typeof(obj) == 'object') {
		value = obj.value;
	} else {
		value = obj + '';
	}

	result = value.replace(/-/g, '');
	result = result.toUpperCase();

	if(!disable && result.length == 6) {
		if(/[0-9]{2}[A-Z]{3}[0-9]|[A-Z]{2}[0-9]{3}[A-Z]/.exec(result)) {
			result = result.substring(0, 2) + '-' + result.substring(2, 5) + '-' + result.substring(5, 6);
		} else if(/[0-9][A-Z]{3}[0-9]{2}|[A-Z][0-9]{3}[A-Z]{2}/.exec(result)) {
			result = result.substring(0, 1) + '-' + result.substring(1, 4) + '-' + result.substring(4, 6);
		} else {
			result = result.substring(0, 2) + '-' + result.substring(2, 4) + '-' + result.substring(4, 6);
		}
	}
	
	if(typeof(obj) == 'object') {
		if(result != obj.value) {
			obj.value = result;
		}
	} else {
		return result;
	}
}

function htmlentities(str) {
	norm = new Array("Ã©","Ã‰","Ã¨","Ãˆ","Ãª","ÃŠ","Ã«","Ã‹","Ã ","Ã€","Ã¹","Ã™","Ã§","Ã‡","Å ");
	spec = new Array('&eacute;', '&Eacute;', '&egrave;', '&Egrave;', '&ecirc;', '&Ecirc;', '&euml;', '&Euml;', '&agrave;', '&Agrave;', '&ugrave;', '&Ugrave;', '&ccedil;', '&Ccedil;', '&Scaron;');
	for(i = 0; i < norm.length; i++) {
		str = replaceAll(str, norm[i], spec[i]);
	}
	return str;
}

function serialize(variable) {
	type = typeof(variable);

	switch(type) {
		case 'string':
			return 's:' + variable.length + ':"' + variable + '";';
		
		case 'number':
			return 's:' + ("" + variable).length + ':"' + variable + '";';
		
		case 'object':
			if(variable.length == undefined || variable.length == 0) {
				// Of een lege array, of een associatieve array
				var str = '';
				var len = 0;
				for(var i in variable) {
					str = str + 's:' + ("" + i).length + ':"' + i + '";' + serialize(variable[i]);
					len++;
				}
				return 'a:' + len + ':{' + str + '}';
			} else {
				// Normale array
				str = '';
				for(i = 0; i < variable.length; i++) {
					str = str + 'i:' + i + ';' + serialize(variable[i]);
				}
				return 'a:' + variable.length + ':{' + str + '}';
			}

		case 'undefined':
			return "N;";

		default:
			return "N;";
	}
}


function whf_createInputHash(){
	inputHash = '';
	
	for (index in whf_fields){
		try {
				
			// commented fields:
			if (document.getElementById(index) == undefined){
				continue;
			}
			
			if(whf_fields[index]['items']!=null) {
				// Radio Field
				inputHash = inputHash + '^%^' + whf_fields[index]['items'][0].value;
				continue;
			}
			value = document.getElementById(index).value;


			if(document.getElementById(index).type == 'checkbox'){
				value = (document.getElementById(index).checked == true)?'true':'false';
			}
			if(document.getElementById(index).type == 'hidden'){
				value = '';
			}
					
			inputHash = inputHash + '_'  + value;
		}
		catch(e) {
		}
	}

	return whe_SimpleHash(inputHash);
}

function whe_SimpleHash(content){

	var len= content.length;

	var total =0;
	for(var teller = 0;teller <len;teller++){
		total = (total + ((teller+1) * content.charCodeAt(teller)*(len-teller+1))) & 131071;
	}
	return total;
}



function whf_formChanged(){
	if (!(whf_createInputHash() == whf_inputHash)){
		return true;
	} else {
		return false;
	}
}


function ColumnView(container_id) {
	this.id = whf_columnviews.length;
	whf_columnviews[this.id] = this;
	this.container_id = container_id;
	this.tr_id = container_id + '_tr';
	this.container = document.getElementById(this.container_id);
	this.items = new Array();
	this.itemsPerColumn = 0;
	this.columnPadding = 10;
	this.minColumnWidth = 150;
	this.minVisibleColumns = 0;
	this.numColumns = 0;
	this.itemHeight = 20;
	this.itemClassName = 'option';
	this.columnmClassName = 'column';
	this.redrawing = false;
}

ColumnView.prototype.add = function(s) {
	this.items.push(s);
}

ColumnView.prototype.clear = function() {
	this.items = new Array();
}

ColumnView.prototype.onresize = function() {
	if(this.resizeTimer) {
		window.clearTimeout(this.resizeTimer);
	}
	this.resizeTimer = window.setTimeout('whf_columnviews[' + this.id + '].draw()', 40);
}

ColumnView.prototype.draw = function(redrawItems) {
	if(this.redrawing) {
		return;
	}
	this.drawing = true;

	totalHeight = this.container.offsetHeight;
	totalHeight = totalHeight - 23;	// Scrollbar

	itemsPerColumn = Math.floor(totalHeight / this.itemHeight);
	minVisibleColumns = Math.floor(this.container.offsetWidth / (this.minColumnWidth + this.columnPadding));

	if(redrawItems == false && itemsPerColumn == this.itemsPerColumn && minVisibleColumns == this.minVisibleColumns) {
		return;
	}

	if(redrawItems != true) {
		for(i = 0; i < this.items.length; i++) {
			this.items[i] = document.getElementById(this.container_id + '_option' + i).innerHTML;
		}
	}

	this.itemsPerColumn = itemsPerColumn;
	this.minVisibleColumns = minVisibleColumns;
	this.container.innerHTML = '<table cellspacing="0" cellpadding="0"><tr id="' + this.tr_id + '"></tr></table>';

	this.numColumns = Math.ceil(this.items.length / this.itemsPerColumn);
	if(this.minVisibleColumns > this.numColumns) {
		if(this.numColumns > 0) {
			columnWidth = (this.container.offsetWidth / this.numColumns) - this.columnPadding;
		} else {
			columnWidth = 120;
		}
	} else {
		if(this.minVisibleColumns > 0) {
			columnWidth = (this.container.offsetWidth / this.minVisibleColumns) - this.columnPadding;
		} else {
			columnWidth = 120;
		}
	}

	curColumn = 0;
	curRow = 0;
	column = new Array();
	opt = new Array();
	for(i = 1; i <= this.items.length; i++) {
		if(curRow == 0) {
			curColumn++;
			column[curColumn] = document.createElement('td');
			column[curColumn].className = this.columnClassName;
			column[curColumn].style.width = columnWidth;
			column[curColumn].style.verticalAlign = 'top';
		}

		opt[i] = document.createElement('div');
		opt[i].id = this.container_id + '_option' + (i - 1);
		opt[i].className = this.itemClassName;
		opt[i].style.width = columnWidth;
		opt[i].style.height = this.itemHeight +'px';
		opt[i].innerHTML = this.items[i - 1];
		column[curColumn].appendChild(opt[i]);

		curRow++;
		if(curRow >= this.itemsPerColumn) {
			curRow = 0;
			document.getElementById(this.tr_id).appendChild(column[curColumn]);
		}
	}
	if(curRow > 0) {
		document.getElementById(this.tr_id).appendChild(column[curColumn]);
	}

	this.drawing = false;
}



/* Masks */

function whf_newMask(id, mask) {
	if(mask.length == 0) {
		return;
	}
	nr = whf_masks.length;
	whf_masks[nr] = new whf_Mask(nr, id, mask);
}

function whf_Mask_onkeydown(obj) {
	nr = obj.getAttribute('maskNr');
	return whf_masks[nr].onkeydown();
}

function whf_Mask_onkeyup(obj) {
	nr = obj.getAttribute('maskNr');
	return whf_masks[nr].onkeyup();
}

function whf_Mask_onkeypress(obj) {
	nr = obj.getAttribute('maskNr');
	return whf_masks[nr].onkeypress();
}

function whf_Mask_onfocus(obj) {
	nr = obj.getAttribute('maskNr');
	return whf_masks[nr].onfocus();
}

function whf_Mask_onclick(obj) {
	nr = obj.getAttribute('maskNr');
	return whf_masks[nr].onclick();
}

function whf_Mask_onpaste(obj) {
	nr = obj.getAttribute('maskNr');
	return whf_masks[nr].onpaste();
}


function whf_Mask(nr, id, mask) {
	this.nr = nr;
	this.id = id;
	this.mask = mask;
	this.value = '';
	this.field = document.getElementById(this.id);
	this.field.setAttribute('maskNr', this.nr);
	if(!this.field.className) {
		this.field.style.fontFamily = 'Courier New, courier';
		this.field.style.fontSize = '9pt';
	}
	if(this.field.size == 20 && this.mask.length != 0) {
		this.field.size = this.mask.length;
	}
	this.maxLength = this.mask.replace(/[^#Xx]/g, '').length;
	this.field.maxLength = this.mask.length;
	this.caretPos = 0;
	this.inputPos = 0;

	oldValue = this.field.value;
	this.field.value = '';
	this.redraw(false);

	if(oldValue != '') {
		this.handleInput(oldValue);
	}

	if(!this.field.onkeydown) {
		this.field.onkeydown = function() { return whf_Mask_onkeydown(this); }
	} else {
		var tmp = this.field.onkeydown;
		this.field.onkeydown = function() { ret1 = whf_Mask_onkeydown(this); ret2 = tmp(); return(ret1 && ret2);}
	}
	if(!this.field.onkeyup) {
		this.field.onkeyup = function() { return whf_Mask_onkeyup(this); }
	} else {
		var tmp = this.field.onkeyup;
		this.field.onkeyup = function() { ret1 = whf_Mask_onkeyup(this); ret2 = tmp(); return(ret1 && ret2); }
	}
	if(!this.field.onkeypress) {
		this.field.onkeypress = function() { return whf_Mask_onkeypress(this); }
	} else {
		var tmp = this.field.onkeypress;
		this.field.onkeypress = function() { ret1 = whf_Mask_onkeypress(this); ret2 = tmp(); return(ret1 && ret2); }
	}
	if(!this.field.onfocus) {
		this.field.onfocus = function() { return whf_Mask_onfocus(this); }
	} else {
		var tmp = this.field.onfocus;
		this.field.onfocus = function() { ret1 = whf_Mask_onfocus(this); ret2 = tmp(); return(ret1 && ret2); }
	}
	if(!this.field.onclick) {
		this.field.onclick = function() { return whf_Mask_onclick(this); }
	} else {
		var tmp = this.field.onclick;
		this.field.onclick = function() { ret1 = whf_Mask_onclick(this); ret2 = tmp(); return(ret1 && ret2); }
	}
	if(!this.field.onpaste) {
		this.field.onpaste = function() { return whf_Mask_onpaste(this); }
	} else {
		var tmp = this.field.onpaste;
		this.field.onpaste = function() { ret1 = whf_Mask_onpaste(this); ret2 = tmp(); return(ret1 && ret2); }
	}
}

whf_Mask.prototype.getCaretPos = function() {
	this.caretPos = whf_getSelectionStart(this.field);
}

whf_Mask.prototype.calcInputPos = function() {
	this.inputPos = 0;
	moveLeft = 0;
	for(i = 0; i < this.caretPos; i++) {
		if(this.inputPos < this.value.length) {
			if(this.isSpecialMaskChar(i)) {
				this.inputPos++;
			}
		} else {
			moveLeft++;
		}
	}

	if((this.caretPos - moveLeft) < this.mask.length && !this.isSpecialMaskChar(this.caretPos - moveLeft)) {
		moveLeft--;
	}

	if(moveLeft > 0) {
		this.moveLeft(moveLeft);
	}
	if(moveLeft < 0) {
		this.moveRight(-moveLeft);
	}
}

whf_Mask.prototype.isSpecialMaskChar = function(pos) {
	maskChar = this.mask.charAt(pos);
	if(maskChar == 'X' || maskChar == 'x' || maskChar == '#') {
		return true;
	} else {
		return false;
	}
}


/* Look if we should move the caret to the right */
whf_Mask.prototype.checkMoveRight = function() {
	moveRight = 0;
	while(this.caretPos + moveRight < this.mask.length && !this.isSpecialMaskChar(this.caretPos + moveRight)) {
		moveRight++;
	}

	if(moveRight > 0) {
		this.moveRight(moveRight);
	}
}

whf_Mask.prototype.redraw = function(setCaret) {
	s = '';
	j = 0;
	for(i = 0; i < this.mask.length; i++) {
		if(!this.isSpecialMaskChar(i)) {
			s += this.mask.charAt(i);
		} else {
			if(j >= this.value.length) {
				s += ' ';
			} else {
				s += this.value.charAt(j);
				j++;
			}
		}
	}
	this.field.value = s;

	// Reposition caret
	if(setCaret) {
		window.setTimeout('whf_masks[' + this.nr + '].moveTo(' + this.caretPos + ')', 5);
	}
}

whf_Mask.prototype.handleInput = function(input) {
	if(input.length == 0) {
		return;
	}

	var redraw = false;
	seltext = whf_getSelectedText(this.field);
	if(seltext != '') {
		if(seltext.length == this.mask.length) {
			this.value = '';
			redraw = true;
		} else {
			// We can't handle partly selections yet
			return;
		}
	}


	s = '';
	pos = this.caretPos;
	for(i = 0; i < input.length; i++) {
		inputChar = input.charAt(i);
		while(pos < this.mask.length && !this.isSpecialMaskChar(pos)) {
			pos++;
		}
		if(pos < this.mask.length) {
			maskChar = this.mask.charAt(pos);
			switch(maskChar) {
				case 'X':
					inputChar = inputChar.toUpperCase();
					if( (inputChar >= 'A' && inputChar <= 'Z') ||
						 (inputChar >= '0' && inputChar <= '9') ) {
						s += inputChar;
						pos++;
					}
					break;
				case 'x':
					if( (inputChar >= 'A' && inputChar <= 'Z') ||
						 (inputChar >= 'a' && inputChar <= 'z') ||
						 (inputChar >= '0' && inputChar <= '9') ) {
						pos++;
						s += inputChar;
					}
					break;
				case '#':
					if(inputChar >= '0' && inputChar <= '9') {
						s += inputChar;
						pos++;
					}
					break;
			}
		}
	}
	this.insertChar(s);

	if(redraw) {
		this.redraw(true);
	}
}

whf_Mask.prototype.handleBackspace = function() {
	seltext = whf_getSelectedText(this.field);
	if(seltext != '') {
		if(seltext.length == this.mask.length) {
			if(this.value != '') {
				this.value = '';
				this.redraw(true);
			} else {
				this.moveHome();
			}
			return;
		} else {
			// We can't handle partly selections yet
			return;
		}
	}

	if(this.value != '') {
		if(!this.isSpecialMaskChar(this.caretPos - 1)) {
			this.caretPos = this.caretPos - 2;
		} else {
			this.caretPos--;
		}
		this.value = this.value.substring(0, this.inputPos - 1) + this.value.substring(this.inputPos, this.value.length);
		this.redraw(true);
	} else {
		this.moveHome();
	}
}

whf_Mask.prototype.handleDelete = function() {
	seltext = whf_getSelectedText(this.field);
	if(seltext != '') {
		if(seltext.length == this.mask.length) {
			this.value = '';
			this.redraw(true);
			return;
		} else {
			// We can't handle partly selections yet
			return;
		}
	}

	this.value = this.value.substring(0, this.inputPos) + this.value.substring(this.inputPos + 1, this.value.length);
	this.redraw(true);
}

whf_Mask.prototype.insertChar = function(input) {
	if(input == '') {
		return;
	}

	if(this.value.length + input.length > this.maxLength) {
		return;
	}

	redraw = false;

	if(this.inputPos == this.value.length) {
		this.value += input;
	} else {
		this.value = this.value.substring(0, this.inputPos) + input + this.value.substring(this.inputPos, this.value.length);
		redraw = true;
	}
	this.inputPos += input.length;

	if(input.length > 1) {
		this.caretPos = this.mask.length;
		redraw = true;
	} else {
		r = document.selection.createRange();
		r.moveEnd('character', 1);
		r.text = input;
		r.select();

		this.caretPos++;
		this.checkMoveRight();
	}
	if(redraw) {
		this.redraw(true);
	}
}

whf_Mask.prototype.moveRight = function(amount) {
	if(amount == null) {
		amount = 1;
	}
	if(amount == 0) {
		return;
	}

	this.caretPos += amount;

	r = this.field.createTextRange();
	r.move('character', this.caretPos);
	r.select();

	this.calcInputPos();
}

whf_Mask.prototype.moveLeft = function(amount) {
	if(amount == null) {
		amount = 1;
	}
	if(amount == 0) {
		return;
	}

	this.caretPos -= amount;
	if(this.caretPos < 0) {
		this.caretPos = 0;
	}

	r = this.field.createTextRange();
	r.move('character', this.caretPos);
	r.select();

	this.calcInputPos();
}

whf_Mask.prototype.moveHome = function() {
	r = this.field.createTextRange();
	r.move('character', 0);
	r.select();

	this.caretPos = 0;
	this.inputPos = 0;
}

whf_Mask.prototype.moveEnd = function() {
	r = this.field.createTextRange();
	r.move('character', this.mask.length);
	this.caretPos = this.mask.length;
	this.calcInputPos();
}

whf_Mask.prototype.moveTo = function(pos) {
	r = this.field.createTextRange();
	r.move('character', -999999);
	r.move('character', pos);
	r.select();

	this.caretPos = pos;
	this.calcInputPos();
}


whf_Mask.prototype.onkeydown = function() {
	keyCode = event.keyCode;

	if(event.ctrlKey || event.altKey) {
		return true;
	}

	switch(keyCode) {
		case 8:
			// Backspace
			this.handleBackspace();
			return false;

		case 27:
			// Escape
			this.value = '';
			this.redraw(true);
			return false;

		case 33:
			// Pageup
			this.moveHome();
			return false;

		case 34:
			// Pagedown
			this.moveEnd();
			return false;

		case 35:
			// End
			this.moveEnd();
			return false;

		case 36:
			// Home
			this.moveHome();
			return false;

		case 37:
			// Move left
			moveLeft = 1;
			while(this.caretPos - moveLeft >= 0 && !this.isSpecialMaskChar(this.caretPos - moveLeft)) {
				moveLeft++;
			}
			this.moveLeft(moveLeft);
			return false;

		case 38:
			// Move up
			this.moveHome();
			return false;

		case 39:
			// Move right
			this.moveRight(1);
			return false;

		case 40:
			// Move down
			this.moveEnd();
			return false;

		case 46:
			// Delete
			this.handleDelete();
			return false;

		default:
			break;
	}

	return true;
}

whf_Mask.prototype.onkeyup = function() {
	return true;
}

whf_Mask.prototype.onkeypress = function() {
	keyCode = event.keyCode;
	inputChar = String.fromCharCode(keyCode);
	this.handleInput(inputChar);
	return false;
}

whf_Mask.prototype.onfocus = function() {
	this.getCaretPos();
	this.calcInputPos();
	return true;
}

whf_Mask.prototype.onclick = function() {
	this.getCaretPos();
	this.calcInputPos();
	return true;
}

whf_Mask.prototype.onpaste = function() {
	pasteData = window.clipboardData.getData('Text');
	this.handleInput(pasteData);
	return false;
}

/**
 * util_addToFavorites -- add the current page as a bookmark
 *
 * TODO: safari, opera, konqueror, ...
 *
 * Known bugs: firefox adds a 'open in side panel' option by default, probably from version 1.5.0.1 on
 */
function util_addToFavorites() {
	title = document.title;
	url = document.location.href;

	if(window.external) { // IE Favorite
		window.external.AddFavorite(url, title);
	} else if(window.sidebar) { // Mozilla/Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	} else if(window.opera && window.print) { // Opera Hotlist
		return true;
	}
}


/**
 *  util_showFb triggers a floating box to show 
 *  howto: Create a division like:
 * 	<DIV id="[name]">
 * 	</DIV>

 * Usage
 * onmouseOver="util_showFb('[title]','[content]');" onmouseOut="util_hideFb();"
 */


function util_showFb(element,type){
		

		floatingBoxId = 'errmsg'+  element.id.substr(type.length);
		if (whe_visibleDiv != floatingBoxId  && (document.getElementById(floatingBoxId) != undefined)){


//			divHeight = document.getElementById(floatingBoxId).style.pixelHeight;//.substr(0,document.getElementById(floatingBoxId).style.height.indexOf("px"));
//			document.getElementById('fbTitle').innerHTML = title;
//			document.getElementById('fbContent').innerHTML = content;
//			fContent = title+content;
			document.getElementById('whe_error_root').style.display='inline';
			document.getElementById('whe_error_root').style.visibility='inherit';
			document.getElementById(floatingBoxId).style.display='inline';
			document.getElementById(floatingBoxId).style.visibility='inherit';
//			for (st in document.getElementById(floatingBoxId).style){
//				alert(st+" "+document.getElementById(floatingBoxId).style[st]);
//			}
			whe_divWidth = document.getElementById(floatingBoxId).style;
			whe_visibleDiv = floatingBoxId;
		}
	return;	
}

function util_hideFb(){

	if (whe_visibleDiv != -1) {
		document.getElementById('whe_error_root').style.display='none';
		document.getElementById('whe_error_root').style.visibility='hidden';
		document.getElementById(whe_visibleDiv).style.display='none';
		document.getElementById(whe_visibleDiv).style.visibility='hidden';
		whe_visibleDiv = -1;
	}

	return;
}


function util_createFloatingDiv(id) {
	if (typeof(whf_floatingDivStyle) == 'undefined'){
		_style = "border:1px solid black;border-color:#000;background-color:#fff;padding:2px;";
	} else {
		_style = whf_floatingDivStyle;
	}
	document.getElementById('whe_error_root').innerHTML = document.getElementById('whe_error_root').innerHTML + '<div id="'+id+'" class="whf_Floatbox" style="position:absolute;z-index:1000;visibility:hidden;display:none;width:'+whf_errorFloatBoxWidth+'px;'+_style+'"></div>\n';
}


/** MouseHandler
 *  Receives a handle from any mousemovement in the document, moves whe_visibleDiv accordingly (if set)
**/

function whe_getMouseXY(e) {

	// Div movement
	if (whe_visibleDiv != -1){

		if (whe_IE) {
			var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
			tempX = event.x + document.body.scrollLeft;
			tempY = event.y + document.body.scrollTop;
			mousePosX = event.clientX;
			mousePosY = event.clientY;
			winW = iebody.clientWidth;
			winH = iebody.clientHeight;
//		document.getElementById('fbTitle').innerHTML = winH;M
		} else {

			tempX =  e.layerX;
			tempY =  e.layerY;
			winW = window.innerWidth;
	 		winH = window.innerHeight;

			mousePosX = e.clientX;
			mousePosY = e.clientY;
		}
     
		if (tempX <0) {tempX = 0;}
		if (tempY <0) {tempY = 0;}
 
	 	// determine alternative position:
	
		if(((parseInt(mousePosX)+15)+(whf_errorFloatBoxWidth)) > winW){
			tempX = tempX - whf_errorFloatBoxWidth-30;
		} 

		if(((parseInt(mousePosY)+100)) > winH){
			tempY = tempY -120;
		} 

		document.getElementById(whe_visibleDiv).style.left = (tempX+15)+'px';
		document.getElementById(whe_visibleDiv).style.top = (tempY+15)+'px';
	}
 
	if (whe_oldhandler.length >0){
		whe_oldhandler(e);

	}

	return;
} // eof mousemove

function whf_addOnBlur(id,handler,addBeforeFirst){

	if(whf_fields[id]['onblur'] == undefined){
		whf_fields[id]['onblur'] = new Array(handler);
	}else if(addBeforeFirst){
		tmp = whf_fields[id]['onblur'];
		whf_fields[id]['onblur'] = new Array(handler);
		for(teller =0;teller < tmp.length;teller++){
			whf_fields[id]['onblur'].push(tmp[teller]);
		}
	} else {
		whf_fields[id]['onblur'].push(handler);
	}

	onblurCaption = '';
	for (index in whf_fields[id]['onblur']){
		 onblurCaption = onblurCaption + whf_fields[id]['onblur'][index]+'; ';
	}
	whf_fields[id]['_onblurcaption']= onblurCaption;
	if (!(whf_fields[id]['obj'] == undefined)){
		whf_fields[id]['obj'].onblur = function(){eval(whf_fields[this.id]['_onblurcaption']);};
	}

}

function whf_fixTime(field) {
	if(field.value.length == 0) {
		return;
	}
	var id = field.id;
	var split = field.value.split(':');
	var i;
	for(i = 0; (whf_fields[id]['with_seconds']?3:2)>i; i++) {
		if((typeof split[i])=='undefined') {
			split[i] = '00';
		}
		split[i] = split[i].replace(/ +/,'');
		if(split[i].length == 0) {
			split[i] = '00';
		}
		if(isNaN(split[i])) {
			return;
		}
		while(split[i].length < 2) {
			split[i] = '0' + split[i];
		}
	}
	field.value=split.join(':');
}


function whf_setSelectionRange(input, start, end) {
	input.setSelectionRange(start, end);
};

function whf_getSelectedText(input) {
	startpos = whf_getSelectionStart(input);
	endpos = whf_getSelectionEnd(input);
	if(startpos == endpos) {
		return '';
	}
	return input.value.substr(startpos, endpos - startpos);
}

function whf_getSelectionStart(input) {
	return input.selectionStart;
};

function whf_getSelectionEnd(input) {
	return input.selectionEnd;
};



whf_ajax = {};
whf_ajax = function(url, callback) {
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			this.xmlHttp = false;
		}
	}
	@end @*/
	if (!this.xmlHttp && typeof XMLHttpRequest != 'undefined') {
		this.xmlHttp = new XMLHttpRequest();
	}

	if(callback) {
		var self=this;
		this.xmlHttp.onreadystatechange = function() { self.handler(); };
		this.callback=callback;
		this.async=true;
	}
	else {
		this.async=false;
	}
	this.url=url;
}
whf_ajax.prototype = {
	xmlHttp: null,
	callback: null,
	url: null,
	method: 'GET',
	async: true,
	postdata: null
};
whf_ajax.prototype.handler = function () {
	if(!this.xmlHttp) {
		return;
	}
	if(this.xmlHttp.readyState == 4) {
		this.doCallback();
	}
}
whf_ajax.prototype.doCallback = function () {
	this.callback(this.xmlHttp);
}
whf_ajax.prototype.setCallback = function (callback) {
	this.callback=callback;
}
whf_ajax.prototype.setRealCallback = function (callback) {
	this.xmlHttp.onreadystatechange=callback;
}
whf_ajax.prototype.setPostData = function (postdata) {
	this.postdata=postdata;
	this.method='POST';
}
whf_ajax.prototype.setAsync = function (async) {
	this.async=async;
}
whf_ajax.prototype.setMethod = function (method) {
	this.method=method;
}
whf_ajax.prototype.send = function () {
	this.xmlHttp.open(this.method, this.url, this.async);
	this.xmlHttp.send(this.postdata);
	if(!this.async && this.callback)
		this.doCallback();
	else if(!this.async)
		return this.xmlHttp.responseText;
}

// Start of MD5 functionality

function whe_array(n) {
  for(i=0;i<n;i++) this[i]=0;
  this.length=n;
}

function whe_integer(n) { return n%(0xffffffff+1); }

function whe_shr(a,b) {
  a=whe_integer(a);
  b=whe_integer(b);
  if (a-0x80000000>=0) {
    a=a%0x80000000;
    a>>=b;
    a+=0x40000000>>(b-1);
  } else
    a>>=b;
  return a;
}

function whe_shl1(a) {
  a=a%0x80000000;
  if (a&0x40000000==0x40000000)
  {
    a-=0x40000000;  
    a*=2;
    a+=0x80000000;
  } else
    a*=2;
  return a;
}

function whe_shl(a,b) {
  a=whe_integer(a);
  b=whe_integer(b);
  for (var i=0;i<b;i++) a=whe_shl1(a);
  return a;
}

function whe_and(a,b) {
  a=whe_integer(a);
  b=whe_integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0) 
    if (t2>=0) 
      return ((t1&t2)+0x80000000);
    else
      return (t1&b);
  else
    if (t2>=0)
      return (a&t2);
    else
      return (a&b);  
}

function whe_or(a,b) {
  a=whe_integer(a);
  b=whe_integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0) 
    if (t2>=0) 
      return ((t1|t2)+0x80000000);
    else
      return ((t1|b)+0x80000000);
  else
    if (t2>=0)
      return ((a|t2)+0x80000000);
    else
      return (a|b);  
}

function whe_xor(a,b) {
  a=whe_integer(a);
  b=whe_integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0) 
    if (t2>=0) 
      return (t1^t2);
    else
      return ((t1^b)+0x80000000);
  else
    if (t2>=0)
      return ((a^t2)+0x80000000);
    else
      return (a^b);  
}

function whe_not(a) {
  a=whe_integer(a);
  return (0xffffffff-a);
}


    function whe_md5_F(x,y,z) {
	return whe_or(whe_and(x,y),whe_and(whe_not(x),z));
    }

    function whe_md5_G(x,y,z) {
	return whe_or(whe_and(x,z),whe_and(y,whe_not(z)));
    }

    function whe_md5_H(x,y,z) {
	return whe_xor(whe_xor(x,y),z);
    }

    function whe_md5_I(x,y,z) {
	return whe_xor(y ,whe_or(x , whe_not(z)));
    }

    function whe_md5_rotateLeft(a,n) {
	return whe_or(whe_shl(a, n),(whe_shr(a,(32 - n))));
    }

    function whe_md5_FF(a,b,c,d,x,s,ac) {
        a = a+whe_md5_F(b, c, d) + x + ac;
	a = whe_md5_rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function whe_md5_GG(a,b,c,d,x,s,ac) {
	a = a+whe_md5_G(b, c, d) +x + ac;
	a = whe_md5_rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function whe_md5_HH(a,b,c,d,x,s,ac) {
	a = a+whe_md5_H(b, c, d) + x + ac;
	a = whe_md5_rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function whe_md5_II(a,b,c,d,x,s,ac) {
	a = a+whe_md5_I(b, c, d) + x + ac;
	a = whe_md5_rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function whe_md5_transform(buf,offset) { 
	var a=0, b=0, c=0, d=0; 
	var x = whe_md5_transformBuffer;
	
	a = whe_md5_state[0];
	b = whe_md5_state[1];
	c = whe_md5_state[2];
	d = whe_md5_state[3];
	
	for (i = 0; i < 16; i++) {
	    x[i] = whe_and(buf[i*4+offset],0xff);
	    for (j = 1; j < 4; j++) {
		x[i]+=whe_shl(whe_and(buf[i*4+j+offset] ,0xff), j * 8);
	    }
	}

	/* Round 1 */
	a = whe_md5_FF ( a, b, c, d, x[ 0], whe_md5_S11, 0xd76aa478); /* 1 */
	d = whe_md5_FF ( d, a, b, c, x[ 1], whe_md5_S12, 0xe8c7b756); /* 2 */
	c = whe_md5_FF ( c, d, a, b, x[ 2], whe_md5_S13, 0x242070db); /* 3 */
	b = whe_md5_FF ( b, c, d, a, x[ 3], whe_md5_S14, 0xc1bdceee); /* 4 */
	a = whe_md5_FF ( a, b, c, d, x[ 4], whe_md5_S11, 0xf57c0faf); /* 5 */
	d = whe_md5_FF ( d, a, b, c, x[ 5], whe_md5_S12, 0x4787c62a); /* 6 */
	c = whe_md5_FF ( c, d, a, b, x[ 6], whe_md5_S13, 0xa8304613); /* 7 */
	b = whe_md5_FF ( b, c, d, a, x[ 7], whe_md5_S14, 0xfd469501); /* 8 */
	a = whe_md5_FF ( a, b, c, d, x[ 8], whe_md5_S11, 0x698098d8); /* 9 */
	d = whe_md5_FF ( d, a, b, c, x[ 9], whe_md5_S12, 0x8b44f7af); /* 10 */
	c = whe_md5_FF ( c, d, a, b, x[10], whe_md5_S13, 0xffff5bb1); /* 11 */
	b = whe_md5_FF ( b, c, d, a, x[11], whe_md5_S14, 0x895cd7be); /* 12 */
	a = whe_md5_FF ( a, b, c, d, x[12], whe_md5_S11, 0x6b901122); /* 13 */
	d = whe_md5_FF ( d, a, b, c, x[13], whe_md5_S12, 0xfd987193); /* 14 */
	c = whe_md5_FF ( c, d, a, b, x[14], whe_md5_S13, 0xa679438e); /* 15 */
	b = whe_md5_FF ( b, c, d, a, x[15], whe_md5_S14, 0x49b40821); /* 16 */

	/* Round 2 */
	a = whe_md5_GG ( a, b, c, d, x[ 1], whe_md5_S21, 0xf61e2562); /* 17 */
	d = whe_md5_GG ( d, a, b, c, x[ 6], whe_md5_S22, 0xc040b340); /* 18 */
	c = whe_md5_GG ( c, d, a, b, x[11], whe_md5_S23, 0x265e5a51); /* 19 */
	b = whe_md5_GG ( b, c, d, a, x[ 0], whe_md5_S24, 0xe9b6c7aa); /* 20 */
	a = whe_md5_GG ( a, b, c, d, x[ 5], whe_md5_S21, 0xd62f105d); /* 21 */
	d = whe_md5_GG ( d, a, b, c, x[10], whe_md5_S22,  0x2441453); /* 22 */
	c = whe_md5_GG ( c, d, a, b, x[15], whe_md5_S23, 0xd8a1e681); /* 23 */
	b = whe_md5_GG ( b, c, d, a, x[ 4], whe_md5_S24, 0xe7d3fbc8); /* 24 */
	a = whe_md5_GG ( a, b, c, d, x[ 9], whe_md5_S21, 0x21e1cde6); /* 25 */
	d = whe_md5_GG ( d, a, b, c, x[14], whe_md5_S22, 0xc33707d6); /* 26 */
	c = whe_md5_GG ( c, d, a, b, x[ 3], whe_md5_S23, 0xf4d50d87); /* 27 */
	b = whe_md5_GG ( b, c, d, a, x[ 8], whe_md5_S24, 0x455a14ed); /* 28 */
	a = whe_md5_GG ( a, b, c, d, x[13], whe_md5_S21, 0xa9e3e905); /* 29 */
	d = whe_md5_GG ( d, a, b, c, x[ 2], whe_md5_S22, 0xfcefa3f8); /* 30 */
	c = whe_md5_GG ( c, d, a, b, x[ 7], whe_md5_S23, 0x676f02d9); /* 31 */
	b = whe_md5_GG ( b, c, d, a, x[12], whe_md5_S24, 0x8d2a4c8a); /* 32 */

	/* Round 3 */
	a = whe_md5_HH ( a, b, c, d, x[ 5], whe_md5_S31, 0xfffa3942); /* 33 */
	d = whe_md5_HH ( d, a, b, c, x[ 8], whe_md5_S32, 0x8771f681); /* 34 */
	c = whe_md5_HH ( c, d, a, b, x[11], whe_md5_S33, 0x6d9d6122); /* 35 */
	b = whe_md5_HH ( b, c, d, a, x[14], whe_md5_S34, 0xfde5380c); /* 36 */
	a = whe_md5_HH ( a, b, c, d, x[ 1], whe_md5_S31, 0xa4beea44); /* 37 */
	d = whe_md5_HH ( d, a, b, c, x[ 4], whe_md5_S32, 0x4bdecfa9); /* 38 */
	c = whe_md5_HH ( c, d, a, b, x[ 7], whe_md5_S33, 0xf6bb4b60); /* 39 */
	b = whe_md5_HH ( b, c, d, a, x[10], whe_md5_S34, 0xbebfbc70); /* 40 */
	a = whe_md5_HH ( a, b, c, d, x[13], whe_md5_S31, 0x289b7ec6); /* 41 */
	d = whe_md5_HH ( d, a, b, c, x[ 0], whe_md5_S32, 0xeaa127fa); /* 42 */
	c = whe_md5_HH ( c, d, a, b, x[ 3], whe_md5_S33, 0xd4ef3085); /* 43 */
	b = whe_md5_HH ( b, c, d, a, x[ 6], whe_md5_S34,  0x4881d05); /* 44 */
	a = whe_md5_HH ( a, b, c, d, x[ 9], whe_md5_S31, 0xd9d4d039); /* 45 */
	d = whe_md5_HH ( d, a, b, c, x[12], whe_md5_S32, 0xe6db99e5); /* 46 */
	c = whe_md5_HH ( c, d, a, b, x[15], whe_md5_S33, 0x1fa27cf8); /* 47 */
	b = whe_md5_HH ( b, c, d, a, x[ 2], whe_md5_S34, 0xc4ac5665); /* 48 */

	/* Round 4 */
	a = whe_md5_II ( a, b, c, d, x[ 0], whe_md5_S41, 0xf4292244); /* 49 */
	d = whe_md5_II ( d, a, b, c, x[ 7], whe_md5_S42, 0x432aff97); /* 50 */
	c = whe_md5_II ( c, d, a, b, x[14], whe_md5_S43, 0xab9423a7); /* 51 */
	b = whe_md5_II ( b, c, d, a, x[ 5], whe_md5_S44, 0xfc93a039); /* 52 */
	a = whe_md5_II ( a, b, c, d, x[12], whe_md5_S41, 0x655b59c3); /* 53 */
	d = whe_md5_II ( d, a, b, c, x[ 3], whe_md5_S42, 0x8f0ccc92); /* 54 */
	c = whe_md5_II ( c, d, a, b, x[10], whe_md5_S43, 0xffeff47d); /* 55 */
	b = whe_md5_II ( b, c, d, a, x[ 1], whe_md5_S44, 0x85845dd1); /* 56 */
	a = whe_md5_II ( a, b, c, d, x[ 8], whe_md5_S41, 0x6fa87e4f); /* 57 */
	d = whe_md5_II ( d, a, b, c, x[15], whe_md5_S42, 0xfe2ce6e0); /* 58 */
	c = whe_md5_II ( c, d, a, b, x[ 6], whe_md5_S43, 0xa3014314); /* 59 */
	b = whe_md5_II ( b, c, d, a, x[13], whe_md5_S44, 0x4e0811a1); /* 60 */
	a = whe_md5_II ( a, b, c, d, x[ 4], whe_md5_S41, 0xf7537e82); /* 61 */
	d = whe_md5_II ( d, a, b, c, x[11], whe_md5_S42, 0xbd3af235); /* 62 */
	c = whe_md5_II ( c, d, a, b, x[ 2], whe_md5_S43, 0x2ad7d2bb); /* 63 */
	b = whe_md5_II ( b, c, d, a, x[ 9], whe_md5_S44, 0xeb86d391); /* 64 */

	whe_md5_state[0] +=a;
	whe_md5_state[1] +=b;
	whe_md5_state[2] +=c;
	whe_md5_state[3] +=d;

    }

    function whe_md5_init() {
	whe_md5_count[0]=whe_md5_count[1] = 0;
	whe_md5_state[0] = 0x67452301;
	whe_md5_state[1] = 0xefcdab89;
	whe_md5_state[2] = 0x98badcfe;
	whe_md5_state[3] = 0x10325476;
	for (i = 0; i < whe_md5_digestBits.length; i++)
	    whe_md5_digestBits[i] = 0;
    }

    function whe_md5_update(b) { 
	var index,i;
	
	index = whe_and(whe_shr(whe_md5_count[0],3) , 0x3f);
	if (whe_md5_count[0]<0xffffffff-7) 
	  whe_md5_count[0] += 8;
        else {
	  whe_md5_count[1]++;
	  whe_md5_count[0]-=0xffffffff+1;
          whe_md5_count[0]+=8;
        }
	whe_md5_buffer[index] = whe_and(b,0xff);
	if (index  >= 63) {
	    whe_md5_transform(whe_md5_buffer, 0);
	}
    }

    function whe_md5_finish() {
	var bits = new whe_array(8);
	var	padding; 
	var	i=0, index=0, padLen=0;

	for (i = 0; i < 4; i++) {
	    bits[i] = whe_and(whe_shr(whe_md5_count[0],(i * 8)), 0xff);
	}
        for (i = 0; i < 4; i++) {
	    bits[i+4]=whe_and(whe_shr(whe_md5_count[1],(i * 8)), 0xff);
	}
	index = whe_and(whe_shr(whe_md5_count[0], 3) ,0x3f);
	padLen = (index < 56) ? (56 - index) : (120 - index);
	padding = new whe_array(64); 
	padding[0] = 0x80;
        for (i=0;i<padLen;i++){
		  whe_md5_update(padding[i]);
		}
        for (i=0;i<8;i++) {
		  whe_md5_update(bits[i]);
	 	}

	for (i = 0; i < 4; i++) {
	    for (j = 0; j < 4; j++) {
		whe_md5_digestBits[i*4+j] = whe_and(whe_shr(whe_md5_state[i], (j * 8)) , 0xff);
	    }
	} 
    }

/* End of the MD5 algorithm */

function whe_md5_hexa(n) {
 var hexa_h = "0123456789abcdef";
 var hexa_c=""; 
 var hexa_m=n;
 for (hexa_i=0;hexa_i<8;hexa_i++) {
   hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c;
   hexa_m=Math.floor(hexa_m/16);
 }
 return hexa_c;
}



function whe_MD5(entree) 
{
 var l,s,k,ka,kb,kc,kd;

 whe_md5_init();
 for (k=0;k<entree.length;k++) {
   l=entree.charAt(k);
   whe_md5_update(whe_md5_ascii.lastIndexOf(l));
 }
 whe_md5_finish();
 ka=kb=kc=kd=0;
 for (i=0;i<4;i++) ka+=whe_shl(whe_md5_digestBits[15-i], (i*8));
 for (i=4;i<8;i++) kb+=whe_shl(whe_md5_digestBits[15-i], ((i-4)*8));
 for (i=8;i<12;i++) kc+=whe_shl(whe_md5_digestBits[15-i], ((i-8)*8));
 for (i=12;i<16;i++) kd+=whe_shl(whe_md5_digestBits[15-i], ((i-12)*8));
 s=whe_md5_hexa(kd)+whe_md5_hexa(kc)+whe_md5_hexa(kb)+whe_md5_hexa(ka);
 return s; 
}
// End of MD5 functionality



