/* global.js
 * Contains global javascript functions
 */

// global popup window variables so we can give them the focus at any time
var keyPopup;
var keySpeciesPopup;
var previewPopup;
var speciesDataPopup;
var pdfPopup;

function getSynchronousWsResponse(wsName, postData) {
    var xmlhttp = null;
    if (window.XMLHttpRequest)
        xmlhttp = new XMLHttpRequest();
    else if (window.ActiveXObject) {
        if (new ActiveXObject("Microsoft.XMLHTTP"))
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        else
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    var url = contextPath + "/ws/DFOws.asmx/" + wsName;
    
    xmlhttp.open("POST", url, false);//false means synchronous
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.send(postData);
    var responseText = xmlhttp.responseText;
    return responseText;
}

function showKey(keyName) {
    var url = contextPath + "/viewer/" + keyName + ".aspx";
    var windowName = "keyWindow";
    var options = "width=1000, height=700, toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes";
    keyPopup = window.open(url, windowName, options);
    keyPopup.focus();
}

function showDescription(keySpeciesName) {
    var url = contextPath + "/data/" + keySpeciesName + ".aspx";
    var windowName = "keySpeciesWindow";
    var options = "width=1000, height=700, toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes";
    keySpeciesPopup = window.open(url, windowName, options);
    keySpeciesPopup.focus();
}

function showTemplatePreview(keyId) {
    var url = contextPath + "/admin/preview_key_data_template.aspx?key_id=" + keyId;
    var windowName = "previewWindow";
    var options = "width=1000, height=700, toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes";
    previewPopup = window.open(url, windowName, options);
    previewPopup.focus();
}

function showSpeciesDataPage(speciesId) {
    var url = contextPath + "/admin/edit_species_data.aspx?species_id=" + speciesId;
    var windowName = "speciesWindow";
    var options = "width=1000, height=700, toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes";
    speciesDataPopup = window.open(url, windowName, options);
    speciesDataPopup.focus();
}

function showPdf(keyId) {
    var url = contextPath + "/get_pdf.ashx?keyId=" + keyId;
    var windowName = "pdfWindow";
    var options = "width=1000, height=700, toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes";
    pdfPopup = window.open(url, windowName, options);
    pdfPopup.focus();
}

function toggleUsageInfo(pageId) {
    var usageList = $('helpContentsDiv');
    var state;
    if (usageList.style.display === 'none') {
        usageList.style.display = 'block';
        state = 'open';
    } else {
        usageList.style.display = 'none';
        state = 'closed';
    }
    
    storeHelpState(pageId, state);
}

function storeHelpState(pageId, state) {
    createCookie(pageId, state)
    helpStates.set(pageId, state);
}

function setPageHelpState(pageId) {
    var helpState = helpStates.get(pageId);
    var helpDiv = $('helpContentsDiv');

    if (helpState === 'open') {
        helpDiv.style.display = 'block';
    } else if (helpState === 'closed') {
        helpDiv.style.display = 'none';
    }
}

function toggleAcknowledgements() {
    var acks = $('acknowledgements');
    if (acks.style.display === 'none') {
        acks.style.display = 'block';
    } else {
        acks.style.display = 'none';
    }
}

function toggleSearchHelp() {
    var help = $('searchHelpContents');
    if (help.style.display === 'none') {
        help.style.display = 'block';
    } else {
        help.style.display = 'none';
    }
}

function toggleFormattingTips() {
    var format = $('formattingList');
    if (format.style.display === 'none') {
        format.style.display = 'block';
    } else {
        format.style.display = 'none';
    }
}
