function btnClicked(obj)
{
  alert(obj.id + " clicked. Label is: " + obj.innerText);
}

function writeButton(label, id, title)
{
  document.write('<table id="btn_' + id + '" cellspacing="0" cellpadding="0" border="0" onClick="btnClicked(this)" style="cursor: hand;" title="' + title + '"><tr><td width="11" height="18" class="clSTWButtonLeft"></td><td width="11" height="18" class="clSTWButtonMiddle">' + label + '</td><td width="11" height="18" class="clSTWButtonRight"></td></tr></table>');
}

function intRandom() {
  var s=Math.random().toString();
  var r='';
  for (var i = 3; i <= 12; i++) {
    r=r+s.charAt(i);
  }
  return r;
}

WebLinkWin = "";
function startWebLink(parms){
  if(WebLinkWin){
      if(WebLinkWin.closed){
          WebLinkWin = window.open("http://www.frontdesk.dk/weblink/req.wl?"+parms+"&RND="+intRandom(), "WebLinkWin", "top=" + ((screen.availHeight/2) - (480 / 2)) + ",left=" + ((screen.availWidth/2) - (640 / 2)) + ",width=640,height=480,resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0")        } else { 
          WebLinkWin.focus();
      }
  } else {
      WebLinkWin = window.open("http://www.frontdesk.dk/weblink/req.wl?"+parms+"&RND="+intRandom(), "WebLinkWin", "top=" + ((screen.availHeight/2) - (480 / 2)) + ",left=" + ((screen.availWidth/2) - (640 / 2)) + ",width=640,height=480,resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0")
  }
}

HelpWin = "";
function showHelp(parms){
  if(HelpWin){
      if(HelpWin.closed){
          HelpWin = window.open("http://www.frontdesk.dk/weblink/req.wl?"+parms+"&RND="+intRandom(), "HelpWin", "top=25,left=" + ((screen.availWidth) - 283) + ",width=250,height=447,resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0")
      } else { 
          HelpWin.focus();
      }
  } else {
         HelpWin = window.open("http://www.frontdesk.dk/weblink/req.wl?"+parms+"&RND="+intRandom(), "HelpWin", "top=25,left=" + ((screen.availWidth) - 283) + ",width=250,height=447,resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0")
  }
}

function wlSubmit(parms) {
  document.weblinkform.elements['GOTO'].value='';
  document.weblinkform.elements['RETURN'].value='';
  document.weblinkform.elements['CM'].value='';
  document.weblinkform.elements['FUN'].value='';
  document.weblinkform.elements['FUN2'].value='';
  document.weblinkform.elements['BUTTON'].value='';
  document.weblinkform.elements['FOCUS'].value='';
  document.weblinkform.elements['ID'].value='';
  document.weblinkform.elements['GID'].value='';
  document.weblinkform.elements['EID'].value='';
  var uparms = parms+'&RND='+intRandom();
  var parm = uparms.split("&");
  for (var i = 0; i < parm.length; i++)
    document.weblinkform.elements[parm[i].split('=')[0]].value = parm[i].split('=')[1]; 
  document.weblinkform.submit();
}

var used=0;
function submitOnEnter(e) {
  if (document.all) {
  e = window.event;
  }
  var key;
  if (document.layers)
    key = e.which;
  if (document.all)
    key = e.keyCode;
  if (key==13) {
    if (used==0) {
      used=1;
      if (document.layers)
        return false;
      else if (document.all)
        e.returnValue = false;
      document.weblinkform.submit();
    }
  }
}

function getQueryVariable(pVarName) {
  var url=document.URL.split('?');
  var parms=url[1].split('&'); 
  var varName=pVarName.toLowerCase();
  var vData='';
  for (var i = 0; i < parms.length; i++) {
    if (parms[i].split('=')[0].toLowerCase()==varName) {
      vData=parms[i].split('=')[1];
      break;
    } 
  }   
  return vData;
}

function getWLSTWParms() {
  var url=document.URL.split('?');
  var parms=url[1].split('&'); 
  var vData='';
  for (var i = 0; i < parms.length; i++) {
    if (parms[i].split('=')[0].toLowerCase().substring(0,5)=='wlstw') {
      if (!vData=='') vData=vData+'&';
      vData=vData+parms[i].split('=')[0].toLowerCase().substring(5,99)+'='+parms[i].split('=')[1];
    } 
  }   
  return vData;
}

function getWLStateID() { return getQueryVariable('wlframeid'); }

function getNewStateID() {
  var hexStr='0123456789ABCDEF';
  var wlStateID='';
  for (var i = 1; i <= 20; i++) {
    wlStateID=wlStateID+hexStr.charAt(Math.round((Math.random()*100)/7.14)+1);
  }
  return wlStateID;
}

function getWLIFrameWidth(wlWidth,wlAjustWidth) {
  if (wlWidth<=0) {
    totalWidth=document.documentElement.offsetWidth+wlWidth-4+wlAjustWidth;
  } else {
    totalWidth=wlWidth;
  }
  if (totalWidth<480) totalWidth=480;  
  return totalWidth;
}

function getWLIFrameHeight(wlHeight,wlAjustHeight) {
  if (wlHeight<=0) {
    if ((navigator.userAgent.indexOf('Firefox')!=-1) || (navigator.userAgent.indexOf('Opera')!=-1)) {
      totalHeight=document.body.clientHeight+wlHeight+wlAjustHeight;
    } else {
      totalHeight=document.documentElement.offsetHeight+wlHeight+wlAjustHeight;
      if (navigator.userAgent.indexOf('MSIE')!=-1) totalHeight=totalHeight-4;
    }
  } else {
    totalHeight=wlHeight;
  }
  if (totalHeight<360) totalHeight=360;
  return totalHeight;
}

var wlRLoadCalled     = 0;
var wlRLoadID         = '';
var wlRWidth          = 0;
var wlRAjustWidth     = 0;
var wlRHeight         = 0;
var wlRAjustHeight    = 0;
var wlLastTotalWidth  = 0;
var wlLastTotalHeight = 0;

function handleWebLinkIFrame(wlLoadID,wlWidth,wlAjustWidth,wlHeight,wlAjustHeight,wlParms) {  
  var wlStateID=getWLStateID();
  if (!wlStateID=='') {

    totalHeight=getWLIFrameHeight(wlHeight,wlAjustHeight);
    totalWidth=getWLIFrameWidth(wlWidth,wlAjustWidth);

    if (!wlParms=='') wlParms='&'+wlParms;

    document.getElementById(wlLoadID).innerHTML = '';

    wlRLoadID=wlLoadID;
    wlRWidth=wlWidth;
    wlRAjustWidth=wlRAjustWidth;
    wlRHeight=wlHeight;
    wlRAjustHeight=wlAjustHeight;
    wlLastTotalWidth=totalWidth;
    wlLastTotalHeight=totalHeight;

  } else {

  alert('Missing wlframeid from handleWebLinkIFrame ! Contact STW at support@stw.dk.');

  }
}

function loadWebLinkIFrame(wlLoadID,wlWidth,wlAjustWidth,wlHeight,wlAjustHeight,wlParms) {  
  if (wlRLoadCalled==0) {
    wlRLoadCalled=1;
    cParms=getWLSTWParms();
    if (cParms=='') {
      cParms=wlParms;
    } else {
      if (!wlParms=='') cParms=cParms+'&'+wlParms;
    }
    handleWebLinkIFrame(wlLoadID,wlWidth,wlAjustWidth,wlHeight,wlAjustHeight,cParms);
  }
}

var wlResizeTimer;

function doWLResizeTimer() {
  if (wlLastTotalHeight!=0) {
    totalHeight=getWLIFrameHeight(wlRHeight,wlRAjustHeight);
    totalWidth=getWLIFrameWidth(wlRWidth,wlRAjustWidth);
    var doUpdateIFrame=0;
    if (totalWidth!=wlLastTotalWidth) doUpdateIFrame=1;
    if (totalHeight!=wlLastTotalHeight) doUpdateIFrame=1;
    if (doUpdateIFrame==1) {
      handleWebLinkIFrame(wlRLoadID,wlRWidth,wlRAjustWidth,wlRHeight,wlRAjustHeight,''); 
    }
  }
} 

function resizeWebLinkIFrame() {  
  if (!wlRLoadID=='') {
    if (wlResizeTimer) clearTimeout(wlResizeTimer);
    wlResizeTimer=setTimeout("doWLResizeTimer()",100);
  }
}

function unloadWebLinkIFrame() {
  if (!wlRLoadID=='') {
    var wlStateID=getWLStateID();
    document.getElementById(wlRLoadID).innerHTML = '';
  }
}

function loadWebLinkIFramePage(page) {
  var parms='wlframeid='+getNewStateID();
  if (page.indexOf('?')==-1) {
    parms='?'+parms; } else {
    parms='&'+parms; }
  location.href=page+parms;
}

function doWebLinkCommand(wlLoadID,wlParms) {  
  var wlStateID=getWLStateID();
  if (!wlStateID=='') {
     document.getElementById(wlLoadID).innerHTML = '';
  } else {
  alert('Missing wlframeid from doWebLinkCommand ! Contact STW at support@stw.dk.');
  }
}

function doWebLinkReturnTo(wlParms) {  
  var wlStateID=getWLStateID();
  if (!wlStateID=='') {
     var exitTo='';
     if (!wlParms=='') exitTo='&exitto='+wlParms;
     location.href='http://www.frontdesk.dk/weblink/req.wl?browserid='+wlStateID+'&rnd='+intRandom()+'&cm=exit'+exitTo;
  } else {
  alert('Missing wlframeid from doWebLinkReturn ! Contact STW at support@stw.dk.');
  }
}

function loadWebLinkSearchBlock(wlLoadID,wlHotelID,wlWidth,wlHeight,wlParms) {  
  var wlUHeight = wlHeight;
  if (wlUHeight<100) wlUHeight=100;
  var wlUWidth = wlWidth;
  if (wlUWidth<100) wlUWidth=100;

  if (!wlParms=='') wlParms='&'+wlParms;

  document.getElementById(wlLoadID).innerHTML = '<iframe id="WebLinkSearchBlock" name="WebLinkSearchBlock" marginwidth="0" marginheight="0" frameborder="no" width="'+wlUWidth+'" height="'+wlUHeight+'" scrolling="no" src="http://www.frontdesk.dk/weblink/req.wl?'+
     'getsearchblock='+wlHotelID+'&height='+wlUHeight+'&width='+wlUWidth+'&rnd='+intRandom()+wlParms+'">Your browser does not support iframes!</iframe>';
}

function dispPicture(picRef) {
  winWidth=screen.availWidth-10;
  if (winWidth>790) winWidth=790;
  winHeight=Math.abs(winWidth/1.3333333333333);
  winTop=(screen.availHeight/2)-((winHeight+7.5)/2);
  if (winTop<10) winTop=0;
  winLeft=(screen.availWidth/2)-((winWidth+10)/ 2);
  if (winLeft<1) winLeft=1;
  dispWin=window.open(picRef, 'DispPicWin', 'top='+winTop+',left='+winLeft+',width='+winWidth+',height='+winHeight+',resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0');
  dispWin.focus();
}

WebLinkPartnerWin = "";
function startPartnerLink(pID,pPartnerID,pLang,pURL) {
  if(WebLinkPartnerWin){
      if(WebLinkPartnerWin.closed){
          WebLinkPartnerWin = window.open('http://www.frontdesk.dk/weblink/req.wl?startpartnerlink='+pID+'&partnerid='+pPartnerID+'&ln='+pLang+'&goto='+pURL+'&rnd='+intRandom(), '_blank', '');
      } else { 
          WebLinkPartnerWin.focus();
      }
  } else {
    WebLinkPartnerWin = window.open('http://www.frontdesk.dk/weblink/req.wl?startpartnerlink='+pID+'&partnerid='+pPartnerID+'&ln='+pLang+'&goto='+pURL+'&rnd='+intRandom(), '_blank', '');
  }
}
