/*
    According to Duck.com
    ---------------------
    Javascript for AJAX and related functions
*/

var lockAjax;
var http;

lockAjax = false;

function doPost(url, params, func) {
    if (lockAjax == true) {
        alert ("Another Request is already in progress.  Please try again in a moment");
        exit;
    }
    http = createRequestObject();
    if (http) {
        http.onreadystatechange = func;
        http.open('POST', url, true);
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", params.length);
        http.setRequestHeader("Connection", "close");
        http.send(params);
    }
}

function createRequestObject(){
    var xmlHttp=null;
    try{
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e){
        // Internet Explorer
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    if (xmlHttp==null){
        alert ("Your browser does not support AJAX! Please use Internet Explorer 6+, or Firefox!");
        return false;
    }
    return xmlHttp;
}


function sendMD5() {
    sendText = 'hashme=' + document.getElementById('toolInput').value;
    showLoading();
    doPost('/tools/md5.php', sendText, getMD5);
    lockAjax = true;
}

function getMD5() {
    if (http.readyState == 4){
        lockAjax = false;
        document.getElementById('toolOutput').value = http.responseText;
        hideLoading();
    }
}


function sendEncode() {
    sendText = 'encodeme=' + document.getElementById('toolInput').value;
    showLoading();
    doPost('/tools/encode.php', sendText, getEncode);
    lockAjax = true;
}

function getEncode() {
    if (http.readyState == 4){
        lockAjax = false;
        document.getElementById('toolOutput').value = http.responseText;
        hideLoading();
    }
}


function sendDecode() {
    sendText = 'decodeme=' + document.getElementById('toolInput').value;
    showLoading();
    doPost('/tools/decode.php', sendText, getDecode);
    lockAjax = true;
}

function getDecode() {
    if (http.readyState == 4){
        lockAjax = false;
        document.getElementById('toolOutput').value = http.responseText;
        hideLoading();
    }
}

function askTimestamp() {
    showLoading();
    doPost('/tools/timestamp.php', 'update=1', updateTimestamp);
    lockAjax = true;
}

function updateTimestamp() {
    if (http.readyState == 4){
        lockAjax = false;
        document.getElementById('toolOutput').value = http.responseText;
        hideLoading();
    }
}


function sendEncode64() {
    sendText = 'encodeme64=' + document.getElementById('toolInput').value;
    showLoading();
    doPost('/tools/encode64.php', sendText, getEncode64);
    lockAjax = true;
}

function getEncode64() {
    if (http.readyState == 4){
        lockAjax = false;
        document.getElementById('toolOutput').value = http.responseText;
        hideLoading();
    }
}


function sendDecode64() {
    sendText = 'decodeme64=' + document.getElementById('toolInput').value;
    showLoading();
    doPost('/tools/decode64.php', sendText, getDecode64);
    lockAjax = true;
}

function getDecode64() {
    if (http.readyState == 4){
        lockAjax = false;
        document.getElementById('toolOutput').value = http.responseText;
        hideLoading();
    }
}

function toolInfo(text) {
    document.getElementById('toolInfo').innerHTML = text;
}

function prepTools() {
    var elm = document.getElementById('getTime');
    elm.onclick = askTimestamp;
    elm.onmouseover = function () {toolInfo('Returns the Current Unix Timestamp (no Input needed)')};
    elm.onmouseout = function () {toolInfo('Enter an Input and Pick a Function Above.')};

    elm = document.getElementById('getHash');
    elm.onclick = sendMD5;
    elm.onmouseover = function () {toolInfo('Returns the MD5 Hashed Value of Input.')};
    elm.onmouseout = function () {toolInfo('Enter an Input and Pick a Function Above.')};

    elm = document.getElementById('urlEnd');
    elm.onclick = sendEncode;
    elm.onmouseover = function () {toolInfo('Returns the URL Encoded Value of Input.')};
    elm.onmouseout = function () {toolInfo('Enter an Input and Pick a Function Above.')};

    elm = document.getElementById('urlDec');
    elm.onclick = sendDecode;
    elm.onmouseover = function () {toolInfo('Returns the URL Decoded Value of Input.')};
    elm.onmouseout = function () {toolInfo('Enter an Input and Pick a Function Above.')};

    elm = document.getElementById('b64End');
    elm.onclick = sendEncode64;
    elm.onmouseover = function () {toolInfo('Returns the Base64 Encoded Value of Input.')};
    elm.onmouseout = function () {toolInfo('Enter an Input and Pick a Function Above.')};

    elm = document.getElementById('b64Dec');
    elm.onclick = sendDecode64;
    elm.onmouseover = function () {toolInfo('Returns the Base64 Decoded Value of Input.')};
    elm.onmouseout = function () {toolInfo('Enter an Input and Pick a Function Above.')};

}
