function copyToClipboard(id){
    var a = document.getElementById(id);
    window.clipboardData.setData('text',a.value);
}

var state = 'hidden';

function showhide(div1,div2) {
    if(document.getElementById("filename").value.length==0){
        alert("Please select an image file from your computer first.");
        return false;
    }

    if (state == 'visible') {
        state = 'hidden';
    } else {
        state = 'visible';
    }
    if (document.all) {
        eval( "document.all." + div1 + ".style.visibility = state");
        eval( "document.all." + div2 + ".style.visibility = state");
    }
    if (document.getElementById && !document.all) {
        div1 = document.getElementById(div1);
        div2 = document.getElementById(div2);
        div1.style.visibility = state;
        div2.style.visibility = state;
    }
}

function startUpload(){
    document.getElementById('controlit').innerHTML = '<a HREF="/">Cancel</a>';
    openProgressBar();
    return true;
}

function stopUpload(success,para){
    if (success == 1){
        window.location.href='/b_'+para+'.html';
    }
    else {
        document.getElementById('notifier').style.color = '#FF0000';
        document.getElementById('notifier').innerHTML = 'There is an error when converting. Maybe the server is busy or the uploaded file is NOT an image.';
        document.getElementById('controlit').innerHTML = '<a HREF="/">Retry</a>';
    }
    return true;
}

interval = null;

function createXmlHttpRequestObj()
{
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }

    return xmlHttp;
}


function fetch(uuid) {
    req = createXmlHttpRequestObj();
    req.open("GET", "/progress?X-Progress-ID="+uuid, 1);

    // req.setRequestHeader("X-Progress-ID", uuid);
    req.onreadystatechange = function () {
        if (req.readyState == 4) {
            if (req.status == 200) {

                var upload = eval(req.responseText);

                document.getElementById('status').innerHTML = "State: "+upload.state;


                if (upload.state == 'done' || upload.state == 'uploading') {
                    bar = document.getElementById('progressbar');
                    w = 300 * upload.received / upload.size;
                    bar.style.width = w + 'px';

                    document.getElementById('percent').innerHTML = Math.ceil((upload.received / upload.size) * 100) + '%';
                }

                if (upload.state == 'done') {
                    window.clearTimeout(interval);
                }
            }
        }
    }
    req.send(null);
}

function openProgressBar() {

    uuid = "";
    for (i = 0; i < 32; i++) {
        uuid += Math.floor(Math.random() * 16).toString(16);
    }

    document.getElementById("converter").action="uploading.php?X-Progress-ID=" + uuid;


    interval = window.setInterval(
        function () {
            fetch(uuid);
        },
        1000
        );
}

