/*
' **************************************************
' © VWA Software Development 2006
' Release: 1.01.0000
' Changed: Nov 7, 2008
' Author : Berco Wassink/Henk Salomons
' **************************************************
*/

// Extensions
String.prototype.Contains = function(textToCheck) {
    return (this.toLowerCase().indexOf(textToCheck.toLowerCase()) > -1);
}


function removeSys(sURL) {
    // _sys_230_230_ eruit filteren.
    var regexSys = /_sys_[0-9]+_[0-9]+_/i;
    sURL = sURL.replace(regexSys, '');
    return sURL;
}


// Browser info
var s = navigator.userAgent.toLowerCase();
var VWABrowserInfo =
{
    IsIE: s.Contains('msie'),
    IsIE7: s.Contains('msie 7'),
    IsGecko: s.Contains('gecko'),
    IsSafari: s.Contains('safari'),
    IsOpera: s.Contains('opera'),
    IsMac: s.Contains('macintosh'),
    IsWin: navigator.appVersion.toLowerCase().Contains('win')
};

function cloneObject(oObject) {
    for (i in oObject) {
        this[i] = oObject[i];

    }
}


function getElementStyle(elem, StyleAttr) {
    if (elem.currentStyle) {
        return elem.currentStyle[StyleAttr];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(StyleAttr);
    }
    return "";
}

/*
------------------------------------------------------------------- 
File manager 
*/

var _sID;
var _FuncOnChange = '';

// File manager openen.
function selectFile(sID, sBackGroundImgDir, sFunctionOnChange, sType) {
    if (!sFunctionOnChange)
        sFunctionOnChange = '';
    if (!sType)
        sType = '';

    _FuncOnChange = sFunctionOnChange;

    // Id onthouden 
    _sID = sID
    var sURL = 'pop_fileManager.aspx';
    sURL += '?BaseSubDir=' + sBackGroundImgDir;
    if ($get(sID))
        sURL += '&file=' + $get(sID).value
    sURL += '&Type=' + sType;

    top.OpenPopup(window, 'fileResult', sURL, 'File', 750, 520, 'Afbeelding kiezen', true);
}

//Resultaat van file manager
function fileResult(ReturnValue) {
    if (ReturnValue) {
        $get(_sID).value = ReturnValue;
    }
    // Onchange functie uitvoeren.
    eval(_FuncOnChange + '("' + ReturnValue + '")');
}

/* --------------------------------------------------------------------------------
Modaal openen van popupjes
-----------------------------------------------------------------------------------*/
var oHolder = null;
var oPopup = null;

var oPopupStruct = function(_sPopupID, _iWidth, _iHeight, _sHTML, _sOkID, _sCancelID, bIframe, bRoundedCurves) {
    this.sPopupID = _sPopupID;               // Id van popup.
    this.iWidth = _iWidth || 300;           // Breedte van popup;
    this.iHeight = _iHeight || 400;          // Hoogte van popup;
    this.sHTML = _sHTML;                   // Html waar popup mee gevult wordt.
    this.sOkId = _sOkID || 'btnOk';        // ID van knop om te bevestigen.
    this.sCancelId = _sCancelID || 'btnCancel'; // ID van knop om te annuleren.
    this.sOpenerId = 'divOpener' + _sPopupID;  // ID van opener ( divOpener + ID )
    this.oHolder = null;
    this.onCloseHandler = null;                  // Array van functiepointers    
    this.args = null;                      // argumenten die meegestuurd kunnen worden.
    this.bIframe = (bIframe ? true : false);  // Wordt de popup gebruikt voor een iframe, zo ja
    // Moet in de sHTML de url staan voor dit iframe
    this.oContent = null;                   // Hier komt de daatwerkelijke inhoud in.
    this.bRoundedCurves = (bRoundedCurves ? true : false);
}

// maakt dynamisch een tabel als masker
// Hieronder komen 2 divjes, in het laatste divje wordt
// de HTML gekopieerd van de opgegeven ID
function ModalPopupOpen(sPopupID, iWidth, iHeight, sHTML, sOkId, sCancelId, bIframe, bRoundedCurves) {

    if (!oPopup)
        oPopup = new oPopupStruct(sPopupID, iWidth, iHeight, sHTML, sOkId, sCancelId, bIframe, bRoundedCurves);

    // Div opzoeken die gepresenteerd moet worden.
    var oTarget = $get(sPopupID);

    // Controleren of deze popup al geopend is!
    var oDiv = null;

    oDiv = $get(oPopup.sOpenerId);

    if (!oDiv) {
        oDiv = document.createElement("div");
        oDiv.id = oPopup.sOpenerId;
        oDiv.className = 'modalForeGround';
        oDiv.style.position = 'absolute';
        // oDiv.style.overflow = 'hidden';    
        oDiv.style.overflow = 'visible';

        if (oPopup.bRoundedCurves) {
            // tabel maken met evt. afgeronde hoekjes..
            var oTable = document.createElement("table");
            oTable.cellPadding = 0;
            oTable.cellSpacing = 0;
            oTable.style.width = "100%";
            oTable.style.height = "100%";
            oTable.className = 'pop';
            oDiv.appendChild(oTable);
            var oRow1 = oTable.insertRow(0);
            var oRow2 = oTable.insertRow(1);
            var oRow3 = oTable.insertRow(2);
            var oCellTL = oRow1.insertCell(0);
            oCellTL.className = 'popTL';
            var oCellT = oRow1.insertCell(1);
            oCellT.className = 'popT';
            var oCellTR = oRow1.insertCell(2);
            oCellTR.className = 'popTR';

            var oCellCL = oRow2.insertCell(0);
            oCellCL.className = 'popCL';
            var oCellC = oRow2.insertCell(1);
            oCellC.className = 'popC';
            var oCellCR = oRow2.insertCell(2);
            oCellCR.className = 'popCR';

            var oCellBL = oRow3.insertCell(0);
            oCellBL.className = 'popBL';
            var oCellB = oRow3.insertCell(1);
            oCellB.className = 'popB';
            var oCellBR = oRow3.insertCell(2);
            oCellBR.className = 'popBR';
            oPopup.oContent = oCellC;
        } else {
            oPopup.oContent = oDiv;
        }
    }
    else {
        oPopup.ReFill = true;
    }

    if (oTarget) {
        if (!iWidth)
            oPopup.iWidth = parseInt(oTarget.style.width);
        if (!iHeight)
            oPopup.iHeight = parseInt(oTarget.style.height) + (oPopup.bRoundedCurves ? 26 : 0);
    }

    // Controleren op iFrame
    if (oPopup.bIframe) {
        // Alleen iframe maken als het geen refill is.
        if (!oPopup.ReFill)
            oPopup.oContent.innerHTML = '<iframe name="' + oPopup.sPopupID + 'Ifrm" id="' + oPopup.sPopupID + 'Ifrm" src="javascript:void(document.write(\'\'))" style="width:100%; height:100%; background:#ECE9D8;" vspace="0" scrolling="no" frameborder="0" border="0"></iframe>';
    } else {

        // Controleren of er html is meegestuurd, zo ja deze verwerken.     
        if (sHTML && sHTML.length != 0) {
            oPopup.oContent.innerHTML = sHTML;
            oPopup.oHolder = null;
        } else if (oTarget) {
            // Element opzoeken waar div op dat moment is staat.
            // En bewaren in public oPopup.oHolder
            oPopup.oHolder = VWABrowserInfo.IsGecko ? oTarget.parentNode : oTarget.parentElement;

            // Target verwijderen en plaatsen in oDiv.
            oPopup.oHolder.removeChild(oTarget);
            oPopup.oContent.appendChild(oTarget);
        }
    }

    if (oPopup.bIframe) {
        setTimeout(popupSetURL, 5);
    }

    if (!oPopup.ReFill) {

        // Modaal masker maken            
        CreateModalMask(oPopup.sPopupID, 'modalBackground');

        // Hieronder de popup div met inhoud plaatsen   
        document.body.appendChild(oDiv);

        // Div in het midden van het scherm postitioneren.
        SetPosition(oDiv, oPopup.iWidth, oPopup.iHeight);

        // Key down handler toevoegen.
        $addHandler(document, 'keydown', popupKeyPressed);
    } else {
        SetPosition(oDiv, oPopup.iWidth, oPopup.iHeight);
    }
}

function popupSetURL() {
    // Iframe opzoeken.
    iframe = document.getElementById(oPopup.sPopupID + 'Ifrm').contentWindow;
    var sURL = oPopup.sHTML;

    // Datum voor uitschakelen cache.
    var oData = new Date();
    sURL += (sURL.Contains('?') ? '&' : '?') + 't=' + oData.format("hms");
    iframe.location = sURL;
}

function popupKeyPressed(e) {
    // Enter
    if (e.keyCode == 13) {
        var okButton = $get(oPopup.sOkId);
        if (okButton && okButton.onclick)
            okButton.onclick();
    }
    // Escape
    if (e.keyCode == 27) {
        var cancelButton = $get(oPopup.sCancelId);
        if (cancelButton && cancelButton.onclick)
            cancelButton.onclick();
    }
}


function ModalPopupClose(bDirectClose) {
    // Controleren of kort vertraagd moet worden voor afronding aanroep vanuit popup iframe
    if (!bDirectClose) {
        window.setTimeout('ModalPopupClose(true)', 100);
        return false;
    }

    var oAnimation = $find('BoxAnimation');
    if (oAnimation)
        oAnimation._onClick.stop();

    // Opener verwijderen
    var oOpenener = $get(oPopup.sOpenerId);
    if (oOpenener) {
        var oTarget = null;
        
        // Controleren of er html geplaats is of een div gekopieerd.
        if (oPopup.oHolder) {
            // Div opzoeken die terug geplaatst moet worden
            oTarget = oPopup.oContent.childNodes[0];
            if (oTarget) {
                // En terug plaatsen in zijn holder voor hergebruik.
                oPopup.oHolder.appendChild(oTarget);
            }
        }
    }

    oOpenener.innerHTML = '';
    document.body.removeChild(oOpenener);

    oOpenener.id = '';
    oOpenener = null;
   

    // masker verwijderen
    document.body.removeChild($get("tblOpener" + oPopup.sPopupID));

   
    $removeHandler(document, 'keydown', popupKeyPressed);

    if (oPopup.onCloseHandler)
        oPopup.onCloseHandler(oPopup.args);
        
   
    oPopup = null;
}

// Maakt modal masker
function CreateModalMask(sID, sClassName) {

    var oMaskDiv = document.createElement("div");
    if (sClassName)
        oMaskDiv.className = sClassName;

    oMaskDiv.id = 'tblOpener' + sID;

    oMaskDiv.style.cssText = "z-index:1000;position: absolute; top: 0px; width: 100%; height:100%;";
    document.body.appendChild(oMaskDiv);

    var iHeight = document.body.scrollHeight;
    if (typeof (iDivsetHeight) != 'undefined')
        iHeight = (iDivsetHeight > iHeight) ? iDivsetHeight : iHeight;
    oMaskDiv.style.height = iHeight + 'px';
    
    var oAnimation = $find('FadeAnimation');

    // Move animatie opzoeken.
    if (oAnimation) {     
        oAnimation._onClick._animation.set_target(oMaskDiv);
        oAnimation._onClick._animation.play();
    }
}


function CheckPosition(oPos) {
     // Screen size
    GetScreenSize();
    
    if (!oPos.MarginWidth)
        oPos.MarginWidth = 0;

    if (!oPos.MarginHeight)
        oPos.MarginHeight = 0;

    // Controleren of het om procenten gaat
    var iIndex = oPos.InnerWidth.toString().lastIndexOf('%');

    if (iIndex > 0) {
        oPos.InnerWidth = oPos.InnerWidth.substring(0, iIndex)
        oPos.InnerWidth = (iWindowWidth * (oPos.InnerWidth / 100)) - oPos.MarginWidth;
    }
    iIndex = oPos.InnerHeight.toString().lastIndexOf('%');
    if (iIndex > 0) {
        oPos.InnerHeight = oPos.InnerHeight.substring(0, iIndex)
        oPos.InnerHeight = (iWindowHeight * (oPos.InnerHeight / 100)) - oPos.MarginHeight;
    }

    if (oPos.InnerWidth <= 0)
        oPos.InnerWidth = 515;
    if (oPos.InnerHeight <= 0)
        oPos.InnerHeight = 360;

    // Randen toevoegen
    oPos.PopupWidth = oPos.InnerWidth + oPos.MarginWidth;
    oPos.PopupHeight = oPos.InnerHeight + oPos.MarginHeight;
    oPos.offsetY = 0
    
    var oDivSet = $get('DivSet');
    if (oDivSet && !bUpdate) {
       oPos.offsetY = Sys.UI.DomElement.getBounds($get('DivSet')).y;
    }
    
    return oPos;
}


// functie zorgt voor het juist positioneren van oPopup
function SetPosition(oPopupDiv, iInnerWidth, iInnerHeight, iMarginWidth, iMarginHeight,  oAnimation) {

    var oBounds = null;

    // In geval er een window  resize wordt uitgevoerd, is oPopupDiv null
    if (!oPopupDiv || Object.getTypeName(oPopupDiv) === 'Sys.UI.DomEvent') {
        oPopupDiv = $get(oPopup.sOpenerId);
        if (oPopupDiv && oPopupDiv._bounds)
            oBounds = CheckPosition(oPopupDiv._bounds);
    } else {
       oBounds = CheckPosition({ 'PopupWidth': 0, 'PopupHeight': 0, 'MarginWidth': iMarginWidth, 'MarginHeight': iMarginHeight, 'InnerHeight': iInnerHeight, 'InnerWidth': iInnerWidth });
    }

    if (!oPopupDiv)
        return false;    
        
    // Indien een herpositie, de al aanwezige instellingen behouden.
    if (!oPopupDiv._PositionSet) {
        oPopupDiv.style.position = 'absolute';
        oPopupDiv.style.zIndex = 1001;
        oPopupDiv.style.display = 'block';
    }
 
  
    if (oAnimation) {
        oAnimation.play();            
    } else {
        oPopupDiv.style.width = oBounds.PopupWidth + 'px';
        oPopupDiv.style.height = oBounds.PopupHeight + 'px';
        oPopupDiv.style.left = (iWindowWidth / 2) - (oBounds.PopupWidth / 2) + 'px';
        oPopupDiv.style.top = ((iWindowHeight / 2) - (oBounds.PopupHeight / 2) - oBounds.offsetY) + 'px';
    }       
    
   // oPopupDiv._width = oBounds.PopupWidth;
   // oPopupDiv._height = oBounds.PopupHeight;
    oPopupDiv._left = (iWindowWidth / 2) - (oBounds.PopupWidth / 2);
    oPopupDiv._top = ((iWindowHeight / 2) - (oBounds.PopupHeight / 2) - oBounds.offsetY);
    oPopupDiv._PositionSet = true;
    oPopupDiv._bounds = oBounds;
    
    window.setTimeout(SetPositionMask, 200); 
}

function SetPositionMask() {
   
    if (!oPopup)
        return false;
    // Masker nog resizen
    var oMask = $get('tblOpener' + oPopup.sPopupID);
    if (oMask) {
        var iScrollHeight = document.body.scrollHeight;        
        if (typeof (iDivsetHeight) != 'undefined')
            iScrollHeight = (iDivsetHeight > iScrollHeight) ? iDivsetHeight : iScrollHeight;
        oMask.style.height = iScrollHeight + 'px';
        oMask.style.width = document.body.scrollWidth + 'px';
    }
}

/* --------------------------------------------------------------------------------
Einde  Modaal openen van popupjes
-----------------------------------------------------------------------------------*/


/*  ------------------------------------------------------------------- 
Validatie 
-----------------------------------------------------------------------*/

function isNumber(sValue) {
    // Parameter controleren
    if ((typeof (sValue) == 'undefined') || (sValue == null))
        sValue = '';
    else if (typeof (sValue) == 'object')
        sValue = GetValue(sValue);

    if (sValue.length == 0)
        return false;
    else {
        sValue = replaceString(sValue, ',', '.');
        return !isNaN(sValue);
    }
}

function toNumber(oNumber) {
    var iNumber = 0;

    if (typeof (oNumber) === "number" && !isNaN(oNumber))
        return oNumber;

    if (typeof (oNumber) === "string")
        iNumber = Number.parseInvariant(replaceString(oNumber, ',', '.'));

    if (!isNumber(iNumber))
        iNumber = 0;
    return iNumber
}

function Val(sValue) {
    if ((sValue.length == 0) || (isNaN(sValue)))
        return 0;
    return parseFloat(sValue);
}

function isImage(sValue) {
    if (!sValue)
        return false;

    return sValue.match("^.+\.((jpg)|(gif)|(png)|(jpeg))$");
}


/*  ------------------------------------------------------------------- 
Einde Validatie 
-----------------------------------------------------------------------*/

function VWA_FormatNumber(iNumber) {
    //var sNumber = iNumber.localeFormat("D");
    var sNumber = replaceString(iNumber, '.', ',');
    return sNumber;
}

function VWA_FormatNumberRound(iNumber, iDec, bThousand) {
    // Getal afronden
    if (iDec >= 0)
        iNumber = round(iNumber, iDec);

    var sNumber = iNumber.toString();
    if (iDec >= 0) {
        var iPos = sNumber.lastIndexOf('.');
        if (iPos >= 0)
            iPos = sNumber.length - iPos - 1;
        else {
            iPos = 0;
            sNumber += '.';
        }
        for (iCnt = iPos; iCnt < iDec; iCnt++)
            sNumber += '0';
    }
    return replaceString(sNumber, '.', ',');
}

function SetFocus(oCtl) {
    if (!oCtl)
        return false;
    if (oCtl.type == 'hidden')
        return false;
    try {
        oCtl.focus();
        if (oCtl.type == 'text' || oCtl.type == 'password')
            oCtl.select();
    } catch (e) {
        return false;
    }

    return true;
}

function error(oCtl, text) {
    if ((oCtl) && (oCtl.type != 'hidden')) {
        try {
            oCtl.focus();
            if (oCtl.type == 'text' || oCtl.type == 'password')
                oCtl.select();
        } catch (e) {
            // Skip errors
        }
    }

    // error op het scherm schrijven
    alert(text);

    if ((!document.all) && (oCtl) && (oCtl.type != 'hidden')) {
        try {
            if (oCtl.type == 'text' || oCtl.type == 'password')
                oCtl.value = '';
        } catch (e) {
            // Skip errors
        }
    }

    return true;
}

function GetValue(oCtl) {
    if (!oCtl)
        return false;

    // Juiste methode van bepalen waarde
    var sValue = '';
    switch (oCtl.type) {
        case 'select-one':
            if (oCtl.selectedIndex >= 0) {
                sValue = oCtl.options[oCtl.selectedIndex].value;
                if (sValue == '_XXX_')
                    sValue = '';
            }
            break;
        case 'checkbox':
            if (oCtl.checked)
                sValue = 1;
            else
                sValue = 0;
            break;
        default:
            switch (oCtl.tagName) {
                case "DIV":
                case "LABEL":
                    sValue = oCtl.innerHTML;
                    break;
                default:
                    sValue = oCtl.value;
                    break;
            }
            break;
    }
    return sValue;
}

function SetValue(oCtl, sValue) {
    if (!oCtl || (!sValue && sValue != 0))
        return false;

    // Juiste methode van instellen waarde
    var sType = '';
    var bReturn = false;
    if (oCtl.type)
        sType = oCtl.type;
    else if (oCtl.length)
        sType = oCtl[0].type;
    switch (sType) {
        case 'select-one':
            var bSelected = false;
            for (var iCnt = 0; iCnt < oCtl.options.length; iCnt++)
                if (sValue.toString().toLowerCase() == oCtl.options[iCnt].value.toString().toLowerCase()) {
                oCtl.options[iCnt].selected = true;
                bSelected = true;
                bReturn = true;
                break;
            }
            if ((!bSelected) && (oCtl.options.length > 0))
                oCtl.options[0].selected = true;
            break;
        case 'checkbox':
            if (isNumber(sValue))
                oCtl.checked = (toNumber(sValue) != 0);
            else if (sValue) {
                oCtl.checked = true;
                bReturn = true;
            }
            else
                oCtl.checked = false;
            break;
        case 'radio':
            for (var iCnt = 0; iCnt < oCtl.length; iCnt++)
                if (oCtl[iCnt].value == sValue) {
                oCtl[iCnt].checked = true;
                return true;
            }
            break;
        default:
            switch (oCtl.tagName) {
                case "DIV":
                case "LABEL":
                    oCtl.innerHTML = sValue;
                    break;
                case "SELECT":
                    var options = oCtl.getElementsByTagName("OPTION");
                    for (var i = 0; i < options.length; i++) {
                        var option = options[i];
                        if (option.value == sValue) {
                            option.selected = true;
                        }
                    }
                    break;
                default:
                    // File input kan niet altijd gezet worden.
                    // Daarom try catch
                    try {
                        oCtl.value = sValue;
                    }
                    catch (e) { }
                    break;
            }
    }
    return bReturn;
}

function GetTable(sTable) {
    var oTable = $get(sTable);
    
    // # (onderstaand) Firefox/ie compatible bugfix 
    if (oTable.getElementsByTagName("TBODY") && oTable.getElementsByTagName("TBODY")[0])
        oTable = oTable.getElementsByTagName("TBODY")[0];
    
    return oTable;
}

function GetInnerText(oCtl) {
    //var regExp=/<\S[^>]*>/g;
    var regExp = /<\/?[^>]+>/gi;
    var sTxt = '';
    if (oCtl.innerText) {
        sTxt = oCtl.innerText;
    } else {
        var sHTML = oCtl.innerHTML;
        sHTML = replaceString(sHTML, '<br>', '\n');
        sTxt = sHTML.replace(regExp, "");
    }
    return sTxt;
}

function CheckLen(oCtl, iMaxLen) {
    var sTxt = GetValue(oCtl);
    if (sTxt.length > iMaxLen)
        oCtl.value = sTxt.substring(0, iMaxLen);
    return true;
}

function InitCap(sTxt) {
    if (sTxt.length > 0)
        sTxt = sTxt.substr(0, 1).toUpperCase() + sTxt.substr(1).toLowerCase();
    else
        sTxt = '';
    return sTxt;
}

function trim(sTxt, sTrimChar) {
    if (!sTrimChar)
        sTrimChar = ' ';
    while (sTxt.length > 0 && sTxt.substr(0, 1) == sTrimChar)
        sTxt = sTxt.substr(1);
    while (sTxt.length > 0 && sTxt.substr(sTxt.length - 1, 1) == sTrimChar)
        sTxt = sTxt.substr(0, sTxt.length - 1);
    return sTxt;
}

function round(iNumber, iDec, bRoundUp) {
    if (bRoundUp) {
        if (iDec > 0)
            return Math.ceil(iNumber * Math.pow(10, iDec)) / Math.pow(10, iDec);
        else
            return Math.ceil(iNumber);
    } else {
        if (iDec > 0)
            return Math.round(iNumber * Math.pow(10, iDec)) / Math.pow(10, iDec);
        else
            return Math.round(iNumber);
    }
}

function replaceString(szString, szFind, szReplace) {
    var iMatched, fSubstring, sSubstring;
    iMatched = 0;
    szString = szString.toString();
    szFind = szFind.toLowerCase();
    while (iMatched >= 0) {
        iMatched = szString.toLowerCase().indexOf(szFind, iMatched);
        if (iMatched < 0) {
            // Special check on carriage return+linefeed
            if (szFind == '\n')
                szString = replaceString(szString, '\r', '');
            return szString;
        }
        fSubstring = szString.substring(0, iMatched);
        sSubstring = szString.substring(iMatched + szFind.length, szString.length);
        szString = fSubstring + szReplace + sSubstring;
        iMatched += szFind.length + 1;
    }
    return szString;
}

function ListOptionRemove(oCtl, iIndex) {
    if (VWABrowserInfo.IsIE)
        oCtl.options.remove(iIndex);
    else
        oCtl.options[iIndex] = null;
}
function ListOptionAdd(oCtl, oOption) {
    if (VWABrowserInfo.IsIE)
        oCtl.options.add(oOption, oCtl.options.length);
    else
        oCtl.options[oCtl.options.length] = oOption;
    delete (oOption);
}


//***********************************************************************
// Knoppen onMouseOver kleurinstelling
function ButtonHigh(oBut) {
    oBut.className = 'butHigh';
}
function ButtonNorm(oBut) {
    oBut.className = 'butNorm';
}

// Knoppen en-/disable
function ButtonEnable(sButton) {
    var oBut = document.getElementById(sButton);
    oBut.disabled = false;
    oBut.className = 'butNorm';
}
function ButtonDisable(sButton) {
    var oBut = document.getElementById(sButton);
    oBut.disabled = true;
    oBut.className = 'butDisable';
}

//***********************************************************************
// Screen size
var iWindowWidth = 0, iWindowHeight = 0;
function GetScreenSize() {
    // Breedte van venster/scherm
    if (window.innerWidth)
        iWindowWidth = window.innerWidth;
    else if (document.body.offsetWidth)
        iWindowWidth = document.body.offsetWidth;
    else if (window.screen) {
        try {
            iWindowWidth = window.screen.width;
        } catch (e) { }
    }

    // Hoogte van venster/scherm
    if (window.innerHeight)
        iWindowHeight = window.innerHeight;
    else if (document.body.offsetHeight)
        iWindowHeight = document.body.offsetHeight;
    else if (window.screen) {
        try {
            iWindowHeight = window.screen.height;
        } catch (e) { }
    }
}
function GetScrollPosition() {
    var x = 0;
    var y = 0;
    if (typeof (window.pageYOffset) == 'number') {
        x = window.pageXOffset;
        y = window.pageYOffset;
    }
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    }
    var position = { 'x': x, 'y': y }
    return position;
}

var Countries = null;

var CountrySettings = {
    'CountryControlId': '',
    'StateControlId': '',
    'Street2Id': '',
    'iCountryId': '',
    'bRequesting': false
}

var CacheCountries = [];


function SetCountryID(sCountryControlId, sStateControlId, sStreet2Id, iCountryId) {
    // Parameters controleren
    if ((typeof (sStateControlId) == 'undefined') || (sStateControlId == null))
        sStateControlId = '';
    if ((typeof (sStreet2Id) == 'undefined') || (sStreet2Id == null))
        sStreet2Id = '';

    if (CountrySettings.bRequesting) {
        setTimeout('SetCountryID("' + sCountryControlId + '","' + sStateControlId + '","' + sStreet2Id + '",' + iCountryId + ')', 200);
        return false;
    }

    // Ingesteld land
    if ((typeof (iCountryId) == 'undefined') || (iCountryId == null))
        iCountryId = toNumber(GetValue($get(sCountryControlId)))

    CountrySettings.CountryControlId = sCountryControlId;
    CountrySettings.StateControlId = sStateControlId;
    CountrySettings.Street2Id = sStreet2Id;
    CountrySettings.iCountryId = iCountryId;

    // Eerst 'Cache' proberen
    if (CacheCountries[iCountryId] == null) {
        if (!CountrySettings.bRequesting) {
            VWA.FrontService.GetCountryState(iCountryId, setCountrySucces, setCountryFailure);
            CountrySettings.bRequesting = true;
        }
    } else
        setCountrySucces(CacheCountries[iCountryId], true)
}

function setCountrySucces(oCountry, bNotAdd) {
    CountrySettings.bRequesting = false;
    if ((typeof (oCountry) != 'undefined') && (oCountry != null)) {
        if ((CountrySettings.Street2Id.length > 0) && ($get(CountrySettings.Street2Id)) && ($get(CountrySettings.Street2Id).style))
            $get(CountrySettings.Street2Id).style.display = (oCountry.ShowStreetNr2) ? '' : 'none';

        // Lijst wissen
        if ((CountrySettings.StateControlId.length > 0) && ($get(CountrySettings.StateControlId))) {
            var oStateID = $get(CountrySettings.StateControlId);
            var iStateID = GetValue(oStateID);
            while (oStateID.options.length > 0)
                ListOptionRemove(oStateID, 0);
            for (var iState = 0; iState < oCountry.States.length; iState++) {
                // Staat toevoegen
                var oState = oCountry.States[iState];
                var oItem = new Option();
                oItem.value = oState.StateID;
                oItem.text = oState.Name;
                oItem.selected = (oState.StateID == iStateID);
                ListOptionAdd(oStateID, oItem);
            }
        }
        if (!bNotAdd)
            CacheCountries[oCountry.CountryID] = oCountry;

    } else {
        if ((CountrySettings.Street2Id.length > 0) && ($get(CountrySettings.Street2Id)) && ($get(CountrySettings.Street2Id).style))
            $get(CountrySettings.Street2Id).style.display = 'none';

        // Lijst wissen
        if ((CountrySettings.StateControlId.length > 0) && ($get(CountrySettings.StateControlId))) {
            var oStateID = $get(CountrySettings.StateControlId);
            while (oStateID.options.length > 0)
                ListOptionRemove(oStateID, 0);
        }
    }

}

function setCountryFailure(sErr) {
    CountrySettings.bRequesting = false;
    alert(sErr._ResultText);
}

//***********************************************************************
// Hourglass show/hide functions
var timerHourglass = null

function HourGlassShow(sTxt) {
    // Parameters controleren
    if ((typeof (sTxt) == 'undefined') || (sTxt == null))
        sTxt = 'Moment geduld a.u.b.';

    // Tekst bij zandloper
    if (sTxt.length > 0)
        $get('tdHourglass').innerHTML = sTxt;

    //document.getElementById('tblHglassTxt').style.display='none';

    // Stel display in van zandloper
    var oHG = $get('tableHourglass');
    oHG.style.display = '';

    oHG.style.height = (bUpdate ? iWindowHeight : (iWindowHeight - 5 > iDivsetHeight ? iWindowHeight - 5 : iDivsetHeight)) + 'px';

    // Tekst evt. iets vertraagt aanroepen, om te veel geflikker van de melding te voorkomen
    timerHourglass = setTimeout(HourGlassShowT, tHourGlassShow);
    // HourGlassShowT en tHourGlassShow staan in Page.js en Update.js, omdat voorkant en achterkant verschillend kunnen zijn.
    oHG = null;
}


function HourGlassHide() {
    if ($get('tblHglassTxt'))
        $get('tblHglassTxt').style.display = 'none';

    $get('tableHourglass').style.display = 'none';

    if (timerHourglass)
        clearTimeout(timerHourglass);
    timerHourglass = null;
}


function HourGlassShowT() {
    var tbl = $get('tblHglassTxt');
    if (tbl) {
        tbl.style.position = 'absolute';
        var boundsDS = GetScrollPosition();
        tbl.style.display = 'block';
        height = parseInt(tbl.style.height + 0, 10);
        width = parseInt(tbl.style.width + 0, 10);
        tbl.style.top = ((iWindowHeight / 2 - (height / 2)) + boundsDS.y) + 'px';
        tbl.style.left = ((iWindowWidth / 2 - (width / 2)) + boundsDS.x) + 'px';
    }
}

function getOrCreateHidden(sName) {
    var oHid = theForm[sName];
    if (!oHid) {
        oHid = document.createElement("INPUT")
        oHid.type = "hidden";
        oHid.name = sName;
        theForm.appendChild(oHid);
    }
    return oHid;
}

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded()