
///////////////////////// validate form helper funcitons ///////////////////////////////////////////////

function csvCheckFx(el, chkfx, chk, val) {
    if (!el) return;
	var tmpFx = chkfx.fxName+'("'+String.trim(el.value)+'")';
	if (!eval(tmpFx)) {
		return new ValidationError(el, chkfx.errMsg, val);
	}
    return null;
}
/*
 * Checks to see if form input is required. It looks for
 * chk with 'r??_??' returns ValidationError object if an error found
 */
function csvCheckRequired(el, chk, val, frm) {
    if (!el) return;

	var filter = /^r_/;
	if(!csvIsNullOrSpace(el.id) && el.type == "radio" && filter.test(chk)) {
		var radio = frm[el.name];
     	for(var i=0; i < radio.length; i++){
          	if(radio[i].checked)
               return null;
        }
		return new ValidationError(el, 'Value is required', val);	
	}
	else if(!csvIsNullOrSpace(el.id) && el.type == "checkbox" && filter.test(chk)) {
		var checkbox = frm[el.name];
        if(checkbox.checked)
            return null;
		return new ValidationError(el, 'Value is required', val);
	}
	else {
		if (!csvIsNullOrSpace(el.id) && filter.test(chk)) {
			if (csvIsNullOrSpace(String.trim(el.value))) {
				return new ValidationError(el, 'Value is required', val);
			}
		}
	}
    return null;
}
function csvElementMetaInfo(el) {
    if (!el) return;
    return('id='+el.id+'\nvalue='+el.value);
}
function csvGetDisplayErrorColumns() {
	return Math.round(csvTableWidth/csvValidationErrorColumnWidth);
}
function csvDisplayErrorMessage(html, lblStyle, fldStyle) {
    //write message
	if (html) {
        var el = cbeGetElementById(csvValidationErrorMessageMarker);
		if (el) {
			el.innerHTML = html;
        }
    }
    //change label and element class name
    if (csvValidationErrors) {
        for (i=0; i<csvValidationErrors.length; i++) {
            var elErr = csvValidationErrors[i];
            if (elErr) {
                //find label for error element
                var elErrLabel = cbeGetElementById(elErr.label());
                if (elErrLabel) {
                    elErrLabel.className = lblStyle;
                } else {
                    if (csvValidationDebug) alert('Unable to find error label for '+elErr.label());
                }
                if(elErr.element.type != "radio" && elErr.element.type != "checkbox") elErr.element.className = fldStyle;
            }
        }
    }
}

//////////////// restraint functions //////////////////////
var csvDateField;

function csvIsNullOrSpace(str) {
    if (str==null)
        return true;
    else if (str.length=0 || str=='')
			return true;
	else {
		var filter = /^\s+$/;
        return filter.test(str);
	}
}
function csvIsAlpha(str)
{
	var filter = /^[a-zA-Z]+$/;
	if (!csvIsNullOrSpace(str) && filter.test(str))
	    return true;
	else return false;
}
function csvIsAlphaSpace(str)
{
	if (csvIsNullOrSpace(str))
		return false;

	var filter = /^[a-zA-Z ]+$/;
	if (filter.test(str))
	    return true;
	else return false;
}
function csvIsEmail(str)
{
    var filter=/^([\w]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,3}(?:\.[a-z]{2})?)$/i;
    if (!csvIsNullOrSpace(str) && filter.test(str))
        return true;
    else return false;
}
function csvIsReal(str)
{
	var filter = /^-?\d*\.?\d*$/;
	if (!csvIsNullOrSpace(str) && filter.test(str))
	    return true;
	else return false;
}
function csvIsMoney(str)
{
    var re = /,/gi;
	var filter = /^(\$)?(\d*)(\.\d{0,2})?$/;
    str=str.replace(re, "");
	if (!csvIsNullOrSpace(str) && filter.test(str))
        return true;
    else return false;
}
function csvIsInteger(str)
{
	var filter = /(^\d\d*$)/;
	if (!csvIsNullOrSpace(str) && filter.test(str))
	    return true;
	else return false;
}
function csvIsTwoNum(str)
{
	var filter = /(^\d{2}$)/;
	if (!csvIsNullOrSpace(str) && filter.test(str))
	    return true;
	else return false;
}
function csvIsThreeNum(str)
{
	var filter = /(^\d{3}$)/;
	if (!csvIsNullOrSpace(str) && filter.test(str))
	    return true;
	else return false;
}
function csvIsFourNum(str)
{
	var filter = /(^\d{4}$)/;
	if (!csvIsNullOrSpace(str) && filter.test(str))
	    return true;
	else return false;
}
function csvIsFiveNum(str)
{
	var filter = /(^\d{5}$)/;
	if (!csvIsNullOrSpace(str) && filter.test(str))
	    return true;
	else return false;
}
function csvIsNonZeroInteger(str)
{
	var filter = /^[1-9]+$/;
	if (!csvIsNullOrSpace(str) && filter.test(str))
	    return true;
	else return false;
}
function csvIsZip(str)
{
	var filter = /^[0-9]{5}((-|\s)?[0-9]{4})?$/;
	if (!csvIsNullOrSpace(str) && filter.test(str))
	    return true;
	else return false;
}

function csvIsAlphaNum(str)
{
	var filter = /^[a-zA-Z0-9]+$/;
	if (!csvIsNullOrSpace(str) && filter.test(str))
	    return true;
	else return false;
}

/* String trim functions. These functions extend string so you can use them on any string. */
function String_ltrim(str)
{
    if (str)
        return str.replace(/^\s+/,'');
    else return '';
}
function String_rtrim(str)
{
    if (str)
        return str.replace(/\s+$/,'');
    else return '';
}
function String_trim(str){
    if (str)
        return String.rtrim(String.ltrim(str));
    else return '';
}

String.trim = String_trim;
String.ltrim = String_ltrim;
String.rtrim = String_rtrim;