﻿/* ************************************** */
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 (document.getElementById('search_options')) {
        if (document.getElementById('search_options').style.display == 'none') {
            document.getElementById('search_options').style.display = 'block';
            document.getElementById('advanced_link').className = 'advanced_show';
        }
        else {
            document.getElementById('search_options').style.display = 'none';
            document.getElementById('advanced_link').className = 'advanced_hide';
        }
    }
}

function bgnCheckSearchTerm() {
    if (document.getElementById('search_term')) {

        if (document.getElementById('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 (document.getElementById('coymain')) { document.getElementById('coymain').style.display = 'none'; }
    if (document.getElementById('coycomments')) { document.getElementById('coycomments').style.display = 'block'; }
}

function bgnShowCoyMain() {
    if (document.getElementById('coymain')) { document.getElementById('coymain').style.display = 'block'; }
    if (document.getElementById('coycomments')) { document.getElementById('coycomments').style.display = 'none'; }
}

function bgnShowProdComments() {
    if (document.getElementById('prodmain')) { document.getElementById('prodmain').style.display = 'none'; }
    if (document.getElementById('prodcomments')) { document.getElementById('prodcomments').style.display = 'block'; }
}

function bgnShowProdMain() {
    if (document.getElementById('prodmain')) { document.getElementById('prodmain').style.display = 'block'; }
    if (document.getElementById('prodcomments')) { document.getElementById('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 (document.getElementById('RssContent')) { document.getElementById('RssContent').innerHTML = retVal; }
}

function bgnShowMoreRssLinks()
{
    if(document.getElementById('rss_more')){
        document.getElementById('rss_more').style.display = 'block';
    }

    if(document.getElementById('rss_more_link')) {
        document.getElementById('rss_more_link').style.display = 'none';
    }
}

function bgnShowLessRssLinks()
{
    if(document.getElementById('rss_more')){
        document.getElementById('rss_more').style.display = 'none';
    }

    if(document.getElementById('rss_more_link')) {
        document.getElementById('rss_more_link').style.display = 'block';
    }
}

function bgnShowChildCats(id) {
    if (document.getElementById('catchild' + id)) {
        if ('catchild' == document.getElementById('catchild' + id).className) {
            document.getElementById('catchild' + id).className = 'catchildhide';

            if (document.getElementById('imgcat' + id)) {
                document.getElementById('imgcat' + id).src = _imgRoot + 'plus.gif';
            }
            
            //document.getElementById('hlcatexp' + id).className = 'cat-expand';
        }
        else {
            document.getElementById('catchild' + id).className = 'catchild';

            if (document.getElementById('imgcat' + id)) {
                document.getElementById('imgcat' + id).src = _imgRoot + 'minus.gif';
            }
            
            //document.getElementById('hlcatexp' + id).className = 'cat-collapse';
        }
    }
}

function updatePageLength() {
    var url = location.href.substring(0, location.href.indexOf('?'));    
    location.href = url + '?rpp=' + document.getElementById('rpp').options[document.getElementById('rpp').selectedIndex].value;
}

function updatePageLengthB() {
    var url = location.href.substring(0, location.href.indexOf('?'));
    location.href = url + '?rpp=' + document.getElementById('rppb').options[document.getElementById('rppb').selectedIndex].value;
}

function bgnUpdatePageLength(obj) {
    var page_length = 0;

    if (obj) { page_length = obj.options[obj.selectedIndex].value; }

    if (page_length > 0) {
        var callbackFile = _appRoot + 'callbacks/UpdatePageLength.aspx?val=' + page_length;

        new Ajax.Request(callbackFile, {
            method: 'post',
            parameters: { rpp: page_length },
            onSuccess: function(transport) {
            bgnUpdatePageLengthSuccess(transport.responseText, page_length);
            },
            onFailure: function(transport) {
                alert('Error setting the default page length');
            }
        });
    }
}

function bgnUpdatePageLengthBottom() {
    var page_length = 0;

    if (document.getElementById('rppb')) {
        page_length = document.getElementById('rppb').options[document.getElementById('rppb').selectedIndex].value;        
    }

    if (page_length > 0) {
        var callbackFile = _appRoot + 'callbacks/UpdatePageLength.aspx?val=' + page_length;

        new Ajax.Request(callbackFile, {
            method: 'post',
            parameters: { rpp: page_length },
            onSuccess: function(transport) {
            bgnUpdatePageLengthSuccess(transport.responseText, page_length);
            },
            onFailure: function(transport) {
                alert('Error setting the default page length');
            }
        });
    }
}

function bgnUpdatePageLengthSuccess(retval, val) {
    if ('success' == retval) {
        location.reload(true);
        var btm_ddl = document.getElementById('rppb');
        var top_ddl = document.getElementById('rpp');
        for (var i = 0; i < btm_ddl.options.length; i++) {
            if (btm_ddl.options[i].value == val) {
                btm_ddl.options[i].selected = true;
                top_ddl.options[i].selected = 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');
        document.getElementById('txtEmailFromName').focus()
        return false;
    }

    if (bgnIsFieldEmpty('txtEmailFrom')) {
        alert('Please enter your email address');
        document.getElementById('txtEmailFrom').focus()
        return false;
    }
    
    if (bgnIsFieldEmpty('txtEmailTo')) {
        alert('You must enter the email address of the person you want to send this to');
        document.getElementById('txtEmailTo').focus()
        return false;
    }

    if (bgnIsFieldEmpty('txtSubject')) {
        alert('You must enter a subject line');
        document.getElementById('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 (document.getElementById('startletter')) {
        document.getElementById('startletter').value = letter;
        document.getElementById('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 GetSearchAllCategoryCompanies(pub, srt, cat, sid) {
    var divid = 'sa_coys';

    new Ajax.Request(_appRoot + 'callbacks/SearchCompanies.aspx',
    {
        method: 'post',
        parameters: { pub: pub, srt: srt, cat: cat, 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 = 'ctl00_cphMain_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 GetSearchAllCategoryProducts(pub, srt, cat, sid) {
    var divid = 'ctl00_cphMain_sa_prods';

    new Ajax.Request(_appRoot + 'callbacks/SearchProducts.aspx',
    {
        method: 'post',
        parameters: { pub: pub, srt: srt, cat: cat, 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 (document.getElementById(divid)) {
        if (retval.length > 0) {
            document.getElementById(divid).innerHTML = retval;
        }
        else {
            document.getElementById(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 GetSearchCategoryFiveCompanies(pub, srt, cat, sid) 
{
    var divid = 's5_companies';

    new Ajax.Request(_appRoot + 'callbacks/SearchCompanies.aspx',
    {
        method: 'post',
        parameters: { pub: pub, srt: srt, cat: cat, 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 GetSearchCategoryFiveProducts(pub, srt, cat, sid) {
    var divid = 's5_products';

    new Ajax.Request(_appRoot + 'callbacks/SearchProducts.aspx',
    {
        method: 'post',
        parameters: { pub: pub, srt: srt, cat: cat, 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 (document.getElementById(fieldName)) {
        if (document.getElementById(fieldName).value.length == 0) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return true;
    }
}

function openProductVideoPopup(path) {
    //alert(path);
    contactWindow = window.open(path, "Product Video", "toolbar=0, status=0, menubar=0, width=450, height=450, scrollbars=0");
    contactWindow.focus();
}


var dragapproved=false
var minrestore=0
var initialwidth,initialheight
var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all

function iecompattest() {
    return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body
}

function drag_drop(e) {
    if (ie5 && dragapproved && event.button == 1) {
        document.getElementById("dwindow").style.left = tempx + event.clientX - offsetx + "px"
        document.getElementById("dwindow").style.top = tempy + event.clientY - offsety + "px"
    }
    else if (ns6 && dragapproved) {
        document.getElementById("dwindow").style.left = tempx + e.clientX - offsetx + "px"
        document.getElementById("dwindow").style.top = tempy + e.clientY - offsety + "px"
    }
}

function initializedrag(e) {
    offsetx = ie5 ? event.clientX : e.clientX
    offsety = ie5 ? event.clientY : e.clientY
    document.getElementById("dwindowcontent").style.display = "none" //extra
    tempx = parseInt(document.getElementById("dwindow").style.left)
    tempy = parseInt(document.getElementById("dwindow").style.top)

    dragapproved = true
    document.getElementById("dwindow").onmousemove = drag_drop
}

function loadwindow(url, width, height) {
    if (!ie5 && !ns6)
        window.open(url, "", "width=width,height=height,scrollbars=1")
    else {
        document.getElementById("dwindow").style.display = ''
        document.getElementById("dwindow").style.width = initialwidth = width + "px"
        document.getElementById("dwindow").style.height = initialheight = height + 25 + "px"
        document.getElementById("dwindow").style.left = "30%"
        document.getElementById("dwindow").style.top = ns6 ? window.pageYOffset * 1 + 30 + "px" : iecompattest().scrollTop * 1 + 30 + "px"
        document.getElementById("cframe").src = url
    }
}

function maximize() {
    if (minrestore == 0) {
        minrestore = 1 //maximize window
        document.getElementById("maxname").setAttribute("src", "restore.gif")
        document.getElementById("dwindow").style.width = ns6 ? window.innerWidth - 20 + "px" : iecompattest().clientWidth + "px"
        document.getElementById("dwindow").style.height = ns6 ? window.innerHeight - 20 + "px" : iecompattest().clientHeight + "px"
    }
    else {
        minrestore = 0 //restore window
        document.getElementById("maxname").setAttribute("src", "max.gif")
        document.getElementById("dwindow").style.width = initialwidth
        document.getElementById("dwindow").style.height = initialheight
    }
    document.getElementById("dwindow").style.left = ns6 ? window.pageXOffset + "px" : iecompattest().scrollLeft + "px"
    document.getElementById("dwindow").style.top = ns6 ? window.pageYOffset + "px" : iecompattest().scrollTop + "px"
}

function closeit() {
    document.getElementById("dwindow").style.display = "none"
}

function stopdrag() {
    dragapproved = false;
    document.getElementById("dwindow").onmousemove = null;
    document.getElementById("dwindowcontent").style.display = "" //extra
}
