// REX JAVASCRIPT UTILS
// (c) Etelos Systems Inc. 2002
// 
// Ahmad Baitalmal
//

// Format a text box into a phone number
function format_as_phonenumber( e,box )
{
	if((e.keyCode==8)||(e.keyCode==46)||(e.keyCode==37)||(e.keyCode==39)) return true;
	str = box.value;
	str = str.replace(/\(/g,"");
	str = str.replace(/\)/g,"");
	str = str.replace(/\-/g,"");
	str = str.replace(/\s/g,"");
	if( str.length > 3 &&  str.length < 7 )
	{
		pat = /(...)(.*)/;
		box.value = str.replace( pat, "$1-$2");
		return;
	}
	else if( str.length == 7 )
	{
		pat = /(...)(....)/;
		box.value = str.replace( pat, "$1-$2");
		return;
	}
        else if( str.length > 7 )
	{
		pat = /(...)(...)(.*)/;
		box.value = str.replace( pat, "($1) $2-$3");
		return;
	}
	else
	{
		box.value = str;
	}
}

function format_as_currency( e,box )
{
	if((e.keyCode==8)||(e.keyCode==46)||(e.keyCode==37)||(e.keyCode==39)) return true;
	str = box.value;
	str = str.match(/[0-9]/g);
	str = str.join("");
	
	if(str.length > 2)
	{
		digits = str.length;
		twopoints = digits - 2;
		dollars = str.substring(0,twopoints);
		coins = str.substring(twopoints,digits);
		divide = dollars.length / 3;
		divide = Math.floor(divide);
		money = "";
		if(divide)
		{
			for (var x = 1; x <= divide; x++)
			{
				howmuch = dollars.length;
				touse = howmuch - 3;
				money = dollars.substring(touse,howmuch)  + money;
				dollars = dollars.substring(0,touse);
				if (dollars.length > 0)
				{
					money = ","+money;
				}
			}
		}
		box.value = "$ "+dollars+money+"."+coins;
		return;
	}
	else if( str.length == 2 )
	{
		pat = /(..)/;
		box.value = str.replace( pat, "$ .$1");
		return;
	}
}


function format_as_date( e,box )
{
	if((e.keyCode==8)||(e.keyCode==46)||(e.keyCode==37)||(e.keyCode==39)) return true;
	str = box.value;
	box.value = "";
	str = str.match(/[0-9]/g);
	str = str.join("");
	if(str.length > 8)
	{
		str = str.substring(0,8);
	}
	pat = /(..)(..)(....)/;
	box.value = str.replace( pat, "$1/$2/$3");
	return;
}

function format_as_sixdate( e,box )
{
	if((e.keyCode==8)||(e.keyCode==46)||(e.keyCode==37)||(e.keyCode==39)) return true;
	str = box.value;
	box.value = "";
	str = str.match(/[0-7]/g);
	str = str.join("");
	if(str.length > 6)
	{
		str = str.substring(0,6);
	}
	pat = /(..)(..)(..)/;
	box.value = str.replace( pat, "$1/$2/$3");
	return;
}
