/* This notice must be untouched at all times.
demos.js	v. 1.1

Copyright (c) 2007-2007 Tunsoft/Geir Tunlid. All rights reserved.
Date         Ver.   By:           What:
01/04/2007   1.00    Geir Tunlid   Created 
01/05/2007   1.01    Geir Tunlid   Changed it to started date/time ----> ended date/time
05/08/2007   1.1     Geir Tunlid   Added text replacer function in textarea

LICENSE: LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For more details on the GNU Lesser General Public License,
see http://www.gnu.org/copyleft/lesser.html
*/

// GLOBAL VARIABLE
var WebAjaxReqXML = null;
 
///////////////////////////////////////////////////
function WebAjaxReqXMLResults()
 {
 if(WebAjaxReqXML.readyState == 4)
   {
   document.getElementById('report_demo').options.length = 0;
   if(WebAjaxReqXML.status != 200)
     {
     document.getElementById('report_demo').options[document.getElementById('report_demo').options.length] = new Option("Error reading from server", "false");
     alert('Unable to read data from server. Status '+WebAjaxReqXML.status);
     return;
     }
   //
   numItems = document.getElementById('report_demo').options.length;
   document.getElementById('report_demo').options[numItems] = new Option("Started date - Ended date","false");
   //
   var txt = WebAjaxReqXML.responseText.toLowerCase();
   var pos=0;
   var i=0;
   for(i=0; i<999; i++)
     {
     var startDate = null; 
     var endedDate = null; 
     var x1 = txt.indexOf('<a href="',pos);
     if(x1<0) { break; }
     var x2 = txt.indexOf('">',x1);
     var s1 = txt.substring(x1+9,x2);
     if(s1.indexOf('auto_')==0)
        {
        var sp = s1.split("_");
        try { startDate = sp[1] + '/' + sp[2] + '/' + sp[3] + ' - ' + sp[4] + ':' + sp[5]; } catch(e) { }
        var fromDate = new Date(sp[1], sp[2], sp[3], sp[4], sp[5]);
        ///
        /// 18-Dec-2006 20:13
        /// 789 123456789 123
        var x3 = txt.indexOf('right',x2)+0;
        endedDate = txt.substring(x3+15,x3+17);
        var d2 = txt.substring(x3+7,x3+9);
        var m2 = txt.substring(x3+10,x3+13);
        var y2 = txt.substring(x3+14,x3+18);
        var h2 = txt.substring(x3+19,x3+21);
        var mi2 = txt.substring(x3+22,x3+24);
        if(m2=='jan') { m2='01'; }
        if(m2=='feb') { m2='02'; }
        if(m2=='mar') { m2='03'; }
        if(m2=='apr') { m2='04'; }
        if(m2=='may') { m2='05'; }
        if(m2=='jun') { m2='06'; }
        if(m2=='jul') { m2='07'; }
        if(m2=='aug') { m2='08'; }
        if(m2=='sep') { m2='09'; }
        if(m2=='oct') { m2='10'; }
        if(m2=='nov') { m2='11'; }
        if(m2=='dec') { m2='12'; }
        endedDate = y2 + '/' + m2 + '/' + d2 + ' - ' + h2 + ':' + mi2;
        var toDate = new Date(y2, m2, d2, h2, mi2);
        ///
        // var desc = fromDate.toLocaleString() + ' ----> ' + toDate.toLocaleString();
        var desc = startDate + ' ----> ' + endedDate;
        ///
        numItems = document.getElementById('report_demo').options.length; 
        document.getElementById('report_demo').options[numItems] = new Option(desc,s1); 
        }
     pos=x2;
     }
   }
 }
 
//////////////////////////////////////////////////////////////////
function WebAjax(AjaxURL)
 {
 WebAjaxReqXML = null;
 try { document.domain = "battle.no"; } catch(e) { }
 if(WebAjaxReqXML == null) try { WebAjaxReqXML = new XMLHttpRequest(); } catch(e) { }
 if(WebAjaxReqXML == null) try { WebAjaxReqXML = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { }
 if(WebAjaxReqXML == null) try { WebAjaxReqXML = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { }
 if(WebAjaxReqXML)
   {
   var x = new Date().getTime(); // PREVENT IE CACHING
   WebAjaxReqXML.open('GET', AjaxURL + '?sn='+x, true);
   WebAjaxReqXML.setRequestHeader("Connection", "close");
   WebAjaxReqXML.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
   WebAjaxReqXML.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
   WebAjaxReqXML.setRequestHeader("Pragma", "no-cache");
   WebAjaxReqXML.onreadystatechange = WebAjaxReqXMLResults;
   WebAjaxReqXML.send(null);
   }
 else
   {
   alert("Your browser does not support Ajax");
   }
 }
 
 
//////////////////////////////////////////////////////////////////
function GetDemoFiles()
  {
  var selSrv = document.getElementById('report_srv').options[document.getElementById('report_srv').selectedIndex].value;
  document.getElementById('report_demo').options.length = 0;
  document.getElementById('report_demo').options[document.getElementById('report_demo').options.length] = new Option("Please wait...","false");
  document.getElementById('report_demo').options[0].selected = true;
  //WebAjax('demos.htm?demo='+selSrv); // DEBUGGING
  WebAjax('/?demo='+selSrv); // LIVE
  }


//////////////////////////////////////////////////////////////////
function ubbc(tArea, open, end) {
  if(open == "link") { 
    var txt = prompt("Enter URL for the link.","http://");
    if (txt) { open = '[a href='+txt+']'; }
    else { return false; }
  }
  if(open == "img") 
    { 
    var txt = prompt("Enter URL for the IMG.","http://"); 
    if (txt) { open = '[img]'+txt+''; }
    else { return false; }
    }
  if(open == "srv_rules") { 
    var txt = prompt("Link text",""); 
    if (txt) { open = '[a href=http://www.battle.no/?section=servers&page=rules]'+txt+''; }
    else { return false; }
  }
  if(open == "report") { 
    var txt = prompt("Link text",""); 
    if (txt) { open = '[a href=http://www.battle.no/?section=klagemuren&page=report]'+txt+''; }
    else { return false; }
  }
  if(open == "demo_howto") { 
    var txt = prompt("Link text",""); 
    if (txt) { open = '[a href=http://forum.battle.no/index.php?showtopic=5992]'+txt+''; }
    else { return false; }
  }
  if(open == "klagemur_test") { 
    var txt = prompt("Link text",""); 
    if (txt) { open = '[a href=http://www.battle.no/klagemur-test.php]'+txt+''; }
    else { return false; }
  }
  
  if(open == "[ul]")
    {
    var list_item = prompt("Enter a list item. Press \"Cancel\" when you are done.","");
    if (list_item)
      {
      while (list_item)
        {
        open = open+"\n[li]"+list_item+"[/li]";
        list_item = prompt("Enter a list item. Press \"Cancel\" when you are done.","");
        }
      open = open+"\n";
      }
    else { return false; }
    }

  var isIE = (document.all)? true : false;
  var open = (open)? open : "";
  var end = (end)? end : "";
  if (isIE)
    {
    tArea.focus();
    var curSelect = document.selection.createRange();
    if (arguments[3])
      { 
      curSelect.text = open + arguments[3] + "]" + curSelect.text + end;
      }
    else
      {
      curSelect.text = open + curSelect.text + end;
      }
    }
  else if (!isIE && typeof tArea.selectionStart != "undefined")
    {
    var selStart = tArea.value.substr(0, tArea.selectionStart);
    var selEnd = tArea.value.substr(tArea.selectionEnd, tArea.value.length);
    var curSelection = tArea.value.replace(selStart, '').replace(selEnd, '');
    if (arguments[3])
      {
      tArea.value = selStart + open + arguments[3] + "]" + curSelection + end + selEnd;
      }
    else
      {
      tArea.value = selStart + open + curSelection + end + selEnd;
      }
    }
  else
    { 
    tArea.value += (arguments[3])? open + arguments[3] + "]" + end : open + end;
    }
  try { tArea.focus(); } catch(e) {}
  }



function openWindow(theURL,winName,features) {
    var w = window.open(theURL,winName,features);
}

function hideLoadingPage() {
  document.getElementById ? document.getElementById('loading').style.display = 'none' // DOM
  : document.all ? document.all.loading.style.display = 'none'  // IE 4
  : null; // unsupported
}

// Multiple file selector by Stickman -- http://www.the-stickman.com 
// with thanks to: [for Safari fixes] Luis Torrefranca -- http://www.law.pitt.edu and Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com [for duplicate name bug] 'neal'
function MultiSelector( list_target, max ) {
  this.list_target = list_target;
  this.count = 0;
  this.id = 0;
  if( max ) { this.max = max; }
  else { this.max = -1; };
  this.addElement = function( element ) {
    if( element.tagName == 'INPUT' && element.type == 'file' ) {
      element.name = 'file_' + this.id++;
      element.multi_selector = this;
      element.onchange = function() {
        var new_element = document.createElement( 'input' );
        new_element.type = 'file';
        this.parentNode.insertBefore( new_element, this );
        this.multi_selector.addElement( new_element );
        this.multi_selector.addListRow( this );
        this.style.position = 'absolute';this.style.left = '-1000px';};
        if( this.max != -1 && this.count >= this.max ) {
          element.disabled = true;
        };
        this.count++;this.current_element = element;
      }
      else {alert( 'Error: not a file input element' );
    };
  };
  this.addListRow = function( element ) {
    var new_row = document.createElement( 'div' );
    var new_row_button = document.createElement( 'input' );
    new_row_button.type = 'button';
    new_row_button.value = 'Delete';
    new_row.element = element;
    new_row_button.onclick= function() {
      this.parentNode.element.parentNode.removeChild( this.parentNode.element );
      this.parentNode.parentNode.removeChild( this.parentNode );
      this.parentNode.element.multi_selector.count--;
      this.parentNode.element.multi_selector.current_element.disabled = false;
      return false;
    };
    new_row.innerHTML = element.value;new_row.appendChild( new_row_button );
    this.list_target.appendChild( new_row );
  };
};