
// common validation functions
function checkRequired(control, fieldName, required) {
	var value = control.value;
	if (isEmpty(value) && required) {
		return handleValidationError(control, getMsgRequiredField(fieldName));
	}
    return true;
}

function validateLength(control, fieldName, min, max, errorMsg, required)
{
	var tmpValue = control.value;
	if (isEmpty(tmpValue)) {
		return checkRequired(control, fieldName, required);
	}
	return validateLengthOfValue(control, fieldName, min, max, errorMsg);
}

function validateLengthOfValue(control, fieldName, min, max, errorMsg)
{
	var result = true;
	if(min > -1) {
	    if (control.value.length < min) {
	    	result = handleValidationError(control, getMsgValueTooShort(fieldName, min)+"\r\n"+errorMsg);
	    }
	}
	if(max > -1 && result) {
	    if (control.value.length > max) {
	    	result = handleValidationError(control, getMsgValueTooLong(fieldName, max)+"\r\n"+errorMsg);
	    }
	}
	return result;
}

function validateRangeInt(control, fieldName, min, max, errorMsg, required)
{
	var tmpValue = control.value;
	if (isEmpty(tmpValue)) {
		return checkRequired(control, fieldName, required);
	}
    var intVal = parseInt(tmpValue,10);
    if (isNaN(intVal) || intVal < min || intVal > max) {
    	return handleValidationError(control, errorMsg.length > 0 ? errorMsg : getMsgValidRange(fieldName, min, max));
    } else {
        return true;
    }
}

function validateRangeFloat(control, fieldName, min, max, errorMsg, required)
{
	var tmpValue = control.value;
	if (isEmpty(tmpValue)) {
		return checkRequired(control, fieldName, required);
	}
	tmpValue = deleteTausenderPunktalan(tmpValue);
	tmpValue = tmpValue.replace(",",".");
	tmpValue = Number(tmpValue);
    if (isNaN(tmpValue) || tmpValue < min || tmpValue > max) {
    	return handleValidationError(control, errorMsg.length > 0 ? errorMsg : getMsgValidRange(fieldName, formatDecimal(min), formatDecimal(max)));
    }
    else {
        return true;
    }
}

function validateRangeCurrency(control, fieldName, min, max, errorMsg, required)
{
	var tmpValue = control.value;
	if (isEmpty(tmpValue)) {
		return checkRequired(control, fieldName, required);
	}
	tmpValue = deleteTausenderPunktalan(tmpValue);
	tmpValue = tmpValue.replace(",","");
	tmpValue = Number(tmpValue);
    if (isNaN(tmpValue) || tmpValue < min || tmpValue > max) {
    	return handleValidationError(control, errorMsg.length > 0 ? errorMsg : getMsgValidRange(fieldName, formatLongToBetrag(min), formatLongToBetrag(max)));
	} else {
    	return true;
	}
}

function validatePattern(control, fieldName, pattern, errorMsg)
{
    var parser = new RegExp(pattern, "");
    if (!parser.test(control.value)) {
    	return handleValidationError(control, errorMsg.length > 0 ? errorMsg : getMsgInvalidField(fieldName));
    }
    else {
        return true;
    }
}

function validatePatternLength(control, fieldName, pattern, min, max, errorMsg, required)
{
	var tmpValue = control.value;
	if (isEmpty(tmpValue)) {
		return checkRequired(control, fieldName, required);
	}
	return validateLengthOfValue(control, fieldName, min, max, errorMsg) &&
		   validatePattern(control, fieldName, pattern, errorMsg);
}

function encodeURIComponentSD(href)
{
	var rep = encodeURI(href);
	// this caracters also have to be masked
	// rep = " ,, // ?? :: @@ && == ++ $$ ";
	rep = replaceAll(rep, "/\\,/", "%2C");
	rep = replaceAll(rep, "/\\//", "%2F");
	rep = replaceAll(rep, "/\\?/", "%3F");
	rep = replaceAll(rep, "/\\:/", "%3A");
	rep = replaceAll(rep, "/\\@/", "%40");
	rep = replaceAll(rep, "/\\&/", "%26");
	rep = replaceAll(rep, "/\\=/", "%3D");
	rep = replaceAll(rep, "/\\+/", "%2B");
	rep = replaceAll(rep, "/\\$/", "%24");
	return rep;
}
function replaceAll(str, expr, strrep) {
	var x = 0;
	// "|| x < 1000" ==> anticipates infinite loop
	while(str.match(expr) || x < 1000) {
		str = str.replace(eval(expr), strrep);
		x++;
	}
	return str;
}var allInOneErrorMessagesEnabled = false; //defaultfunction handleValidationError(control, errorMsg) {	if (allInOneErrorMessagesEnabled) {		addValidationError(control, errorMsg);		return true;	} else {		doFocus(control, errorMsg);		return false;	}}