var extractPreviewLength = 70;

/* -----------------------------------------------------------------------------------------
*  FUNCTION ppathwindow() Open purchase path window
*                 Params: windowfile (.asp or .htm file to be  displayed in the new window)
*                         xsize (pixels)
*                         ysize (pixels)
*                   NOTE: Checks screen size of user and sets window size specifically.
*                         If no sizes are passed into function, defaults are used.
*-------------------------------------------------------------------------------------------*/
function ppathwindow(windowfile,xsize,ysize) {

    if(!windowfile) { 
        alert("ppathwindow routine was not passed a file to open. Aborting operation."); 
        return;
    }

    var Uscreenx, Uscreeny, Uwinx, Uwiny ;
        
    // -- Check user's screen size
    Uscreenx = screen.availWidth  ;
    Uscreeny = screen.availHeight ;
    
    // -- See if user has tiny screen (less than 800x600). If true, override function parameters 
    if(Uscreenx < 801 || Uscreeny < 601) {
        xsize = (Uscreenx - 160) ;
        ysize = (Uscreeny - 60) ;
    }
    
    // -- If no width or height provided on parameter line, or not set in code above due to small screen,
    // -- set to default sizes:
    if(!xsize || !ysize) {
        var docname = windowfile.toLowerCase();
        if(docname.indexOf('login.asp')        >-1) { xsize=550; ysize=500 } ;
        if(docname.indexOf('acctopen.asp')     >-1) { xsize=550; ysize=500 } ;
        if(docname.indexOf('purchase_confirm') >-1) { xsize=550; ysize=500 } ;
        if(docname.indexOf('purchase_collect') >-1) { xsize=550; ysize=500 } ;
    }
   
   // -- Open purchase path window. "ppathwin" is handle to that window.
   // -- See if this window is already open.  If so, just redirect its contents to the windowfile specified in the call!
   // -- This occurs if the purchase confirm page is called by an non-logged-in user, which calls ppathwindow (again) for login.asp.

   if(!opener) {
        // -- Open a purchase path window...none currently exists.
        window.name   = 'parentL1' ;
        var ppathwin  = window.open(windowfile,'ppathwindow', 'width=' + xsize + ',height=' + ysize + ',top=10,left=10,screenx=10,screeny=10,scrollbars=yes,resizable=yes,location=no,status=yes') ;
        ppathwin.name = 'ppathwindow'
   }
   else {
        // -- Redirect this already-open popup window to requested location!
        //alert("Window name: " + window.name + " Opener location is:" + opener.location + " Redirect destination is: " + windowfile)
        document.location.href = windowfile ;
  }
}


/* -----------------------------------------------------------------------------------------
*  FUNCTION reloadparent() Reload the window that spawned the current window, then
*                          Close the window (self) that called this routine
*-------------------------------------------------------------------------------------------*/
function reloadparent() {
    if(!opener) { 
        document.location = 'default.asp' ; 
        return;
    }
    opener.location.reload(true);
    self.close();
}


/* -----------------------------------------------------------------------------------------
*  FUNCTION redirectparent() Redirect the window that spawned the current window, then
*                            Close the window (self) that called this routine
*                   PARAMS:  destination (url)
*-------------------------------------------------------------------------------------------*/
function redirectparent(destination) {
    opener.location = destination ;
    self.close();
}


/* -----------------------------------------------------------------------------------------
*  FUNCTION checkppath()  
*           Ref is defined by calling program form
*           If Ref points to a purchase_confirm page or purchase_collect page, redirect this 
*           window itself to that destination and leave parent window alone.
*           If Ref points to any other page but purchase types, reload the parent 
*           if it exists and destroy this window.
*           If rdir=1 on URL querystring, set rdir variable as true and redirect parent
*           instead of reloading it!
*           If rdir<>1 or does not exist...default to 'false' and reload parent.
*-------------------------------------------------------------------------------------------*/
function checkppath() {

    var query     = location.search.substring(1).toLowerCase() ;
    var URLargs   = getURLargs() ;
    var test      = Ref.toLowerCase() ;
    Ref           = unescape(Ref) ;
    var rdir      = URLargs.rdir ;

    if(test.indexOf('purchase') > -1) {
        document.location = Ref ; 
    }
    else { 
        switch (rdir){
        case 'false':
            reloadparent() ;
            break;
        case 'true':
            redirectparent(Ref) ;
            break;
        case 'self':    
			document.location = Ref;
			break;
        default:
			if(!opener){
				document.location = 'default.asp' ; 
            }
            else{
				redirectparent(Ref) ;
			}       
            break;
        }
        
    }
}


/* -----------------------------------------------------------------------------------------
*  FUNCTION checkppath2()  
*           Updated version of checkppath() to not utilize popup windows.
*           Ref is defined by calling program form.
*-------------------------------------------------------------------------------------------*/
function checkppath2() {

    var query     = location.search.substring(1).toLowerCase() ;
    var URLargs   = getURLargs() ;
    var test      = Ref.toLowerCase() ;
    Ref           = unescape(Ref) ;

    document.location = Ref ; 
}


/*-------------------------------------------------------------------------------- 
* FUNCTION getURLargs() Get querystring argument name=value pairs from URL
*                       Return JS object with URL parameter pairs.
*                       PARAMS  if not passed a string, takes current querystring
*                       USAGE:  var URLargs = getURLargs()
*---------------------------------------------------------------------------------*/
function getURLargs(URL2parse){
    var URLargs = new Object();
    var query = (!URL2parse) ? location.search.substring(1) : URL2parse ;
    var pairs = query.split("&");                   // break at & separator
    for (var L=0; L<pairs.length; L++) {
        var pos = pairs[L].indexOf('=');            // look for "name=value"
        if (pos == -1) continue;                    // if not found, skip
        var argname = pairs[L].substring(0,pos) ;   // extract the "name" part
        var argvalue = pairs[L].substring(pos+1) ;  // extract the "value" part
        
        if (URLargs[argname]) {
            URLargs[argname] = URLargs[argname] + ',' + unescape(argvalue);
            // Object already exists, it is probably a multi-select input,
            // and we need to generate a separator-delimited string
            // by appending to what we already have stored.
        } 
        else {
            URLargs[argname] = unescape(argvalue);
            // Normal case: store value as object property
        }
        
    }
    return URLargs ;                                // return the args object
}


/* ------------------------------------------------------------------------------
*  FUNCTION loadStyle() - Allow different style sheets to be loaded based on browser
*                  Params: 
*                   Calls: 
*                 Returns: loaded stylesheet
*-------------------------------------------------------------------------------*/
function loadStyle (ieWin, navWin, ieMac, navMac) {
	if (is.ie) {
		if (is.mac) {
			document.write('<link rel="stylesheet" href="' + ieMac + '" type="text/css">');
			//alert('<link rel="stylesheet" href="' + ieMac + '" type="text/css">');
		} else {
			document.write('<link rel="stylesheet" href="' + ieWin + '" type="text/css">');
			//alert('<link rel="stylesheet" href="' + ieWin + '" type="text/css">');
		}
	} else if (is.ns) {
		if (is.mac) {
			document.write('<link rel="stylesheet" href="' + navMac + '" type="text/css">');
			//alert('<link rel="stylesheet" href="' + navMac + '" type="text/css">');
		} else {
			document.write('<link rel="stylesheet" href="' + navWin + '" type="text/css">');
			//alert('<link rel="stylesheet" href="' + navWin + '" type="text/css">');
		}
	} else {
		document.write('<link rel="stylesheet" href="' + ieWin + '" type="text/css">');
		//alert('<link rel="stylesheet" href="' + ieWin + '" type="text/css">');
	}
}


/* ------------------------------------------------------------------------------
*  FUNCTION BrowserCheck() - Determine user's browser specifications
*                  Params: none
*                 Returns: "is" object with properties
*-------------------------------------------------------------------------------*/
function BrowserCheck() {
	var b        = navigator.appName ;
	var agt      = navigator.userAgent.toLowerCase();
	this.version = navigator.appVersion
	this.moz4    = (agt.indexOf('mozilla/4')>0) ;
	this.v   = parseInt(this.version)
	if (b=="Netscape") this.b = "ns"
	if (b=="Microsoft Internet Explorer") this.b = "ie"
	this.ns  = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie  = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)    
    this.mac = (agt.indexOf("mac")!=-1);    //--- Mac code added by lance 11/19/99 
    var ver  = parseFloat(navigator.appVersion);
    
    // -- Set minimum browser boolean here!
    this.min = ((this.ns && (ver>=4)) || this.ie || this.moz4 ); // Note: IE doesn't report minor versions
    
    // -- DEBUG
        //alert('Browser Sniffer Diagnostic Dialog Box! ' + '\n\n' + 
                //'AppVersion = ' + navigator.appVersion + '\n\n' +
                //' this.b = ' + this.b + ' this.v = ' + this.v + ' this.ie = ' + this.ie + ' min= ' + this.min);
    // -- END DEBUG
}



/* ------------------------------------------------------------------------------
*  FUNCTION browversion() - Write string of browser name, version..etc.
*                  Params: none
*                 Returns: string of browser name, version...etc.
*-------------------------------------------------------------------------------*/
function browversion() {
    document.write('Name= <b>' + navigator.appName + '</b><br>Agent= <b>' + navigator.userAgent + '</b><br>Version= <b>' + navigator.appVersion + '</b>');
}

/* ------------------------------------------------------------------------------
*  FUNCTION resizeWindow() - resize window based on name passed
*                  Params: name of file or window type
*                          If it detects a small window (640x480..etc.) it resizes
*                          to that window's maximum size minus a buffer gap.
*                          NOTE:  purchconfirm and purchcollect are surrogate
*                                 names covering "_cbs, _s, _e" versions of each.
*-------------------------------------------------------------------------------*/

function resizeWindow(docname) {
    if(!docname) return ;
    docname = docname.toLowerCase() ;
    var Uscreenx, Uscreeny, Uwinx, Uwiny, Uwintop, Uwinleft ;
    Uscreenx = screen.availWidth ;
    Uscreeny = screen.availHeight ;
    
    // -- check window size values differently in NN versus IE due to DOM differences...
    Uwinx    = (window.innerWidth)  ? window.innerWidth  : document.body.offsetWidth  ;
    Uwiny    = (window.innerHeight) ? window.innerHeight : document.body.offsetHeight ;
    
    // -- check window location values differently in NN versus IE
    Uwinleft = (window.screenX) ? window.screenX : document.body.clientLeft ;    
    Uwintop  = (window.screenY) ? window.screenY : document.body.clientTop  ;

    // -- special case for homepage resize
    if(docname == 'default.asp') {
        window.resizeTo(Uscreenx - 100,Uscreeny - 200) ;
        return ;
    }
            
    // -- if window is already within desired size, don't bother resizing.
    // -- (solves problem with looping resize because of this function called by onLoad event in Netscape)
    
    // -- cancel-out of the resize if window is OK already!
    if(Uwinx<620 && Uwinx>560)    return ;
    if(Uwinleft<11 && Uwintop<11) return ;
        
    window.moveTo(10,10);
    
    if(Uscreenx < 801 || Uscreeny < 601) {
        window.resizeTo(Uscreenx - 50,Uscreeny - 55) ;
        return;
    }
    else {
        if(docname == 'login.asp'    && Uwinx<550) window.resizeTo(550,500) ;
        if(docname == 'acctopen.asp' && Uwinx<550) window.resizeTo(550,500) ;
        if(docname == 'purchconfirm' && Uwinx<550) window.resizeTo(550,500) ;
        if(docname == 'purchcollect' && Uwinx<550) window.resizeTo(550,500) ;
    }

}


function rediroldbrowser(){ 
    if(!is.min) { document.location.href='browserwarn.asp' ; } 
}

function getcookiedata(MBcookieName) {
    var allcookies = this.document.cookie ;
    // -- extract named cookie from collection
    var start = allcookies.indexOf(MBcookieName + '=');
    if (start == -1) return ;   // -- cookie not defined
    var end = allcookies.indexOf(';',start);
    if (end == -1) end = allcookies.length ;
    var cookieval = allcookies.substring(start,end);
    //alert('start=' + start + ' end=' + end + '\n' + allcookies + '\n' + cookieval);
    if (!cookieval) { var cookieval = '' ; }
    return cookieval ;
}



/*-------------------------------------------------------------------
*FUNCTION keydown.  This function will be used on all form pages.  On keydown event,
*if the user clicks return it will send them off to perform the action requested by 
*the particular form page being called.
*parameters (e) - this is the event being fired.
*--------------------------------------------------------------------*/
function keyDown(e) {
		var Key;
		
		if (is.ns) {
			Key = e.which;
			var s = new String(e.target)
			if (Key==13 && s.indexOf('input') != -1){	
				if(enterCMD != '') {eval(enterCMD);}
				else {return false ;}
			}
			
			
		} else if (is.ie) {
		    Key = event.keyCode;
			if ((Key==13) && (event.srcElement.tagName != 'A')){
				if(enterCMD != '') {eval(enterCMD);}
				else {return false ;}
			}
		}
}


/*----------------------------------------------------------------------
* FUNCTION CookiesAccepted()
* This routine tests for existence of cookie string in header.
* If no cookie string exists...browser must not accept them!
* RETURNS: "true" if cookie exists, "false" if no cookie string exits
*----------------------------------------------------------------------*/
function CookiesAccepted() {
    return (document.cookie.length > 0) ? 'true' : 'false' ;
}

/*------------------------------------------------------------------------
* FUNCTION NoCookieWarning()
* This routine displays an alert if cookie string is not found in header
*------------------------------------------------------------------------*/
function NoCookieWarning() {
    if(CookiesAccepted() == 'false') { alert('Sorry, it appears cookies are disabled on your browser.\nCookies must be enabled to successfully use this page.\nPlease enable the cookie option in your browser\'s settings.') ; }
}

function MM_openBrWindow(theURL,winName,features) { window.open(theURL,winName,features); }

function openMBsite(theURL) {
    if(!theURL) { return ; }
    var Uscreenx, Uscreeny ;
        
    // -- Check user's screen size
    Uscreenx = screen.availWidth  ;
    Uscreeny = screen.availHeight ;
    
    // -- See if user has tiny screen (less than 800x600). If true, use small window dimensions
    if(Uscreenx < 801 || Uscreeny < 601) {
        xsize = (Uscreenx - 40) ;
        ysize = (Uscreeny - 160) ;
    }
    else {
        xsize = 780 ;
        ysize = 550 ;
    }
    window.open(theURL,'mbcorporatesite','toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=' + xsize + ',height=' + ysize + ',top=10,left=10,screenx=10,screeny=10');  
}
// -----------------------END COMMON FUNCTIONS--------------------------



// -- Create the "is" object when this page loads...

var is = new BrowserCheck() ;