/************************************
 *
 * Format input into valid format
 * Triggered on submit
 *
 ************************************/

//variables used to fade bgcolor of text-input
var iId = null;
var sr, sg, sb;	
var er, eg, eb;
var interval = 1;
var step;
var steps = 16;
var colorstart, colorend;
var daEl;
var timerRunning = false;
var hexa = new Array();
for (var i = 0; i < 16; i++) {
	hexa[i] = i.toString(16);
}
var active_input = null;
var alert_color = "#F1AB00";	//set new bgcolor of inputfields when not valid value
var normal_color = "#ffffff";
var interval_length = 85;			//Fade from alert_color to normal_color


//other variables
var msg 	= "";
var error = "";								//error message
var error_list = new Array();	//array containing the erroneous elements
var change_value=false;				//is the input value changed
var allow_submit = 0;					//do not allow submit when >0




/***************************************
 * insert . in dates
 ***************************************/
function date_insert_punctation(e, input)
{
  if( !e ) {
   if( window.event ) {
    //DOM
    e = window.event;
   } else {
    //TOTAL FAILURE, WE HAVE NO WAY OF REFERENCING THE EVENT
    return;
    }
	}

	var input_name = input.name;
  var	output = input.value;

	if ( (output.match(/^\d{2}$/) || output.match(/^\d{2}\.\d{2}$/)) && e.keyCode != 8 )
	{
		document.main[input.name].value = output+".";
	}
	else if ( output.match(/^\d{3,4}$/) ) 
	{
		var buffer = output.replace(/^(\d{2})(.*)$/, "$1\.$2");
		document.main[input.name].value = buffer;
	}
	else if ( output.match(/^\d{2}\.\d{3,4}$/) )
	{
		var buffer = output.replace(/^(\d{2}\.\d{2})(.*)$/, "$1\.$2");
		document.main[input.name].value = buffer;
	}

}





/***************************************************
 *	Main function:
 * 		
 *	 Input: 	User input and type definition
 *	 Output:	Input on valid format, based on type def.
 *						When input is changed, the valid value
 *						is set directly in proper input field
 *						in the web-browser. The input field is
 * 						also highlighted with a warning color
 *						that fades into white within a few sec's.
 *
 ***************************************************/
function format(f)
{
		var form_ind  = get_form_index(f.form.name);
		var input_ind = get_input_index(f.name, form_ind);

		var field = document.forms[form_ind].elements[input_ind];
		
		//set true if application corrects user's input
		change_value = false;


		//string?
		if (field.validate_as == "string") 
		{
			//field.value = toString(field.value);
		}


		//letter?
		else if (field.validate_as == "letters")
		{
			if (field.value != toLetter(field.value))
			{
				field.value = toLetter(field.value);
				change_value = true;
			}
		}


		//digits
		else if (field.validate_as == "digits")
		{
			if (field.value != toDigits(field.value))
			{
				field.value = toDigits(field.value);
				change_value = true;
			}
		}

		//number?
		else if (field.validate_as == "number")
		{
			if (field.value != toNumber(field.value))
			{
				field.value = toNumber(field.value);
				change_value = true;
			}
		}

		//integer?
		else if (field.validate_as == "integer")
		{
			if (field.value != toInteger(field.value))
			{
				field.value = toInteger(field.value);
				change_value = true;
			}
		}

		//decimal
		else if (field.validate_as == "decimal")
		{
			if (field.value != toDecimal(field.value))
			{
				field.value = toDecimal(field.value);
				change_value = true;
			}
		}

		//boolean
		else if (field.validate_as == "boolean")
		{
			if (field.value != toBoolean(field.value))
			{
				field.value = toBoolean(field.value);
				change_value = true;
			}
		}

		//time?
		else if (field.validate_as == "time")
		{
			if (field.value != toTime(field.value))
			{
				field.value = toTime(field.value);
				change_value = true;
			}
		}

		//date?
		else if (field.validate_as == "date")
		{
			//field.value = toDate_strict(field.value);		//Accepts only: (DD)(SEPARATOR)(MM)(SEPARATOR)(YYYY)
			if (field.value != toDate(field.value))
			{
				field.value = toDate(field.value);
				change_value = true;
			}
		}

		//year?
		else if (field.validate_as == "year")
		{
			if (field.value != toYear(field.value))
			{
				field.value = toYear(field.value);
				change_value = true;
			}
		}

		//email?
		else if (field.validate_as == "email")
		{
			if (field.value != toEmail(field.value))
			{
				field.value = toEmail(field.value);
				change_value = true;
			}
		}


		//caplitalize_first?
		else if (field.validate_as == "capitalize_first")
		{
			if (field.value != toCapital_first(field.value))
			{
				field.value = toCapital_first(field.value);
				change_value = true;
			}
		}

		//caplitalize_first?
		else if (field.validate_as == "capitalize_first_text")
		{
			if (field.value != toCapital_first_text(field.value))
			{
				field.value = toCapital_first_text(field.value);
				change_value = true;
			}
		}

		//caplitalize_all?
		else if (field.validate_as == "capitalize_all")
		{
			if (field.value != toCapital_all(field.value))
			{
				field.value = toCapital_all(field.value);
				change_value = true;
			}
		}

		//legat_nr_short?
		else if (field.validate_as == "legat_nr_short") 
		{
			if (field.value != toLegat_nr_short(field.value))
			{
				field.value = toLegat_nr_short(field.value);
				change_value = true;
			}
		}

		//legat_nr_long?
		else if (field.validate_as == "legat_nr_long") 
		{
			if (field.value != toLegat_nr_long(field.value))
			{
				field.value = toLegat_nr_long(field.value);
				change_value = true;
			}
		}


		//legat account?
		else if (field.validate_as == "legat_account") 
		{
			if (field.value != toLegat_account(field.value))
			{
				field.value = toLegat_account(field.value);
				change_value = true;
			}
		}


		else 
		{
			return;
		}

		/****************************
		* Is the input value changed?
		* - Then set fading color
		*****************************/
		if(change_value)
		{
			//prevent submitting
			allow_submit++;
			set_warning_color(field);
		}
} //end format()




/***************************************************
 *
 *	Functions that format input into valid format
 *
 ***************************************************/
//is this a valid String?
function toString(f)
{
	//don't see any problems here...
}


//remove non-letters
function toLetter(f)
{	
	return f.replace(/[^a-zA-Z]/g, "");

	//function toLetter(val) {
  //if (val > 25) return val - 26 + 0x32;
  //return val + 0x61;

}



//is this a valid Digits?
function toDigits(f)
{
	f = f.replace(/\D/g, "");
	if (f.length==0) f="0";
	return f;//.replace(/\D/g, "");
}


//is this a valid Number?
function toNumber(f)
{
	//decimal if delimiter found, else integer
	if (f.match(/[\.,]/)) return toDecimal(f);
	else return toInteger(f);
}


//is this a valid Integer?
function toInteger(f)
{
	var res = f.match(/^(-?)(.*)$/);		//negative number?
	if (res[2])
	{ 
		res[2] = toDigits(res[2]);					//remove non-digits
		if (res[2].length>1 && res[2].match(/^0/))  
		{
			res[2] = res[2].replace(/^(0*)/, "");
		}
		if (res[2].length == 0) res[2] = "0";		
		return res[1]+res[2];
	}
	else return 0;
}


//format into valid decimal?
function toDecimal(f)
{
	var res = f.match(/^(-?)(.*?)([\.,])(.*)$/);		//negative number?
	
	//valid format
	if (res)
	{
		res[2] = toDigits(res[2]);					//remove non-digits
		res[4] = toDigits(res[4]);					//remove non-digits
		if (res[2].length>1 && res[2].match(/^0/))  
		{
			res[2] = res[2].replace(/^(0*)/, "");
			if (res[2].length == 0) res[2] = "0";
		}		
		return res[1]+res[2]+"\."+res[4];
	}
	
	//add decimal to integer
	else 
	{
		res = f.match(/^(-?)(.*?)$/);
		res[2] = toDigits(res[2]);
		if (res[2].length>1 && res[2].match(/^0/))  
		{
			res[2] = res[2].replace(/^(0*)/, "");
		}		
		if (res[2].length == 0) res[2] = "0";
		return res[1]+res[2]+".0";
	}
}


//is this a valid boolean?
function toBoolean(f)
{
	if (f.match(/^(1|true|y)$/i)) return "1";	
	else return "0";
}


//is this a valid time?
function toTime(f)
{
	var hh_max = 23;
	var mm_max = 59;
	var ss_max = 59;
	
	var hh = "00"; var mm = "00";	var ss = "00";

	//(DIGIT)(SEP)(DIGIT)[(SEP)(DIGIT)]
	if ( result = f.match(/^(\d{1,2})(\D?(\d{1,2}))?(\D?(\d{1,2}))?$/) )
	{
		if (result[1]) hh = result[1];
		if (result[3]) mm = result[3];
		if (result[5]) ss = result[5];
	}

	//make sure it is a valid time
	if (hh>hh_max) hh=hh_max;
	if (mm>mm_max) mm=mm_max;
	if (ss>ss_max) ss=ss_max;

	//present values with two digits
	if (hh.length == 1) hh = "0".concat(hh);
	if (mm.length == 1) mm = "0".concat(mm);
	if (ss.length == 1) ss = "0".concat(ss);

	return hh+":"+mm+":"+ss;
}



//is this a valid date? - check the format and call valid_date()
function toDate_strict(f)
{
	var day 	= "00";
	var month = "00"; 
	var year 	= "0000";
	var split_symbol = "\/|-|\\.|\ |\,|\:";													//legal split symbols

	//regExp's for two seperators
	var two_sep_regExp = new RegExp("^(\\d{2})("+split_symbol+"){1}(\\d{2})("+split_symbol+"){1}(\\d{2}(\\d{2}))$");
	var res; 	//results from regexp-matches

	//------------PREFERRED FORMAT--------------------------
	//if input is on the form (DIGITS)(SEPARATOR)(DIGITS)(SEPARATOR)(DIGITS) == (dd mm yyyy): 
	if ( (res = two_sep_regExp.exec(f)) )
	{
	  if (res[5]) year = format_year(res[5]);
	  if (res[3]) month = res[3];
		if (res[1]) day = res[1];
	}

	//else alert("Dette er en ugyldig dato!");

	var date = year+"-"+month+"-"+day;
	date = valid_date(date);

	return date[2]+"."+date[1]+"."+date[0];


//	return day+"-"+month+"-"+year;
}





//is this a valid date? - check the format and call valid_date()
function toDate(f)
{
	var unknown_day   = "00";
	var unknown_month = "00";
	var unknown_year  = "0000";
	var day 	= unknown_day; 
	var month = unknown_month; 
	var year 	= unknown_year; 

	var valid_months = new RegExp("(jan|feb|mar|apr|mai|jun|jul|aug|sep|okt|nov|des)");	//months of the year
	var split_symbol = "\/|-|\\.|\ |\,|\:";													//legal split symbols

	//regExp's for one and two seperators
	var one_sep_regExp = new RegExp("^(\\d(\\d)?)("+split_symbol+"){1}(\\d{2}(\\d{2})?)$" );
	var two_sep_regExp = new RegExp("^(\\d{2})("+split_symbol+"){1}(\\d{2})("+split_symbol+"){1}(\\d{2}(\\d{2}))$");


	var res; 	//results from regexp-matches


	//------------PREFERRED FORMAT--------------------------
	//if input is on the form (DIGITS)(SEPARATOR)(DIGITS)(SEPARATOR)(DIGITS) == (dd mm yyyy): 
	if ( (res = two_sep_regExp.exec(f)) != null )
	{

	  year = format_year(res[5]);
	  month = res[3];
		day = res[1];
	}



	//if input is two or four digits without separators - this is a year
	else if (f.match(/^\d{4}$/)||f.match(/^\d{2}$/)) year = 	format_year(f);


	//if input is 6 or 8 digits without separators DDMMYY/DDMMYYYY
	else if (res = f.match(/^(\d{2})(\d{2})(\d{2}(\d{2})?)$/)) 
	{
		//error += res[1] + ".." + res[2]+ ".."+ res[3];
	  year = format_year(res[3]);
	  month = res[2];
	  day = res[1];
	}



	//if input is on the form (DIGITS)(SEPARATOR)(DIGITS) == (mm yyyy): 
	else if ( (res = one_sep_regExp.exec(f)) != null )
	{
	  year = format_year(res[4]);
	  month = res[1];
	}



	//split input into day-month-year
	// - accepts both numbered and spelled month 
	else 
	{
		var split_regExp = new RegExp(split_symbol);

		//split input into day-month-year
		var split_res = f.split(split_regExp);

		if (split_res.length == 3)		
		{
			
			if (split_res[0].match(/^\d{1,2}$/) ) day = split_res[0];
			if (split_res[2].match(/^(\d{1,2}){1,2}$/) ) year = format_year(split_res[2]);
	
	
			//find month
			if (split_res[1].match(/^\d{1,2}$/) )	month = split_res[1];
			else if ( (res = split_res[1].match(valid_months)) != null ) 
			{
				//present month with digits - find index from $valid_months
				month = (((valid_months.source).indexOf(res[1])-1)/4)+1;
				if (month < 10) month = "0".concat(month.toString());
			}
		}
		else 
		{
			day	= unknown_day; 
			month = unknown_month; 
			year = unknown_year; 
		}
	}

		
	if ( month > 12) month = 12;
	if ( month.length == 1 ) month = "0".concat(month);	
	if ( day > 31) day = 31;
	if ( day.length == 1) day = "0".concat(day);	
	
	//alert(day+month+year);

	var date = year+"-"+month+"-"+day;
	
	date = valid_date(date);

	return date[2]+"."+date[1]+"."+date[0];
}



//is this a valid year?
function toYear(f)
{
	var unknown_year = "0000";
	f = toDigits(f);
	

	//format into four digits when only two
	if (f.match(/^\d{2}$/))
	{
		f = format_year(f);
	}
	
	else if(f.match(/^\d{4}$/) == null) f = unknown_year;

	return f;
}


//is this a valid email address?
function toEmail(f)
{
	// regex for e-mail addresses where full=$1, user=$2, domain=$3
	// see pod documentation about this regex
	var word_rx = "[\x21\x23-\x27\x2A-\x2B\x2D\x2F\\w\x3D\x3F]+";
	var user_rx = word_rx         // valid chars
             + "(\." + word_rx + ")*";  // possibly more words preceded by a dot
	var dom_rx = "\\w[-\\w]*(?:\\.\\w[-\\w]*)*"; // less valid chars in domain names
	var ip_rx = "\\[\\d{1,3}(?:\\.\\d{1,3}){3}\\]";

	var address_rx = "^((" + user_rx + ")\@(" + dom_rx + "|" + ip_rx + "))$";
	
	var address_regExp = new RegExp(address_rx);

	var res =	f.match(address_regExp);
	if (res) return res[1];
	else return "unkonwn@email.adr";
}



//the date is now on a proper format, but it is a valid date?
function valid_date(f)
{
	//yyyy-mm-dd
	var date = f.split("-");  //(year,month,day)
	var month_length=0;
	
	//error += date[0] + ", " +date[1] + ", " + date[2];

	//year
	date[0] = toYear(date[0]);


	//check month
	if ( date[1].match(/^((0[1,3,5,7,8])|(1[0,2])|0{2})$/) )
	{
	  month_length = 31;			//(jan, march, may...)
	}
	else if ( date[1].match(/^((0[4,6,9])|(1[1]))$/) )
	{
	  month_length = 30;			//(apr, june, sept...)
	}	
	else if ( date[1].match(/^02$/) )
	{
	  //may divide on 4, and not 2000
	  if( !(((date[0])/4).toString()).match(/\D/) && !date[0].match(/^\d\d00$/) )
	  {
		month_length = 29;	  	//(feb "skuddår"...)	
	  }
	  else
	  {
  		month_length = 28;		//(feb normal)
	  }
	}	
 	else 
	{
		date[1] = 12;
		month_length = 31;
	}

	//check day
	if ( (date[2] < 0) || (date[2] > month_length) && !date[2].match(/^0{2}$/) )
	{
		date[2] = month_length;
	}

	return date;
}




//format two-digit year into four-digit year
function format_year(y)
{
	var year_fwd = 25;						
  var year_back = 75;		
	var date = new Date();
	var this_year = (date.getFullYear()).toString();

	var year_min  = (parseInt(this_year) - parseInt(year_back)).toString(); 
	var res = year_min.match(/\d{2}$/);
			year_min = res[0];

	var year_max  = (parseInt(this_year) + parseInt(year_fwd)).toString(); 
	    res = year_max.match(/\d{2}$/);
			year_max = res[0];


	//var unknown_year = "0000";

	//when two digits - make it four..
	if (y.match(/^\d{2}$/) )
	{
		if (y >= year_min) {y = "19".concat(y);}
		else if (y < year_max) {y = "20".concat(y);}		
	}

	//else if (y.match(/^\d{4}$/)) y = unknown_year;
	//error += y;
	return y;		
}



//set first letter capitalized of each word, e.g. name
function toCapital_first(f)
{	
	if (f.length==0) return "";

	f=f.toLowerCase();
	f=f.replace(/\ *$/, "");	//remove spaces at the end

	var str = f.split('');
	if (str[0].match(/^[\D]/)) str[0]=str[0].toUpperCase();

	var result = str[0];
	for (i=1; i < str.length; i++)
	{
		if( str[i-1].match(/(\ |\.|-|_|,)/))   // && str[i] < str.length-1 )
		{
			str[i] = str[i].toUpperCase();
		} 
		result += str[i];
	}

	//a few names should be forced with a lowercase letter in front
	result = result.replace(/Van\ /g, 'van ');
	result = result.replace(/Von\ /g, 'von ');
	result = result.replace(/De\ /g, 'de ');
	result = result.replace(/Da\ /g, 'da ');
	
	return result;
}

//set only first letter capitalized - e.g. text
function toCapital_first_text(f)
{	
	if (f.length==0) return "";

	f=f.toLowerCase();
	f=f.replace(/\ *$/, "");	//remove spaces at the end

	var str = f.split('');
	if (str[0].match(/^[\D]/)) str[0]=str[0].toUpperCase();

	var result = str[0];
	for (i=1; i < str.length; i++) { result += str[i]; }

	//a few names should be forced with a lowercase letter in front
	result = result.replace(/Van\ /g, 'van ');
	result = result.replace(/Von\ /g, 'von ');
	result = result.replace(/De\ /g, 'de ');
	result = result.replace(/Da\ /g, 'da ');
	
	return result;
}



//set all letters capitalized
function toCapital_all(f)
{	
	return f.toUpperCase();
}

//12345.67 => 12.345,67
function toCurrencyOutput(number) {
  number = number +""; // Makes it a string if it isn't already
  number = toDecimal(number,2);
  number = number.replace(/\./g, ",");

	var sign = "";
	if( isNaN(number.charAt(0)) ) {
		sign = number.charAt(0);
		number = number.substring(1);
	}

  if (number.length>6) { number = number.substring(0,number.length-6) +"." +number.substring(number.length-6,number.length); }
  return sign + number;
}

//12.345,67 => 12345.67
function toCurrencyInput(number) {
  number = number +""; // Makes it a string if it isn't already
  number = number.replace(/\./g, "");
  number = number.replace(/,/g, "\.");
  return number;
}

//returns a decimal string with a fixed number of decimals
function toDecimal(number,dlength) {
  number=(parseFloat(number) + ((parseFloat(number)<0)?-(0.5/Math.pow(10,dlength)):(0.5/Math.pow(10,dlength))) ) + "";
  var iandd = number.split(/\./);
  if (iandd[1].length > dlength) { iandd[1] = iandd[1].substring(0, dlength); }
  while (iandd[1].length < dlength) { iandd[1] += "0"; }
  return iandd[0] +"." +iandd[1];
}



//make legal Legat_nr_short?
function toLegat_nr_short(f)
{
	//prefix: L=legat or S=stell
	var prefix=f.replace(/^(s|l)?.*$/i, "$1").toUpperCase();
	if(prefix.length == 0 || prefix == "$1") {prefix="L";}

	f=f.replace(/^.*?(\d{1,5}).*?$/, "$1");
	if(f.match(/\D/)) {f="00000"};
	while (f.length < 5){	f="0".concat(f);}
	
	return f=prefix.concat(f);
}



//make legal Legat_nr_long?
function toLegat_nr_long(f)
{	
	//prefix: L=legat or S=stell
	var prefix=f.replace(/^(s|l)?.*$/i, "$1").toUpperCase();
	if(prefix.length == 0 || prefix == "$1") {prefix="L";}

	//xx-xxxxx: two separate numbers
	if(f.match(/\d{1,2}\D+\d{1,5}/))
	{
		f=f.replace(/^.*?(\d{1,2}).*?\D+(\d{1,5}).*$/, "$1-$2");
	
		var f_split = f.split("-");
		while (f_split[0].length < 2){	f_split[0]="0".concat(f_split[0]);}
		while (f_split[1].length < 5){	f_split[1]="0".concat(f_split[1]);}
		f=f_split[0].concat("-").concat(f_split[1]);
	}
	//__-xxxxx:	only one number found 
	else if(f.match(/\d{1,5}/)) 
	{
		f=f.replace(/^.*?(\d{1,5}).*$/, "$1");
		while (f.length < 5){	f="0".concat(f);}
		f="00-".concat(f);
	}
	//__-_____: 	no number found 
	else f="00-00000";		

	return f=prefix.concat(f);
}



//make legat_account?
function toLegat_account(f)
{
	//at least two + five digits found
	if (f.match(/\d{4}.*\d{2}.*\d{5}/)) return f=f.replace(/^.*?(\d{4}).*?(\d{2}).*?(\d{5}).*$/, "$1.$2.$3");
		
	//less digits found 
	else 
	{
		if (f.match(/(\d*\D+){0,2}(\d*)/))  f=f.replace(/^\D*(\d*)\D*(\d*)\D*(\d*).*$/, "$1.$2.$3");
		var num = f.split(".");
		
		//set max-lengths of each part
		if(num[0].length>4) num[0]=num[0].replace(/^(\d{4}).*$/, "$1");
		if(num[1].length>2) num[1]=num[1].replace(/^(\d{2}).*$/, "$1");
		if(num[2].length>5) num[2]=num[2].replace(/^(\d{5}).*$/, "$1");
	
		//each part must at least have one digit
		if(num[0].length==0) num[0]="0";
		if(num[1].length==0) num[1]="0";
		if(num[2].length==0) num[2]="0";
		
		return num[0].concat(".").concat(num[1]).concat(".").concat(num[2]);
	}
}


// date from english to norwegian format and vice versa
function convert_date(f)
{
	f=f.replace(/^(\d*)-(\d*)-(\d*)$/, "$3-$2-$1");
	return f;
}

// date from output format to input format
function dateToInput(input_date)
{
	input_date=input_date.replace(/^(\d*).(\d*).(\d*)$/, "$3-$2-$1");
	return input_date;
}

// date from input format to output format
function dateToOutput(output_date)
{
	output_date=output_date.replace(/^(\d*)-(\d*)-(\d*)$/, "$3.$2.$1");
	return output_date;
}


//get index of actual form
function get_form_index(in_val)
{
	var i=0;
	while (i < document.forms.length)
	{
		if(document.forms[i].name == (in_val).toString()) 
		{
			return i;
		}
		i++;
	}
	return false;	
}


//get index of actual input element
function get_input_index(in_val, f_ind)
{
	var i=0;
	while (i < document.forms[f_ind].elements.length)
	{
		if(document.forms[f_ind].elements[i].name == (in_val).toString()) 
		{
			return i;
		}
		i++;
	}
	return false;	
}



//set warning color of input text field when value is changed
function set_warning_color(field)
{
	if (active_input!=null) 
	{
		active_input.style.backgroundColor=normal_color; 
		clearInterval(iId);
		iId = null;
		timerRunning = false;
		allow_submit--;
	}

	//make current textfield the active one
	active_input=field;

	//Fade color
	myfade(field, alert_color, normal_color, 20, interval_length)
}	



/***************************************************************************
* Thanks to Jeroen for the fading source code, http://forums.devshed.com/t75627/s.html
* - some changes are made to fit format.js    
****************************************************************************/
function hex(i) {
	if (i < 0)	  return "00";
	else if (i > 255) return "ff";
	return "" + hexa[Math.floor(i/16)] + hexa[i%16];
}
function hextodec(daHex) {
	return parseInt('0x' + daHex);
}
function setColor(r,g,b) {
	var hr = hex(r); var hg = hex(g); var hb = hex(b);
	var daColor = "#"+hr+hg+hb;
	daEl.style.backgroundColor = daColor;
	if (daColor == colorend.toLowerCase()) {
		clearInterval(iId);
		iId = null;
		timerRunning = false;
		active_input = null;
		allow_submit--;
	}
}
function fade() {
	step++;
	setColor(
		Math.floor(sr * ((steps-step)/steps) + er * (step/steps)),
		Math.floor(sg * ((steps-step)/steps) + eg * (step/steps)),
		Math.floor(sb * ((steps-step)/steps) + eb * (step/steps))
		);
}
function myfade(el,cs,ce,iv,st) {
	daEl = el;
	colorstart = cs;
	colorend = ce;
	interval = iv;
	steps = st;
	step = 0;
	if (timerRunning) {
		clearInterval(iId);
		iId = null;
	}
	var myRe = /#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i;
	if (colorstart.match(myRe)) {
		sr = hextodec(RegExp.$1);
		sg = hextodec(RegExp.$2);
		sb = hextodec(RegExp.$3);
	}
	if (colorend.match(myRe)) {
		er = hextodec(RegExp.$1);
		eg = hextodec(RegExp.$2);
		eb = hextodec(RegExp.$3);
	}
	timerRunning = false;
	iId = setInterval("fade()",interval);
	timerRunning = true;
}
/**************
* <END THANKS>
***************/

