var current_cms = null;
var is_on_home_page = false;
var the_page;
var override_popup = false;
var do_not_center = false;

var curr_event;
var do_not_fade = true;

var fade_limit = 3;

window.popdown_cb = noop;
window.popup_cb = noop;

var popup_div = "popup";

function FadeObject(id)
{
	this.id = id;
	this.s = document.getElementById(id);
	
	this.fade_in = _obj_fade_in;
	this.fade_out = _obj_fade_out;

	this._fade_in = obj_fade_in;
	this._fade_out = obj_fade_out;

	this.fade_interval = null;
}

function _obj_fade_in()
{
	var obj = this;
	this.fade_interval = window.setInterval(function(event) { obj._fade_in(); }, 100);
}

function _obj_fade_out()
{
	var obj = this;
	this.fade_interval = window.setInterval(function(event) { obj._fade_out(); }, 100);
}

function obj_fade_out()
{
	var b = this.s;
	
	var fade_limit = 0;
	
	
	if ( b != null )
	{
		if ( b.style.opacity == "" ) b.style.opacity = "1";
		if ( document.all && b.style.filter == "" ) b.style.filter = "alpha(opacity=100)";

		if ( ( document.all && b.style.filter == "alpha(opacity=" +(fade_limit*10)+ ")" ) || ( !document.all && b.style.opacity == fade_limit/10 ) )
		{
			clearInterval(this.fade_interval);
		}
		else
		{
			if ( document.all )
			{
				(b.style.filter+ "").match(/alpha\(opacity=(\d+)\)/);
				var i = parseInt(RegExp.$1);
				i -= 10;
				
				b.style.filter = "alpha(opacity=" +i+ ")";
			}
			else
			{
				var i = b.style.opacity;
				b.style.opacity = parseFloat(i) - 0.10;
			}
		}	
	}	
}

function obj_fade_in()
{
	var b = this.s;
	
	if ( b != null )
	{
		if ( b.style.opacity == "" ) b.style.opacity = "0.3";
		if ( document.all && b.style.filter == "" ) b.style.filter = "alpha(opacity=30)";
	
		if ( ( document.all && b.style.filter == "alpha(opacity=100)" ) || ( !document.all && b.style.opacity == "1" ) )
		{
			clearInterval(this.fade_interval);
		}
		else
		{
			if ( document.all )
			{
				(b.style.filter+ "").match(/alpha\(opacity=(\d+)\)/);
				var i = parseInt(RegExp.$1);
				i += 10;
				
				b.style.filter = "alpha(opacity=" +i+ ")";
			}
			else
			{
				var i = b.style.opacity;
				b.style.opacity = parseFloat(i) + 0.10;
			}
		}	
	}	
}


function popup(msg, r, c, h)
{
	var has_close = false;

	if ( current_cms != null && c )
	{
		msg = eval(current_cms+ "_popup_close(msg)");
		has_close = true;
	}

	popup_cb();

	fade_object_id2 = fade_object_id;
	fade_object_id = null;
	fade_cb = null;
	
	if ( popup_div == null ) popup_div = "popup";
	
	var d = document.getElementById(popup_div);
	
	var b = document.getElementById("main_table");
	
	do_not_fade = true;
	
	if ( !do_not_fade )
	{	
		fade_interval = setInterval(fade_out, 1);
	}
		
	if ( d != null )
	{
		d.style.filter = "alpha(opacity=100)";
		d.style.opacity = "1";
		if ( b != null ) b.scrollIntoView();
	
		
		
		if ( h != null )
		{
			d.style.height = h;
		}
		else
		{
			d.style.height = "";
		}
	
		msg = "<span style='z-index: 10;'>" +msg+ "</span>";
	
		d.innerHTML = msg;
		
		if ( !do_not_center ) center_somthing(d);

		toggle(popup_div, curr_event, false, false);
	}
	
	if ( r )
	{
		window.setTimeout(_delay_hide, 3500);
	}
}

function center_something(d)
{
	center_somthing(d);
}

function center_somthing(d)
{

	align_top(d);
	align_left(d)
}

function align_top(d)
{

	var h;
	if ( self.innerHeight )
	{
		h = self.innerHeight+ "";	
	}
	else if ( document.documentElement && document.documentElement.clientHeight )
	{
		h = document.documentElement.clientHeight+ "";
	}
	else
	{
		h = document.body.clientHeight+ "";
	}
	
	if ( !h.match(/^\d+$/) )
	{
		h.match(/(^\d+)/);
		h = RegExp.$1;
	}
	
	var h_ = d.style.height+ "";
			
	if ( !h_.match(/^\d+$/) )
	{
		h_.match(/(^\d+)/);
		h_ = RegExp.$1;
	}		
	
	var p = d.style.padding;
	
	if ( !p.match(/^\d+$/) )
	{
		p.match(/(^\d+)/);
		p = RegExp.$1;
	}		
	
	d.style.top = (h / 2) - (h_ / 2) + ( (document.all) ? 0 : "px" );
	
	if ( !document.all )
	{		
		d.style.top -= h;
	}

}

function align_left(d)
{

	var w;

	if ( self.innerWidth )
	{
		w = self.innerWidth+ "";	
	}
	else if ( document.documentElement && document.documentElement.clientWidth )
	{
		w = document.documentElement.clientWidth+ "";
	}
	else
	{
		w = document.body.clientWidth+ "";
	}
	
	if ( !w.match(/^\d+$/) )
	{
		w.match(/(^\d+)/);
		w = RegExp.$1;
	}
	
	w -= ( document.all ) ? 20 : 35; // stupid scrollbars
	
	var w_ = d.style.width+ "";

	if ( !w_.match(/^\d+$/) )
	{
		w_.match(/(^\d+)/);
		w_ = RegExp.$1;
	}		
	
	p = d.style.padding;
	
	if ( !p.match(/^\d+$/) )
	{
		p.match(/(^\d+)/);
		p = RegExp.$1;
	}		

	d.style.left = (w / 2) - (w_ / 2)+ "px";
	
	//alert((w / 2) - (w_ / 2));
	
	if ( !document.all )
	{		
		var l = d.style.left;
		
		if ( !l.match(/^\d+$/) )
		{
			l.match(/(^\d+)/);
			l = RegExp.$1;
		}		
		
		d.style.left = parseInt(l) + parseInt(p/4);
	}


}

var fade_interval;
var fade_object_id;
var fade_object_id2;
var fade_cb;

function fade_out()
{
	id = ( fade_object_id == null ) ? "main_table" : fade_object_id;
	
	var b = document.getElementById(id);
	
	fade_limit = ( isNaN(fade_limit) ) ? 3 : fade_limit;
	
	if ( b != null )
	{
		if ( b.style.opacity == "" ) b.style.opacity = "1";
		if ( document.all && b.style.filter == "" ) b.style.filter = "alpha(opacity=100)";

		if ( ( document.all && b.style.filter == "alpha(opacity=" +(fade_limit*10)+ ")" ) || ( !document.all && b.style.opacity == fade_limit/10 ) )
		{
			clearInterval(fade_interval);
			
			if ( fade_cb != null )
			{
				fade_interval = window.setInterval(fade_in, 50);
			}
			else
			{
				fade_object_id = null;
				fade_limit = 3;
			}
		}
		else
		{
			if ( document.all )
			{
				(b.style.filter+ "").match(/alpha\(opacity=(\d+)\)/);
				var i = parseInt(RegExp.$1);
				i -= 10;
				
				b.style.filter = "alpha(opacity=" +i+ ")";
			}
			else
			{
				var i = b.style.opacity;
				b.style.opacity = parseFloat(i) - 0.10;
			}
		}	
	}	
}

function fade_in()
{
	id = ( fade_object_id == null ) ? "main_table" : fade_object_id;
	
	var b = document.getElementById(id);
	
	if ( b != null )
	{
		if ( b.style.opacity == "" ) b.style.opacity = "0.3";
		if ( document.all && b.style.filter == "" ) b.style.filter = "alpha(opacity=30)";
	
		if ( ( document.all && b.style.filter == "alpha(opacity=100)" ) || ( !document.all && b.style.opacity == "1" ) )
		{
			clearInterval(fade_interval);
			
			if ( fade_object_id2 != null ) 
			{
				fade_object_id = fade_object_id2;
				_restore(fade_object_id);
				fade_cb = true;
				fade_object_id2 = null;
			}
			
			if ( fade_cb != null )
			{
				fade_interval = window.setInterval(fade_out, 50);
			}
			else
			{
				fade_object_id = null;
			}
			
			popdown_cb();

		}
		else
		{
			if ( document.all )
			{
				(b.style.filter+ "").match(/alpha\(opacity=(\d+)\)/);
				var i = parseInt(RegExp.$1);
				i += 10;
				
				b.style.filter = "alpha(opacity=" +i+ ")";
			}
			else
			{
				var i = b.style.opacity;
				b.style.opacity = parseFloat(i) + 0.10;
			}
		}	
	}	
}

function popdown()
{
	if ( popup_div == null ) popup_div = "popup";

	var d = document.getElementById(popup_div);

	if ( !do_not_fade )
	{
		fade_interval = setInterval(fade_in, 1);
	}
	else
	{
		popdown_cb();
	}
	
	hide_something(popup_div);
	
	popup_div = null;

	for ( var i=0; i<flash_objects_to_hide.length; i++ )
	{
		var tmp = document.getElementById(flash_objects_to_hide[i]);
		if ( tmp != null )
		{
			tmp.style.visibility = "visible";
		}
	}

}


function _delay_hide()
{
	popdown();
}


function open_window(theURL,winName,features) 
{
	var win = window.open(theURL,winName,features);
	win.focus();
	win.opener = this;
}

function toggle(id, e, pos, l)
{
	var d = document.getElementById(id);
	
	if ( d != null )
	{
		d.style.display = ( d.style.display == "block" ) ? "none" : "block";
	}
	
	if ( d != null && pos )
	{				
		var w = d.style.width+ "";
		w = w.replace(/px$/, "");
	
		if ( document.all )
		{
			d.style.top = (document.body.scrollTop + currY + 5) + "px";
			d.style.left = ( l  ? (currX - w) : (currX + 5) ) + "px";		
		}
		else
		{
			d.style.top = (posTop(e) + 15) + "px";
			d.style.left = ( l ? (posLeft(e) - w) : (posLeft(e) + 5)  ) + "px";
		}
	}
	
	if ( d != null && !pos )
	{
		if ( !do_not_center ) center_somthing(d);
	}
}

function hide_something(id)
{
	var d = document.getElementById(id);
	
	if ( d != null )
	{
		d.style.display = "none";
	}				
}


var timers = new Array();
function show_something(id, h, t)
{
	var d = document.getElementById(id);
	
	if ( d != null )
	{
		d.style.display = "block";
	}				
	
	if ( h )
	{
		timers.push(id);
		window.setTimeout(hide_timers, t);
	}
}

function hide_timers()
{
	for ( var i=0; i<timers.length; i++ )
	{
		hide_something(timers[i]);
	}
	timers = new Array();
}

function posTop(elem)
{
	var x=-1;
	while(elem)
	{		
		x+=elem.offsetTop;
		elem=elem.offsetParent;
	} 
	
	return x;
}		

function posLeft(elem)
{
	var x=-1;
	while(elem)
	{		
		x+=elem.offsetLeft;
		elem=elem.offsetParent;
	} 
	
	return x;
}


function validate(form)
{
	var s = true;
	var msg = "Please correct the following errors:\n\n";
	
	for ( var i=0; i<form_validators.length; i++ )
	{
		var tmp = form_validators[i];
		var f = form.elements[tmp.name];
		
		if ( f.type.toLowerCase().indexOf("select") > -1 )
		{
			
			if ( f.selectedIndex == 0 )
			{
				s = false;
				msg += " - " +tmp.msg+ "\n";		
			}
		}
		else if ( f.type.toLowerCase() == "text" )
		{		
			if ( f.value.length == 0 )
			{
				s = false;
				msg += " - " +tmp.msg+ "\n";		
			}
		}
	}
	
	if ( !s )
	{
		alert(msg);
	}
	
	return ( s );
}


function RequiredField(a, b)
{
	this.name = a;
	this.msg = b;
}

function Page()
{
	this.props = new Array();
}




function cms_resize_images(e, cols)
{
	var content_td = document.getElementById(e);
	var w = content_td.width;

	w = w.replace(/px/, "");

	var max_width = w / cols;
	
	max_width *= .9 - ( (cols - 1) * 4 / 100 );
	
	var images = content_td.getElementsByTagName("img");
	
	for ( var i=0; i<images.length; i++ )
	{
		var img = images[i];
		if ( (img.id+ "").match(/image_/) )
		{
			var _w = img.width;
			var _h = img.height;
			
			if ( _w > max_width )
			{			
				img.width = max_width;
				
				var _r = _h / _w;
				
				img.height = max_width * _r;
			}
		}
	}

}

function show_tr(id)
{
	var d = document.getElementById(id);
	
	if ( d != null )
	{
		d.style.display = ( document.all ) ? "block" : "table-row";
	}				
}



function noop()
{
	return ( false );
}


function fade(b)
{
	b.style.opacity = "0.5";
	b.style.opacity = "alpha(opacity=50)";

}

function restore(b)
{
	b.style.opacity = "1.0";
	b.style.filter = "alpha(opacity=100)";
}

function _restore(i)
{
	var b = document.getElementById(i);
	if ( b != null )
	{
		restore(b);
	}
}

function unrestore(b)
{
	b.style.opacity = "0.0";
	b.style.filter = "alpha(opacity=0)";
}

function _unrestore(i)
{
	var b = document.getElementById(i);
	if ( b != null )
	{
		unrestore(b);
	}
}
