/*
 * iPhoneNumberInput version 0.0.2 (2009/12/20)
 *
 * The MIT License
 * 
 * Copyright (c) 2009 Hiromu Ogawa
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * JS
 *
 *   $(function() {
 *     $('input[type=number]').iPhoneNumberInput();
 *   });
 *
 * HTML
 *
 *   <input type="number" />
 */
 
(function($) {
	$.fn.iPhoneNumberInput = function() {
		if(navigator.userAgent.match(/iPhone|iPod/) == null) {
			return;
		}
		this.each(function() {
			var replace = function() {
				$(this).attr('value', $(this).attr('value').replace(/[^0-9-\.]/g, function(s) {
					switch(s) {
						case '☆': return '11';
						case '♪': return '111';
						case '→': return '1111';
						case String.fromCharCode(0x00a5): return '22'; // yen sign
						case '$': return '222';
						case '￡': return '2222';
						case '%': return '33';
						case '°': return '333';
						case '#': return '3333';
						case '○': return '44';
						case '＊': return '444';
						case '・': return '4444';
						case '+': return '55';
						case '×': return '555';
						case '÷': return '5555';
						case '<': return '66';
						case '=': return '666';
						case '>': return '6666';
						case '「': return '77';
						case '」': return '777';
						case '：': return '7777';
						case '〒': return '88';
						case '々': return '888';
						case '〆': return '8888';
						case '^': return '99';
						case '|': return '999';
						case String.fromCharCode(0x005c): return '9999'; // back slash
						case String.fromCharCode(0x301c): return '00'; // wave dash
						case '…': return '000';
						default: return '';
					}
				}));
			};
			$(this).change(replace);
		});
		return this;
	};
})(jQuery);

