function check_decimal(value, operator, fixPartPrec, decPartPrec)
{

//  if (decPartPrec!=-1)
  // alert("check_dec:"+value+" "+operator+" "+fixPartPrec+" "+decPartPrec);
  // we supress thounsand separator
  
  if (_browser.DEC_SEP!='')
  {
  	regexp = "/[" + _browser.DEC_SEP + "]/g";
  	value = value.replace(eval(regexp), ".");
  }
  
  if (_browser.THO_SEP!='')
  {
  	regexp = "/[" + _browser.THO_SEP + "]/g";
  	value = value.replace(eval(regexp), "");
  }

  // we supress space
  regexp = "/[ ]/g";
  value = value.replace(eval(regexp), "");
  

	regexp = "/[e]/g";
  // we replace lower case exponent sign by upper case
  value = value.replace(eval(regexp), "E");
  
  
  pos = value.indexOf("E");	
  if (pos>=0)
  {
  	leftvalue = value.substring(0, pos);
  	exppart = value.substring(pos+1);
  }
  else
  {
  	leftvalue = value;
  	exppart = null;
  }
  
  pos = leftvalue.indexOf(".");
  if (pos>=0)
  {
  	intpart = leftvalue.substring(0, pos);
  	decpart = leftvalue.substring(pos+1);
  }
  else
  {
  	intpart = leftvalue;
  	decpart = null;
  }
  
  if (!check_subpart(intpart, operator, fixPartPrec, true)) return null;
  if (decpart!=null && !check_subpart(decpart, operator, decPartPrec, false)) return null;
  if (exppart !=null && !check_subpart(exppart , operator,  -1, true)) return null;
 
  return value;
}

function check_subpart(value, operator,  precision, allowSign)
{
	//alert("check_sub:" + value + " " + operator + " " + precision + " " + allowSign);
	
	precact = 0;
	for (i=0;i<value.length;i++)
	{
		c = value.charAt(i);
		
		if ((c=='+' || c=='-') && (i==0) && (allowSign)) continue;
		if (c>='0' && c<='9') 
		{
			
			precact = precact +1;
			continue;
		}
		if (c==' ') continue;
		if (c==0) continue;

		return false;
	}

	if (precision!=-1)
	{
		//alert('go:'+precision+' ' + precact+ ' '+operator);
		if (operator=='=' && precact!=precision) return false;
		else if (operator=='<=' && precact>precision) return false;
		else if (operator=='<' && precact>=precision) return false;
		else if (operator=='>=' && precact<precision) return false;
		else if (operator=='>' && precact<=precision) return false;
	}
	
	return true;
}


/**
 *
 *
 *
 *
 *
 */
function browser_validate(elementName, formName, dataType, dataFmt,type,operator,value, message)
{

	var 	rawValue; 
	var 	failed 	= false;

	formElm = _browser.getFormElement(elementName, formName);
	if (formElm==null)
		return true;
	dataValue = formElm.value;

	//alert(elementName +':' + formName +':'+dataType+':'+dataFmt+':'+type+':'+operator+':'+value+':'+message);
	
	// ===============================================================
	// =
	// = Type validation and conversion
	// =
	// ===============================================================
	
	// ==============
	//  Date
	// ==============
	if (dataType == "date") 
	{
		var fmt = null;
		
		// get back the date format
		if (dataFmt == null || dataFmt == "") 
		{
			fmt = _browser.getDateFormat();
		}
		else
		{
			fmt = dataFmt;
		}

		// if we can't parse : rawValue=null, failed.
		rawValue = _browser.parseDate(dataValue, fmt);
		
		if (rawValue == null) 
		{
			failed = true; 
		}
		else 
		{
			rawValue = rawValue.valueOf();
		}
	}
	// =======================
	//  Integer or Decimal
	// =======================
	else if (dataType == "integer" ||
			 dataType == "decimal") 
	{
		fixPartPrec = -1;
		decPartPrec = -1;

		if (dataType=='decimal' && value!="") 
		{
			var parts = value.split(";"); 
			fixPartPrec = parts[0];
			decPartPrec = parts[1];
		}
		
		
		rawValue = dataValue;
		dataValue = check_decimal(dataValue, operator, fixPartPrec, decPartPrec);


		if (dataValue==null) 
		{
			failed = true;
			dataValue = rawValue;
		}
		else
		{
			rawValue = new Number(dataValue);
			//alert(rawValue);
			if (rawValue.toString() == "NaN") failed = true;

			//alert(type);
		
			if (dataType == "integer") 
			{
				if (String(dataValue).indexOf(".") != -1 || String(dataValue).indexOf(_browser.DEC_SEP) != -1)
				{
					failed = true;
				}
			}
		}
	}
	// ========================================
	//  Elsecase : no type validation : text
	// ========================================
	else 
	{
		rawValue = dataValue;
	}



	// ===============================================================
	// =
	// = Apply the validation operator:
	// =
	// =	compare			: compare the value with a value and a operator
	// = 	range			: verify that the value is in a range
	// = 	expression		: check the value with a regex expression
	// = 	mandatory		: verify that value exist
	// = 	script			: pass the verification to a script
	// = 	inlinescript	: pass the verification to a script
	// = 	length			: verify size of the input
	// =	positive		: verify that number is positive
	// = 	uppercase		: verify that input is uppercase
	// =
	// ===============================================================

	
	// =======================================
	// positive
	// =======================================
	if (!failed && type == "positive") 
	{
		if (rawValue <= 0)
		{
			failed = true;
		}
	}

	// =======================================
	// mandatory
	// =======================================
	else if (!failed && type == "mandatory") 
	{
		if (new String(dataValue).replace(/\s*/, "").length == 0)
		{
			failed = true; 
		}
	}

	// =======================================
	//	uppercase
	// =======================================
	else if (!failed && type == "uppercase") 
	{
		 dataValue = new String(dataValue).toUpperCase();
	}
	
	// =======================================
	//  length
	// =======================================
	else if (!failed && type == "length") 
	{
		lg = new String(dataValue).length;
		cond = new String(lg) + operator + new String(value);
		if (eval(cond)==false) 
		{
			failed = true;
		}
	}

	// =======================================
	//  expression
	// =======================================
	else if (!failed && type == "expression") 
	{
		if (!(new RegExp(value).exec(dataValue))) 
		{
			failed = true; 
		}
	}

	// =======================================
	//  script
	// =======================================
	else if (!failed && type == "script") 
	{
		if (!eval(value+"(" + dataValue + ");"))
		{
			failed = true; 
		}
	}

	// =======================================
	//  inlinescript
	// =======================================
	else if (!failed && type == "inlinescript") 
	{
		if (!eval(value)) 
		{
			failed = true; 
		}
	}


	// =======================================
	//  other check base on value
	// =======================================
	if (!failed && type!="" && type!="type") 
	{
		if (value.length == 0) return true;
			
		var parts = value.split(";"); 
		var loop; 
		var max = parts.length;
			
		if (dataType == "date") 
		{
			for (loop = 0; loop < max; loop++) 
				parts[loop] = _browser.parseDate(parts[loop],fmt);
		}
			
			
		for (loop = 0; loop < max && !failed; loop++) 
		{
			var current = parts[loop];


			// =======================================
			//  Range checking
			// =======================================
			if (type == "range") 
			{
				if (loop==0) 
				{
					operator = ">=";
				}
				else if (loop==1) 
				{
					operator = "<=";
				}
			}

			if (type == "date") current = current.valueOf();
				
			// =======================================
			//  Compare checking
			// =======================================
			if (type == "compare" || type=="range") 
			{
				if (operator == "<>") 
				{
					if (rawValue == current) failed = true;
				}
				else if (operator == "<=") 
				{
					if (rawValue > current) failed = true;
				}
				else if (operator == "<") 
				{
					if (rawValue >= current) failed = true;
				}
				else if (operator == ">=") 
				{
					if (rawValue < current) failed = true;
				}
				else if (operator == ">") 
				{
					if (rawValue <= current) failed = true;
				}
				else if (operator == "and") 
				{
					if (!(current & rawValue)) failed = true;
				}
				else if (operator == "or") 
				{
					if (!(current | rawValue)) failed = true;
				}
				else if (type!="range" && current != rawValue) failed = true;
			}
			else 
			{ 
				if (operator == "exclude") 
				{
					if (rawValue >= parts[loop] || rawValue <= parts[loop + 1]) 
					{
						failed = true; 
					}
				}
//				else if (rawValue < parts[loop] || rawValue > parts[loop + 1]) 
//				{
//					failed = true;
//				}
				
				loop++; 
				if (loop + 1 >= max) break;
				
			}	// else
			
		}		// for
		
	}	// if

	// =======================================
	// reassign convert value to element
	// =======================================
	formElm.value = dataValue;
	
	// =======================================
	// construct error message
	// =======================================
	if (failed) 
	{
		var msg = dataValue; 
				
			
		if (msg!=null && msg.length) msg += " - ";
				
		if (message != null ||  message!='null' || message.length == 0) 
		{
			if (dataType == "date")
			{
				message = _browser.getLanguageText("invalidDate");
			}
			else if (dataType == "number") 	
			{
				message = _browser.getLanguageText("invalidNumber");
			}
			else 
			{
				message = _browser.getLanguageText("invalidValue");
			}
		}
						
		msg += message; 
	} 
		
	return !failed;
}

