function init() {
  exec_login();
}

function show_login() {
	getObj('login').style.display = 'block';
}


function exec_login() {
  FL.responseHandler = 'login';
  FL.handle( 'store', 'modules/site/login.php', 'username', 'password' );
  //wait before throwing the login box
  setTimeout( 'show_login()', 100 );  
}
function login( response ) {
  if( response == 1 ) {
    getObj('usernametext').innerHTML = 'Unknown username';
    getObj('usernametext').style.color = 'red'; 
    getObj('usernametext').style.fontStyle = 'italic';
    getObj('passwordtext').innerHTML = 'Password';
    getObj('passwordtext').style.fontStyle = 'normal';
    getObj('passwordtext').style.color = 'black'; 
  } else if( response == 2 ) {
    getObj('passwordtext').innerHTML = 'Invalid password';
    getObj('passwordtext').style.fontStyle = 'italic';
    getObj('passwordtext').style.color = 'red';
    getObj('usernametext').innerHTML = 'Username';
    getObj('usernametext').style.fontStyle = 'normal';
    getObj('usernametext').style.color = 'black';     
  } else if( response == 3 ) {
    FL.responseHandler = 'replace_login';
    FL.handle( 'retrieve', 'modules/site/usermenu.php' );
  } else {
    getObj('username').value = '';
    getObj('password').value = '';
  }
}

function replace_login( response ) {
  getObj( 'login' ).innerHTML = URLDecode( response );
}


function get_signup() {
  FL.responseHandler = 'show_content';
  FL.handle( 'retrieve', 'modules/site/signup.php?action=showform' ); 
}

function show_content( response ) {
  getObj( 'content' ).innerHTML = URLDecode( response );
}

function do_signup() {
  //let php check the fields (handiger met talen)
  FL.responseHandler = 'show_content';
  FL.handle( 'store', 'modules/site/signup.php?action=checkform', 'name', 'email', 'password1', 'password2' );  
}

function blog_availability() {
  //let php check if blog url is available
  FL.responseHandler = 'show_blogavailability';
  FL.handle( 'store', 'modules/site/check_blogavailability.php', 'url' );  
}

function show_blogavailability( response ) {
  getObj( 'blogavailability' ).innerHTML = URLDecode( response );  
}

function toggle_advanced_blogsettings() {
  getObj( 'advanced_blogsettings' ).style.display = (getObj( 'advanced_blogsettings' ).style.display == 'none') ? 'block' : 'none';
}

function readCookie(cname) {
    var cookieName = cname + '=';
    var cookies = document.cookie;
    var pos = cookies.indexOf(cookieName);

    if (pos != -1) {
        var start = pos + cookieName.length;
        var end = cookies.indexOf(';', start);

        if (end == -1) {
            end = cookies.length;
        }
        return unescape(cookies.substring(start, end));
    }
}

function setCookie(pName, pValue, expire_period) {
    var expireDate = new Date();
    expireDate.setTime( (new Date()).getTime() + expire_period );
    document.cookie = pName + '=' + escape(pValue) + "; expires=" + expireDate.toGMTString() + "; path=/";
}

function setFocus( obj ) {
  getObj( obj ).focus();
}

function catchKey( evt, func, params ) {
  evt = (evt) ? evt : (window.event) ? event : null;
  if (evt) {
    var charCode = (evt.charCode) ? evt.charCode :
                   ((evt.keyCode) ? evt.keyCode :
                   ((evt.which) ? evt.which : 0));
    if (charCode == 13) func(params);
  }  
}

function URLDecode( encoded )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   
   return plaintext;
};
function getObj(object) {
  if(document.all) myObject=eval('document.all[object]');
  else if(document.layers) myObject=eval('document.layers[object]');
  else if(document.getElementById && !document.all) myObject=eval('document.getElementById(object)');
  else return;
  return myObject;
}

function popup(url, w, h, scroll) {
 	if( !w ) w = '550';
 	if( !h ) h = '400';
 	mytop = (screen.height - h) / 2;
	myleft = (screen.width - w) / 2;
	window.open(url, "popupscreen","width=" + w + ",height=" + h + ",menubar=0,toolbar=0, location=0,directories=0,status=0,scrollbars=" + scroll + ",resizable=1,top=" + mytop + ",left=" + myleft + "");
}
function findObj(object) {
  return getObj(object);
}

function showEditorUpload() {
  var arrPageSizes = getPageSize();
  $('body').append('<div id="upload-overlay"></div>');	
  
  var el = document.createElement( 'div' );
  el.id = 'editor-upload-container';
  el.innerHTML = $('#editor-upload-container-src').html();
  
  $('#editor-upload-container-src' ).remove();
  

  $('body').append( el );
  
  $('#upload-overlay').css({
  	backgroundColor:	'#000',
  	opacity:			0.8,
  	width:				arrPageSizes[0],
  	height:				arrPageSizes[1]
  }).fadeIn();
  
  $('#editor-upload-container').fadeIn();  
}

function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
};


function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
};

function showInfoBar(caption){
	hideInfoBar();
	$(document.body).append("<div id=infoBar><table width=100%><tr nowrap id=infoContent><td width=30></td><td align=left>" + caption + "</td><td width=60 align=left><div id=infoClose>[Sluiten]</div></td></tr></table></div>");
	$("#infoClose").click(hideInfoBar);
	$("#infoClose").css("border-color","#ffffff");
	$("#infoClose").css("border","7");
	$("#infoBar").hide();
	$("#infoContent").css("font-weight","normal");
	$("#infoContent").css("font","15px Arial,Verdana,sans-serif;");
	$("#infoContent").css("color","black");
	$("#infoBar").css("background-color","#FFFF99");
	$("#infoBar").css("position","absolute");
    $(window).resize(info_position);
    $(window).scroll(info_position);
	info_position();
	$("#infoBar").slideDown(400); //i like sliding in more than fade in this case. i think it catches the users attention more effectively
	//$("#infoBar").fadeIn(800); //if you like this more remark the previous line

}

function hideInfoBar(){
	$("#infoBar").fadeOut(400)
	$("#infoBar").remove();
}

function info_position() {
	var scrollPosition = $(window).scrollTop();
	$("#infoBar").css("top",scrollPosition +"px");
	$("#infoContent").css("height","45px");
	$("#infoBar").css("left","0px");
	$("#infoBar").css("width",$(window).width());
}

