﻿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);
        includeWzTooltip();
    },
    
    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 ()
    {
        var hintContainer = document.getElementById('phone_hint')
        if (!hintContainer) {
            var hint = document.createElement("div");
            hint.id = 'phone_hint';
            hint.className = 'phone_hint';
            hint.innerHTML = 'Use the numbers only and formatting will be done automatically.<br />E.g.: 1231231234123456 will be formated as 123-123-1234 ext. 123456';
            hint.style.display = 'none';
            document.body.appendChild(hint);
        }
        TagToTip('phone_hint', BALLOON, true, ABOVE, true, FIX, [this, 0, 0], DELAY, 0);
        return true;
    },
    
    onBlur: function ()
    {
        UnTip('phone_hint');
        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;
    }
}