/**
 * take the string and rotate it to the rot 13 algorithmus
 * @param {Object} value
 */
function rot13decode_encode(value) {	
	var retvalue = '';
	for (i = 0; i < value.length; i++) {
		if(value.charCodeAt(i) >= 65 && value.charCodeAt(i) <= 77) {
			retvalue = retvalue + String.fromCharCode(value.charCodeAt(i)+13);
		}
		else if(value.charCodeAt(i) >= 78 && value.charCodeAt(i) <= 90) {
			retvalue = retvalue + String.fromCharCode(value.charCodeAt(i)-13);
		}
		else if(value.charCodeAt(i) >= 97 && value.charCodeAt(i) <= 109) {
			retvalue = retvalue + String.fromCharCode(value.charCodeAt(i)+13);
		}
		else if(value.charCodeAt(i) >= 110 && value.charCodeAt(i) <= 122) {
			retvalue = retvalue + String.fromCharCode(value.charCodeAt(i)-13);
		}
		else {
			retvalue = retvalue + value.charAt(i);
		}
	}
	return retvalue;
}

/**
 * add smilie code to the guestbook message text area
 * @param {Object} value
 */
function Smilie(value){
	var input = document.getElementById("input_message");
  	input.focus();
	
	/* für Internet Explorer */
  	if (typeof document.selection != 'undefined') {
		/* Einfügen des Formatierungscodes */
	    var range = document.selection.createRange();
	    var insText = range.text;
	    range.text = value;
	    /* Anpassen der Cursorposition */
	    range = document.selection.createRange();
	    if (insText.length === 0) {
	      range.move('character', -value.length);
	    }
		else {
	      range.moveStart('character', value.length);      
	    }
	    range.select();
	}
	/* für neuere auf Gecko basierende Browser */
	else if (typeof input.selectionStart != 'undefined') {
	}
	/* für die übrigen Browser */
	else {
	}
}

var __refUrl = '';
/**
 * check userlogin and post back to retUrl
 * @param {Object} sessionid
 */
function __doLogin_Postback(retUrl,connector) {
	__refUrl = retUrl;
	// get username
	var username = document.getElementById('frontenduserinput').value;
	// get passwort and make md5 hash
	var passwd = document.getElementById('frontendpasswdinputhidden').value;
	// get progressbar to show
	var progr = document.getElementById('progressbar_div');
	if (progr !==null) {
		progr.style.display = "block";
	}
	var error = document.getElementById('login_fail_div');
	if (error !==null) {
		error.style.display = "none";
	}
	// generate Xml Request Object
	var oMyxml = new MYXml();
	oMyxml.exeCommand(connector+"&username="+username+"&passwd="+passwd, __donLogin_Check_Postback);
}

/**
 * check userlogin and post back to retUrl
 * @param {Object} sessionid
 */
function __doGetPW_Postback(retUrl,connector) {
	__refUrl = retUrl;
	// get username
	var username = document.getElementById('frontenduserinput').value;
	// get passwort and make md5 hash
	var passwd = document.getElementById('frontendpasswdinputhidden').value;
	// get progressbar to show
	var progr = document.getElementById('progressbar_div');
	if (progr !==null) {
		progr.style.display = "block";
	}
	var error = document.getElementById('login_fail_div');
	if (error !==null) {
		error.style.display = "none";
	}
	// generate Xml Request Object
	var oMyxml = new MYXml();
	oMyxml.exeCommand(connector+"&username="+username+"&passwd="+passwd, __donLogin_Check_Postback);
}

function __doLogout_Postback(retUrl, connector) {
	__refUrl = retUrl;
	var progr = document.getElementById('progressbar_div');
	if (progr !==null) {
		progr.style.display = "block";
	}
	// generate Xml Request Object
	var oMyxml = new MYXml();
	oMyxml.exeCommand(connector, __donLogout_Check_Postback);
}

function __donLogout_Check_Postback(xml){
	var retVal = xml.SelectSingleNode('Connector/ReturnValue').attributes.getNamedItem('value').value;
	if (retVal === 'true') {
		location.href = __refUrl;
		return;
	}
}

/**
 * check if userlogin was OK so go back to retUrl
 * @param {Object} xml
 */
function __donLogin_Check_Postback(xml) {
	var retVal = xml.SelectSingleNode('Connector/ReturnValue').attributes.getNamedItem('value').value;
	
	// login was OK so go back to retUrl
	if (retVal == 'true') {
		// go to refUrl
		location.href=__refUrl;
		return;
	}
	
	// hide progressbar
	var progr = document.getElementById('progressbar_div');
	if (progr !== null) {
		progr.style.display = "none";
	}
	// hide general error div
	var error = document.getElementById('login_fail_div');
	if (error !== null) {
		error.style.display = "none";
	}
	// hide user not active error div
	var u_error = document.getElementById('login_user_fail_div');
	if (u_error !== null) {
		u_error.style.display = "none";
	}
	// hide group not active error div
	var g_error = document.getElementById('login_group_fail_div');
	if (g_error !== null) {
		g_error.style.display = "none";
	}
	if (retVal == 'U_deact') {		// user exists but is deactivated
		if (u_error !== null) {
			u_error.style.display = "block";
		}
	}
	else if (retVal == 'G_deact') {		// user exists but is deactivated
		if (g_error !== null) {
			g_error.style.display = "block";
       		}
	}
	else {
		if (error !== null) {
			error.style.display = "block";
		}
	}
	
	document.getElementById('frontenduserinput').value = '';
	document.getElementById('frontendpasswdinputhidden').value = '';
}

function __doRegister_Postback(retUrl, connector) {
	// hide all divs
	var faila_el = document.getElementById('fail_uactive_div');
	if(faila_el !== null)
	{
		faila_el.style.display = "none";	
	}
	var fail_el = document.getElementById('fail_unactive_div');
	if(fail_el !== null)
	{
		fail_el.style.display = "none";
	}
	var ok_el = document.getElementById('ok_user_div');
	if(ok_el !== null)
	{
		ok_el.style.display = "none";	
	}
	var progress = document.getElementById('progressbar_div');
	if(progress !== null)
	{
		progress.style.display = "block";	
	}
	
	if(lang == 'de') {
		message_mail = 'Bitte E-Mail Adresse eingeben!';
		message_username = 'Bitte Benutzername eingeben!';
		message_passwd = 'Passwort darf nicht leer sein!';
		message_passwd_rep = 'Passwortwiederholung darf nicht leer sein!';
		message_passwd_rep_fail = 'Passwortwiederholung stimmt nicht!';
	}
	else if(lang == 'en') {
		message_mail = 'Please insert e-mail address!';
		message_username = 'Please insert username!';
		message_passwd = 'Please insert password!';
		message_passwd_rep = 'Please insert password repetition!';
		message_passwd_rep_fail = 'password repetition fails';
	}
	
	// check desired inputs
	if(!__doCheckNotEmpty('reg_mail', 'mail_fail_div', message_mail)) {
		return;
	}
	if(!__doCheckNotEmpty('reg_usern', 'usern_fail_div', message_username)) {
		return;
	}
	if(!__doCheckNotEmpty('reg_passwd', 'passwd_empty_div', message_passwd)) {
		return;
	}
	if(!__doCheckNotEmpty('reg_passwd_rep', 'passwd_rep_fail_div', message_passwd_rep)) {
		return;
	}
	
	var password = document.getElementById('reg_passwd').value;
	var password_rep = document.getElementById('reg_passwd_rep').value;
	/*
	// check passwd not empty
	if(password === '') {
		// show div that password is empty
		var passwd_empty_div = document.getElementById('passwd_empty_div');
		// if error div exists
		if(passwd_empty_div !== null) {
			passwd_empty_div.style.display = "block";
		}
		else {
			alert("Passwort darf nicht leer sein!");
		}
		return;
	}
	*/
	// check passwd repeat
	if(password !== password_rep) {
		// show div that password repeat was not ok
		var passwd_rep_div = document.getElementById('passwd_rep_fail_div');
		// if error div exists
		if(passwd_rep_div !== null) {
			passwd_rep_div.style.display = "block";
		}
		else {
			alert(message_passwd_rep_fail);
		}
		document.getElementById('reg_passwd_rep').value = '';
		return;
	}	

	var anrede = __doRegEntry_String('reg_anr','anrede');
	var titel = __doRegEntry_String('reg_titel','titel');
	var company = __doRegEntry_String('reg_comp','firma');
	var first_name = __doRegEntry_String('reg_firstn','vorname');
	var last_name = __doRegEntry_String('reg_lastn','name');
	var address = __doRegEntry_String('reg_addr','strasse');
	var zip = __doRegEntry_String('reg_zip','plz');
	var city = __doRegEntry_String('reg_ort','ort');
	var country = __doRegEntry_String('reg_land','land');
	var phone = __doRegEntry_String('reg_telefon','telefon');
	var mobile = __doRegEntry_String('reg_mobile','mobil');
	var fax = __doRegEntry_String('reg_fax','fax');
	var mail = __doRegEntry_String('reg_mail','mail');
	var web = __doRegEntry_String('reg_web','web');
	var newsletter = __doRegEntry_Checked('reg_news','newsletter');
	var user_name = __doRegEntry_String('reg_usern','username');
	
	var passwd = __doRegEntry_String('reg_passwd','passwd');
	
	var sent = anrede + titel + company + first_name + last_name + address + zip + city + country + 
		phone + mobile + fax + mail + web + newsletter + user_name + passwd;

	// generate Xml Request Object
	var oMyxml = new MYXml();
	oMyxml.exeCommand(connector+sent, __donRegister_Check_Postback);
}

/**
 * generate parameterstring for ajax request
 * if element exists so generate string
 * @param {Object} id		id of element in the html page
 * @param {Object} param	parameter name for string
 */
function __doRegEntry_String(id, param) {
	var T = document.getElementById(id);
	
	if(T !== null) {
		return "&"+param+"="+T.value;
	}
	return "";
}

/**
 * generate parameterstring for ajax request
 * if element exists so generate string
 * @param {Object} id		id of element in the html page
 * @param {Object} param	parameter name for string
 */
function __doRegEntry_Checked(id, param) {
	var T = document.getElementById(id);
	
	if(T !== null) {
		return "&"+param+"="+T.checked;
	}
	return "";
}

/**
 * check input element, if exists so check value != empty, if not empty all right
 * if value empty check if fail_div exists, if exist so show it otherwise show alert window
 * @param {String} id			id of html element
 * @param {String} fail_div		id of div which is displayed when input is empty
 * @param {String} text			text for alert window if no fail_div was found
 */
function __doCheckNotEmpty(id, fail_div, text) {
	var el = document.getElementById(id);
	if(el !== null) {
		// if desired elementvalue is empty so show message
		if(el.value === '') {
			var fail_el = document.getElementById(fail_div);
			// if fail_div exists, so display it otherwise show alert window
			if(fail_el !== null) {
				fail_el.style.display = "block";
				return false;
			}
			else {
				alert(text);
				return false;
			}
		}
	}
	else {
		alert('element to check not exists: ' + id);
		return false;
	}
	return true;
}

function __donRegister_Check_Postback(xml){
	var retVal = xml.SelectSingleNode('Connector/ReturnValue').attributes.getNamedItem('value').value;
	// hide progressbar
	var progress = document.getElementById('progressbar_div');
	if(progress !== null)
	{
		progress.style.display = "none";	
	}
        var input_div = document.getElementById('user_input_div');
	switch(retVal) {
		case 'U_active':		// user allready exists and is active
			var faila_el = document.getElementById('fail_uactive_div');
			if(faila_el !== null)
			{
				faila_el.style.display = "block";
				return false;	
			}
			break;
		case 'U_deactive':		// user allready exists and is deactive
			var fail_el = document.getElementById('fail_unactive_div');
			if(fail_el !== null)
			{
				fail_el.style.display = "block";
				return false;	
			}
			break;
		case 'true':
			var ok_el = document.getElementById('ok_user_div');
			if(ok_el !== null)
			{
				ok_el.style.display = "block";
                                input_div.style.display = "none";
				return true;	
			}
			break;
		default:
			break;
	}
}


/**
 * 		mycsm intern functions don't touch
 * 		functions for login, logout and different edit site functions
 * 
 * 
 */
function login() {
	// check frontend or backend login
	if(document.getElementById('backend_login').checked === true) {
		window.open('admin_weissenbacher.php');
	}
	else {
		checkLogin();
	}
}

function checkLogin() {
	//alert(document.getElementById('frontenduserinput').value);
	//alert(document.getElementById('frontendpasswdinput').value);
	 change2Admin();
}

function change2Admin() {
	// change all divs to editable
	var contents = getElementsByRegExpId(/(content_).+.(_content)/);
	for(i=0; i < contents.length; i++) {
		//contents[i].style.border = '1px solid red';
		// generate new div vor editable reference
		var edit_div = document.createElement('DIV');
		edit_div.className = 'editdiv';
		edit_div.onclick = function() {
			alert('edit');
			};
		contents[i].insertBefore(edit_div, contents[i].firstChild);
	}
	_log = true;
}

function logout() {
	var contents = getElementsByRegExpId(/(content_).+.(_content)/);
	for (i = 0; i < contents.length; i++) {
		contents[i].removeChild(contents[i].firstChild);
	}
}

function getElementsByRegExpId(p_regexp, p_element, p_tagName) {
	p_element = p_element === undefined ? document : p_element;
	p_tagName = p_tagName === undefined ? '*' : p_tagName;
	var v_return = [];
	var v_inc = 0;
	for(var v_i = 0, v_il = p_element.getElementsByTagName(p_tagName).length; v_i < v_il; v_i++) {
		if(p_element.getElementsByTagName(p_tagName).item(v_i).id && p_element.getElementsByTagName(p_tagName).item(v_i).id.match(p_regexp)) {
			v_return[v_inc] = p_element.getElementsByTagName(p_tagName).item(v_i);
			v_inc++;
		}
	}
	return v_return;
}

function editContent() {
	
}





/**
 *  region for all document events for the mycms system
 */
// key event to manage login function
_strg = false;

// indicate that logedin
_log = false;

document.onkeydown = function(evt) {
	// for browser independence
	evt = (evt) ? evt : ((event) ? event : null);
	if(evt.keyCode == 17) {
		_strg = true;
	}
	else if(_strg === true && evt.keyCode == 77) {
		
		// if already logedin so logout
		if(_log) {
				logout();
				_log = false;
				return;
		}
		
		// check if login div exists
		if(document.getElementById('mainlogin') !== null)
		{
			return;
		}
		_strg = false;
		showLogin(parseInt(document.body.clientHeight,10)/2 - 100 + 'px', parseInt(document.body.clientWidth,10)/2 - 100 + 'px', true, '');
	}
	else {
		_strg = false;
	}
};

function showLogin(top,left, allow_backend, newRef) {
	// do loginfunction
		// create div over site
		
		var opacity_div = document.createElement('DIV');
		opacity_div.className = 'opacitydiv';
		
		opacity_div.id = 'opacitydiv';
		
		document.getElementById('mycmsform').appendChild(opacity_div);
		
		// create logindiv
		var login_div = document.createElement('DIV');

		login_div.style.left = left;
		login_div.style.top = top;
		
		login_div.id = 'mainlogin';
		login_div.className = 'mainlogin';
		document.getElementById('mycmsform').appendChild(login_div);
		
		// create login caption
		var login_caption = document.createElement('DIV');
		login_caption.className = 'logincaption';
		login_caption.innerHTML = 'Login';
		
		login_div.appendChild(login_caption);
		
		// creade div for checkbox
		var chb_div = document.createElement('DIV');
		chb_div.className = 'chbdiv';
		login_div.appendChild(chb_div);
		
		// create table for packend input
		var backend_table = document.createElement('TABLE');
		backend_table.className = 'backendtable';
		backend_table.id = 'backendtable';
		chb_div.appendChild(backend_table);
		// create tbody element for table
		var backent_tbody = document.createElement('TBODY');
		backend_table.appendChild(backent_tbody);
		// create tr for username
		var backend_trback = document.createElement('TR');
		backent_tbody.appendChild(backend_trback);
		
		//create td for back text
		var backend_tdbacklbl = document.createElement('TD');
		backend_tdbacklbl.className = 'desclabel';
		backend_trback.appendChild(backend_tdbacklbl);
		
		// create td for backend input
		var backend_tdbackinput = document.createElement('TD');
		backend_trback.appendChild(backend_tdbackinput);
		
		// create login radio checkbox for backend
		var backend_chb = document.createElement('INPUT');
		backend_chb.id = 'backend_login';
		backend_chb.type = 'checkbox';
		backend_chb.onclick = function(){
			if (this.checked) {
				document.getElementById("frontenduserinput").disabled = true;
			}
			else {
				document.getElementById("frontenduserinput").disabled = false;
			}
		};
		// if backend login is needed
		if (allow_backend) {
			backend_tdbacklbl.innerHTML = 'Backend login:';
			backend_tdbackinput.appendChild(backend_chb);
		}
			
		// create div for frontend login
		var frontent_div = document.createElement('DIV');
		frontent_div.className = 'frontenddiv';
		login_div.appendChild(frontent_div);
		
		// create table for username and password
		var frontend_table = document.createElement('TABLE');
		frontend_table.className = 'frontendtable';
		frontend_table.id = 'frontendtable';
		frontent_div.appendChild(frontend_table);
		// create tbody element for table
		var frontent_tbody = document.createElement('TBODY');
		frontend_table.appendChild(frontent_tbody);
		// create tr for username
		var frontend_truser = document.createElement('TR');
		frontent_tbody.appendChild(frontend_truser);
		
		//create td for username text
		var frontend_tduserlbl = document.createElement('TD');
		frontend_tduserlbl.innerHTML = 'Benutername:';
		frontend_tduserlbl.className = 'desclabel';
		frontend_truser.appendChild(frontend_tduserlbl);
		// create td for unsername input
		var frontend_tduserinput = document.createElement('TD');
		frontend_truser.appendChild(frontend_tduserinput);
		// create unsername input
		var frontendt_userinput = document.createElement('INPUT');
		frontendt_userinput.id = 'frontenduserinput';
		frontendt_userinput.name = 'frontenduserinput';
		frontend_tduserinput.appendChild(frontendt_userinput);
		
		// create tr for passwd
		var frontend_trpasswd = document.createElement('TR');
		frontent_tbody.appendChild(frontend_trpasswd);
		
		//create td for passwd text
		var frontend_tdpasswdlbl = document.createElement('TD');
		frontend_tdpasswdlbl.innerHTML = 'Passwort:';
		frontend_tdpasswdlbl.className = 'desclabel';
		frontend_trpasswd.appendChild(frontend_tdpasswdlbl);
		// create td for passwd input
		var frontend_tdpasswdinput = document.createElement('TD');
		frontend_trpasswd.appendChild(frontend_tdpasswdinput);
		// create passwd input
		var frontend_passwdinput = document.createElement('INPUT');
		frontend_passwdinput.type = 'password';
		frontend_passwdinput.id = 'frontendpasswdinput';
		frontend_passwdinput.name = 'frontendpasswdinput';
		frontend_passwdinput.onkeyup = function() {
			document.getElementById('frontendpasswdinputhidden').value = MD5(this.value);
		};
		frontend_tdpasswdinput.appendChild(frontend_passwdinput);
	//	frontend_passwdinput.setAttribute("type", "password");
	
		// insert hidden hidden input field for assword hash
		var frontend_passwdinput_hidden = document.createElement('INPUT');
		frontend_passwdinput_hidden.type = 'password';
		frontend_passwdinput_hidden.style.width = '0px';
		frontend_passwdinput_hidden.style.height = '0px';
		frontend_passwdinput_hidden.style.display = 'none';
		frontend_passwdinput_hidden.id = 'frontendpasswdinputhidden';
		frontend_passwdinput_hidden.name = 'frontendpasswdinputhidden';
		frontend_tdpasswdinput.appendChild(frontend_passwdinput_hidden);
		
		// buttons div
		var btn_div = document.createElement('DIV');
		btn_div.className = 'btndiv';
		login_div.appendChild(btn_div);
		
		// create login button
		var login_btn;
		
		if (newRef !== '') {
			login_btn = document.createElement('INPUT');
			login_btn.className = 'loginbtn';
			login_btn.type = 'submit';
			login_btn.value = 'Login';
			//login_btn.method = 'GET';
			// login_btn.href = newRef;
			// alert(document.getElementById('mycmsform'));
			
		}
		else {
			login_btn = document.createElement('INPUT');
			login_btn.className = 'loginbtn';
			//login_btn.type = 'button';
			login_btn.innerHTML = 'Login';
			login_btn.value = 'Login';
			login_btn.onclick = function(){
				login();
				document.body.removeChild(document.getElementById('mainlogin'));
				document.body.removeChild(document.getElementById('opacitydiv'));
			};
		}
		btn_div.appendChild(login_btn);
		// create cancel button
		var cancel_btn = document.createElement('INPUT');
		cancel_btn.className = 'cancelbtn';
		cancel_btn.type = 'button';
		cancel_btn.value = 'Cancel';
		// if login2new so deactivate function
		if (newRef === '') {
			cancel_btn.onclick = function(){
				document.body.removeChild(document.getElementById('mainlogin'));
				document.body.removeChild(document.getElementById('opacitydiv'));
			};
		}
		btn_div.appendChild(cancel_btn);
}
		
    /**
    * o------------------------------------------------------------------------------o
    * | This package is licensed under the Phpguru license 2008. A quick summary is  |
    * | that the code is free to use for non-commercial purposes. For commercial     |
    * | purposes of any kind there is a small license fee to pay. You can read more  |
    * | at:                                                                          |
    * |                  http://www.phpguru.org/static/license.html                  |
    * o------------------------------------------------------------------------------o
    *
    * ? Copyright 2008 Richard Heyes
    */

/**
* Shows a mail dialog
* 
* @param string contentID ID of content layer to use HTML of
* @param int    width     Width of dialog
*/
__visibleDialog = null;

function MailDialog_Show(contentID, width, attachement)
{
    /**
    * Hide all selects
    */
    if (document.all) {
        var selectObjs = document.getElementsByTagName('select');

        for (var i=0; i<selectObjs.length; ++i) {
            if (!selectObjs[i].md_nohide) {
                selectObjs[i].md_vis = selectObjs[i].style.visibility;
                selectObjs[i].style.visibility = 'hidden';
            }
        }
    }
    
    /**
    * Create the background layer if necessary
    */
    var dialogBg = document.getElementById('mailBg');
    
    if (!dialogBg) {
        var bgDiv = document.createElement('div');
        bgDiv.id = 'mailBg';
        bgDiv.className = 'mailBg';
    
        document.body.appendChild(bgDiv);
        
        var dialogBg = document.getElementById('mailBg');
    }
    
    
    /**
    * Create the shadow layer
    */
    var dialogShadow = document.getElementById('mailShadow');
    
    if (!dialogShadow) {
        var shadowDiv = document.createElement('div');
        shadowDiv.id = 'mailShadow';
        shadowDiv.className = 'mailShadow';
        
        document.body.appendChild(shadowDiv);
        
        var dialogShadow = document.getElementById('mailShadow');
    }

    // Show the dialog
    var dialog = document.getElementById(contentID);
    dialog.style.visibility = 'hidden';
    dialog.style.width = width + 'px';
    
    dialogShadow.style.visibility = 'visible';
    dialogShadow.style.width = dialog.offsetWidth + 'px';
    dialogShadow.style.height = dialog.offsetHeight + 'px';

    /**
    * Insert the header into the dialog
    */
    var dialogHeader = document.createElement('div');
    dialogHeader.id = 'dialogHeader';
    dialogHeader.className = 'mailDialogHeader';
    dialog.appendChild(dialogHeader);
    dialogHeader = document.getElementById('dialogHeader');

    Fade(dialogBg, true, null, null, 70);
    Fade(dialog, true, null, null, 100);
    Fade(dialogShadow, true, null, null, 50, 'MailDialog_FinishFade');
    
    
    // Moz stuff
    if (!document.all) {
        dialogBg.style.width  = document.body.scrollWidth + 'px';
        dialogBg.style.height = document.body.scrollHeight + 'px';
        
        dialog.style.left = document.body.offsetWidth / 2 - width / 2 + 'px';
        dialog.style.top  = document.body.clientHeight / 2 - dialog.offsetHeight / 2 + 'px';
        
        dialogShadow.style.left = (document.body.offsetWidth / 2 - width / 2) + 4 + 'px';
        dialogShadow.style.top  = (document.body.clientHeight / 2 - dialog.offsetHeight / 2) + 4 + 'px';
        
        dialogHeader.style.width = (dialog.offsetWidth - 2) + 'px';
        dialogHeader.style.paddingTop = '5px';
    
    // IE stuff
    } else {
        dialogHeader.style.width = dialog.offsetWidth + 'px';
    }
    
    // Set visible dialog var
    __visibleDialog = contentID;
}


/**
* Closes a mail dialog
*/
function MailDialog_Close()
{
    var dialogBg     = document.getElementById('mailBg');
    var dialog       = document.getElementById(__visibleDialog);
    var dialogShadow = document.getElementById('mailShadow');

    
    // Lose the dialog header
    dialog.removeChild(document.getElementById('dialogHeader'));

    // Hide stuff
    dialogBg.style.MozOpacity = 0;
    dialogBg.style.visibility = 'hidden';
    
    dialog.style.MozOpacity = 0;
    dialog.style.visibility = 'hidden';
    dialogShadow.style.visibility = 'hidden';
    
    
    __visibleDialog = null;
    
    /**
    * Unhide all selects
    */
    var selectObjs = document.getElementsByTagName('select');
    
    for (var i=0; i<selectObjs.length; ++i) {
        if (!selectObjs[i].md_nohide) {
            selectObjs[i].style.visibility = selectObjs[i].md_vis;
        }
    }
}

function MailDialog_FinishFade()
{
    var shadow  = document.getElementById('mailShadow');
    var visible = document.getElementById(__visibleDialog)

    shadow.style.width = visible.offsetWidth;
    shadow.style.height = visible.offsetHeight;
    shadow.style.filter = 'Alpha(opacity=50) progid:DXImageTransform.Microsoft.Blur(pixelradius=2)';
}

///////////////////////////////////////////////////////////////////////
//     This fade library was designed by Erik Arvidsson for WebFX    //
//                                                                   //
//     For more info and examples see: http://webfx.eae.net          //
//     or contact Erik at http://webfx.eae.net/contact.html#erik     //
//                                                                   //
//     Feel free to use this code as lomg as this disclaimer is      //
//     intact.                                                       //
///////////////////////////////////////////////////////////////////////


var __fadeArray = new Array();    // Needed to keep track of wich elements are animating

//////////////////  fade  ////////////////////////////////////////////////////////////
//                                                                                  //
//   parameter: fadeIn                                                              //
// description: A boolean value. If true the element fades in, otherwise fades out  //
//              The steps and msec are optional. If not provided the default        //
//              values are used. Opacity is optional and specifies the start/end    //
//              opacity values. onfinish is used when the fadein/out has finished   // 
//              and is called (so pass in a function name)                          //
//////////////////////////////////////////////////////////////////////////////////////

function Fade(el, fadeIn, steps, msec, opacity, onfinish)
{
    if (steps    == null)  steps   = 4;
    if (msec     == null)  msec    = 25;
    if (opacity  == null)  opacity = 100;
    if (onfinish == null)  onfinish = '';

    if (el.fadeIndex == null) {
        el.fadeIndex = __fadeArray.length;
    }

    __fadeArray[el.fadeIndex] = el;

    if (el.style.visibility == "hidden") {
        el.style.display  = 'block';
        el.fadeStepNumber = 0;
    } else {
        el.fadeStepNumber = steps;
    }

    if (fadeIn) {
        el.style.filter = "Alpha(Opacity=0)";
        el.style.MozOpacity = '0';
    } else {
        el.style.filter = "Alpha(Opacity=" + opacity + ")";
        el.style.MozOpacity = opacity / 100;
    }

    window.setTimeout("RepeatFade(" + fadeIn + "," + el.fadeIndex + "," + steps + "," + msec + ", " + opacity + ", '" + onfinish + "')", msec);
}

//////////////////////////////////////////////////////////////////////////////////////
//  Used to iterate the fading                                                      //
//////////////////////////////////////////////////////////////////////////////////////
function RepeatFade(fadeIn, index, steps, msec, opacity, onfinish)
{
    el = __fadeArray[index];

    c = el.fadeStepNumber;
    if (el.fadeTimer != null) {
        window.clearTimeout(el.fadeTimer);
    }

    if (c == 0 && !fadeIn) {            // Done fading out!
        el.style.visibility = "hidden"; // If the platform doesn't support filter it will hide anyway
        el.style.display    = "none"; // If the platform doesn't support filter it will hide anyway

        if (onfinish) {
            eval(onfinish + "()");
        }
        return;

    } else if (c == steps && fadeIn) {    // Done fading in!
        el.style.filter = "Alpha(Opacity=" + opacity + ")";
        el.style.MozOpacity = opacity / 100;
        el.style.visibility = "visible";

        if (onfinish) {
            eval(onfinish + "()");
        }
        return;

    } else {
        fadeIn ? c++ : c--;
        el.style.visibility = "visible";
        el.style.filter = "Alpha(Opacity=" + opacity*c/steps + ")";
        el.style.MozOpacity = (opacity / 100) * c/steps;

        el.fadeStepNumber = c;
        el.fadeTimer = window.setTimeout("RepeatFade(" + fadeIn + "," + index + "," + steps + "," + msec + ", " + opacity + ", '" + onfinish + "' )", msec);
    }
}
