
/* Regexp patterns */
var xAlpha				= /^[a-zA-Z]+$/g;
var xalpha				= /^[^A-Z0-9]+$/g;
var xALPHA				= /^[A-Z]+$/g;
var xalphanum			= /^[^A-Z]+$/g;
var xALPHANUM			= /^[A-Z0-9_]+$/g;
var xAlphanum			= /^[^<>\\=]+$/g;
var xinteger			= /^[\-]?[0-9]+$/g;
var xfloat				= /^[\-|\+]?[0-9]+[\.]?[0-9]*?$/g;
var xhexa16				= "^[0-9a-fA-F]{1,2}$";
var xhexa32				= "^[0-9a-fA-F]{1,4}$";
var xdate_ch			= "^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}$";
var xphone				= "^[\+]{0,1}[0-9| ]{7,}$";
var xemail				= /^[a-zA-Z0-9|\-|\.|\_]+[@][a-zA-Z0-9|\-|\.|\_]+[a-zA-Z]{2,}$/;
var xdb_fieldname	= /^[a-zA-Z][a-zA-Z0-9_\.]*$/g;

var xtest				= /^[\-|\+]?[0-9]+[\.]?[0-9]+$/g;

var fieldsBackgroundColor	= new Array();
var fieldsColor				= new Array();
var fieldsErrorFlag			= new Array();
var errorCode				= null;

String.prototype.trim = function() {
 /* skip leading and trailing whitespace and return everything in between */
  var str = this;
  str = str.replace(/^\s*(.*)/, "$1");
  str = str.replace(/(.*?)\s*$/, "$1");
  return str;
}

function validate_checkLength(str, len_min, len_max) {
	result = 0;
	if((str.length < len_min) | (str.length > len_max)) {
    errorCode++;
  	result = 1;
  }
	return result;
}

function validate_checkValue(value, value_min, value_min_inclusive
							, value_max, value_max_inclusive) {

	result = 0;
	bitmask =	(value_min != null)
				+ ((value_min_inclusive != null) * 2)
				+ ((value_max != null) * 4)
				+ ((value_max_inclusive != null) * 8);
//alert(value +" "+ value_min +" "+ value_min_inclusive +" "+ value_max +" "+ value_max_inclusive)
//alert(value_max +" bitmask : " + bitmask);

	switch(bitmask) {

		/* check min value only */
		case 1:
			result = (value > value_min);
		break;

		/* check min or equal value only */
		case 3:
			result = (value >= value_min);
		break;

		/* check max value only */
		case 4:
			result = (value < value_max);
		break;

		/* check min and max value only */
		case 5:
			result = (value > value_min) & (value < value_max);
		break;

		/* check max or equal value only */
		case 12:
			result = (value <= value_max);
		break;

		/* check min or equal and max value */
		case 7:
	result = (value >= value_min) & (value < value_max);
		break;

		/* check min and max or equal only */
		case 13:
	result = (value > value_min) & (value <= value_max);
		break;

		/* check min or equal and max or equal value */
		case 15:
	result = (value >= value_min) & (value <= value_max);
		break;

	}	// end switch
//alert("result:"+result);
	return result;
}

function validate_add_reqmsg(field_name, field_value) {
	return ERR_required + cr;
}

function validate_add_errmsg(field_name, field_value, pattern) {
	return eval("ERR_" + pattern) + cr;
}

/* dumm1 and dummy2 are not used for string length */
function validate_string(formName, fieldName, pattern, length_min, dummy1,
							length_max, dummy2, required) {

	var ele = xGetElementById(formName).elements[fieldName];
	errorCode = false;
	var errorText = "";

	/* critical error : the field does not exists */
	if(!ele) {
		alert("ERROR : can not validate, the field [" + fieldName
		+ "] is not defined");
	}
	else {
		var field_value = ele.value;
		/* store actual background and color of the field */
  	if(fieldsBackgroundColor[fieldName] == null) {
  		fieldsBackgroundColor[fieldName]	= "" + ele.style.backgroundColor;
  		fieldsColor[fieldName]						= "" + ele.style.color;
  	}
  	/* First test if the field is required or not */
	if(required == 1 && field_value.trim().length == 0) {
		errorCode++;
		errorText += validate_add_reqmsg(fieldName, field_value);
	} 
if(field_value.trim().length != 0) {
	if(field_value.match(eval(pattern)) == null) {
			errorCode++;
			errorText += validate_add_errmsg(fieldName, field_value, pattern);
		}
		bitmask = (length_min != null) + ((length_max != null) * 2);
		switch(bitmask) {
			case 1:
				/* check min value only */
				if(validate_checkLength(field_value, length_min, false) != 0) {
					errorText +=  ERR_LIMIT_1 + length_min + ERR_LIMIT_END + "\n";
				}
			break;

			case 2:
				/* check max value only */
				if(validate_checkLength(field_value, 0, length_max) != 0) {
					errorText +=  ERR_LIMIT_2 + length_max + ERR_LIMIT_END + "\n";
        }
      break;

        case 3:
  				/* check max value only */
  				if(validate_checkLength(field_value, length_min, length_max) != 0) {
						errorText += ERR_LIMIT_3 + length_min + ERR_LIMIT_TO
            + length_max + ERR_LIMIT_END + "\n";
          }
        break;
      }	// end switch
  	} // end else
}
	/* build error text if someting happend */
  if(errorCode != false) {
    ele.style.backgroundColor	= "#ff0033";
    ele.style.color				= "#ffffff";
    
    out += "[ " + fieldName + " : " + field_value + " ]" +cr + cr + errorText
		+ cr + sep + cr;
	}
	else {
    /* reset bgcolor to last color */
    ele.style.backgroundColor	= fieldsBackgroundColor[fieldName];
    ele.style.color				= fieldsColor[fieldName];
  }
	return;
}					// end function

function validate_number(formName, fieldName, pattern
						, value_min, value_min_inclusive
						, value_max, value_max_inclusive, required) {

	var ele = xGetElementById(formName).elements[fieldName];
	errorCode = false;
	var errorText = "";

	/* critical error : the field does not exists */
	if(!ele) {
		alert("ERROR : can not validate, the field [" + fieldName
		+ "] is not defined");
	}
	else {
		var field_value = ele.value;

		/* store actual background and color of the field */
		if(fieldsBackgroundColor[fieldName] == null) {
			fieldsBackgroundColor[fieldName]	= "" + ele.style.backgroundColor;
			fieldsColor[fieldName]				= "" + ele.style.color;
		}
		/* First test if the field is required or not */
		if(required == 1 && field_value.trim().length == 0) {
			errorCode++;
			errorText += validate_add_reqmsg(fieldName, field_value);
		} 
		else {
			if(required == 1 && field_value.match(eval(pattern)) == null) {
				errorCode++;
				errorText += validate_add_errmsg(fieldName, field_value, pattern);
			}
			if(validate_checkValue(field_value, value_min, value_min_inclusive
							, value_max, value_max_inclusive) == false) {
				errorCode++;
						errorText +=  ERR_LIMIT_NUM_1 + value_min + ERR_LIMIT_NUM_AND
													+ value_max + "\n";
			}
		}
	} // end else

	/* build error text if someting happend */
	if(errorCode != false) {
		ele.style.backgroundColor	= "#ff0033";
		ele.style.color				= "#ffffff";
    
    out += "[ " + fieldName + " : " + field_value + " ]" +cr + cr + errorText
		+ cr + sep + cr;
	}
	else {
    /* reset bgcolor to last color */
    ele.style.backgroundColor	= fieldsBackgroundColor[fieldName];
    ele.style.color				= fieldsColor[fieldName];
  }
	return;
}					// end function

function array_dump(array) {
	var buffer = "";
	for(var x=0; x < array.length; x++) {
		buffer += array[x].value + cr;
  }
}


