﻿function openWindow(url)
{
    window.open(url);
}

function openDialogue(url, id, key, reportType, height) {
        
    var top = window.screen.height / 2 - 250;
    var left = window.screen.width / 2 - 100;
       
    if(!height) {
       height = 500;
    }
       
    var dialog = window.open(
        "Dialogue/" + url + ".aspx?reportId=" + id + "&key=" + key + "&reportType=" + reportType, 
        "_blank", 
        "top=" + top + ",left=" + left + ",height=" + height + 
        ",width=600,location=no,status=no,menubar=no,toolbar=no,resizable=yes,scrollbars=yes", 
        false);
    
    dialog.onunload = function() {
    // Reload the main window
    //alert('reload');
    }
}

function ReplaceParentElement(parentWindowsElementId, thisWindowElementId)
{
    /// <summary>Replaces the innerHTML of the given element in the 
    /// parent window with the given element in this window</summary>
    /// <param name="parentWindowsElementId" type="String">The element to be replaced</param>
    /// <param name="thisWindowElementId" type="String">The element to be copied</param>
    
    var reportList = null;
    var dialogueList = null;
    
    if (window.opener == null) 
    {
        return;
    }
        
    reportList = window.opener.document.getElementById(parentWindowsElementId);
    dialogueList = document.getElementById(thisWindowElementId);
    
    if (reportList != null && dialogueList != null)
    {
        reportList.innerHTML = dialogueList.innerHTML;
    }
}

function updateRemainingChars(textbox, spanId, max) 
{
    /// <summary>Updates the number of remaining chars</summary>

    var tegn = (new String(textbox.value)).length;
    var igjen = max-tegn;
        
    if(igjen > 0) {
        var tekst = document.createTextNode("(Maks " + max + " tegn) - " + igjen + " tegn igjen.");
    } else {
        var tekst = document.createTextNode("(Maks " + max + " tegn) - " + (0-igjen)+ " for mange tegn");
    }
    hjelpetekstContainer = document.getElementById(spanId)
    hjelpetekstContainer.replaceChild(tekst, hjelpetekstContainer.firstChild);
}

function updateTotalChars(textbox, spanId) 
{
    /// <summary>Updates the number of total chars</summary>
    
    var tegn = (new String(textbox.value)).length;
        
    var tekst = document.createTextNode("(" + tegn + " tegn)");
    
    hjelpetekstContainer = document.getElementById(spanId)
    hjelpetekstContainer.replaceChild(tekst, hjelpetekstContainer.firstChild);
}

function updateMinMaxChars(textbox, spanId, min, max) 
{
    /// <summary>Updates the number of total chars</summary>
    
    var length = (new String(textbox.value)).length;
    var tooFew = min - length;
    var tooMany = length - max;
    
    var output = "(" + length + " tegn";
       
    if (tooFew > 0)
        output += ", " + tooFew + " for få tegn)";
    else if (tooMany > 0)
        output += ", " + tooMany + " for mange tegn)";  
    else
        output += ")";
       
    var tekst = document.createTextNode(output);
    
    hjelpetekstContainer = document.getElementById(spanId)
    hjelpetekstContainer.replaceChild(tekst, hjelpetekstContainer.firstChild);
}



function openHelpText(textId, context, regionId, height)
{
    var top = ( window.screen.height / 2 - 250);
    var left =  ( window.screen.width / 2 - 100);
    
    if(!height) {
        height = 500;
    }
    
    var dialog = window.open("dialogue/veiledning.aspx?textId=" + textId +"&context=" + context + "&regionId=" + regionId, "Veiledning", "top="+top+",left="+left+",height="+height+",width=600,location=no,status=no,menubar=no,toolbar=no,resizable=yes,scrollbars=yes", false);
}

function TimeConsumingSubmit(button)
{
    //button.disabled = true;
    button.value = "Legg til (kan ta tid)...";
}