﻿//////////////////////////////////////////////////////////////////////
///  Declares
//////////////////////////////////////////////////////////////////////

// Column Types
var CT_NAME = 1;
var CT_DESC = 2;
var CT_PROFILE1 = 3;
var CT_PROFILE2 = 4;
var CT_PROFILE3 = 5;
var CT_PROFILE4 = 6;
var CT_PROFILE5 = 7;
var CT_PROFILE6 = 8;
var CT_PROFILE7 = 9;
var CT_PROFILE8 = 10;
var CT_PROFILE9 = 11;
var CT_PROFILE10 = 12;
var CT_PROFILE11 = 13;
var CT_PROFILE12 = 14;
var CT_PROFILE13 = 15;
var CT_PROFILE14 = 16;
var CT_PROFILE15 = 17;
var CT_PROFILE16 = 18;
var CT_CREATED_BY = 19;
var CT_CREATED_DATE = 20;
var CT_MODIFIED_BY = 21;
var CT_MODIFIED_DATE = 22;

// Sort Directions
var SD_ASCENDING = 1;
var SD_DESCENDING = 2;


//////////////////////////////////////////////////////////////////////
///  ColumnList Class
//////////////////////////////////////////////////////////////////////
function ColumnList() { 
    var _cols = [];
       
    // constructor
    var data = getSetting("ColumnInfo");
    
    if(data) 
        loadColumnInfo(data);
    else   
        loadDefaultData();
    
    // methods
    this.ReadColumnInfo = function(tbl) {
        for(var i = 1; i < tbl.rows.length; i++) 
            _cols[i - 1] = new ColumnInfo(tbl.rows[i]);            
    }
    
    this.SetNewColWidth = function(colType, newWidth) {
        // Fix New Width
        var sWidth = newWidth.toString();
        sWidth = sWidth.replace("px","");
        
        // Cast as integer 
        newWidth = parseInt(sWidth, 0);
        
        for(var i = 0; i < _cols.length; i++) {
            if(_cols[i].ColumnID == colType) {                                
                _cols[i].Width = newWidth;
                return;
            }
        }
    }
        
    this.Save = function() {
        var sCookie = this.Serialize();        
        setCookie("ColumnInfo", sCookie);
    }
    
    this.Items = function(index) {
        return _cols[index];
    }
    
    this.Length = function() {
        return _cols.length;
    }
    
    this.Serialize = function() {
        var sRet = "";
    
        for(var i = 0; i < _cols.length; i++) {
            if(sRet.length > 0) sRet += "|";            
            sRet += _cols[i].toString();        
        }
        
        return sRet;
    }
       
    this.TotalWidth = function() {
        var nWidth = 0;
        
        for(var i = 0; i < _cols.length; i++) {
            if(_cols[i].Visible == "1")
                nWidth += parseInt(_cols[i].Width, 0);
        }
        
        return nWidth;
    }
        
    // private 
    function loadColumnInfo(value) {
        var sCols = value.split("|");
        for(var i = 0; i < sCols.length; i++) 
            _cols[i] = new ColumnInfo(sCols[i]);        
    }
        
    function loadDefaultData() {                
        _cols[0] = new ColumnInfo("1~250~1");
        _cols[1] = new ColumnInfo("2~200~1");
        
        for(var i = 1; i <= 16; i++) {             
            _cols[i+1] = new ColumnInfo((i+2).toString() + "~100~1"); 
        }
                
        _cols[18] = new ColumnInfo("19~100~1");
        _cols[19] = new ColumnInfo("20~100~1");
        _cols[20] = new ColumnInfo("21~100~1");
        _cols[21] = new ColumnInfo("22~100~1");
    }
}

//////////////////////////////////////////////////////////////////////
///  ColumnInfo Class
//////////////////////////////////////////////////////////////////////
function ColumnInfo(data) {
    if(data.id) {
        // Parse Table Row
        if(!data) return;
        
        // Get Base Name 
        this.BaseName = data.id.replace("trCol","");
        this.ColumnID = getColumnID(this.BaseName);
        
        // Column Name
       this.DisplayName = data.childNodes[0].innerText;
        
        // Get Width
        var txt = document.getElementById("txt" + this.BaseName + "Width");
        if(txt) {
            var n = parseInt(txt.value, 0);
            if(!isNaN(n))
                this.Width = n;
            else   
                this.Width = 100;
        }
            
        // Get Visible 
        var chk = document.getElementById("chk" + this.BaseName + "Visible");
        if(chk && chk.checked == true)
            this.Visible = 1;
        else
            this.Visible = 0;
    }    
    else {
        // Parse String
        if(!data) return;
        
        var sArgs = data.split("~");
        
        this.ColumnID = parseInt(sArgs[0], 0);
        
        var names = getNames(this.ColumnID);
        
        this.BaseName = names[0];
        this.DisplayName = names[1];
        this.Width = parseInt(sArgs[1], 0);
        this.Visible = sArgs[2];    
    }

    function getNames(columnID) {
        var names = [];
        
        switch(columnID) { 
            case CT_NAME:
                names[0] = "Name";
                names[1] = "Name";                
                break;
                
            case CT_DESC:
                names[0] = "Desc";
                names[1] = "Description";
                break;
                
            case CT_MODIFIED_BY:
                names[0] = "ModifiedBy";
                names[1] = "Modified By";                
                break;
                
            case CT_MODIFIED_DATE:
                names[0] = "ModifiedDate";
                names[1] = "Modified Date";                
                break;
                
            case CT_CREATED_BY:
                names[0] = "CreatedBy";
                names[1] = "Created By";                                
                break;
                
            case CT_CREATED_DATE:
                names[0] = "CreatedDate";
                names[1] = "Created Date";                                
                break;
                
            default:
                // Profile Field
                var index = (columnID - 2);
                
                names[0] = "Profile" + index.toString();
                
                if(index <= 12)                                    
                    names[1] = "Text Field " + index.toString();
                    
                else if(index <= 14)
                    names[1] = "Number Field " + (index - 12).toString();
                    
                else   
                    names[1] = "Date Field " + (index -14).toString();
                    
                break;
        }
        
        return names;
    }
    
    function getColumnID(baseName) {
        switch(baseName.toLowerCase()) {  
            case "name":
                return CT_NAME;                
                break;
                
            case "desc":       
                return CT_DESC;         
                break;
                
            case "modifiedby": 
                return CT_MODIFIED_BY;               
                break;
                
            case "modifieddate":      
                return CT_MODIFIED_DATE;          
                break;
                
            case "createdby":           
                return CT_CREATED_BY;     
                break;
                
            case "createddate":      
                return CT_CREATED_DATE;          
                break;
                
            default:
                // Profile Field
                var index = parseInt(baseName.replace("Profile", ""), 0);
                return (index + 2);                
        }
    }
}

ColumnInfo.prototype.toString = function() {
    return this.ColumnID.toString() + "~" + this.Width.toString() + "~" + this.Visible.toString();        
}


//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
///
///         Cookie Code
///
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
    // set a default expiration date to 1 year hence 
    var d = new Date();    
    d.setFullYear(d.getFullYear() + 1, d.getMonth(), d.getDay());
    
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "; expires=" + d.toGMTString()) +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
      
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
	
function getSetting(settingName) {
    var s = getCookie(settingName);
    
    if(s != null && s.length > 0) 
        return s;
        
    return getDefaultSetting(settingName);    
}

function getDefaultSetting(inputName) {
    var itm = document.getElementById(inputName);
    if(!itm) return "";
    
    return itm.value;    
}
