var formatter = {
	strip_non_numeric: function( str )
	{
		str += '';
		var rgx = /^\d|\.|-$/;
		var out = '';
		for( var i = 0; i < str.length; i++ )
		{
			if( rgx.test( str.charAt(i) ) ){
				if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) ||
							( str.charAt(i) == '-' && out.length != 0 ) ) ){
					out += str.charAt(i);
				}
			}
		}
		return out;
	},
	initialize: function( ) {
		$('input.us_state').mask( "aa" );
		$('input.percentage').mask( "9.99" );
		//$('input.zip, input.zip_code').mask( "99999-9999" );
		$('input.zip, input.zip_code, input.postal_code').mask( "99999" );
		$('input.date').mask( "99/99/9999" );
		$('input.time_frame').mask( "99:99 aa to 99:99 aa" );
		$('input.date_frame').mask( "99/99/9999 to 99/99/9999" );
		$('input.phone_number').mask( "(999) 999-9999" );
		$('input.ssn').mask( "999-99-9999" );
	}
}
/*
 *   Add constraints to form controls.
 */
var constraints = {
	numeric: function( evt )
	{
		var rgx = /^\d|\.|-$/;

		if( evt.charCode != 0 && !rgx.test( String.fromCharCode( evt.charCode ) ) )
		{
			evt.preventDefault( )
		}
	},

	positive_float: function( evt )
	{
		var rgx = /^\d|\.$/;

		if( evt.charCode != 0 && !rgx.test( String.fromCharCode( evt.charCode ) ) )
		{
			evt.preventDefault( )
		}
	},

	positive_integer: function( evt )
	{
		if( evt.charCode > 31 && (evt.charCode < 48 || evt.charCode > 57) )
		{
			evt.preventDefault( );
		}
	},
	phone_number: function( evt )
	{
		var phone_string = $(this).val( ).trim( );

		phone_string = phone_string.replace(/[\(\)\.\+\-\ ]/g, '');        //the plus added and min of 10 digits for international numbers

		if( phone_string != "" )
		{
			if( !isNaN( parseInt(phone_string) ) && phone_string.length >= 10 )
			{
				$(this).val( phone_string );	
			}
		}
	},

	initialize: function( )
	{
		$('input.constraint_numeric').keypress( constraints.numeric );
		$('input.constraint_positive_float').keypress( constraints.positive_float );
		$('input.constraint_positive_integer').keypress( constraints.positive_integer );
		$('input.constraint_phone_number').keypress( constraints.phone_number );
	}
}


/*
 *   Validate form data.
 */
var validate = {
	is_email: function( strEmail )
	{
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

		// search email text for regular exp matches
		if( strEmail == undefined || strEmail.search(validRegExp) == -1 )
		{
			return false;
		} 

		return true; 
	},

	is_phone: function( strphone )
	{
		return_string = false;
		strphone = $.trim( strphone );
		if( strphone != "" )
		{
			new_str = strphone.replace(/[\(\)\.\+\-\ ]/g, '');        //the plus added and min of 10 digits for international numbers
			if( !isNaN( parseInt(new_str) ) && new_str.length >= 10 )
			{
				return_string = true;
			}
		}
		return return_string;
	},

	is_url: function( strweb )
	{
		validRegExp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
		if( strweb == "" || strweb.search(validRegExp) == -1 )
		{
			return false;
		} 
		return true; 
	}
};



$(document).ready( formatter.initialize );
$(document).ready( constraints.initialize );

