function search() {
    site = document.searchform.site.options[document.searchform.site.selectedIndex].value;
    request = document.searchform.request.options[document.searchform.request.selectedIndex].value;
    searchText = document.searchform.searchfor.value;

    if (request == "QS") {
        quickSearch(site, searchText);
    }
    else if (request == "US") {
        upcSearch(site, searchText);
    }
    else if (request == "AS") {
        searchPage(site);
    }
    else if (request == "HP") {
        homePage(site);
    }
    else if (request == "BS") {
        bestsellers(site);
    }
    else if (request == "NR") {
        newReleases(site);
    }
}


function quickSearch(site, searchText) {
    if (site == "TW") {
        searchURL = "http://www.towerrecords.com/Music/Default.aspx?search_in=music&free_text={0}&";
    }
    else if (site == "AM") {
        searchURL = "http://www.amazon.com/exec/obidos/external-search?mode=music&keyword={0}&tag=readerware";
    }
    else if (site == "BN") {
        searchURL = "http://service.bfast.com/bfast/click?sourceid=9196118&bfmid=2181&categoryid=mus_anymtch&qsrch=A&WRD={0}";
    }
    else if (site == "BO") {
        searchURL = "http://www.borderstores.com/search/search.jsp?srchTerms={0}&mediaType=2&srchType=Author";
    }
    else if (site == "HC") {
        searchURL = "http://half.ebay.com/search/search.jsp?product=music&keyword={0}&AID=1100790&PID=204820";
    }
    else if (site == "AL") {
        searchURL = "http://www.allmusic.com/cg/amg.dll?P=amg&opt1=1&sql={0}";
    }
    else if (site == "L2") {
        alert("LOC does not support a direct Artist search\n\nPlease use Advanced Search");
        return;
    }
    else if (site == "SG") {
        searchURL = "http://www.samgoody.com/portal/sg_music_home";
    }
    else if (site == "MP") {
        searchURL = "http://www.mediaplay.com/portal/mp_music_home";
    }
    else if (site == "BA") {
        searchURL = "http://cdbaby.com/found?allsearch={0}&allsearchsubmit=Search";
    }
    else if (site == "FR") {
        searchURL = "http://www.freedb.org/freedb_search.php?words={0}&allfields=NO&fields=artist&fields=title&allcats=YES&grouping=none";
    }
    else if (site == "CD") {
        searchURL = "http://www.gracenote.com/music/search-adv.html?q={0}&qartist=&qdisc=&qtrack=&n=10&x=0&y=0";
    }
    else if (site == "EI") {
        searchURL = "http://eil.com/shop/artistlist.asp?artistname={0}";
    }
    else if (site == "TU") {
        searchURL = "http://uk.towerrecords.com/searchresults.asp?urlid=&D2=music&entry={0}";
    }
    else if (site == "RG") {
        searchURL = "http://www.realgroovy.co.nz/index.asp?searchstring={0}&stype=artist&c=search&x=11&y=7";
    }
    else if (site == "AU") {
        document.forms[2].elements[1].value=searchText;
        document.forms[2].elements[4].value="";
        document.forms[2].submit();
        return
    }
    else if (site == "AC") {
        document.forms[4].elements[1].value=searchText;
        document.forms[4].submit();
        return
    }
    else if (site == "BG") {
        alert("Quick Search not supported for BMG\n\nPlease use Advanced Search");
        return;
    }
    else if (site == "FC") {
        document.forms[6].elements[0].value=searchText;
        document.forms[6].submit();
        return
    }
    else if (site == "CB") {
        searchURL = "http://www.christianbook.com/Christian/Books/easy_find?Ntk=product.full_auth_name&Ntt={0}&action=Search&N=0&Ne=0&event=ESRCN&nav_search=1&Go.x=0&Go.y=0&Go=Go";
    }

    i = searchURL.indexOf("{0}");

    if (i != -1) {
        searchText = escape(searchText);

        if (site == "TU") {
            searchText = escape(searchText);
        }

        searchURL = searchURL.substring(0, i) + searchText + searchURL.substring(i + 3, searchURL.length);
    }

    openWindow(searchURL);
}


function upcSearch(site, searchText) {
    if (site == "TW") {
        searchURL = "http://www.towerrecords.com/product.asp?pfid={0}";
    }
    else if (site == "AM") {
        var parts=searchText.split('.');

        if(parts.length == 5) {
            searchText = decodePart(parts[3]);
            document.searchform.searchfor.value = searchText;
        }

        document.forms[3].elements[0].value=searchText;
        document.forms[3].submit();
        return
    }
    else if (site == "BN") {
        searchURL = "http://service.bfast.com/bfast/click?bfmid=2181&sourceid=9196118&bfpid={0}&bfmtype=music";
    }
    else if (site == "BO") {
        searchURL = "http://www.borderstores.com/search/search.jsp?srchTerms={0}&mediaType=2&srchType=ISBN";
    }
    else if (site == "HC") {
        searchURL = "http://www.half.com/search/search.jsp?meta_id=2&product=music&search_by=upc&query={0}";
    }
    else if (site == "SG") {
        searchURL = "http://shop.samgoody.com/site/catalog/catalog.jsp?upc={0}";
    }
    else if (site == "MP") {
        searchURL = "http://shop.mediaplay.com/site/catalog/catalog.jsp?upc={0}";
    }
    else if (site == "BA") {
        alert("CD Baby does not support a UPC search\n\nPlease use Advanced Search");
        return;
    }
    else if (site == "FR") {
        alert("freedb.org does not support a UPC search\n\nPlease use Advanced Search");
        return;
    }
    else if (site == "CD") {
        alert("CDDB does not support a UPC search\n\nPlease use Advanced Search");
        return;
    }
    else if (site == "EI") {
        alert("eil.com does not support a UPC search\n\nPlease use Advanced Search");
        return;
    }
    else if (site == "AL") {
        alert("All Music Guide does not support a UPC search\n\nPlease use Advanced Search");
        return;
    }
    else if (site == "L2") {
        alert("LOC does not support a UPC search\n\nPlease use Advanced Search");
        return;
    }
    else if (site == "TU") {
        searchURL = "http://uk.towerrecords.com/product.asp?pfid={0}";
    }
    else if (site == "RG") {
        alert("Real Groovy does not support a UPC search\n\nPlease use Advanced Search");
        return;
    }
    else if (site == "AU") {
        var parts=searchText.split('.');

        if(parts.length == 5) {
            searchText = decodePart(parts[3]);
            document.searchform.searchfor.value = searchText;
        }

        document.forms[2].elements[1].value="";
        document.forms[2].elements[4].value=searchText;
        document.forms[2].submit();
        return
    }
    else if (site == "AC") {
        var parts=searchText.split('.');

        if(parts.length == 5) {
            searchText = decodePart(parts[3]);
            document.searchform.searchfor.value = searchText;
        }

        document.forms[5].elements[0].value=searchText;
        document.forms[5].submit();
        return
    }
    else if (site == "BG") {
        alert("UPC Search not supported for BMG\n\nPlease use Advanced Search");
        return;
    }
    else if (site == "FC") {
        var parts=searchText.split('.');

        if(parts.length == 5) {
            searchText = decodePart(parts[3]);
            document.searchform.searchfor.value = searchText;
        }

        document.forms[6].elements[0].value=searchText;
        document.forms[6].submit();
        return
    }
    else if (site == "CB") {
        alert("Christianbook.com does not support a UPC search\n\nPlease use Advanced Search");
        return;
    }

    i = searchURL.indexOf("{0}");

    if (i != -1) {
        var parts=searchText.split('.');

        if(parts.length == 5) {
            searchText = decodePart(parts[3]);
            document.searchform.searchfor.value = searchText;
        }

        if ((site == "TW") ||
            (site == "BO") ||
            (site == "FC") ||
            (site == "HC")) {
            if (searchText.length == 13) {
                var group = searchText.substring(0, 2);

                if ((group >= "00") &&
                    (group <= "09")) {
                    searchText = searchText.substring(1);
                }
            }
        }

        if ((site == "TW") ||
            (site == "TU")) {
            searchText = searchText.substring(0, searchText.length - 1);
        }

        if (site == "BG") {
            searchText = searchText.substring(1);
        }

        searchURL = searchURL.substring(0, i) + searchText + searchURL.substring(i + 3, searchURL.length);
    }

    openWindow(searchURL);
}


function searchPage(site) {
    if (site == "TW") {
        searchURL = "http://www.towerrecords.com/Music/Default.aspx?adv=1";
    }
    else if (site == "AM") {
        searchURL = "http://www.amazon.com/exec/obidos/redirect?tag=readerware&path=tg/stores/static/-/music/search";
    }
    else if (site == "BN") {
        searchURL = "http://service.bfast.com/bfast/click?bfmid=2181&sourceid=9196118&categoryid=music_search";
    }
    else if (site == "BO") {
        searchURL = "http://www.borderstores.com/search/search.jsp?tt=gn";
    }
    else if (site == "HC") {
        searchURL = "http://half.ebay.com/search/power_search.cfm?product=music";
    }
    else if (site == "AL") {
        searchURL = "http://www.allmusic.com";
    }
    else if (site == "L2") {
        searchURL = "http://catalog.loc.gov/cgi-bin/Pwebrecon.cgi?DB=local&PAGE=bbSearch";
    }
    else if (site == "SG") {
        searchURL = "http://www.samgoody.com/portal/sg_music_home";
    }
    else if (site == "MP") {
        searchURL = "http://www.mediaplay.com/portal/mp_music_home";
    }
    else if (site == "BA") {
        searchURL = "http://cdbaby.com/search";
    }
    else if (site == "FR") {
        searchURL = "http://www.freedb.org/freedb_search.php";
    }
    else if (site == "CD") {
        searchURL = "http://www.gracenote.com/music/";
    }
    else if (site == "EI") {
        searchURL = "http://eil.com/shop/search.asp";
    }
    else if (site == "TU") {
        searchURL = "http://uk.towerrecords.com/search_music.asp";
    }
    else if (site == "RG") {
        searchURL = "http://www.realgroovy.co.nz/index.asp?c=music&s=music";
    }
    else if (site == "AU") {
        searchURL = "http://www.amazon.co.uk/exec/obidos/redirect?tag=readerware03&path=tg/stores/static/-/music/search";
    }
    else if (site == "AC") {
        searchURL = "http://www.amazon.ca/exec/obidos/tg/search/-/music/";
    }
    else if (site == "BG") {
        searchURL = "http://www.bmgmusicservice.com/catalog/search/adv_search.jhtml";
    }
    else if (site == "FC") {
        searchURL = "http://www.familychristian.com/default.asp?intCategory=7";
    }
    else if (site == "CB") {
        searchURL = "http://www.christianbook.com/Christian/Books/advanced_search/";
    }

    openWindow(searchURL);
}


function homePage(site) {
    if (site == "TW") {
        homeURL = "http://www.towerrecords.com/Music/Default.aspx";
    }
    else if (site == "AM") {
        homeURL = "http://www.amazon.com/exec/obidos/redirect?tag=readerware&path=tg/browse/-/5174";
    }
    else if (site == "BN") {
        homeURL = "http://service.bfast.com/bfast/click?bfmid=2181&sourceid=9196118&categoryid=rn_music_home";
    }
    else if (site == "BO") {
        homeURL = "http://www.borderstores.com/index.jsp?tt=gn";
    }
    else if (site == "AL") {
        homeURL = "http://www.allmusic.com";
    }
    else if (site == "L2") {
        homeURL = "http://catalog.loc.gov/";
    }
    else if (site == "HC") {
        homeURL = "http://half.ebay.com/products/music/index.cfm";
    }
    else if (site == "SG") {
        homeURL = "http://www.samgoody.com/portal/sg_music_home";
    }
    else if (site == "MP") {
        homeURL = "http://www.mediaplay.com/portal/mp_music_home";
    }
    else if (site == "BA") {
        homeURL = "http://cdbaby.com/home";
    }
    else if (site == "FR") {
        homeURL = "http://www.freedb.org/index.php";
    }
    else if (site == "CD") {
        homeURL = "http://www.gracenote.com/music/";
    }
    else if (site == "EI") {
        homeURL = "http://eil.com/";
    }
    else if (site == "TU") {
        homeURL = "http://uk.towerrecords.com/music.asp";
    }
    else if (site == "RG") {
        homeURL = "http://www.realgroovy.co.nz/index.asp?c=music&s=music";
    }
    else if (site == "AU") {
        homeURL = "http://www.amazon.co.uk/exec/obidos/redirect?tag=readerware03&path=tg/stores/browse/-/music/229816";
    }
    else if (site == "AC") {
        homeURL = "http://www.amazon.ca/exec/obidos/tg/browse/-/916514/";
    }
    else if (site == "BG") {
        homeURL = "http://service.bfast.com/bfast/click?bfmid=20904140&siteid=38558230&bfpage=12for1msstatic";
    }
    else if (site == "FC") {
        homeURL = "http://www.familychristian.com/default.asp?intCategory=7";
    }
    else if (site == "CB") {
        homeURL = "http://www.christianbook.com/html/specialty/1002.html/";
    }

    openWindow(homeURL);
}


function bestsellers(site) {
    if (site == "TW") {
        homeURL = "http://www.towerrecords.com/Music/Default.aspx?feature=New+Releases&";
    }
    else if (site == "AM") {
        homeURL = "http://www.amazon.com/exec/obidos/redirect?tag=readerware&path=subst/music/charts/hot-100-music.html";
    }
    else if (site == "BN") {
        homeURL = "http://service.bfast.com/bfast/click?bfmid=2181&sourceid=9196118&categoryid=mus_charts";
    }
    else if (site == "BO") {
        homeURL = "http://www.borderstores.com/index.jsp?tt=gn";
    }
    else if (site == "AL") {
        homeURL = "http://www.allmusic.com";
    }
    else if (site == "L2") {
        homeURL = "http://catalog.loc.gov/";
    }
    else if (site == "HC") {
        homeURL = "http://half.ebay.com/products/top.cfm?tag=music";
    }
    else if (site == "SG") {
        homeURL = "http://www.samgoody.com/portal/sg_music_bestsellers";
    }
    else if (site == "MP") {
        homeURL = "http://www.mediaplay.com/portal/mp_music_bestsellers";
    }
    else if (site == "BA") {
        homeURL = "http://cdbaby.com/topnow";
    }
    else if (site == "FR") {
        homeURL = "http://www.freedb.org/index.php";
    }
    else if (site == "CD") {
        homeURL = "http://www.gracenote.com/music/";
    }
    else if (site == "EI") {
        homeURL = "http://eil.com/features/BestSellersWeek.asp";
    }
    else if (site == "TU") {
        homeURL = "http://uk.towerrecords.com/music.asp";
    }
    else if (site == "RG") {
        homeURL = "http://www.realgroovy.co.nz/index.asp?c=music&s=music";
    }
    else if (site == "AU") {
        homeURL = "http://www.amazon.co.uk/exec/obidos/redirect?tag=readerware03&path=tg/stores/browse/-/music/229816";
    }
    else if (site == "AC") {
        homeURL = "http://www.amazon.ca/exec/obidos/tg/browse/-/1040276/";
    }
    else if (site == "BG") {
        homeURL = "http://service.bfast.com/bfast/click?bfmid=20904140&siteid=38558230&bfpage=12for1msstatic";
    }
    else if (site == "FC") {
        homeURL = "http://www.familychristian.com/default.asp?intCategory=7";
    }
    else if (site == "CB") {
        homeURL = "http://www.christianbook.com/Christian/Books/cms_content?page=551311&sp=1002&event=1002RNF";
    }

    openWindow(homeURL);
}


function newReleases(site) {
    if (site == "TW") {
        homeURL = "http://www.towerrecords.com/Music/Default.aspx?feature=New+Releases&";
    }
    else if (site == "AM") {
        homeURL = "http://www.amazon.com/exec/obidos/redirect?tag=readerware&path=tg/browse/-/465672";
    }
    else if (site == "BN") {
        homeURL = "http://service.bfast.com/bfast/click?bfmid=2181&sourceid=9196118&categoryid=rn_onthecharts";
    }
    else if (site == "BO") {
        homeURL = "http://www.borderstores.com/index.jsp?tt=gn";
    }
    else if (site == "AL") {
        homeURL = "http://www.allmusic.com";
    }
    else if (site == "L2") {
        homeURL = "http://catalog.loc.gov/";
    }
    else if (site == "HC") {
        homeURL = "http://half.ebay.com/products/just_in_bin.cfm?tag=music";
    }
    else if (site == "SG") {
        homeURL = "http://www.samgoody.com/portal/sg_music_newreleases";
    }
    else if (site == "MP") {
        homeURL = "http://www.mediaplay.com/portal/mp_music_newreleases";
    }
    else if (site == "BA") {
        homeURL = "http://cdbaby.com/new";
    }
    else if (site == "FR") {
        homeURL = "http://www.freedb.org/index.php";
    }
    else if (site == "CD") {
        homeURL = "http://www.gracenote.com/music/";
    }
    else if (site == "EI") {
        homeURL = "http://eil.com/shop/latest-Arrivals.asp";
    }
    else if (site == "TU") {
        homeURL = "http://uk.towerrecords.com/newrelease.asp?genre=";
    }
    else if (site == "RG") {
        homeURL = "http://www.realgroovy.co.nz/index.asp?s=music&c=p&p=recent";
    }
    else if (site == "AU") {
        homeURL = "http://www.amazon.co.uk/exec/obidos/redirect?tag=readerware03&path=tg/stores/browse/-/music/229816";
    }
    else if (site == "AC") {
        homeURL = "http://www.amazon.ca/exec/obidos/tg/browse/-/1003050/";
    }
    else if (site == "BG") {
        homeURL = "http://service.bfast.com/bfast/click?bfmid=20904140&siteid=38558230&bfpage=12for1msstatic";
    }
    else if (site == "FC") {
        homeURL = "http://www.familychristian.com/default.asp?intCategory=7";
    }
    else if (site == "CB") {
        homeURL = "http://www.christianbook.com/Christian/Books/cms_content?page=549837&sp=1002";
    }

    openWindow(homeURL);
}


function openWindow(url) {
    window.open(url, "Readerware", "location=yes,menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes,status=yes,directories=yes");
}


// Copyright (c) 2000  Dustin Sallings dustin@spy.net
// Any work derived from this code must retain the above copyright.
// Please send all modifications of this script to me.
//
// modified by Andrew Irwin  irwin@aesop.rutgers.edu
function decodePart(str) {
    var m = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-";
    var result = "";
    var packer = 0;
    var count = 0;

    var i = 0;
    for (i=0; i < str.length; i++) {
        // Get the offset to the current character in our map
        var x = m.indexOf(str.charAt(i));

        // For invalid characters, point them out really well!
        if(x<0) {
            result += " > " + str.charAt(i) + " < ";
            continue;
        }

        // only count valid characters...why not?
        count++;

        // Pack them bits.
        packer = packer << 6 | x

        // every four bytes, we have three valid characters.
        if (count == 4) {
            result += String.fromCharCode((packer >> 16) ^ 67);
            result += String.fromCharCode((packer >> 8 & 255) ^ 67);
            result += String.fromCharCode((packer & 255) ^ 67);
            count=0; packer=0;
        }
    }

    // Now, deal with the remainders
    if(count==2) {
        result += String.fromCharCode((( (packer << 12) >> 16) ^ 67));
    } else if(count == 3) {
        result += String.fromCharCode(( (packer << 6) >> 16) ^ 67);
        result += String.fromCharCode(( (packer << 6) >> 8 & 255) ^ 67);
    }
    return result;
}


AU") {
        homeURL = "http://www.amazon.co.uk/gp/redirect.html?link_code=ur2&tag=readerware03&camp=1634&creative=6738&location=%2Fgp%2Fbrowse.html%3Fnode%3D297457";
    }
    else if (site == "AC") {
        homeURL = "http://www.amazon.ca/exec/obidos/tg/browse/-/1003050/";
    }
    else if (site == "BG") {
        homeURL = "http://service.bfast.com/bfast/click?bfmid=20904140&siteid=38558230&bfpage=12for1msstatic";
    }
    else if (site == "FC") {
        homeURL = "http://www.familychristian.com/default.asp?intCategory=7";
    }
    else if (site == "CB") {
        homeURL = "http://www.christianbook.com/Christian/Books/cms_content?page=549837&sp=1002";
    }

    openWindow(homeURL);
}


function openWindow(url) {
    window.open(url, "Readerware", "location=yes,menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes,status=yes,directories=yes");
}


// Copyright (c) 2000  Dustin Sallings dustin@spy.net
// Any work derived from this code must retain the above copyright.
// Please send all modifications of this script to me.
//
// modified by Andrew Irwin  irwin@aesop.rutgers.edu
function decodePart(str) {
    var m = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-";
    var result = "";
    var packer = 0;
    var count = 0;

    var i = 0;
    for (i=0; i < str.length; i++) {
        // Get the offset to the current character in our map
        var x = m.indexOf(str.charAt(i));

        // For invalid characters, point them out really well!
        if(x<0) {
            result += " > " + str.charAt(i) + " < ";
            continue;
        }

        // only count valid characters...why not?
        count++;

        // Pack them bits.
        packer = packer << 6 | x

        // every four bytes, we have three valid characters.
        if (count == 4) {
            result += String.fromCharCode((packer >> 16) ^ 67);
            result += String.fromCharCode((packer >> 8 & 255) ^ 67);
            result += String.fromCharCode((packer & 255) ^ 67);
            count=0; packer=0;
        }
    }

    // Now, deal with the remainders
    if(count==2) {
        result += String.fromCharCode((( (packer << 12) >> 16) ^ 67));
    } else if(count == 3) {
        result += String.fromCharCode(( (packer << 6) >> 16) ^ 67);
        result += String.fromCharCode(( (packer << 6) >> 8 & 255) ^ 67);
    }
    return result;
}



