var Qs_Form_Element_Phone = { message: new qs.Message({ 'en': { 'tipAutoFormatting': 'Use the numbers only and formatting will be done automatically.', 'tipDashFormat': 'E.g.: 1231231234123 will be formated as (123) 123-1234 ext. 123.', 'tipDotFormat': 'E.g.: 1231231234123 will be formated as 123.123.1234 ext. 123.' }, 'uk': { 'tipAutoFormatting': 'Використовуйте тільки цифри і форматування буде виконано автоматично.', 'tipDashFormat': 'Наприклад.: 1231231234123 буде відформатований як (123) 123-1234 ext. 123.', 'tipDotFormat': 'Наприклад.: 1231231234123 буде відформатований як 123.123.1234 ext. 123.' }, ru: { 'tipAutoFormatting': 'Используйте только цифры и форматирование будет выполнено автоматически.', 'tipDashFormat': 'Например.: 1231231234123 будет отформатирован как (123) 123-1234 ext. 123.', 'tipDotFormat': 'Например.: 1231231234123 будет отформатирован как 123.123.1234 ext. 123.' } }), element: null, idTimer: null, options: {}, elementOptions: {}, showTooltip: false, init: function (id, options) { var element = document.getElementById(id); if (!element) { alert('Qs_Form_Element_Phone. Can not find element with id = ' + id); return false; } if (!options.format) { alert('Qs_Form_Element_Phone. Phone format is not set'); return false; } if (!element.tagName || element.tagName != 'INPUT') { alert('Qs_Form_Element_Phone. Element (id = ' + id + ') is not "input"'); return false; } if (element.type != 'text' && element.type != 'tel') { alert('Qs_Form_Element_Phone. Element (id = ' + id + ') type is not "text"'); return false; } $(element).focus(Qs_Form_Element_Phone.onFocus); $(element).blur(Qs_Form_Element_Phone.onBlur); $(element).keyup(Qs_Form_Element_Phone.onKeyUp); Qs_Form_Element_Phone.setElementOptions(id, options); if (Qs_Form_Element_Phone.showTooltip) { var tipFormat = 'tip' + _.str.capitalize(options.format); var tip = Qs_Form_Element_Phone.message.get('tipAutoFormatting') + '
' + Qs_Form_Element_Phone.message.get(tipFormat); $(element).tooltip({title: tip, html: true}); } }, getElementOptions: function (id) { if (typeof Qs_Form_Element_Phone.elementOptions[id] == 'undefined') { return null; } return Qs_Form_Element_Phone.elementOptions[id]; }, getElementOption: function (id, name) { var options = Qs_Form_Element_Phone.getElementOptions(id); if (options) { if (typeof options[name] != 'undefined') { return options[name]; } } return null; }, setElementOption: function (id, name, value) { var options = Qs_Form_Element_Phone.getElementOptions(id); if (options) { options[name] = value; } else { Qs_Form_Element_Phone.elementOptions[id] = {name: value}; } }, setElementOptions: function (id, options) { Qs_Form_Element_Phone.elementOptions[id] = options; }, onFocus: function () { $(this).tooltip('show'); return true; }, onBlur: function () { $(this).tooltip('hide'); return true; }, onKeyUp: function () { if (Qs_Form_Element_Phone.idTimer) { if (Qs_Form_Element_Phone.element == this){ clearTimeout(Qs_Form_Element_Phone.idTimer); Qs_Form_Element_Phone.idTimer = null; } else { Qs_Form_Element_Phone.formatReal(); } Qs_Form_Element_Phone.element = null; } Qs_Form_Element_Phone.element = this; Qs_Form_Element_Phone.idTimer = setTimeout("Qs_Form_Element_Phone.formatReal();", 750); }, formatReal: function () { if(!Qs_Form_Element_Phone.element) { return false; } var el = Qs_Form_Element_Phone.element; var format = Qs_Form_Element_Phone.getElementOption(el.id, 'format'); var phone_number = el.value; var new_phone = ''; if (format == 'dotFormat') { new_phone = Qs_Form_Element_Phone.dotFormatString(phone_number); } else { new_phone = Qs_Form_Element_Phone.dashFormatString(phone_number); } if (el.value != new_phone) { el.value = new_phone; } return true; }, dashFormatString: function(phone_number) { var new_phone = '('; phone_number = phone_number.replace(/\D*/g, ""); phone_number = phone_number.substring(0, 16); for (var i = 0; i < phone_number.length; i++) { new_phone = new_phone + phone_number.charAt(i); if ((i == 2 ) && phone_number.charAt(i+1) != '') { new_phone += ') '; } if ((i == 5) && phone_number.charAt(i+1) != '') { new_phone = new_phone + '-'; } if ((i == 9 && phone_number.length > 10) && phone_number.charAt(i+1) != '') { new_phone = new_phone + ' ext. '; } } if (new_phone == '(') { new_phone = ''; } return new_phone; }, dotFormatString: function(phone_number) { var new_phone = ''; phone_number = phone_number.replace(/\D*/g, ""); phone_number = phone_number.substring(0, 16); var length = phone_number.length; var step = 0; var steps = [3, 3, 4]; var stepsSum = 10; for (var i in steps) { step = (length > steps[i]) ? steps[i] : length; new_phone += phone_number.substring(0, step); if (step == steps[i]) { new_phone += '.'; } phone_number = phone_number.substring(step); length = phone_number.length; if ('' == phone_number) { break; } } if (new_phone.length > (stepsSum + 2)) { new_phone = new_phone.substring(0, (stepsSum + 2)); } if ('' != phone_number) { new_phone += ' ext. ' + phone_number; } return new_phone; } }