var acnumber = "";
var incall = false;
var firstReg = true;
var phoneStatus;
var phoneErrormess;
var phoneChanged;
var phone;
var loggedIn = false;


function numbersonly(e) {
    if (!e) {
        e = event;
    }
    var unicode = e.charCode ? e.charCode : e.keyCode;
    //alert('numbersonly, unicode=' + unicode);

    // backspace or delete
    if (unicode==8 || unicode==46) {
        if (incall) {
            hangup();
        } else {
            // remove last digit
            acnumber=acnumber.substring(0, acnumber.length-1);
            document.getElementById('NumberBar').innerHTML = acnumber;
        }
    }

    // return
    if (unicode==13) {
        if (!incall) {
            send();
        }
    }
    if ((unicode==42)||(unicode==35)||(unicode>=48 && unicode<=57)) {
        var but = String.fromCharCode(unicode);
        buttonPressed(but);
    }
    return false;
}

function areyousure(e) {
    var retval;
    if (incall) {
        retval="You are in a call at the moment. If you leave this page, your call will be cut off.";
    }
    return retval;
}




// All the functions that call the applet
function phoneStatusPoll(){
    var phone = getPhoneFromHere();
    if (phone){
        statusjson = phone.getCallStatus();
        phoneStatus = eval("(" + statusjson + ")");
        phoneStatus.status="Loaded....";
    } else {
        phoneStatus = new Object();
        phoneStatus.status="Loading....";
    }
    // displayStatus();
}
function dial() {
    var phone = getPhoneFromHere();
    if (phone) {
        var no = acnumber;
        phone.dial(acnumber);
        var mbar = document.getElementById('MessageBar');
        mbar.innerHTML = "Dialling";
    }
    return false;
}
function hangup() {
    var mbar = document.getElementById('NumberBar').innerHTML = '';
    acnumber = "";
    var phone = getPhoneFromHere();
    if (phone) {
        phone.hangup();
    }
    incall = false;
}
function answer() {
    return false;
}
function buttonPressed(but) {
    if (incall) {
        var phone = getPhoneFromHere();
        if (phone) {
            phone.sendDTMF(but.charCodeAt(0));
        }
    }
    acnumber += but;
    document.getElementById('NumberBar').innerHTML = acnumber;
}



// All the functions called by the applet
function phonefromherestatus(what, error, statusjson) {
    phoneErrormess = error;
    phoneStatus = eval("(" + statusjson + ")");

    var mbar = document.getElementById('MessageBar');
    if (what == 1) {
        // HOOK
        incall = false;
        mbar.innerHTML = "Ready to dial";
    } else if (what == 2) {
        // CONNECTED
        incall = true;
        mbar.innerHTML = "Answered";
    } else if (what == 3) {
        // RINGING
        mbar.innerHTML = "Ringing";
    }
}

function phonefromhereGotDtmf(digit) {
}

function phonefromhereGotText(text) {
}


// The rest of the functions (applet unrelated)
function logmein () {
    if (loggedIn == false) {
        var appletDiv = document.getElementById('appletContainer');
        var u = document.getElementById('username').value;
        var p = document.getElementById('password').value;

        var appletText = makeApplet(1, u, p);
        appletDiv.innerHTML = appletText;

        loggedIn = true;
        phoneStatusPollStart();

        document.onkeypress = numbersonly;
        window.onbeforeunload = areyousure;
    } else {
        var mbar = document.getElementById('MessageBar');
        mbar.innerHTML = "Already logged in";
    }

    return false;
}

function phoneStatusPollStart(){
    statusTimer = window.setInterval(phoneStatusPoll, 1000);
}

function send() {
    dial();
}

