function init() {
  exec_login();
}

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

function show_signup_form() {
	getObj('signup_form').style.display = 'block';
	getObj('login_form').style.display = 'none';
	getObj('username').focus();
}

function exec_login() {
  FL.responseHandler = 'login';
  FL.handle( 'store', 'modules/site/login.php', 'login_email', 'login_password' );
  //wait before throwing the login box
  setTimeout( 'show_login()', 100 );  
}
function login( response ) {
  if( response == 1 ) {
    getObj('usernametext').innerHTML = 'Unknown email';
    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('email').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', 'site_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 latestPlottrs( sa, ts ) {
  FL.responseHandler = 'showLatestPlottrs';
  FL.handle( 'retrieve', '/modules/site/getlatestplottrs.php?sa=' + sa + '&ts=' + ts );
}

function showLatestPlottrs( response ) {
  getObj( 'latestPlottrs' ).innerHTML = URLDecode( response );
}

function enlargeImage( img, pID ) {
  /*
  var d = getObj( 'img_' + pID );
  d.innerHTML = '<a href="#" onclick="delargeImage( \'' + img + '\', ' + pID + ' );"><img src="' + img + '" align="left" style="border:0px;padding-right:5px;" /></a>';  
  */
  //get page size
  var size = getPageSizeWithScroll();
  var y = getPageHeight()
  var x = getPageWidth()
  var h = (y - 300) / 2;
  var w = (x - 400) / 2;
  
  var d = getObj( 'overlay' );
  d.style.height  = size[1] + 'px';
  d.style.display = 'block';

  var html = '<div id="box" style="top:' + h + 'px;left:' + w + 'px;">';
  html+= '<a href="#" onclick="hideOverlay();"><img src="' + img + '" width="400" style="border:0px;" /></a>';  
  html+= '</div><div id="closebox" style="top:' + (h + 4 ) + 'px;left:' + ( w + 635 ) + 'px;"><a href="#"><img src="/if/close.gif" border="0" onclick="hideOverlay();" /></a></div>';

  d.innerHTML = html;
}

function hideOverlay() {
  var d = getObj( 'overlay' );
  d.style.display = '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 URLEncode( )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = document.URLForm.F1.value;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	document.URLForm.F2.value = encoded;
	return false;
};
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 isFunction(a) { return typeof a=='function'; }
function isObject(a) { return a&&typeof a=='object'||isFunction(a); }
function isArray(a) { return isObject(a)&&a.constructor==Array; }
function isUndefined(a) { return typeof a=='undefined'; }
function isDefined(a) { return !isUndefined(a); }
function isString(a) { return typeof a=='string'; }
function isDigit(num) { if (num.length>1) return false; var string="1234567890"; return string.indexOf(num)!=-1; }

    
function showPlottrOnMap( pID ) {
  if( typeof( plottr ) != 'undefined' ) {
	  var m = plottr.findMarkerByPID( pID );
	  if( m ) {
	    plottr.map.setCenter( m.getPoint(), 15 );
	    plottr.initInfoTabs( m );
	  } else
	    window.setTimeout( 'showPlottrOnMap( ' + pID + ' )', 1000 );
	} else {
	  window.location.href = '/map#show=' + pID;
	}
}

function handleAnchor() {
  //find any anchor in the url
  var uri = new String( this.window.location.href );
  var hashstr = uri.substr( (uri.indexOf( '#' ) + 1 ), ( uri.length - uri.indexOf( '#' ) ) );
  var parts = hashstr.split( '&' );
  for( i = 0; i < parts.length; i++ ) {
    var a = parts[i].split( '=' );
    if( a[0] == 'show' ) {
      showPlottrOnMap( a[1] );  
    }
  }
}

  function findPosX(obj) {
  	var curleft = 0;
  	if (obj.offsetParent) {
  		while (obj.offsetParent) {
  			curleft += obj.offsetLeft
  			obj = obj.offsetParent;
  		}
  	} else if (obj.x)
  		curleft += obj.x;
  	//alert( curleft );
  	return curleft;
  }

  function findPosY(obj) {
  	var curtop = 0;
  	if (obj.offsetParent) {
  		while (obj.offsetParent) {
  			curtop += obj.offsetTop
  			obj = obj.offsetParent;
  		}
  	} else if (obj.y)
  		curtop += obj.y;
  	return curtop;
  }
  
  // Main function to retrieve mouse x-y pos.s
  function getMouseXY(e) {
    if (IE) {
      mouse_Xpos = event.clientX + document.body.scrollLeft
      mouse_Ypos = event.clientY + document.body.scrollTop
    } else {
      mouse_Xpos = e.pageX
      mouse_Ypos = e.pageY
    }  
    // catch possible negative values in NS4
    if (mouse_Xpos < 0){mouse_Xpos = 0}
    if (mouse_Ypos < 0){mouse_Ypos = 0}  

    //get scrollo
    if (self.pageYOffset) {
    	scroll_X = self.pageXOffset;
    	scroll_Y = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
    	scroll_X = document.documentElement.scrollLeft;
    	scroll_Y = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers {
    	scroll_X = document.body.scrollLeft;
    	scroll_Y = document.body.scrollTop;
    }
    return true
  }  
function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

function getPageHeight(){
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}
function getPageWidth(){
	if (window.innerWidth && window.scrollMaxX) {// Firefox
		var x = window.innerWidth;
	} else if (document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		var x = document.body.clientWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		var x = document.body.offsetWidth;
	}
	return x;
}

function alertSize() {
  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;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}