function check_range_number(obj, max_value, min_value, fieldCaption){
	//alert("fieldCaption: " + fieldCaption);
	if (!obj || !obj.value) return true; // if value = null then return true;
	if (max_value==null && min_value ==null) return true;
	var retval = true;
	if (max_value != null){
		if (typeof(max_value) == "string") max_value = parseFloat(max_value, 10);
		if (obj.value > max_value) retval = false;
		//alert(obj.value + " > " + max_value + ": " + retval );
	} else {
		max_value = "";
	}
	if (min_value !=null) {
		if (typeof(min_value) == "string") min_value = parseFloat(min_value, 10);
		if (obj.value < min_value) retval = false;
	} else {
		min_value = "";
	}
	if (retval==true) return true;
	var arr = new Array(3);
	arr[0] = fieldCaption;
	var msgAlert = MSG_OUTOFRANGE;
	if (typeof(max_value) == "number" && !isNaN(max_value) 
		&& typeof(min_value) == "number" && !isNaN(min_value)) {
		msgAlert = MSG_OUTOFRANGE;
		arr[1] = min_value;
		arr[2] = max_value;
	} else if (typeof(min_value) == "number" && !isNaN(min_value)) {
		msgAlert = MSG_MORETHAN;
		arr[1] = min_value;
	} else if (typeof(max_value) == "number" && !isNaN(max_value)) {
		msgAlert = MSG_LESSTHAN;
		arr[1] = max_value;
	} else {
		return true;
	}
	
	msgAlert = getMessage(arr, msgAlert)
	if (msgAlert != null && msgAlert != "") alert(msgAlert);
	obj.focus();

	return false;
}


function check_range_date(obj, max_value, min_value, fieldCaption){
	if (!obj || !obj.value) return true; // if value = null then return true;
	if (max_value==null && min_value ==null) return true;
	var retval = true;
	var objvalue = Str2Date(obj.value), max_date_value = null, min_date_value = null;
	if (max_value != null){
		if ( typeof(max_value) == "string" ) max_date_value = Str2Date(max_value);
		if (objvalue > max_date_value) retval = false;
		//alert(obj.value + " > " + max_value + ": " + retval );
	} else {
		max_value = "";
	}
	if (min_value !=null) {
		if ( typeof(min_value) == "string" ) min_date_value = Str2Date(min_value);
		if (objvalue < min_date_value) retval = false;
	} else {
		min_value = "";
	}
	//alert(max_date_value + ":" + min_date_value + ":" + retval);
	if (retval==true) return true;
	var arr = new Array(3);
	arr[0] = fieldCaption;
	var msgAlert = MSG_OUTOFRANGE;
	if (getObjectClass(max_date_value) == "Date" && max_date_value != null 
		&& getObjectClass(min_date_value) == "Date" && min_date_value !=null) {
		msgAlert = MSG_OUTOFRANGE;
		arr[1] = min_value;
		arr[2] = max_value;
	} else if (getObjectClass(min_date_value) == "Date" && min_date_value !=null) {
		msgAlert = MSG_MORETHAN;
		arr[1] = min_value;
	} else if (getObjectClass(max_date_value) == "Date" && max_date_value != null) {
		msgAlert = MSG_LESSTHAN;
		arr[1] = max_value;
	} else {
		return true;
	}
	
	msgAlert = getMessage(arr, msgAlert)
	if (msgAlert != null && msgAlert != "") alert(msgAlert);
	obj.focus();

	return false;
}

function check_range_length(obj, max_value, min_value, fieldCaption){
	if (!obj || !obj.value || !obj.value.length) return true; // if value = null then return true;
	if (max_value==null && min_value ==null) return true;
	var retval = true;
	var objlength = obj.value.length;
	if (max_value != null){
		if (typeof(max_value) == "string") max_value = parseFloat(max_value, 10);
		if (objlength > max_value) retval = false;
		//alert(objlength + " > " + max_value + ": " + retval );
	} else {
		max_value = "";
	}
	if (min_value !=null) {
		if (typeof(min_value) == "string") min_value = parseFloat(min_value, 10);
		if (objlength < min_value) retval = false;
	} else {
		min_value = "";
	}
	if (retval==true) return true;
	//alert(max_value + ":" + min_value);
	var arr = new Array(3);
	arr[0] = fieldCaption;
	var msgAlert = MSG_LENGTH;
	if (typeof(max_value) == "number" && !isNaN(max_value) && 
		typeof(min_value) == "number" && !isNaN(min_value)) {
		msgAlert = (max_value == min_value) ? MSG_LENGTH_CONSTANT : MSG_LENGTH;
		arr[1] = min_value;
		arr[2] = max_value;
	} else if (typeof(min_value) == "number" && !isNaN(min_value)) {
		msgAlert = MSG_LENGTH_MIN;
		arr[1] = min_value;
	} else if (typeof(max_value) == "number" && !isNaN(max_value)) {
		msgAlert = MSG_LENGTH_MAX;
		arr[1] = max_value;
	} else {
		return true;
	}
	
	msgAlert = getMessage(arr, msgAlert)
	if (msgAlert != null && msgAlert != "") alert(msgAlert);
	obj.focus();
	return false;
}

/* field = one String or 1 array String
  */
function getMessage(field, message){
	//alert("field: " + field + ", typeof(field): " + typeof(field) + "\n" + message);
	if (field==null) return null; // in this cas, no show msg , return only 
	if (message==null) return "";
	if (typeof(field) == "string"){
		if (message.indexOf("%0")==-1) {
			return field + " " + message;
		} else {
			//temp = message;
			//temp.replace("%0", " " + field + " ");
			return utf8_2_unicode(message.replace("%0", " " + field + " "));
		}
	} else { // field = array of string
		var pos = 0;
		for (i=0;i<field.length;i++){
			if (field[i]!=null){
				if (message.indexOf("%" + i)!=-1){
					message = message.replace("%" + i, field[i])
				}
			}
		}
		return utf8_2_unicode(message);
	}
}

//Overwrite the function check_format in common.js
//Include common before pt_common to overwrite
// max_value, min_value : 
//	if format numeric/date then they are min max numeric/date value
//	else 	they are min max length of control
function check_format(obj, format, notnull, fieldCaption, max_value, min_value){
	//alert("fieldCaption: " + fieldCaption);
	if (!obj) return true;
	if (typeof(notnull) == "boolean" && (notnull)
		&&(!check_notnull(obj,getMessage(fieldCaption, MSG_REQUIRED)))) return false;
	
	if (typeof(notnull) == "number" ) { // length min of control
		var length = toNumber("" + notnull, false);
		//alert(length);
		if ( !isNaN(length) && !check_range_length(obj, null, length, fieldCaption) ) return false;  // length min required of field
	}
	var nPara = check_format.arguments.length; // normally, nPara = 4 
	//alert("Number Para of Function:" + nPara);
	if ( format==null && nPara == 4 ) { // no format AND not parametre min_value, max_value
		return true;
	}	
	//alert("nPara:"+nPara);
	
	//alert("step 0:"+format + ":" + typeof(format));
	var sFormat = typeof(format) == "string" ? format.toLowerCase() : format;
	if (sFormat=="date"){
		if (!check_date(obj,getMessage(fieldCaption, MSG_INVALID))) return false;
	}else if (sFormat =="datetime"){
		if (!check_datetime(obj,getMessage(fieldCaption, MSG_INVALID))) return false;
	}else if (sFormat =="time"){
		if (!check_time(obj,getMessage(fieldCaption, MSG_INVALID))) return false;
	}else if (sFormat =="number"){
		if (!check_number(obj,getMessage(fieldCaption, MSG_NUMBER),"+-")) return false;
	}else if (sFormat =="number+"){
		if (!check_number(obj,getMessage(fieldCaption, MSG_NUMBER_POS),"+")) return false;
	}else if (sFormat =="number-"){
		if (!check_number(obj,getMessage(fieldCaption, MSG_NUMBER_NEG),"-")) return false;
	}else if (sFormat =="integer"){
		if (!check_integer (obj,getMessage(fieldCaption, MSG_INTEGER),"+-")) return false;
	}else if (sFormat =="integer+"){
		if (!check_integer (obj,getMessage(fieldCaption, MSG_INTEGER_POS),"+")) return false;
	}else if (sFormat =="integer-"){
		if (!check_integer (obj,getMessage(fieldCaption, MSG_INTEGER_NEG),"-")) return false;
	}else if (sFormat =="email"){
		if (!check_email (obj, getMessage(fieldCaption, MSG_INVALID))) return false;
	}else if (sFormat =="password"){
		if (!check_password (obj, getMessage(fieldCaption, MSG_PASSWORD))) return false;
	}else if (sFormat =="alphanumeric"){
		if (!check_alphanumeric (obj, getMessage(fieldCaption, MSG_INVALID))) return false;
	}else if (sFormat =="file"){
		if (!check_file (obj, getMessage(fieldCaption, MSG_INVALID))) return false;
	}else if (sFormat =="year"){
		return check_year(obj, getMessage(fieldCaption, MSG_INVALID));
	}else if (sFormat =="month"){
		return check_month(obj, getMessage(fieldCaption, MSG_INVALID));
	}else if (sFormat =="week"){
		return check_week(obj, getMessage(fieldCaption, MSG_INVALID));
	}else if (sFormat != null && sFormat != "" ){
		alert("Function("+sFormat+") hasn't construct yet");
		return false;
	}
	
	var isNumberFormat = (typeof(sFormat) == "string") && (sFormat.indexOf("number") == 0 || sFormat.indexOf("integer") == 0);
	if ( isNumberFormat ) {
		//alert("checking number format...:"+check_range_number(obj, max_value, min_value, fieldCaption));
		return check_range_number(obj, max_value, min_value, fieldCaption);
	}
	
	var isDateFormat = (typeof(sFormat) == "string") && (sFormat.indexOf("date") >= 0 || sFormat.indexOf("time") >= 0);
	if ( isDateFormat ) {
		// ATTENTION : not complet for type "datetime" and "time"
		return check_range_date(obj, max_value, min_value, fieldCaption);
	}
					
	
	//alert("checking length of control....");
	//alert("checking length of control...:"+max_value + ":" + min_value);
	return check_range_length(obj, max_value, min_value, fieldCaption);
}

/*
	&#1086; -> String.fromCharCode(1086) ...
*/
function utf8_2_unicode(s) {
	var idx_begin = 0, idx_end = -1;
	var ret = "";
	var tt = 0;
	
	while ( (idx_begin = s.indexOf("&#", idx_end+1)) >= 0 ) {
		ret += s.substring(idx_end+1, idx_begin); // NOTE : not include chat at index idx_begin		
		idx_end = s.indexOf(";", idx_begin);
		if ( idx_end < 0 ) { // not found ; 
			//ret += s.substring(idx_begin);
			idx_end = idx_begin;
			break;
		}
		var charCode = parseInt(s.substring(idx_begin+2, idx_end));
		//alert(charCode + " : " + (idx_begin+2) + " : " + idx_end);
		if ( isNaN(charCode) || charCode < 127 ) { // has some error here!
			ret += s.substring(idx_begin, idx_end+1); // get ; into ret
			continue; 
		}
		ret += String.fromCharCode(charCode);
		//tt++;
		//if ( tt > 100) break;
	}
	ret += s.substring(idx_end+1);
	return ret;
}




/*
//alert(getEleveurDomaine.length); : How to get the number of parameters expected by a JavaScript function? 
// testF.arguments : array of parameters passed 
// testF.arguments.length : number of parameters passed , testF.arguments[i] : valeur of parameters passed
//alert(getEleveurDomaine.arguments.length);
*/
