var qs = document.location.href;

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

var jquery_fade = false;
var jquery_fx = false;
var jquery_expose = false;
var expose_bg_color = "black";
var fade_value = 0.7;

var form_validators = new Array();


function number_of_cols()
{
	return ( 0 );
}

function load_link(uri, type, target)
{

	//alert(current_cms);
	if ( type == "static" )
	{
		window.open('/cgi-bin/static.cgi?cms=' +current_cms+ '&' +uri, target);
	}
	else if ( type == 'dynamic' )
	{
		window.open('/cgi-bin/dynamic.cgi?cms=' +current_cms+ '&' +uri, target);
	}
	else if ( type == 'external' )
	{
		if ( uri.match(/^javascript:(.+);/) )
		{
			document.location.href = uri;
		}
		else
		{	
			window.open(uri, target);
		}
	}
	else if ( type == "file" )
	{
		window.open(uri, '_new');
	}
}

var curr_event;
var do_not_fade = true;

var fade_limit = 3;

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

var popup_div = "popup";

var flash_objects_to_hide = new Array();

var inline_edit_win;

function edit_element_inline(type, uid, pwd)
{	
	if ( current_cms == null ) return;
	
	if ( type == "text" )
	{
		if ( pwd == null )
		{
			pwd = prompt("You are trying to edit a content section. In order to continue, please enter the admin email and password, eg. a@b.co.za:123");
		}
		
		if ( pwd != null )
		{
			document.cookie = "admin_cms=" +current_cms;
			
			if ( pwd.match(/^([^:]+):(\w+)$/) )
			//inline_edit_win = window.open("/blank.html", "inline_edit_win", "width=800, height=600, scrollbars=auto, menubar=no, location=no");
				
			window.open("/cgi-bin/scripts/user_login.cgi?inline=1&inline_element_uid=" +uid+ "&email=" +RegExp.$1+ "&password=" +RegExp.$2, "_inline_edit_win", "width=800, height=600, scrollbars=auto, menubar=no, location=no");
		}
	}
}

function column_delete(e, uid)
{
	curr_event = e;
	var p = confirm("Are you sure you want to delete this column and all its elements?");
	
	if ( p )
	{
		window.open('/cgi-bin/scripts/column_delete.cgi?uid=' +uid, '_postman');
	}	
}

function column_add(e, uid, position)
{
	curr_event = e;
	window.open('/cgi-bin/scripts/column_add.cgi?uid=' +uid+ "&position=" +position, '_postman');
}


function element_delete(e, uid, type, f)
{
	curr_event = e;
	var p = true;

	if ( !f )
	{
		p = confirm("Are you sure you want to delete this element?");
	}
	
	if ( p )
	{
		window.open('/cgi-bin/scripts/element_delete.cgi?uid=' +uid+ "&type=" +type, '_postman');
	}
}


function element_edit(e, uid, type)
{
	open_window('/cgi-bin/element_edit.cgi?uid=' +uid+ "&element_type=" +type, '_edit', 'width=600, height=500, scrollbars=yes');
}
function element_visibility(e, uid, type, v)
{
	open_window('/cgi-bin/scripts/element_visibility.cgi?uid=' +uid+ "&element_type=" +type+ "&v=" +v, '_postman');
}
function element_order(e, parent)
{
	open_window('/cgi-bin/element_order.cgi?parent_uid=' +parent, '_edit', 'width=600, height=500, scrollbars=yes');
}

function element_add(e, pos, parent, type)
{
	open_window('/cgi-bin/element_add.cgi?position=' +pos+ "&parent_uid=" +parent+ "&type=" +type, '_edit', 'width=600, height=500,, scrollbars=yes');
}
function element_duplicate(e, uid, type)
{
	open_window('/cgi-bin/element_duplicate.cgi?uid=' +uid+ "&type=" +type, '_edit', 'width=600, height=500, scrollbars=yes');
}



// Drag start

function Browser() {

	var ua, s, i;
	
	this.isIE = false;
	this.isNS = false;
	this.version = null;
	
	ua = navigator.userAgent;
	
	s = "MSIE";
	if ( (i = ua.indexOf(s) ) >= 0 ) 
	{
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	
	s = "Netscape6/";
	if ( (i = ua.indexOf(s) ) >= 0 ) 
	{
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	
	// Treat any other "Gecko" browser as NS 6.1.
	
	s = "Gecko";
	if ( (i = ua.indexOf(s)) >= 0 ) 
	{
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}

var browser = new Browser();

var dragObj = new Object();
dragObj.zIndex = 0;


function dragStart(event, id) 
{
	var el;
	var x, y;
	
	// If an element id was given, find it. Otherwise use the element being
	// clicked on.
	
	
	if ( browser.isIE ) return;
	
	if (false)
	{
		dragObj.elNode = document.getElementById(id);
	}
	else
	{
		if (browser.isIE)
		{
			dragObj.elNode = window.event.srcElement;
		}
		if (browser.isNS)
		{
	  		dragObj.elNode = event.target;
		}
		// If this is a text node, use its parent element.
	
		if (dragObj.elNode.nodeType == 3)
		{
	  		//dragObj.elNode = dragObj.elNode.parentNode;
		}
	}

	
	if ( dragObj.elNode.tagName.toLowerCase().match(/select|input|option|textarea/) )
	{
		dragStop(event);
		return;
	}
	
	if (browser.isIE) 
	{
		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (browser.isNS) 
	{
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}

	dragObj.cursorStartX = x;
	dragObj.cursorStartY = y;
	dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
	dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
	
	if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
	if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

	dragObj.elNode.style.zIndex = ++dragObj.zIndex;
	dragObj.elNode.style.cursor = "move";

	if (browser.isIE) 
	{
		document.attachEvent("onmousemove", dragGo);
		document.attachEvent("onmouseup",   dragStop);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS) 
	{
		document.addEventListener("mousemove", dragGo,   true);
		document.addEventListener("mouseup",   dragStop, true);
		event.preventDefault();
	}
}

function dragGo(event) 
{

	var x, y;
	
	// Get cursor position with respect to the page.
	
	if (browser.isIE) 
	{
		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	
	if (browser.isNS) 
	{
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}

	
	// Move drag element by the same amount the cursor has moved.
	
	dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
	dragObj.elNode.style.top = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";	
	
	
	if (browser.isIE) 
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (browser.isNS)
	{
		event.preventDefault();	
	}
}

function dragStop(event) 
{

	// Stop capturing mousemove and mouseup events.

	if (browser.isIE) 
	{
		document.detachEvent("onmousemove", dragGo);
		document.detachEvent("onmouseup",   dragStop);
	}
	
	if (browser.isNS) 
	{
		document.removeEventListener("mousemove", dragGo,   true);
		document.removeEventListener("mouseup",   dragStop, true);
	}
	
	dragObj.elNode.style.cursor = "default";
}	
// END Drag

function hide_flash_objects()
{
	for ( var i=0; i<flash_objects_to_hide.length; i++ )
	{
		var tmp = document.getElementById(flash_objects_to_hide[i]);
		
		if ( tmp != null )
		{
            try
            {
                tmp.style.visibility = "hidden";
            }
            catch ( e )
            {
            }
		}
	}	
}

function show_flash_objects()
{
	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 popup(msg, r, c, h)
{
	var has_close = false;

	hide_flash_objects();

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

	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");
	
	if ( !do_not_fade )
	{	
		fade_interval = setInterval(fade_out, 1);
	}
	
	do_not_fade = true;
		
	//do_not_center = false;

	if ( d != null )
	{
		//d.style.filter = "alpha(opacity=100)";
		//d.style.opacity = "1";
		//if ( b != null ) b.scrollIntoView();

		if ( c && !has_close ) msg += "<br />[ <a href='javascript:popdown();'>Close</a> ]";

		msg = "<span style='z-index: 10;'>" +msg+ "</span>";
	
		d.innerHTML = msg;

		try
		{
			h = $("#" +popup_div).innerHeight();
			//alert($("#" +popup_div).outerHeight());
		}		
		catch ( e )
		{
				h = 400;
		}		
		
		if ( jquery_fade )
		{
			if ( popup_div == null ) popup_div = "popup";
			if ( browser.isIE && browser.version < 8 )
			{
				try
				{
					$("select:not(#" +popup_div+ " select)").css("visibility", "hidden");
				}
				catch ( e ) {}
			}
			$("#" +popup_div).slideDown("fast");
			if ( jquery_expose )
			{
				if ( popup_div == null ) popup_div = "popup";
				$("#" +popup_div).expose({
                                                                api: true, 
                                                                color: expose_bg_color,
                                                                onBeforeLoad: function() { 
                                                                    this.fit();  
                                                                }                                                                 
                                                            }).load();
			}
			
		}
		else
		{
			toggle(popup_div, curr_event, false, false);
		}
		
		if ( h != null )
		{
			//d.style.height = h+ "px";
		}
	
		//alert(d.style.height+ ", " +h);
		
		
		
		if ( !do_not_center ) 
		{
			center_somthing(d, h);
			
		}
		
		//d.scrollIntoView();
	}
	
	if ( r )
	{
		window.setTimeout(_delay_hide, 3500);
	}
}

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

function center_somthing(d, h)
{

	align_top(d, h);
	align_left(d)
}

document.onmousemove = getMouseXY;	

var check_isIE = ( document.all != null );
var check_mouseX = 0;
var check_mouseY = 0;

function getMouseXY(e)
{
	if (!e) e = window.event;
	if ( e )
	{
		try
		{
			check_mouseX = check_isIE ? (e.clientX + document.body.scrollLeft) : e.pageX;
			check_mouseY = check_isIE ? (e.clientY + document.body.scrollTop) : e.pageY;
		}
		catch (e)
		{
		}
	}
}

function align_top(d, h)
{
	var ns=(document.layers);
	var ie=(document.all);
	var w3=(document.getElementById && !ie);
	
	var calunit=ns? "" : "px"
	
	if (ie){documentWidth  =document.body.offsetWidth/2+document.body.scrollLeft-20;
	documentHeight =document.body.offsetHeight/2+document.body.scrollTop-20;}	
	else if (ns){documentWidth=window.innerWidth/2+window.pageXOffset-20;
	documentHeight=window.innerHeight/2+window.pageYOffset-20;} 
	else if (w3){documentWidth=self.innerWidth/2+window.pageXOffset-20;
	documentHeight=self.innerHeight/2+window.pageYOffset-20;} 		
	
	//alert( d.style.height );
	
	if ( h == null || h == 0 )
	{
		var w = d.style.height;
		if ( w.match(/(\d+)px/) )
		{
			w = RegExp.$1;
		}
		
		if ( isNaN(w) || w == 0 )
		{
			w = 300;
		}
		
		h = w;
	}	
	
	d.style.top = documentHeight - h/2 + calunit;
	
}

function align_left(d)
{

	var ns=(document.layers);
	var ie=(document.all);
	var w3=(document.getElementById && !ie);
	
	var calunit=ns? "" : "px"
	
	if (ie){documentWidth  =document.body.offsetWidth/2+document.body.scrollLeft-20;
	documentHeight =document.body.offsetHeight/2+document.body.scrollTop-20;}	
	else if (ns){documentWidth=window.innerWidth/2+window.pageXOffset-20;
	documentHeight=window.innerHeight/2+window.pageYOffset-20;} 
	else if (w3){documentWidth=self.innerWidth/2+window.pageXOffset-20;
	documentHeight=self.innerHeight/2+window.pageYOffset-20;} 
	
	var w = d.style.width;
	if ( w.match(/(\d+)px/) )
	{
		w = RegExp.$1;
	}
	
	if ( isNaN(w) || w == 0 )
	{
		w = 300;
	}
	
	d.style.left = documentWidth - w/2 + calunit;

}

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, 100);
			}
			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, 100);
			}
			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(expose)
{
	if ( popup_div == null || popup_div == "" ) popup_div = "popup";

	var d = document.getElementById(popup_div);
	if ( jquery_fade && expose != -1 )
	{
		
		$("#" +popup_div).slideUp("fast", function() { 
															popdown_cb(); 
															
															if ( jquery_expose ) 
															{ 
																if ( popup_div == null || popup_div == "" ) popup_div = "popup";
																$("#" +popup_div).expose({api: true}).close();
																if ( browser.isIE && browser.version < 8 ) 
																{
																		$("select").css("visibility", "visible");
																}
															}  
													} 
		);
	}
	else
	{	
		if ( !do_not_fade )
		{
			fade_interval = setInterval(fade_in, 1);
		}
		else
		{
			popdown_cb();
		}
	
		hide_something(popup_div);
	}
			
	popup_div = null;

	show_flash_objects();
}


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, _w)
{
	var d = document.getElementById(id);
	
	if ( d != null )
	{
		if ( jquery_fx )
		{
			$("#" +id).toggle("fast");	
		}
		else
		{
			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 + event.currY + 5) + "px";
			d.style.left = ( l  ? (event.currX - w) : (event.currX + 5) ) + "px";		
		}
		else
		{
			d.style.top = (posTop(e) + 15) + "px";
			d.style.left = ( l ? (posLeft(e) - w) : (posLeft(e) + 5)  ) + "px";
		}
*/

		try
		{
			d.style.top = check_mouseY+ "px";
			d.style.left = ( l ) ? (check_mouseX - _w)+ "px" : check_mouseX+ "px";
		}
		catch ( e ) {}
		//alert("Put it here: " +check_mouseX+ ", " +check_mouseY);
	}
	
	if ( d != null && !pos )
	{
		if ( !do_not_center ) center_something(d);
	}
}

function hide_something(id)
{
	var d = document.getElementById(id);
	
	if ( jquery_fx )
	{
		$("#" +id).slideUp("fast");	
	}
	else
	{
		_hide_something(d);
	}
}

function _hide_something(d)
{
	if ( d != null )
	{
		d.style.display = "none";
	}				
}

function _show_something(d, h, t, s)
{
	
	if ( d != null )
	{
		
		s = ( s == null ) ? "block" : s;
		
		d.style.display = s;
	}				
	
	if ( h )
	{
		timers.push(id);
		window.setTimeout(hide_timers, t);
	}
}


var timers = new Array();
function show_something(id, h, t, s)
{
	
	var d = document.getElementById(id);
	
	if ( jquery_fx )
	{
		$("#" +id).slideDown("fast");	
	}
	else
	{
		_show_something(d, h, t, s);
	}
}

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:<br /><br />";
	
	var validators = form_validators;
	
	var form_id = ( form.getAttribute("id") == "" || form.getAttribute("id") == null ) ? form.getAttribute("name") : form.getAttribute("id");
	
	if ( form_validators[form_id] != null )
	{
		validators = form_validators[form_id];
		//alert("GOT IT: " +validators.length);
	}
		
	for ( var i=0; i<validators.length; i++ )
	{
		var tmp = validators[i];
		
		if ( tmp == null ) continue;
		
		var f = form.elements[tmp.name];
		
		if ( f.type == null ) continue;
		
		if ( f.type.toLowerCase().indexOf("select") > -1 )
		{
			if ( f.selectedIndex == 0 )
			{
				s = false;
				msg += " - " +tmp.msg+ "<br />";		
			}
		}
		else if ( f.type.toLowerCase().match(/text|password/) )
		{		
			if ( f.value.length == 0 )
			{
				s = false;
				msg += " - " +tmp.msg+ "<br />";		
			}
		}
	}
	
	if ( !s )
	{
		popup(msg, false, true);
	}
	
	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 ) ? "inline" : "table-row";
	}				
}



function noop()
{
	return ( false );
}




function hilite_week()
{
	var date = new Date();
	var d = date.getDate()+ "";
	if ( d.length == 1 ) d = "0" +d;

	var t = document.getElementById("day_" +d);
	
	if ( t == null ) return;
	
	for ( var i=0; i<6; i++ )
	{
		var tmp = document.getElementById("week_" +i);
		if ( tmp != null )
		{
			fade(tmp);
		}
	
	}
	restore(t.parentNode);

}

function hilite_day()
{
	var date = new Date();
	var d = date.getDate()+ "";
	if ( d.length == 1 ) d = "0" +d;

	fade_object_id = "day_span_" +d;
	
	//fade_cb = true;
	
	//fade_interval = window.setInterval(fade_out, 100);
	
	var fade_object = document.getElementById(fade_object_id);
	
	if ( fade_object != null )
	{
	
		var p = fade_object.parentNode;
		p.style.background = "#EEEEEE";
	}
}

function refresh_page(q)
{
	var r = document.location.href;
	r = r.replace(/#$/, "");
	
	if ( q != null )
	{
		var t = ( r.indexOf("?") > -1 ) ? "&" : "?";
		r += t +q;
	}
	
	document.location.replace(r);

}

function calendar_hide_col()
{
	for ( var i=0; i<=5; i++ )
	{
		var s = document.getElementById("week_" +i);
	
		if ( s != null )
		{
			s.childNodes.item(0).style.visibility = "hidden";
		}
	}
}
function calendar(f)
{
	var date = new Date();

	var year = date.getYear();
	if ( (year+ "").length == 3 ) year += 1900;

	var w = window.open("/cgi-bin/calendar.cgi?f=" +f.id+ "&year=" +year+ "&month=" +(date.getMonth()+1), "_cal", "width=440, height=180, statusbar=no, toolbar=no, menubar=no, scrollbars=no");
	w.opener = this;
	w.moveTo(480, 380);
	w.focus();
}

function GenericObject(p)
{
	this.props = new Array();
	
	if ( p != null )
	{
		this.props = p;
	}
}
function fade(b)
{
	
	b.style.opacity = fade_value;
	b.style.filter = "alpha(opacity=" +(fade_value * 10)+ ")";
}

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

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

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

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);
	}
}
function is_logged_in()
{
	if ( (member_uid+ "").match(/^\d+$/) && member_uid != "0" )
	{
		return ( true );
	}
	else if ( (member_uid+ "").match(/(\w+)/)  && member_uid != "0" )
	{
		return ( true );
	}
	
	return ( false );
}

function close_calendar()
{
	hide_something("calendar_div");
}

function shuffle(o)
{
	for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	
	return ( o );
}


function scroll(id, v)
{
	var d = document.getElementById(id);
	if ( d != null )
	{
		d.scrollTop += ( v > 0 ) ? 90 : -90;
	}
	
	var i = document.getElementById("scrollbar_img");
	
	if ( i != null )
	{
		var d_ = document.getElementById("scrollbar_div");
		var h_;
		
		if ( (d_.style.height+ "").match(/([\d\.]+)px/) )
		{
			h_ = parseFloat(RegExp.$1);
		}
		else
		{
			h_ = d_.style.height;
		}
		//alert(h_);
		var pos = i.style.top;
		//alert(pos);
		
		if ( isNaN(i.style.top) )
		{
			(i.style.top+ "").match(/([\d\.]+)px/);
			pos = parseFloat(RegExp.$1);
		}
		
		h_ -= 60;
		//alert(d.scrollHeight);
		var h = parseFloat(h_ / d.scrollHeight);
		//alert(h);
		//alert(( pos + 90 * h )+ ", " +new String(h_)+ ", " +new String(pos));
		
		if ( v > 0 && ( pos + 90 * h ) >= h_ ) return;
		//alert(pos + ( ( v > 0 ) ? (90.0 * h) : 90.0 * (-1.0*h) ));
		i.style.top = pos + (( ( v > 0 ) ? (90.0 * h) : 90.0 * (-1.0*h) )) + "px";
		
	}
}

function scroll_to_top(id)
{
	var d = document.getElementById(id);
	if ( d != null )
	{
		d.scrollTop = 0;
	}
}

function auto_scroll(id, v)
{
	
}

var current_scroll_src;

function register_scroll_src(src)
{
	current_scroll_src = src;
	//alert(src);
}


function handle_scroll(src, delta) 
{
	if ( current_scroll_src == null ) return;
	if (delta < 0)
	{
		scroll(current_scroll_src.id, 1);
	}
	else
	{
		scroll(current_scroll_src.id, -1);
	}
}

function wheel(event){
	
	event = ( window.event != null ) ? window.event : event;
	//alert(event);
	try
	{
		//|| event.target.tagName == "body" 
		if ( current_scroll_src == null ) return;
	}
	catch ( e )
	{
		return;
	}
		
	var delta = 0;
	if (!event) event = window.event;
	
	if (event.wheelDelta) {
		delta = event.wheelDelta/120; 
		if (window.opera) delta = -delta;
	} else if (event.detail) {
		delta = -event.detail/3;
	}
	if (delta)
		handle_scroll(this, delta);
        if (event.preventDefault)
                event.preventDefault();
        event.returnValue = false;
}

/* Initialization code. */
if (window.addEventListener)
	window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;


function popup_menu(s, d)
{
	window.clearTimeout(hide_timer);
	
	for ( var i=0; i<menus.length; hide_something(menus[i++]) );
	
	show_something(s);
}

var hide_timer;

function popup_menu_hide(s)
{
	hide_timer = window.setTimeout(_popup_menu_hide, 1500, s);	
}

function _popup_menu_hide(s)
{
	for ( var i=0; i<menus.length; hide_something(menus[i++]) );
	
}

function clear_popup_menu_hide(d)
{
	window.clearTimeout(hide_timer);
}

function get_lang()
{
	var re = new RegExp(current_cms+ "_lang=(\\w\\w)");
	if ( document.cookie.match(re) )
	{
		return ( RegExp.$1 );
	}
	else
	{
		return ( "en" );
	}
}

function get_go_mobile()
{
	var re = new RegExp(current_cms+ "_go_mobile=(true|false)");
	if ( document.cookie.match(re) )
	{
		return ( ( RegExp.$1 == "true" ) );
	}
	else
	{
		return ( false );
	}
}

function get_window_size() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return ( {w:myWidth, h:myHeight } );
}

	
function select_option(s, o)
{
	try
	{
		var s_ = ( typeof(s) == "string" ) ? document.getElementById(s) : s;
		//alert(s_+ ", " +o);
		
		if ( s_.multiple )
		{
			o = o.replace(/,/g, "|");
			o = "\\b(" +o+ ")\\b";
		}
		
		var re = new RegExp(o);
		//alert(re);
		
		for ( var i=0; i<s_.options.length; i++ )
		{
			s_.options[i].selected = false;
			if ( s_.options[i].value.match(re) || s_.options[i].text.match(re) )
			{
				s_[i].selected = true;
				if ( !s_.multiple ) break;
			}
		}
	}
	catch ( e ) 
	{
		//alert(e);
	}
}

function clear_field(e, c)
{
	var v = e.value;
	
	var re = new RegExp(c);
	
	if ( !v.match(re) )
	{
			e.value = "";
	}
}


String.prototype.unescapeHtml = function () 
{
    var t = $("<div>"+this+"</div>").html();
    alert(t);
    return ( t );
} 
