//------------------------------------------
// Invision Power Board v2.0
// Global JS File
// (c) 2003 Invision Power Services, Inc.
//
// http://www.invisionboard.com
//------------------------------------------

//==========================================
// Set up
//==========================================

var input_red      = 'input-warn';
var input_green    = 'input-ok';
var input_ok_box   = 'input-ok-content';
var input_warn_box = 'input-warn-content';

var img_blank      = 'system/blank.gif';
var img_tick       = 'system/aff_tick.gif';
var img_cross      = 'system/aff_cross.gif';

// Sniffer based on http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html

var uagent    = navigator.userAgent.toLowerCase();
var is_safari = ( (uagent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var is_opera  = (uagent.indexOf('opera') != -1);
var is_webtv  = (uagent.indexOf('webtv') != -1);
var is_ie     = ( (uagent.indexOf('msie') != -1) && (!is_opera) && (!is_safari) && (!is_webtv) );
var is_ie4    = ( (is_ie) && (uagent.indexOf("msie 4.") != -1) );
var is_moz    = (navigator.product == 'Gecko');
var is_ns     = ( (uagent.indexOf('compatible') == -1) && (uagent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns4    = ( (is_ns) && (parseInt(navigator.appVersion) == 4) );
var is_kon    = (uagent.indexOf('konqueror') != -1);

var is_win    =  ( (uagent.indexOf("win") != -1) || (uagent.indexOf("16bit") !=- 1) );
var is_mac    = ( (uagent.indexOf("mac") != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var ua_vers   = parseInt(navigator.appVersion);

/*-------------------------------------------------------------------------*/
// INIT GD Image
/*-------------------------------------------------------------------------*/

function init_gd_image()
{
	var reg_img = document.getElementById('gd-antispam');
	
	try
	{
		reg_img.style.cursor = 'pointer';
	}
	catch(e)
	{
		reg_img.style.cursor = 'hand';
	};
	
	reg_img._ready  = 1;
	
	reg_img.onclick = do_change_img;
};

/*-------------------------------------------------------------------------*/
// Perform the image change act=captcha&amp;do=showimage&amp;captcha_unique_id=$captcha_unique_id
/*-------------------------------------------------------------------------*/

function captcha_image_change()
{
	var rc 	 = '';
	var act	 = 'Reg';
	var code = 'image';
	
	var qparts = reg_img.src.split("?");

	if ( qparts.length > 1 ) 
	{
		var qvars = qparts[1].split("&");

		for ( var i = 0 ; ( i < qvars.length ) ; i++ ) 
		{
			var qparts = qvars[i].split( "=" );
			
			if ( qparts[0] == 'captcha_unique_id' )
			{
				rc = qparts[1];
			};
			
			if ( qparts[0] == 'act' )
			{
				act = qparts[1];
			};
			
			if ( qparts[0] == 'do' )
			{
				code = qparts[1];
			};
		};
	};
	
	ipb_var_script_url = ipb_var_script_url.replace( new RegExp( "&amp;", "g" ) , '&' );

	var url = ipb_var_script_url + 'act=xmlout&do=change-gd-img&img=' + rc;
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			// Could do a little loading graphic here?
			return;
		}
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;

		reg_img.src = ipb_var_script_url + 'act=' + act + '&do=' + code + '&captcha_unique_id=' + html;
		
		document.getElementById( 'captcha-hidden-field' ).value = html;
	};
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( url );
};

/*-------------------------------------------------------------------------*/
// Create pop up links (So that JS browsers get pop-ups and non JS browsers don't
/*-------------------------------------------------------------------------*/

function create_pop_up_links()
{
	var href = document.getElementsByTagName( 'A' );
	var _len = href.length;
	
	if ( _len )
	{
		for ( var i = 0 ; i < _len ; i++ )
		{
			if ( href[i].id.match( /^cvg-pu-/ ) )
			{
				href[i].onclick = create_pop_up_links_mousedown;
			}
		}
	}
}

/*-------------------------------------------------------------------------*/
// Create pop up links (So that JS browsers get pop-ups and non JS browsers don't
/*-------------------------------------------------------------------------*/

function create_pop_up_links_mousedown( e )
{
	ipsclass.cancel_bubble(e, true);
	
	if ( this.id )
	{
		ipsclass.pop_up_window( document.getElementById( this.id ).href, 500, 600 );
		return false;
	}
}

/*-------------------------------------------------------------------------*/
// Fix IE PNG images
/*-------------------------------------------------------------------------*/

function ie_fix_png()
{
	if ( is_ie )
	{
		document.onreadystatechange = ie_fix_png_do;
	}
}

function ie_fix_png_do()
{
	if ( document.readyState == 'complete' )
	{
		var pos     = navigator.userAgent.indexOf("MSIE ");
		var version = navigator.userAgent.substring(pos + 5);
		var blanky  = ipb_skin_url + "/blank.gif";
	
		if (pos == -1)
		{
			return false;
		}
	
		if ( ! ((version.indexOf("5.5") == 0) || (version.indexOf("6") == 0)) && (navigator.platform == ("Win32")) )
		{
			return;
		}
	
		var images = document.getElementsByTagName( 'IMG' );
		var _len   = images.length;
	
		if ( _len )
		{
			for ( var i = 0 ; i < _len ; i++ )
			{
				if ( images[i].src.match( /\.png$/ ) )
				{
					var element = images[i];
					var _width  = 0;
					var _height = 0;
					var _src    = 0;
				
					if ( ! element.style.width )
					{
						_width = element.width;
					}

					if ( ! element.style.height )
					{
						_height = element.height;
					}
				
					_src        = element.src;
					element.src = blanky;

					if ( _width )
					{
						element.style.width  = _width+"px";
					}
					if ( _height )
					{
						element.style.height = _height+"px";
					}

					element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + _src + "',sizingMethod='scale')";
				}
			}
		}
	}
}

/*-------------------------------------------------------------------------*/
// Add shadow to an ID
/*-------------------------------------------------------------------------*/

function add_shadow( wrapname, divname )
{
	var divobj  = document.getElementById( divname );
	var wrapobj = document.getElementById( wrapname );
	
	//----------------------------------
	// Transform the DIV
	//----------------------------------
	
	if ( is_ie )
	{
		wrapobj.className      = 'shadow-ie';
		wrapobj.style.width    = divobj.offsetWidth  + 4 + 'px';
		wrapobj.style.height   = divobj.offsetHeight + 4 + 'px';
	}
	else
	{
		wrapobj.className      = 'shadow-moz';
		wrapobj.style.width    = divobj.offsetWidth  + 0 + 'px';
		wrapobj.style.height   = divobj.offsetHeight + 0 + 'px';
	}
}

/*-------------------------------------------------------------------------*/
// DST Auto correction
/*-------------------------------------------------------------------------*/

function global_dst_check( tzo, dst )
{
	var thisoffset = tzo + dst;
	var dstoffset  = new Date().getTimezoneOffset() / 60;
	var dstset     = 0;
	var url        = ipb_var_script_url + 'act=xmlout&do=dst-autocorrection&md5check=' + ipb_md5_check;
	
	if ( Math.abs( thisoffset + dstoffset ) == 1 )
	{
		try
		{
			/*--------------------------------------------*/
			// Main function to do on request
			// Must be defined first!!
			/*--------------------------------------------*/

			do_request_function = function()
			{
				//----------------------------------
				// Ignore unless we're ready to go
				//----------------------------------

				if ( ! xmlobj.readystate_ready_and_ok() )
				{
					// Could do a little loading graphic here?
					return;
				}

				//----------------------------------
				// INIT
				//----------------------------------

				var html = xmlobj.xmlhandler.responseText;

				if ( html == 'error' )
				{
					window.location = url.replace( '&xml=1', '&xml=0');
				}
			}

			//----------------------------------
			// LOAD XML
			//----------------------------------

			xmlobj = new ajax_request();
			xmlobj.onreadystatechange( do_request_function );

			xmlobj.process( url );
			xml_dst_set( url + '&xml=1' );
			dstset = 1;
		}
		catch(e)
		{
			dstset = 0;
		}
		
		//----------------------------------
		// No fancy?
		//----------------------------------
		
		if ( dstset == 0 )
		{
			window.location = url;
		}
	}
}

/*-------------------------------------------------------------------------*/
// Lang replace
/*-------------------------------------------------------------------------*/

function lang_build_string()
{
	if ( ! arguments.length || ! arguments )
	{
		return;
	}
	
	var string = arguments[0];
	
	for( var i = 1 ; i < arguments.length ; i++ )
	{
		var match  = new RegExp('<%' + i + '>', 'gi');
		string = string.replace( match, arguments[i] );
	}
	
	return string;
}


/*-------------------------------------------------------------------------*/
// CHOOSE SKIN
/*-------------------------------------------------------------------------*/

function chooseskin(obj)
{
	choosebox = obj.options[obj.selectedIndex].value;
	extravars = '';
	
	if ( choosebox != -1 && ! isNaN( choosebox ) )
	{
		if ( document.skinselectorbox.skinurlbits.value )
		{
			extravars = '&' + document.skinselectorbox.skinurlbits.value;
			
			//----------------------------------
			// Strip out old skin change stuff
			// setskin=1&skinid=2
			//----------------------------------
			
			extravars = extravars.replace( /set_lang=\d{1,}/g, ''  );
			extravars = extravars.replace( /set_style=\d{1,}/g, ''  );
			extravars = extravars.replace( /&{1,}/g         , '&' );
			extravars = extravars.replace( /s=&/g           , ''  );
		}
		
		window.location = ipb_var_script_url + 'set_style=' + choosebox + extravars;
	}
}


/*-------------------------------------------------------------------------*/
// CHOOSE LANG
/*-------------------------------------------------------------------------*/

function chooselang(obj)
{
	choosebox = obj.options[obj.selectedIndex].value;
	extravars = '';
	
	try
	{
		if ( document.langselectorbox.langurlbits.value )
		{
			extravars = '&' + document.langselectorbox.langurlbits.value;
			
			//----------------------------------
			// Strip out old skin change stuff
			// setskin=1&skinid=2
			//----------------------------------
			
			extravars = extravars.replace( /set_lang=\d{1,}/g, ''  );
			extravars = extravars.replace( /set_style=\d{1,}/g, ''  );
			extravars = extravars.replace( /&{1,}/g         , '&' );
			extravars = extravars.replace( /s=&/g           , ''  );
		}
	}
	catch( e )
	{
	}
	
	window.location = ipb_var_script_url + 'set_lang=' + choosebox + extravars;
}

/*-------------------------------------------------------------------------*/
// Write Calendar popup
/*-------------------------------------------------------------------------*/

function show_calendar( fieldid )
{
	formfield = my_getbyid( fieldid );
	
	cal = new calendar_us( formfield );
	cal.year_scroll = true;
	cal.time_comp   = false;
	cal.popup();
}



/*-------------------------------------------------------------------------*/
// Center a div on the screen
/*-------------------------------------------------------------------------*/

function center_div()
{
	this.divname = '';
	this.divobj  = '';
}

/*-------------------------------------------------------------------------*/
// Main run function
/*-------------------------------------------------------------------------*/

center_div.prototype.move_div = function()
{
	try
	{
		this.divobj = document.getElementById( this.divname );
	}
	catch(e)
	{
		return;
	}
	
	//----------------------------------
	// Figure width and height
	//----------------------------------
	
	var my_width  = 0;
	var my_height = 0;
	
	if ( typeof( window.innerWidth ) == 'number' )
	{
		//----------------------------------
		// Non IE
		//----------------------------------
	  
		my_width  = window.innerWidth;
		my_height = window.innerHeight;
	}
	else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		//----------------------------------
		// IE 6+
		//----------------------------------
		
		my_width  = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
	}
	else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		//----------------------------------
		// Old IE
		//----------------------------------
		
		my_width  = document.body.clientWidth;
		my_height = document.body.clientHeight;
	}
	
	//----------------------------------
	// Show (but behind the zIndex...
	//----------------------------------
	
	this.divobj.style.position = 'absolute';
	this.divobj.style.display  = 'block';
	this.divobj.style.zIndex   = -1;
	
	//----------------------------------
	// Get div height && width
	//----------------------------------
	
	var divheight = parseInt( this.divobj.style.Height );
	var divwidth  = parseInt( this.divobj.style.Width );
	
	divheight = divheight ? divheight : 200;
	divwidth  = divwidth  ? divwidth  : 400;
	
	//----------------------------------
	// Get current scroll offset
	//----------------------------------
	
	var scrolly = this.getYscroll();
	
	//----------------------------------
	// Finalize...
	//----------------------------------
	
	var setX = ( my_width  - divwidth  ) / 2;
	var setY = ( my_height - divheight ) / 2 + scrolly;
	
	setX = ( setX < 0 ) ? 0 : setX;
	setY = ( setY < 0 ) ? 0 : setY;
	
	this.divobj.style.left = setX + "px";
	this.divobj.style.top  = setY + "px";
	
	//----------------------------------
	// Show for real...
	//----------------------------------
	
	this.divobj.style.zIndex = 99;
}

/*-------------------------------------------------------------------------*/
// Hide div
/*-------------------------------------------------------------------------*/

center_div.prototype.hide_div = function()
{
	try
	{
		if ( ! this.divobj )
		{
			return;
		}
		else
		{
			this.divobj.style.display  = 'none';
		}
	}
	catch(e)
	{
		return;
	}
}

/*-------------------------------------------------------------------------*/
// Get YScroll
/*-------------------------------------------------------------------------*/

center_div.prototype.getYscroll = function()
{
	var scrollY = 0;
	
	if ( document.documentElement && document.documentElement.scrollTop )
	{
		scrollY = document.documentElement.scrollTop;
	}
	else if ( document.body && document.body.scrollTop )
	{
		scrollY = document.body.scrollTop;
	}
	else if ( window.pageYOffset )
	{
		scrollY = window.pageYOffset;
	}
	else if ( window.scrollY )
	{
		scrollY = window.scrollY;
	}
	
	return scrollY;
}
