//alert("js OK");

function addEvent(elm, evType, fn, useCapture)
// cross-browser event handling for IE5+, NS6+ and Mozilla 
// By Scott Andrew 
{
  if (elm.addEventListener) { 
    elm.addEventListener(evType, fn, useCapture); 
    return true; 
  } else if (elm.attachEvent) { 
    var r = elm.attachEvent('on' + evType, fn); 
    return r; 
  } else {
    elm['on' + evType] = fn;
  }
}

function dropEvent(elm, evType, fn, useCapture)
// cross-browser event handling for IE5+, NS6+ and Mozilla 
// By Greg Robbins :P 
{
  if (elm.removeEventListener) { 
    elm.removeEventListener(evType, fn, useCapture); 
    return true; 
  } else if (elm.detachEvent) { 
    var r = elm.detachEvent('on' + evType, fn); 
    return r; 
  } else {
    elm['on' + evType] = null;
  }
}

// climb up the tree to the supplied tag.
function ascendDOM(e, target) {
  while (e.nodeName.toLowerCase() != target && e.nodeName.toLowerCase() != 'html')
  {
    e = e.parentNode;
    //alert(e);
  }
  return (e.nodeName.toLowerCase() == 'html') ? null : e;
}

function fetchTarget(e)
{
    if(window.event && window.event.srcElement)
    {
        return window.event.srcElement;
    }
    else if(e && e.target)
    {
        return e.target;
    }
    else
    {
        return;
    }
}

function cancelClick()
{
    return false;
}


function addAllEvents()
{
    fetchMapElements();
}


function fetchMapElements()
{
    areas = document.getElementsByTagName("AREA");
    if(areas.length < 1) return;
    
    mapPreload();
    
    for(i = 0; i < areas.length; i++)
    {
        myArea = areas[i];
        if(myArea.id.match(/mapSrc_([a-z]+)/))
        {
            myAreaObj = document.getElementById(myArea.id)
            addEvent(myAreaObj, "mouseover", mapOver, false);
            addEvent(myAreaObj, "mouseout", mapOut, false);
            //addEvent(myAreaObj, "click", mapClick, false);
            
            refArr = myAreaObj.id.match(/mapSrc_([a-z]+)/);
            ref = refArr[1];
            myLinkObj = document.getElementById("mapLink_" + ref);
            if(myLinkObj)
            {
                addEvent(myLinkObj, "mouseover", mapOver, false);
                addEvent(myLinkObj, "mouseout", mapOut, false);
                //addEvent(myLinkObj, "click", mapClick, false);
            }
        }
    }
}

function mapPreload()
{
    if(document.images)
    {
        map1 = new Image(598,329);
        map1.src = "img_std/map_australia.gif";
        map2 = new Image(598,329);
        map2.src = "img_std/map_asia.gif";
        map3 = new Image(598,329);
        map3.src = "img_std/map_europe.gif";
        map4 = new Image(598,329);
        map4.src = "img_std/map_northamerica.gif";
        map5 = new Image(598,329);
        map5.src = "img_std/map_southamerica.gif";
    }
}

function mapOver(e)
{
    target = fetchTarget(e);
    refArr = target.id.match(/(mapSrc|mapLink)_([a-z]+)/);
    ref = refArr[2];
    
    linkObj = document.getElementById("mapLink_" + ref);
    linkObj.className = "on";
    
    mapObj = document.getElementById("worldMap");
    mapObj.src = "img_std/map_" + ref + ".gif";
    
}

function mapOut(e)
{
    linkObj = document.getElementById("mapLink_" + ref);
    linkObj.className = "";
    
    mapObj = document.getElementById("worldMap");
    mapObj.src = "img_std/map_off.gif";
}

function mapClick(e)
{
    target = fetchTarget(e);
    refArr = target.id.match(/mapSrc_([a-z]+)/);
    ref = refArr[1];
    alert(ref);
}

function copyFormData(srcLang, destLang, fields)
{
    argv = copyFormData.arguments;
    argc = argv.length;
    for(i = 2; i < argc; i++)
    {
        srcId = argv[i] + "_" + srcLang;
        destId = argv[i] + "_" + destLang;
        srcObj = document.getElementById(srcId);
        destObj = document.getElementById(destId);
        if(srcObj.nodeName == "INPUT" || srcObj.nodeName == "TEXTAREA")
        {
            destObj.value = srcObj.value;
        }
    }
}

function copyContent(srcLang, destLang, idField)
{
    srcEditor = tinyMCE.getInstanceById(idField + "_" + srcLang);
    destEditor = tinyMCE.getInstanceById(idField + "_" + destLang);
    
    srcContent = srcEditor.getHTML();
    destEditor.setHTML(srcContent);
}


function loadSectionShiftButtons()
{
    //get all images
    allImages = document.getElementsByTagName("IMG");
    
    //loop thru images
    for(i = 0; i < allImages.length; i++)
    {
        imageId = allImages[i].id;
        //shiftUp_sec_3 , shiftDown_sec_3
        if(imageId.match(/shift(Up|Down)_sec_([0-9]+)/))
        {
            addEvent(allImages[i], "click", shiftSection, false);
            addEvent(allImages[i], "mouseover", shiftRoll, false);
            addEvent(allImages[i], "mouseout", shiftRoll, false);
        }
    }
}

function loadPageShiftButtons()
{
    //get all images
    allImages = document.getElementsByTagName("IMG");
    
    //loop thru images
    for(i = 0; i < allImages.length; i++)
    {
        imageId = allImages[i].id;
        //shiftUp_sec_3 , shiftDown_sec_3
        if(imageId.match(/shift(Up|Down)_page_([0-9]+)/))
        {
            addEvent(allImages[i], "click", shiftPage, false);
            addEvent(allImages[i], "mouseover", shiftRoll, false);
            addEvent(allImages[i], "mouseout", shiftRoll, false);
        }
    }
}

function shiftRoll(e)
{
    target = fetchTarget(e);
    currSourceArr = target.src.match(/shift(Up|Down)_(on|off)/);
    newOnOff = currSourceArr[2] == "on" ? "off" : "on";
    newSrc = "img_std/shift" + currSourceArr[1] + "_" + newOnOff + ".gif";
    target.src = newSrc;
}

function shiftSection(e)
{
    target = fetchTarget(e);
    //alert(target.id);
    matches = target.id.match(/shift(Up|Down)_sec_([0-9]+)/);
    shiftMethod = matches[1] == "Up" ? "up" : "down";
    idSection = matches[2];
    //alert(direction + "\n" + idSection);
    ajaxObj.call("action=ajax_shiftSection&shiftMethod=" + shiftMethod + "&idSection=" + idSection, shiftSectionResp);
}

function shiftPage(e)
{
    target = fetchTarget(e);
    //alert(target.id);
    matches = target.id.match(/shift(Up|Down)_page_([0-9]+)/);
    shiftMethod = matches[1] == "Up" ? "up" : "down";
    idPage = matches[2];
    //alert(shiftMethod + "\n" + idPage);
    ajaxObj.call("action=ajax_shiftPage&shiftMethod=" + shiftMethod + "&idPage=" + idPage, shiftPageResp);
}

function shiftSectionResp(resp)
{
    idSection = resp.idSection;
    affectedIdSection = resp.affectedIdSection;
    shiftMethod = resp.shiftMethod;
    
    //fetch target table & buttons
    shiftTableId = "secTable_" + idSection;
    shiftTableObj = document.getElementById(shiftTableId);
    shiftTableUpButton = document.getElementById("shiftUp_sec_" + idSection);
    shiftTableDownButton = document.getElementById("shiftDown_sec_" + idSection);
    
    //fetch affected table & buttons
    affectedTableId = "secTable_" + affectedIdSection;
    affectedTableObj = document.getElementById(affectedTableId);
    affectedTableUpButton = document.getElementById("shiftUp_sec_" + affectedIdSection);
    affectedTableDownButton = document.getElementById("shiftDown_sec_" + affectedIdSection);
    
    parentNodeObj = shiftTableObj.parentNode;
    
    if(shiftMethod == "down")
    {
        removedNode = parentNodeObj.removeChild(affectedTableObj);
        parentNodeObj.insertBefore(removedNode, shiftTableObj);
    }
    if(shiftMethod == "up")
    {
        removedNode = parentNodeObj.removeChild(shiftTableObj);
        parentNodeObj.insertBefore(removedNode, affectedTableObj);
        //turn off shiftTableUpButton!
        shiftTableUpButton.src = shiftTableUpButton.src.replace(/on/,"off");
    }
    
    //check buttons
    //get all the tables
    allTables = parentNodeObj.getElementsByTagName("TABLE");
    //loop thru
    for(i = 0; i < allTables.length; i++)
    {
        //get idSection for this table
        matches = allTables[i].id.match(/secTable_([0-9]+)/);
        useId = matches[1];
        upButton = document.getElementById("shiftUp_sec_" + useId);
        downButton = document.getElementById("shiftDown_sec_" + useId);
        //if this is the first table, hide the UP button
        if(i == 0)
        {
            upButton.style.display = "none";
        }
        //if this is the last table, hide the DOWN button
        else if(i == (allTables.length - 1))
        {           
            downButton.style.display = "none";
        }
        //for all others, display both buttons
        else
        {
            upButton.style.display = "inline";
            downButton.style.display = "inline";
        }
    }
}

function shiftPageResp(resp)
{
    idPage = resp.idPage;
    affectedIdPage = resp.affectedIdPage;
    shiftMethod = resp.shiftMethod;
    
    //alert("idPage = " + idPage + ", affectedIdPage = " + affectedIdPage + ", shiftMethod = " + shiftMethod);
    //return;
    
    //fetch target row & buttons
    shiftRowId = "pageRow_" + idPage;
    shiftRowObj = document.getElementById(shiftRowId);
    shiftRowUpButton = document.getElementById("shiftUp_page_" + idPage);
    shiftRowDownButton = document.getElementById("shiftDown_page_" + idPage);
    
    //fetch affected row & buttons
    affectedRowId = "pageRow_" + affectedIdPage;
    affectedRowObj = document.getElementById(affectedRowId);
    affectedRowUpButton = document.getElementById("shiftUp_page_" + affectedIdPage);
    affectedRowDownButton = document.getElementById("shiftDown_page_" + affectedIdPage);
    
    //swap classes
    affectedRowObj.className = affectedRowObj.className == "dk" ? "lt" : "dk";
    shiftRowObj.className = shiftRowObj.className == "dk" ? "lt" : "dk";  
    
    parentNodeObj = shiftRowObj.parentNode;
    
    if(shiftMethod == "down")
    {
        removedNode = parentNodeObj.removeChild(affectedRowObj);
        parentNodeObj.insertBefore(removedNode, shiftRowObj);
    }
    if(shiftMethod == "up")
    {
        removedNode = parentNodeObj.removeChild(shiftRowObj);
        parentNodeObj.insertBefore(removedNode, affectedRowObj);
        //turn off shiftRowUpButton!
        shiftRowUpButton.src = shiftRowUpButton.src.replace(/on/,"off");
    }
    
    //check buttons
    //get all the rows
    allRows = parentNodeObj.getElementsByTagName("TR");
    //loop thru (start at 2nd row)
    for(i = 1; i < allRows.length; i++)
    {
        //get idPage for this row
        matches = allRows[i].id.match(/pageRow_([0-9]+)/);
        useId = matches[1];
        upButton = document.getElementById("shiftUp_page_" + useId);
        downButton = document.getElementById("shiftDown_page_" + useId);
        
        //find the secRowSpan_xx TH
        thElements = allRows[i].getElementsByTagName("TH");
        //found it
        if(thElements.length > 0)
        {
            secRowSpanObj = allRows[i].removeChild(thElements[0]);
        }
        
        //if this is the second row
        if(i == 1)
        {
            //hide the UP button
            upButton.style.display = "none";
        }
        //if this is the last row, hide the DOWN button
        else if(i == (allRows.length - 1))
        {           
            downButton.style.display = "none";
        }
        //for all others, display both buttons
        else
        {
            upButton.style.display = "inline";
            downButton.style.display = "inline";
        }
    }
    
    //insert secRowSpan into firstRow
    allRows[1].insertBefore(secRowSpanObj, allRows[1].firstChild);
}

function loadConstantEditButtons()
{
    allAnchors = document.getElementsByTagName("A");
    
    for(i = 0; i < allAnchors.length; i++)
    {
        myA = allAnchors[i];
        if(myA.id.match(/edit_c_([0-9]+)/));
        {
            addEvent(myA, "click", editConstant, false);
        } 
    }
}

function editConstant(e)
{
    target = fetchTarget(e);
    matches = target.id.match(/edit_c_([0-9]+)/);
    if(!matches) return;
    idConst = matches[1];
    langs = new Array("en", "es", "fr");
    
    for(i = 0; i < langs.length; i++)
    {
        lang = langs[i];
        fieldId = "c_" + lang + "_" + idConst;
        fieldObj = document.getElementById(fieldId);
        fieldContent = fieldObj.innerHTML;
        
        taObj = document.createElement("TEXTAREA");
        taObj.setAttribute("id", "val_" + lang + "_" + idConst);
        taObj.setAttribute("class", "constantArea");
        taObj.innerHTML = fieldContent;
        
        fieldObj.parentNode.replaceChild(taObj, fieldObj);
    }
    
    target.innerHTML = "Save";
    dropEvent(target, "click", editConstant, false);
    addEvent(target, "click", saveConstant, false);
}

function saveConstant(e)
{
    target = fetchTarget(e);
    matches = target.id.match(/edit_c_([0-9]+)/);
    idConst = matches[1];
    langs = new Array("en", "es", "fr");
    
    for(i = 0; i < langs.length; i++)
    {
        lang = langs[i];
        taId = "val_" + lang + "_" + idConst;
        taObj = document.getElementById(taId);
        taContent = taObj.value;
        
        eval("text_" + lang + " = escape(taContent)");
    }
    ajaxObj.call("action=ajax_saveConstant&text_en=" + text_en + "&text_es=" + text_es + "&text_fr=" + text_fr + "&idConstant=" + idConst, saveConstantResp);
}


function saveConstantResp(resp)
{
    idConst = resp;
    langs = new Array("en", "es", "fr");
    
    for(i = 0; i < langs.length; i++)
    {
        lang = langs[i];
        taId = "val_" + lang + "_" + idConst;
        taObj = document.getElementById(taId);
        taContent = taObj.value;
        
        spanObj = document.createElement("SPAN");
        spanObj.setAttribute("id", "c_" + lang + "_" + idConst);
        spanObj.innerHTML = taContent;
        
        taObj.parentNode.replaceChild(spanObj, taObj);
    }
    
    editButtonId = "edit_c_" + idConst;
    editButtonObj = document.getElementById(editButtonId);
    editButtonObj.innerHTML = "Edit";
    dropEvent(editButtonObj, "click", saveConstant, false);
    addEvent(editButtonObj, "click", editConstant, false);
}

function addConstant()
{
    //get objects
    constantNameObj = document.getElementById("constantName");
    textEnObj = document.getElementById("text_en");
    textEsObj = document.getElementById("text_es");
    textFrObj = document.getElementById("text_fr");
    
    //get values
    constantNameVal = constantNameObj.value;
    textEn = escape(textEnObj.value);
    textEs = escape(textEsObj.value);
    textFr = escape(textFrObj.value);
    
    //alert(constantNameVal + ", " + textEn + ", " + textEs + ", " + textFr);
    
    ajaxObj.call("action=ajax_addConstant&text_en=" + textEn + "&text_es=" + textEs + "&text_fr=" + textFr + "&constantName=" + constantNameVal, addConstantResp);
}



function addConstantResp(resp)
{
    //get values
    idConstant = resp["idConstant"];
    text_en = resp["text_en"];
    text_es = resp["text_es"];
    text_fr = resp["text_fr"];
    constantName = resp["constantName"];
    constantError = resp["error"];
    
    addConstantErrorObj = document.getElementById("addConstantError");
    
    //if successful...
    if(idConstant)
    {
        //create a new row for table
        //create cells
        labelCellObj = document.createElement("TD");
        labelCellObj.setAttribute("class", "constantLabel");
        labelCellObj.innerHTML = constantName;
        
        enCellObj = document.createElement("TD");
        enSpanObj = document.createElement("SPAN");
        enSpanObj.setAttribute("id", "c_en_" + idConstant);
        enSpanObj.innerHTML = text_en;
        enCellObj.appendChild(enSpanObj);
        
        esCellObj = document.createElement("TD");
        esSpanObj = document.createElement("SPAN");
        esSpanObj.setAttribute("id", "c_es_" + idConstant);
        esSpanObj.innerHTML = text_es;
        esCellObj.appendChild(esSpanObj);
        
        frCellObj = document.createElement("TD");
        frSpanObj = document.createElement("SPAN");
        frSpanObj.setAttribute("id", "c_fr_" + idConstant);
        frSpanObj.innerHTML = text_fr;
        frCellObj.appendChild(frSpanObj);
        
        buttonCellObj = document.createElement("TD");
        buttonObj = document.createElement("A");
        buttonObj.setAttribute("id", "edit_c_" + idConstant);
        buttonObj.setAttribute("class", "button");
        buttonObj.innerHTML = "Edit";
        addEvent(buttonObj, "click", editConstant, false);
        buttonCellObj.appendChild(buttonObj);
        
        //create row
        newRowObj = document.createElement("TR");
        newRowObj.setAttribute("class", "newConstant");
        newRowObj.appendChild(labelCellObj);
        newRowObj.appendChild(enCellObj);
        newRowObj.appendChild(esCellObj);
        newRowObj.appendChild(frCellObj);
        newRowObj.appendChild(buttonCellObj);
        
        //get table & insert row
        constTableObj = document.getElementById("constTable");
        insertBeforeRow = constTableObj.rows[1];
        constTableObj.insertBefore(newRowObj, insertBeforeRow);
        
        //clear new constant form
        //get objects
        constantNameObj = document.getElementById("constantName");
        textEnObj = document.getElementById("text_en");
        textEsObj = document.getElementById("text_es");
        textFrObj = document.getElementById("text_fr");
        
        //clear data
        constantNameObj.value = "";
        textEnObj.value = "";
        textEnObj.innerHTML = "";
        textEsObj.value = "";
        textEsObj.innerHTML = "";
        textFrObj.value = "";
        textFrObj.innerHTML = "";
        
        addConstantErrorObj.innerHTML = "";
    }
    else
    {
        addConstantErrorObj.innerHTML = constantError;
    }
}

function submitUpload()
{
    //Get objects
    buttonObj = document.getElementById("uploadButton");
    inProcessObj = document.getElementById("uploading");
    uploadErrObj = document.getElementById("uploadErr");
    postPicInputObj = document.getElementById("postPicInput");
    //if nothing selected...
    if(postPicInputObj.value == "")
    {
        uploadErrObj.innerHTML = "No image selected";
        return;
    }
    
    //Clear any previous error
    uploadErrObj.innerHTML = "";
    //Hide upload button
    buttonObj.style.display = "none";
    //show in Process div
    inProcessObj.style.display = "inline";
    //submit form
    document.forms.uploadForm.submit();
}


function uploadImageResp(origW,origH,origFilesize,outputFilename,origFilename, submitId)
{   
    buttonObj = document.getElementById("uploadButton");
    inProcessObj = document.getElementById("uploading");
    postPicInputObj = document.getElementById("postPicInput");
    //Hide in process thing
    buttonObj.style.display = "inline";
    //show in upload button
    inProcessObj.style.display = "none";
    //empty input field
    postPicInputObj.value = "";
    
    photoContainerObj = document.getElementById("photoContainer");
    //Make container
    containerDivObj = document.createElement("DIV");
    containerDivObj.id = "imgContainer_" + submitId;
    
    //Make image
    imgObj = document.createElement("IMG");
    imgObj.src = "img_trophy_tmp/" + outputFilename;
    containerDivObj.appendChild(imgObj);
    
    //Make filename
    filenameObj = document.createElement("P");
    filenameObj.innerHTML = "<strong>" + origFilename + "</strong>";
    containerDivObj.appendChild(filenameObj);
    
    //Make tech. descrip.
    techDescObj = document.createElement("P");
    techDescObj.innerHTML = "<strong>" + aippTrophyConst_Dimensions + ":</strong> " + origW + " x " + origH + "<br><strong>" + aippTrophyConst_Filesize + ":</strong> " + origFilesize + " Kb";
    containerDivObj.appendChild(techDescObj);
    
    //Make title
    titleObj = document.createElement("P");
    titleObj.id = "title_" + submitId;
    titleObj.innerHTML = "<strong>" + aippTrophyConst_Title + ":</strong> <input id='title_" + submitId + "'><br><strong>" + aippTrophyConst_Credits + ":</strong><br><textarea id='credits_" + submitId + "'></textarea><br><strong>" + aippTrophyConst_Visible + ":<strong> <input type='checkbox' id='vis_" + submitId + "' value='1' checked>";
    containerDivObj.appendChild(titleObj);
    
    //Make delete button
    deleteButtonObj = document.createElement("A");
    deleteButtonObj.className = "button warn";
    deleteButtonObj.id = "deleteButton_" + submitId;
    deleteButtonObj.innerHTML = aippTrophyConst_Delete;
    addEvent(deleteButtonObj, "click", execDeleteButton, false);
    containerDivObj.appendChild(deleteButtonObj);
    
    //Add clear div
    clearDivObj = document.createElement("DIV");
    clearDivObj.className = "clear";
    containerDivObj.appendChild(clearDivObj);
    
    photoContainerObj.insertBefore(containerDivObj, photoContainerObj.firstChild);
    
    return;
}


function uploadImageError(errMsg)
{
    uploadErrObj = document.getElementById("uploadErr");
    uploadErrObj.innerHTML = errMsg;
}


function submitVidUpload()
{
    //Get objects
    buttonObj = document.getElementById("vidUploadButton");
    inProcessObj = document.getElementById("vidUploading");
    uploadErrObj = document.getElementById("vidUploadErr");
    postVidInputObj = document.getElementById("postVidInput");
    //if nothing selected...
    if(postVidInputObj.value == "")
    {
        uploadErrObj.innerHTML = "No file selected";
        return;
    }
    
    //Clear any previous error
    uploadErrObj.innerHTML = "";
    //Hide upload button
    buttonObj.style.display = "none";
    //show in Process div
    inProcessObj.style.display = "inline";
    //submit form
    document.forms.videoUploadForm.submit();
}

function uploadVideoResp(origFilesize,outputFilename,origFilename, submitId)
{   
    /*alert("Caught uploadVideoResp!");
    alert("origFilesize " + origFilesize + "\noutputFilename " + outputFilename + "\norigFilename " + origFilename + "\nsubmitId " + submitId)
    return;*/
    
    vidButtonObj = document.getElementById("vidUploadButton");
    vidInProcessObj = document.getElementById("vidUploading");
    postVidInputObj = document.getElementById("postVidInput");
    //Hide in process thing
    buttonObj.style.display = "inline";
    //show in upload button
    vidInProcessObj.style.display = "none";
    //empty input field
    postVidInputObj.value = "";
    
    
    //return;
    
    
    vidContainerObj = document.getElementById("vidContainer");
    //Make container
    containerDivObj = document.createElement("DIV");
    containerDivObj.id = "vidContainer_" + submitId;
    
    //Make image
    /*
    imgObj = document.createElement("IMG");
    imgObj.src = "img_trophy_tmp/" + outputFilename;
    containerDivObj.appendChild(imgObj);
    */
    
    //Make filename
    filenameObj = document.createElement("P");
    filenameObj.innerHTML = "<strong>" + origFilename + "</strong>";
    containerDivObj.appendChild(filenameObj);
    
    //Make tech. descrip.
    /*
    techDescObj = document.createElement("P");
    techDescObj.innerHTML = "<strong>" + aippTrophyConst_Filesize + ":</strong> " + origFilesize + " Kb";
    containerDivObj.appendChild(techDescObj);
    */
    
    //Make title
    titleObj = document.createElement("P");
    titleObj.id = "title_" + submitId;
    titleObj.innerHTML = "<strong>" + aippTrophyConst_Title + ":</strong> <input id='vTitle_" + submitId + "'><br><strong>" + aippTrophyConst_Credits + ":</strong><br><textarea id='vCredits_" + submitId + "'></textarea><br><strong>" + aippTrophyConst_Visible + ":<strong> <input type='checkbox' id='vVis_" + submitId + "' value='1' checked>";
    containerDivObj.appendChild(titleObj);
    
    //Make delete button
    deleteButtonObj = document.createElement("A");
    deleteButtonObj.className = "button warn";
    deleteButtonObj.id = "vDeleteButton_" + submitId;
    deleteButtonObj.innerHTML = aippTrophyConst_Delete;
    addEvent(deleteButtonObj, "click", execVDeleteButton, false);
    containerDivObj.appendChild(deleteButtonObj);
    
    //Add clear div
    clearDivObj = document.createElement("DIV");
    clearDivObj.className = "clear";
    containerDivObj.appendChild(clearDivObj);
    
    vidContainerObj.insertBefore(containerDivObj, vidContainerObj.firstChild);
    
    return;
}


function uploadVideoError(errMsg)
{
    uploadErrObj = document.getElementById("uploadErr");
    uploadErrObj.innerHTML = errMsg;
}



function execDeleteButton(e)
{
    target = fetchTarget(e);
    matches = target.id.match(/deleteButton_([0-9a-f]+)/);
    submitId = matches[1];
    ajaxObj.call("action=ajax_removeTmpPhoto&tmpRef=" + submitId, execDeleteButtonResp);
}

function execDeleteButtonResp(resp)
{
    submitId = resp;
    imgContainerId = "imgContainer_" + submitId;
    imgContainerObj = document.getElementById(imgContainerId);
    if(!imgContainerObj) alert("can't find " + imgContainerId);
    imgContainerObj.parentNode.removeChild(imgContainerObj);
    return true;
}



function execVDeleteButton(e)
{
    target = fetchTarget(e);
    matches = target.id.match(/vDeleteButton_([0-9a-f]+)/);
    submitId = matches[1];
    ajaxObj.call("action=ajax_removeTmpVideo&tmpRef=" + submitId, execVDeleteButtonResp);
}

function execVDeleteButtonResp(resp)
{
    submitId = resp;
    imgContainerId = "vidContainer_" + submitId;
    imgContainerObj = document.getElementById(imgContainerId);
    if(!imgContainerObj) alert("can't find " + imgContainerId);
    imgContainerObj.parentNode.removeChild(imgContainerObj);
    return true;
}


function editDeletePhoto(idPhoto)
{
    ajaxObj.call("action=ajax_removePhoto&idPhoto=" + idPhoto, editDeletePhotoResp);
}

function editDeletePhotoResp(resp)
{
    imgContainerId = "editImgContainer_" + resp;
    imgContainerObj = document.getElementById(imgContainerId);
    if(!imgContainerObj) alert("can't find " + imgContainerId);
    imgContainerObj.parentNode.removeChild(imgContainerObj);
    return true;
}


function editDeleteVideo(idVideo)
{
    ajaxObj.call("action=ajax_removeVideo&idVideo=" + idVideo, editDeleteVideoResp);
}

function editDeleteVideoResp(resp)
{
    imgContainerId = "editVideoContainer_" + resp;
    imgContainerObj = document.getElementById(imgContainerId);
    if(!imgContainerObj) alert("can't find " + imgContainerId);
    imgContainerObj.parentNode.removeChild(imgContainerObj);
    return true;
}


function submitTrophyCollection()
{
    trophyFormObj = document.getElementById("collectionForm");
    newElements = new Array();
    /*
     * Get all uploaded photos
     */
    //get all INPUT elements
    allInputs = document.getElementsByTagName("INPUT");
    for(i = 0; i < allInputs.length; i++)
    {
        matches = allInputs[i].id.match(/title_([a-zA-Z0-9]+)/);
        if(matches)
        {
            submitId = matches[1];
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "photos[]";
            newFormElm.value = submitId;
            newElements.push(newFormElm);
            
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "titles[" + submitId + "]";
            newFormElm.value = allInputs[i].value;
            newElements.push(newFormElm);
        }
        
        matchesVis = allInputs[i].id.match(/vis_([a-zA-Z0-9]+)/);
        if(matchesVis)
        {
            submitId = matchesVis[1];
            
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "vis[" + submitId + "]";
            newFormElm.value = allInputs[i].checked == true ? 1 : 0;
            newElements.push(newFormElm);
        }
    }
    allInputs = null;
    //get all credits - TEXTAREA
    allInputs = document.getElementsByTagName("TEXTAREA");
    for(i = 0; i < allInputs.length; i++)
    {
        matches = allInputs[i].id.match(/credits_([a-zA-Z0-9]+)/);
        if(matches)
        {
            submitId = matches[1];
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "credits[" + submitId + "]";
            newFormElm.value = allInputs[i].value;
            newElements.push(newFormElm);
        }
    }
    
    /*
     * Get all existing photos
     */
    //get all INPUT elements
    allInputs = document.getElementsByTagName("INPUT");
    for(i = 0; i < allInputs.length; i++)
    {
        matches = allInputs[i].id.match(/editTitle_([a-zA-Z0-9]+)/);
        if(matches)
        {
            submitId = matches[1];
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "editPhotos[]";
            newFormElm.value = submitId;
            newElements.push(newFormElm);
            
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "editTitles[" + submitId + "]";
            newFormElm.value = allInputs[i].value;
            newElements.push(newFormElm);
        }
        
        matchesVis = allInputs[i].id.match(/editVis_([a-zA-Z0-9]+)/);
        if(matchesVis)
        {
            submitId = matchesVis[1];
            
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "editVis[" + submitId + "]";
            newFormElm.value = allInputs[i].checked == true ? 1 : 0;
            newElements.push(newFormElm);
        }
    }
    allInputs = null;
    //get all credits - TEXTAREA
    allInputs = document.getElementsByTagName("TEXTAREA");
    for(i = 0; i < allInputs.length; i++)
    {
        matches = allInputs[i].id.match(/editCredits_([a-zA-Z0-9]+)/);
        if(matches)
        {
            submitId = matches[1];
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "editCredits[" + submitId + "]";
            newFormElm.value = allInputs[i].value;
            newElements.push(newFormElm);
        }
    }
    
    //alert("finished");
    //trophyFormObj.submit();
    //return;
    
    
    /********
     * VIDEOS
     *********/
    /*
     * Get all uploaded videos
     */
    //get all INPUT elements
    allInputs = document.getElementsByTagName("INPUT");
    for(i = 0; i < allInputs.length; i++)
    {
        matches = allInputs[i].id.match(/vTitle_([a-zA-Z0-9_]+)/);
        if(matches)
        {
            submitId = matches[1];
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "videos[]";
            newFormElm.value = submitId;
            newElements.push(newFormElm);
            
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "vTitles[" + submitId + "]";
            newFormElm.value = allInputs[i].value;
            newElements.push(newFormElm);
        }
        
        matchesVis = allInputs[i].id.match(/vVis_([a-zA-Z0-9_]+)/);
        if(matchesVis)
        {
            submitId = matchesVis[1];
            
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "vVis[" + submitId + "]";
            newFormElm.value = allInputs[i].checked == true ? 1 : 0;
            newElements.push(newFormElm);
        }
    }
    allInputs = null;
    //get all credits - TEXTAREA
    allInputs = document.getElementsByTagName("TEXTAREA");
    for(i = 0; i < allInputs.length; i++)
    {
        matches = allInputs[i].id.match(/vCredits_([a-zA-Z0-9_]+)/);
        if(matches)
        {
            submitId = matches[1];
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "vCredits[" + submitId + "]";
            newFormElm.value = allInputs[i].value;
            newElements.push(newFormElm);
        }
    }
    
    /*
     * Get all existing photos
     */
    //get all INPUT elements
    allInputs = document.getElementsByTagName("INPUT");
    for(i = 0; i < allInputs.length; i++)
    {
        matches = allInputs[i].id.match(/editVTitle_([a-zA-Z0-9_]+)/);
        if(matches)
        {
            submitId = matches[1];
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "editVideos[]";
            newFormElm.value = submitId;
            newElements.push(newFormElm);
            
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "editVTitles[" + submitId + "]";
            newFormElm.value = allInputs[i].value;
            newElements.push(newFormElm);
        }
        
        matchesVis = allInputs[i].id.match(/editVVis_([a-zA-Z0-9_]+)/);
        if(matchesVis)
        {
            submitId = matchesVis[1];
            
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "editVVis[" + submitId + "]";
            newFormElm.value = allInputs[i].checked == true ? 1 : 0;
            newElements.push(newFormElm);
        }
    }
    allInputs = null;
    //get all credits - TEXTAREA
    allInputs = document.getElementsByTagName("TEXTAREA");
    for(i = 0; i < allInputs.length; i++)
    {
        matches = allInputs[i].id.match(/editVCredits_([a-zA-Z0-9_]+)/);
        if(matches)
        {
            submitId = matches[1];
            newFormElm = document.createElement("INPUT");
            newFormElm.setAttribute("type", "hidden");
            newFormElm.name = "editVCredits[" + submitId + "]";
            newFormElm.value = allInputs[i].value;
            newElements.push(newFormElm);
        }
    }
    
    for(a = 0; a < newElements.length; a++)
    {
        trophyFormObj.appendChild(newElements[a]);
    }
    
    //alert("finished");
    trophyFormObj.submit();
}


function setPublishButtons()
{
    allAnchors = document.getElementsByTagName("A");
    for(i = 0; i < allAnchors.length; i++)
    {
        if(allAnchors[i].id.match(/^publishCollection_([0-9]+)$/))
        {
            addEvent(allAnchors[i], "click", publishCollection, false);
        }
        if(allAnchors[i].id.match(/^unpublishCollection_([0-9]+)$/))
        {
            addEvent(allAnchors[i], "click", unpublishCollection, false);
        }
    }
}


function publishCollection(e)
{
    //alert("publish!");
    target = fetchTarget(e);
    matches = target.id.match(/^publishCollection_([0-9]+)$/);
    idCollection = matches[1];
    //alert(idCollection);
    ajaxObj.call("action=ajax_publishCollection&myAction=publish&idCollection=" + idCollection, publishCollectionResp);
}

function unpublishCollection(e)
{
    //alert("unpublish!");
    target = fetchTarget(e);
    matches = target.id.match(/^unpublishCollection_([0-9]+)$/);
    idCollection = matches[1];
    //alert(idCollection);
    ajaxObj.call("action=ajax_publishCollection&myAction=unpublish&idCollection=" + idCollection, publishCollectionResp);
}

function publishCollectionResp(resp)
{
    idCollection = resp.idCollection;
    myAction = resp.myAction;
    containerCellObj = document.getElementById("publishCell_" + idCollection);
    //alert(containerCellObj.id);
    //get old button and delete it to avoid conflict
    oldButton = containerCellObj.getElementsByTagName("A")[0];
    //alert(oldButton.id);
    oldButton.parentNode.removeChild(oldButton);
    
    publishButtonObj = document.createElement("A");
    publishButtonObj.className = "button";
    
    //switch to unpublish button
    if(myAction == "publish")
    {
        containerCellObj.innerHTML = aippTrophyConst_Yes + " ";
        addEvent(publishButtonObj, "click", unpublishCollection, false);
        publishButtonObj.setAttribute("id", "unpublishCollection_" + idCollection);
        publishButtonObj.innerHTML = aippTrophyConst_Unpublish;
    }
    
    //switch to unpublish button
    if(myAction == "unpublish")
    {
        containerCellObj.innerHTML = aippTrophyConst_No + " ";
        addEvent(publishButtonObj, "click", publishCollection, false);
        publishButtonObj.setAttribute("id", "publishCollection_" + idCollection);
        publishButtonObj.innerHTML = aippTrophyConst_Publish;
    }
    
    containerCellObj.appendChild(publishButtonObj);
}


function fetchViewImagesButtons()
{
    allAnchors = document.getElementsByTagName("A");
    for(i = 0; i < allAnchors.length; i++)
    {
        if(allAnchors[i].id.match(/viewImages_([0-9]+)/))
        {
            allAnchors[i].openState = false;
            addEvent(allAnchors[i], "click", viewImages, false);
        }
    }
}
function viewImages(e)
{
    target = fetchTarget(e);
    idCollection = target.id.match(/viewImages_([0-9]+)/)[1];
    openState = target.openState;
    //alert(idCollection + ", " + openState);
    if(openState == false)
    {
        target.openState = true;
        ajaxObj.call("action=ajax_fetchDownloadImages&idCollection=" + idCollection + "&lang=" + pageLang, fetchDownloadImagesResp);
    }
    else
    {
        imgSeriesContainerObj = document.getElementById("imSerCon_" + idCollection);
        imgSeriesContainerObj.innerHTML = "";
        target.innerHTML = aippTrophyConst_Show;
        target.openState = false;
    }
}

function fetchDownloadImagesResp(resp)
{
    idCollection = resp.idCollection;
    imgSeriesContainerObj = document.getElementById("imSerCon_" + idCollection);
    //loop thru photos
    for(i = 0; i < resp.photos.length; i++)
    {
        pic = resp.photos[i];
        
        containerDivObj = document.createElement("DIV");
        
        myImage = document.createElement("IMG");
        myImage.setAttribute("src", "img_tro/" + pic.filenameLoRes);
        containerDivObj.appendChild(myImage);
        
        filenameObj = document.createElement("P");
        filenameObj.innerHTML = pic.filenameHiRes;
        containerDivObj.appendChild(filenameObj);
        
        fileDataObj = document.createElement("P");
        fileDataObj.innerHTML = "<strong>" + aippTrophyConst_Dimensions + ":</strong> " + pic.origW + " x " + pic.origH + "<br><strong>" + aippTrophyConst_Filesize + ":</strong> " + pic.filesize + " Kb";
        containerDivObj.appendChild(fileDataObj);
        
        titleObj = document.createElement("P");
        titleObj.innerHTML = "<strong>" + aippTrophyConst_Title + ":</strong> " + pic.title;
        containerDivObj.appendChild(titleObj);
        
        creditObj = document.createElement("P");
        creditObj.innerHTML = "<strong>" + aippTrophyConst_Credits + ":</strong> " + pic.credit;
        containerDivObj.appendChild(creditObj);
        
        downloadButtonObj = document.createElement("A");
        downloadButtonObj.setAttribute("href", "/downloads/" + pic.filenameHiRes);
        downloadButtonObj.className = "button";
        downloadButtonObj.innerHTML = aippTrophyConst_Download + " - " + pic.filesize + " Kb";
        containerDivObj.appendChild(downloadButtonObj);
        
        clearObj = document.createElement("DIV");
        clearObj.className = "clear";
        containerDivObj.appendChild(clearObj);
        
        imgSeriesContainerObj.appendChild(containerDivObj);
    }
    //change button
    viewImageButtonObj = document.getElementById("viewImages_" + resp.idCollection);
    viewImageButtonObj.innerHTML = aippTrophyConst_Hide;
}


function setWinnerRadios()
{
    allRadios = document.getElementsByTagName("INPUT");
    for(i = 0; i < allRadios.length; i++)
    {
        //setWin_avantgarde_1_2005
        matches = allRadios[i].id.match(/setWin_([a-z]+)_([0-9]{1,2})_([0-9]{4})/);
        if(matches)
        {
            addEvent(allRadios[i], "click", setWinner, false);
            //alert(allRadios[i].id)
            //if this box is checked
            if(allRadios[i].checked == true)
            {
                category = matches[1];
                parentRowObj = document.getElementById("row_" + category);
                rowCheckBoxes = parentRowObj.getElementsByTagName("INPUT");
                //disable other boxes
                for(j = 0; j < rowCheckBoxes.length; j++)
                {
                    if(allRadios[i] != rowCheckBoxes[j])
                    {
                        rowCheckBoxes[j].disabled = true;
                    }
                }
            }
        }
    }
}


function setWinner(e)
{
    target = fetchTarget(e);
    //setWin_avantgarde_1_2005
    matches = target.id.match(/setWin_([a-z]+)_([0-9]{1,2})_([0-9]{4})/);
    category = matches[1];
    prize = matches[2];
    year = matches[3];
    //alert(target.category + ", " + target.prize + ", " + target.year + ", idCollection: " + currentIdCollection);
    
    //disable other boxes in this row
    parentRowObj = document.getElementById("row_" + category);
    rowCheckBoxes = parentRowObj.getElementsByTagName("INPUT");
    //alert(parentRowObj.id);
    for(i = 0; i < rowCheckBoxes.length; i++)
    {
        if(target != rowCheckBoxes[i])
        {
            rowCheckBoxes[i].disabled = target.checked == true ? true : false;
        }
    }
    
    //determine action
    myAction = target.checked ? "win" : "unwin";
    //send call
    ajaxObj.call("action=ajax_setWinner&myAction=" + myAction + "&idCollection=" + currentIdCollection + "&category=" + category + "&prize=" + prize + "&year=" + year, setWinnerResp);

}

function setWinnerResp(resp)
{
    //alert(resp);
}

function setupYearChange()
{
    yearObj = document.getElementById("yearCompeting");
    addEvent(yearObj, "change", yearChange, false);
}

function yearChange(e)
{
    target = fetchTarget(e);
    //alert(target.value);
    year = target.value;
    
    ajaxObj.call("action=ajax_yearChange&year=" + year + "&idCollection=" + currentIdCollection + "&lang=" + pageLang, yearChangeResp);
}

function yearChangeResp(resp)
{
    winnersTableObj = document.getElementById("winnersTableContainer");
    winnersTableObj.innerHTML = resp;
    setWinnerRadios();
}


function adminVotingSetup()
{
    anchors = document.getElementsByTagName("A");
    
    for(i = 0; i < anchors.length; i++)
    {
        deleteMatch = anchors[i].id.match(/deleteFromVoting_([0-9]+)/);
        if(deleteMatch)
        {
            addEvent(anchors[i], "click", deleteFromVoting, false);
        }
        
        addMatch = anchors[i].id.match(/addToVoting_([_A-Z]+)/);
        if(addMatch)
        {
            addEvent(anchors[i], "click", addToVoting, false);
        }
    }
}

function deleteFromVoting(e)
{
    target = fetchTarget(e);
    idTrophyVote = target.id.match(/deleteFromVoting_([0-9]+)/);
    ajaxObj.call("action=ajax_deleteTrophyFromVoting&idTrophyVote=" + idTrophyVote[1], deleteFromVotingResp);
}

function deleteFromVotingResp(resp)
{
    if(resp != false)
    {
        //assign vars
        // {"idTrophyVote":"53","title":"Test Collection #2","idTrophyCollection":"74","companyName":"Anne McGuigan","countPhotos":"3"}
        idTrophyVote = resp["idTrophyVote"];
        title = resp["title"];
        idTrophyCollection = resp["idTrophyCollection"];
        companyName = resp["companyName"];
        countPhotos = resp["countPhotos"];
        category = resp["category"];
        
        //Delete row from selections
        deleteRow = document.getElementById("votingTr_" + idTrophyVote);
        parentTbody = ascendDOM(deleteRow, "tbody");
        parentTbody.removeChild(deleteRow);
        
        //check for addVotingTable
        addTrophyTableObj = document.getElementById("addVotingTable_" + category);
        if(!addTrophyTableObj)
        {
            return;
        }
        
        //get tbody
        tbodyArr = addTrophyTableObj.getElementsByTagName("TBODY");
        addTrophyTBodyObj = tbodyArr[0];
        
        //create new row
        newRowObj = document.createElement("TR");
        newRowObj.id = "addVotingRow_" + category + "_" + idTrophyCollection;
        
        //create & append cells
        companyNameTd = document.createElement("TD");
        companyNameTd.innerHTML = companyName;
        newRowObj.appendChild(companyNameTd);
        
        titleTd = document.createElement("TD");
        titleTd.innerHTML = title;
        newRowObj.appendChild(titleTd);
        
        countPhotosTd = document.createElement("TD");
        countPhotosTd.innerHTML = countPhotos;
        newRowObj.appendChild(countPhotosTd);
        
        addButton = document.createElement("A");
        addButton.className = "button";
        addButton.id = "addTrophyToVotingCategory_" + category + "_" + idTrophyCollection; 
        addButton.innerHTML = "Add";
        addEvent(addButton, "click", addTrophyToVotingCategory, false);
        
        buttonTd = document.createElement("TD");
        buttonTd.appendChild(addButton);
        newRowObj.appendChild(buttonTd);
        /*
        idTrophySignup = rows[i]["idTrophySignup"];
        */

        addTrophyTBodyObj.appendChild(newRowObj);
    }
}

function addToVoting(e)
{
    target = fetchTarget(e);
    addMatch = target.id.match(/addToVoting_([_A-Z]+)/);
    //alert(addMatch[1]+ ", year: " + trophyYear);
    ajaxObj.call("action=ajax_addTrophyToVoting&category=" + addMatch[1] + "&year=" + trophyYear, addToVotingResp);
}

function addToVotingResp(resp)
{
    if(!resp) return;
    
    //alert(resp);
    //{"title":"Test Collection #2","companyName":"Anne McGuigan","idTrophyCollection":"74","idTrophySignup":"20","countPhotos":"3"}
    category = resp["category"];
    rows = resp["rows"];
    
    if(rows.length < 1) return;
    
    //fetch category display container
    addTrophySelectorObj = document.getElementById("addVotingSelector_" + category);
    
    //create table obj
    addTrophyTableObj = document.createElement("TABLE");
    addTrophyTableObj.cellSpacing = 0;
    addTrophyTableObj.cellPadding = 0;
    addTrophyTableObj.border = 0;
    addTrophyTableObj.id = "addVotingTable_" + category;
    addTrophyTBodyObj = document.createElement("TBODY");
    
    //header row
    newRowObj = document.createElement("TR");
    
    //th cells
    thObj = document.createElement("TH");
    thObj.innerHTML = "Participant";
    newRowObj.appendChild(thObj);
    
    thObj = document.createElement("TH");
    thObj.innerHTML = "Collection";
    newRowObj.appendChild(thObj);
    
    thObj = document.createElement("TH");
    thObj.innerHTML = "Photos";
    newRowObj.appendChild(thObj);
    
    thObj = document.createElement("TH");
    thObj.innerHTML = "Add to voting";
    newRowObj.appendChild(thObj);
    
    addTrophyTBodyObj.appendChild(newRowObj);
    
    //loop thru rows
    for(i = 0; i < rows.length; i++)
    {
        idTrophyCollection = rows[i]["idTrophyCollection"];
        
        //create row
        newRowObj = document.createElement("TR");
        newRowObj.id = "addVotingRow_" + category + "_" + idTrophyCollection;
        
        //create & append cells
        companyName = rows[i]["companyName"];
        companyNameTd = document.createElement("TD");
        companyNameTd.innerHTML = companyName;
        newRowObj.appendChild(companyNameTd);
        
        title = rows[i]["title"];
        titleTd = document.createElement("TD");
        titleTd.innerHTML = title;
        newRowObj.appendChild(titleTd);
        
        countPhotos = rows[i]["countPhotos"];
        countPhotosTd = document.createElement("TD");
        countPhotosTd.innerHTML = countPhotos;
        newRowObj.appendChild(countPhotosTd);
        
        addButton = document.createElement("A");
        addButton.className = "button";
        addButton.id = "addTrophyToVotingCategory_" + category + "_" + idTrophyCollection; 
        addButton.innerHTML = "Add";
        addEvent(addButton, "click", addTrophyToVotingCategory, false);
        
        buttonTd = document.createElement("TD");
        buttonTd.appendChild(addButton);
        newRowObj.appendChild(buttonTd);
        /*
        idTrophySignup = rows[i]["idTrophySignup"];
        */

        addTrophyTBodyObj.appendChild(newRowObj);
    }
    
    //complete table and append to container
    addTrophyTableObj.appendChild(addTrophyTBodyObj);
    addTrophySelectorObj.appendChild(addTrophyTableObj);
    
    addTrophySelectorObj.style.display = "block";
    
    //change button
    addTrophyControlButton = document.getElementById("addToVoting_" + category);
    dropEvent(addTrophyControlButton, "click", addToVoting, false);
    addEvent(addTrophyControlButton, "click", dropAddToVoting, false);
    addTrophyControlButton.innerHTML = "Finished with category";
}

function dropAddToVoting(e)
{
    target = fetchTarget(e);
    //alert(target.id);
    matches = target.id.match(/addToVoting_([A-Z]+)/);
    category = matches[1];
    //alert(category);
    //fetch category display container
    addTrophySelectorObj = document.getElementById("addVotingSelector_" + category);
    addTrophyTableObj = document.getElementById("addVotingTable_" + category);
    addTrophySelectorObj.removeChild(addTrophyTableObj);
    addTrophySelectorObj.style.display = "none";
    
    //change button
    dropEvent(target, "click", dropAddToVoting, false);
    addEvent(target, "click", addToVoting, false);
    target.innerHTML = "Add collections to this category";
}


function addTrophyToVotingCategory(e)
{
    target = fetchTarget(e);
    matches = target.id.match(/addTrophyToVotingCategory_([A-Z]+)_([0-9]+)/);
    if(!matches) return;
    category = matches[1];
    idTrophyCollection = matches[2];
    
    //alert("category: " + category + ", idCollection: " + idCollection + ", year: " + trophyYear);
    ajaxObj.call("action=ajax_addTrophyCollectionToVoting&category=" + category + "&idTrophyCollection=" + idTrophyCollection + "&year=" + trophyYear, addTrophyToVotingCategoryResp);
}

function addTrophyToVotingCategoryResp(resp)
{
    /**
     * Assign returned vars
     */
    idTrophyVote = resp["idTrophyVote"];
    title = resp["title"];
    idTrophyCollection = resp["idTrophyCollection"];
    companyName = resp["companyName"];
    idTrophySignup = resp["idTrophySignup"];
    countPhotos = resp["countPhotos"];
    category = resp["category"];
    
    /**
     * Add new row to category
     */
    //fetch containing table
    containingTableObj = document.getElementById("trophyVotingCollections_" + category);
    tbodyArr = containingTableObj.getElementsByTagName("TBODY");
    tbodyObj = tbodyArr[0];
    
    //create new row
    newRow = document.createElement("TR");
    newRow.id = "votingTr_" + idTrophyVote;
    
    //create cells
    tdObj = document.createElement("TD");
    linkObj = document.createElement("A");
    linkObj.href = "admin_trophy.html?action=viewParticipant&idParticipant=" + idTrophySignup;
    linkObj.title = companyName;
    linkObj.innerHTML = companyName;
    tdObj.appendChild(linkObj);
    newRow.appendChild(tdObj);
    
    tdObj = document.createElement("TD");
    linkObj = document.createElement("A");
    linkObj.href = "admin_trophy.html?action=viewCollection&idCollection=" + idTrophyCollection;
    linkObj.title = title;
    linkObj.innerHTML = title;
    tdObj.appendChild(linkObj);
    newRow.appendChild(tdObj);
    
    tdObj = document.createElement("TD");
    tdObj.innerHTML = countPhotos;
    newRow.appendChild(tdObj);
    
    tdObj = document.createElement("TD");
    linkObj = document.createElement("A");
    linkObj.id = "deleteFromVoting_" + idTrophyVote;
    addEvent(linkObj, "click", deleteFromVoting, false);
    linkObj.innerHTML = "Delete";
    linkObj.className = "button warn";
    tdObj.appendChild(linkObj);
    newRow.appendChild(tdObj);
    
    tbodyObj.appendChild(newRow);
    
    /**
     * Delete row from category list
     */
    //addTrophyTableObj.id = "addVotingTable_" + category;
    //newRowObj.id = "addVotingRow_" + category + "_" + idTrophyCollection;
    
    //get containing table
    addVotingTableObj = document.getElementById("addVotingTable_" + category);
    tbodyArr = addVotingTableObj.getElementsByTagName("TBODY");
    tbodyObj = tbodyArr[0];
    
    //get row to be removed
    removeRow = document.getElementById("addVotingRow_" + category + "_" + idTrophyCollection);
    tbodyObj.removeChild(removeRow);
}



function currentCandidateSetup()
{
    linkArr = document.getElementsByTagName("A");
    for(i = 0; i < linkArr.length; i++)
    {
        if(linkArr[i].id.match(/vote_([0-9]+)/))
        {
            addEvent(linkArr[i], "click", currentCandidateDisplay, false);
        }
    }
    
    idPublicationSelectorObj = document.getElementById("idPublicationSelector");
    if(idPublicationSelectorObj)
    {
        addEvent(idPublicationSelectorObj, "change", votePublicationSelectorChange, false);
        ajaxObj.call("action=ajax_checkPublicationVote&idPublication=" + idPublicationSelectorObj.value + "&year=" + 2012, votePublicationSelectorChangeResp);
    }
    
    idPublicationInputObj = document.getElementById("idPublicationInput");
    if(idPublicationInputObj && idPublicationInputObj.value != "")
    {
        ajaxObj.call("action=ajax_checkPublicationVote&idPublication=" + idPublicationInputObj.value + "&year=" + 2012, votePublicationSelectorChangeResp);
    }
}

function votePublicationSelectorChange(e)
{
    target = fetchTarget(e);
    //alert(target.value);
    ajaxObj.call("action=ajax_checkPublicationVote&idPublication=" + target.value + "&year=" + 2012, votePublicationSelectorChangeResp);
}

function votePublicationSelectorChangeResp(resp)
{
    // fetch needed objects
    radiosArr = document.getElementsByTagName("INPUT");
    voteButtonContainerObj = document.getElementById("voteButtonContainer");
    alreadyVotedAlertObj = document.getElementById("alreadyVotedAlert");
    
    if(resp.votedIdTrophyVoteArr.length < 1)
    {
        //activate & unset all radios
        for(i = 0; i < radiosArr.length; i++)
        {
            //skip all non-radios
            if(radiosArr[i].type != "radio") continue;
            //radiosArr[i].checked = false;
            radiosArr[i].disabled = false;
        }
        
        voteButtonContainerObj.style.display = "block";
        alreadyVotedAlertObj.style.display = "none";
        
        return;
    }
    
    voteButtonContainerObj.style.display = "none";
    alreadyVotedAlertObj.style.display = "block";
    
    for(i = 0; i < radiosArr.length; i++)
    {
        //skip all non-radios
        if(radiosArr[i].type != "radio") continue;
        //parse out ID
        match = radiosArr[i].id.match(/idTrophyVote_([0-9]+)/);
        idTrophyVote = match[1];
        
        //loop voted vales
        for(j = 0; j < resp.votedIdTrophyVoteArr.length; j++)
        {
            //alert(idTrophyVote + ", " + resp.votedIdTrophyVoteArr[j]);
            
            if(resp.votedIdTrophyVoteArr[j] != idTrophyVote)
            {
                radiosArr[i].checked = false;
            }
            else
            {
                radiosArr[i].checked = true;
                break;
            }
        }
        radiosArr[i].disabled = true;
    }
}

function currentCandidateDisplay(e)
{
    target = fetchTarget(e);
    matches = target.id.match(/vote_([0-9]+)/);
    idTrophyCollection = matches[1];
    
    //alert(idTrophyCollection);
    
    ajaxObj.call("action=ajax_fetchVotingImages&idTrophyCollection=" +  idTrophyCollection + "&lang=" + pageLang, currentCandidateDisplayResp);
}

function currentCandidateDisplayResp(resp)
{
    companyName = resp["companyName"];
    title = resp["title"];
    country = resp["country"];
    photos = resp["photos"];
    videos = resp["videos"];
    categoryLabel = resp["categoryLabel"];
    
    //get container obj
    imgContainerObj = document.getElementById("voteImgContainer");
    imgContainerObj.innerHTML = "";
    
    //create header
    headerObj = document.createElement("H2");
    headerObj.innerHTML = companyName + " (" + country + ")";
    imgContainerObj.appendChild(headerObj);
    
    pObj = document.createElement("P");
    pObj.innerHTML = "Category: " + categoryLabel;
    imgContainerObj.appendChild(pObj);
    
    
    //make list of photos
    photoListObj = document.createElement("UL");
    for(i = 0; i < photos.length; i++)
    {
        listItemObj = document.createElement("LI");
        imageObj = document.createElement("IMG");
        imageObj.src = "trophyVote/" + photos[i];
        
        listItemObj.appendChild(imageObj);
        photoListObj.appendChild(listItemObj);
    }
    
    imgContainerObj.appendChild(photoListObj);
    
    
    //make list of videos
    videoDivObj = document.createElement("DIV");
    videoDivObj.align = "center"
    imgContainerObj.appendChild(videoDivObj);
    for(i = 0; i < videos.length; i++)
    {
        videoDivObj.innerHTML = '<iframe width="425" height="349" src="http://www.youtube.com/embed/' + videos[i] + '?rel=0" frameborder="0" allowfullscreen></iframe>';
    }
    
    
}


function checkVoteSubmit()
{
    radioArr = document.getElementsByTagName("INPUT");
    counter = 0;
    for(i = 0; i < radioArr.length; i++)
    {
        if(radioArr[i].type != "radio") continue;
        if(radioArr[i].checked == true) counter++;
    }
    
    if (counter < 5)
    /*if (counter < 1)*/
    {
        alert("Please select one participant from each category.");
        return false
    }
    
    document.forms.trophyVoteForm.submit();
    
}





addEvent(window, "load", addAllEvents, false);

