

// brake any referring site who is using frames
function abcFrameBuster() {
    if (self.parent.frames.length != 0){
        self.parent.location=document.location;
    }
}

//abcFrameBuster();

function showObject(e) {

    var elem = document.getElementById(e);
    elem.style.display = 'block';

}

function hideObject(e) {

    elem = document.getElementById(e);
    elem.style.display = 'none';

}

function slBanner(i, a) {
    
    var imgelem = document.getElementById(i);
    var linkelem = document.getElementById(a);


    var ranNum= Math.floor(Math.random()*4) + 1;
    switch (ranNum) {
        case 1:
           imgelem.src = 'http://www.abcteach.com/ads/sitelicensebanner.jpg';
           linkelem.href = '/site_license_benefits.php';
          break;
        case 2:
           imgelem.src = 'http://www.abcteach.com/images/ads/mail.jpg';
           linkelem.href = '/site_license_benefits.php';
          break;
        case 3:
           imgelem.src = 'http://www.abcteach.com/images/ads/mail-2.jpg';
           linkelem.href = '/site_license_benefits.php';
          break;
        case 4:
           imgelem.src = 'http://www.abcteach.com/images/ads/mail-3.jpg';
           linkelem.href = '/site_license_benefits.php';
          break;
     }

}

function abcBanner(i, a) {
    
    var imgElem = document.getElementById(i);
    var linkElem = document.getElementById(a);


    var ranNum= Math.floor(Math.random()*10) + 1;
    switch (ranNum) {
        case 1:
            imgElem.src = 'http://static.abcteach.com/ads/HandwritingGeneratorBanner.jpg';
            linkElem.href = '/directory/basics/handwriting/';
            break;
        case 2:
            imgElem.src = 'http://static.abcteach.com/ads/CrosswordPuzzleGenerators.jpg';
            linkElem.href = '/directory/fun_activities/crossword_puzzles/';
            break;
        case 3:
            imgElem.src = 'http://static.abcteach.com/ads/WordSearchBanner.jpg';
            linkElem.href = '/directory/fun_activities/word_searches/';
            break;
        case 4:
            imgElem.src = 'http://static.abcteach.com/ads/SudokuPuzzleGenerators.jpg';
            linkElem.href = '/directory/fun_activities/puzzle_fun/sudoku/';
          break;
        case 5:
            imgElem.src = 'http://static.abcteach.com/ads/UniversityPartnerBanner.jpg';
            linkElem.href = '/universities.php';
          break;
        case 6:
           imgElem.src = 'http://static.abcteach.com/ads/GiftBanner.jpg';
            linkElem.href = '/gift.php';
            break;
        case 7:
           imgElem.src = 'http://static.abcteach.com/ads/MathGeneratorBanner.jpg';
           linkElem.href = '/directory/basics/math/';
           break;
        case 8:
           imgElem.src = 'http://static.abcteach.com/ads/MissingLetterGeneratorBanne.jpg';
           linkElem.href = '/directory/basics/spelling/fillin_letter_worksheets/';
           break;
        case 9:
           imgElem.src = 'http://static.abcteach.com/ads/WordWallGeneratorBanner.jpg';
           linkElem.href = '/directory/teaching_extras/word_walls/';
          break;
        case 10:
           imgElem.src = 'http://static.abcteach.com/ads/SiteLicenseBanner.jpg';
           linkElem.href = '/site_license_benefits.php';
          break;
     }

}

function mass(form) {
    var i=0;
    var value=0;

    if (form.toggle_all.checked) {
        value=1;
    } else {
        value=0;
    }

    for (i = 0; i < form.length; i++) {
        if (form.elements[i].name.substring(0, 8) == 'checkbox' && !form.elements[i].disabled) {
            form.elements[i].checked = value;
        }
    }
}

// cross-browser get X-coord of event
function mouseX(evt) {
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
       return evt.clientX + (document.documentElement.scrollLeft ?
       document.documentElement.scrollLeft :
       document.body.scrollLeft);
    else return null;
}

// cross-browser get Y-coord of event
function mouseY(evt) {
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
       return evt.clientY + (document.documentElement.scrollTop ?
       document.documentElement.scrollTop :
       document.body.scrollTop);
    else return null;
}

// cross-browser get dimensions of visible screen
function getViewPortDimensions() {
    var vpWidth = 0, vpHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        vpWidth = window.innerWidth;
        vpHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        vpWidth = document.documentElement.clientWidth;
        vpHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        vpWidth = document.body.clientWidth;
        vpHeight = document.body.clientHeight;
    }
    var vpXOffset = 0, vpYOffset = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        vpYOffset = window.pageYOffset;
        vpXOffset = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        vpYOffset = document.body.scrollTop;
        vpXOffset = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        vpYOffset = document.documentElement.scrollTop;
        vpXOffset = document.documentElement.scrollLeft;
    }
    return [ vpWidth, vpHeight, vpXOffset, vpYOffset ];
}

function changeImage(increment, imgCount) {
    var previewImg = document.getElementById('preview_img');
    var endIndex = previewImg.src.lastIndexOf('.');
    var startIndex = previewImg.src.lastIndexOf('-')+1;
    var imgNumber = previewImg.src.substr(startIndex, endIndex-startIndex);
    var imgPrefix = previewImg.src.substr(0, previewImg.src.lastIndexOf('-')+1);
    if (increment) {
        imgNumber = parseInt(imgNumber) + 1;
        imgNumber = imgNumber>(imgCount-1) ? parseInt(imgNumber)-1 : imgNumber;
    }
    else {
        imgNumber = parseInt(imgNumber) - 1;
        imgNumber = imgNumber<0 ? parseInt(imgNumber)+1 : imgNumber;
    }
    var currNumber = document.getElementById('preview_image_number');
    currNumber.innerHTML = (parseInt(imgNumber) + 1);
    previewImg.src = imgPrefix + imgNumber + '.png';
}

// show or hide content preview window with supplied image and description
function togglePreview(evt, imgName, imgCount, description) {
    addPreviewer(document.body);
    if (!evt) {
        evt = window.event;
    }
    var previewDiv = document.getElementById('preview_div');
    var pointerDiv = document.getElementById('pointer_div');
    var previewImg = document.getElementById('preview_img');
    var pointerImg = document.getElementById('pointer_img');
    if (imgName != '' && (previewDiv.style.visibility == 'hidden' || previewImg.src.slice(-imgName.length, previewImg.src.length) != imgName)) {
        previewImg.src = '';
        previewImg.src = imgName;
        if (imgCount > 1) {
            var scrollWidget = '<div align="center"><img src="http://www.abcteach.com/phphtmllib/images/widgets/prev_group_button.gif" onclick="changeImage(false, ' + imgCount + ');">';
            scrollWidget = scrollWidget + ' <span id="preview_image_number">1</span> of ' + imgCount + ' ';
            scrollWidget = scrollWidget + '<img src="http://www.abcteach.com/phphtmllib/images/widgets/next_group_button.gif" onclick="changeImage(true, ' + imgCount + ');">';
            scrollWidget = scrollWidget + '</div><hr/>';
            description = scrollWidget + description;
        }
        var previewMsg = document.getElementById('preview_msg');
        previewMsg.innerHTML = description;
        var xPos = mouseX(evt);
        var yPos = mouseY(evt);
        var vpDims = getViewPortDimensions();
        var yAdj = 0;
        var divHeight = previewDiv.offsetHeight;
        if (divHeight+yPos-50 > vpDims[3] + vpDims[1]) {
            yAdj = (vpDims[3]+vpDims[1])-(divHeight+yPos-50);
        }
        var xAdj = 0;
        var pointerAdj = 0;
        var divWidth = previewDiv.offsetWidth;
        if (divWidth+xPos+70 > vpDims[2] + vpDims[0]) {
            xAdj = -140-divWidth;
            pointerAdj = -46-48;
            pointerImg.src = 'http://static.abcteach.com/globals/images/pointer_r.png';
        }
        else {
            pointerImg.src = 'http://static.abcteach.com/globals/images/pointer_l.png';
        }
        previewDiv.style.left = xPos + 70 + xAdj;
        previewDiv.style.top = yPos - 50 + yAdj;
        pointerDiv.style.left = xPos + 23 + pointerAdj;
        pointerDiv.style.top = yPos - 50;
        previewDiv.style.visibility = 'visible';
        pointerDiv.style.visibility = 'visible';
    }
    else if (previewDiv.style.visibility == 'visible') {
        previewDiv.style.visibility = 'hidden';
        pointerDiv.style.visibility = 'hidden';
    }
}

// detect whether the mouse is within the preview div
function mouseInPreview(evt) {
    if (!evt) {
        evt = window.event;
    }
    var previewDiv = document.getElementById('preview_div');
    var mX = mouseX(evt);
    var mY = mouseY(evt);
    var divX = parseInt(previewDiv.style.left);
    var divY = parseInt(previewDiv.style.top);
    var inPreview = true;
    if (mX < divX || mX > divX+previewDiv.offsetWidth) {
        inPreview = false;
    }
    if (mY < divY || mY > divY+previewDiv.offsetHeight) {
        inPreview = false;
    }
    return inPreview;
}

// choose between two alternative views of content
function setContentView(shownViewName, hiddenViewName) {
    var shownView = document.getElementById(shownViewName);
    var hiddenView = document.getElementById(hiddenViewName);
    shownView.style.display = 'block';
    hiddenView.style.display = 'none';
    placeGoogleAd();
}

// add the previewer widget to a page
function addPreviewer(parentElem) {
    var previewerId = 'preview_div';
    var previewDiv = document.getElementById(previewerId);
    if (!previewDiv) {
        var previewImgId = 'preview_img';
        var previewMsgId = 'preview_msg';
        var pointerDivId = 'pointer_div';
        var pointerImgId = 'pointer_img';
        // set up floating div for displaying pointer to preview image
        var pointerDiv = document.createElement('DIV');
        pointerDiv.id = pointerDivId;
        pointerDiv.style.position = 'absolute';
        pointerDiv.style.visibility = 'hidden';
        pointerDiv.style.backgroundColor = 'transparent';
        pointerDiv.style.zIndex = '100';
        var pointerImg = document.createElement('IMG');
        pointerImg.src = 'http://static.abcteach.com/globals/images/pointer_l.png';
        pointerImg.id = pointerImgId;
        parentElem.appendChild(pointerDiv);
        pointerDiv.appendChild(pointerImg);
        // set up floating div for displaying preview image and text
        // preview image
        var previewImg = document.createElement('IMG');
        previewImg.style.border = '1px solid #696';
        previewImg.style.width = '300px';
        previewImg.id = previewImgId;
        // preview message
        var previewMsgDiv = document.createElement('DIV');
        previewMsgDiv.style.padding = '6px';
        var previewMsg = document.createElement('SPAN');
        previewMsg.id = previewMsgId;
        // close link
        var closeDiv = document.createElement('DIV');
        closeDiv.align = 'center';
        var closeCommand = document.createElement('SPAN');
        closeCommand.innerHTML = '[close]';
        closeCommand.style.cursor = 'pointer';
        closeCommand.onclick = function(e) { togglePreview(e, '', ''); };
        // set up entire preview div
        previewDiv = document.createElement('DIV');
        previewDiv.style.position = 'absolute';
        previewDiv.style.visibility = 'hidden';
        previewDiv.style.border = '2px solid #363';
        previewDiv.style.padding = '3px';
        previewDiv.style.textAlign = 'center';
        previewDiv.style.backgroundColor = '#CFC';
        previewDiv.style.width = '320px';
        previewDiv.style.zIndex = '100';
        previewDiv.onmouseout = function(e) {if (!mouseInPreview(e)) togglePreview(e, '', '');};
        previewDiv.id = previewerId;

        parentElem.appendChild(previewDiv);

        // put preview div together
        previewDiv.appendChild(closeDiv);
        previewDiv.appendChild(previewImg);
        previewDiv.appendChild(document.createElement('BR'));
        previewDiv.appendChild(previewMsgDiv);

        // fill in closeDiv
        closeDiv.appendChild(closeCommand);
        closeDiv.appendChild(document.createElement('BR'));
        closeDiv.appendChild(document.createElement('BR'));

        // fill in previewMsgDiv
        previewMsgDiv.appendChild(previewMsg);
        
//        return previewDiv;
    }
}

function checkEmail(str) {

    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1){
        return false;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false;
    }

    if (str.indexOf(at,(lat+1))!=-1){
        return false;
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
    }

    if (str.indexOf(dot,(lat+2))==-1){
        return false;
    }

    if (str.indexOf(" ")!=-1){
        return false;
    }

    return true;
}

function popImg(image, mouseover) {
    if (mouseover) {
        image.src = image.src.replace('.png', '_ms.png');
    }
    else {
        image.src = image.src.replace('_ms.png', '.png');
    }
}

function selectTab(tab) {
    tab.src = tab.src.replace('tab.png', 'tab_ms.png');
}
function deselectTab(tab) {
    tab.src = tab.src.replace('tab_ms.png', 'tab.png');
}
function makeTabActive(siteId) {
    var currURL = document.location.pathname;
    if (siteId == 1) {
        /* free tabs */
        // special free directory mapping
        if (currURL.slice(0,23) == '/directory/interactive/') {
            currURL = '/interactive_home.php';
        }        
        else if (currURL.slice(0,11) == '/directory/') {
            currURL = '/directory_home.php';
        }        
        // directory tab equivalents
        var directoryEquiv = ['/search.php', '/php/static_directory.php', '/whats_new.php'];
        for (var i=0; i<directoryEquiv.length; i++) {
            if (currURL == directoryEquiv[i]) {
                currURL = '/directory_home.php';
            }
        }
        // abctools equivalents
        var toolsEquiv = ['/free_flash_view.php',
                            '/free_abc_sort_form.php',
                            '/free_bingo_form.php',
                            '/free_crossword_form.php',
                            '/free_desk_tag_form.php',
                            '/free_handwriting_worksheet_form.php',
                            '/free_letter_fill_in_form.php',
                            '/free_math_problem_form.php',
                            '/free_math_worksheet_form.php',
                            '/free_misspelling_list_form.php',
                            '/free_shapebook_form.php',
                            '/free_spelling_list_form.php',
                            '/free_sudoku_form.php',
                            '/free_word_search_form.php',
                            '/free_word_search_template_form.php',
                            '/free_word_shape_form.php',
                            '/free_word_unscramble_form.php',
                            '/free_word_wall_form.php'];
        for (var i=0; i<toolsEquiv.length; i++) {
            if (currURL == toolsEquiv[i]) {
                currURL = '/abctools_home.php';
            }
        }
        // membership tab equivalents
        var membershipEquiv = ['/site_license_benefits.php',
                                '/pto.php',
                                '/group_membership.php',
                                '/homeschool.php',
                                '/universities.php',
                                '/gift.php',
                                '/pto_membership.php'];
        for (var i=0; i<membershipEquiv.length; i++) {
            if (currURL == membershipEquiv[i]) {
                currURL = '/membership.php';
            }
        }
    }
    else if (siteId == 16) {
        /* member tabs */
        // directory tab equivalents
        var directoryEquiv = ['/directory.php', '/whats_new.php', '/search.php'];
        for (var i=0; i<directoryEquiv.length; i++) {
            if (currURL == directoryEquiv[i]) {
                currURL = '/mem_directory_home.php';
            }
        }
        // myabcteach tab equivalents
        var myabcEquiv = ['/favorites.php', '/recently_viewed.php', '/document_list.php'];
        for (var i=0; i<myabcEquiv.length; i++) {
            if (currURL == myabcEquiv[i]) {
                currURL = '/mem_myabcteach_home.php';
            }
        }
        // abctools equivalents
        var toolsEquiv = ['/flash_view.php',
                            '/abctools.php',
                            '/word_list_form.php',
                            '/word_search_form.php',
                            '/word_shape_form.php',
                            '/word_unscramble_form.php',
                            '/sudoku_form.php',
                            '/handwriting_practice_form.php',
                            '/handwriting_template_form.php',
                            '/crossword_form.php',
                            '/bingo_form.php',
                            '/letter_fill_in_form.php',
                            '/new_shapebook_form.php',
                            '/word_wall_form.php',
                            '/desk_tag_form.php',
                            '/abc_order_form.php',
                            '/misspelling_list_form.php',
                            '/spelling_list_form.php',
                            '/math_problem_form.php',
                            '/math_worksheet_form.php',
                            '/abctool_preferences.php'];
        for (var i=0; i<toolsEquiv.length; i++) {
            if (currURL == toolsEquiv[i]) {
                currURL = '/mem_abctools_home.php';
            }
        }
    }
    // continue with activating tab
    var list = document.getElementById('primary-links');
    var listItem = null;
    var listA = null;
    var listImg = null;
    var selectedItem = null;
    var prevPath = null;
    for (var i=0; i<list.childNodes.length; i++) {
        if (list.childNodes[i].tagName && list.childNodes[i].tagName.toLowerCase() == 'li') {
            listItem = list.childNodes[i];
            listA = document.getElementById(listItem.id.replace('i','a'));
            listAPath = listA.href;
            $startIndex = listAPath.indexOf('/', 8);
            $endIndex = listAPath.indexOf('?')==-1 ? listAPath.length : listAPath.indexOf('?');
            listAPath = listAPath.slice($startIndex, $endIndex);
            if ( listAPath == '/' ) {  // there can only be an exact match on the root directory, otherwise everything would match it
                if (currURL == '/' || currURL == '/index.html' || currURL == '/index.php') {
                    selectedItem = listItem;
                    break;
                }
            }
            else if ( listAPath == currURL.slice(0, listAPath.length) && // match all others to themselves -and- descendants
                    (!prevPath || listAPath.length > prevPath.length) ) { // match the current url to the most specific matching tab (by url length)
                prevPath = listAPath;
                selectedItem = listItem;
            }
        }
    }
    if (selectedItem) {
        selectedImg = document.getElementById(selectedItem.id.replace('i','im'));
        selectedImg.src = selectedImg.src.replace('tab.png', 'tab_active.png');
        selectedItem.className = 'active-tab';
    }
}

function toggleBoxBG(bg, elem, showPrompt) {
    var suffix = '-prompt';
    if (elem.value) elem.value = elem.value.replace(/^ */, '');
    if (showPrompt && (!elem.value || elem.value == '')) {
        if (bg.className.indexOf(suffix) == -1) {
            bg.className = bg.className + suffix;
        }
    }
    else {
        bg.className = bg.className.replace(suffix, '');
    }
}

function setPageWidths(siteId) {
    var freeWidths = [['/directory_home.php', ['314px', '291px', '291px'], ['30px', '18px', '21px']],
                        ['/abctools_home.php', ['292px', '292px', '292px'], ['30px', '30px', '30px']],
                        ['/interactive_home.php', ['280px', '350px', '292px'], ['20px', '10px', '10px']]];
    var memberWidths = [['/', ['215px', '489px', '225px'], ['0px', '0px', '0px']],
                        ['/index.php', ['215px', '489px', '225px'], ['0px', '0px', '0px']],
                        ['/flash_view.php', ['1px', '939px', '1px'], ['0px', '0px', '0px']],
                        ['/clipart_selection_form.php', ['1px', '939px', '1px'], ['0px', '0px', '0px']],
                        ['/site_redesign.php', ['1px', '939px', '1px'], ['0px', '0px', '0px']]];

    var currURL = document.location.pathname;
    var pageWidths = siteId==1 ? freeWidths : memberWidths;
    for (var i=0; i<pageWidths.length; i++) {
        if (currURL == pageWidths[i][0]) {
            var left = document.getElementById('sidebar-left');
            var main = document.getElementById('main');
            var right = document.getElementById('sidebar-right');
            if (left) left.style.width = pageWidths[i][1][0];
            if (left) left.style.paddingLeft = pageWidths[i][2][0];
            if (main) main.style.width = pageWidths[i][1][1];
            if (main) main.style.paddingLeft = pageWidths[i][2][1];
            if (right) right.style.width = pageWidths[i][1][2];
            if (right) right.style.paddingLeft = pageWidths[i][2][2];
        }
    }
}

function loadQuote(element) {
    var quotes = ['"Thank you" for this wonderful site. I teach from all the super ideas every week." - Mary, Florida',
            '"Your site has saved me hours and hours of preparation" -Bruce, UK',
            '"abcteach is such a time saver and I can always find something that relates to my curriculum." - Susan, Ohio',
            '"I am so happy with your site.  I can\'t imagine teaching without it!" - Wendy, New York',
            '"abcteach is by far the most valuable teaching tool I have come across in nearly 30 years of teaching!" - Ann',
            '"Thank you! Your website saves me 10 hours a week of work!" - Bruce, Saudi Arabia',
            '"abcteach never fails to be an invaluable resource in my never-ending quest to teach outside the box." - Laura',
            '"This is the BEST site that I have ever joined. I am still amazed I find new things every day." - Brenda, Mississippi'];
            
            var key = Math.round(Math.random() * (quotes.length - 1));
            element.innerHTML = quotes[key];
}

function placeGoogleAd() {
    var thumbView = document.getElementById('thumbnail_view');
    var target = null;
    if (thumbView && thumbView.style.display != 'none') {
        target = document.getElementById('thumb-ad-target');
    }
    else {
        target = document.getElementById('list-ad-target');
    }

    var ad = document.getElementById('directory-google-ad');
    if (target && ad) {
        target.appendChild(ad);
    }
}

function getFutureDate(quantity, units) {
    var currentDate = new Date();
    var increment = 0;
    var msPerDay = 24 * 60 * 60 * 1000;
    switch (units) {
        case 'day':
        case 'days':
            increment = 1 * quantity * msPerDay;
            var futureMS = currentDate.getTime() + increment;
            currentDate.setTime(futureMS);
            break;
        case 'week':
        case 'weeks':
            increment = 7 * quantity * msPerDay;
            var futureMS = currentDate.getTime() + increment;
            currentDate.setTime(futureMS);
            break;
        case 'month':
        case 'months':
            var currMonth = currentDate.getMonth();
            var futureMonth = (currMonth + quantity)%12;
            var futureYear = currentDate.getFullYear() + Math.floor((currMonth + quantity)/12);
            currentDate.setMonth(futureMonth);
            currentDate.setYear(futureYear);
            break;
        case 'year':
        case 'years':
            currentDate.setYear(currentDate.getFullYear() + quantity);
            break;
        default:
            break;
    }
    return currentDate;
}

