﻿/* ************************************** */
var _appRoot = '';
var _imgRoot = '';
var _pubID = 0;
var _cookieName = 'PennWellBGN';
/* ************************************** */

//Trim function for strings - front and end trim spaces
//Ref: http://www.codestore.net/store.nsf/unid/BLOG-20060313
String.prototype.trim = function()
{
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

function bgnNotAvailable() {
    alert('This feature is not yet available');
}

function bgnShowAdvancedSearch() {
    if ($('search_options')) {
        if ($('search_options').style.display == 'none') {
            $('search_options').style.display = 'block';
            $('advanced_link').className = 'advanced_show';
        }
        else {
            $('search_options').style.display = 'none';
            $('advanced_link').className = 'advanced_hide';
        }
    }
}

function bgnCheckSearchTerm() {
    if ($('search_term')) {

        if ($('search_term').value.trim().length < 2) {
            alert('You must enter a search term.');
            return false;
        }
        else {
            return true;
        }
    }
    else {
        return false;
    }
}

function bgnStipHtml(str) {
    var re = /<\S[^>]*>/g;
    return str.replace(re, "");
}

function bgnShowCoyComments() {
    if ($('coymain')) { $('coymain').style.display = 'none'; }
    if ($('coycomments')) { $('coycomments').style.display = 'block'; }
}

function bgnShowCoyMain() {
    if ($('coymain')) { $('coymain').style.display = 'block'; }
    if ($('coycomments')) { $('coycomments').style.display = 'none'; }
}

function bgnShowProdComments() {
    if ($('prodmain')) { $('prodmain').style.display = 'none'; }
    if ($('prodcomments')) { $('prodcomments').style.display = 'block'; }
}

function bgnShowProdMain() {
    if ($('prodmain')) { $('prodmain').style.display = 'block'; }
    if ($('prodcomments')) { $('prodcomments').style.display = 'none'; }
}

function bgnGetExternalRssFeed(uri, numlinks)
{
    var callbackFile = _appRoot + 'callbacks/GetRssFeed.aspx?rand=' + bgnGetRandomNumber();

    new Ajax.Request(callbackFile,
    {
        method: 'post',
        parameters: { rssuri: uri, maxlinks: numlinks },
        onSuccess: function(transport) {
            bgnGetExternalRssFeedSuccess(transport.responseText);
        },
        onFailure: function(transport) {
            alert('Error retrieving the RSS Feed');
        }
    });
}

function bgnGetRandomNumber()
{
	var d = new Date();
	var time = d.getTime();

	return time;
}

function bgnGetExternalRssFeedSuccess(retVal) {   
    if ($('RssContent')) { $('RssContent').innerHTML = retVal; }
}

function bgnShowMoreRssLinks()
{
    if($('rss_more')){
        $('rss_more').style.display = 'block';
    }

    if($('rss_more_link')) {
        $('rss_more_link').style.display = 'none';
    }
}

function bgnShowLessRssLinks()
{
    if($('rss_more')){
        $('rss_more').style.display = 'none';
    }

    if($('rss_more_link')) {
        $('rss_more_link').style.display = 'block';
    }
}

function bgnShowChildCats(id) {
    if ($('catchild' + id)) {
        if ('catchild' == $('catchild' + id).className) {
            $('catchild' + id).className = 'catchildhide';

            if ($('imgcat' + id)) {
                $('imgcat' + id).src = _imgRoot + 'plus.gif';
            }
            
            //$('hlcatexp' + id).className = 'cat-expand';
        }
        else {
            $('catchild' + id).className = 'catchild';

            if ($('imgcat' + id)) {
                $('imgcat' + id).src = _imgRoot + 'minus.gif';
            }
            
            //$('hlcatexp' + id).className = 'cat-collapse';
        }
    }
}

function bgnUpdatePageLength() {
    var page_length = 0;

    if ($('rpp')) { page_length = $('rpp').options[$('rpp').selectedIndex].value; }

    if (page_length > 0) {
        var callbackFile = _appRoot + 'callbacks/UpdatePageLength.aspx';

        new Ajax.Request(callbackFile, {
            method: 'post',
            parameters: { rpp: page_length },
            onSuccess: function(transport) {
                bgnUpdatePageLengthSuccess(transport.responseText);
            },
            onFailure: function(transport) {
                alert('Error setting the default page length');
            }
        });
    }
}

function bgnUpdatePageLengthSuccess(retval) {
    if ('success' == retval) { location.reload(true); }
}

function bgnPrintPage() {

    // grab the current url, add the prntf param to it and open a print dialog
    var currentUrl = window.location.toString();

    if (currentUrl.indexOf("?") > -1) {
        currentUrl = currentUrl + "&prtf=1";
    }
    else {
        currentUrl = currentUrl + "?prtf=1";
    }

    // open a new window
    printFriendlyWindow = window.open(currentUrl, "PrinterFriendlyVersion", "toolbar=0, status=0, menubar=1, width=650, height=500, scrollbars=1");
    printFriendlyWindow.focus();
    printFriendlyWindow.print();
}

function bgnShowEmailColleagueForm(pub) {
    var link = window.location.href;
    var url = _appRoot + "EmailColleague.aspx?pub=" + pub + "&pagelink=" + encodeURIComponent(link);
    //.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
    var emailColleagueWindow = window.open(url, "EmailColleague", "toolbar=0, status=0, menubar=0, width=650, height=470, scrollbars=0");
    emailColleagueWindow.focus();
}

function bgnValidateEmailColleagueForm() {
    if (bgnIsFieldEmpty('txtEmailFromName')) {
        alert('Please enter your name');
        $('txtEmailFromName').focus()
        return false;
    }

    if (bgnIsFieldEmpty('txtEmailFrom')) {
        alert('Please enter your email address');
        $('txtEmailFrom').focus()
        return false;
    }
    
    if (bgnIsFieldEmpty('txtEmailTo')) {
        alert('You must enter the email address of the person you want to send this to');
        $('txtEmailTo').focus()
        return false;
    }

    if (bgnIsFieldEmpty('txtSubject')) {
        alert('You must enter a subject line');
        $('txtSubject').focus()
        return false;
    }

    return true;
}

function bgnEmailPage() {
    var who = '[email]';
    var what = 'This might be of interest to you: ' + document.title;
    var link = window.location.href;
    var body = document.title + '  \r\n' + link;
    parent.location.href = 'mailto:' + who + '?subject=' + what + '&body=' + body;
}

function bgnBookmark() {
    var urlAddress = window.location.href;
    var pageName = document.title;

    if (window.sidebar) {
        window.sidebar.addPanel(pageName, urlAddress, "");
    } else if (document.all) {
        window.external.AddFavorite(urlAddress, pageName);
    } else {
        alert("Sorry, your browser doesn't support this");
    }
}

function bgnAlphaRefine(letter) {
    if ($('startletter')) {
        $('startletter').value = letter;
        $('frmAlphaRefine').submit();
    }
}

function GetSearchAllCompanies(pub,sid) {
    var divid = 'sa_coys';
    
    new Ajax.Request(_appRoot + 'callbacks/SearchCompanies.aspx',
    {
        method: 'post',
        parameters: { pub: pub, sid: sid },
        onSuccess: function(transport) {
            DisplaySearchAllResults(transport.responseText, divid);
        },
        onFailure: function(transport) {
            DisplaySearchAllResults('<p>Error retrieving the search results</p>', divid);
        }
    });
}

function GetSearchAllFeaturedCompanies(pub, sid) {
    var divid = 'sa_flstngs';

    new Ajax.Request(_appRoot + 'callbacks/SearchCompanies.aspx',
    {
        method: 'post',
        parameters: { pub: pub, sid: sid, fl: '1' },
        onSuccess: function(transport) {
            DisplaySearchAllResults(transport.responseText, divid);
        },
        onFailure: function(transport) {
            DisplaySearchAllResults('<p>Error retrieving the search results</p>', divid);
        }
    });
}

function GetSearchAllProducts(pub, sid) {
    var divid = 'sa_prods';

    new Ajax.Request(_appRoot + 'callbacks/SearchProducts.aspx',
    {
        method: 'post',
        parameters: { pub: pub, sid: sid },
        onSuccess: function(transport) {
            DisplaySearchAllResults(transport.responseText, divid);
        },
        onFailure: function(transport) {
            DisplaySearchAllResults('<p>Error retrieving the search results</p>', divid);
        }
    });
}

function GetSearchAllCategories(pub, sid) {
    var divid = 'sa_cats';

    new Ajax.Request(_appRoot + 'callbacks/SearchCategories.aspx',
    {
        method: 'post',
        parameters: { pub: pub, sid: sid },
        onSuccess: function(transport) {
            DisplaySearchAllResults(transport.responseText, divid);
        },
        onFailure: function(transport) {
            DisplaySearchAllResults('<p>Error retrieving the search results</p>', divid);
        }
    });
}

function DisplaySearchAllResults(retval, divid) {
    if ($(divid)) {
        if (retval.length > 0) {
            $(divid).innerHTML = retval;
        }
        else {
            $(divid).innerHTML = '<br/>';
        }
    }
}


function GetSearchFiveCompanies(pub, sid)
{
	var divid = 's5_companies';

	new Ajax.Request(_appRoot + 'callbacks/SearchCompanies.aspx',
    {
    	method: 'post',
    	parameters: { pub: pub, sid: sid, t5: 1 },
    	onSuccess: function(transport)
    	{
    		DisplaySearchAllResults(transport.responseText, divid);
    	},
    	onFailure: function(transport)
    	{
    		DisplaySearchAllResults('<p>Error retrieving the search results</p>', divid);
    	}
    });
}


function GetSearchFiveProducts(pub, sid)
{
	var divid = 's5_products';

	new Ajax.Request(_appRoot + 'callbacks/SearchProducts.aspx',
    {
    	method: 'post',
    	parameters: { pub: pub, sid: sid, t5:1 },
    	onSuccess: function(transport)
    	{
    		DisplaySearchAllResults(transport.responseText, divid);
    	},
    	onFailure: function(transport)
    	{
    		DisplaySearchAllResults('<p>Error retrieving the search results</p>', divid);
    	}
    });
}

function GetSearchFiveCategories(pub, sid)
{
	var divid = 's5_categories';

	new Ajax.Request(_appRoot + 'callbacks/SearchCategories.aspx',
    {
    	method: 'post',
    	parameters: { pub: pub, sid: sid, t5: 1 },
    	onSuccess: function(transport)
    	{
    		DisplaySearchAllResults(transport.responseText, divid);
    	},
    	onFailure: function(transport)
    	{
    		DisplaySearchAllResults('<p>Error retrieving the search results</p>', divid);
    	}
    });
}

function bgnIsFieldEmpty(fieldName) {
    if ($(fieldName)) {
        if ($(fieldName).value.length == 0) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return true;
    }
   }