//function for currency validation
function currencyFormat( th, e ) 
{

	//alert( e.keyCode );
	key = (e.keyCode)? e.keyCode:e.which;

	//if( key < 48 || key > 57 ) 
	th.value = stripCharsNotInBag(th.value,'0123456789');//return false;

	num = th.value.toString().replace(/\$|\,/g,'');


	if(isNaN(num))
	num = "";

	sign = (num == (num = Math.abs(num)));
	num = Math.floor( num * 100 + 0.50000000001 );
	cents = num % 100;
	num = Math.floor(num/100).toString();

	if( cents < 10)
	cents = "0" + cents;
	for ( var i = 0; i < Math.floor(( num.length - (1+i)) / 3 ); i++ )
	num = num.substring(0, num.length - ( 4 * i + 3) ) + ',' + num.substring( num.length  - (4 * i + 3) );

	//th.value = (((sign)?'':'-') + '$' + num + '.' + cents) ;

	if( num != "0" )
		th.value = (((sign)?'':'-') + '$ ' + num ); 	
	else
		th.value = "";
}

function currencyOther( th, e ) 
{
	//alert( e.keyCode );
	key = (e.keyCode)? e.keyCode:e.which;

	//if( key < 48 || key > 57 ) 
	th.value = stripCharsNotInBag(th.value,'0123456789');//return false;

	num = th.value.toString().replace(/\$|\,/g,'');

	var sign;

	if(isNaN(num))
	num = "";

	if( num != "" )
	{
		sign = (num == (num = Math.abs(num)));
		num = Math.floor( num * 100 + 0.50000000001 );
		cents = num % 100;
		num = Math.floor(num/100).toString();

		if( cents < 10)
		cents = "0" + cents;
		for ( var i = 0; i < Math.floor(( num.length - (1+i)) / 3 ); i++ )
		num = num.substring(0, num.length - ( 4 * i + 3) ) + ',' + num.substring( num.length  - (4 * i + 3) );

		//th.value = (((sign)?'':'-') + '$' + num + '.' + cents) ;

		//if( num != "0" )
			th.value = (((sign)?'':'-') + '$ ' + num ); 	
		//else
		//	th.value = "";
	}
	else
		th.value = num;
}
	
	function trimString (str)
	{
		var str1;
		str1=str;
		return str1.replace(/^\s+/g, '').replace(/\s+$/g, '');
	}
			
	function stripCharsNotInBag (s, bag)
	{
		var i;
		var returnString = "";
		for (i = 0; i < s.length; i++)
		{   
			var c = s.charAt(i);
			if (bag.indexOf(c) != -1) returnString += c;
		}
		return returnString;
	}		

	function inputPhone(oControl,lExt)
	{
		if(oControl.value.length <= 13 )
		{
			// strip out any non-valid characters
			cPhone = stripCharsNotInBag(oControl.value,'0123456789-');
			
			
			// check for empties
			if (cPhone == '')
			{
				oControl.value = cPhone;
				return null;
			}
			// check if the first char is a 1, remove it after there are 2 chars
			if ((cPhone.length == 2) && (cPhone.substring(0,1) == '1'))
				cPhone = cPhone.substring(1,cPhone.length);
			
			// strip all formatting chars			
			cPhone = stripCharsNotInBag(cPhone,'0123456789');
			cPhone = "(" + cPhone;			
			
			if (cPhone != '1')
			{
				// add the formatting chars back in
				cPhone = cPhone;

				if (cPhone.length > 2)
				cPhone = cPhone.substring(0,4) + ')' + cPhone.substring(4,cPhone.length);

				if (cPhone.length > 6)
					cPhone = cPhone.substring(0,8) + '-' + cPhone.substring(8,cPhone.length);
				
			}
			// return new value
			oControl.value = cPhone;
		}
	}	

	function inputSSN(oControl,lExt)
	{

				// strip out any non-valid characters
				cSSN = stripCharsNotInBag(oControl.value,'0123456789-');
 
				// check for empties
	            
				if (cSSN == '')
				{
					oControl.value = cSSN;
					return null;
				}

				/* check if the first char is a 1, remove it after there are 2 chars
				if ((cSSN.length == 2) && (cSSN.substring(0,1) == '1'))
				cSSN = cSSN.substring(1,cSSN.length); */

	 

				// strip all formatting chars
				cSSN = stripCharsNotInBag(cSSN,'0123456789');
	 

				if (cSSN != '1')

				{
					// add the formatting chars back in

					cSSN = cSSN;

					if (cSSN.length > 2)
					cSSN = cSSN.substring(0,3) + '-' + cSSN.substring(3,cSSN.length); 

					if (cSSN.length > 4)
					cSSN = cSSN.substring(0,6)+'-'+cSSN.substring(6,cSSN.length);

				}
				
				// return the new value

				oControl.value = cSSN;

	}	
	
	function inputSIN(oControl,lExt)
	{

				// strip out any non-valid characters
				cSSN = stripCharsNotInBag(oControl.value,'0123456789-');
 
				// check for empties
	            
				if (cSSN == '')
				{
					oControl.value = cSSN;
					return null;
				}

				/* check if the first char is a 1, remove it after there are 2 chars
				if ((cSSN.length == 2) && (cSSN.substring(0,1) == '1'))
				cSSN = cSSN.substring(1,cSSN.length); */

	 

				// strip all formatting chars
				cSSN = stripCharsNotInBag(cSSN,'0123456789');
	 

				if (cSSN != '1')

				{
					// add the formatting chars back in

					cSSN = cSSN;

					if (cSSN.length > 2)
					cSSN = cSSN.substring(0,3) + '-' + cSSN.substring(3,cSSN.length); 

					if (cSSN.length > 4)
					cSSN = cSSN.substring(0,7)+'-'+cSSN.substring(7,cSSN.length);

				}
				
				// return the new value

				oControl.value = cSSN;

	}	
	
	function fnSpaceValidation(e)
	{
		return ( e.keyCode != 32 )				  
	}
	function test()
	{
		alert('works');
	}
	
	function fnNumericValidation(e)
	{	
		return ( e.keyCode >= 48 && e.keyCode  <= 57 )
	}		
	
	
	function inputYear( obj )
	{
		yearVal = stripCharsNotInBag(obj.value,'0123456789-');
		
		obj.value = yearVal;
	}
	
	function addText( obj )
	{
		if ( obj.value != '' && obj.value.indexOf("Years") == -1 )
		{	
			if( obj.value > 30 )
			{
				alert('Should be less than 30');
				obj.value = '';
				obj.focus();
			}
			else
			{
				if( obj.value > 1 )
					obj.value = yearVal + ' Years';
				else
					obj.value = yearVal + ' Year';
			}
		}		
	}