/* ***** BEGIN LICENSE BLOCK *****
 * Licensed under Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * Full Terms at http://devedge.netscape.com/lib/js/license/mpl-tri-license.txt
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Netscape code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2001
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Bob Clary 
 *
 * ***** END LICENSE BLOCK ***** */

function xbDetectBrowser()
{
  var oldOnError = window.onerror;
  var element = null;

  window.onerror = null;
  
  // work around bug in xpcdom Mozilla 0.9.1
  window.saveNavigator = window.navigator;

  navigator.OS    = '';
  navigator.version  = parseFloat(navigator.appVersion);
  navigator.org    = '';
  navigator.family  = '';

  var platform;
  if (typeof(window.navigator.platform) != 'undefined')
  {
    platform = window.navigator.platform.toLowerCase();
    if (platform.indexOf('win') != -1)
      navigator.OS = 'win';
    else if (platform.indexOf('mac') != -1)
      navigator.OS = 'mac';
    else if (platform.indexOf('unix') != -1 || platform.indexOf('linux') != -1 || platform.indexOf('sun') != -1)
      navigator.OS = 'nix';
  }

  var i = 0;
  var ua = window.navigator.userAgent.toLowerCase();
  
  if (ua.indexOf('opera') != -1)
  {
    i = ua.indexOf('opera');
    navigator.family  = 'opera';
    navigator.org    = 'opera';
    navigator.version  = parseFloat('0' + ua.substr(i+6), 10);
  }
  else if ((i = ua.indexOf('msie')) != -1)
  {
    navigator.org    = 'microsoft';
    navigator.version  = parseFloat('0' + ua.substr(i+5), 10);
    
    if (navigator.version < 4)
      navigator.family = 'ie3';
    else
      navigator.family = 'ie4'
  }
  else if (ua.indexOf('gecko') != -1)
  {
    navigator.family = 'gecko';
    var rvStart = ua.indexOf('rv:');
    var rvEnd   = ua.indexOf(')', rvStart);
    var rv      = ua.substring(rvStart+3, rvEnd);
    var rvParts = rv.split('.');
    var rvValue = 0;
    var exp     = 1;

    for (var i = 0; i < rvParts.length; i++)
    {
      var val = parseInt(rvParts[i]);
      rvValue += val / exp;
      exp *= 100;
    }
    navigator.version = rvValue;

    if (ua.indexOf('netscape') != -1)
      navigator.org = 'netscape';
    else if (ua.indexOf('compuserve') != -1)
      navigator.org = 'compuserve';
    else
      navigator.org = 'mozilla';
  }
  else if ((ua.indexOf('mozilla') !=-1) && (ua.indexOf('spoofer')==-1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('opera')==-1)&& (ua.indexOf('webtv')==-1) && (ua.indexOf('hotjava')==-1))
  {
    var is_major = parseFloat(navigator.appVersion);
    
    if (is_major < 4)
      navigator.version = is_major;
    else
    {
      i = ua.lastIndexOf('/')
      navigator.version = parseFloat('0' + ua.substr(i+1), 10);
    }
    navigator.org = 'netscape';
    navigator.family = 'nn' + parseInt(navigator.appVersion);
  }
  else if ((i = ua.indexOf('aol')) != -1 )
  {
    // aol
    navigator.family  = 'aol';
    navigator.org    = 'aol';
    navigator.version  = parseFloat('0' + ua.substr(i+4), 10);
  }
  else if ((i = ua.indexOf('hotjava')) != -1 )
  {
    // hotjava
    navigator.family  = 'hotjava';
    navigator.org    = 'sun';
    navigator.version  = parseFloat(navigator.appVersion);
  }

  window.onerror = oldOnError;
}

xbDetectBrowser();

/**
 * Browser sniffing variables
 */

onerror=handleErr;

/**
 * Opens a New (non-scrolling) Window with the specified URL
 */
function openWindow(url, target, width, height, centerWindow) {
	var left, top;
	if (centerWindow) {
		left = (screen.width - width) / 2;
		top = (screen.height - height) / 2;
	} else {
		left = 0;
		top = 0;
	}
	var win = window.open(url, target, "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=" + width + ", height=" + height + ", top=" + top + ", left=" + left);
	if (win === null || typeof (win) == "undefined") {
		alert("It appears that you have pop up blocking for this website turned on.  To view the form, please turn off pop up blocking for this website.");
	}

	win.focus();
	
	return win;
}


/**
 * Opens a New Scrollable Window with the specified URL
 */
function openScrollableWindow(url, target, width, height, centerWindow) {
	var left, top, newwidth, newheight;

	//set dimensions to the minimum of provided numbers and the dimensions of user's screen minus a buffer or 100 pixels.
	newwidth = Math.min(screen.width - 50, width);
	newheight = Math.min(screen.height - 50, height);

	if (centerWindow) {
		left = Math.floor( (screen.width - newwidth) / 2 );
		top = Math.floor( (screen.height - newheight) / 2 );
	} else {
		left = 0;
		top = 0;
	}

	var opts = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=" + newwidth + ", height=" + newheight + ", top=" + top + ", left=" + left;

	var newWin = window.open(url, target, opts);

	newWin.focus();

	return newWin;
}

/**
 * Display the error
 * @param msg The error message
 * @param url The url of the page that threw the error
 * @param l The line number on that page
 */
function handleErr( msg, url, l ) {
	// Do nothing
}

/**
 * Checks to see if the pop up needs to be displayed before going to the link
 */
function openExternalLink( linkObject ) {
	if ( !linkObject ) {
		return;
	}

	var link = linkObject.href;
	if (link.indexOf("www.pokemon-tcg.com") >= 0 || link.indexOf("www.nintendo.com") >= 0 || link.indexOf("www.pokemon.com") >= 0 || link.indexOf("www.pokemoncenter.com") >= 0) {
		location.href = link;
	} else {
		popup = openWindow("/common/popup.html?url=" + link, "_blank", 450, 200, true);
	}
	return false;
}


/**
 * Parse the request's query string
 */
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

/**
 * Get a query parameter value
 */
function Querystring_get( key, default_ ) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}

/**
 * Set a cookie value
 */
function setCookie( name, value, exp_y, exp_m, exp_d, path, domain, secure ) {

	var cookie_string = name + "=" + escape ( value );

	if ( exp_y ) {
		var expires = new Date ( exp_y, exp_m, exp_d );
		cookie_string += "; expires=" + expires.toGMTString();
	}

	if ( path ) {
		cookie_string += "; path=" + escape ( path );
	}

	if ( domain ) {
		cookie_string += "; domain=" + escape ( domain );
	}
	
	if ( secure ) {
		cookie_string += "; secure";
	}
	
	document.cookie = cookie_string;
}

/**
 * Get a cookie value
 */
function getCookie( cookie_name ) {

	var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

	if ( results ) {
		return unescape( results[1] );
	}
	else {
		return "";
	}
}