
var FDValidate = FD.Validate = {
	_mode: 0, // 0=return on error, 1=save or return on error
	_skip: false,
	_page: false
}

FDValidate.setMode = function(mode) {
	this._mode = mode;
}
FDValidate.Init = function(page) {
	try { this._mode = (FD.Form.Info.isAdmin && !FD.Form.Info.isTesting ? 1 : 0) && (typeof oWorkflow == 'undefined' || oWorkflow.isAdmin); } catch (e) { }
	this._page = page;
	this._skip = false;
}
FDValidate.CheckValue = function(vpEl, npType, npRange, cpMsg, vpFEl, opWnd, npPage, lpResetValue) {

	if (this._skip || (this._mode == 1 && this._page)) return true;

	var oWnd = (opWnd ? opWnd : (window.getElement ? window : top));
	var aEl = '', oEl = new Array(), oFEl = { el: null, focus: null };
	var lReturn = false, i = 0, lAlert = false, cType = '';

	if (typeof vpEl == 'string') {
		aEl = vpEl.split(',');
		vpEl = aEl[0];
	}
	oEl[0] = oWnd.getElement(document, vpEl);
	for (i = 1; i < aEl.length; i++)
		oEl[i] = oWnd.getElement(document, aEl[i]);


	if (oWnd.isObject(vpFEl))
		oFEl.el = oFEl.focus = oWnd.getElement(document, vpFEl);
	else
		oFEl.el = oFEl.focus = oEl[0];

	// check if it is a checkbox-/optiongroup and if it has an enabled option
	if (typeof oFEl.el.length != 'undefined' && (typeof oFEl.el.tagName == 'undefined' || oFEl.el.tagName != 'SELECT')) {
		lReturn = true;
		for (i = 0; i < oFEl.el.length; i++) {
			lReturn = lReturn && (oFEl.el[i].disabled || oFEl.el[i].readOnly);
			if (!lReturn) { // there is an enabled option
				oFEl.focus = oFEl.el[i];
				break;
			}
		}
	} else {
		lReturn = oFEl.el.disabled || oFEl.el.readOnly || (typeof oFEl.el.style != 'undefined' && oFEl.el.style.display == 'none'); //&& !FD.Form.Hidden.Check(aFields.get(oFEl.el.name))
	}

	if (npPage && npPage > 0) {
		//var nDisplay = getElement( oWnd.document, 'Page' + npPage ).getAttribute( 'display' ) ;
		lReturn = lReturn || (typeof FDPages != 'undefined' && !FDPages.checkDisplay(npPage)); //( !isEmpty( nDisplay ) && !parseInt( nDisplay ) ) ;
	}

	var oFld = null;
	if (typeof aFields != 'undefined') {
		oFld = aFields.search(oFEl.focus.name);
		lReturn = lReturn || (oFld && FD.Form.Hidden.Check(oFld.id));
	}
	if (typeof oWorkflow != 'undefined')
		lReturn = lReturn || (oFld && oWorkflow.check(oFld.id, true));

	if (lReturn) return true;

	switch (npType) {
		case 1:
			{ // required
				if (oEl[0].type && oEl[0].type == 'checkbox')
					lAlert = !oEl[0].checked;
				else if (typeof oEl[0][0] != 'undefined' && typeof oEl[0].tagName == 'undefined') { // chg-/cbg-group
					var nChecked = 0;
					for (i = 0; i < oEl[0].length; i++) {
						if (oEl[0][i].checked) nChecked++;
					}
					lAlert = nChecked == 0;
				} else {
					lAlert = oWnd.isEmpty(oEl[0].value);
				}
				cType = FD.Texts.Validate.get('REQUIRED');
				break;
			}
		case 2:
			{ // isnumber
				lAlert = (isNaN(oEl[0].value)) ? true : false;
				cType = FD.Texts.Validate.get('NUMERIC');
				break;
			}
		case 3:
			{ // minwaarde
				lAlert = (oEl[0].value.length > 0 && String.create(oEl[0].value).float() < npRange) ? true : false;
				cType = FD.Texts.Validate.get('NUMERIC');
				break;
			}
		case 4:
			{ // maxwaarde
				lAlert = (oEl[0].value.length > 0 && String.create(oEl[0].value).float() > npRange) ? true : false;
				cType = FD.Texts.Validate.get('NUMERIC');
				break;
			}
		case 5:
			{ // geldige datum invoer
				var cDates = oEl[0].value;
				if (oEl.length > 1)
					cDates = cDates + ',' + oEl[1].value;
				var nRetVal = oWnd.FD.Date.Validate(cDates, FD.Texts.get('DATE_FORMAT'), npRange);

				if (nRetVal == -1) cType = FD.Texts.Validate.get('DATE_FORMAT') + ': ' + FD.Texts.get('DATE_FORMAT') + '.';
				else if (nRetVal == -4) cType = FD.Texts.Validate.get('DATE_PAST');
				else if (nRetVal == -5) cType = FD.Texts.Validate.get('DATE_FUTURE');
				else if (nRetVal == -2) cType = FD.Texts.Validate.get('DATE_PAST_PRESENT');
				else if (nRetVal == -3) cType = FD.Texts.Validate.get('DATE_FUTURE_PRESENT');
				lAlert = nRetVal < 0;
				break;
			}
		case 6:
			{ // geldige tekens
				var nRetVal = oWnd.CheckChars(oEl[0].value, 3, npRange); // npRange kan een eigen array met geldige charcodes zijn
				if (nRetVal != -1) {
					cType = FD.Texts.Validate.get('CHARACTER') + (nRetVal + 1) + '.';
					lAlert = true;
				} else {
					var cValue = oEl[0].value.toLowerCase();
					if (cValue.search('\\.com') > -1 || cValue.search('\\.htm') > -1 || cValue.search('\\.html') > -1) {
						cType = FD.Texts.Validate.get('DOT_COM');
						lAlert = true;
					}
				}
				break;
			}
		case 7:
			{ // eerste positie een spatie
				if (oEl[0].value.charAt(0) == ' ') {
					cType = FD.Texts.Validate.get('CHARACTER') + '1.';
					lAlert = true;
				}
				break;
			}
		case 8:
			{ // minwaarde checkboxgroup/optiongroup
				var nChecked = 0;
				for (i = 0; i < oEl[0].length; i++) {
					if (oEl[0][i].checked) nChecked++;
				}
				lAlert = (oEl[0].length && nChecked < npRange) || (!oEl[0].length && !oEl[0].checked);
				if (oEl[0].length > 1)
					cType = FD.Texts.Validate.get('MIN_VALUE') + npRange + FD.Texts.Validate.get('SELECT_OPTIONS');
				else
					cType = FD.Texts.Validate.get('SINGLE_VALUE');
				break;
			}
		case 9:
			{ // maxwaarde checkboxgroup
				var nChecked = 0;
				for (i = 0; i < oEl[0].length; i++) {
					if (oEl[0][i].checked) nChecked++;
				}
				lAlert = nChecked > npRange;
				cType = FD.Texts.Validate.get('MAX_VALUE') + npRange + FD.Texts.Validate.get('SELECT_OPTIONS');
				break;
			}
		case 10:
			{ // minimaal aantal opties( chg, cbo, opg )
				var cGroup = escape(oEl[0].value);
				var aGroup = cGroup.split(splitChar(cGroup, true));
				lAlert = (aGroup.length < npRange);
				if (aGroup.length == npRange) lAlert = emptyStr(aGroup[npRange - 1]);
				cType = FD.Texts.Validate.get('MIN_OPTIONS');
				break;
			}
		case 11:
			{ // email-adres
				var nRetVal = oWnd.emailCheck(oEl[0].value);
				if (nRetVal < 0) cType = FD.Texts.Validate.get('EMAIL');
				else if (nRetVal > 0) cType = FD.Texts.Validate.get('EMAIL_CHARACTER') + nRetVal + '.';
				lAlert = nRetVal != 0;
				break;
			}
		case 12:
			{ // minwaarde listbox
				var nSelected = 0;
				for (i = 0; i < oEl[0].length; i++) {
					if (oEl[0][i].selected) nSelected++;
				}
				lAlert = nSelected < npRange;
				cType = FD.Texts.Validate.get('MIN_VALUE') + npRange + FD.Texts.Validate.get('SELECT_OPTIONS');
				break;
			}
		case 13:
			{ // alternatieve optie
				if (typeof oEl[0][0] != 'undefined')
					lAlert = (oEl[0][oEl[0].length - 1].checked && CheckEmpty(oEl[1].value));
				else
					lAlert = (oEl[0].checked && CheckEmpty(oEl[1].value));
				oFEl.focus = oEl[1];
				cType = FD.Texts.Validate.get('ALTERNATIVE');
				break;
			}
		case 14:
			{ // volgens url-standaard
				lAlert = oEl[0].value != oWnd.URLEncode(oEl[0].value);
				cType = FD.Texts.Validate.get('URL');
				break;
			}
		case 15:
			{ // combo
				var cValue = (oEl[0].selectedIndex > -1 ? oEl[0].options[oEl[0].selectedIndex].value : '');
				lAlert = (npRange == 0 && oEl[0].selectedIndex == 0) || (npRange == 1 && cValue == '##') || cValue == '';
				cType = FD.Texts.Validate.get('REQUIRED');
				break;
			}
		case 16:
			{ // file
				var cValue = oEl[0].value, aExt = cValue.match((oEl[0].getAttribute('fdparam') || oEl[0].getAttribute('fd:param') ? /(\..{2,4})(\?.*)?$/ : /(\..{2,4})$/));
				var cExt = oEl[0].getAttribute('fdaccept');
				if (cExt == null) cExt = oEl[0].getAttribute('fd:accept');
				if (aExt != null) aExt[1] = aExt[1].toUpperCase();
				if (cExt) cExt = cExt.toUpperCase();
				//alert(cExt + '; ' + aExt);
				lAlert = oEl[0].value.length > 0 && (aExt == null || (!isEmpty(cExt) && (',' + cExt + ',').match(',' + aExt[1] + ',') == null));
				cType = FD.Texts.Validate.get('FILE_EXTENSION') + (!isEmpty(cExt) ? cExt : '.*');
			}
	}

	if (lAlert) {
		return this.Alert(cpMsg, cType, oFEl, oWnd, npPage, lpResetValue);
	}

	return true;

}
function CheckValue(vpEl, npType, npRange, cpMsg, vpFEl, opWnd, npPage, lpResetValue) {
	//FD.Debug.depricated(CheckValue, 'depricated! use FDValidate.CheckValue instead');
	return FDValidate.CheckValue(vpEl, npType, npRange, cpMsg, vpFEl, opWnd, npPage, lpResetValue);
}

FDValidate.Alert = function(cpMsg, cpAlert, opFEl, opWnd, npPage, lpResetValue) {
	var retval = false;

	if (opFEl && !opFEl.el) opFEl = { el: opFEl, focus: opFEl };
	if (lpResetValue && typeof opFEl.el.oldValue != 'undefined') {
		opFEl.el.value = opFEl.el.oldValue;
	}
	var oWnd = (opWnd ? opWnd : (window.getElement ? window : top));
	var cMsg = FD.Texts.Validate.get('INVALID_INPUT');
	if (typeof cpMsg == 'string' && cpMsg != '') {
		cpMsg = unescape(cpMsg).stripHTML();
		cMsg = cMsg + '\r\n' + FD.Texts.Validate.get('AT_QUESTION') + (cpMsg.length > 80 ? (cpMsg.substr(0, 77) + '...') : cpMsg);
	}
	if (typeof cpAlert == 'string' && cpAlert != '') cMsg = cMsg + '%0A%0A' + cpAlert;
	if (typeof oProgress != 'undefined') oProgress.finish(0);
	this.Select(opFEl, oWnd, npPage);
	if (!Browser.ie && !Browser.camino) FDBlink.check(opWnd, opFEl);

	if (this._mode == 0) {
		alert(unescape(cMsg));
	} else if (this._mode == 1) {
		retval = confirm(unescape(cMsg + '\r\n\r\n' + FD.Texts.Validate.get('CONTINUE_SAVE')));
		if (retval) this._skip = true;
	}
	
	if (!retval && (Browser.ie || Browser.camino)) FDBlink.check(opWnd, opFEl);
	FDBlink.lmt = 5;

	return retval;
}
function CheckAlert(cpMsg, cpAlert, opFEl, opWnd, npPage, lpResetValue) {
	//FD.Debug.depricated(CheckAlert, 'depricated! use FDValidate.Alert instead');
	return FDValidate.Alert(cpMsg, cpAlert, opFEl, opWnd, npPage, lpResetValue);
}

FDValidate.Select = function(opFEl, opWnd, npPage) {
	if (typeof npPage == 'number' && npPage > 0) {
		if (typeof opWnd.FDPages != 'undefined') opWnd.FDPages.GoTo(npPage, true);
		else if (typeof opWnd.switchPanel != 'undefined') opWnd.switchPanel(npPage, true);
		else if (typeof opWnd.switchTab != 'undefined') opWnd.switchTab(npPage);
	}
	if (opFEl) {
		var oEl = opFEl.focus;
		if (typeof oEl.length != 'undefined' && (typeof oEl.tagName == 'undefined' || oEl.tagName != 'SELECT')) oEl = oEl[0];
		try { opWnd.Select('', oEl, true); } catch (e) { };
		FDEvents.FireEvent('ValidateAfterSelect');
	}
	return false;
}
function CheckSelect(opFEl, opWnd, npPage) {
	//FD.Debug.depricated(CheckSelect, 'depricated! use FDValidate.Select instead');
	return FDValidate.Select(opFEl, opWnd, npPage);
}

var FDBlink = { wnd: null, focus: null, el: null, tmr: 0, cnt: 0, lmt: 0 };
FDBlink.exec = function() {
	window.clearTimeout(this.tmr);
	this.tmr = 0;
	if (this.lmt == 0 || this.cnt < this.lmt) {
		this.tmr = this.exec.delay(this, 350); // window.setTimeout('FDBlink.exec();', 350);
		this.show(this.el[0].style.visibility == 'hidden');
	} else {
		this.stop();
	}
	if (this.lmt > 0) this.cnt++;
}
FDBlink.show = function(show) {
	if (!this.el) return;
	if (show) {
		for (var i = 0; i < this.el.length; i++) this.el[i].style.visibility = '';
	} else {
		for (var i = this.el.length - 1; i >= 0; i--) this.el[i].style.visibility = 'hidden';
	}
}
FDBlink.stop = function() {
	if (this.tmr) window.clearTimeout(this.tmr);
	this.tmr = 0;
	this.show(true);
	if (FD.Browser.ie || FD.Browser.camino) FDValidate.Select(this.focus, this.wnd);
	this.wnd = null; this.focus = null; this.el = null;
}
FDBlink.check = function(wnd, obj) {
	if (this.el) this.stop();
	this.wnd = wnd || window; this.focus = obj;
	if (typeof obj.el.length != 'undefined' && (typeof obj.el.tagName == 'undefined' || obj.el.tagName != 'SELECT')) {
		this.el = obj.el;
	} else this.el = new Array(obj.el);
	this.cnt = this.lmt = 0;
	this.exec();
}
function CheckBlink(opWnd, opEl) {
	//FD.Debug.depricated(CheckBlink, 'depricated! use FDBlink.check instead');
	FDBlink.check(opWnd, opEl);
}