﻿/* This function serves as JavaScript Class for Javascript Trace messages. */
function jsTraceEvent(msg, source) 
{
    this.message = msg;
    this.time = new Date();
    this.fromFirst = 0;
    this.fromLast = 0;
    this.source = source?source:"unknown"; 
    this.toString = function()
    {
        return this.message + " - " + this.time.getHours() + ":" + this.time.getMinutes() + ":" 
        + this.time.getSeconds() + "." + this.time.getMilliseconds() + ", " + this.fromLast + " secs";
    }
}

/*  Creates an json object named debugWindow which can be used to create Javascript Trace information */
var debugWindow = 
{
    /*  Startup function for debugWindow to be called on window.onload. This function tries to create
        a new section within the asp trace information, and adds any trace messages that have already 
        been logged.
     */
    "init": function()
    {
        if ($get("__asptrace"))
        {
            var traceArea = $get("__asptrace").getElementsByTagName("span")[0];
            
            var table = document.createElement("table");
            table.cellSpacing = "0px";
            table.cellPadding = "0px";
            table.border = "0px";
            table.style.width = "100%";
            table.style.borderCollapse = "collapse";
            traceArea.insertBefore(table, traceArea.firstChild)
            
            this.tbody = document.createElement("tbody");
            table.appendChild(this.tbody);
            
            var tr = document.createElement("tr");
            this.tbody.appendChild(tr); 
            
            var th = document.createElement("th");
            th.align = "left";
            th.colSpan = "10";
            th.className = "alt";
            tr.appendChild(th);
            
            var h3 = document.createElement("h3");
            th.appendChild(h3);
            
            var b = document.createElement("b");
            h3.appendChild(b);
            
            b.appendChild(document.createTextNode("Javascript Trace Information"));
            
            tr = document.createElement("tr");
            tr.align = "left";
            tr.className = "subhead";
            this.tbody.appendChild(tr);
            
            th = document.createElement("th");
            th.appendChild(document.createTextNode("Category"));
            tr.appendChild(th);
            
            th = document.createElement("th");
            th.appendChild(document.createTextNode("Message"));
            tr.appendChild(th);
            
            th = document.createElement("th");
            th.appendChild(document.createTextNode("From First(s)"));
            tr.appendChild(th);
            
            th = document.createElement("th");
            th.appendChild(document.createTextNode("From Last(s)"));
            tr.appendChild(th);
            
            tr = document.createElement("tr");
            this.tbody.appendChild(tr);
            
            for (index in this.events) { this.display(this.events[index], (index % 2 == 1)); }
        }
    },
    "window" : null,
    "tbody" : null,
    // This array stores all logged Javascript trace messages
    "events" : new Array(),
    /* This function expects a json object of type jsTraceEvent. It does time calculation,
       adds the object to the event collection, and calls the display method.
     */
    "add" : function(jsEvent)
    {
        if (this.events.length > 0) {
            jsEvent.fromFirst = (jsEvent.time.getTime() - this.events[0].time.getTime())/1000;
            jsEvent.fromLast = (jsEvent.time.getTime() - this.events[this.events.length-1].time.getTime())/1000;
        }
        this.events[this.events.length] = jsEvent;         
        this.display(jsEvent, ((this.events.length-1) % 2 == 1));
    },
    /*  This function adds a row to the JavaScript Trace table containing information on a javascript trace event.
        The jsEvent parameter should be of type jsTraceEvent, and alt should be a 0 (false) or 1(true) 
        which will use the alternate row styles.
     */    
    "display" : function(jsEvent, alt)
    {
        if (this.tbody)
        {
            var tr = document.createElement("tr");
            if (alt) {tr.className = "alt";}
            
            var td = document.createElement("td");
            td.appendChild(document.createTextNode(jsEvent.source));
            tr.appendChild(td);
            
            td = document.createElement("td");
            td.appendChild(document.createTextNode(jsEvent.message));
            tr.appendChild(td);
            
            td = document.createElement("td");
            td.appendChild(document.createTextNode(jsEvent.fromFirst));
            tr.appendChild(td);
            
            td = document.createElement("td");
            td.appendChild(document.createTextNode(jsEvent.fromLast));
            tr.appendChild(td);
            
            this.tbody.appendChild(tr);
        }
    }
}

/*  This code create a window onload manager. The purpose of this manager
    is to allow for multiple functions to be run at window.onload.
 */
if (!window.onloadManager)
{
    window.onloadManager = new Array();
    if (window.onload) {window.onloadManager[0] = window.onload;}
    window.onload = function() { 
    for (index in window.onloadManager) { window.onloadManager[index](); }
    };
}
window.onloadManager.unshift(function() { debugWindow.init(); });

/*  The following function are used as part of listview sorting */
var sortCategory;
var sortAttribute;

function sortDS(dsURI, Category, Attribute, link)
{
    link.style.cursor = "wait";
    if (DataSourceRegistry[dsURI].data)
    {
        if (Category == sortCategory && Attribute == sortAttribute) 
        {
            DataSourceRegistry[dsURI].data.reverse();
        }
        else
        {
            sortCategory = Category;
            sortAttribute = Attribute;
            DataSourceRegistry[dsURI].data.sort(compare);
        }
        DataSourceRegistry[dsURI].render();
    }
    var clearCursor = function() {link.style.cursor = "pointer";};
    setTimeout(clearCursor, 100);    
    return false;
}

function compare(a,b) 
{
    return getValue(a) - getValue(b);
}

function getValue(x) 
{
    if (sortCategory == "pricing") 
    {
        if (x["pricing"]) 
        {
            return x.pricing[sortAttribute];
        }
        else
        {
            return 0;
        }
    }
    else if (!x.attributes[sortCategory]) 
    {
        return 0;
    } 
    else if (!x.attributes[sortCategory][sortAttribute])
    {
        return 0;
    }
    else if (isNumeric(x.attributes[sortCategory][sortAttribute]))
    {
        return x.attributes[sortCategory][sortAttribute];
    }
    else if (x.attributes[sortCategory][sortAttribute].toLowerCase() == "yes")
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

function isNumeric(sText)
{
   var validChars = "0123456789.";
   var isNumber=true;
   var char;
 
   for (i = 0; i < sText.length && isNumber == true; i++) 
   { 
      char = sText.charAt(i); 
      if (validChars.indexOf(char) == -1) 
      {
         isNumber = false;
      }
   }
   return isNumber;
}
/* The following functions are for the View Switch */
function toggleView () {
	if ($get("ViewAsList").checked == true)
	{
		$get("ctlPhoneList").style.display = "block";
		$get("ctlPhoneThumbs").style.display = "none";
	}
	else
	{
		$get("ctlPhoneList").style.display = "none";
		$get("ctlPhoneThumbs").style.display = "block";
	}
}

if (!window.onloadManager)
{
	window.onloadManager = new Array();
	if (window.onload) {window.onloadManager[0] = window.onload;}
	window.onload = function() {
		for (index in window.onloadManager) { window.onloadManager[index](); }
	};
}
window.onloadManager[window.onloadManager.length] = function()
{
	toggleView();
};