if (typeof(pliant_interface) == 'undefined'){
  var _pliant = new function(){
var action_request;
var action_request_is_active = false;
var action_request_retry = 0;
var session_id;
var alive_request;
var alive_request_tries = 0;
var alive_timer;
var alive_sleep = 1;
var alive_now = false;
var ui_path='dummy';
var ready;
var session_end = false;
function alert_disconnect(txt) {
  if (typeof custom_alert_disconnect=='function') { custom_alert_disconnect(txt) }
  else { alert(txt); }
}
function adjust(f) {
  f.setAttribute('size',Math.max(f.value.length,1));
  frameadjust(0);
}
function adjust2(f) {
  nx = 1;
  ny = 1;
  i1 = 0;
  while (i1 < f.value.length) {
    i2 = f.value.indexOf('\n',i1);
    if (i2 == -1) { i2 = f.value.length; }
    nx = Math.max(nx,i2-i1);
    if (i2 < f.value.length) { ny = ny+1; }
    i1 = i2+1;
  }
  f.setAttribute('cols',nx);
  f.setAttribute('rows',ny);
  frameadjust(0);
}
function new_xhr() {
  return new XMLHttpRequest();
}
var pliant_jsmark;
function update_section(name,content) {
  if (name=='javascript' && content.indexOf(';')!=-1){
    var m;
    content = content.replace(/<p.*?>/,'');
    if (pliant_jsmark){
      m = content.lastIndexOf(pliant_jsmark);
      if (m>=0)
        content = content.substr(m+pliant_jsmark.length);
    }
    m = content.lastIndexOf('$');
    pliant_jsmark = content.substr(m);
    content = content.substr(0,m); 
    eval(content);
    return;
  }
  if (name=='invisible') return;
  var s=document.getElementById('pliant-'+name);
  if (!s)
    alert("section "+name+" doesn't exist");
  else {
    s.innerHTML = content;
    s.style.display = content.replace(/\r|\n|\s/g, '').length == 0 ? 'none' : '';
    var sections = s.getElementsByTagName('SPAN');
    for (var i=0; i<sections.length; i++)
      if ((sections[i].id||'').indexOf('pliant-')==0)
        if (sections[i].innerHTML.replace(/\r|\n|\s/g, '').length==0)
          sections[i].style.display = 'none';
    if (typeof(pliant_update_section)=='function') pliant_update_section.apply(this,[s,name,content]);
  }
}
this.init_section = function(id) {
  var s = document.getElementById(id);
  if (s.innerHTML.replace(/\r|\n|\s/g, '').length==0)
    s.style.display = 'none';
}
function apply(p) {
  m0 = -1;
  mm0 = p.indexOf('',0);
  if (mm0 >= 0) {
    mm1 = p.indexOf('',mm0+1);
    if (mm1 < 0) mm1 = p.length-1;
    var suffix=session_id ? '?sid='+session_id : '';
    window.location.href = p.substr(mm0+1,mm1-mm0-1)+suffix;
    return;
  }
  mm0 = p.indexOf('',0);
  if (mm0 >= 0) {
    mm1 = p.indexOf('',mm0+1);
    if (mm1 < 0) mm1 = p.length-1;
    eval(p.substr(mm0+1,mm1-mm0-1));
    m0 = p.indexOf('',mm1);
  }
  while (true) {
    m1 = p.indexOf('',m0+1);
    if (m1 == -1) {
      return;
    }
    m2 = p.indexOf('',m1+1);
    if (m2 == -1) {
      return;
    }
    if (p.substr(m0+1,1) == '') {
      m0 = m0+1;
      session_end = true;
    var suffix=session_id ? '?sid='+session_id : '';
      window.location.replace(p.substr(m0+1,m1-m0-1)+suffix);
      return;
    } else if (p.substr(m0+1,1) == '') {
      m0 = m0+1;
      eval(p.substr(m0+1,m1-m0-1));
    } else if (p.substr(m0+1,1) == '') {
      m0 = m0+1;
      top.wmain.scrollTo(parseInt(p.substr(m0+1,m1-m0-1)),parseInt(p.substr(m1+1,m2-m1-1)));
    } else {
        var name=p.substr(m0+1,m1-m0-1);
        var tmp=document.createElement('div');
        tmp.innerHTML = p.substr(m1+1,m2-m1-1);
        var content = '';
        try { content = tmp.getElementsByTagName('span')[0].innerHTML } catch(e) {}
        update_section(name, content);
    };
    m0 = m2;
  }
}
function help(h) {
  apply('help<span id="help">'+h+'</span>');
}
function change(f) {
  if (!session_id) active_setup();
  action_request = new_xhr();
  if (!action_request) {
    clearTimeout(alive_timer);
    alert('Unsupported  ? web ui');
    return;
  }
  action_request_is_active = true;
  action_request.onreadystatechange = action_result;
  action_request.open('POST', '/_/'+session_id+'/set/'+f.name+'/ui_path'+ui_path, true);
  wait_on();
  action_request.send(f.value);
}
function wait_on() {
  document.body.className = 'wait';
  if (typeof(pliant_custom_wait_on)=='function')
    pliant_custom_wait_on();
}
function wait_off() {
  document.body.className = '';
  if (typeof(pliant_custom_wait_off)=='function')
    pliant_custom_wait_off();
}
function layout() {
  return '?'+top.innerWidth+'/'+top.innerHeight
}
function run2(id) {
  if (!session_id) active_setup();
  action_request = new_xhr();
  if (!action_request) {
    clearTimeout(alive_timer);
    alert('Web browser  ? is not supported by Pliant HTTP proxy');
    return;
  }
  action_request_is_active = true;
  action_request.onreadystatechange = action_result;
  action_request.open('GET', '/_/'+session_id+'/run/'+id+'/ui_path'+ui_path+layout());
    wait_on();
  action_request.send('');
}
function run1(id) {
  if (!action_request_is_active) {
    run2(id);
  } else {
    if (action_request_retry > 0) {
      action_request_retry = action_request_retry-1;
      setTimeout(function(){ run1(id) } ,250);
    } else {
      action_request_is_active = false;
      run2(id);
    }
  }
}
function run(id) {
  action_request_retry = 20;
  run1(id);
}
function action_result() {
  if (action_request.readyState == 4) {
    clearTimeout(alive_timer);
    wait_off();
    try {
      if (action_request.status != 200) {
        if (!session_end) {
          alert_disconnect('Session is closed');
        }
      } else {
        apply(action_request.responseText);
        if (!alive_now) alive_now = true;
        alive_sleep = 1;
        alive_timer = setTimeout(alive,alive_sleep*1000);
      }
    } catch(e) {
      if (!session_end) {
        alert_disconnect('Server seems to be down');
      }
    }
    action_request_is_active = false;
  }
}
function ssr_display(id) {
  w = top.wmain;
  w = window;
  t = w.document.getElementById(id);
  size_x = parseInt(t.getAttribute('size_x'));
  size_y = parseInt(t.getAttribute('size_y'));
  ix0 = Math.floor((w.pageXOffset-t.offsetLeft)/256);
  iy0 = Math.floor((w.pageYOffset-t.offsetTop)/256);
  ix1 = Math.ceil((w.pageXOffset+w.innerWidth-t.offsetLeft)/256);
  iy1 = Math.ceil((w.pageYOffset+w.innerHeight-t.offsetTop)/256);
  for( iy = Math.max(iy0,0); iy < Math.min(iy1,Math.floor((size_y+255)/256)); iy++ ) {
    for (ix = Math.max(ix0,0); ix < Math.min(ix1,Math.floor((size_x+255)/256)); ix++ ) {
      c = w.document.getElementById(id+'/'+ix+'/'+iy);
      if( c.getAttribute('src') == '/_/empty.png' ) {
        x0 = ix*256;
        y0 = iy*256;
        x1 = Math.min((ix+1)*256,size_x);
        y1 = Math.min((iy+1)*256,size_y);
        c.setAttribute('src','/_/'+session_id+'/ssr/1/'+id+'/'+x0+'/'+y0+'/'+x1+'/'+y1+'/ui_path'+ui_path);
      }
    }
  }
}
function scrolled() {
  all = document.getElementsByName('ssr');
  for( i = 0; i < all.length; i++ ) {
    ssr_display(all[i].getAttribute('id'));
  }
}
function alive() {
  if (!session_id) active_setup();
  scrolled();
  if (action_request_is_active) {
    alive_timer = setTimeout(alive,1000);
    return;
  }
  alive_request = new_xhr();
  if (!alive_request) {
    alert('Unsupported  ? web ui');
    return;
  }
  alive_request_tries = (alive_request_tries + 1) % 3;
  alive_request.onreadystatechange = alive_result;
  alive_request.open('GET', '/_/'+session_id+'/alive'+'/ui_path'+ui_path, true);
  alive_request.send('');
}
function alive_result() {
  if (alive_request.readyState == 4) {
    try {
      if (alive_request.status != 200 && alive_request_tries==0) {
        if (!session_end) {
          alert_disconnect('Session is closed');
        }
      } else {
        if (alive_request.status == 200) {
          alive_request_tries = 0;
          apply(alive_request.responseText);
          alive_sleep = Math.min(alive_sleep*2,120);
        } else {
          alive_sleep = 1;
        }
        alive_timer = setTimeout(alive,alive_sleep*1000);
      }
    } catch(e) {
      if (!session_end) {
        alert_disconnect('Server seems to be down');
      }
    }
  }
}
var down_request;
function mousedown(id,x,y) {
  if (!session_id) active_setup();
  down_request = new_xhr();
  if (!down_request) {
    alert('Unsupported  ? web ui');
    return;
  }
  down_request.onreadystatechange = down_result;
  down_request.open('GET', '/_/'+session_id+'/event/press/button1/'+id+'/'+x+'/'+y+layout(), true);
  down_request.send('');
}
function down_result() {
  if (down_request.readyState == 4) {
    try {
      if (down_request.status != 200) {
        if (!session_end) {
          alert_disconnect('Session is closed');
        }
      } else {
        apply(down_request.responseText);
        if (!alive_now) alive_launch();
      }
    } catch(e) {
      if (!session_end) {
        alert_disconnect('Server seems to be down');
      }
    }
  }
}
var up_request
function mouseup(id,x,y) {
  if (!session_id) active_setup();
  up_request = new_xhr();
  if (!up_request) {
    alert('Unsupported  ? web ui');
    return;
  }
  up_request.onreadystatechange = up_result;
  up_request.open('GET', '/_/'+session_id+'/event/release/button1/'+id+'/'+x+'/'+y, true);
  up_request.send('');
}
function up_result() {
  if (up_request.readyState == 4) {
    try {
      if (up_request.status != 200) {
        if (!session_end) {
          alert_disconnect('Session is closed');
        }
      } else {
        apply(up_request.responseText);
       if (!alive_now) alive_launch();
      }
    } catch(e) {
      if (!session_end) {
        alert_disconnect('Server seems to be down');
      }
    }
  }
}
function frameadjust(retries) {}
function generate_random(len) {
  var chars = '123456790abcdefghijklmnopqrstuvwxtzABCDEFGHIJKLMNOPQRSTUVWXTZ';
  var result = '';
  for (var i=0; i<len; i++) {
    var index = Math.floor(Math.random() * chars.length);
    result += chars.substring(index,index+1);
  }
  return result;
}
function startup0(path,sid) {
  if (ready) return;
  ui_path=path;
  if (sid) { ready = true; session_id = sid; return }
  var have_sid=false;
  var i = window.location.search.indexOf('sid=');
  if (i != -1){
    var j = window.location.search.substr(i+4).indexOf('&');
    var l = window.location.search.length;
    var sid = window.location.search.substr(i+4, j!=-1 ? j : l);
    if (sid.length == 32 && sid.indexOf('Faeh2fa3')==0) { 
      session_id = sid;
      have_sid = true;
    }
  }
  if (document.cookie.indexOf('SID=Faeh2fa3') != -1) {
    window.name='';
    return;
  }
  if (document.cookie.indexOf('SID=ahW5shoh') == -1){
    if (window.name.indexOf('pliant_reload') == -1) {
      var gsid = 'ahW5shoh'+generate_random('24');
      var date = new Date();
      date.setTime(date.getTime()+(1440*60*1000));
      document.cookie = 'SID='+gsid+'; expires='+date.toGMTString()+'; path=/';
      if (!have_sid) {
        window.name = 'pliant_reload';
        var a0, a1, a2, a3, a4;
        a0='w' ; a1='indow.' ; a2='locat' ; a3='ion.rel' ; a4='oad()'
        eval(a0+a1+a2+a3+a4)
      }
    } else {
      window.name = '';
      if (!have_sid)
        window.location.search='?sid='+'Faeh2fa3'+generate_random('24');
    }
  } else {
    window.name='';
  }
  ready = true;
}
function active_setup() {
  if (!session_id)
    session_id = 'Faeh2fa3'+generate_random('24');
  if (document.cookie.indexOf('SID=Faeh2fa3') == -1) {
      var date = new Date();
      date.setTime(date.getTime()+(1440*60*1000));
      document.cookie = 'SID='+session_id+'; expires='+date.toGMTString()+'; path=/';
  }
}
function startup(param1,param2) {
  startup0(param1,param2);
  scrolled();
  if (typeof pliant_custom_startup=='function') pliant_custom_startup();
}
function startup_xy(x,y,extra) {
  startup0(extra);
  window.scrollTo(x,y);
  scrolled();
  if (typeof pliant_custom_startup=='function') pliant_custom_startup();
}
function alive_launch() {
  alive_now = true;
  alive_timer = setTimeout(alive,1000);
}
function upload_start(f, id) {
  var a = f.getElementsByTagName('INPUT');
  var upload = a[0].type=='file' ? a[0] : a[1];
  if (upload.value=='') return false;
  if (!session_id) active_setup();
  f.target = 'upload_iframe-'+id;
  f.action = '/_/'+session_id+'/upload/'+id+'/ui_path'+ui_path;
 document.getElementById('upload_wait-'+id).innerHTML='...';
  return true;
}
function upload_complete(id) {
  if (!alive_now) { alive_launch() } else { alive(); }
 document.getElementById('upload_wait-'+id).innerHTML='';
}
this.adjust=adjust;
this.adjust2=adjust2;
this.change=change;
this.frameadjust=frameadjust;
this.help=help;
this.upload_start=upload_start;
this.upload_complete=upload_complete;
this.mousedown=mousedown;
this.mouseup=mouseup;
this.run=run;
this.scrolled=scrolled;
this.startup_xy=startup_xy;
this.startup=startup;
this.startup0=startup0;
  }
}
var pliant_custom_startup_actions = new Array();
function pliant_custom_startup() {
  for (var i=0; i<pliant_custom_startup_actions.length; i++) {
    var action = pliant_custom_startup_actions[i];
    action();
  }
}
function pliant_custom_startup_xy(x,y){
  pliant_custom_startup();
}

(function(){
  var element;
  var timer;
  pliant_custom_wait_on = function(){
    if (!element){
      element = document.createElement("DIV");
      element.className = "please_wait_message";
      element.style.background = '#222';
      element.style.color = 'white';
      element.style.paddingLeft = '1em';
      element.style.paddingRight = '1em';
      //element.style.width = '200px';
      element.style.margin = 'auto';
      element.style.padding = '2px';
      element.style.display = 'none';
      element.style.position = 'fixed';
      element.style.left = '0px';
      element.style.top = '0px';
      element.style.fontSize = '20px';
      element.style.fontFamily = 'times';
      document.body.appendChild(element);
    }
    element.innerHTML = "Working... Please wait.";
    var TIMEOUT = 2000;
    timer = setTimeout(function(){
      element.style.display = 'block';
      timer = setTimeout(function(){
        element.innerHTML = 'Still working... Please wait.';
      }, TIMEOUT*10);
    }, TIMEOUT);
  }
  pliant_custom_wait_off = function(){
    clearTimeout(timer);
    element.style.display='none';
  }
})();

function resize_to_fill_document(el) {
    /*  http://www.quirksmode.org/viewport/compatibility.html */
    var test1 = document.body.scrollHeight;
    var test2 = document.body.offsetHeight
    if (test1 >= test2) { // all but Explorer Mac
      el.style.width = document.body.scrollWidth +"px";
      el.style.height = document.body.scrollHeight+"px";
    } else { // Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
      el.style.width = document.body.offsetWidth+"px";
      el.style.height = document.body.offsetHeight+"px";
    }
}

function custom_alert_disconnect(txt) {
    setTimeout("custom_alert_disconnect_callback(\""+txt+"\")", 6000);
}

function custom_alert_disconnect_callback(txt) {
    window.scrollTo(0,0);
    var message_box = document.getElementById("modal_alert_disconnect_message");
    message_box.innerHTML = txt;
    var el = document.getElementById("modal_alert_disconnect_greyout");
    if (window.attachEvent) // IE - hide selects
      for (var i = 0; i < document.forms.length; i++)
        for (var e = 0; e < document.forms[i].length; e++) 
          if (document.forms[i].elements[e].tagName == "SELECT") 
            document.forms[i].elements[e].style.visibility="hidden"; 
    resize_to_fill_document(el);
    el.style.display = "block";
    var el2 = document.getElementById("modal_alert_disconnect");
    resize_to_fill_document(el2);
    el2.style.display = "block";
}

function pliant_try_reconnect() {
    if (typeof session_id != 'undefined')
      window.location.search = "?sid="+escape(session_id);
    else
      window.location.reload();
}

//FIXME: section is no longer used
function pliant_find(section,id,type) {
  if (!type) type = "INPUT";
  var inputs = document.getElementsByTagName(type);
  for (var i=0; i<inputs.length; i++) {
    if (inputs[i].name == id)
      return inputs[i];
  }
}
function pliant_set(section,id, value) {
  var obj = pliant_find(section,id);
  obj.value = value;
  _pliant.change(obj);
}

