//---- window functions ----//
// "private" method = you should not have to call this function directly, use one of the other popXXXXX functions, they chain up to this
function popNewWin(URL, winName, winAttribs) {
        winAttribs=winAttribs+"location=0,menubar=0,status=0,titlebar=0,toolbar=0";
        var newWindow=window.open(URL, winName, winAttribs);
        if (newWindow.opener == null) newWindow.opener = mainWindow;
        return newWindow;
}

// For all the popXXXXX funtions:
// URL is the page you would like to be shown in the popped-up window
// winName is a ONE word name for that window
// wwidth is a number for the width of the window
// wheight is a number representing the window's height
function popFlexible(URL, winName, wwidth, wheight) {
        flexibleWin=popNewWin(URL, winName, "width="+wwidth+",height="+wheight+",resizable=1,scrollbars=1,");
        flexibleWin.focus();
}
function popResizable(URL, winName, wwidth, wheight) {
        resizableWin=popNewWin(URL, winName, "width="+wwidth+",height="+wheight+",resizable=1,scrollbars=0,");
        resizableWin.focus();
}
function popScrollable(URL, winName, wwidth, wheight) {
        scrollableWin=popNewWin(URL, winName, "width="+wwidth+",height="+wheight+",resizable=0,scrollbars=1,");
        scrollableWin.focus();
}
function popRestricted(URL, winName, wwidth, wheight) {
        restrictedWin=popNewWin(URL, winName, "width="+wwidth+",height="+wheight+",resizable=0,scrollbars=0,");
        restrictedWin.focus();
}

//---- div toggling, tokenizing, & related print functions ----//
function printPage() {
    //some ABSOLUTE lameness here.....
    var foo = document.getElementById("mediaLeftNaviTD");
    if(foo) {
        if(foo.width == 5) { 
            foo.width=131;
            toggleGroupOn('mediaTopNavDiv,mediaLeftNaviDiv,mediaLeadersDiv');
        } else {
            foo.width=5;
            toggleGroupOff('mediaTopNavDiv,mediaLeftNaviDiv,mediaLeadersDiv');
        }
        return; //lame
    }

    var foo2 = document.getElementById("leftNaviTD");
    if(foo2) {
        if(foo2.width == 5) { 
            foo2.width=159;
            toggleGroupOn('topAdDiv,leftNaviDiv,networkNaviDiv,rightAdDiv');
        } else {
            foo2.width=5;
            toggleGroupOff('topAdDiv,leftNaviDiv,networkNaviDiv,rightAdDiv');
        }
        return;
    }
}

String.prototype.tokenize = tokenize;
function tokenize() {
    var input             = "";
    var separator         = " ";
    var trim              = "";
    var ignoreEmptyTokens = true;
    try {
        String(this.toLowerCase());
    } catch(e) {
        window.alert("Tokenizer Usage: string myTokens[] = myString.tokenize(string separator, string trim, boolean ignoreEmptyTokens);");
        return;
    }
    if(typeof(this) != "undefined") {
          input = String(this);
    }
    if(typeof(tokenize.arguments[0]) != "undefined") {
        separator = String(tokenize.arguments[0]);
    }
    if(typeof(tokenize.arguments[1]) != "undefined") {
        trim = String(tokenize.arguments[1]);
    }
    if(typeof(tokenize.arguments[2]) != "undefined") {
        if(!tokenize.arguments[2])
            ignoreEmptyTokens = false;
    }
    var array = input.split(separator);
    if(trim)
        for(var i=0; i<array.length; i++) {
            while(array[i].slice(0, trim.length) == trim)
                array[i] = array[i].slice(trim.length);
            while(array[i].slice(array[i].length-trim.length) == trim)
                array[i] = array[i].slice(0, array[i].length-trim.length);
        }
    var token = new Array();
    if(ignoreEmptyTokens) {
        for(var i=0; i<array.length; i++)
            if(array[i] != "")
                token.push(array[i]);
    } else {
        token = array;
    }
    return token;
}

function toggleSingleLayerOnGroupOff(whichLayer, allLayers) {
    return toggleMultipleOffAndMultipleOn(allLayers, whichLayer);
}
function toggleSingleLayerOffGroupOn(whichLayer, allLayers) {
    return toggleMultipleOffAndMultipleOn(whichLayer, allLayers);
}
function toggleGroupOn(layersOn) {
    return toggleMultipleOffAndMultipleOn(undefined, layersOn);
}
function toggleGroupOff(layersOff) {
    return toggleMultipleOffAndMultipleOn(layersOff, undefined);
}

function toggleMultipleOffAndMultipleOn(layersOff, layersOn) {
    if(layersOff) {
        var tokensOff = layersOff.tokenize(",", "", false);
        // First, turn layers OFF
        for(var i=0; i<tokensOff.length; i++) {
            if (document.getElementById) {
                var poop = document.getElementById(tokensOff[i]).style;
                poop.display = 'none';
            } else if (document.all) {
                var poop = document.all[tokensOff[i]].style;
                poop.display = 'none';
            } else if (document.layers) {
                var poop = document.layers[tokensOff[i]].style;
                poop.display = 'none';
            }
        }
    }

    if(layersOn) {
        var tokensOn  = layersOn.tokenize(",", "", false);
        // Now, ON
        for(var i=0; i<tokensOn.length; i++) {
            if (document.getElementById) {
                // this is the way the standards work
                var style2 = document.getElementById(tokensOn[i]).style;
                style2.display = '';
            } else if (document.all) {
                // this is the way old msie versions work
                var style2 = document.all[tokensOn[i]].style;
                style2.display = '';
            } else if (document.layers) {
                // this is the way nn4 works
                var style2 = document.layers[tokensOn[i]].style;
                style2.display = '';
            }
        }
    }
}

function popPrintWin(URL, winName, wwidth, wheight) {
        var winAttribs="width="+wwidth+",height="+wheight+",resizable=1,scrollbars=1,location=0,menubar=0,status=0,titlebar=0,toolbar=0";
        var newWindow=window.open(URL, winName, winAttribs);
        if (newWindow.opener == null) newWindow.opener = mainWindow;
        newWindow.focus();
        return newWindow;
}

function printThisDotcomPage() {
    if(document.getElementById) {
        var foo2 = document.getElementById("printMeKBL");
        if(foo2) {
            var clonedFullContent = foo2.cloneNode(true);
            clonedFullContent.style.overflow="visible";

    		var newWin = popPrintWin("", "printWin", 725, 500);

            var objectTags = clonedFullContent.getElementsByTagName("p");
            for(var k=0; k<objectTags.length; k++) {
                if(objectTags[k] && objectTags[k].id && (objectTags[k].id=='removeForPrintPage') && objectTags[k].hasChildNodes()) {
                    while(objectTags[k].hasChildNodes()) {
                        objectTags[k].removeChild(objectTags[k].firstChild);
                    }
                } else if(objectTags[k] && objectTags[k].id && (objectTags[k].id=='removeForPrintPage') && (objectTags[k].nextSibling != null)) {
                    while(objectTags[k].nextSibling.hasChildNodes()) {
                        objectTags[k].nextSibling.removeChild(objectTags[k].nextSibling.firstChild);
                    }
                }
            }

            var scriptTags = clonedFullContent.getElementsByTagName("object");
            for(var kk=0; kk<scriptTags.length; kk++) {
                if(scriptTags[kk]) {
                    while(scriptTags[kk].hasChildNodes()) {
                        scriptTags[kk].removeChild(scriptTags[kk].firstChild);
                    }
//                    scriptTags[kk].innerHTML = "";
                }
            }

//    		var newWin = popPrintWin("", "printWin", 800, 500);

            var printWinBody = newWin.document.getElementsByTagName("body")[0];
            if(printWinBody) {
                while(printWinBody.hasChildNodes()) {  //just in case it has elements already
                    printWinBody.removeChild(printWinBody.firstChild);
                }
            }

            var printWinHead = newWin.document.getElementsByTagName("head")[0];
            if(!printWinHead) {
                printWinHead = newWin.document.createElement('head');
                newWin.document.appendChild(printWinHead);
            }

            var printWinTitle = newWin.document.getElementsByTagName("title")[0];
            if(!printWinTitle) {
                printWinTitle = newWin.document.createElement('title');
                printWinTitle.text = "Korfbal League";
                if(printWinHead) {
                    printWinHead.appendChild(printWinTitle);
                }
            }

            printWinBody.style.backgroundColor="#FFFFFF";
            printWinBody.style.margin="10px;10px;10px;10px";

            var printWinPrinterButtonLink = newWin.document.createElement('a');
            var printWinPrinterButtonImage = newWin.document.createElement('img');
            printWinPrinterButtonLink.id="printWinLinkToPrint";
            printWinPrinterButtonImage.src="http://www.korfballeague.nl/images/printMe.gif";
            printWinPrinterButtonImage.border="0";
            printWinPrinterButtonImage.align="right";
            printWinPrinterButtonLink.appendChild(printWinPrinterButtonImage);

            printWinBody.appendChild(printWinPrinterButtonLink);

            var printWinLinks = clonedFullContent.getElementsByTagName("a");
            var numLinks = printWinLinks.length;
            for(var i=0; i<numLinks; i++) {
                var aLink = printWinLinks[i];
                aLink.disabled="DISABLED";
                aLink.href="javascript:void(0)";
                if(aLink.target) {
                    aLink.target = "";
                }
            }             var fullDiv = newWin.document.createElement('div');
            fullDiv.innerHTML = clonedFullContent.innerHTML;
            printWinBody.appendChild(fullDiv);

            var printWinStyleSheet = newWin.document.createElement('link');
            printWinStyleSheet.rel = "StyleSheet";
            printWinStyleSheet.href = "http://www.korfballeague.nl/style.css";
            printWinStyleSheet.type = "text/css";

            var printWinHead = newWin.document.getElementsByTagName("head")[0];
            printWinHead.appendChild(printWinStyleSheet);

            var printMeLink = newWin.document.getElementById("printWinLinkToPrint");
            if(printMeLink)
                printMeLink.href = "javascript:this.print()";

            var foo2 = newWin.document.getElementById("homePrintEmailSearchBar");
            if(foo2)
                foo2.style.display='none';
        }
    } else {
        alert("Sorry, your browser does not support this feature");
    }
}
