/* Stylesheet for GSMS webaccess layer. */
/* $Id: tacuweb.js,v 1.4.4.9 2008/11/01 15:11:33 bmcgee Exp $ */

function enterDetails() {
    var timeNow = new Date();
    var userMsisdn = document.enterNumberAndPin.userMsisdn.value;
    // Normalise the userMsisdn.
    if (userMsisdn.charAt(0) == '0') {
        userMsisdn = countryCode+userMsisdn.substring(1);
    }
    else if (userMsisdn.charAt(0) == '+') {
        userMsisdn = userMsisdn.substring(1);
    }
    // Update the form field with the normalised msisdn.
    document.enterNumberAndPin.userMsisdn.value = userMsisdn;

    var userPin = document.enterNumberAndPin.userPin.value;
    document.enterNumberAndPin.timeStamp.value = timeNow.getTime();
    document.enterNumberAndPin.authToken.value = generateAuthToken(userMsisdn,timeNow.getTime(),userPin);
    document.enterNumberAndPin.userPin.disabled=true;
    document.enterNumberAndPin.submit();
}

function goHelp(locale,baseUrl) {
    var helpFile = "TacuHelp_"+locale+".html";
    document.location = baseUrl+"/webui/help/"+helpFile+"#TacuTextEntry";
}

function generateAuthToken(userMsisdn,timeStamp,userPin) {
    // Return MD5 token.
    var auth = hex_md5(userMsisdn+timeStamp+userPin);
    return auth;
}


function userLogout() {
    document.logOut.userStatus.value = "logout";
    document.logOut.submit();
}


function goList(listName,baseUrl) {
    document.location = baseUrl+"/webui/details?listName="+listName;
}

function goPost(baseUrl) {
    document.location = baseUrl+"/webui/post";
}

function goBack() {
    history.back();
}

function confirmPost() {
    // Display the confirm panel.
    var confirmPanel = document.getElementById("postConfirm");
    var groupsList = getSelectedGroups();
    var postMsg = document.enterText.postMsg.value;
    if (groupsList.length == 0) {
    showErrorPanel("nolist");
    }
    else if (postMsg.length == 0) {
        showErrorPanel("notext");
    }
    else {
        var groupsNode = document.createTextNode(listGroupsText(groupsList));
        var postPara = document.getElementById("postGroups");
        postPara.appendChild(groupsNode);
        var postMsgNode = document.createTextNode(document.enterText.postMsg.value);
        document.getElementById("postText").appendChild(postMsgNode);
        // Set scrollbar to bottom.
        confirmPanel.style.display = "block";
        confirmPanel.scrollTop = confirmPanel.scrollHeight;
    }
}

// Global variable used when processing selected groups.
var listGroups;

function postMessage() {
    // Close the confirm panel.
    // Invoke the post action on each list in the postGroups list.
    // Display status for each post action in the statusArea.

    var confirmPanel = document.getElementById("postConfirm");
    confirmPanel.style.display = "none";
    // Need to remove child elements from postConfirm panel in case a new postMessage is invoked.
    var postPara = document.getElementById("postGroups");
    postPara.removeChild(postPara.firstChild);
    var postText = document.getElementById("postText");
    postText.removeChild(postText.firstChild);

    // Display the statusArea. Append status line for each group in list.
    var statusArea = document.getElementById("statusArea");
    listGroups = getSelectedGroups();
    document.getElementById("statusBtnArea").style.display = "none";
    statusArea.style.display = "block"; 
    processList(0);
}

function showErrorPanel(errorType) {
    document.getElementById(errorType).style.display = "block";
    document.getElementById("postError").style.display = "block";
}

function cancelMessage(panelName) {
    // Could have been called from postConfirm or postError.
    // If called from postConfirm, need to remove child elements from postConfirm panel 
    // in case a new postMessage is invoked.
    if (panelName == "postConfirm") {
        var postPara = document.getElementById("postGroups");
        postPara.removeChild(postPara.firstChild);
        var postText = document.getElementById("postText");
        postText.removeChild(postText.firstChild);
    }
    else if (panelName == "postError") {
        // Switch off error message.
        document.getElementById("nolist").style.display = "none";
        document.getElementById("notext").style.display = "none";
    }
    document.getElementById(panelName).style.display = "none";
}

function closeStatusArea() {
    // Need to clear out child nodes in case a new postMessage is invoked.
    var statusArea = document.getElementById("statusArea");
    statusArea.style.display = "none";
    var statusTextArea = document.getElementById("statusTextArea");
    var newTextArea = statusTextArea.cloneNode(false);
    statusArea.removeChild(statusTextArea);
    statusArea.insertBefore(newTextArea,statusArea.firstChild);
}
    
function getSelectedGroups() {
    var result = new Array();
    // Get the rows from listGroups. Any row that contains a checked checkbox is selected.
    var groupsTable = document.getElementById("listGroups");
    var tableRows = groupsTable.rows;
    // We are only interested in TD elements.
    for (var i=0; i<tableRows.length; i++) {
        var rowCells = tableRows[i].cells;
        if (rowCells[0].tagName == "TD") {
            if (rowCells[1].firstChild.checked == true) {
                result.push(rowCells[0].firstChild.firstChild);
            }
        }
    }
    return result;
}

function listGroupsText(listGroups) {
    var result = "";
    var firstResult = true;
    for (var i=0; i<listGroups.length; i++) {
        if (!firstResult) {
            result += ", ";
        }
        result += listGroups[i].nodeValue;
        firstResult = false;
    }
    return result;
}


// Character counting methods.
var maxChars = 120;

function taLimit(taObj) {
    if (taObj.value.length == maxChars) {
        return false;
    }
    return true;
}

function taCount(taObj,Cnt) {
    objCnt = document.getElementById(Cnt);
    objVal = taObj.value;
    if (objVal.length > maxChars-10) {
        objCnt.style.color = "red";
    }
    else {
        objCnt.style.color = "black";
    }
    if (objVal.length > maxChars) {
        objVal = objVal.substring(0,maxChars);
        taObj.value = objVal;
    }
    if (objCnt) {
        objCnt.innerHTML = maxChars-objVal.length;
    }
    return true;
}

function processList(index) {
    var f = window.frames["postFrame"];
    var tempStr = document.enterText.postMsg.value;
    // If message contains a newline, replace it with a space.
    if (tempStr.search(/[\u000A]/) != -1) {
        tempStr = tempStr.replace(/[\u000A]/g," ");
    }
    // If message contains a Euro symbol, replace it with "E".
    if (tempStr.search(/[\u20AC]/) != -1) {
        tempStr = tempStr.replace(/[\u20AC]/g,"E");
    }
    // If message has non-ascii characters, replace with "?".
    if (tempStr.search(/[^\u0000-\u007F]/) != -1) {
        tempStr = tempStr.replace(/[^\u0000-\u007F]/g,"?");
    }

    var postMsg = encodeURIComponent(tempStr);
    var timeNow = new Date();
    var postURL = "";
    var newIndex;
    if (index < listGroups.length) {
        // Use relative url.
        postURL = "postmsg?listName=" + listGroups[index].nodeValue + "&postMsg=" + postMsg;
    }
    
    if (postURL.length > 0) {
        f.location.replace(postURL);
        // Update the status area.
        var paraNode = document.createElement("p");
        paraNode.setAttribute("class","statusLine");
        var statusText = document.createTextNode(postStatusGroup+' '+listGroups[index].nodeValue+'.');
        paraNode.appendChild(statusText);
        document.getElementById("statusTextArea").appendChild(paraNode);
        // Set scrollbar to bottom.
        var scrollArea = document.getElementById("statusArea");
        scrollArea.scrollTop = scrollArea.scrollHeight;        

        // Wait, and post to the next group.    
        newIndex = index+1;
        setTimeout("processList("+newIndex+")",1000);
    }
    else {
        // No more groups. Enable the close button and scroll to bottom.
        document.getElementById("statusBtnArea").style.display = "block";
        var scrollArea = document.getElementById("statusArea");
        scrollArea.scrollTop = scrollArea.scrollHeight;        
    }
}

function getTimestamp(timeNow) {
    var year = timeNow.getFullYear().toString();
    var month = timeNow.getMonth().toString();
    if (month.length == 1) {
        month = "0"+month;
    }
    var date = timeNow.getDate().toString();
    if (date.length == 1) {
        date = "0"+date;
    }
    var hours = timeNow.getHours().toString();
    if (hours.length == 1) {
        hours = "0"+hours;
    }
    var minutes = timeNow.getMinutes().toString();
    if (minutes.length == 1) {
        minutes = "0"+minutes;
    }

    return year+month+date+":"+hours+minutes;
}
    

