﻿function createSilverlightControl(version, SilverlightDiv, XAML_Source, createSilverlightDelegate, noSilverlightHTML, SilverlightControlID)
{
    // verify a parent element was specified for the Silverlight Control
    if ( !SilverlightDiv )
    { alert("ERROR: SilverlightDiv was not specified in call to createSilverlightControl"); return; }

    // verify a XAML Source file was specified for the Silverlight Control
    if ( !XAML_Source )
    { alert("ERROR: XAML_Source was not specified in call to createSilverlightControl"); return; }

    // verify that alternate content was specified for the Silverlight Control if Silverlight isn't installed
    if ( !noSilverlightHTML )
    { alert("ERROR: noSilverlightHTML was not specified in call to createSilverlightControl"); return; }

    // create a unique ID for the Silverlight control if one wasn't specified
    if ( !SilverlightControlID )
    { SilverlightControlID = SilverlightDiv.id + "_SilverlightControl"; }
    
    if ( Silverlight.isInstalled(version) )
    {
        recordDebugMessage("common.js:createSilverlightControl - Silverlight installed", 9);

	    Silverlight.createObjectEx({
		    source: XAML_Source,
		    parentElement: SilverlightDiv,
		    id: SilverlightControlID,
		    properties: {
			    width: "100%",
			    height: "100%",
			    version: "1.0",
			    isWindowless: 'true',
			    background:"transparent"
            },
		    events: {
			    onLoad: createSilverlightDelegate,
			    onError: function(sender, args) {
				    var errorDiv = document.getElementById("errorLocation");
				    if (errorDiv != null) {
					    var errorText = args.errorType + "- " + args.errorMessage;
    							
					    if (args.ErrorType == "ParserError") {
						    errorText += "<br>File: " + args.xamlFile;
						    errorText += ", line " + args.lineNumber;
						    errorText += " character " + args.charPosition;
					    }
					    else if (args.ErrorType == "RuntimeError") {
						    errorText += "<br>line " + args.lineNumber;
						    errorText += " character " +  args.charPosition;
					    }
					    errorDiv.innerHTML = errorText;
				    }	
			    }
		    }
	    });
    }
    else
    {
        recordDebugMessage("common.js:createSilverlightControl - Silverlight NOT installed", 9);
        
        SilverlightDiv.innerHTML=noSilverlightHTML;
        document.body.innerHTML;
    }
}

function recordDebugMessage(message, eventLevel)
{
    var debuggingEnabled = false;
    var eventThreshold = 9;
    
    var debugDiv = document.getElementById("debugLocation");
    if (debugDiv != null) {
        if (debuggingEnabled) {
            if (eventThreshold >= eventLevel) {
                var debugText = "Debug Message(" + getCurrentTime() + ") : " + message + "<br />";

                var currentDebugText = debugDiv.innerHTML;
                debugDiv.innerHTML = debugText + currentDebugText;
            }
        }
    }	
}

function getCurrentTime() {
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds()
    var milliseconds = now.getMilliseconds();
    var timeValue = "" + hours
    timeValue += ((minutes < 10) ? ":0" : ":") + minutes
    timeValue += ((seconds < 10) ? ":0" : ":") + seconds
    timeValue += "." + milliseconds
    return timeValue;
}

function delegate(target, callback) {
  return function() { callback.apply(target, arguments); };
}

// javascript query string parsing utils
// pass location.search to the constructor: var page = new PageQuery(location.search)
// get values like: var myValue = page.getValue("param1") etc.

function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0].toLowerCase() == s.toLowerCase())
				return this.keyValuePairs[j].split("=")[1];
		}
		return -1;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }	
}

var NWObjectReference;
function openNextWebWindow(url)
{
      if(NWObjectReference == null || NWObjectReference.closed)
      {
            var w = 1020;
            var h = 667;
            wleft = (screen.width - w) / 2;
            wtop = (screen.height - h - 95) / 2;
            if (wleft < 0) wleft = 0;
            if (wtop < 0) wtop = 0;
            NWObjectReference = window.open(url, "nextwebwindow", "height="+h+",width="+w+",left="+wleft+",top="+wtop+",scrollbars=yes,status=1,resizable=1",true);

            if (NWObjectReference==null || typeof(NWObjectReference)=="undefined") {
                  alert("You have a pop-up blocker that blocked the console window from being opened.\rPlease configure your pop-up blocker to allow pop-ups from this site.");
            }
      }
      else
      {
            if (NWObjectReference.location = url)
            {
                NWObjectReference.focus();
            }
            else
            {
                NWObjectReference.location = url;
            }
      }
}




function PNGswap(myID)
{
   var strOver  = "_on"
   var strOff = "_off"
   var oSpan = document.getElementById(myID)
   var currentAlphaImg = oSpan.filters(0).src
   if (currentAlphaImg.indexOf(strOver) != -1){
      oSpan.filters(0).src = currentAlphaImg.replace(strOver,strOff)
      }
   else{
      oSpan.filters(0).src = currentAlphaImg.replace(strOff,strOver)
      }
}



function imgSwap(oImg)
{
   var strOver  = "_on"    // image to be used with mouse over
   var strOff = "_off"     // normal image
   var strImg = oImg.src
   if (strImg.indexOf(strOver) != -1) {
      oImg.src = strImg.replace(strOver,strOff)
      }
   else {
      oImg.src = strImg.replace(strOff,strOver)
      }
}
