function addWkColumn(tblId, wkStart) {
    var tbl = document.getElementById(tblId);
    var tblBodyObj = tbl.tBodies[0];
    for (var i = 0; i < tblBodyObj.rows.length; i++) {
        // Month Header
        if (i == 0) {
            // Add extra colspan column
            tblBodyObj.rows[i].cells[0].colSpan = 8;
        }
        // Week Header
        if (i == 1) {
            // Add week column headline
            var newCell = tblBodyObj.rows[i].insertCell(0);
            newCell.innerHTML = 'wk';
            newCell.style.fontWeight = 'bold';
            newCell.style.verticalAlign = 'bottom';
            newCell.style.backgroundColor = '#DDEEFF';
        }
        // Normal row
        if (i >= 2) {
            // Add the weeknumbers
            var newCell = tblBodyObj.rows[i].insertCell(0);
            var displayText = wkStart;
            if (displayText == 53) { displayText = '53/1'; }
            else if (displayText > 53) { displayText = displayText - 52 }
            newCell.innerHTML = displayText;
            wkStart += 1;
            newCell.style.backgroundColor = '#DDEEFF';
        }
    }
}

function TabNext(obj, event, len, next_field) {
    if (event == "down") {
        field_length = obj.value.length;
    }
    else if (event == "up") {
        if (obj.value.length != field_length) {
            field_length = obj.value.length;
            if (field_length == len) {
                var field = document.getElementById(next_field);
                if (field != null) { field.focus(); }
            }
        }
    }
}


// enable/disable validators and ajax callouts clientside
function EnableValidator(cbClientId, rfvFirstNameClientId, cvDateClientId, revEmailClientId, vceBehaviourID1, vceBehaviourID2, vceBehaviourID3) {
    var checked = document.getElementById(cbClientId).checked;
    ValidatorEnable(document.getElementById(rfvFirstNameClientId), checked);
    ValidatorEnable(document.getElementById(cvDateClientId), checked);
    ValidatorEnable(document.getElementById(revEmailClientId), checked);
    if (!checked) {
        var i;
        var arr = new Array(vceBehaviourID1, vceBehaviourID2, vceBehaviourID3)
        for (i in arr) {
            var validator = $find(arr[i]);
            if (validator != null) {
                validator.hide();
                if (validator._elementToValidate != null) { Sys.UI.DomElement.removeCssClass(validator._elementToValidate, validator._highlightCssClass); }
            }
        }
    }
}

//select all checkbox in the first column of the gridview(gridid)
//by the checked value of the given checkbox id (id)
function SelectAll(id, gridId) {
    var grid = document.getElementById(gridId);
    var cell;
    if (grid.rows.length > 0) {
        for (i = 1; i < grid.rows.length; i++) {
            cell = grid.rows[i].cells[0];
            for (j = 0; j < cell.childNodes.length; j++) {
                if (cell.childNodes[j].type == "checkbox") {
                    cell.childNodes[j].checked = document.getElementById(id).checked;
                }
            }
        }
    }
}




function disableCopyPaste() {
    // current pressed key
    var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
    if (event.ctrlKey && (pressedKey == "c" || pressedKey == "v")) {
        // disable key press porcessing
        alert('Doordat er vaan fouten gemaakt worden met het invullen van een e-mailadres is het kopieren/plakken niet toegestaan voor dit veld');
        event.returnValue = false;
    }
}


//check session timeout
var sTO = 0;
function startClock(iTimeOutAlert) {
    iTimeOutAlert = (iTimeOutAlert / 100) * 60;
    clearTimeout(sTO); dWatch = 0;
    dStarted = new Date();
    updateClock(iTimeOutAlert);
}
function updateClock(iTimeOutAlert) {
    dNow = new Date(); dWatch = dNow.getTime() - dStarted.getTime();
    dClock = Math.round(dWatch / 1000);
    if (dClock >= iTimeOutAlert) { alert("De sessie is verlopen.\nU gaat terug naar de startpagina..."); window.location.href = "Default.aspx"; }
    else sTO = setTimeout("updateClock(" + iTimeOutAlert + ");", 250);

}


//Check visibility of element
function checkVisibility() {
    return document.getElementById('ctl00$ContentPlaceHolder1$tbBirthDate').value == ''
}



//BEGIN .NET custom validator(nk)
function cValidator(ctr, val) {
    this.ctr = ctr;
    this.val = val;
    this.Error = true;

    this.Validate = function() {
        switch (this.val) {
            case '':
                break;
            case 'on': //checkboxlist
                this.chkCheckBoxList();
                break;
            default:
                //check email
                if (this.ctr.name != null) {
                    if (this.ctr.name.toUpperCase().indexOf('EMAIL') != -1) {
                        this.Error = !this.ValidateEmail(this.val);
                    } else this.Error = false;
                } else this.Error = false;
                break;
        }
        this.chkRadio();
        this.setStyle();
        return !this.Error;
    }
    this.setStyle = function(_ctr) {
        var ctr = _ctr ? _ctr : this.ctr;
        if (this.Error) ctr.className += ctr.className.indexOf(/ failure/i) == -1 ? ' failure' : ''
        else ctr.className = ctr.className.replace(/ failure/i, '');
    }
    this.chkRadio = function() {
        var c = this.ctr.getElementsByTagName('label');
        if (c.length != null) {
            for (var i = 0; i < c.length; i++) this.setStyle(c[i]);
        };
    }
    this.chkCheckBoxList = function() {
        var c = this.ctr.getElementsByTagName('input');
        if (c.length != null) {
            for (var i = 0; i < c.length; i++) {
                if (c[i].checked) { this.Error = false; break; }
            }
        }
    }
    this.ValidateEmail = function(str) {
        var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
        return filter.test(str);
    }
}

function chkEmpty(sender, args) {
    var ctr = document.getElementById(sender.controltovalidate);
    var validator = new cValidator(ctr, args.Value);
    args.IsValid = validator.Validate();
}


//END .NET custom validator(nk)



//mouse progress panel
function getMouse(e) {
    var x_offset = 8; var y_offset = -12;
    var d = document.getElementById('divMouseProgress');
    if (document.layers) {
        d.top = e.pageY + y_offset;
        d.left = e.pageX + x_offset;
    } else {
        var bd = document.documentElement ? document.documentElement : document.body;
        e = e || window.event || {};
        d.style.top = (y_offset + e.clientY + bd.scrollTop) + 'px';
        d.style.left = (x_offset + e.clientX + bd.scrollLeft) + 'px';
    }
}



//end progresspanel

function setFooter() {
    var h = getHeight();
    var m = document.getElementById('main');
    var f = document.getElementById('contentfooter');
    if (h - f.offsetHeight > m.offsetHeight) {
        f.style.position = 'absolute';
        f.style.left = m.offsetLeft + 'px';
        f.style.top = h - f.offsetHeight + 'px';
    } else {
        f.style.position = 'relative';
        f.style.left = '0px';
        f.style.top = '0px';
    }


}
function getHeight() {
    if (typeof (window.innerHeight) == 'number') {
        return window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        return document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        return document.body.clientHeight;
    } else return 0;
}
function getElementTop(elem) {
    var yPos = elem.offsetTop;
    var tempEl = elem.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}
function getElementLeft(elem) {
    var xPos = elem.offsetLeft;
    var tempEl = elem.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

function cd() {
    return confirm("Weet je zeker dat je dit wilt verwijderen?");
}

function doLogin(returnURL) {
    document.SecureLoginForm.returnurl.value = returnURL;
    document.SecureLoginForm.username.value = document.Form1.f_username.value;
    document.SecureLoginForm.password.value = document.Form1.f_password.value;
    document.SecureLoginForm.submit();
}

function openImage(url1) {
    var wnd = window.open(url1, '', 'width=600,height=800,top=' + (screen.height / 2 - 115) + ', left=' + (screen.width / 2 - 60) + ', scrollbars=yes,resizable=yes,status=yes,toolbar=no,menubar=no');
}

function openWindow(sUrl, sHeight, sWidth) {
    var load = window.open(sUrl, 'info', 'scrollbars=yes,menubar=no,height=' + sHeight + ',width=' + sWidth + ',resizable=yes,toolbar=no,location=no,status=no');
    load.focus()
}
function openDroomboeket(sUrl, sHeight, sWidth) {
    var load = window.open(sUrl, 'info', 'scrollbars=yes,menubar=no,height=' + sHeight + ',width=' + sWidth + ',resizable=no,toolbar=no,location=no,status=no');
    load.focus()
}

function openSendACard(sUrl, sHeight, sWidth) {
    var load = window.open(sUrl, 'info', 'scrollbars=no,menubar=no,height=' + sHeight + ',width=' + sWidth + ',resizable=no,toolbar=no,location=no,status=no');
    load.focus()
}

function countChars() {
    var sMaxLength = 120; // max length for the textbox
    //var declaration **changed for Firefox**
    var txtObj = document.getElementById('tbText');
    var startText = document.getElementById('startText');
    var endText = document.getElementById('endText');
    var myCounter = document.getElementById('myCounter');
    var cbxSms = document.getElementById('chBxSms');
    var btnSave = document.getElementById('ButtonSave');
    // check if Sms is checked.
    if (cbxSms.checked == true) {
        // show restrictions for textbox and the counter
        CounterContaner.style.visibility = "visible"; // for IE
        CounterContaner.style.display = "block"; // for Firefox...
        startText.style.visibility = "visible";
        startText.style.display = "block";
        //myCounter.style.visibility = "visible";
        //myCounter.style.display = "block";
        //endText.style.visibility = "visible";
        //endText.style.display = "block";
        // if the lenght of the text is longer then the maxlength: display warning
        if (txtObj.value.length > sMaxLength) {
            txtObj.className = "failure"; // change text in textarea to red
            startText.firstChild.nodeValue = "Je bericht is nu ";
            endText.firstChild.nodeValue = " tekens te lang voor een SMS reminder. De tekst zal ingekort worden.";
            if (myCounter) myCounter.firstChild.nodeValue = txtObj.value.length - sMaxLength; // update counter
            btnSave.style.visibility = "hidden";
            btnSave.style.display = "none";
        }
        else {
            // if text is not longer then the maxlength: show normal message
            txtObj.className = "normal";
            startText.firstChild.nodeValue = "Voor de SMS heb je nog maar ";
            endText.firstChild.nodeValue = " tekens over.";
            if (myCounter) myCounter.firstChild.nodeValue = sMaxLength - txtObj.value.length; // update counter
            btnSave.style.visibility = "visible";
            btnSave.style.display = "block";
        }
    }
    else {
        // if the sms in not checked hide the messages and warnings
        CounterContaner.style.visibility = "hidden";
        CounterContaner.style.display = "none";
        //startText.style.visibility = "hidden";
        //startText.style.display = "none";
        //myCounter.style.visibility = "hidden";
        //myCounter.style.display = "none";
        //endText.style.visibility = "hidden";
        //endText.style.display = "none";
        btnSave.style.visibility = "visible";
        btnSave.style.display = "block";
        txtObj.className = "normal";
    }
}

//for testing
function countChars111111111() {
    var sMaxLength = 120; //max length for the textbox
    var txtObj = document.getElementById('tbText');
    var startText = document.getElementById('startText');
    var cbxSms = document.getElementById('chBxSms');
    //check if Sms is checked.
    if (cbxSms.checked == true) {
        //if the lenght of the text is longer then the maxlength: display warning
        if (txtObj.value.length > sMaxLength) {
            txtObj.className = "failure";
            startText.style.visibility = "visible";
            startText.style.display = "block";
            startText.innerText = "Characters count is more than 120!";
        }
        else {
            //if text is not longer then the maxlength: show normal message
            txtObj.className = "standard";
            startText.innerText = "";
            startText.style.visibility = "hidden";
            startText.style.display = "none";
        }
    }
    else {
        //if the sms in not checked hide the messages and warnings
        txtObj.className = "standard";
        startText.style.visibility = "hidden";
        startText.style.display = "none";
    }
}

// function save scroll position to saved value
function saveScroll(what, to) {
    //alert(window.screenTop);
    alert(document.getElementById(what).clientTop);
    //alert(document.all[what].scrollHeight);
    //document.all[to].value = document.all[what].scrollTop;
    //document.getElementById(to).value = document.getElementById(what).scrollTop;
    //var whatObj = document.getElementById(what);
    //var toObj = document.getElementById(to);
    //if (whatObj != null) && (toObj != null) {
    //	toObj.value = whatObj.scrollTop;
    //}
    return false;
}

// function moves scroll position to saved value
function moveScroll(what, from) {
    //var whatObj = document.getElementById(what);
    //var fromObj = document.getElementById(from);
    //if (whatObj != null) && (fromObj != null) {
    //whatObj.focus();
    //	whatObj.scrollTop = fromObj.value;
    //}
}

function printWindow() {
    bV = parseInt(navigator.appVersion)
    if (bV >= 4) window.print()
}

function clearText(thefield) {
    if (thefield.defaultValue == thefield.value)
        thefield.value = ""
} 
