/*
 * This code will correct a problem with ie in mootools' '$$' selector function 
 * that creates a bunch of temporary attributes that should not be printed. 
 */
if (window.ie) $$.unique = function(array){
	var elements = [];
	for (var i = 0, l = array.length; i < l; i++){
		if (array[i].$included) continue;
		var element = $(array[i]);
		if (element && !element.$included){
			element.$included = true;
			elements.push(element);
		}
	}
	for (var n = 0, d = elements.length; n < d; n++) elements[n].removeAttribute('$included');
	return new Elements(elements);
};





function centerHiddenElement(elmnt)
{
	elmnt.setStyle('display', 'block').setOpacity(0.0);
	centerElement(elmnt);
	elmnt.setOpacity(1.0);
}

function centerElement(elmnt) {
	centerElementVertically(elmnt);
	centerElementHorizontally(elmnt);
}

function centerElementHorizontally(elmnt) {
	elmnt.style.position = 'absolute';
	var left = parseInt(getWindowScrollWidth() + (getWindowWidth() / 2) - (elmnt.scrollWidth / 2));
	left = left > 0 ? left : 0;
	elmnt.setStyle('left', left + 'px');
}

function centerElementVertically(elmnt) {
	elmnt.style.position = 'absolute';
	var top = parseInt(getWindowScrollHeight() + (getWindowHeight() / 2) - (elmnt.scrollHeight / 2));
	top = top > 0 ? top : 0;
	elmnt.style.top = top + 'px';
}

function getWindowHeight() { return window.getHeight(); }
function getWindowScrollHeight() { return window.getScrollTop(); }
function getWindowScrollWidth() { return window.getScrollLeft(); }
function getWindowWidth() { return window.getWidth(); }
function getWindowFullHeight() { return window.getScrollHeight(); }
function getWindowFullWidth() { return window.getScrollWidth(); }

function scroll_to(el) {
    var offsetTop = 0;
    while(typeof(el) != "undefined" && el != null) {
        offsetTop += el.offsetTop;
        el = el.offsetParent;
    }
    window.scrollTo(0, offsetTop);
}


function redirect(url) {
    //  This version does NOT cause an entry in the browser's
    //  page view history.  Most browsers will always retrieve
    //  the document from the web-server whether it is already
    //  in the browsers page-cache or not.
 
    window.location.replace( url );
}


function addTooltips(obj)
{
	var Tooltips = new Tips(obj, {
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});
}

function appendError(error_string) {
	var obj = new Element('p');
	obj.appendText(error_string);
	$$('#error div.body').adopt(obj);
	openPopUp('error');
}
function showShadow() {
	var shadow = $('shadow');
	shadow.setOpacity(0.7);
	shadow.setStyle('display', 'block');
	shadow.setStyle('height', getWindowFullHeight());
	shadow.setStyle('width', getWindowFullWidth());
}
function hideShadow() {
	var shadow = $('shadow');
	shadow.setStyle('display', 'none');
}
function openPopUp(boxName) {
	showShadow();
	var box = $(boxName);
	centerHiddenElement(box);
}
function closePopUp(box) {
	hideShadow();
	closeAllWindows();
}
function closeAllWindows() {
	$$('.hoverBox').each(function(item){
		item.setStyle('display', 'none');
	});
}

function linkEmail(email) {
	var replace = {at: '@', dot: '.'};
	email.substitute(replace);
}


// Logins
function loginSite(){
	document.getElementById("formLogin").submit();
}









function login(form) {
	if(!$chk(form)) {
		$$('form.login').each( function(item) {
			item.setStyle('display', 'none');
		}); 
		$('loginSelect').setStyle('display', '');
	} else {
		var item = $(form);
		$('loginSelect').setStyle('display', 'none');
		item.setStyle('display', '');
	}
}

			
function checkWebmailLogin() {
	var form = $('webmaillogin');
	var user = $('email');
	var pass = $('pass');
	
	if(user.value.match(/@imperium.ca/)) {
		user.value = user.value.replace(/@imperium.ca/i, '');
		user.set('name', 'username');
		pass.set('name', 'password');
		form.set('action', "https://mail.imperium.ca/exchweb/bin/auth/owaauth.dll");
	}
	form.submit();
}

function extractDomain() {
	$('reg_domain').value = $('reg_domain').value.replace('http://', '').replace('www\.', '');
	$('opensrslogin').submit();
}