
var AllOperations = new Array(100);
var AllDescriptions = new Array(100);
var AllImages = new Array(100);
var CurrentPosition = -1
var MaxPosition =  -1;
var CurrentDescription = '';
var CurrentImage = '';
var CachedLabelCommand = '';
var CachedLabelDescription =  '';

function selectCategory(cat)
{
    hide("cat0");
    hide("cat1");
    hide("cat2");
    hide("cat3");
    hide("cat4");
    hide("cat5");
    hide("cat6");
    hide("cat7");
    hide("cat8");
    hide("cat9");
    hide("cat10");
    hide("cat11");
    hide("cat12");
    hide("V1");

    show(cat);
}

function clearSelections()
{
    document.getElementById('cat0').selectedIndex = -1;
    document.getElementById('cat1').selectedIndex = -1;
    document.getElementById('cat2').selectedIndex = -1;
    document.getElementById('cat3').selectedIndex = -1;
    document.getElementById('cat4').selectedIndex = -1;
    document.getElementById('cat5').selectedIndex = -1;
    document.getElementById('cat6').selectedIndex = -1;
    document.getElementById('cat7').selectedIndex = -1;
    document.getElementById('cat8').selectedIndex = -1;
    document.getElementById('cat9').selectedIndex = -1;
    document.getElementById('cat10').selectedIndex = -1;
    document.getElementById('cat11').selectedIndex = -1;
    document.getElementById('cat12').selectedIndex = -1;
}


function setImage(value)
{
    element = document.getElementById('img1');
    element.src = value;
}

function getImage()
{
    element = document.getElementById('img1');
    return element.src;
}


function selectColorValue(id,value)
{
    var element = document.getElementById(id);
    if (element != null)
    {
        element.innerHTML = value;
        element.style.backgroundColor = value;
    }
}

function selectTextValue(id,value)
{
    var element = document.getElementById(id);
    if (element != null)
    {
        element.innerHTML = value;
    }
}

function showColorFonts(id,command,description)
{
    CachedLabelCommand = command;
    CachedLabelDescription = description;
    show(id);
}




function reportError(error)
{
    AllImages[CurrentPosition] = CurrentImage;

    setImage(CurrentImage);
    var element = document.getElementById('count');
    element.innerHTML = error;
}


function hide(id)
{
    var element = document.getElementById(id);
    if (element != null)
    {
        element.style.display = 'none';
    }
    return;
}

function show(id)
{
    var element = document.getElementById(id);
    if (element != null)
        element.style.display = 'block';
    return;
}

function addImage(image,stats)
{
    MaxPosition++;
    AllImages[MaxPosition] = image;
    CurrentPosition = MaxPosition;
    AllDescriptions[CurrentPosition] = 'Image Loaded'+stats;
    setImage(image);
    updateOperationDisplay();
}

function reportError(error)
{
    AllImages[CurrentPosition] = CurrentImage;
    setImage(CurrentImage);
    var element = document.getElementById('operation');
    element.innerHTML = error;
}



function updateOperationDisplay()
{
    var c, m, display, click;
    var element = document.getElementById('operation');

    if (CurrentPosition >= 0)
    {
        currentDescription = AllDescriptions[CurrentPosition];
        currentDescription = currentDescription.replace(/_/g," ");
        c = CurrentPosition+1;
        m = MaxPosition+1;
        display = ''+c+'/'+m+':';
        display = display+'  '+currentDescription;
    }
    else
    {
        display = "&nbsp;";
    }
    

    if (MaxPosition >= 1)
    {
        var nav = navigator.appVersion.toLowerCase();
        if (nav.indexOf("mac") != -1)
            click = "   (control-click to save)";
        else
            click = "   (right-click to save)";
        display += click;
    }
    element.innerHTML = display;


    return;
}



function nextImage()
{
    if (CurrentPosition < 0)
        return;
    CurrentPosition++;
    if (CurrentPosition > MaxPosition)
        CurrentPosition = 0;
    image = AllImages[CurrentPosition];
    setImage(image);
    updateOperationDisplay();
}


function previousImage()
{
    if (CurrentPosition < 0)
        return;

    CurrentPosition--;
    if (CurrentPosition < 0)
        CurrentPosition = MaxPosition;
    image = AllImages[CurrentPosition];
    setImage(image);
    updateOperationDisplay();
}


function undoImage()
{
    if (CurrentPosition < 0)
        return;
    if (MaxPosition < 0)
        return;

    AllImages.splice(CurrentPosition,1);
    AllDescriptions.splice(CurrentPosition,1);
    MaxPosition--;
    if (MaxPosition < 0)
    {
        CurrentPosition = -1;
        image = "http://www.ezimba.com/images/rect256.png";
    }
    else
    {
        if (CurrentPosition > MaxPosition)
            CurrentPosition = 0;
        image = AllImages[CurrentPosition];
    }
    setImage(image);
    updateOperationDisplay();
}


function executeLoad()
{
/*
    var element = document.getElementById('load');
    if (element != null)
        element.style.display = 'none';
*/

    CurrentImage = getImage();
    setImage("http://www.ezimba.com/images/busy.gif");
}

function executeLabelCommand()
{
    hide('V1');

    var element;
    var command = CachedLabelCommand;
    var description = CachedLabelDescription;
    var label = '';
    var labelcolor = '';
    var backgroundcolor = '';
    var font = '';

     element = document.getElementById('label');
        label = element.value;
    element = document.getElementById('labelcolor');
        labelcolor = element.innerHTML;
    element = document.getElementById('backgroundcolor');
        backgroundcolor = element.innerHTML;
    element = document.getElementById('font');
        font = element.innerHTML;

    command = command+'&LABEL='+label+'&COLORLABEL='+labelcolor+'&COLORBACKGROUND='+backgroundcolor+'&FONT='+font;
    description = CachedLabelDescription;
    executeCommand(command,description);
} 

function executeUserCommand(usercommand)
{
    var opdesc;

    if (usercommand.indexOf("EZIMBALABEL") >= 0)
    {
        if (CurrentPosition < 0)
            return;
        if (MaxPosition < 0)
            return;
        usercommand = usercommand.replace("EZIMBALABEL",""); 
        opdesc = usercommand.split(":");
        showColorFonts('V1',opdesc[0],opdesc[1]);
    }
    else
    {
        opdesc = usercommand.split(":");
        executeCommand(opdesc[0], opdesc[1]);
    }
}


function executeCommand(command,description)
{
    if (CurrentPosition < 0)
        return;
    if (MaxPosition < 0)
        return;

    CurrentDescription = description;
    CurrentImage = getImage();
    setImage("http://www.ezimba.com/images/busy.gif");

    image = AllImages[CurrentPosition];
    image = image.replace("http://www.ezimba.com",""); 
    command = command.replace("EZIMBA-KAKAPOIMAGE",image); 

    var previousImages = '';
    var i = CurrentPosition;
    for (i = CurrentPosition - 1; i >=0; i--)
    {
        image = AllImages[i];
        image = image.replace("http://www.ezimba.com",""); 
        previousImages += image;
        previousImages += 'XX';
    }
    command  += '&PREVIOUSIMAGES=';
    command  += previousImages;

    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var url="http://www.ezimba.com/xiphone/niop.html";
    xmlHttp.onreadystatechange=executeCommandFinish;
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(command);
} 


function executeCommandFinish() 
{ 
    if (xmlHttp.readyState==4)
    { 
        s = xmlHttp.responseText;
        if (s.length > 2)
        {
            image = s.replace("EZIMBA-SUCCESS","http://www.ezimba.com"); 
            setImage(image);
            MaxPosition++;
            AllImages[MaxPosition] = image;
            CurrentPosition = MaxPosition;
            AllDescriptions[CurrentPosition] = CurrentDescription;
            updateOperationDisplay();
        }
    }
    clearSelections();
}

function GetXmlHttpObject()
{
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");
        }
    }
    return xmlHttp;
}

