﻿

var PopWinHandle;
function PopWin(sUrl, sName, iWidth, iHeight, sScroll) {
    sScroll = '0';
    if (PopWinHandle != null) PopWinHandle.close();
    LeftPosition = (screen.width) ? (screen.width - iWidth) / 2 : 0;
    TopPosition = (screen.height) ? (screen.height - iHeight) / 2 : 0;
    sSettings = 'height=' + iHeight + ',width=' + iWidth + ',top=' + TopPosition + ',left=' + LeftPosition + ',status=1,scrollbars=0';
    PopWinHandle = window.open(sUrl, sName, sSettings);
    PopWinHandle.focus();
}

function PopWinWithMenu(sUrl, sName, iWidth, iHeight) {
    sScroll = 0;
    if (PopWinHandle != null) PopWinHandle.close();
    LeftPosition = (screen.width) ? (screen.width - iWidth) / 2 : 0;
    TopPosition = (screen.height) ? (screen.height - iHeight) / 2 : 0;
    sSettings = 'height=' + iHeight + ',width=' + iWidth + ',top=' + TopPosition + ',left=' + LeftPosition + 'resizable=yes,menubar=yes,scrollbars=yes,status=yes,toolbar=yes';
    PopWinHandle = window.open(sUrl, sName, sSettings);
    PopWinHandle.focus();
}

function ShowDialog(sUrl, iWidth, iHeight) {
    var sFeatures = "dialogHeight: " + iHeight + "px;dialogWidth: " + iWidth + "px;";
    var result = window.showModalDialog(sUrl, window, sFeatures);
    return result;
}

// Get radio button list value
function GetRadioButtonValue(id) {
    var val = $('#' + id + ' input:radio:checked').val();
    return val;
}

function GetCheckboxValue(id) {
    var val = $('#' + id + ' input:checkbox:checked').val();
    if (val != null && val.toLowerCase() == "on")
        return true;
    else
        return false;
}

function PopImage(sUrl) {
    PopWin(sUrl, '', 100, 100, 0);
}


function PopClose() {
    PopWinHandle = null;
}


function Replace(Expression, Find, Replacement) {
    NewStr = ''
    for (i = 0; i < Expression.length; i++) {
        if (Expression.charAt(i) == Find) {
            NewStr += Replacement
        } else {
            NewStr += Expression.charAt(i)
        }
    }
    return NewStr
}

function SetPageTitle() {
    if (ie) {
        var title = ''
        var h1 = document.getElementsByTagName("H1")
        if (h1[0]) {
            title = h1[0].innerText;
        }

        document.title = title;
    }
}

function ReloadParentAndClose() {
    window.opener.document.forms[0].submit();
    window.opener.PopClose();
    window.close();
}

function ReloadParent() {
    window.opener.document.forms[0].submit();
    PopWinHandle.focus();
}

//function to check if request is under process
var FormSubmitted = 0;
var DontCheckSubmit = 0;
function submitForm(form) {
    if (DontCheckSubmit == 1)
        return true;

    if (FormSubmitted == 0) {
        FormSubmitted = 1;
        //form.submit();
        return true;
    } else {
        alert('Vänligen vänta, vi behandlar just nu din förfrågan.');
        return false;
    }
}

//functions to set options in dropdown onKewDown:
var SearchStr = ''
var LastSender
function KeySearch(Event, Sender) {
    if (LastSender != Sender) LastSender = Sender;
    SearchStr += String.fromCharCode(Event.keyCode);

    //if(SearchStr.length > 2) {
    //alert(SearchStr.length);
    for (I = 0; I < Sender.options.length; I++) {
        OptionValue = Sender.options[I].text.substr(0, SearchStr.length)
        if (OptionValue.toLowerCase() == SearchStr.toLowerCase()) {
            SetOption(Sender, I)
            break;
        }
    }
    //}
}

function ismaxlength(obj, maxlength) {
    var mlength = maxlength;
    if (obj.value.length > mlength)
        obj.value = obj.value.substring(0, mlength)
}

function SetOption(Object, Value) {
    Object.options[Value].selected = true
    setTimeout("ResetKeySearch()", 5000)
}

function ResetKeySearch() {
    SearchStr = ''
}

//end KeySearch in options



function jsPrintDivContent(divToPrint) {

    var da = (document.all) ? 1 : 0;
    var pr = (window.print) ? 1 : 0;

    if (!pr)
        return;

    var printArea = document.getElementById(divToPrint);

    if (printArea == null && da)
        printArea = document.all.divToPrint;

    if (printArea) {
        var sStart = "<html><head>" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/Styles/MainStyle.css\" />" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/Styles/menustyle.css\" />" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/Styles/ContentPageStyle.css\" />" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/Styles/LeftMenuStyle.css\" />" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/Styles/SearchStyle.css\" />" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/Styles/AdditionalContentPageStyle.css\" />" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/Styles/FaceBox.css\" />" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/Styles/NewsStand.css\" />" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/Styles/StolenObjects.css\" />" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/util/styles/contextmenu.css\" />" +
                     "<link type=\"text/css\" rel=\"stylesheet\" href=\"/Styles/HideForPrint.css\" />" +
                     "</head><body onload=\"javascript:window.print();\">";
        sStop = "</body></html>";

        var w = window.open('about:blank', 'printWin', 'resizable=yes,width=650,height=440,scrollbars=yes');
        wdoc = w.document;
        wdoc.open();
        wdoc.write(sStart + printArea.innerHTML);
        wdoc.writeln(sStop);
        wdoc.close();
    }

}