/////////ArticlePackage - Generic functions//////////////////////////////////////////////
function handleStartpackageCheckbox(tr) {
    var checked;

    if (tr.tagName == 'TD') {
        tr = tr.parentNode;
    }

    if (tr.cells[0].childNodes[0].tagName == "SPAN") {
        checked = tr.cells[0].childNodes[0].childNodes[0].checked;
    } else {
        checked = tr.cells[0].childNodes[0].checked;
    }

    if (checked) {
        tr.cells[4].childNodes[0].disabled = false;
        tr.cells[4].childNodes[0].focus();
    } else {
        tr.cells[4].childNodes[0].value = '0';
        tr.cells[4].childNodes[0].disabled = true;
    }

}

function handleArticleCheckbox(tr) {

    if (tr.cells[1].childNodes[0].checked) {
        tr.cells[0].childNodes[0].disabled = false;
        //tr.cells[0].childNodes[0].checked =false;
        //tr.cells[4].childNodes[0].disabled=false;
        //tr.cells[4].childNodes[0].focus();
    } else {
        tr.cells[0].childNodes[0].checked = false;
        tr.cells[0].childNodes[0].disabled = true;
        tr.cells[4].childNodes[0].value = '0';
        tr.cells[4].childNodes[0].disabled = true;
    }
}

function ResetAmount(tr) {
    RecalculateBudget();
}

function RecalculateBudget() {

    var tblContents = document.getElementById('tblStartPackage');
    var tr; 														//tablerow
    var intArticles = 0;
    var intCredits = 0;
    var strArticleID = '';
    /*
    for (var i = 0; i < tblContents.tBodies[0].rows.length; i++) {
    tr = tblContents.tBodies[0].rows[i];
    if (tr.cells[3]) {			//If cell 2 exists, this row is not a subhead
    //var chkArticle = tr.cells[0].childNodes[0];
    var txtAmount = tr.cells[3].childNodes[0];
    var credits = parseInt(tr.cells[4].childNodes[0].innerHTML);
    if (isNaN(txtAmount.value)) txtAmount.value = 0;
    if (txtAmount.value > 0) {
    intArticles += parseInt(txtAmount.value)
    intCredits += (parseInt(txtAmount.value) * credits)
    }
    }
    }
    */
    var lblAmountArticles = document.getElementById('articlepackage_lblAmountArticles');
    var lblPackageCredits = document.getElementById('articlepackage_lblPackageCredits');

    lblAmountArticles.innerHTML = intArticles;
    lblPackageCredits.innerHTML = intCredits;
}

/////////Menu - Generic functions//////////////////////////////////////////////
function setLanguage(strLanguageType) {
    var txtLanguage = document.getElementById('menu:txtLanguage');
    var txtMenuChoosen = document.getElementById('menu:txtMenuChoosen');

    txtLanguage.value = strLanguageType;
    txtMenuChoosen.value = "1";

    if (typeof (Page_ClientValidate) == 'function')
        Page_ClientValidate();

    Page_IsValid = true;

    window.document.all['Menu_butMenu'].click();
}

function setAction(strReference, strPermission) {
    var txtAction = document.getElementById('menu:txtAction');
    var txtPermission = document.getElementById('menu:txtPermission');
    var txtMenuChoosen = document.getElementById('menu:txtMenuChoosen');

    txtAction.value = strReference;
    txtPermission.value = strPermission;
    txtMenuChoosen.value = "1";

    if (typeof (Page_ClientValidate) == 'function')
        Page_ClientValidate();

    Page_IsValid = true;
    window.document.all['Menu_butMenu'].click();
}

function selectArticleDetail(objLabel) {

    var strArticleID = '';

    if (objLabel.getAttribute('articleID') != null)
        strArticleID = objLabel.getAttribute('articleID');

    executePopUp('ArticleDetail', '', strArticleID, '');
}

/////////Application - Generic functions///////////////////////////////////////
var localReference, localPermission, localID, localargumentName;

function ErrorCallback(result, eventArgs) {
    //Session invalid?
    location.href = 'index.aspx';
}

function SucceededCallback(result, eventArgs) {
    if (result == true) {
        var objDate = new Date();
        var strTimeStamp = objDate.toTimeString();
        var strProperties = '';
        var strPopup = 'PopUp.aspx?ID=' + localID + '&Reference=' + localReference + '&Permission=' + localPermission + '&Time=' + strTimeStamp;
        var args = new Array();

        //if(Reference == 'OrderPrintable')
        //	strProperties = 'help:no;unadorned:yes;status=no;scroll:no;caption:no;resizable:yes;edge:sunken;center:yes;dialogWidth:700px;dialogHeight:600px;left:10px;top:10px;Width=700px;Height=600px';

        if (localargumentName != '')
            args = getArgs(localargumentName);

        //        if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
        //            strProperties = 'help:no;unadorned:yes;status=no;scroll:no;caption:no;resizable:yes;edge:sunken;center:yes;dialogWidth:550px;dialogHeight:380px;';

        //            if (localReference == 'OrderPrintable' || localReference == 'RetourOrderPrintable' || localReference == 'ArticleDetail')
        //                strProperties = 'help:no;unadorned:yes;status=no;scroll:no;caption:no;resizable:yes;edge:sunken;center:yes;dialogWidth:800px;dialogHeight:600px;left:10px;top:10px;Width=700px;Height=600px';

        //            window.showModalDialog(strPopup, args, strProperties);
        //        }
        //        else {
        //            strProperties = 'height=450, width=450, resizable=yes, status=yes, toolbar=no, menubar=no, location=no';

        //        if (localReference == 'OrderPrintable' || localReference == 'RetourOrderPrintable' || localReference == 'ArticleDetail')
        //            strProperties = 'height=600, width=800, resizable=yes, status=yes, toolbar=no, menubar=no, location=no';

        //window.open(strPopup, args, strProperties);
        strPopup += "&fancybox=1";
        $.fancybox({
            type: 'iframe',
            href: strPopup,
            closeBtn: true,
            width: '70%',
            height: '70%',
            wrapCSS: 'fancybox-buikema',
            padding: 0
        });
        // }
    } else {
        location.href = 'index.aspx';
    }
}

function executePopUp(Reference, Permission, idValue, argumentName) {

    localReference = Reference;
    localPermission = Permission;
    localID = idValue;
    localargumentName = argumentName

    WWWWebService.CheckSession(SucceededCallback, ErrorCallback);
}

function executeNewWindow(ReferenceURL) {
    window.open(ReferenceURL, '_blank', 'height=600, width=700, resizable=yes, status=yes, toolbar=no, menubar=no, location=no', true);
}

function executeNewWindowWebsite() {
    window.open('http://www.buikema.com');
}

function cancelRefresh() {
    // keycode for F5 function
    //if (window.event && window.event.keyCode == 116) {
    //	window.event.keyCode = 8;
    //}
    // keycode for backspace
    //if (window.event && window.event.keyCode == 8) {
    //	// try to cancel the backspace
    //	window.event.cancelBubble = true;
    //	window.event.returnValue = false;
    //	return false;
    //}
}

function RorC() {

    var top = self.screenTop;
    //alert('top=' + top);
    //top > 9000 -->> means window is closed by user, only works in <= IE6!!
    //TODO:new trigger for close window
    if (top > 9000) {
        var butSignOut = document.getElementById('Menu_butSignOut');
        if (butSignOut != 'null') {
            butSignOut.click();
        }
    }
}

function windowClosed() {
    //window.open('logoff.aspx', '_blank','height=100, width=200');
    if (document.all) {
        RorC()
    }
}

/***********************************************
* Auto Maximize Window Script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for this script and 100's more.
***********************************************/
/*function resizeMe() {
debugger;
if (document.all) {
if (top.window.screen.width != screen.availWidth &&
top.window.screen.height != screen.availHeigth) {
top.window.moveTo(0, 0);
top.window.resizeTo(screen.availWidth, screen.availHeight);
}

}
else if (document.layers || document.getElementById) {
top.window.moveTo(0, 0);
if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth) {
top.window.outerHeight = screen.availHeight;
top.window.outerWidth = screen.availWidth;
}
}
var windowWithControl = document.getElementById('hWidth');

if (windowWithControl != null)
windowWithControl.value = window.screen.availWidth;
}*/

//Disable right mouse click Script
///////////////////////////////////
/*function clickIE4() {
if (event.button == 2) {
//alert(message);
return false;
}
}

function clickNS4(e) {
if (document.layers || document.getElementById && !document.all) {
if (e.which == 2 || e.which == 3)
return false;
}
}*/
/*
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
*/

var to;
var blnVisible = false;

function fnConfigurationPanel() {
    var panel = document.getElementById('pnlConfiguration');

    if (panel != null) {
        if (panel.style.display == 'block')
            panel.style.display = 'none';
        else
            panel.style.display = 'block';

        blnVisible = !blnVisible;
        //if (window.event) window.event.returnValue=0;
    }
}

/*document.oncontextmenu = new Function("return false")*/
document.onkeydown = cancelRefresh;

function setOpacity(id, level) {
    var element = document.getElementById(id);
    if (element) {
        element.style.display = 'inline';
        element.style.zoom = 1;
        element.style.opacity = level;
        element.style.MozOpacity = level;
        element.style.KhtmlOpacity = level;
        element.style.filter = "alpha(opacity=" + (level * 100) + ");";
    }
}

function fadeIn(id, steps, duration, interval, fadeOutSteps, fadeOutDuration) {
    var fadeInComplete;
    for (i = 0; i <= 1; i += (1 / steps)) {
        setTimeout("setOpacity('" + id + "', " + i + ")", i * duration);
        fadeInComplete = i * duration;
    }
    //set the timeout to start after the fade in time and the interval to display the 
    //message on the screen have both completed            
    setTimeout("fadeOut('" + id + "', " + fadeOutSteps + ", " + fadeOutDuration + ")", fadeInComplete + interval);
}

function fadeOut(id, steps, duration) {
    var fadeOutComplete;
    for (i = 0; i <= 1; i += (1 / steps)) {
        setTimeout("setOpacity('" + id + "', " + (1 - i) + ")", i * duration);
        fadeOutComplete = i * duration;
    }
    //completely hide the displayed message after the fade effect is complete              
    setTimeout("hide('" + id + "')", fadeOutComplete);
}

function hide(id) {
    if (document.getElementById(id)) {
        document.getElementById(id).style.display = 'none';
    }
}

// function to possibly override keypress
trapfunction = function (event) {
    var keynum;
    var type;
    var readOnly;
    if (window.event) // eg. IE
    {
        keynum = window.event.keyCode;
        type = document.activeElement.getAttribute("type")
    }
    else if (event.which) // eg. Firefox
    {
        keynum = event.which;
        type = event.target.getAttribute("type");
    }

    // backspace has code 8
    if (keynum == 8) {
        if (((type === "text") | (type === "textarea") | (type === "password"))) {
            return true;
        }
        else {
            //location.reload(true);
            return false;
        }
    }
    return true;
}
function showKey(event) {
    //if (window.event) {e=event;} //IE
    if (event.keyCode == 13 || event.keyCode == 8) {
        if (document.getElementById('Menu_SearchUser_butSearch') != null) {
            if (document.getElementById('Menu_SearchUser_butSearch').hasFocus == 1) {
                event.returnValue = false;
                event.cancel = true;

                document.getElementById('Menu_SearchUser_butSearch').click();
            }
        }
    }
}

function AttachHandlers(sender, args) {
    $("img[@src='images/poppetje.gif']").toggle(
                                        function () {
                                            $("#pnlConfiguration").fadeIn();
                                        },
                                        function () {
                                            $("#pnlConfiguration").fadeOut();
                                        }
                        );

    $("#pnlConfiguration").hover(
                                        function () { }
                                        ,
                                        function () {
                                            $("img[@src='images/poppetje.gif']").click();
                                        }
                        );
    //$("#pnlConfiguration").css("left",$("img[@src='images/poppetje.gif']").offset().left);
    //$("#pnlConfiguration").css("top",$("img[@src='images/poppetje.gif']").offset().top+25);

    $("#pnlConfiguration input").change(
                                        function () {
                                            var a = "__doPostBack('" + $(this).attr("name") + "', '')"
                                            setTimeout(a, 0);
                                        }
                        );
}

// bepalen hoogte content, waar nodig een scrollbar
function resizeScrollarea() {
    $("html,body").css("overflow", "hidden");

    var w = $(window).width() - ($(".fContent").outerWidth());
    var h = $(window).height() - (($("#body-container").outerHeight() - $("#body-container").height()) + $("#pre-header").outerHeight() + $("#header").outerHeight() + ($("#content").outerHeight() - $("#content").height()) + $(".page-heading").outerHeight() + $("#footer").outerHeight() + $(".aContent").outerHeight() + $(".cFooter").outerHeight());
    var total_h = $(".fContent").height();

    if (total_h > h && $('.ie7 #scrollable').length == 0) {
        $('.ie7 .fContent>*').wrapAll('<div id="scrollable" />');
    }

    $(".fContent").height(h + "px").css("overflow", "auto");
    
}
function refreshScrollArea(func, ajax) {
    if (!($.browser.msie && $.browser.version == "6.0") && ajax) {
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(func);
    }
    if (!ajax) {
        resizeScrollarea();
    }
}
