// --------------- Functions to handle employees -----------------------
function employee_add(){
  var c='';
  var inRow=parseInt(document.getElementById('no').value)+1;
  var o=document.getElementById('listing').insertRow(inRow);
  c=o.insertCell(-1);c.width=12;c.style.verticalAlign='bottom';c.innerHTML='<INPUT TYPE="hidden" NAME="id_'+inRow+'" VALUE="0" STYLE="width:20px;"><A HREF="javascript:employee_moveup('+inRow+');"><IMG SRC="gfx/up.gif" BORDER="0"></A><BR><A HREF="javascript:employee_movedown('+inRow+');"><IMG SRC="gfx/down.gif" BORDER="0"></A><BR><A HREF="javascript:employee_delete('+inRow+');"><IMG SRC="gfx/delete.gif" BORDER="0"></A>';
  c=o.insertCell(-1);c.style.verticalAlign='bottom';c.innerHTML='<IMG ID="img_'+inRow+'" SRC="gfx/employee.jpg"><INPUT TYPE="hidden" NAME="pic_'+inRow+'" VALUE="" STYLE="width:85px;">&nbsp;<A HREF="javascript:poppic(\'empl\',\'big.pic_0\',\'picupdate('+inRow+')\',0);"><IMG SRC="gfx/edit.gif" BORDER="0"></A><A HREF="javascript:employee_picdelete('+inRow+');"><IMG SRC="gfx/delete.gif" BORDER="0"></A>';
  c=o.insertCell(-1);c.style.textAlign='right';c.style.verticalAlign='bottom';c.innerHTML='<TABLE CELLSPACING="0" CELLPADDING="0"><TR><TD>Name :</TD><TD>&nbsp;<INPUT TYPE="text" NAME="navn_'+inRow+'" VALUE="" STYLE="width:520px;"></TD></TR><TR><TD>Title :</TD><TD>&nbsp;<INPUT TYPE="text" NAME="tittel_'+inRow+'" VALUE="" STYLE="width:520px;"></TD></TR><TR><TD>Phone :</TD><TD>&nbsp;<INPUT TYPE="text" NAME="telefon_'+inRow+'" VALUE="" STYLE="width:520px;"></TD></TR><TR><TD>Email :</TD><TD>&nbsp;<INPUT TYPE="text" NAME="email_'+inRow+'" VALUE="" STYLE="width:520px;"></TD></TR></TABLE>';
  document.getElementById('listing').rows[inRow].style.paddingBottom=10;
  document.getElementById('no').value=inRow;
  document.getElementById('navn_'+inRow).focus();
}

function employee_delete(t){
  var i=0;
  if(confirm('This will remove employee from list')){
    for(i=parseInt(t);i<parseInt(document.getElementById('no').value);i++){employee_movedown(i)}
    document.getElementById('listing').deleteRow(parseInt(document.getElementById('no').value));
    document.getElementById('no').value=parseInt(document.getElementById('no').value)-1;
  }
}

function employee_moveup(t){
  if(t<1){
    alert('Cannot move up.\n\nThis is the top employee');
  }else{
    var id=document.getElementById('id_'+(parseInt(t)-1)).value;
    var img=document.getElementById('pic_'+(parseInt(t)-1)).value;
    var navn=document.getElementById('navn_'+(parseInt(t)-1)).value;
    var tittel=document.getElementById('tittel_'+(parseInt(t)-1)).value;
    var telefon=document.getElementById('telefon_'+(parseInt(t)-1)).value;
    var email=document.getElementById('email_'+(parseInt(t)-1)).value;
    document.getElementById('id_'+(parseInt(t)-1)).value=document.getElementById('id_'+(parseInt(t))).value;
    document.getElementById('pic_'+(parseInt(t)-1)).value=document.getElementById('pic_'+(parseInt(t))).value;
    document.getElementById('navn_'+(parseInt(t)-1)).value=document.getElementById('navn_'+(parseInt(t))).value;
    document.getElementById('tittel_'+(parseInt(t)-1)).value=document.getElementById('tittel_'+(parseInt(t))).value;
    document.getElementById('telefon_'+(parseInt(t)-1)).value=document.getElementById('telefon_'+(parseInt(t))).value;
    document.getElementById('email_'+(parseInt(t)-1)).value=document.getElementById('email_'+(parseInt(t))).value;
    document.getElementById('id_'+(parseInt(t))).value=id;
    document.getElementById('pic_'+(parseInt(t))).value=img;
    document.getElementById('navn_'+(parseInt(t))).value=navn;
    document.getElementById('tittel_'+(parseInt(t))).value=tittel;
    document.getElementById('telefon_'+(parseInt(t))).value=telefon;
    document.getElementById('email_'+(parseInt(t))).value=email;
    picupdate(parseInt(t)-1);
    picupdate(parseInt(t));
  }
}

function employee_movedown(t){
  if(t>parseInt(document.getElementById('no').value)-1){
    alert('Cannot move down.\n\nThis is the last employee');
  }else{
    var id=document.getElementById('id_'+(parseInt(t)+1)).value;
    var img=document.getElementById('pic_'+(parseInt(t)+1)).value;
    var navn=document.getElementById('navn_'+(parseInt(t)+1)).value;
    var tittel=document.getElementById('tittel_'+(parseInt(t)+1)).value;
    var telefon=document.getElementById('telefon_'+(parseInt(t)+1)).value;
    var email=document.getElementById('email_'+(parseInt(t)+1)).value;
    document.getElementById('id_'+(parseInt(t)+1)).value=document.getElementById('id_'+(parseInt(t))).value;
    document.getElementById('pic_'+(parseInt(t)+1)).value=document.getElementById('pic_'+(parseInt(t))).value;
    document.getElementById('navn_'+(parseInt(t)+1)).value=document.getElementById('navn_'+(parseInt(t))).value;
    document.getElementById('tittel_'+(parseInt(t)+1)).value=document.getElementById('tittel_'+(parseInt(t))).value;
    document.getElementById('telefon_'+(parseInt(t)+1)).value=document.getElementById('telefon_'+(parseInt(t))).value;
    document.getElementById('email_'+(parseInt(t)+1)).value=document.getElementById('email_'+(parseInt(t))).value;
    document.getElementById('id_'+(parseInt(t))).value=id;
    document.getElementById('pic_'+(parseInt(t))).value=img;
    document.getElementById('navn_'+(parseInt(t))).value=navn;
    document.getElementById('tittel_'+(parseInt(t))).value=tittel;
    document.getElementById('telefon_'+(parseInt(t))).value=telefon;
    document.getElementById('email_'+(parseInt(t))).value=email;
    picupdate(parseInt(t)+1);
    picupdate(parseInt(t));
  }
}

function employee_picdelete(t){
  if(confirm('This will delete picture')){
    document.getElementById('pic_'+(parseInt(t))).value='';
    picupdate(t);
  }
}


// --------------- Functions to decrypt mail addresses -----------------------
var rot13map;

function reverseMe(orig){splitext=orig.split("");revertext=splitext.reverse();reversed=revertext.join("");return reversed;}

function rot13init(){
  var map = new Array();
  var s   = "abcdefghijklmnopqrstuvwxyz";
  for (i=0; i<s.length; i++){map[s.charAt(i)]=s.charAt((i+13)%26)};
  for (i=0; i<s.length; i++){map[s.charAt(i).toUpperCase()]=s.charAt((i+13)%26).toUpperCase()};
  return map;
}

function rot13(a){
  if (!rot13map){rot13map=rot13init()};
  s = "";
  for (i=0; i<a.length; i++){
    var b = a.charAt(i);
    s	+= (b>='A' && b<='Z' || b>='a' && b<='z' ? rot13map[b] : b);
  }
  return s;
}

function doemail(inEmail){
  var resEmail=rot13(reverseMe(inEmail));
  document.write('email: <A HREF="mailto:'+resEmail+'">'+resEmail+'</A>');
}


// ------------- Functions for Google Earth -------------------------
function loadEarth(){google.earth.createInstance('map', initCB, failureCB)}
function GUnload(){}
function failureCB(errorCode){}

function SetPos(inPos) {
  var LatLon=inPos.split(',');
  var la = ge.createLookAt('');
//  la.set(String(LatLon[0]),String(LatLon[1]),0,ge.ALTITUDE_RELATIVE_TO_GROUND,0,0,9000000);
  la.set(String(LatLon[0]),String(LatLon[1]),0,ge.ALTITUDE_RELATIVE_TO_GROUND,0,0,LatLon[2]);
  ge.getView().setAbstractView(la);
}

function initCB(instance) {
  ge = instance;
  ge.getWindow().setVisibility(true);
  ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
  ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
//  ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
  var la = ge.createLookAt('');
  la.set(50,20,0,ge.ALTITUDE_RELATIVE_TO_GROUND,0,0,5500000);
  ge.getView().setAbstractView(la);
  loadKml('');
}

function loadKml(criteria) {
  var kmlUrl = '';
  if(criteria==''){criteria='0'};
  if(typeof(admin)!="undefined"){criteria+='&admin=true'}
  kmlUrl=GetURL() + 'getPos.asp?criteria='+criteria+'&t='+new Date();
  if (currentKmlObjects['all']) {ge.getFeatures().removeChild(currentKmlObjects['all']);currentKmlObject=null;}
  google.earth.fetchKml(ge, kmlUrl, function(kmlObject) {
    if (kmlObject) {
      currentKmlObjects['all'] = kmlObject;
      ge.getFeatures().appendChild(kmlObject);
    } else {
      currentKmlObjects['all'] = null;
      setTimeout(function() {alert('Bad or null KML.');}, 0);
    }
  });
}

  function GetURL(){
    var Orig=document.location.href;
    var Dest='';
    var URLsplit=Orig.split('/');
    for(var i=0;i<URLsplit.length-1;i++){Dest+=URLsplit[i]+'/'}
    return Dest;
  }

function PDFupdate(){
  if(document.big.pdf.value!=''){
    document.getElementById('PDFfile').rows[0].cells[0].innerHTML='<A HREF="javascript:getPDF_tmp(document.big.pdf.value);">Download PDF</A>&nbsp;&nbsp;<A HREF="javascript:delPDF();"><IMG SRC="gfx/delete.gif" BORDER="0"></A>';
  }else{
    document.getElementById('PDFfile').rows[0].cells[0].innerHTML='';
  }
};

function getPDF_tmp(inFile){
  with(document.getElementById('dl')){innerHTML='<iframe height=0 width=0 src=\'adm_newsPDF.asp?file='+inFile+'\'></iframe>';style.display='block'};
}

function delPDF(){
  if(confirm('This will DELETE the PDF document')){;
    document.big.pdf.value='';
    PDFupdate();
  }
}

function litebilde_update(){
  if(document.big.litebildedata.value==''){
    document.getElementById('kortbilde').src='gfx/spacer.gif';
    document.getElementById('zzz').rows[0].cells[1].innerHTML='&nbsp;&nbsp;<A HREF="javascript:poppic(\'tmb\',\'big.litebildedata\',\'bilder_update()\',0);">add</A>';
  }else{
    document.getElementById('kortbilde').src='images/'+document.big.litebildedata.value;
    document.getElementById('zzz').rows[0].cells[1].innerHTML='&nbsp;&nbsp;<A HREF="javascript:if(confirm(\'This will remove the picture\')){document.big.litebildedata.value=\'\';litebilde_update()};"><IMG SRC="gfx/delete.gif" BORDER="0"></A><BR>&nbsp;&nbsp;<A HREF="javascript:poppic(\'tmb\',\'big.litebildedata\',\'bilder_update()\',0);"><IMG SRC="gfx/edit.gif" BORDER="0"></A>';
  }
};

function bilde_update(){
  if(document.big.litebildedata.value==''){
    document.getElementById('kortbilde').src='gfx/spacer.gif';
    document.getElementById('zzz').rows[0].cells[1].innerHTML='&nbsp;&nbsp;<A HREF="javascript:poppic(\'small\',\'big.litebildedata\',\'bilder_update()\',0);">add</A>';
  }else{
    document.getElementById('kortbilde').src='images/'+document.big.litebildedata.value;
    document.getElementById('zzz').rows[0].cells[1].innerHTML='&nbsp;&nbsp;<A HREF="javascript:if(confirm(\'This will remove the picture\')){document.big.litebildedata.value=\'\';litebilde_update()};"><IMG SRC="gfx/delete.gif" BORDER="0"></A><BR>&nbsp;&nbsp;<A HREF="javascript:poppic(\'small\',\'big.litebildedata\',\'bilder_update()\',0);"><IMG SRC="gfx/edit.gif" BORDER="0"></A>';
  }
};

function storebilder_update(){
  var i=0;
  var intHTML='';
  var myRow=document.getElementById('piclistings');
  for(i=0;myRow.rows[0].cells.length>0;i++){myRow.rows[0].deleteCell();}
  i=0;
  if(document.big.bilderdata.value!=''){
    var picArr=document.big.bilderdata.value.split(';');
    for(i=0;i<picArr.length;i++){addcell('piclistings',0,'<TABLE CELLSPACING="0" CELLPADDING="0"><TR><TD VALIGN="bottom"><IMG SRC="images/'+picArr[i]+'" WIDTH="50"></TD><TD>&nbsp;</TD><TD VALIGN="bottom"><A HREF="javascript:pic_moveup('+i+');"><IMG SRC="gfx/up.gif" BORDER="0"></A><BR><A HREF="javascript:pic_movedown('+i+');"><IMG SRC="gfx/down.gif" BORDER="0"></A><BR><A HREF="javascript:pic_delete('+i+');"><IMG SRC="gfx/delete.gif" BORDER="0"></A><BR><A HREF="javascript:poppic(\'small\',\'big.bilderdata\',\'bilder_update()\','+i+');"><IMG SRC="gfx/edit.gif" BORDER="0"></A></TD><TD>&nbsp;&nbsp;&nbsp;</TD></TR></TABLE>')}
  };
  addcell('piclistings',0,'<TABLE CELLSPACING="0" CELLPADDING="0"><TR><TD><BR><BR><A HREF="javascript:poppic(\'small\',\'big.bilderdata\',\'bilder_update()\','+i+');">add</A></TD></TR></TABLE>');
};

function addcell(inTable,inRow,inHTML){
  var o=document.getElementById(inTable).rows[inRow].insertCell();
  o.innerHTML=inHTML;
}

function pic_moveup(inIndex){
  if(inIndex>0){;
    var picArr=document.big.bilderdata.value.split(';');
    var tmp=picArr[inIndex-1];
    picArr[inIndex-1]=picArr[inIndex];
    picArr[inIndex]=tmp;
    tmp=''
    for(var i=0;i<picArr.length;i++){tmp+=picArr[i]+';'}
    document.big.bilderdata.value=String(tmp).substring(0,tmp.length-1);
    storebilder_update()
  }else{
    alert('Unable to move up.\nThis picture is on top');
  }
}

function pic_movedown(inIndex){
  var picArr=document.big.bilderdata.value.split(';');
  if(inIndex<picArr.length-1){;
    var tmp=picArr[parseInt(inIndex)+1];
    picArr[parseInt(inIndex)+1]=picArr[inIndex];
    picArr[inIndex]=tmp;
    tmp=''
    for(var i=0;i<picArr.length;i++){tmp+=picArr[i]+';'}
    document.big.bilderdata.value=String(tmp).substring(0,tmp.length-1);
    storebilder_update()
  }else{
    alert('Unable to move down.\nThis picture is at the bottum');
  }
}

function pic_delete(inIndex){
  if(confirm('This will remove this picture from this list')){
    var tmp='';
    var picArr=document.big.bilderdata.value.split(';');
    for(var i=0;i<picArr.length;i++){if(i!=inIndex){tmp+=picArr[i]+';'}}
    document.big.bilderdata.value=String(tmp).substring(0,tmp.length-1);
    storebilder_update()
  }
}

function popglobe(i){
  var myPop=window.open('adm_maploc.asp?loc='+i,'map','height=800,width=800,statusbar=no,toolbar=no,menubar=no,scrollbars=no');
  myPop.focus();
}
function poppic(inType,inField,inFunc,inID){
  var myPop=window.open('adm_pics.asp?type='+inType+'&field='+inField+'&exit='+inFunc+'&id='+inID,'pic','height=400,width=800,statusbar=no,toolbar=no,menubar=no,scrollbars=yes');
  myPop.focus();
}

function popAtt(inFile){
  var myPop=window.open('pdf/'+inFile,'att','height=800,width=1024,statusbar=no,toolbar=no,menubar=no,scrollbars=yes');
  myPop.focus();
}


// ---------------- Misc functions --------------------------
function contactform_validate(){
  var res='';
  var o=document.contact;
  if(o.info.value==''){res='Contact body is empty';o.info.focus()}
  if(o.overskrift.value==''){res='Contact header is empty';o.overskrift.focus()}
  if(o.email.value==''){res='Your email address is left empty';o.email.focus()}
  if(o.telefon.value==''){res='Your telephone number is left empty';o.telefon.focus()}
  if(o.navn.value==''){res='Your name is left empty';o.navn.focus()}
  if(res==''){return true}else{alert(res);return false}
}

