function openDialog(url, title, width, height, acb, modal, autoSize, doPost, postData) {
    if (modal != null && modal) {

    }
    var method = "GET";
    var data = null;
    if (doPost) {
        method = "POST";
        data = postData;
    }
    swidth = window.screen.availWidth;
    sheight = window.screen.availHeight;

    dleft = (swidth - ((width != "auto") ? width : (swidth / 2))) / 2;
    dtop = (sheight - ((height != "auto") ? height : (sheight / 2))) / 2;
    if (navigator.appName == "Microsoft Internet Explorer")
    {
        var position = document.documentElement.scrollTop;
    }
    else {
        var position = window.pageYOffset;
    }
    dtop += position;
    var dlg = byId('dlg');
    dlg.style.left = dleft + "px";
    dlg.style.top = dtop + "px";
    byId("dlgBody").style.width = "";
    dlg.style.height = "";
    if (autoSize) {
        byId("dlgBody").style.height = "auto";

        dlg.style.width = "auto";
    } else {
        if (width != "auto") {
            dlg.style.width = (width + 1) + "px";
        } else
        {
            dlg.style.width = width;
        }
        if (height != "auto") {
            byId("dlgBody").style.height = height + "px";
        } else
        {
            byId("dlgBody").style.height = height;
        }

    }
    byId("dlgTitle").innerHTML = "&nbsp;" + title;
    byId("dlgBody").innerHTML = "<br><br><img src='images/loading.gif'>&nbsp;loading...";
    dlg.style.display = "";
    tstr = new Date().getTime();
    if (url.indexOf("?") > 0) {
        tstr = "&t=" + tstr;
    }
    else {
        tstr = "?t=" + tstr;
    }
    initialize("mainFrameBody", "dlg");

    Request.send(url + tstr, method, data, openDialogCallback, acb);
}

function openDialogCallback(resp) {
    byId("dlgBody").innerHTML = resp.trim();
    if (afterCallBack != null) {
        var tmp = afterCallBack;
        afterCallBack = null;
        eval(tmp);
    }
}

function openDialogLocally(title, innerHTML, width, height, autoSize) {

    swidth = window.screen.availWidth;
    sheight = window.screen.availHeight;

    dleft = (swidth - ((width != "auto") ? width : (swidth / 2))) / 2;
    dtop = (sheight - ((height != "auto") ? height : (sheight / 2))) / 2;
    if (navigator.appName == "Microsoft Internet Explorer")
    {
        var position = document.documentElement.scrollTop;
    }
    else {
        var position = window.pageYOffset;
    }
    dtop += position;
    var dlg = byId('dlg');
    dlg.style.left = dleft + "px";
    dlg.style.top = dtop + "px";
    if (autoSize) {
        byId("dlgBody").style.height = "auto";

        dlg.style.width = "auto";
    } else {
        if (width != "auto") {
            dlg.style.width = (width + 1) + "px";
        } else
        {
            dlg.style.width = width;
        }
        if (height != "auto") {
            byId("dlgBody").style.height = height + "px";
        } else
        {
            byId("dlgBody").style.height = height;
        }

    }
    byId("dlgTitle").innerHTML = "&nbsp;" + title;
    byId("dlgBody").innerHTML = innerHTML;
    dlg.style.display = "";
}

function openPopupDialog(url, title, width, height, acb, modal, autoSize, doPost, postData, x, y) {
    var method = "GET";
    var data = null;
    if (doPost) {
        method = "POST";
        data = postData;
    }
    swidth = window.screen.availWidth;
    sheight = window.screen.availHeight;

    dleft = (swidth - ((width != "auto") ? width : (swidth / 2))) / 2;
    dtop = (sheight - ((height != "auto") ? height : (sheight / 2))) / 2;

    var position = window.pageYOffset;
    if (navigator.appName == "Microsoft Internet Explorer") {
        position = document.documentElement.scrollTop;
    }
    dtop += position;
    if(x) {
        dleft = x;
    }
    if(y) {
        dtop = y;
    }
    var newDialogId = (new Date()).getTime();
    // Main dialog table
    var dlg = document.createElement("table");
    dlg.setAttribute("border", 0);
    dlg.setAttribute("cellspacing", 0);
    dlg.setAttribute("cellpadding", 0);
    dlg.setAttribute("id", "dlg_" + newDialogId);
    dlg.className = "popupDialog";
    dlg.style.left = dleft + "px";
    dlg.style.top = dtop + "px";
    dlg.style.zIndex = ++dialogZindex;
    var dlgTr = document.createElement("tr");
    var dlgTd = document.createElement("td");
    dlgTd.setAttribute("valign", "top");
    dlgTr.appendChild(dlgTd);

    var dlgTd2 = document.createElement("td");
    dlgTd2.setAttribute("onmousedown", "makeResizableHorizontally('dlg_" + newDialogId + "', event)");
    dlgTd2.setAttribute("align", "left");
    dlgTd2.setAttribute("valign", "top");
    dlgTd2.style.width = "8px";
    dlgTd2.style.backgroundImage = "url('images/shad_left_top.png')";
    dlgTd2.style.backgroundRepeat = "no-repeat";
    dlgTd2.style.cursor = "e-resize";
    var sImg = document.createElement("img");
    sImg.src = "images/s.gif";
    sImg.style.width = "8px";

    dlgTd2.appendChild(sImg);

    // Dialog title table
    var dlgTitle = document.createElement("table");
    dlgTitle.setAttribute("border", 0);
    dlgTitle.setAttribute("cellspacing", 0);
    dlgTitle.setAttribute("cellpadding", 0);
    var dlgTitleTr = document.createElement("tr");
    dlgTitleTr.setAttribute("valign", "top");
    dlgTitleTr.className = "toolbarTitle";
    var dlgTitleTd = document.createElement("td");
    dlgTitleTd.setAttribute("id", "dlgTitle_" + newDialogId);
    dlgTitleTd.setAttribute("onmousedown", "makeDragable('dlg_" + newDialogId + "', event);");
    dlgTitleTd.style.cursor = "move";
    dlgTitleTd.style.width = "100%";
    dlgTitleTd.style.height = "20px";
    dlgTitleTd.style.textAligment = "left";
    dlgTitleTd.innerHTML = "&nbsp;Dialog";
    var dlgTitleCloseTd = document.createElement("td");
    var dlgTitleCloseImage = document.createElement("img");
    dlgTitleCloseImage.src = "images/ico/cancel.gif";
    dlgTitleCloseImage.setAttribute("onclick", "byId('dlg_" + newDialogId + "').parentNode.removeChild(byId('dlg_" + newDialogId + "'));");
    dlgTitleCloseImage.title = "Close";
    dlgTitleCloseImage.style.cursor = "pointer";
    dlgTitleCloseImage.style.margin = "0px 5px";

    var dlgBodyDiv = document.createElement("div");
    dlgBodyDiv.setAttribute("id", "dlgBody_" + newDialogId);
    dlgBodyDiv.style.overflow = "auto";
    dlgBodyDiv.className = "popupDialogBody";

    dlgTitleCloseTd.appendChild(dlgTitleCloseImage);
    dlgTitleTr.appendChild(dlgTitleTd);
    dlgTitleTr.appendChild(dlgTitleCloseTd);
    dlgTitle.appendChild(dlgTitleTr);

    var dlgTr2 = document.createElement("tr");
    dlgTr2.setAttribute("height", "8px");

    var dlgTr2Td1 = document.createElement("td");
    dlgTr2Td1.style.height = "8px";
    dlgTr2Td1.style.backgroundImage = "url('images/shad_bott_left.png')";
    dlgTr2Td1.style.backgroundRepeat = "no-repeat";
    dlgTr2Td1.style.cursor = "s-resize";
    dlgTr2Td1.setAttribute("onmousedown", "makeResizableVertically('dlg_" + newDialogId + "', event)");

    var sImg2 = document.createElement("img");
    sImg2.src = "images/s.gif";
    sImg2.style.height = "8px";

    var dlgTr2Td2 = document.createElement("td");
    dlgTr2Td2.style.width = "8px";
    dlgTr2Td2.style.height = "8px";
    dlgTr2Td2.style.backgroundRepeat = "no-repeat";
    dlgTr2Td2.style.cursor = "nw-resize";
    dlgTr2Td2.style.backgroundImage = "url('images/shad_corner.png')";
    dlgTr2Td2.setAttribute("onmousedown", "makeResizableXY('dlg_" + newDialogId + "', event)");

    dlgTr2Td1.appendChild(sImg2);
    dlgTr2.appendChild(dlgTr2Td1);
    dlgTr2.appendChild(dlgTr2Td2);

    if (autoSize) {
        if (height) {
            dlgBodyDiv.style.height = height + "px";
        } else {
            dlgBodyDiv.style.height = "auto";
        }
        if (width) {
            dlg.style.width = (width + 1) + "px";
        } else {
            dlg.style.width = "auto";
        }
    } else {
        if (width != "auto") {
            dlg.style.width = (width + 1) + "px";
        } else {
            dlg.style.width = width;
        }
        if (height != "auto") {
            dlgBodyDiv.style.height = height + "px";
        } else {
            dlgBodyDiv.style.height = height;
        }
    }
    dlgTd.appendChild(dlgTitle);
    dlgTd.appendChild(dlgBodyDiv);
    dlgTr.appendChild(dlgTd2);
    dlg.appendChild(dlgTr);
    dlg.appendChild(dlgTr2);

    dlgTitleTd.innerHTML = "&nbsp;" + title;

    var loadingImg = document.createElement("img");
    loadingImg.src = "images/loading.gif";

    dlgBodyDiv.appendChild(document.createElement("br"));
    dlgBodyDiv.appendChild(document.createElement("br"));
    dlgBodyDiv.appendChild(loadingImg);
    dlgBodyDiv.appendChild(document.createTextNode(" loading..."));

    if (isIE()) {
        document.getElementsByTagName("body")[0].innerHTML += dlg.outerHTML;
    } else {
        document.getElementsByTagName("body")[0].appendChild(dlg);
    }

    var tstr = new Date().getTime();
    if(doPost) {
        data += "&t=" + tstr + "&dlgId=" + newDialogId;
    } else {
        if (url.indexOf("?") > 0) {
            tstr = "&t=" + tstr;
        } else {
            tstr = "?t=" + tstr;
        }
        tstr += "&dlgId=" + newDialogId;
        url += tstr;
    }

    Request.send(url, method, data, function(response) {
        byId("dlgBody_" + newDialogId).innerHTML = response.trim();
    }, acb);
}

function hidePopupDialog(id) {
    byId('dlg_' + id).parentNode.removeChild(byId('dlg_' + id));
}

function loadRelationBrowser() {
    var dsfn = byId("dsfn").value;
    swfobject.embedSWF("cn_RelationBrowser.swf?dataSource=" + dsfn, "rbContainer", "800", "650", "9.0.0");
}

var conceptTabs;

function loadConceptEditorTabs() {
    conceptTabs = new Array();
    conceptTabs[0] = "tabMain";
    conceptTabs[1] = "tabConstraints";
    conceptTabs[2] = "tabImages";
    conceptTabs[3] = "tabSounds";
    conceptTabs[4] = "tabDictionaries";
    conceptTabs[5] = "tabReferences";
    conceptTabs[6] = "tabStatistics";
}
var currCid = "";
function saveChangedConcept(conceptId) {
    var HW = byId("hw_in_" + conceptId).value;
    var desc = byId("desc_in_" + conceptId).value;
    var exmpl = byId("exmpl_in_" + conceptId).value;
    var pos = byId("uw_part_of_speech").value;
    var comment = byId("comments_in_" + conceptId).value;
    
    HW = encodeURIComponent(HW.unicodeEncode());
    pos = encodeURIComponent(pos.unicodeEncode());

    exmpl = exmpl.replaceAll("\"", "'");
    desc = desc.replaceAll("\"", "'");
    comment = comment.replaceAll("\"", "'");

    desc = encodeURIComponent(desc.unicodeEncode());
    exmpl = encodeURIComponent(exmpl.unicodeEncode());
    comment = encodeURIComponent(comment.unicodeEncode());

    var data = "action=update_concept";
    data += "&uw_head_word="+HW;
    data += "&uw_example="+exmpl;
    data += "&uw_description="+desc;
    data += "&uw_part_of_speech="+pos;
    data += "&uw_comments="+comment;
    data += "&uw_id="+conceptId;

    Request.send("cn_concept_save.jsp", "POST", data, function(response) {
        window.location.href = "index.jsp?page=cn&action=view&cid=" + conceptId;
    });
}

function saveNewConcept() {
    var id = byId("uw_new_id").value;
    var uw_string = byId("uw_string").value.trim();
    var uw_head_word = byId("uw_head_word").value.trim();
    var uw_description = byId("uw_description").value.trim();
    var uw_example = byId("uw_example").value.trim();
    var uw_part_of_speech = byId("uw_part_of_speech").value;
    var uw_comments = byId("uw_comments").value.trim();

    if(uw_string == "") {
        alert("UW String is required.");
    }
    if(uw_head_word == "") {
        alert("Head word is required.");
    }
    var data = "action=save_new_concept";
    data += "&uw_string="+uw_string;
    data += "&uw_head_word="+uw_head_word;
    data += "&uw_example="+uw_example;
    data += "&uw_description="+uw_description;
    data += "&uw_part_of_speech="+uw_part_of_speech;
    data += "&uw_comments="+uw_comments;
    data += "&uw_id="+id;
    
    Request.send("cn_concept_save.jsp", "POST", data, function(response) {
        window.location.href = "index.jsp?page=cn&action=view&cid=" + id;
    });
}

var currentDARelId = null;
var currentDAtcId = null;


function saveDisambiguation(relationId, conceptId) {

    byId("saveDAIcon_" + relationId).innerHTML = "<img src='images/loading.gif'>";
    var daRadio = byName("da_" + relationId);
    var tcid = getSelectedRadioValue(daRadio);
    if (tcid == "") {
        alert("Please make a selection.");
        byId("saveDAIcon_" + relationId).innerHTML = "<img src='images/ico/disk.gif'>";
        return;
    }
    if (currentDARelId != null) {
        alert("Another save operation is now in process. Please wait.");
        return;
    }

    currentDARelId = relationId;
    currentDAtcId = tcid;
    tstr = new Date().getTime();
    var hw = byId('searched_hw_' + currentDARelId).value;
    var relationType = byId('reltypes_' + relationId).value;

    hw = encodeURIComponent(hw.unicodeEncode());
    relationType = encodeURIComponent(relationType.unicodeEncode());

    Request.send("cn_concept_disambiguate_save.jsp?t" + tstr + "&rid=" + relationId + "&rtype=" + relationType + "&tcid=" + tcid + "&tchw=" + hw + "&cid=" + conceptId, "GET", null, saveDisambiguationCallBack);
}

function saveDisambiguationCallBack(response) {
    response = response.trim();
    if (response.indexOf("CN Server:") == 0) {
        alert(response);
        return;
    }

    byId("saveDAIcon_" + currentDARelId).innerHTML = "<img src='images/ico/disk.gif'>";
    byId("tcid_" + currentDARelId).innerHTML = currentDAtcId;
    byId("uwString").innerHTML = response;
    var hw = byId('searched_hw_' + currentDARelId).value;
    byId("tchw_" + currentDARelId).innerHTML = hw;
    byId("tchw_hidden_" + currentDARelId).value = hw;
    currentDARelId = null;
    currentDAtcId = null;
}

var currentCid;
function createConstraint(conceptId) {

    byId("saveDAIcon_-1").innerHTML = "<img src='images/loading.gif'>";
    var daRadio = byName("da_-1");
    var tcid = getSelectedRadioValue(daRadio);
    if (tcid == "") {
        alert("Please make a selection.");
        byId("saveDAIcon_-1").innerHTML = "<img src='images/ico/disk.gif'>";
        return;
    }
    if (currentDARelId != null) {
        alert("Another save operation is now in process. Please wait.");
        return;
    }
    currentCid = conceptId;
    currentDARelId = -1;
    currentDAtcId = tcid;
    tstr = new Date().getTime();
    var hw = byId('searched_hw_-1').value;
    var relationType = byId('reltypes_-1').value;


    hw = encodeURIComponent(hw.unicodeEncode());
    relationType = encodeURIComponent(relationType.unicodeEncode());

    Request.send("cn_concept_disambiguate_save.jsp?t" + tstr + "&rid=-1" + "&rtype=" + relationType + "&tcid=" + tcid + "&tchw=" + hw + "&cid=" + conceptId, "GET", null, createConstraintCallBack);
}

function createConstraintCallBack(response) {
    response = response.trim();
    if (response.indexOf("CN Server:") == 0) {
        alert(response);
        return;
    }

    byId("saveDAIcon_-1").innerHTML = "<img src='images/ico/disk.gif'>";
    byId("uwString").innerHTML = response;
    hide('dlg');
    currentDARelId = null;
    currentDAtcId = null;
    loadContent("cn_conceptEdit_constraints.jsp?cid=" + currentCid, "constraintsBody", "constraintsBody");
}

// deleting constraint

var currentCidDelete;
function deleteteConstraint(conceptId, relationId) {
    if (!confirm("You are about to delete a constraint. Are you sure?")) {
        return;
    }
    currentCidDelete = conceptId;
    tstr = new Date().getTime();
    Request.send("cn_constraint_delete.jsp?t" + tstr + "&rid=" + relationId + "&cid=" + conceptId, "GET", null, deleteConstraintCallBack);
}

function deleteConstraintCallBack(response) {
    response = response.trim();
    if (response.indexOf("CN Server:") == 0) {
        alert(response);
        return;
    }
    byId("uwString").innerHTML = response;
    loadContent("cn_conceptEdit_constraints.jsp?cid=" + currentCidDelete, "constraintsBody", "constraintsBody")
}
// end of deleting constraint


/*function openDisambiguationPanel(relationId, conceptId) {
    var hw = byId('tchw_hidden_' + relationId).value;
    byId('search_' + relationId).value = hw;

    hw = encodeURIComponent(hw.unicodeEncode());
    loadContent('cn_concept_disambiguate.jsp?hw=' + hw + '&rid=' + relationId + "&cid=" + conceptId, 'constraint_' + relationId, 'constraint_body_' + relationId)
}*/

function targetConceptSearchByHW(relationId, conceptId) {
    var searchStr = byId("search_" + relationId).value;
    var buttonSet = "edit";
    if (relationId == -1) {
        buttonSet = "create";
    }

    searchStr = encodeURIComponent(searchStr.unicodeEncode());
    var urlStr = "cn_concept_disambiguate.jsp?hw=" + searchStr + "&rid=" + relationId + "&cid=" + conceptId + "&buttonset=" + buttonSet;
    loadContent(urlStr, 'constraint_body_' + relationId, 'constraint_body_' + relationId)
}

function newConstraint(cid) {
    openDialog('cn_constraint_create.jsp?cid=' + cid, 'new Constraint', 700, 200, '')
}

function conceptSearch(p) {
    var searchStr = byId("cnsearch").value;
    if (searchStr.length == 0) {
        return;
    }
    hideSearchResultTab();
    var searchBy = byName("cnsearchby");
    tstr = new Date().getTime();
    if (searchBy != null && searchBy.length > 0 && searchBy[getSelectedRadio(searchBy)] != null) {
        searchBy = searchBy[getSelectedRadio(searchBy)].value
    }
    else {
        searchBy = "hw";
    }
    searchStr = encodeURIComponent(searchStr.unicodeEncode());
    var urlStr = "cn_searchResults.jsp";
    if (p) {
        urlStr = "public_cn_searchResults.jsp";
        currCid = null;
        byId("searchresultscontainter_panel").style.display = "";
        //showSearchResultTab('unl');
    }
    urlStr += "?t=" + tstr + "&cnsearch=" + searchStr + "&cnsearchby=" + searchBy;
    loadContent(urlStr, 'searchresults', 'searchresultscontainter_unl');
    cn_startHeight = null;
}

function conceptSearchCN(p) {
    var searchStr = byId("cnsearch").value;
    if (searchStr.length == 0) {
        return;
    }
    hideSearchResultTab();
    saveAdvancedOptions();
    var searchBy = "string";

    if (Get_Cookie("cn_search_by_string") == "true") {
        searchBy = "string";
    }
    if (Get_Cookie("cn_search_by_word") == "true") {
        searchBy = "word";
    }
    if (Get_Cookie("cn_search_by_number") == "true") {
        searchBy = "number";
    }
    searchBy = encodeURIComponent(searchBy.unicodeEncode());
    var searchIn = "hw";
    if (Get_Cookie("cn_search_in_description") == "true") {
        searchIn = "desc";
    }

    searchStr = encodeURIComponent(searchStr.unicodeEncode());

    var urlStr = "cn_searchResults.jsp";

    urlStr += "?cnsearch=" + searchStr + "&cnsearchBy=" + searchBy + "&cnsearchIn=" + searchIn;
    urlStr += "&partsOfSpeech=" + Get_Cookie("cn_part_of_speech");

    if (p != null) {
        urlStr += "&p=" + p + "&epp=15";
    }
    //console.log(urlStr);
    loadContent(urlStr, 'searchresults', 'searchresultscontainter_unl');
    cn_startHeight = null;
}

////////////////////////google search/////////////////////////////////
function showWebSearchResult() {
    var sr_web = byId("sr_web");
    var sr_img = byId("sr_img");
    sr_img.className = "tabborderbottom tabborderleft tabbordertop cellbg2";
    sr_web.className = "tabbordertop cellbg1";
    byId("searchType").value = "web";
}

function showImgSearchResult() {
    var sr_web = byId("sr_web");
    var sr_img = byId("sr_img");
    sr_web.className = "tabborderbottom  tabbordertop cellbg2";
    sr_img.className = "tabborderleft tabbordertop cellbg1";
    ;
    byId("searchType").value = "img";
}

function showSearchResultTab(lng) {
    //var sr_unl = byId("sr_unl");
    //var sr_unl_tab = byId("searchresultscontainter_unl_tr");

    var sr_en = byId("sr_en");
    var sr_en_tab = byId("searchresultscontainter_en_tr");

    var sr_fr = byId("sr_fr");
    var sr_fr_tab = byId("searchresultscontainter_fr_tr");

    var sr_pt = byId("sr_pt");
    var sr_pt_tab = byId("searchresultscontainter_pt_tr");

    var sr_ab = byId("sr_ab");
    var sr_ab_tab = byId("searchresultscontainter_ab_tr");

    var sr_am = byId("sr_am");
    var sr_am_tab = byId("searchresultscontainter_am_tr");

    var sr_ru = byId("sr_ru");
    var sr_ru_tab = byId("searchresultscontainter_ru_tr");


    //sr_unl_tab.style.display="none";
    //sr_unl.className="tabborderbottom tabborderleft tabbordertop cellbg2";

    sr_en_tab.style.display = "none";
    sr_en.className = "tabborderbottom tabborderleft tabbordertop cellbg2";

    sr_fr_tab.style.display = "none";
    sr_fr.className = "tabborderbottom tabborderleft tabbordertop cellbg2";

    sr_pt_tab.style.display = "none";
    sr_pt.className = "tabborderbottom tabborderleft tabbordertop cellbg2";

    sr_ab_tab.style.display = "none";
    sr_ab.className = "tabborderbottom tabborderleft tabbordertop cellbg2";

    sr_am_tab.style.display = "none";
    sr_am.className = "tabborderbottom tabborderleft tabbordertop cellbg2";

    sr_ru_tab.style.display = "none";
    sr_ru.className = "tabborderbottom tabborderleft tabbordertop cellbg2";

    var sr_sel = byId("sr_" + lng);
    var sr_sel_tab = byId("searchresultscontainter_" + lng + "_tr");

    sr_sel_tab.style.display = "";
    sr_sel.className = "tabborderleft tabbordertop cellbg1";

}

function hideSearchResultTab() {

    if (byId("tabs_row")) {
        byId("tabs_row").style.display = "none";
    }
    var sr_sel_tab1 = byId("searchresultscontainter_en_tr");

    var sr_sel_tab2 = byId("searchresultscontainter_fr_tr");

    var sr_sel_tab3 = byId("searchresultscontainter_pt_tr");

    var sr_sel_tab4 = byId("searchresultscontainter_ab_tr");

    var sr_sel_tab5 = byId("searchresultscontainter_am_tr");

    var sr_sel_tab6 = byId("searchresultscontainter_ru_tr");

    if (sr_sel_tab1) {
        sr_sel_tab1.style.display = "none";
    }
    if (sr_sel_tab2) {
        sr_sel_tab2.style.display = "none";
    }
    if (sr_sel_tab3) {
        sr_sel_tab3.style.display = "none";
    }
    if (sr_sel_tab4) {
        sr_sel_tab4.style.display = "none";
    }
    if (sr_sel_tab5) {
        sr_sel_tab5.style.display = "none";
    }
    if (sr_sel_tab6) {
        sr_sel_tab6.style.display = "none";
    }
    if (byId("searchresultscontainter_panel")) {
        byId("searchresultscontainter_panel").width = "100%"
    }
}

var currCid = null;
var currLng = null;
var searchResultsLoaded = new Hashtable();


function publicRelationBrowser(cid)
{
    showSearchResultTab("en");
    byId("searchresultscontainter_td_en").innerHTML = '<img src = "images/loading.gif" />loading...';
    if (currCid != null) {
        byId("concept_td_" + currCid).className = "tabborderleft pub_search"
    }
    currCid = cid;
    lng = byId('searchLang' + cid).value;
    currLng = lng;
    byId("concept_td_" + cid).className = "tabborderleft pub_search_selected";
    byId("searchresultscontainter_panel").width = "420px";
    if (cn_startHeight == null) {
        cn_startHeight = parseInt(byId("searchresultscontainter_unl").offsetHeight) - 1;
    }
    byId("cn_sres_compenser").style.height = "0px";
    //cn_sres_compenser

    if (navigator.appVersion.indexOf("MSIE") != -1)
    {
        byId("searchresultscontainter_en").style.height = (cn_startHeight - 12) + "px";
    }
    else {
        byId("searchresultscontainter_en").style.height = (cn_startHeight + 1) + "px";
    }
    Request.send("cn_relationBrowser.jsp?cid=" + cid, "GET", null, publicRelationBrowserCallBack, 'loadRelationBrowser()');

}

function publicRelationBrowserCallBack(resp)
{
    byId("searchresultscontainter_td_en").innerHTML = resp.trim();

    if (afterCallBack != null) {
        var tmp = afterCallBack;
        afterCallBack = null;
        eval(tmp);
    }
}

function ggSearch(cid) {

    if (currCid != null) {
        byId("concept_td_" + cid).style.border.top = "";
        byId("concept_td_" + cid).style.border.left = "";
        byId("concept_td_" + cid).style.border.bottom = "";
        byId("concept_td_" + cid).style.border.right = "1px solid #96A8CC"
    }
    currCid = cid;
    byId("concept_td_" + cid).style.border.top = "1px solid #96A8CC";
    byId("concept_td_" + cid).style.border.left = "1px solid #96A8CC";
    byId("concept_td_" + cid).style.border.bottom = "1px solid #96A8CC";
    byId("concept_td_" + cid).style.border.right = "";
    getRelatedConcepts();
    showSearchResultTab('en');
    //getRelatedConceptsTranslated(cid,"fr");
    //getRelatedConceptsTranslated(cid,"pt");
}
function formSizes()
{
    var cn_resHeight = parseInt(byId("searchresultscontainter_unl").offsetHeight) - 1;
    var gg_resHeight = parseInt(byId("searchresultscontainter_" + currLng).offsetHeight);
    if (gg_resHeight > cn_resHeight)
    {
        if (navigator.appVersion.indexOf("MSIE") != -1)
        {
            byId("cn_sres_compenser").style.height = (gg_resHeight - cn_resHeight + 29) + "px";
        }
        else {
            byId("cn_sres_compenser").style.height = (gg_resHeight - cn_resHeight + 28) + "px";
        }
    }

}
var cn_startHeight = null;
function ggSearchLng(cid) {
    byId("tabs_row").style.display = "";
    if (currCid != null) {
        byId("concept_td_" + currCid).className = "tabborderleft pub_search"
    }
    if (cid != null) {
        currCid = cid;
    }
    else {
        cid = currCid;
    }


    lng = byId('searchLang' + cid).value;
    currLng = lng;
    byId("concept_td_" + cid).className = "tabborderleft pub_search_selected";
    if (lng == 'en') {
        getRelatedConcepts();
    }
    byId("searchresultscontainter_panel").width = "420px";
    if (cn_startHeight == null) {
        cn_startHeight = parseInt(byId("searchresultscontainter_unl").offsetHeight) - 1;
    }
    byId("cn_sres_compenser").style.height = "0px";
    //cn_sres_compenser

    if (navigator.appVersion.indexOf("MSIE") != -1)
    {
        byId("searchresultscontainter_" + lng).style.height = 0 + "px";// (cn_startHeight-3)+"px";
        byId("searchresultscontainter_" + lng).style.height = (cn_startHeight - 40) + "px";
    }
    else {
        byId("searchresultscontainter_" + lng).style.height = 0 + "px";// cn_startHeight+"px";
        byId("searchresultscontainter_" + lng).style.height = (cn_startHeight - 28) + "px"
    }

    if (lng == 'en') {
        showSearchResultTab('en')
    } else {
        getCurrConceptTranslated(lng);
    }
    //getRelatedConceptsTranslated(cid,"pt");
}
function getCurrConceptTranslated(lng) {
    if (currCid == null) {
        alert("please select a meaning first.");
        return;
    }
    //if(searchResultsLoaded.get(lng) != currCid){
    getRelatedConceptsTranslated(currCid, lng);
    //	searchResultsLoaded.put(lng,currCid);
    //}
    showSearchResultTab(lng)
}

function getRelatedConcepts() {
    if (currCid == null) {
        alert("please select a meaning first.");
        return;
    }
    var lng = "en";
    byId("searchresultscontainter_td_" + lng).innerHTML = "<img src='images/loading.gif'>loading...";
    byId("icon_" + lng).src = "images/loading.gif";
    tstr = new Date().getTime();
    var hw = getInnerText(byId("hw_" + currCid));
    Request.send("public_cn_search_getRelations.jsp?&t=" + tstr + "&cid=" + currCid + "&hw=" + escape(hw), "GET", null, getRelatedConceptsCallBack);
}

function getRelatedConceptsCallBack(resp) {
    byId("searchstring").value = resp;
    googleQuery("en");
}

function getRelatedConceptsTranslated(cid, lng) {
    byId("searchresultscontainter_td_" + lng).innerHTML = "<img src='images/loading.gif'>loading...";
    byId("icon_" + lng).src = "images/loading.gif";
    tstr = new Date().getTime();
    var hw = getInnerText(byId("hw_" + cid));
    Request.send("public_cn_search_getRelationsTranslated.jsp?lng=" + lng + "&t=" + tstr + "&cid=" + cid + "&hw=" + escape(hw), "GET", null, getRelatedConceptsTranslatedCallBack);
}

function getRelatedConceptsTranslatedCallBack(resp) {
    resp = resp.trim();
    var startIndex = resp.indexOf("site:") + 5;
    var endIndex = startIndex + 2;
    var lng = resp.substring(startIndex, endIndex);
    if (lng == "en") {
        resp = resp.substring(endIndex).trim();
    }
    if (resp.length > endIndex + 1) {

        byId("searchstring").value = resp;
        googleQuery(lng);
    } else {
        byId("icon_" + lng).src = "images/ico/stem.png";
        byId("searchresultscontainter_td_" + lng).innerHTML = "We're sorry<br>Dictionary lacks translation for the concept in selected language.";
    }
}

function saveAdvancedOptions() {
    Set_Cookie("cn_search_in_hw", byId("cn_search_in_hw").checked);
    Set_Cookie("cn_search_in_description", byId("cn_search_in_description").checked);
    Set_Cookie("cn_search_by_string", byId("cn_search_by_string").checked);
    Set_Cookie("cn_search_by_word", byId("cn_search_by_word").checked);
    Set_Cookie("cn_search_by_number", byId("cn_search_by_number").checked);

    var selectedPartsOfSpeech = "";
    for (var i = 0; i < byId("cn_part_of_speech").options.length; i++) {
        if (byId("cn_part_of_speech").options[i].selected) {
            selectedPartsOfSpeech += byId("cn_part_of_speech").options[i].value + ",";
        }
    }
    if (selectedPartsOfSpeech != "") selectedPartsOfSpeech = selectedPartsOfSpeech.substring(0, selectedPartsOfSpeech.length - 1);
    Set_Cookie("cn_part_of_speech", selectedPartsOfSpeech);
}

function fillAdvancedOptions() {
    byId("cn_search_by_string").checked = Get_Cookie("cn_search_by_string") == "true";
    byId("cn_search_by_word").checked = Get_Cookie("cn_search_by_word") == "true";
    byId("cn_search_by_number").checked = Get_Cookie("cn_search_by_number") == "true";
    byId("cn_search_in_hw").checked = Get_Cookie("cn_search_in_hw") == "true";
    byId("cn_search_in_description").checked = Get_Cookie("cn_search_in_description") == "true";
    if (Get_Cookie("cn_search_by_string") == null && Get_Cookie("cn_search_by_word") == null && Get_Cookie("cn_search_by_number") == null) {
        byId("cn_search_by_string").checked = true;
    }
    if (Get_Cookie("cn_search_in_hw") == null && Get_Cookie("cn_search_in_description") == null) {
        byId("cn_search_in_hw").checked = true;
    }
    if (Get_Cookie("cn_part_of_speech") != null) {
        var partsOfSpeech = Get_Cookie("cn_part_of_speech").split(',');
        for (var i = 0; i < byId("cn_part_of_speech").options.length; i++) {
            if (partsOfSpeech.contains(byId("cn_part_of_speech").options[i].value)) {
                byId("cn_part_of_speech").options[i].selected = true;
            }
        }
    } else {
        selectAll("cn_part_of_speech");
    }
    if (!byId("cn_search_by_string").checked && !byId("cn_search_by_word").checked && !byId("cn_search_by_number").checked) {
        byId("cn_search_by_string").checked = true;
    }
}
