﻿var lastServicePopup = '';

function ToggleServicePopup(Source, PopupID) {
    
    var Popup = window.document.getElementById(PopupID);
    var Paragraph = null;

    if (lastServicePopup != '' && Source.id != lastServicePopup) {
        window.document.getElementById(lastServicePopup + '_p').style.display = 'none';
    }

    Paragraph = window.document.getElementById(Source.id + '_p');

    if (Popup.style.display != '') {
        Popup.style.display = ''; 
    }
    else {
        Popup.style.display = 'none';
    }

    Paragraph.style.display = Popup.style.display;

    lastServicePopup = Source.id;
}

function FormatPhone(Object)
{
    var number = GetValidPhoneCharacters(Object.value);
    var regexObj = null;

    if (number.length > 0)
    {
        regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

        if (regexObj.test(number))
        {
            number = number.replace(regexObj, "($1) $2-$3");
        }
    }

    Object.value = number;
}

function GetValidPhoneCharacters(Value)
{
    var number = "";

    for (i = 0; i < Value.length; i++)
    {
        if (window.parseInt(Value[i]) > -1)
        {
            number = number + Value[i];
        }
        else if (Value[i].indexOf('(') > -1 || Value[i].indexOf(')') > -1 || Value[i].indexOf('-') > -1 || Value[i].indexOf('.') > -1  || Value[i].indexOf(' ') > -1)
        {
            number = number + Value[i];
        }
    }

    return number;
}
