/*
    This is a javascript for the Germinate project. It is for creating the groups from tables.
    Copyright (C) 2008  Jacek Grzebyta SCRI / UoD Plant Bioinformatics Group
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    ==============================================================================================*/


function showMessage(message) {

    var element = document.getElementById('messagebox');
    //create structure
    element.innerHTML = "<div id='main'></div><div id='close'><a href='javascript:closeMessage();'>Close Window</a></div>";    
    document.getElementById('main').innerHTML = message;
    setMiddle(element);
    element.style.visibility = 'visible';
}

// This function works on non-IE only. Sorry... 
// In that case CSS is used.
function setMiddle(object) {
    if (navigator.appName != 'Microsoft Internet Explorer') {
    //var object = document.getElementById('messagebox');
    
    var height;
    var width;

    if (window.innerHeight && window.innerWidth) {
             height = window.innerHeight;
             width = window.innerWidth;
    }
    else if (document.body.clientHeight && document.body.clientWidth) {
             height = document.body.clientHeight;
             width = document.body.clientWidth;
    }
    else if (document.documentElement.clientHeight && document.documentElement.clientWidth) {
             height = document.documentElement.clientHeight;
             width = document.documentElement.clientWidth;
    }

    var left = (width - object.clientWidth)/2;
    var top = (height - object.clientHeight)/2;
    
    object.style.left = left + 'px';
    object.style.top = top + 'px';
    }
}


function showWait(message) {
    showMessage(message + ' <img src="' + baseURL + 'public/images/loading.gif" alt="" />');
}

function closeMessage() {
    var element = document.getElementById('messagebox');

    element.style.visibility = 'hidden';
    element.innerHTML = '';
}

// Delete session cookie
function logout() {
    var session_id = getCookie('GSID');
    var cont = window.location;
    delCookie('GSID');
    var locPath = location.protocol + '//'+ location.hostname + baseURL+'app/core/session/';
    location.replace(locPath + 'logout.pl?id=' + session_id + '&continue=' + escape(cont));
}

// Login
function login() {
    var cont = window.location;
    location.replace(baseURL+'app/core/session/login.pl?cont='+escape(cont));
}

// Get a value from cookie
// copyright http://www.w3schools.com/js/js_cookies.asp
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

//Delete cookie
function delCookie(c_name) {
    var now = new Date();
    now.setTime(now.getTime()-1);
    var session_id = getCookie('GSID');
    document.cookie = 'GSID= ;expires=' + now.toGMTString() + ';path=/';    
}


