/**********************************************************************************
edge_admin.js
written by: Janette Thomaes
last updated: FEB 11 2008

This is the javascript used by the EDGE3 display pages

Linked in by all EDGE3 web based pages
**********************************************************************************/

///////////////////////////////////////////////////////////
// open a link either in the main page or in a popup
///////////////////////////////////////////////////////////
function listViewAction(lnk, dopop, win_name){
    if(lnk == "todo"){
        alert("Under Construction.");
        document.body.style.cursor = 'normal';
    }else if(lnk != "none"){
        if(dopop == true || dopop == "true"){
            if(!win_name){
                win_name = null;
            }
            var mywin = window.open(lnk,win_name,"directories=no,height=700,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width=900");
            document.body.style.cursor = 'normal';
            try{
                mywin.focus();
            }catch(exception){
            }
        }else{
            window.location.href=lnk;
        }
    }else{
        document.body.style.cursor = 'normal';
        return false;
    }
}
// these three are becoming obsolete...please use listViewAction (above) only
function listViewAction_sbox(selbox){
    push_button();
    var lnk = selbox.value.substring(0, selbox.value.indexOf("###"));
    var dopop = selbox.value.substring(selbox.value.indexOf("###") + 3, selbox.value.length);
    selbox.options[0].selected = true;
    listViewAction(lnk, dopop);
}

function listViewAction_btn(btn){
    push_button();
    var lnk = btn.alt.substring(0, btn.alt.indexOf("###"));
    var dopop = btn.alt.substring(btn.alt.indexOf("###") + 3, btn.alt.length);
    listViewAction(lnk, dopop);
}

function listViewAction_img(img){
    push_button();
    var lnk = img.name.substring(0, img.name.indexOf("###"));
    var dopop = img.name.substring(img.name.indexOf("###") + 3, img.name.length);
    listViewAction(lnk, dopop);
}

function openCompanyAdminPage(company, which, arguments){
    push_button();
    var lnk;
    var dopop = false;
    if(checkaccess(company, "company="+company+"&admin_page="+which)){
        if(which.indexOf("http://") == 0 && which.indexOf("###") != -1){
            var gotoarr = which.split("###");
            lnk = gotoarr[0];
            dopop = gotoarr[1];
        }else if(which.indexOf("F.") == 0){
            lnk = "/cgi-bin/display_admin/display_admin.sh?company=" + company + "&frequency=" + which
        }else{
            lnk = "/cgi-bin/company_admin/company_admin.sh?company="+company+"&admin_page="+which;
        }
        if(arguments){
            if(lnk.indexOf("?") != -1){
                lnk += arguments;
            }else{
                lnk += arguments.replace("&", "?");
            }
        }
        listViewAction(lnk, dopop);
    }else{
        document.body.style.cursor = 'normal';
    }
}

function openCompanyAdminPageFromList(company,which){
    push_button();
    window.location.href="/cgi-bin/company_admin/company_admin.sh?company="+company+"&admin_page="+which;
    document.body.style.cursor = 'normal';
}
///////////////////////////////////////////////////////////
// return true if access is granted this user
///////////////////////////////////////////////////////////
function checkaccess(company){
    var response;
    var allowed_users = document.forms("company_" + company + "_form").allowed.value;
    var current_user = document.forms.security.user.value;
    switch(current_user){
        case 'NO COOKIE':
            response = confirm('You have not set your EDGE username and password on this computer.\n\nClick "OK" to enter your EDGE credentials now.');
            break;
        case 'INVALID LOGIN':
            response = confirm('The username or password entered is incorrect.\n\nClick "OK" to try another login now.');
            break;
        case 'NO FILE':
            alert('A file necessary for determining edit authorization is missing.\n\nPlease contact EDGE Support for assistance.');
            return false;
        default:
            if((" " + allowed_users + ",").indexOf(" " + current_user + ",") != -1){
                return true;
            }else{
                response = alert('You do not have access to edit this company.\n\nAuthorization can be obtained through Publishing, EDGE Support or anyone who currently holds editing rights for this company.');
                return false;
            }
            break;
    }
    if(response){
        window.location.href="/cgi-bin/company_admin/set_user.sh?company="+company;
    }else{
        return false;
    }
}


///////////////////////////////////////////////////////////
//	a button was pushed
///////////////////////////////////////////////////////////
function push_button(){
    document.body.style.cursor = 'wait';
    setTimeout("return true;", 10);
}

///////////////////////////////////////////////////////////
//	onmouseover: let the user know what the icon referes to
///////////////////////////////////////////////////////////

function icon_hover(icon_id, refer_div, exclude_div){
    if(document.getElementById(refer_div)){
        document.getElementById(refer_div).style.backgroundColor="#F2F2F2";
    }else if(refer_div.indexOf("###") != -1){
        var divs = refer_div.split("###");
        for(var i=0; i<divs.length; i++){
            if(document.getElementById(divs[i])){
                document.getElementById(divs[i]).style.backgroundColor="#F2F2F2";
            }
        }
    }
    if(document.getElementById(exclude_div)){
        document.getElementById(exclude_div).style.backgroundColor="white";
    }else if(exclude_div.indexOf("###") != -1){
        var divs = exclude_div.split("###");
        for(var i=0; i<divs.length; i++){
            if(document.getElementById(divs[i])){
                document.getElementById(divs[i]).style.backgroundColor="white";
            }
        }
    }
    imgover(icon_id);
}

function icon_out(icon_id, refer_div){
    if(document.getElementById(refer_div)){
        document.getElementById(refer_div).style.backgroundColor="transparent";
    }else if(refer_div.indexOf("###") != -1){
        var divs = refer_div.split("###");
        for(var i=0; i<divs.length; i++){
            if(document.getElementById(divs[i])){
                document.getElementById(divs[i]).style.backgroundColor="transparent";
            }
        }
    }
    imgout(icon_id);
}

function imgover(img_id){
    document.getElementById(img_id).className = document.getElementById(img_id).className + "_over";
}
function imgout(img_id){
    document.getElementById(img_id).className = document.getElementById(img_id).className.replace("_over", "");
}

// is this obsolete???
function img_link_swap(img_id, img){
    document.getElementById(img_id).src="../../images/"+img;
}

///////////////////////////////////////////////////////////
//	Janette's toolbox
///////////////////////////////////////////////////////////

// check if a certain value is in an array
Array.Exists = function(ary, element){
    if(element in arrToObj(ary)){
        return true;
    }else{
        return false;
    }
}

// find position in array
Array.GetPos = function(ary, element){
    for(var i=0; i<ary.length; i++){
        if(ary[i] == element){
            return i;
        }
    }
    return -1;
}

// convert an array to an object
function arrToObj(arr){
    var obj = {};
    for(var index=0;index<arr.length;index++){
        obj[arr[index]]='';
    }
    return obj;
}
// convert a hex number to a decimal number array
function hexToDec(longHex) {
	r=parseInt(longHex.substring(0,2),16);
	g=parseInt(longHex.substring(2,4),16);
	b=parseInt(longHex.substring(4,6),16);
	return new Array(r,g,b);
}
// convert a decimal number to a hex number (string)
function decToHex(dec) {
	hex=dec.toString(16);
	if(hex.length==1)hex="0"+hex;
	if(hex==100)hex="FF";
	return hex.toUpperCase();
}

// if the on_load_form exists call the function it contains
// this is used only on company_edge_page currently to put the 'will not go public' background
// and shrink the title if it is too long
window.onload = function() {
    if(document.forms.on_load_form){
        if(document.forms.on_load_form.do_on_load){
            eval(document.forms.on_load_form.do_on_load.value);
        }
    }
}

function company_edge_page(compliant, companyname){
    if(!compliant){
        document.body.className = "NONCOMP"
    }
    if(companyname.length > 35){
        document.getElementById("main_page_title").className = "LONGTITLE";
    }
}

///////////////////////////////////////////////////////////
// AJAX -- used to populate a portion of the page after original page load
// content_url -> url to backend script that returns html
// content_div -> the div (or other html tag) which will contain the content returned
///////////////////////////////////////////////////////////

function GetContent(content_url, content_div){
    var xmlHttp;
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

    xmlHttp.onreadystatechange=function(){
        if(xmlHttp.readyState==4){
            ShowContent(xmlHttp.responseText, content_div);
        }
    }
    if(content_url.indexOf("?") != -1){
        content_url += "&curdate=" + (new Date).getTime();
    }else{
        content_url += "?curdate=" + (new Date).getTime();
    }
    xmlHttp.open("GET",content_url,true);
    xmlHttp.send(null);
}

function GetContentAsync(content_url, content_div){
    var xmlHttp;
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

    xmlHttp.onreadystatechange=function(){
        if(xmlHttp.readyState==4){
            ShowContent(xmlHttp.responseText, content_div);
        }
    }
    if(content_url.indexOf("?") != -1){
        content_url += "&curdate=" + (new Date).getTime();
    }else{
        content_url += "?curdate=" + (new Date).getTime();
    }
    xmlHttp.open("GET",content_url,false);
    xmlHttp.send(null);
}

function ShowContent(content_str, content_div){
    // remove fame output
//    alert(content_str);
    if(content_str.lastIndexOf("*") != -1){
        var contents = content_str.substring(0, content_str.lastIndexOf("*"));
        contents = contents.substring(contents.lastIndexOf("*")+1, contents.length);
    }else{
        contents = content_str;
    }
    document.getElementById(content_div).innerHTML = contents;
    if(document.getElementById('code_spot')){
        document.getElementById('code_spot').innerText = contents;
    }
}

// this is like GetContent above except it expects no data back
// if debug_div is not the empty string then any output from the backend script will go there
function RunProcedure(content_url, debug_div){
    var xmlHttp;
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    if(debug_div != "" && document.getElementById(debug_div)){
        xmlHttp.onreadystatechange=function(){
            if(xmlHttp.readyState==4){
                ShowContent(xmlHttp.responseText, debug_div);
            }
        }
    }

    if(content_url.indexOf("?") != -1){
        content_url += "&curdate=" + (new Date).getTime();
    }else{
        content_url += "?curdate=" + (new Date).getTime();
    }
    xmlHttp.open("GET",content_url,false);  // IMPORTANT: third parameter is false here so that we wait until the procedure is finshed before moving on
    xmlHttp.send(null);
}

function ToggleDivSource(source_div){
    if(document.getElementById('list_source_div').style.display=='block'){
        document.getElementById('list_source_link').innerHTML = "(show list source)";
        document.getElementById('list_select_link').innerHTML = "";
        document.getElementById('list_source_div').innerText = "";
        document.getElementById('list_source_div').style.display='none';
    }else{
        document.getElementById('list_source_link').innerHTML = "(hide list source)";
        document.getElementById('list_select_link').innerHTML = "(select source)";
        document.getElementById('list_source_div').innerText = document.getElementById(source_div).innerHTML;
        document.getElementById('list_source_div').style.display='block';
    }
}

function SelectDivText(div_id){
    var selection, range, doc, win;
    var node = document.getElementById(div_id);
    if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof win.getSelection != 'undefined' && typeof doc.createRange != 'undefined' && (selection = window.getSelection()) && typeof selection.removeAllRanges != 'undefined') {
        range = doc.createRange();
        range.selectNode(node);
        selection.removeAllRanges();
        selection.addRange(range);
    } else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())) {
        range.moveToElementText(node);
        range.select();
    }
}

function ToggleFormula(id){
    if(document.getElementById("formula_cell_"+id).className == "formula_cell_show"){
        document.getElementById("formula_cell_"+id).className = "formula_cell";
    }else{
        document.getElementById("formula_cell_"+id).className = "formula_cell_show";
        document.getElementById("formula_cell_"+id).style.top=event.clientY + document.body.scrollTop;
        document.getElementById("formula_cell_"+id).style.left=event.clientX + document.body.scrollLeft;
    }
}
