var call_span;

function call_init()
{
    call_span = document.getElementById("call_span");
    var a = document.createElement("a");
    addEvent(a, "click", call_link_click);
    a.appendChild(document.createTextNode("Get a sample call right now!"));
    call_span.appendChild(a);
}

function call_link_click()
{
    if(!window.RegExp)
    {
        alert("Sorry, looks like you're using an older browser that doesn't support this feature. Please upgrade or try on a different machine.");
        return;
    }

    var div = document.getElementById("call_div");
    if(!div)
    {
        div = document.createElement("div");
        document.getElementsByTagName("body")[0].appendChild(div);
        div.setAttribute("id", "call_div");
        div.className = "livedemo";
    }
    else
    { removeChildren(div); }

    div.style.top = findPosY(call_span)+30+"px";
    div.style.left = "250px";
    var h1 = document.createElement("h1");
    h1.appendChild(document.createTextNode("Get a Call"));
    div.appendChild(h1);
    div.appendChild(document.createElement("hr"));
    var input_p = document.createElement("p");
    input_p.appendChild(document.createTextNode("Your phone number: "));
    var phone_input = document.createElement("input");
    phone_input.setAttribute("id", "phone_input");
    input_p.appendChild(phone_input);
    div.appendChild(input_p);
    var p = document.createElement("p");
    var reserve_button = make_button("Reserve Notice");
    reserve_button.setAttribute("call", "reserve");
    addEvent(reserve_button, "click", verify);
    var overdue_button = make_button("Overdue Notice");
    overdue_button.setAttribute("call", "overdue");
    addEvent(overdue_button, "click", verify);
    var cancel_button = make_button("Close");
    addEvent(cancel_button, "click", close_div);
    p.appendChild(reserve_button);
    p.appendChild(document.createTextNode(" "));
    p.appendChild(overdue_button);
    p.appendChild(document.createTextNode(" "));
    p.appendChild(cancel_button);
    div.appendChild(p);
    var note_p = document.createElement("p");
    note_p.appendChild(document.createTextNode("Note: CallerID will show our toll-free number."));
    note_p.appendChild(document.createElement("br"));
    note_p.appendChild(document.createTextNode("When in actual use, it will display your library's phone number."));
    div.appendChild(note_p);
    phone_input.focus();
}

function verify()
{
    var phone = document.getElementById("phone_input").value;
    phone = phone.replace(/\D/g, "");
    if(phone.length != 10)
    { alert("This must be a 10-digit phone number.\nPunctuate it any way you like."); }
    else
    {
        var div = document.getElementById("place_call_div");
        if(!div)
        {
            div = document.createElement("div");
            div.setAttribute("id", "place_call_div");
            div.className = "object_hide";
            document.getElementsByTagName("body")[0].appendChild(div);
        }
        else
        { removeChildren(div); }

        var img = document.createElement("img");
        img.src = "call.pl?rand="+Math.random()+"&number="+phone+"&call="+this.getAttribute("call");
        div.appendChild(img);

        alert("Please wait while the call is placed.\nAnswer normally, as you would any call.");
    }
}

function do_print()
{
    var object_div = document.getElementById("object_div");
    removeChildren(object_div);

    object_tag = document.createElement("object");
/*    object_tag.setAttribute("type", "text/html");
    object_tag.setAttribute("data", "wrapper.pl");*/
    object_tag.setAttribute("type", "application/pdf");
    object_tag.setAttribute("data", "wrapper.pl?last_name="+document.getElementById("name_input").value);
    object_div.appendChild(object_tag);
}

function close_div()
{
    var div = document.getElementById("call_div");
    if(div)
    { div.parentNode.removeChild(div); }
}

function addEvent(obj, evType, fn)
{
    if (obj.addEventListener)
    { obj.addEventListener(evType, fn, false); }
    else
    { obj.setAttribute("on"+evType, fn); }
}

function removeChildren(object)
{
    if(object)
    {
        while(object.firstChild)
        { object.removeChild(object.firstChild); }
    }
}

function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
    {
        while(1)
        {
            curtop += obj.offsetTop;
            if(!obj.offsetParent)
            { break; }
            obj = obj.offsetParent;
        }
    }
    else if(obj.y)
    { curtop += obj.y; }
    return curtop;
}

function make_button(text)
{
    if(!text)
    { text = "Edit"; }

    var button = document.createElement("button");
    button.appendChild(document.createTextNode(text));
    return button;
}
