/*
    Script: popup.js

    Copyright: Fishnet NewMedia (c) 2003

    Create: 04.01.03

    Version: 1.0

    Compatibility: JavaScript 1.1

    Functions: none

    Changes: none

    Description:  

       This script is backwards-compatible for creating popup windows.
       Any version 3+ browser will support the javascript features used
       to create a new window with the parameters specified, unsupported
       browsers will create a new window as if target="_blank" were used.

       Opening a page in a new window:

           <A href="PAGENAME" target="popup:width=400,height=400,toolbar">LINK</A>

       Opening a page in an existing window:

           <A href="PAGENAME" target="popup:width=400,height=400,toolbar:WINDOWNAME">LINK</A>

       "WINDOWNAME" is that name of the target window,
       "LINK" is the linkable image or text content and
       "PAGENAME" is the name of the page to be loaded

       The script also contains NewPopUp() a function for manually calling
       a new popup window.  This script is useful for creating windows
       triggered by events, such as onload or onunload.

*/

// This function must be called from the onload for the AutoPopUp() function to operate
// This function traverses the page links and any link whose target contains the string 
// "popup" gets an onclick attribute whose action is the AutoPopUp() function.
function InitPopUp() {
    var link;
    for (var i = 0; (link = document.links[i]); i++) {
        if (link.target && link.target.indexOf("popup") == 0) link.onclick = AutoPopUp;
    }
}


// This function is called by the onclick event assigned to a link by the SetAction() function
// The function parses the caller link's target value extracting the window parameters and name
// values.  If no values are defined, a random number is assigned for the window name, and
// the window's parameters are assigned generic values.
function AutoPopUp() {
	// collect attributes
    var link = this.target.split(":");
    var parameters = link[1] ? link[1] : "width=516,height=450,toolbar=yes,scrollbars=yes,resizable=yes";
    var windowname = link[2] ? link[2] : Math.round(Math.random() * 1000000000);
    // open popup window
    var newWin = window.open(this.href, windowname, parameters);
    // set focus on the new window
    if (newWin) newWin.focus();
    // cancel links href action
    return false;
}

// This function is for manually creating a popup window using the "javascript:" pseudo-protocol 
// specifier to initiate the function from a link's href attribute value or using an event action
// such as onload, or onunload
function NewPopUp(URL,Name,w,h,a,t) {
    // collect attributes
    var winN = Name ? Name : Math.round(Math.random() * 1000000000);
    var winW = w ? w : 450;
    var winH = h ? h : 400;
    var aVal = a ? a : "yes";
    var tVal = t ? t : "yes";
    // open popup window
    var win = window.open(URL,winN,'width=' + winW + ',height=' + winH + ',resizable=' + aVal + ',scrollbars=' + aVal + ',toolbar=' + tVal);
    // set focus on the new window
    if (win) win.focus();
}
