/***
* Remember to add this.template in the sub classes
* it is good.
***/
var superClassObj;

function superClass( containerObjectID ){

  this.htmlObj = html;
  this.htmlObj();

  this.HTML     = "";
  this.conObjID = containerObjectID;
  this.conObj   = {};
  this.obj      = {};
  this.sortType = "<";
  this.list     = [];
  this.OVERLIB  = "OVERLIB";
  // Call to server makes an IFRAME called RSIFrame with the same ID
  this.iframeId = "RSIFrame";
  this.tableID = this.conObjID + "_TABLE";
  this.verticalTitle = true;
  this.addResultRowFlag = false;
  this.printFriendlyFlag = false;
  this.bgc = -1;
  this.bgcArr = ['#CFE9FF','#B5CCDF','#EEEEEE'];
  this.csvLink = "";

  this.setConObjID = function(newConObjID){
    if( !empty( newConObjID ) ) this.conObjID = newConObjID;
  }

  this.addval = function( val ){
    return {value:val};
  }

  this.addrows = function( rows ){
    return {rows:rows};
  }

  this.populate = function( Obj ){
    this.Obj = Obj;
    this.list.push( Obj );
  }// end function populate

  this.populateHTML = function( HTML ){
    this.HTML += HTML;
  }

  // Parent -> Child call
  this.PCCall = function(theCall){
    return "window.frames[0]." + theCall;
  }

  this.cout = function(){

    var argv = this.cout.arguments;
    var argc = this.cout.arguments.length;

    if( argc > 0 ){
      eval("this.COUT_" + argv[0] + "();" );
      return;
    }

    $('#'+this.conObjID).html(this.HTML);
    $('#'+this.conObjID).show();
  }

  this.COUT_OVERLIB = function(){
    overlib(this.HTML,CAPTION,"asdasd",STICKY);
  }

  this.COUT_PARENT = function(){
    this.conObj = parent.document.getElementById( this.conObjID );
    this.conObj.innerHTML = this.HTML;
    this.conObj.style.display = "block";
  }

  this.sortResult = function( sortBy , noInit){

    setCookie( "sortBy", sortBy, 365, "/", SERVER_NAME, 0 );
    setCookie( "sortType", this.sortType, 365, "/", SERVER_NAME, 0 );

    if( isUndefined(this.obj[sortBy]) ) return;

    var type = this.obj[sortBy].type;

    function cmp(a,b){
      switch( type ){
        case('string'):{
          return a[sortBy].value.replace(/\*/g , '' ).replace(/^\s*|\s*$/g,'') > b[sortBy].value.replace(/\*/g ,'' ).replace(/^\s*|\s*$/g,'');
        }break;
        case('date'):{
          return a[sortBy].value.replace(/\./g , '' ) - b[sortBy].value.replace(/\./g , '' );
        }break;
        default:{
          return a[sortBy].value - b[sortBy].value;
        }
      }
    }

    if( this.sortType == "<" ){
      this.list.sort(cmp);
      this.sortType = ">";
    }else{
      this.list.sort(cmp);
      this.list.reverse();
      this.sortType = "<";
    }

    if( isUndefined(noInit) ) this.init();

  }

  this.resetObjValues = function(){
    for( var name in this.obj ) this.obj[name].value = 0;
  }

  this.constructHTML = function(){
    this.HTML = "<span style='padding-left:20px;color:#787BAF;font-weight:bold;font-size:15px;' id=modelTitle></span>";
    this.populateHTML( "<table id='" + this.tableID + "' cellpadding=3 cellspacing=3 border=0 class='superClass scrollTable'>" );
    this.titleMenu();
    this.resetObjValues();

    this.populateHTML( "<tbody class='scrollContent'>" );
    for( var key in this.list ){
      this.populateHTML( "<tr>" );
      var flag = 2;
      for( var name in this.obj ){
        var isNumberType = false;
        if( typeof( this.list[key][name].value ) == 'number' ){
          this.obj[name].value += this.list[key][name].value;
          isNumberType = true;
        }
        this.populateHTML( "<td style='" + ( flag % 2 == 0 ? "font-weight:bold;" : "" )+ "white-space:pre;" + ( isNumberType ? "text-align:right;" : "" ) + "'>" + this.valueException( this.list[key][name].value ) + "</td>" );
        flag++;
      }
      this.populateHTML( "</tr>" );
    }
    this.populateHTML( "</tbody>" );
    this.populateHTML( "<table>" );
  }

  this.valueException = function( value ){
    if( value == null || value == 'null' ) return "";
    //if( typeof value == 'number' ) return formatNumber(value);
    return value;
  }

  this.titleMenu = function(){
    this.populateHTML( "<thead class='fixedHeader'><tr>" );
    var c = 1;
    var style = "text-align:center;border:1px solid #ffffff;cursor:pointer;";
    for( var name in this.obj ){
      this.populateHTML( "<th style='font-size:10pt;text-align:center;vertical-align:bottom;'><div style='" + style + "' onClick='" + this.conObjID + "Obj.sortResult(\"" + name + "\");" + this.conObjID + "Obj.coloriseCell(" + c + ");'>" + this.title( name ) + "</div>" );
      if( !this.printFriendlyFlag ){
        this.populateHTML( "<div><span style='" + style + "' onClick='" + this.conObjID + "Obj.collapsCol(" + c + ")'>-</span>&nbsp;<span style='" + style + "' onClick='" + this.conObjID + "Obj.expandCol(" + c + ")'>+</span>&nbsp;" );
        this.populateHTML( "<span style='" + style + "' onclick='" + this.conObjID + "Obj.coloriseResTable(" + c++ + ");'>&#164;</span></div>" );
      }
      this.populateHTML( "</th>" );
    }
    this.populateHTML( "</tr></thead>" );
  }

  this.title = function( colname , vertical ){
    colname = colname.replace( /_/g , '&nbsp;' ).replace( /nummer/g , '#' ).replace( /number/g , '#' ).replace( /dato/g , '/' ).replace( /date/g , '/' ).replace( /aa/g , 'å' ).replace( /oe/g , 'ø' ).replace( /ae/g , 'æ' );
    if( this.verticalTitle ){
      colname = colname.replace( /&nbsp;/g , '<br>' );
    }
    return ucfirst( colname ).replace(/\//g,'<font color=red>&Delta;</font>').replace(/#/g,'<font color=red>#</font>');
  }

  this.addResultRowTop = function(){
    this.addResultRow(1);
  }

  this.addResultRowBottom = function(){
    var tableObj = document.getElementById( this.tableID );
    var rowsObj = tableObj.getElementsByTagName('tr');
    this.addResultRow( rowsObj.length );
  }

  this.addResultRow = function(index){
    this.addResultRowFlag = true;
    var tableObj = document.getElementById( this.tableID );
    var rowObj = tableObj.insertRow(index);
    var i = 0;
    for( var key in this.obj ){
      var cellObj = rowObj.insertCell(i++);
      cellObj.innerHTML = formatNumber( this.obj[key].value );
      cellObj.className = "resultRow";
    }
  }

  this.deleteColumn = function(){
    var allRows = document.getElementById( this.tableID ).rows;
    for (var i=0; i<allRows.length; i++) {
      if (allRows[i].cells.length > 1) {
        allRows[i].deleteCell(-1);
      }
    }
  }

  // this function will change the backgroundColor of the table rows depending on the fluxuation of the cell values in the same column.
  // same row value will result in the samme backgroundColor set
  this.coloriseResTable = function( colNum ){

    //var colorArr = [ "#FFDFFB","#FDFF0F","#7FFF90","#1FFFFB","#B0AFFF","#B5C1CF","#DFFDFF","#FFD3AF" ];
    var colorArr = [ "#FFDFFB","#B0AFFF"];
    var colorIndex = -1;
    var selectedColor;
    var colIndex = colNum - 1;
    var tableObj = document.getElementById(this.tableID);
    var rowsObjArr = tableObj.getElementsByTagName('tr');
    var cellValue = "";

    for( var key in rowsObjArr ){

      if( key == 'length' ) continue;
      if( key == 0 || ( this.addResultRowFlag && key == 1 ) || ( key == ( rowsObjArr.length - 1 ) && this.addResultRowFlag ) ) continue;

      if( $.browser.mozilla && !isFunction( rowsObjArr[key].getElementsByTagName ) ) continue;
      var cellsObjArr = rowsObjArr[key].getElementsByTagName('td');

      if( cellValue != trim( cellsObjArr[colIndex].innerHTML ) ){
        cellValue = trim( cellsObjArr[colIndex].innerHTML );
        if( colorIndex + 1 >= colorArr.length ) colorIndex = -1;
        colorIndex++;
      }
      changeBackgroundColor( rowsObjArr[key] , colorArr[colorIndex] );
    }

  }//end function colorResTable

  this.coloriseCell = function( colNum ){
    var tableObj = document.getElementById(this.tableID);
    var rowsObjArr = tableObj.getElementsByTagName('tr');
    var colorArr = [ "#FDFF00","#FDFF00"];
    var colIndex = colNum - 1;

    for( var key in rowsObjArr ){

      if( isUndefined( rowsObjArr[key] ) || !isNumber( key ) ) continue;

      if( key == 0 || ( this.addResultRowFlag && key == 1 ) || ( key == ( rowsObjArr.length - 1 ) && this.addResultRowFlag ) ) continue;

      if( $.browser.mozilla && !isFunction( rowsObjArr[key].getElementsByTagName ) ) continue;
      if( $.browser.msie && isUndefined( rowsObjArr[key].getElementsByTagName ) ) continue;
      var cellsObjArr = rowsObjArr[key].getElementsByTagName('td');
      cellsObjArr[colIndex].style.backgroundColor = "#FDFF00";
      //changeBackgroundColor( cellsObjArr[colIndex] , colorArr );
    }
  }

  this.linkResCol = function( URL , colNum ){
    var colIndex = colNum - 1;
    var tableObj = document.getElementById(this.tableID);
    var rowsObjArr = tableObj.getElementsByTagName('tr');

    for( var key in rowsObjArr ){
      if( key == 0 || ( this.addResultRowFlag && key == 1 ) || ( key == ( rowsObjArr.length - 1 ) && this.addResultRowFlag ) ) continue;

      if( $.browser.mozilla && !isFunction( rowsObjArr[key].getElementsByTagName ) ) continue;
      if( $.browser.msie && isUndefined( rowsObjArr[key].getElementsByTagName ) ) continue;

      //if( !isFunction( rowsObjArr[key].getElementsByTagName ) || isUndefined( rowsObjArr[key].getElementsByTagName ) ) continue;
      var cellsObjArr = rowsObjArr[key].getElementsByTagName('td');
      cellsObjArr[colIndex].innerHTML = '<a target=iframe href="' + URL.replace( '[REPLACE]' , urlencode( cellsObjArr[colIndex].innerHTML ) ) + '">' + cellsObjArr[colIndex].innerHTML + "</a>";
    }
  }//end function linkResCol

  this.functionalizeCol = function( eventText, useWhichCol, colNum ){
    $("#"+this.tableID).children('tbody').children('tr').each(
      function(index){
        var tmp = $(this).children('td')[colNum-1].innerHTML;
        $(this).children('td')[colNum-1].innerHTML = "<div style='cursor:pointer;text-decoration:underline;' "+ eventText.replace('[REPLACE]','"'+$(this).children('td')[useWhichCol-1].innerHTML+'"') +">"+ tmp + "</div>";
      }
    );

    /*
    for( var key in rowsObjArr ){
      if( key == 0 || ( this.addResultRowFlag && key == 1 ) || ( key == ( rowsObjArr.length - 1 ) && this.addResultRowFlag ) ) continue;
      alert(rowsObjArr[key]);
      //alert( $(rowsObjArr[key]).children('td').length )
      //var cellsObjArr = rowsObjArr[key].getElementsByTagName('td');
      //cellsObjArr[colIndex].innerHTML = '<a target=iframe href="' + URL.replace( '[REPLACE]' , urlencode( cellsObjArr[colIndex].innerHTML ) ) + '">' + cellsObjArr[colIndex].innerHTML + "</a>";
    }
    */
  }

  this.collapsCol = function(colNum){
    this.changeFontSize(colNum,"0px");
  }//end function collapsCol

  this.expandCol = function(colNum){
    this.changeFontSize(colNum,"8pt");
  }//end function collapsCol

  this.changeFontSize = function( colNum, fontSize ){
    var colIndex = colNum - 1;
    var tableObj = document.getElementById(this.tableID);
    var rowsObjArr = tableObj.getElementsByTagName('tr');
    for( var key in rowsObjArr ){
      if( key == 0 || ( this.addResultRowFlag && key == 1 ) || ( key == ( rowsObjArr.length - 1 ) && this.addResultRowFlag ) ) continue;

      if( $.browser.mozilla && !isFunction( rowsObjArr[key].getElementsByTagName ) ) continue;
      if( $.browser.msie && isUndefined( rowsObjArr[key].getElementsByTagName ) ) continue;

      //if( !isFunction( rowsObjArr[key].getElementsByTagName ) || isUndefined(rowsObjArr[key].getElementsByTagName) ) continue;
      var cellsObjArr = rowsObjArr[key].getElementsByTagName('td');
      cellsObjArr[colIndex].style.fontSize=fontSize;
    }
  }

  this.printFriendly = function(){
    this.winObj = winOpen( '/blank.ms' , 'printFriendly' );
    superClassObj = this;
    setTimeout( 'superClassObj.writeWindow()' , 1000 );
  }

  this.writeWindow = function(){
    this.printFriendlyFlag = true;
    this.constructHTML();
    this.printFriendlyFlag = false;
    this.winObj.document.body.innerHTML = this.HTML;
    this.verticalTitle = false;
    var d = new Date();
    this.winObj.document.getElementById('modelTitle').innerHTML = this.title( urldecode( getUrlParam( 'name' ) ) ) + ( getUrlParam( 'employeeName' ) ? "&nbsp;&nbsp;&nbsp;" + urldecode( getUrlParam( 'employeeName' ) ) : "" ) + "&nbsp;&nbsp;&nbsp;" + _date() + "&nbsp;" + _time();
    this.winObj.print();
    if( confirm( 'Skal vinduet lukkes efter print?' ) ){
      this.winObj.close();
    }
    superClassObj = false;
  }

  this.bgcShifter = function(){
    this.bgc++;
    if( this.bgc == this.bgcArr.length ) this.bgc = 0;
  }

  //Sorting as previously
  if( getCookie( "sortBy" ) ){
    this.sortType = getCookie("sortType" );
    this.sortResult(getCookie("sortBy"),true);
  }

}// end function superClass

function html(){

  this.inputText = function( name, value, optional ){
    return "<input type=text name='" + name + "' value='" + value + "' class=noBorder onFocus='setFocus(this);' onBlur='setBlur(this);' " + optional + ">";
  }

  this.inputCheckbox = function( name, value, optional ){
    return "<input type=checkbox name='" + name + "' value='" + value + "' class=noBorder onFocus='setFocus(this);' onBlur='setBlur(this);' " + optional + ">";
  }

  this.inputRadio = function( name, value, optional ){
    return "<input type=radio name='" + name + "' value='" + value + "' class=noBorder onFocus='setFocus(this);' onBlur='setBlur(this);' " + optional + ">";
  }

  this.textarea = function( name, value, optional ){
    return "<textarea name='" + name + "' class=noBorder " + optional + " onFocus='setFocus(this);' onBlur='setBlur(this);'>" + value + "</textarea>";
  }

  this.inputSubmit = function( value, optional ){
    return "<input type=submit name=submit class=sub value='" + value + "' " + optional + ">";
  }

  this.inputButton = function( value, optional ){
    return "<input type=button name='button' class=sub value='" + value + "' " + optional + ">";
  }

  this.inputHidden = function( name, value ){
    return "<input type=hidden name='" + name + "' value='" + value + "'>";
  }

  this.fckeditor = function( name , value , optional ){
    var obj = document.getElementById(name);
    var fck = new FCKeditor(name);
    fck.BasePath = "/js/fckeditor/";
    obj.innerHTML = fck.CreateHtml();
  }

  this.cursor_wait = function(){
    document.body.style.cursor = 'wait';
  }

  this.cursor_clear = function(){
    document.body.style.cursor = 'default';
  }

  this.printFriendlyLink = function(){
    return "<span style='cursor:pointer;font-size:8pt;color:red;text-decoration:underline;' onClick='" + this.conObjID + "Obj.printFriendly();'>Printvenlig udgave</span>";
  }

  this.getCsvLink = function(){
    this.csvLink = document.location.href + "&getCSV=true";
    return "<span style='cursor:pointer;font-size:8pt;color:red;text-decoration:underline;' onClick='window.open( " + this.conObjID + "Obj.csvLink )'>Excel-CSV udgave</span>";
  }

}// End function html
