// This is the amount of time (in milliseconds) that will lapse between each step in the fade
var FadeInterval = 150;

// a holder for the current object that is to be validated.
var inputObj;

var FadeSteps = new Array();
        FadeSteps[0] = "ff";
        FadeSteps[1] = "f0";
        FadeSteps[2] = "ee";
        FadeSteps[3] = "e0";
        FadeSteps[4] = "dd";
        FadeSteps[5] = "d0";
        FadeSteps[6] = "cc";
        FadeSteps[7] = "c0";
        FadeSteps[8] = "bb";
        FadeSteps[9] = "b0";
        FadeSteps[10] = "aa";
        FadeSteps[11] = "a0";
        FadeSteps[12] = "99";
        FadeSteps[13] = "90";
        FadeSteps[14] = "88";
        FadeSteps[15] = "80";

// This is where the fade will start, if you want it to be faster and start with a lighter color, make this number smaller
// It corresponds directly to the FadeSteps below
var StartFadeAt = FadeSteps.length-1;

/**
 * Validates a field based upon a regular expression. If
 * unable to validate, show alert box with given 
 * error message.
 * Typical usage: <input onChange="genericRegExpValidate(this, /^\d*$/, 'Not numeric, please correct.');">
 */
function genericRegExpValidate(field, regexp, errmsg){	
 inputObj = field;
 if (!regexp.test(field.value)) {
  inputObj.focus();
  if(errmsg!=null){
   alert(errmsg);
  }
  DoFade(StartFadeAt, field);
 } 
}

function genericRegExpValidate(field, regexp, errmsg){	
 inputObj = field;
 if (!regexp.test(field.value)) {
  alert(errmsg);
  DoFade(StartFadeAt, field);
 } 
}

/**
 *  A function that fades the background color of a given object to hightlight it
 *  if a validation fails.
 */
function DoFade(colorId) {
 if (colorId>=0) {
  inputObj.style.backgroundColor = "#ff" + FadeSteps[colorId] + FadeSteps[colorId];

  // If it's the last color, set it to white
  if (colorId==0) {
   inputObj.style.backgroundColor = "white";
  }
  colorId --;
  // Wait a little bit and fade another shade
  setTimeout("DoFade("+(colorId)+")", FadeInterval);
 }
}


/**
 * Thousand separator for numbers
 */
function thousandSeparator(obj) {

	var current=obj.value;
	var after=current;
	
	current=current.replace(/,/g,"");
	var decimalpoint=current.lastIndexOf(".");
	
	var n;
	var d;
	if(decimalpoint>=0){
		var f=current.split(".");
		d=f[1];
		n=f[0];
	} else{
		n=current;
	}
	
	var index=parseInt((n.length-1)/3);
	if(index!=0){
		var prefixIndex=n.length-index*3;
		after=n.substr(0,prefixIndex)+","+n.substr(prefixIndex,3);
		for(var i=2;i<=index;i++){
			after+=","+n.substr(prefixIndex+3*(i-1),3);
		}
	
		if(decimalpoint>=0){
			after+="."+d;
		}
	}
	
	obj.value=after;
}

/**
 * Remove "," separator for min value and max value
 */
function removeSeparator(objmin,objmax) {
	var numbermin = document.getElementById(objmin);
	var intIndexOfMatchmin = numbermin.value.indexOf(",");
	while (intIndexOfMatchmin != -1){
		numbermin.value = numbermin.value.replace(",","")
		intIndexOfMatchmin = numbermin.value.indexOf(",");
	}
	document.getElementById(objmin).value = numbermin.value;
	
	var numbermax = document.getElementById(objmax);
	var intIndexOfMatchmax = numbermax.value.indexOf(",");
	while (intIndexOfMatchmax != -1){
		numbermax.value = numbermax.value.replace(",","")
		intIndexOfMatchmax = numbermax.value.indexOf(",");
	} 	
	document.getElementById(objmax).value = numbermax.value;	
}	
