/*
Script Name: Simple Javascript Browser/OS detection
Authors: George Anestis, Websites: http://www.music.tuc.gr/

Version 1.0.0
Copyright (c) 12 Nov 2007

*/

/******************************************************************************
Handle Navigation. 


We test for basic browser type (ie, op, or moz/netscape > 6).
******************************************************************************/
//var baseURL = "http://localhost:8084/";
var baseURL = "http://www.ece.tuc.gr/";

/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
var xmlHttp_personCV = false;
var xmlHttp_labInfo = false;
var xmlHttp_Announcement = false;
var xmlHttp_sidebar = false;
var xmlHttp_header = false;
var xmlHttp_footer = false;
var xPos, yPos;

/****************************************************************************** 
* create the asynchronous requests objects 
*******************************************************************************/ 
function createRequest() {
   try {
      xmlHttp = new XMLHttpRequest();
      xmlHttp_personCV = new XMLHttpRequest();
      xmlHttp_labInfo = new XMLHttpRequest();
      xmlHttp_Announcement = new XMLHttpRequest();
      xmlHttp_sidebar = new XMLHttpRequest();
      xmlHttp_header = new XMLHttpRequest();
      xmlHttp_footer = new XMLHttpRequest();      
   } catch (trymicrosoft) {
      try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      xmlHttp_personCV = new ActiveXObject("Msxml2.XMLHTTP");
      xmlHttp_labInfo = new ActiveXObject("Msxml2.XMLHTTP");
      xmlHttp_Announcement = new ActiveXObject("Msxml2.XMLHTTP");
      xmlHttp_sidebar = new ActiveXObject("Msxml2.XMLHTTP");
      xmlHttp_header = new ActiveXObject("Msxml2.XMLHTTP");
      xmlHttp_footer = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (othermicrosoft) {
        try {
           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
           xmlHttp_personCV = new ActiveXObject("Microsoft.XMLHTTP");
           xmlHttp_labInfo = new ActiveXObject("Microsoft.XMLHTTP");
           xmlHttp_Announcement = new ActiveXObject("Microsoft.XMLHTTP");
           xmlHttp_sidebar = new ActiveXObject("Microsoft.XMLHTTP");
           xmlHttp_header = new ActiveXObject("Microsoft.XMLHTTP");
           xmlHttp_footer = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed) {
          xmlHttp = false;
           xmlHttp_personCV = false;
           xmlHttp_labInfo  = false;
           xmlHttp_Announcement = false;
           xmlHttp_sidebar = false;
           xmlHttp_header = false;
           xmlHttp_footer = false;
        }
    }
   }

   if (!xmlHttp || !xmlHttp_personCV || !xmlHttp_Announcement 
                || !xmlHttp_labInfo  || !xmlHttp_sidebar
                || !xmlHttp_header || !xmlHttp_footer)
      alert("Error initializing XMLHttpRequest!");  
}

createRequest();

/****************************************************************************** 
* create and return the asynchronous requests objects 
*******************************************************************************/ 
function getXMLHttpRequest() {
  try {
      xmlHttp = new XMLHttpRequest();
      
   } catch (trymicrosoft) {
      try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");      
      } catch (othermicrosoft) {
        try {
           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");           
        } catch (failed) {
          xmlHttp = false;          
        }
    }
   }

   if (!xmlHttp) {
      alert("Error initializing XMLHttpRequest!");  
      xmlHttp = null;
   }
   
   return xmlHttp;
}
/****************************************************************************** 
* show/update the sidebar
*******************************************************************************/ 
function showSidebar(action, chapter) {  
   
   // Build the URL to connect to
   var url = baseURL + action;     
   var currentDate = new Date();
   url += "&ms=" + currentDate.getTime();
   
   // Open a connection to the server
   xmlHttp_sidebar.open("GET", url, true); 
   
   // Setup a function for the server to run when it's done. 
   // Technique for passing params to the callback function
   xmlHttp_sidebar.onreadystatechange = function () {updateSidebar(chapter);};   

   // Send the request
   xmlHttp_sidebar.send(null);
   
   // Show the progress indicator
   d.getElementById("sidebar").innerHTML = "<img align='absmiddle' alt='progress' src=./images/progressIndicator.gif> loading...";
}

function updateSidebar(chapter) {
   if (xmlHttp_sidebar.readyState == 4) {
      if (xmlHttp_sidebar.status == 200) {
         var response = xmlHttp_sidebar.responseText;
         d.getElementById("sidebar").innerHTML = response; 	 
         
         //fire an onClick event on the current block          
         triggerOnClickEvent('BlockID-' + chapter);         
      }	
      else if (xmlHttp_sidebar.status == 404) {
         alert("Request URL does not exist");
      }
      else {
         alert("Error: status code is " + xmlHttp_sidebar.status);
      }
   }
}

/****************************************************************************** 
* show/update the header
*******************************************************************************/ 
function showHeader(action) {  
   
   // Build the URL to connect to
   var url = baseURL + action;  
   var currentDate = new Date();
   url += "&ms=" + currentDate.getTime();
   
   // Open a connection to the server
   xmlHttp_header.open("GET", url, true); 
   
   // Setup a function for the server to run when it's done   
   xmlHttp_header.onreadystatechange = updateHeader;   

   // Send the request
   xmlHttp_header.send(null);      
}

function updateHeader() {
   if (xmlHttp_header.readyState == 4) {
      if (xmlHttp_header.status == 200) {
         var response = xmlHttp_header.responseText;
         d.getElementById("header").innerHTML = response; 	                   
      }	
      else if (xmlHttp_header.status == 404) {
         alert("Request URL does not exist");
      }
      else {
         alert("Error: status code is " + xmlHttp_header.status);
      }
   }
}

/****************************************************************************** 
* show/update the footer
*******************************************************************************/ 
function showFooter(action) {  
   var xmlHttp = new XMLHttpRequest();
   // Build the URL to connect to
   var url = baseURL + action;  
   var currentDate = new Date();
   url += "&ms=" + currentDate.getTime();
   
   // Open a connection to the server
   xmlHttp_footer.open("GET", url, true); 
   
   // Setup a function for the server to run when it's done   
   xmlHttp_footer.onreadystatechange = updateFooter;   

   // Send the request
   xmlHttp_footer.send(null);      
}

function updateFooter() {
   if (xmlHttp_footer.readyState == 4) {
      if (xmlHttp_footer.status == 200) {
         var response = xmlHttp_footer.responseText;
         d.getElementById("footer").innerHTML = response; 	                   
      }	
      else if (xmlHttp_footer.status == 404) {
         alert("Request URL does not exist");
      }
      else {
         alert("Error: status code is " + xmlHttp_footer.status);
      }
   }
}

/****************************************************************************** 
* show/update the body div of the container div
*******************************************************************************/ 
function showDataInBody(action) {    
   //xmlHttp = new XMLHttpRequest();    
   xmlHttp = getXMLHttpRequest();
   
   // Build the URL to connect to
   var url = baseURL + action;   
   var currentDate = new Date();
   url += "&ms=" + currentDate.getTime();      
         
   // Open a connection to the server
   xmlHttp.open("GET", url, true); 
   // Setup a function for the server to run when it's done
   xmlHttp.onreadystatechange = updateDataInBody;

   // Send the request
   xmlHttp.send(null);
   
   // Show the progress indicator   
   d.getElementById("body").innerHTML = "<img align='absmiddle' alt='progress' src=./images/progressIndicator.gif> loading...";   
}

function updateDataInBody() {   
  
   if (xmlHttp.readyState == 4) {   
      if (xmlHttp.status == 200) {        
         var response = xmlHttp.responseText;         
         d.getElementById("body").innerHTML = response; 	                 
      }	
      else if (xmlHttp.status == 404) {
         alert("Request URL does not exist");
      }
      else {
         alert("function: updateDataInBody: Error: status code is " + xmlHttp.status);
      }
   }
}

/****************************************************************************** 
* show/update the announcement div
*******************************************************************************/ 
function showAnnouncement(id, offset, action ) {
  // get mouse xPos and mouse yPos when mouse click occurs
  var announceId = 'announce-' + id;
  
  var obj = d.getElementById(announceId);
	
  var coors = findPos(obj);  
  xPos = coors[0] - offset - 25; 
  yPos = coors[1] + 12;
      
  // Build the URL to connect to
  var url = baseURL + action;  
  var currentDate = new Date();
  url += "&ms=" + currentDate.getTime(); 
  
  // Open a connection to the server
  xmlHttp_Announcement.open("GET", url, true); 
  // Setup a function for the server to run when it's done
  xmlHttp_Announcement.onreadystatechange = updateAnnouncement;

  // Send the request
  xmlHttp_Announcement.send(null);    
}

function updateAnnouncement() {
  if (xmlHttp_Announcement.readyState == 4) {
    if (xmlHttp_Announcement.status == 200) {      
      var response = xmlHttp_Announcement.responseText;       
      var announceDiv = d.getElementById("announcementPresentation");
      
      tmp = d.getElementById('announcementsBody');  
      if (tmp == null) {
         tmp = d.getElementById('announcementsBodyFull');  
      }

      announceDiv.innerHTML = response;        
      announceDiv.style.left = xPos + 'px';
      announceDiv.style.top = yPos - tmp.scrollTop + 'px';
      announceDiv.style.visibility = 'visible';                         
    }	
    else if (xmlHttp_Announcement.status == 404) {
      alert("Request URL does not exist");
    }
    else {
      alert("Error: status code is " + xmlHttp_Announcement.status);
    }
  }
}

/****************************************************************************** 
* create announcement using post method. The standard XMLHttpRequest wrapper is 
* designed for GET requests. I modified it to handle POST requests.
* To modify an AJAX GET to a POST, you need to:
* 1. change the first argument of the request.open() function from "get" to "post";
* 2. set an HTTP header with the Content-Type and character set; and
* 3. instead of adding the encoded name/value pairs to the end of the URL, 
*    send them via the request.send() method. They still need to be encoded 
*    the same way, with name/value pairs joined with = and separated by &, but 
*    without the leading ?. 
*******************************************************************************/ 
function createAnnouncement(form, action ) {
    
  var from = d.getElementById("newAnnouncementForm").from.value;
  var title = d.getElementById("newAnnouncementForm").title.value;
  var announcementBody = d.getElementById("newAnnouncementForm").announcementBody.value;
  var attachment = d.getElementById("newAnnouncementForm").attachment.value;      
  
  if (from == "" || title == "" || announcementBody == "") {
      alert("empty values");
  }
  else {          
    // Build the URL to connect to
    var url = baseURL + action;      
    
    // Open a connection to the server
    xmlHttp_Announcement.open("POST", url, true); 
    var contentType = "application/x-www-form-urlencoded; charset=UTF-8;";    
      
    xmlHttp_Announcement.setRequestHeader("Content-Type", contentType);

    // Setup a function for the server to run when it's done
    xmlHttp_Announcement.onreadystatechange = updateCreateAnnouncement; 

    //var query = createQuery(form);  
    var query = createQuery(d.getElementById(form));  
    
    //--------------------------------------------------------------------------
    var containerForm = d.getElementById("fileUploadForm");
    containerForm.removeChild(document.getElementById('fileName'));
    var newFileNameInput = document.createElement('input');
    
   /* newFileNameInput.setAttribute('type', 'file');
    newFileNameInput.setAttribute('id', 'fileName');
    newFileNameInput.setAttribute('name', 'fileName');
    newFileNameInput.setAttribute('value', 'test');
    newFileNameInput.setAttribute('size', '50');
    newFileNameInput.setAttribute('onchange', 'this.form.submit()');   
   
    containerForm.appendChild(newFileNameInput);
   */ 
    alert('value: ' + d.getElementById("fileUploadForm").fileName.value);
    
    //--------------------------------------------------------------------------
    // Send the request
    xmlHttp_Announcement.send(query);    
  }
}

function updateCreateAnnouncement() {
  if (xmlHttp_Announcement.readyState == 4) {
    if (xmlHttp_Announcement.status == 200) {      
      var response = xmlHttp_Announcement.responseText;       
      d.getElementById("body").innerHTML = response;            
    }	
    else if (xmlHttp_Announcement.status == 404) {
      alert("Request URL does not exist");
    }
    else {
      alert("Error: status code is " + xmlHttp_Announcement.status);
    }
  }
}

/****************************************************************************** 
* It takes as input a form object and formats suitably the values of its 
* fields in order to be posted. It encodes with name/value pairs joined with = 
* and separated by &, but without the leading ?. If the only significant 
* elements on the form are <input> and <textarea> elements, we use this function 
* to create a ready-to-use query from a given <form> element.
*******************************************************************************/ 
function createQuery(form) {
  
    var elements = form.elements;
    var pairs = new Array();

    for (var i = 0; i < elements.length; i++) {

        if ((name = elements[i].name) && (value = elements[i].value))
            pairs.push(name + "=" + encodeURIComponent(value));
    }

    return pairs.join("&");
}

/****************************************************************************** 
* handle create announcement response
*******************************************************************************/ 
function handleAnnouncementCreationResponse() {  
  showDataInBody('admin/AdminController?pageId=adminMainView&actionClass=announcementMng&action=announcementsMng');
}

/****************************************************************************** 
* show/update a person's cv in the body div of the container div
*******************************************************************************/ 
function showPersonCV(action) {   
   // Build the URL to connect to
   var url = baseURL + action;    
   var currentDate = new Date();
   url += "&ms=" + currentDate.getTime();
   
   // Open a connection to the server
   xmlHttp_personCV.open("GET", url, true); 
   // Setup a function for the server to run when it's done
   xmlHttp_personCV.onreadystatechange = updatePersonCV;

   // Send the request
   xmlHttp_personCV.send(null);
   
   // Show the progress indicator
   d.getElementById("body").innerHTML = "<img align='absmiddle' alt='progress' src=./images/progressIndicator.gif> loading...";
}

function updatePersonCV() { 
   if (xmlHttp_personCV.readyState == 4) {
      if (xmlHttp_personCV.status == 200) {
         var response = xmlHttp_personCV.responseText;
         document.getElementById("body").innerHTML = response; 	 
      }	
      else if (xmlHttp_personCV.status == 404) {
         alert("Request URL does not exist");
      }
      else {
         alert("Error: status code is " + xmlHttp_personCV.status);
      }
   }
}

/****************************************************************************** 
* show/update lab info the body div of the container div
*******************************************************************************/ 
function showLabInfo(action) {   
   // Build the URL to connect to
   var url = baseURL + action; 
   var currentDate = new Date();
   url += "&ms=" + currentDate.getTime();
 
   // Open a connection to the server
   xmlHttp_labInfo.open("GET", url, true); 
   // Setup a function for the server to run when it's done
   xmlHttp_labInfo.onreadystatechange = updateLabInfo;

   // Send the request
   xmlHttp_labInfo.send(null);
   
   // Show the progress indicator
   d.getElementById("body").innerHTML = "<img align='absmiddle' alt='progress' src=./images/progressIndicator.gif> loading...";
}

function updateLabInfo() { 
   if (xmlHttp_labInfo != null && xmlHttp_labInfo.readyState == 4) {
      if (xmlHttp_labInfo.status == 200) {
         var response = xmlHttp_labInfo.responseText;
         document.getElementById("body").innerHTML = response; 	 
      }	
      else if (xmlHttp_labInfo.status == 404) {
         alert("Request URL does not exist");
      }
      else {
         alert("Error: status code is " + xmlHttp_labInfo.status);
      }
   }
}

/****************************************************************************** 
* expand a block element of the sidebar and reconstruct the rest of it
*******************************************************************************/ 
function showBlockDiv(divID, divBody, allBlockIds, allBlocks ) {
   var i, allBlocksArray, allBlockIdsArray, content, blockBody;
   var spanTag = "<span name=expandedTable></span>";    
   var spanTagConst = "<span name=";
   var blockInfo = "showDataInBody('Controller?actionClass=showBlockInfo&amp;block_id=";
   var historyInfo = "window.ajaxHistory.add('showDataInBody','Controller?actionClass=showBlockInfo&amp;block_id=";
      
   d.getElementById(divID).innerHTML = divBody + spanTag;   
   
   allBlocksArray = allBlocks.split(",");
   allBlockIdsArray = allBlockIds.split(",");
      
   for (i=0; i < allBlockIdsArray.length - 1; ++i ) {                  
      if ( allBlockIdsArray[i] != divID ) {                                       
         
         blockBody = d.getElementById(allBlockIdsArray[i]).innerHTML.replace(/\"/g, " ");                                                      
         var spanFound = blockBody.toLowerCase().indexOf(spanTagConst);                  
         if (spanFound != -1 ) {                                     
            var showBlockDivCode = blockBody.substring(0, spanFound).replace(/\'/g, "\\'");                                    
            
            blockInfo+= allBlockIdsArray[i] + "');";
            historyInfo += allBlockIdsArray[i]  + "');";            
            
            var content = "<table cellspacing=0 cellpadding=0>" +
               "<tr>" +
               "<td valign=top><img src=./images/top_left_corner.gif alt=tuc></td>" +
               "<td class=title width=100%><span class=myLink onClick=\"showBlockDiv('" + allBlockIdsArray[i] + "','" + showBlockDivCode +"','" + allBlockIds + "','" + allBlocks + "');" + blockInfo + historyInfo+ "\">" + allBlocksArray[i] + "  &raquo;</span></td>" +
               "<td valign=top><img src=./images/top_right_corner.gif alt=tuc></td>" +
               "</tr>"+                                    
               "</table>";
            
            // in order to avoid the unterminated string constant error in IE
            // erase the new line and carriage return characters
            content=content.replace(/\n/g, " ");
            content=content.replace(/\r/g, " ");            
            
            d.getElementById(allBlockIdsArray[i]).innerHTML = content;                           
         }        
      }  
   }
}

/****************************************************************************** 
* show the file chooser 
*******************************************************************************/ 
function showFileChooser(id, fileName) {   
   var fileChooserDiv = d.getElementById(id);     
   
   var htmlBody ="<form method='post' id='uploadFileForm'>";
   htmlBody += "<table>";
   htmlBody += "<tr><td colspan='2'>";
   htmlBody += "Επιλέξτε τον αρχείο το οποίο θέλετε να ανεβάσετε:</td></tr>";
   htmlBody += "<tr><td colspan='2'>";
   htmlBody += "<input type='file' name='fileName' size='35'></td></tr>";
   htmlBody += "<tr><td colspan='2'><div class='hr'><hr></div></td></tr>";
   htmlBody += "<tr><td style='padding:2px 5px;' align='right'>";
   htmlBody += "<span id='uploadFile' class='button' ";
   htmlBody += "onClick='buttonClicked(this);' onMouseOver='buttonMouseOver(this);' ";
   htmlBody += "onMouseOut='buttonMouseOut(this);'>Αποθήκευση</span></td>";
   htmlBody += "<td style='padding:2px 5px;' align='left'><span id='cancelUploadFile' class='button' ";
   htmlBody += "onClick='buttonClicked(this);' onMouseOut='buttonMouseOut(this);' onMouseOver='buttonMouseOver(this);'>";
   htmlBody += "Ακύρωση</span></td></tr></table>";
   htmlBody += "</form>";   
   
   fileChooserDiv.innerHTML = htmlBody;
   
   // get mouse xPos and mouse yPos when mouse click occurs       
   var obj = d.getElementById(fileName);
   
	
   var coors = findPos(obj);  
   xPos = coors[0] - 300 -23; 
   yPos = coors[1];   
  
   fileChooserDiv.style.left = xPos + 'px';
   fileChooserDiv.style.top = yPos + 'px';
   fileChooserDiv.style.visibility = 'visible';       
}

/****************************************************************************** 
* show the folder creation dialog 
*******************************************************************************/ 
function showDirCreationDialog(id, command) { 
   var dirCreationDialogDiv = d.getElementById(id);          
   
   var htmlBody = "<form action='' id='createFolderForm'>";
   htmlBody += "<table>";
   htmlBody += "<tr><td colspan='2'>Δώστε το όνομα του φακέλου:</td></tr>";
   htmlBody += "<tr><td colspan='2'><input type='text' name='dirName' size='25' onKeyPress='return disableEnterKey(event);'>";
   htmlBody += "</td></tr>";
   htmlBody += "<tr><td colspan='2'><div class='hr'><hr></div></td></tr>";
   htmlBody += "<tr><td style='padding:2px 5px;' align='right'><span id='createFolder' class='button' ";
   htmlBody += "onClick='buttonClicked(this);' onMouseOver='buttonMouseOver(this);' ";
   htmlBody += "onMouseOut='buttonMouseOut(this);'>Δημιουργία</span></td>"; 
   htmlBody += "<td style='padding:2px 5px;' align='left'><span id='cancelCreateFolder' class='button' ";
   htmlBody += "onClick='buttonClicked(this);' onMouseOut='buttonMouseOut(this);' onMouseOver='buttonMouseOver(this);'>";
   htmlBody += "Ακύρωση</span></td></tr></table>";   
   htmlBody += "</form>";
   
   dirCreationDialogDiv.innerHTML = htmlBody;
   
   // get mouse xPos and mouse yPos when mouse click occurs     
   var obj = d.getElementById(command);
   
	
   var coors = findPos(obj);  
   xPos = coors[0] - 200 - 23; 
   yPos = coors[1];
  
   dirCreationDialogDiv.style.left = xPos + 'px';
   dirCreationDialogDiv.style.top = yPos + 'px';
   dirCreationDialogDiv.style.visibility = 'visible';       
}


/****************************************************************************** 
* show the announcement deletion confirm dialog 
*******************************************************************************/ 
function showAnnouncementDeletionConfirmDialog(id, offset, announcementId) { 
   var dirAnnouncementDeletionConfirmDialogDiv = d.getElementById(id);          
   
   var htmlBody = "<form action='' id='deleteAnnouncementForm'>";
   htmlBody += "<table>";
   htmlBody += "<tr><td colspan='2'>Είστε σίγουρος ότι θέλετε να σβήσετε τη συγκεκριμένη ανακοίνωση;</td></tr>";   
   htmlBody += "<input type='hidden' id='announcementId' name='announcementId' value=" + announcementId +">";
   htmlBody += "<tr><td colspan='2'><div class='hr'><hr></div></td></tr>";
   htmlBody += "<tr><td style='padding:2px 5px;' align='right'><span id='deleteAnnouncement' class='button' ";
   htmlBody += "onClick='buttonClicked(this);' onMouseOver='buttonMouseOver(this);' ";
   htmlBody += "onMouseOut='buttonMouseOut(this);'>Ναι</span></td>"; 
   htmlBody += "<td style='padding:2px 5px;' align='left'><span id='cancelAnnouncementDeletion' class='button' ";
   htmlBody += "onClick='buttonClicked(this);' onMouseOut='buttonMouseOut(this);' onMouseOver='buttonMouseOver(this);'>";
   htmlBody += "Ακύρωση</span></td></tr></table>";   
   htmlBody += "</form>";
   
   dirAnnouncementDeletionConfirmDialogDiv.innerHTML = htmlBody;
   
   // get mouse xPos and mouse yPos when mouse click occurs     
   var obj = d.getElementById('announce-' + announcementId);
   
   tmp = d.getElementById('announcementsBody');  
   if (tmp == null) {
    tmp = d.getElementById('announcementsBodyFull');  
   }
	
   var coors = findPos(obj);  
   xPos = coors[0] - offset; 
   yPos = coors[1];
  
   dirAnnouncementDeletionConfirmDialogDiv.style.left = xPos + 'px';
   dirAnnouncementDeletionConfirmDialogDiv.style.top = yPos - tmp.scrollTop + 'px';
   dirAnnouncementDeletionConfirmDialogDiv.style.visibility = 'visible';       
}

/****************************************************************************** 
*Submit the form when the user presses enter                                  * 
*******************************************************************************/ 
function submitenter(myfield,e) {
  var keycode;
  if (window.event) {
    keycode = window.event.keyCode;
  } 
  else if (e) {
    keycode = e.which;
  } 
  else {
    return true;
  }

  if (keycode == 13) {
    myfield.form.submit();
    return false;
  }
  else {
   return true;
  }
}

/****************************************************************************** 
*Prevent submitting the form when the user presses enter                      * 
*******************************************************************************/ 
function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}
/****************************************************************************** 
* functions for getting element's abs x, y position                           * 
*******************************************************************************/ 
function getElementAbsPosX(el) {
    var dx = 0;
    if (el.offsetParent) {
        dx = el.offsetLeft + 8;
        while (el = el.offsetParent) {
            dx += el.offsetLeft;
        }
    }
    return dx;
}

function getElementAbsPosY(el) {
    var dy = 0;
    if (el.offsetParent) {
        dy = el.offsetTop + el.offsetHeight / 2;
        while (el = el.offsetParent) {
            dy += el.offsetTop;
        }
    }
    return dy;
}

function findPosY(obj)  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

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

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop; 
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop; 
		}
	}        
	return [curleft,curtop];
}
/****************************************************************************** 
* functions for button mouse over/out/click events                            * 
*******************************************************************************/   
function buttonMouseOver(button) {
  button.style.background = '#ef7311'; 
  button.style.borderColor = '#ef7311';
  button.style.color = '#ffffff';   
  button.style.cursor='pointer';
}

function buttonMouseOut(button) {
  button.style.background = 'white'; 
  button.style.color = '#555';
  button.style.border ='1px solid #ef7311';
}

function buttonClicked(button) {
   if (button.id == 'register') {
      window.location='/eceSite/register.jsp';
   } 
   else if (button.id == 'login') {
      var userName = d.getElementById('loginForm').userName.value;
      var password = d.getElementById('loginForm').password.value;   
      window.location='/admin/AdminController?pageId=loginPage&uid=' + userName + '&pwd=' + password;        
      //window.location='/ece/admin/AdminController?actionClass=tuc.ced.music.ecesite.admin.actions.ShowMainPageAction&pageId=loginPage&uid=' + userName + '&pwd=' + password;
   }
   else if (button.id == 'closeAnnouncement') {
      d.getElementById('announcementPresentation').style.visibility = 'hidden';
   }
   else if (button.id == 'en_lang') {                
        changeLanguage('en');
   }
   else if (button.id == 'gr_lang') {                         
        changeLanguage('gr');    
   }
   else if (button.id == 'createFolder') {     
    var dirName = d.getElementById("createFolderForm").dirName.value;
    window.location='/ece/admin/AdminController?pageId=userWorkspaceView&action=createFolder&actionClass=createFolder&dirName=' +  dirName;
    d.getElementById('dirCreationDialogId').style.visibility = 'hidden';
   }
   else if ( button.id == 'cancelCreateFolder') {
    d.getElementById('dirCreationDialogId').style.visibility = 'hidden';
   }
   else if (button.id == 'uploadFile') {     
    var fileName = d.getElementById("uploadFileForm").fileName.value;
    window.location='/ece/admin/AdminController?pageId=userWorkspaceView&action=uploadFile&actionClass=uploadFile&fileName=' +  fileName;
    d.getElementById('fileChooserDialogId').style.visibility = 'hidden';
   }
   else if ( button.id == 'cancelUploadFile') {
    d.getElementById('fileChooserDialogId').style.visibility = 'hidden';
   } 
   else if (button.id == 'deleteAnnouncement') {     
    var announcementId = d.getElementById("deleteAnnouncementForm").announcementId.value;
    showDataInBody('admin/AdminController?pageId=announcementsListView&action=deleteAnnouncement&actionClass=deleteAnnouncement&id=' +  announcementId);        
    d.getElementById('confirmAnnouncementDeletionDialogId').style.visibility = 'hidden';
   }
   else if ( button.id == 'cancelAnnouncementDeletion') {
    d.getElementById('confirmAnnouncementDeletionDialogId').style.visibility = 'hidden';
   }
   else if (button.id == 'createAnnouncement') {         
    var from = d.getElementById("newAnnouncementForm").from.value;
    var title = d.getElementById("newAnnouncementForm").title.value;
    var announcementBody = d.getElementById("newAnnouncementForm").announcementBody.value;
    var attachment = d.getElementById("newAnnouncementForm").attachment.value;
            
    if (from == null || title == null || announcementBody == null) {
      alert("empty values");
    } else {
      var action = "admin/AdminController?pageId=announcementNewView&action=createNewAnnouncement&actionClass=createNewAnnouncement";
      action += "&from=" + from;
      action += "&title=" + title;
      action += "&announcementBody=" + announcementBody;
      action += "&attachment=" + attachment;
    
      createAnnouncement(d.getElementById("newAnnouncementForm"), action);
      //showDataInBody(action);            
    }
   }
}

/****************************************************************************** 
* functions for handling the change language event
*******************************************************************************/   
function changeLanguage(lang) {
     
  var pageId = d.getElementById('pageId').value;  
  var pos = window.location.href.indexOf("&lang="); 
  var len = window.location.href.length;
  var firstPart = window.location.href.substring(0, pos);  
  var chapter, section, newLocation;
  
  var currentDate = new Date();
    
  if (pageId == 'firstPage') {               
     if (pos == -1) {       
       if ( window.location.href.charAt(len-1) == '#' ) {         
        window.location = window.location.href.substring(0, len-1) + '&lang=' + lang + "&ms=" + currentDate.getTime();           
       }
       else {
         window.location += '&lang=' + lang + "&ms=" + currentDate.getTime();                    
       }
     }
     else {                        
         window.location = firstPart + '&lang=' + lang + "&ms=" + currentDate.getTime();                    
     }
  }
  else if (pageId == 'blockInfoView') {          
    chapter = d.getElementById("pageInfoForm").blockId.value; 
    section = '0';
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";        
    
    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);
            
    newLocation = "Controller?actionClass=showBlockInfo";
    newLocation += "&block_id=" + chapter;
    newLocation += "&lang=" + lang;  
            
    showDataInBody(newLocation);
  
  }
  else if (pageId == 'generalInfoView') { 
    /*
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    newLocation = "Controller?actionClass=showGeneralInfo";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";
        
    window.location=baseURL + newLocation + "&ms=" + currentDate.getTime();   
    */   
     
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
             
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    //newLocation += "&action=fromFirstPage";        
    
    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);        
    
    newLocation = "Controller?actionClass=showGeneralInfo";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    
    setTimeout(function(){showDataInBody(newLocation);}, 700);
    //showDataInBody(newLocation);
        
    
  }
  else if (pageId == 'labListBySectorView') {  
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    
    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);
    
    newLocation = "Controller?actionClass=showLabListBySector";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    
    setTimeout(function() {showDataInBody(newLocation);}, 700);  
    
    /*
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    newLocation = "Controller?actionClass=showLabListBySector";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";
        
    window.location=baseURL + newLocation + "&ms=" + currentDate.getTime();                       
   */
  }
  else if (pageId == 'labInfoView') {     
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    var labId = d.getElementById("pageInfoForm").labId.value;
        
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";        
    
    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);
    
    var newBodyLocation = "Controller?actionClass=showLabInfo";
    newBodyLocation += "&lab_id=" + labId;    
    newBodyLocation += "&chapter=" + chapter;
    newBodyLocation += "&section=" + section;
    newBodyLocation += "&lang=" + lang;    
    
    setTimeout(function(){showLabInfo(newBodyLocation);}, 700);    
  }
  else if (pageId == 'sectorInfoView') {     
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    var sectorId = d.getElementById("pageInfoForm").sectorId.value;
        
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;        
    
    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);
    
    var newBodyLocation = "Controller?actionClass=showSectorInfo";
    newBodyLocation += "&sector_id=" + sectorId;    
    newBodyLocation += "&chapter=" + chapter;
    newBodyLocation += "&section=" + section;
    newBodyLocation += "&lang=" + lang;    
    
    setTimeout(function(){showLabInfo(newBodyLocation);}, 700);    
  }
  else if (pageId == 'professorListView') {  
  /*
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    newLocation = "Controller?actionClass=showProfessorList";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";
    
    window.location=baseURL + newLocation + "&ms=" + currentDate.getTime();                       
    */
   
   chapter = d.getElementById("pageInfoForm").chapterId.value;
   section = d.getElementById("pageInfoForm").sectionId.value;
   
   newLocation = "Controller?actionClass=showSidebar";
   newLocation += "&chapter=" + chapter;
   newLocation += "&section=" + section;
   newLocation += "&lang=" + lang;  
    
   showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
   showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
   showSidebar(newLocation, chapter);
   
   newLocation = "Controller?actionClass=showProfessorList";
   newLocation += "&chapter=" + chapter;
   newLocation += "&section=" + section;
   newLocation += "&lang=" + lang;
   
   setTimeout(function(){showDataInBody(newLocation);}, 700);
    
  }
  else if (pageId == 'professorInfoView') {      
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    var person = d.getElementById("pageInfoForm").personId.value;
    
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";
        
    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);
    
    var newBodyLocation = "Controller?actionClass=showProfessorInfo";
    newBodyLocation += "&person_id=" + person;    
    newBodyLocation += "&chapter=" + chapter;
    newBodyLocation += "&section=" + section;
    newBodyLocation += "&lang=" + lang;
    
    setTimeout(function(){showPersonCV(newBodyLocation);}, 700);
  }
  else if (pageId == 'personnelListView') {     
     /*
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    newLocation = "Controller?actionClass=showPersonnelList";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";
    
    window.location=baseURL + newLocation + "&ms=" + currentDate.getTime();                      
   */
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;  

    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);
    
    newLocation = "Controller?actionClass=showPersonnelList";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    
    setTimeout(function(){showDataInBody(newLocation);}, 700);
  }
  else if (pageId == 'personnelInfoView') {     
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    var person = d.getElementById("pageInfoForm").personId.value;
    
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";
    
    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);
    
    var newBodyLocation = "Controller?actionClass=showPersonnelInfo";
    newBodyLocation += "&person_id=" + person;    
    newBodyLocation += "&chapter=" + chapter;
    newBodyLocation += "&section=" + section;
    newBodyLocation += "&lang=" + lang;
    
    setTimeout(function(){showPersonCV(newBodyLocation);}, 700);
  }
  else if (pageId == 'labStaffListView') {   
/*     
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    newLocation = "Controller?actionClass=showLabStaffList";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";
    
    window.location=baseURL + newLocation + "&ms=" + currentDate.getTime();                       
     */
    
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;  

    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);
    
    newLocation = "Controller?actionClass=showLabStaffList";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    
    setTimeout(function(){showDataInBody(newLocation);}, 700);
  }
  else if (pageId == 'labStaffInfoView') {     
    chapter = d.getElementById("pageInfoForm").chapterId.value;
    section = d.getElementById("pageInfoForm").sectionId.value;
    var person = d.getElementById("pageInfoForm").personId.value;
    
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";
    
    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);
    
    var newBodyLocation = "Controller?actionClass=showLabStaffInfo";
    newBodyLocation += "&person_id=" + person;    
    newBodyLocation += "&chapter=" + chapter;
    newBodyLocation += "&section=" + section;
    newBodyLocation += "&lang=" + lang;
    
    setTimeout(function(){showPersonCV(newBodyLocation);}, 700);
  }
  /*
  else if (pageId == 'announcementListView') {     
    chapter = '0';
    section = '0';
    
    newLocation = "Controller?actionClass=showSidebar";
    newLocation += "&chapter=" + chapter;
    newLocation += "&section=" + section;
    newLocation += "&lang=" + lang;
    newLocation += "&action=fromFirstPage";
    
    showHeader("Controller?actionClass=showHeaderFooter&action=showHeader&lang=" + lang);
    showFooter("Controller?actionClass=showHeaderFooter&action=showFooter&lang=" + lang);
    showSidebar(newLocation, chapter);
    
    var newBodyLocation = "Controller?actionClass=showAnnouncementList";
    newBodyLocation += "&action=show";
    newBodyLocation += "&lang=" + lang;
    
    showDataInBody(newBodyLocation);
  }
  */
}

/****************************************************************************** 
*  programmatically trigger an onClick event on the input parameter           * 
*******************************************************************************/   
function triggerOnClickEvent(elementId) {   
   
  var target=d.getElementById(elementId);                     
  
  if (d.dispatchEvent) { // W3C
      var oEvent = d.createEvent( "MouseEvents" );
      oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);      
      target.dispatchEvent( oEvent );
      
   }
   else if(d.fireEvent) { // IE
      target.fireEvent("onclick");      
   }
}

/****************************************************************************** 
*  pause the execution for the requested milliseconds                         * 
*******************************************************************************/   
function pause( millis ) {
  var date = new Date();
  var curDate = null;

  do { curDate = new Date(); }
  while(curDate-date < millis);
} 
