// Key Focing Functions (by Jason Hoi)
// Parse input value to int, phone number - ex. (+61) 03-1234 1234
jQuery.fn.keyForcing = function( rule ){
	var obj = $(this);
	switch( rule ){
		case 'number':
			$(this).live('blur', function(event){
				// match all char except number (also match '.'), we gonna empty out all other char
				var num_patt = /[^0-9.]/g;
				var input_str = $(this).val();
				var l = input_str.length;
				
				// convert the input to only numbers, all char other than number will just be omitted
				var new_input_str = input_str.replace(num_patt, '');
				// trim possible front 'zero' numbers (to ensure the parseInt() works
				for(var i=0; i<new_input_str.length; i++){
					var first_char = new_input_str.substr( 0, 1);
					if ( first_char == '0' )
						new_input_str = new_input_str.substr( 1, new_input_str - 1);
					else
						break;
				}
				
				// parse it into integar numbers (no floating point)
				var output = parseInt( new_input_str );
				
				// if the result is a number larger than 0
				if( !isNaN (output) && output >0 ){
					// exception: allow the last '%' sign for sale price field
					if ($(this).hasClass( 'sale_price' ) ){
						var last_char = input_str.substr( l-1, l);
						if (last_char == '%')
							output = output + '%';
					}
					$(this).val( output );
				}else{
					$(this).val( '' );
				}
			})
		break;
		
		case 'phone_number':
			$(this).live('blur', function(event){
				// match all char except number (also match '.'), because we will delete '.' and all other char
				var num_patt = /[^0-9\(\\)+\s-]/g;
				var input_str = $(this).val();
				// convert the input to only number, all char other than number will just be omitted
				$(this).val( input_str.replace(num_patt, '') );
			})
		break;
		
		case 'no_enter':
			$(this).keydown(function(event){
				// prevent key press - Enter
        		if( ( event.keyCode == 13 ) )
					event.preventDefault();
			});
		break;
	
	}
}
