


function time2DateFormat(timestamp) {
  timeObj = new Date();
  timeObj.setTime(timestamp);
  // Very important: We have a local timestamp that we show without any 
  // timezone offset. Therefore, UTC:
  document.write((timeObj.getUTCDate() < 10 ? "0":"") + timeObj.getUTCDate() + "/" + 
		 (timeObj.getUTCMonth() < 9 ? "0":"") + (timeObj.getUTCMonth()+1) + "/" + 
		 (timeObj.getUTCFullYear() < 2000 ? (timeObj.getUTCFullYear() + 1900):timeObj.getUTCFullYear())  + " " + 
		 (timeObj.getUTCHours() < 10 ? "0":"") + timeObj.getUTCHours() + ":" + 
		 (timeObj.getUTCMinutes() < 10 ? "0":"") +  timeObj.getUTCMinutes() + "h");
}


function checkOrUncheckAllRadioButtons(field, checkflag) {
	if (checkflag) {
		for (i = 0; i < field.length; i++) {
			field[i].checked = true;
			}
	}
	else {
		for (i = 0; i < field.length; i++) {
			field[i].checked = false; 
			}

	}
}

function deleteCheckedDestinations()
{
  if (!confirm("Do you want to remove the selected destinations from the schedule?"))
    return;
  document.formScheduleDetails.action.value='deleteCheckedDestinations';
  document.formScheduleDetails.submit();
}

function deleteAllDestinations()
{
  if (!confirm("Do you want to remove the entire schedule? \r\nNote: Some older destinations may not be available from the ship any more."))
    return;
  if (!confirm("Remove now - are you sure?"))
    return;

  document.formScheduleDetails.action.value='deleteAllDestinations';
  document.formScheduleDetails.submit();
}
function printCheckedDestinations()
{
  document.formScheduleDetails.action.value='printCheckedDestinations';
  document.formScheduleDetails.submit();
}
function getCheckedDestinations() {
	retval = "";
	var checkInputs = document.getElementsByName('removeScheduleEntries[]');
	for (i=0; i<checkInputs.length; i++) {
		scheduleEntryId = checkInputs[i];
		if (scheduleEntryId.checked)
			retval += "&removeScheduleEntries%5B%5D=" + scheduleEntryId.value ;
	}
	return retval;
}
function Fensterweite () {
  if (window.innerWidth) {
    return window.innerWidth;
  } else if (document.body && document.body.offsetWidth) {
    return document.body.offsetWidth;
  } else {
    return 0;
  }
}

function Fensterhoehe () {
  if (window.innerHeight) {
    return window.innerHeight;
  } else if (document.body && document.body.offsetHeight) {
    return document.body.offsetHeight;
  } else {
    return 0;
  }
}
function getHeightOfObj(objId) {
	var obj = document.getElementById(objId);
	if (obj != null) {
		return obj.height;
	}
	return 0;
}
function getWidthOfObj(objId) {
	var obj = document.getElementById(objId);
	if (obj != null) {
		return obj.clientWidth;
	}
	return 0;
}
function resizeApplet(){
  try {
	  
	  var contentsHeight = 0;
	  // Unfortunately, IE and Firefox etc. behave strange when it comes to resizing. Therefore, we try to get the
	  // available size from the table and resize it here:
	  var tableObj = document.getElementById('mainTableId');
	  if (tableObj != null){
		 
		  var tableHeight = document.body.clientHeight;
		  
		// Get the sizes of the rows:
		  tableHeight -= getHeightOfObj('mainTableTopRowId');
		  tableHeight -= getHeightOfObj('mainTableSecondTopRowId');
		  tableHeight -= getHeightOfObj('mainTableMenuRowId');
		  tableHeight -= getHeightOfObj('mainTableFooterRowId');

		  contentsHeight = tableHeight;

		  var contentsRow = document.getElementById('mainTableContentRowId');
		  contentsRow.style.height=contentsHeight + "px";
	  }
	  
     var appletDivObj = document.getElementById('appletDiv');
     /*
   if (document.embeds["Fleettracker"] != null && document.embeds["Fleettracker"].setSize != null)
     document.embeds["Fleettracker"].setSize(Fensterweite()-80,Fensterhoehe() - 210);
   else if (document.applets["Fleettracker"] != null)
    document.applets["Fleettracker"].setSize(Fensterweite()-80,Fensterhoehe() - 210);
     */
     if (appletDivObj != null) {
    	 if (document.embeds["Fleettracker"] != null && document.embeds["Fleettracker"].setSize != null) {
    		 document.embeds["Fleettracker"].setSize(appletDivObj.parentNode.clientWidth - 20, appletDivObj.parentNode.clientHeight);
    	 }
    	 else if (document.applets["Fleettracker"] != null) {
    		 document.applets["Fleettracker"].setSize(appletDivObj.parentNode.clientWidth - 20, appletDivObj.parentNode.clientHeight);

    	 }
    	 appletDivObj.style.width = (appletDivObj.parentNode.clientWidth) + "px";
    	 appletDivObj.style.height = (appletDivObj.parentNode.clientHeight) + "px";
     }
     var divObj = document.getElementById('chart.silverchart');
     if (divObj != null) {
    	 divObj.style.width = (divObj.parentNode.clientWidth) + "px";
    	 divObj.style.height = (divObj.parentNode.clientHeight) + "px";
     }
     divObj = document.getElementById('schedule.silverschedule');
     if (divObj != null) {
    	 divObj.style.width = (divObj.parentNode.clientWidth) + "px";
    	 divObj.style.height = (divObj.parentNode.clientHeight) + "px";
     }
  }
  catch(ex) {
	  
	  // Important: Do NOT alert here! Otherwise, IE hangs up when loading JAVA...: 
    //alert("Error while trying to resize: " + String(ex));
	  
  }
}

function insertTableRow(tableId, rowId,  cssClassname,  valueArray, attrArray, attrValArray){
  
  var tableObj = document.getElementById(tableId);
  var TR = tableObj.tBodies[0].insertRow(tableObj.tBodies[0].rows.length);

  TR.setAttribute('className',cssClassname);
  TR.setAttribute('class',cssClassname);
  TR.setAttribute('id',rowId);
  
  for (i=0; i<valueArray.length; i++) {
    var TD1 = document.createElement("td");
    TD1.appendChild(valueArray[i]);
//     if (trim(TD1.innerText) == "")
//       TD1.innerText = "\u00a0";
    TD1.setAttribute('className',cssClassname);
    TD1.setAttribute('class',cssClassname);
    if (attrArray != null && attrArray[i] != null)
      TD1.setAttribute(attrArray[i], attrValArray[i]);
    TR.appendChild(TD1);
  }
}
function removeTableRow(tableId, rowId) {
  var tableObj = document.getElementById(tableId);
  for(var i=0;i<tableObj.tBodies[0].rows.length; i++) {
    var TR = tableObj.tBodies[0].rows[i];
    if (TR['id'] == rowId) {
      tableObj.tBodies[0].deleteRow(i);
      return;
    }
  }
}
function createHRefNode(ref, onclick, value) {
    var a = document.createElement("a");
    a.setAttribute("href", ref);

    // Note: This works in IE, too:
    a.onclick=new Function(onclick);
    var atext = document.createTextNode(value);
    a.appendChild(atext);
    return a;
}
function createButtonNode(value, onclick, title) {
    var a = document.createElement("input");
    a.setAttribute("type", "button");
    a.setAttribute("value", value);
    a.setAttribute("title", title);
    // Note: This works in IE, too:
    a.onclick=new Function(onclick);
    return a;
}

function trim(s) {
  if (s == null)
    return s;
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
  
function showHideAgentDetails(imageElem, divElem) {
  if (divElem.style.display == 'none') {
    imageElem.src = '../images/minusIcon.png';
    divElem.style.display = 'inline';
    imageElem.title='Click to hide agent details'
      }
  else {
    imageElem.src = '../images/plusIcon.png';
    divElem.style.display = 'none';
    imageElem.title='Click to show agent details'
      }
}
  
function selectTextOfItem(elem) {
	  if (document.selection && document.selection.createRange) {
	    var textRange = document.selection.createRange();
	    textRange.moveToElementText(elem);
	    textRange.select();
	  } else if (document.createRange && window.getSelection) {
	    var range = document.createRange();
	    range.selectNode(elem);
	    var selection = window.getSelection();
	    selection.removeAllRanges();
	    selection.addRange(range);
	}
}
function clearSelectTextOfItem(elem) {
	if (window.getSelection) {
		if (window.getSelection().empty) {  // Chrome
			window.getSelection().empty();
		} else if (window.getSelection().removeAllRanges) {  // Firefox
			window.getSelection().removeAllRanges();
		}
	} else if (document.selection) {  // IE?
		document.selection.empty();
	}

}
function getSelectedText() {
	
	if (document.selection == null && document.getSelection != null)
		var text = document.getSelection();
	
	if ((text == null || text == "") &&
			document.selection != null)
		text = document.selection.createRange().text;

	return text;
}
function copyToClipboard(item)
{
	
	var selText = getSelectedText();
	if (selText == null || selText == "")
		selectTextOfItem(item);
	var selText = getSelectedText();
	
    if (window.clipboardData) // IE
    {  
        window.clipboardData.setData("Text", selText);
    }
    else
    {  
    	try {
    	// You have to sign the code to enable this or allow the action in about:config by changing
		//user_pref("signed.applets.codebase_principal_support", true);
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

		var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;

		// create a transferable
		var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;

		// specify the data we wish to handle. Plaintext in this case.
		trans.addDataFlavor('text/unicode');

		// To get the data from the transferable we need two new objects
		var str = new Object();
		var len = new Object();

		var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);

		var copytext=selText;

		str.data=copytext;

		trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);

		var clipid=Components.interfaces.nsIClipboard;

		if (!clip) return false;

		clip.setData(trans,null,clipid.kGlobalClipboard);

    	}
    	catch(e) {
    		alert("Your browser does not allow to copy into the clipboard. Use Ctrl-c or right-click instead. The error was:\r\n" +e);
    	}		
    }
    clearSelectTextOfItem();
}

function showHideDeparture(checkboxShowHideDeparture) {
  if (checkboxShowHideDeparture.checked == 'checked') {
  		
  }
}

function openLinkInNewWindow(windowName,formname,hrefTarget) {
var win = window.open('about:blank',
		      windowName,
		      'width=1200,height=600,left=100,top=100,resizable=yes,scrollbars=yes');
   
// Very important: We can not set action in IE directly. This works:
 document.forms[formname].attributes['action'].value= hrefTarget;
 document.forms[formname].target=windowName;
 win.focus();
 document.forms[formname].submit();
 return false;
}

/**
 * Used in CAlarm.php.
 */
function showHideNextRows(inputObj, rowObj) {

  var showFlag = (inputObj.value == '+');
  showHideNextRow(showFlag, rowObj);
  if (showFlag)
    inputObj.value = '-';
  else
    inputObj.value = '+';
}
function showHideNextRow(showFlag, rowObj) {

  if(rowObj.nextSibling != null && 
	rowObj.nextSibling.firstChild.innerHTML == "") {
    if (showFlag)
      rowObj.nextSibling.style.display = "";
    else
      rowObj.nextSibling.style.display = "none";

    showHideNextRow(showFlag, rowObj.nextSibling);
  }
}
function showNewsLightBox() {
	 var newsDiv = document.getElementById('lightNewsBox');
	 var contentsDiv = document.getElementById('ftContentsDiv');
	 if (newsDiv != null) {
		 newsDiv.style.top = '0px'; 
		 newsDiv.style.visibility = 'visible';
	 }
	 if (contentsDiv != null) {
		 
		 contentsDiv.style.width = '0%'; 
		 contentsDiv.style.height = '0%';
		 contentsDiv.style.visibility = 'hidden';
	 }
}
function hideNewsLightBox() {
	 var newsDiv = document.getElementById('lightNewsBox');
	 var contentsDiv = document.getElementById('ftContentsDiv');
	 if (newsDiv != null) {
		 newsDiv.style.width = '0%'; 
		 newsDiv.style.height = '0%';
		 newsDiv.style.visibility = 'hidden';
	 }
if (contentsDiv != null) {
		 
		 contentsDiv.style.visibility = 'visible';
		 contentsDiv.style.width = '100%'; 
		 contentsDiv.style.height = '100%';
	 }
}
