var Qs_Form_Element_Phone = { message: new Qs_Message({ 'eng': { }, 'uk': { } }), element: null, idTimer: null, options: {}, elementOptions: {}, 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 (!element.tagName || element.tagName != 'INPUT') { alert('Qs_Form_Element_Phone. Element (id = ' + id + ') is not "input"'); return false; } if (element.type != 'text') { 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); $(element).qtip({ content: 'Use the numbers only and formatting will be done automatically.
' + 'E.g.: 1231231234123456 will be formated as (123)-123-1234 ext. 123456', prerender: true, position: { corner: { target: 'topMiddle', tooltip: 'bottomLeft' } }, style: { tip:true, width:500 } }); }, 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).qtip('show'); return true; }, onBlur: function () { $(this).qtip('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 phone_number = el.value; 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 = ''; } if (el.value != new_phone) { el.value = new_phone; } return true; } }