// SETTING VARS
var pics = new Array();
var undefined;
var headcells=-1
var table_first=0
var vars='';
var fetch='';
var fetch_page='';
var show_type='';
var page_numbers='';
var page_loc = 1;
var create_table = 1;
var account_type=3;
var pages_max=10;

// AJAX OBJECT
function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&amp;timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}


// DYNAMIC TABLE CREATE
function GenTable(responseText, responseStatus) {
if (responseStatus==200) {
var data = responseText
data=data.replace(/&amp;nbsp;/g, " ");
data=data.replace(/&aelig;/g, "æ");
data=data.replace(/&oslash;/g, "ø");
data=data.replace(/&aring;/g, "å");
data=data.replace(/&AElig;/g, "Æ");
data=data.replace(/&Oslash;/g, "Ø");
data=data.replace(/&Aring;/g, "Å");
data=data.replace(/&lt;/g, "<");
data=data.replace(/&gt;/g, ">");

if (create_table == 1) {
RemoveTable('DynTable')
CreateTable('DynTable', data)
} else {

var replace = document.getElementById('DynTable');
replace.innerHTML = data;
}
} 

if (page_numbers != '') {
PageNumbers(page_numbers, page_loc)
}

}

function CreateTable(el, data) {

if (data=='') {

var tblBody = document.getElementById(el);
var row = document.createElement("tr");
var cell = document.createElement("td");
var cellText = document.createTextNode('Ingen data');
cell.appendChild(cellText);

cell.setAttribute("colSpan", 3+tendens*2);
set_class = 'StatsTableLeft';
cell.setAttribute("className", set_class);
cell.setAttribute("class", set_class);
row.appendChild(cell);
tblBody.appendChild(row);

} else {

var data_array=data.split("++")
var data_cols_array = ''
var send_data = ''
var data_cols = 0
var data_rows = data_array.length
var set_class = '';

        // get the reference for the div
        var tblBody = document.getElementById(el);

        // creating all cells
        for (var j = 0; j < data_rows; j++) {

data_cols_array=data_array[j].split("|")
if (data_cols == 0) {data_cols = data_cols_array.length}

            // creates a table row
// i = vertical
//j = horizontal
            var row = document.createElement("tr");

            for (var i = 0; i < data_cols; i++) {
                if (i == 0 && showdata=='StatsMisc') {
                var hash_array=data_cols_array[i].split("----")
                data_cols_array[i] = hash_array[1];
                var cell = document.createElement("td");
                cell.innerHTML = "<a href='./index.php?type=Details&main="+show_type+"&hash="+hash_array[0]+"'><img src='./pics/site/plus.gif' border='0' alt='Vis detaljer'></a>";
                row.appendChild(cell);
                var colspan = 2;
                } else {
                var colspan = 3;                
                }

                var cell = document.createElement("td");
                               
                if (i == tendens || i == 0 || (tendens != null && j == headcells)) {
                cell.innerHTML = data_cols_array[i];
                } else {
                var cellText = document.createTextNode(data_cols_array[i]);
                cell.appendChild(cellText);
                }
                
                if (i == tendens && real_account_type != 3) {
                cell.innerHTML = '<a href="./index.php?type=UpgradeAccount"><font color=silver>Kræver elite</font></a>';
                }
                
                if (i == 0) {
                cell.setAttribute("colSpan", colspan);
                set_class = 'StatsTableLeft';
                } else {
                cell.setAttribute("colSpan", "2");
                }
                if (set_class != '') {
                cell.setAttribute("className", set_class);
                cell.setAttribute("class", set_class);
                set_class = '';
                }
                row.appendChild(cell);
               
            }

            // add the row to the end of the table body
            tblBody.appendChild(row);
        }
}

}


function PageNumbers(num, loc) {

var html = '';
if (loc > 10) {
var page=loc-10;
} else {
var page=0;
}

var bold_begin= '';
var bold_end = '';
if (account_type == 3 || (page_numbers < (add_num*pages_max))) {
var pages_total = Math.ceil(num/add_num);
pages_max = page+10;
var page_num_html = '<b>'+page_numbers+'</b> resultater fundet.<br>';
} else {
var pages_total = pages_max;
var page_num_html = '<b>'+page_numbers+'</b> resultater fundet. Du har adgang til at se <b>'+(add_num*pages_max)+'</b>. Se alle med <a href="./index.php?type=UpgradeAccount">elite konto</a>.<br>';
}

while (num > 0 && page<pages_max) {
page++;
if (page == loc) {
bold_begin = '<b>';
bold_end = '</b>';
} else {
bold_begin= '';
bold_end = '';
}

html = html+'<a href="#" onClick="FetchPage(\''+((page-1)*add_num)+'\'); return false;">'+bold_begin+page+bold_end+'</a> ';
num = num-add_num;
}

if (page > 1) {

if (loc < pages_total) {
html = html+'<a href="#" onClick="FetchPage(\''+((loc)*add_num)+'\'); return false;">næste >></a>';
}


if (loc > 1) {
html = '<a href="#" onClick="FetchPage(\''+((loc-2)*add_num)+'\'); return false;"><< forrige</a> '+html;
}

var send_html='';
els=['PageNumbers','PageNumbersSec']
for (var i_tem = 0; i_tem < els.length; i_tem++) {
if (i_tem == 0) {send_html = page_num_html+html} else {send_html = html}
document.getElementById(els[i_tem]).innerHTML=send_html;
document.getElementById(els[i_tem]).setAttribute("className", "PageNumbers");
document.getElementById(els[i_tem]).setAttribute("className", "PageNumbers");
}


//el.innerHTML = page_num_html+html;

//el.setAttribute("className", "PageNumbers");
//el.setAttribute("class", "PageNumbers");
}


}

function RemoveTable(el) {
var node = document.getElementById(el)
//alert(headcells+1);
while (node.childNodes.length > (headcells+1)) {
node.removeChild(node.lastChild);
}
}

// SORT TABLE
function SortTable(sort_var, sort_order) {
vars = '&sort_var='+sort_var+'&sort_order='+sort_order
FetchTable();
}

function FetchDate(get) {
fetch = '&fetch_date='+get
FetchTable();
}

function FetchPage(get) {
fetch_page = '&fetch_page='+get
page_loc = (get/add_num)+1;
FetchTable();
}


function FetchTable() {
var TableRequest = new ajaxObject('./dyn_data/fetch_data.php', GenTable);
if (show_type != '') {var show_type_fetch = '&show_type='+show_type} else {var show_type_fetch = ''}
TableRequest.update('fetch_data='+showdata+show_type_fetch+vars+fetch+fetch_page);
}

// COMMON FUNCTIONS
function RemoveNodes(el) {
var node = document.getElementById(el)
while (node.childNodes.length > 0) {
node.removeChild(node.firstChild);
}
}

function resetIndex(el, index_num)
{
  var x=document.getElementById(el)
  x.selectedIndex=index_num;
}

// DROPDOWN MENU
function SwichDropdown(n) 
{ 
var myindex=n.selectedIndex
if (n.options[myindex].value != "0") 
{
window.open(n.options[myindex].value, target="_top");
}
}

// ROLL FUNCTIONS
function roll_on(n, c)
{
pic = n.src;

if (pics[pic] == undefined)
{
pics[pic] = new Image();
pics[pic].src = n.src.replace(".gif", "_hover.gif");
}

n.src = pics[pic].src;
}

function roll_off(n, c)
{
pic = n.src;

if (pics[pic] == undefined)
{
pics[pic] = new Image();
pics[pic].src = n.src.replace("_hover.gif", ".gif");
}

n.src = pics[pic].src;
}
   
/////// MENU

var disappeardelay=250  //menu disappear speed onMouseout (in miliseconds)
var enableanchorlink=1 //Enable or disable the anchor link when clicked on? (1=e, 0=d)
var hidemenu_onclick=1 //hide menu when user clicks within menu? (1=yes, 0=no)

/////No further editting needed

var ie5=document.all
var ns6=document.getElementById&&!document.all

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function showhide(obj, e, visible, hidden){
if (ie5||ns6)
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
obj.visibility=visible
else if (e.type=="click")
obj.visibility=hidden
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=0
if (whichedge=="rightedge"){
var windowedge=ie5 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
}
else{
var windowedge=ie5 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
}
return edgeoffset
}

function DropDownMenu(obj, e, dropmenuID){
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
if (typeof dropmenuobj!="undefined") //hide previous menu
dropmenuobj.style.visibility="hidden"
clearhidemenu()
if (ie5||ns6){
obj.onmouseout=delayhidemenu
dropmenuobj=document.getElementById(dropmenuID)
if (hidemenu_onclick) dropmenuobj.onclick=function(){dropmenuobj.style.visibility='hidden'}
dropmenuobj.onmouseover=clearhidemenu
dropmenuobj.onmouseout=ie5? function(){ dynamichide(event)} : function(event){ dynamichide(event)}
showhide(dropmenuobj.style, e, "visible", "hidden")
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
}
return ReturnUrl()
}

function ReturnUrl(){
if ((ie5||ns6) && !enableanchorlink) return false
else return true
}

function contains_ns6(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function dynamichide(e){
if (ie5&&!dropmenuobj.contains(e.toElement))
delayhidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
delayhidemenu()
}

function delayhidemenu(){
delayhide=setTimeout("dropmenuobj.style.visibility='hidden'",disappeardelay)
}

function clearhidemenu(){
if (typeof delayhide!="undefined")
clearTimeout(delayhide)
}


////// MENU END
