/**
* Common helpful Script-tools
* (22/6/2009)
*@author carsten Schäfer
*/
var DOWNLOAD_INDICATOR = "__DOWNLOAD__";
var uriPrefix 				 = "/idcplg?IdcService=SS_GET_PAGE&nodeId=368&";
if(!fragmentAssetsPath) var fragmentAssetsPath = "/groups/jsp/documents/fragmentassets";

function makePopup(url, caption)
{
  var magnifyWin = window.open(uriPrefix+ "imageUrl=" + encodeURIComponent(url) + "&imageCaption="+ encodeURI(caption),
                    "XXX", "resizable=yes,scrollbars=yes");
}

function addTopOfPage()
{
  var sharpPos = document.location.href.indexOf('#');
  var lastUrl;
  if (sharpPos != -1) {
  lastUrl = document.location.href.substr(0,document.location.href.lastIndexOf('#'));
  } else {
    lastUrl = document.location.href;
  }
  document.write("<a href=\"" + lastUrl + "#topofpage\" ><img src=\"/groups/jsp/documents/fragmentassets/icon_topofpage_grey.gif\" class=\"navigationIcon\" alt=\"[Top of page]\" border=\"0\"></a>");
}

function getAnkerURL(anker, text)
{
  var sharpPos = document.location.href.indexOf('#');
  var lastUrl;
  if (sharpPos != -1) {
  lastUrl = document.location.href.substr(0,document.location.href.lastIndexOf('#'));
  } else {
    lastUrl = document.location.href;
  }
  return ("<a class=\"contentColouredAnkerBox\" href=\""
          + lastUrl
          + "#"
          + anker
          +"\">"
          +text
          +"</a>");
}

function putAnkerURL(anker, text)
{
  document.write(getAnkerURL(anker, text));
}

function putImageIcon(url){document.write(getImageIcon(url));}
function getImageIcon(url)
{
  var urlInfo = getTargetInfo(url);
  var isExt = urlInfo[0];
  var isDown = urlInfo[1];
  var icons = new Array("icon_link_coloured.gif", "icon_link_external_coloured.gif", "icon_download_coloured.gif");
  var linkTPL = "<img src='/groups/jsp/documents/fragmentassets/{icon}' class='navigationIcon' alt='{altPrefix} link' border='0' />"
  var temp1= "";
  var temp2= "";
  if(isExt)
  {
    temp1= linkTPL;
    temp1 = temp1.replace(/{icon}/g, icons[1]);
    temp1 = temp1.replace(/{altPrefix}/g, "External");
  }
  if(isDown)
  {
    temp2= linkTPL;
    temp2 = temp2.replace(/{icon}/g, icons[2]);
    temp2 = temp2.replace(/{altPrefix}/g, "Downloadable");
  }
  if(!isExt && !isDown)
  {
    temp1= linkTPL;
    temp1 = temp1.replace(/{icon}/g, icons[0]);
    temp1 = temp1.replace(/{altPrefix}/g, "Internal");
  }


  var ret = " "
    +temp1+temp2
    //+ (isExt  ? "<img src='/groups/jsp/documents/fragmentassets/icon_link_external_coloured.gif' class='navigationIcon' "  :"")
    //+ (isDown ? "<img src='/groups/jsp/documents/fragmentassets/icon_download_coloured.gif' class='navigationIcon' "  :"")
    //+ (!isExt && !isDown ? " <img src='/groups/jsp/documents/fragmentassets/icon_link_coloured.gif' class='navigationIcon' ":"")
    //+(" <img src='/groups/jsp/documents/fragmentassets/icon_"+(isDown ? "download":"link")+"_"+(isExt ? "external_":"")+"coloured.gif' class='navigationIcon' ")
    //+"alt='"+(isExt ? "external":"internal")+" link' border='0'>";
  return ret;
}

function getFormattedLink(write, url, text)
{
  text = text.replace(/'/g, "\'");
  var urlInfo = getTargetInfo(url);
  var ret = ""
    +(isOldLink(url) ? "<span style='color:red'>OLD LINK!</span>":"")
    +getImageIcon(url)
    +"<a href='"+url+"' class='contentColouredAnkerBox' title='"+text+"' "+(urlInfo[0] ? "target='_blank'":"")+">"
    +text+"</a>";
  if(write) document.write(ret);
  else return ret;

}
function getFormattedLinkID(write, id, text)
{
  text = text.replace(/'/g, "\'");
  var urlInfo = getTargetInfo(null);

  var ret = ""
    +getImageIcon(null)
    +"<a href=\""
    +"/idcplg?IdcService=SS_GET_PAGE&nodeId="+id//+"&siteId="+g_ssSourceSiteId

    +"\" class='contentColouredAnkerBox' title='"+text+"'>"
    +text+"</a>";

  if(write)
    document.write(ret);
  else
    return ret;

}

function getFormattedLinkDocID(write, id, section_id, text)//javascript:link('SP_1117714808104','92')
{
  text = text.replace(/'/g, "\'");
  var urlInfo = getTargetInfo(null);
  var section = section_id==-1 ? "":", '"+section_id+"'";
  //https://dev75-cms.eumetsat.int/idcplg?IdcService=SS_GET_PAGE&ssDocName=008277&ssTargetNodeId=115
  var ret = ""
      +getImageIcon(null)
      +"<a href=\""
      +"/idcplg?IdcService=SS_GET_PAGE&ssDocName="+id+(section_id==-1 ? "":"&ssTargetNodeId="+section_id)
      +"\" class='contentColouredAnkerBox' title='"+text+"'>"
  +text+"</a>";

  if(write)
    document.write(ret);
  else
    return ret;
}

function getFormattedLinkContentID(write, id, text)//javascript:link('SP_1117714808104','92')
{
  text = text.replace(/'/g, "\'");
  var uri = (location.href.indexOf("eumetsat.int")!=-1 ? "":"http://dev75.eumetsat.int")
            +"/idcplg?IdcService=GET_FILE&dDocName="+id+"&RevisionSelectionMethod=LatestReleased";
  var ret = ""
    +getImageIcon(DOWNLOAD_INDICATOR)
    +"<a href=\""
      +uri
    +"\" class='contentColouredAnkerBox' title='"+text+"'>"
    +text+"</a>";
  if(id && id.length > 30) ret ="<span style='color:red'>CONTENT-ID '"+id+"'["+id.length+"] too long! Max 30!</span>";

  if(write)
    document.write(ret);
  else
    return ret;

}

function getTargetInfo(url)
{
  var isExternal = false;
  var isDownloadable = false;
  if(url==null) return new Array(isExternal, isDownloadable);

  if(url.indexOf("script:node")!=-1 || url.indexOf("script:link")!=-1)isExternal = false;
  if( DOWNLOAD_INDICATOR==url
    || url.indexOf(".pdf")!=-1
    || url.indexOf(".jpg")!=-1
    || url.indexOf(".gif")!=-1
    || url.indexOf(".png")!=-1
    || url.indexOf(".avi")!=-1
    || url.indexOf(".mpg")!=-1
    || url.indexOf(".exe")!=-1
    || url.indexOf(".ppt")!=-1
    || url.indexOf(".txt")!=-1
    || url.indexOf(".zip")!=-1
    || url.indexOf(".tar")!=-1
    || url.indexOf(".gz")!=-1
    || url.indexOf(".tgz")!=-1
    || url.indexOf(".ccp")!=-1
    || url.indexOf(".c\"")!=-1
    || url.indexOf(".c'")!=-1
    )
  {
    isDownloadable = true;
    //if(  url.indexOf(".txt")!=-1)      isExternal = true;
  }

  if(url.indexOf("tp://")!=-1)isExternal = true;
  if(url.indexOf("#")==0){isExternal = false;isDownloadable = false;}
  return new Array(isExternal, isDownloadable);

}

/*Helps to scramble Email adresses*/
function putLetter(adr){putLetter(adr, null);}
function putLetter(adr, text)
{
  if(adr.indexOf(String.fromCharCode( 60 + 4 ))!=-1)alert("Please replace the "+String.fromCharCode( 60 + 4 )+" in your email adress by '#' - it will later be replaced by the webpage");
  adr = adr.replace(/#/, String.fromCharCode( 60 + 4 ))
  //adr = adr.replace(/add/, String.fromCharCode( 60 + 4 ))
  var prefix = "ma"
                +"il" + "t"+ "o:";
  document.writeln("<nobr><img src=\"/groups/jsp/documents/fragmentassets/icon_email_coloured.gif\" class=\"navigationIcon\""
   +"alt=\"[link]\" border=\"0\"><a class=\"contentColouredAnkerBox\""
   +"href=\""+prefix + adr +"\">"+(text==null ? adr:text)+"</a></nobr>");

}

function isOldLink(url)
{
  var meta = getTargetInfo(url);
  //(isExternal, isDownloadable)s
  if(url && url.length>0)
  {
    if(meta[0] && url.indexOf("//www.eumetsat.int")!=-1 )
      return url.indexOf("eumetsat.int/idcp")==-1;
  }
}

function verifyOneCheckedAtLeast(name, text)
{
	var elts = document.getElementsByName(name);
	var ret = false;
	for(var i=0; elts && i < elts.length;i++){
		//alert("<li>"+elts[i].value+" ==>"+elts[i].checked);
		ret = ret || elts[i].checked==true;
	}
	//alert(ret);
	if(ret==false && text)alert(text);
	return ret;
}

/**
* Tools
* added 24/11/2006 09:56
*/
var separateWindowObject = null;
function openWindow(url, w, h, winName, winDescr, centered)
{
  //winDescr = dependent=
  if(!winName){
    winName = '';
  }
  if(!winDescr){
    winDescr = '';
  }
  else{
    winDescr = ',' + winDescr;
  }
  var availX = screen.availWidth;
  var availY = screen.availHeight;
  if(availX < w) w = availX;
  if(availY < h) h = availX;
  var x = Math.round((availX - w)/ 2);
  var y = 20;//Math.round((availY - h) / 2);
  if(centered)
    winDescr = 'left=' + x + ',top=' + y + ',width=' + w + ',height=' + h + winDescr;
  else
    winDescr = 'left=' + 0 + ',top=' + 0 + ',width=' + w + ',height=' + h + winDescr;

  separateWindowObject =  open (url, winName, winDescr);
  separateWindowObject.focus();
  return separateWindowObject;
}

function openWindowNormal(url, w, h, winName, winDescr)
{
  openWindow(url, w, h, winName, winDescr, false);
}


function openWinCenter(url, w, h, winName, winDescr)
{
  openWindow(url, w, h, winName, winDescr, true);
}

function getTimeString()
{
          var loadTime = getUTCDate();
          return (addZero(loadTime.getDate())
                                + "." + addZero(loadTime.getMonth()+1)
                                + "." + loadTime.getFullYear()
                                + "&nbsp;&nbsp;"
                                + addZero(loadTime.getHours())
                                +":"+addZero(loadTime.getMinutes())
                                //+":"+addZero(loadTime.getSeconds())
                                )+" UTC";

}
function addZero(val )
{
    return val < 10 ? ("0"+val):val;
}


function getUTCDate()
{
  var now = new Date();
  var offsetMillies = now.getTimezoneOffset()*60*1000;
  nowMillis = now.getTime();
  var utcDate = new Date(nowMillis+offsetMillies)
  //alert(utcDate);
  return utcDate;
}

/*Some Cookie setters and getters*/
function createCookie(name, value, hours)
{
  if (hours) {
    var date = new Date();
    date.setTime(date.getTime()+(hours*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

function eraseCookie(name)
{
  createCookie(name, "", -1);
}

function gotoOtherInstance(instanceName){
  var loc    = location.href;
  var newurl = loc.replace(/[\w]{3,8}-cms/, instanceName+"-cms");
  if(loc!=newurl)
    location.href = newurl;
}

/**
*Adding functionality to the string-object
*/
function strtrim() {
  return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

/** Hide an element with certain ID. Text can be set*/
function displayBlock(eID, txt){
  var eIDObj = document.getElementById(eID);
  if(txt) eIDObj.innerHTML = txt;
  else if(txt=="") eIDObj.innerHTML = "";
  eIDObj.style.visibility ="visible";
  eIDObj.style.display    ="block";
}

/** (RE-)Display an element with certain ID as inline*/
function displayInline(eID){
  var eIDObj = document.getElementById(eID);
  eIDObj.style.visibility ="visible";
  eIDObj.style.display    ="inline";
}

/** (RE-)Display an element with certain ID as block. Text can be set*/
function hideBlock(eID, txt){
  var eIDObj = document.getElementById(eID);
  if(txt) eIDObj.innerHTML = txt;
  eIDObj.style.visibility ="hidden";
  eIDObj.style.display    ="none";
}

/** Refreshing the page without caching*/
function setPageRefresher(seconds){
  window.setTimeout("location.reload(true)", seconds*1000);
  //window.setTimeout("location.href = location.href", seconds*1000);
}

/**
* Append a JavaScript URL to the last Script in the header.
*/
function dhtmlLoadScript(url, setbottom, idObj)
{
   //document.write("<script src='"+url+"'></script>");
   //return;
   var eScript = document.createElement("script");
   eScript.src = url;
   eScript.type="text/javascript";
   if(idObj)eScript.id=idObj;
   var targetElts = null;
   if(setbottom)
    targetElts = document.getElementsByTagName("body");
   else
    targetElts = document.getElementsByTagName("head");

   if(targetElts){
	   targetElts[0].appendChild(eScript);
	 }
}

function dhtmlLoadScriptUpdate(idObj, url, nocache){
   var eScript = document.getElementById(idObj);
   var eScript2 = document.createElement("script");

   var newURL = url + (nocache ? "?"+Math.random():"");
   status = "new URL: "+newURL;
   displayBlock("downPlannedText", "new URL: "+newURL+" "+test);
   eScript2.src = newURL;
   eScript2.type= "text/javascript";
   eScript2.id  = idObj;

   var targetElts = null;
	 eScript.replaceChild(eScript, eScript2);

}


/** Inserts a CSS link tag to a CSS file*/
function insertCSSLink(url){
  document.writeln("\n<li"+""+"nk type=\"text/css\" rel='stylesheet' href='"+url+"' />");
}
