﻿//SCRIPTCONTROLLER (execute functions at pageLoad)
function AWFScriptController() {
    this.m_pageLoadFunctions = Array();

    this.AddPageLoad = function(pageLoadFunction) {
        this.m_pageLoadFunctions.push(pageLoadFunction);
    }

    this.pageLoaded = function() {
        for (wierdoName = 0; wierdoName < this.m_pageLoadFunctions.length; wierdoName++) {
            this.m_pageLoadFunctions[wierdoName]();
        }
    }
}

var ScriptController = new AWFScriptController();

/* PAGELOAD 
Only one pageLoad() function is automatically executed.
Therefore all modules can use ScriptController.AddPageLoad(FunctionName) instead of window.pageload.*/
function pageLoad() {
    ScriptController.pageLoaded();
}

/* Helpers  */
function isUInt(s) {
    return (s.toString().search(/^[0-9]+$/) == 0);
}

function isInt(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

if (!Array.prototype.contains) {
    Array.prototype.contains = function(obj) {
        var len = this.length;
        for (var i = 0; i < len; i++) {
            if (this[i] === obj) { return true; }
        }
        return false;
    };
}
if (!Array.prototype.keyValueJoin) {
    Array.prototype.keyValueJoin = function() {
        var _ret = "";
        for (var key in this) {
            if (!(typeof (this[key]) == 'function')) {
                _ret += key + "=" + this[key] + ";";
            }
        }
        return _ret;
    }
}
/* RIS - UCSELECTOBJECT.ASCX */

var _ristextControl;
var _rishiddenfieldControl;
var _risnamehiddenfieldControl;
var _onClickDelegate;
var _risClientChangeFunction;

//OpenWindow
function showRisSelection(textboxID, risHiddenfieldID, risnameHiddenfieldID, ClientChangeFunction) {
    _ristextControl = document.getElementById(textboxID);
    _rishiddenfieldControl = document.getElementById(risHiddenfieldID);
    if (risnameHiddenfieldID) { _risnamehiddenfieldControl = document.getElementById(risnameHiddenfieldID); }
    var wnd = window.radopen(_detailPage_searchRis, "SearchRis");
    if ((wnd._left != null) && (wnd._top != null)) {
        wnd.MoveTo(wnd._left, wnd._top);
    } else {
        //de waarde van top berekenen
        var visibleHeight = document.body.parentNode.clientHeight;
        var popupHeight = wnd._height;
        if (popupHeight.indexOf('px') > 0) {
            popupHeight = popupHeight.substring(0, popupHeight.indexOf('px'));
        }
        var centerTop = (visibleHeight - popupHeight) / 2;
        if (centerTop < 0) {
            centerTop = 0;
        }
        //de waarde van left berekenen
        var visibleWidth = document.body.parentNode.clientWidth;
        var popupWidth = wnd._width;
        if (popupWidth.indexOf('px') > 0) {
            popupWidth = popupWidth.substring(0, popupWidth.indexOf('px'));
        }
        var centerLeft = (visibleWidth - popupWidth) / 2;
        if (centerLeft < 0) {
            centerLeft = 0;
        }

        //toepassen van de waarden
        if (wnd._top != null) {
            wnd.MoveTo(centerLeft, wnd._top);
        } else {
            if (wnd._left != null) {
                wnd.MoveTo(wnd._left, centerTop);
            } else {
                wnd.MoveTo(centerLeft, centerTop);
            }
        }
    }
    _risClientChangeFunction = ClientChangeFunction;
    return false;
}

//Handle Result
function OnRisSearchclose(radWindow) {
    try {
        var oValue = radWindow.argument;
        if (oValue) {
            if ((_risClientChangeFunction != null) && (_rishiddenfieldControl.value != oValue[0])) {
                _risClientChangeFunction(oValue);
            }
            if (oValue[1] == "") { oValue[1] = "." }
            _rishiddenfieldControl.value = oValue[0];
            if (_risnamehiddenfieldControl) { _risnamehiddenfieldControl.value = oValue[1] };
            if (!_ristextControl.isTextEdit) {
                _ristextControl.innerHTML = oValue[1];
            } else {
                _ristextControl.value = oValue[1];
            }
        }
    } catch (Err) {
        alert(Err.name + " " + Err.message);
    }
}

//delete
function deleteRis(txt, hfld, hfldName) {
    if (document.getElementById(txt)) { document.getElementById(txt).innerHTML = ''; }
    if (document.getElementById(hfld)) { document.getElementById(hfld).value = ''; }
    if (document.getElementById(hfldName)) { document.getElementById(hfldName).value = ''; }
    return false;
}


//NtsMessageList
function openNtSSectionDetailWindow(NtSViewURL, HeaderID, SectionID, WindowName) {
    window.open(NtSViewURL + '&VersionID=' + HeaderID + '&SectionID=' + SectionID, WindowName);
}

function openNtSVersionDetailWindow(NtSViewURL, HeaderID, WindowName) {
    window.open(NtSViewURL + '&VersionID=' + HeaderID, WindowName);
}

/* Fontresize */

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 86400000)); //86400000 = milliseconden per dag
        var expires = "; expires=" + date.toGMTString();
    }
    else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function changeFontSize(newSize) {
    var body = document.getElementsByTagName('body')[0];
    var size = parseInt(body.style.fontSize.replace("%", ""));
    if (size == null) { return };
    body.style.fontSize = newSize;
    createCookie('fontSize', body.style.fontSize, 365);
}

function setFontSize() {
    var cookieFontSize = readCookie('fontSize');
    var body = document.getElementsByTagName('body')[0];
    if (cookieFontSize == null) body.style.fontSize = '100%';
    else body.style.fontSize = cookieFontSize;
}

/* login module */

function SubmitEnter(control) {
    if (event.keyCode && event.keyCode == 13) {
        event.returnValue = false;
        event.cancel = true;
        control.click();
    }
}


