﻿// capture the parameters passed to this script
var _scripts = document.getElementsByTagName('script');
var _script = _scripts[_scripts.length - 1];
var _queryString = _script.src/*.replace(/^[^\?]+\??/,'')*/;
var _parameters = _parseQuery(_queryString);

function _parseQuery(query)
{
	var Params = new Object();
			
	if (!query) return Params; // return empty object
 
	var Pairs = query.replace(/^[^\?]+\??/,"").split(/[;&]/);
   
	for (var i = 0; i < Pairs.length; i++)
	{
		var KeyVal = Pairs[i].split('=');
		
		if (!KeyVal || KeyVal.length != 2) continue;

		var key = unescape(KeyVal[0]);
		var val = unescape(KeyVal[1]);
		val = val.replace(/\+/g, ' ');
		Params[key] = val;
	}

	return Params;
}

/********************************************************************************
 Cookie helper class
********************************************************************************/
var Cookies =
{
	_defaultPath: "/",
	
	enabled: function()
	{
		var cookiesEnabled = (navigator.cookieEnabled) ? true : false;
		//if not IE4+ nor NS6+
		if (typeof navigator.cookieEnabled == "undefined" && !cookiesEnabled)
		{ 
			this.setValue("cookietest", "working", 1);
			cookiesEnabled = (this.getValue("cookietest") == "working");
			this.deleteValue("cookietest");
		}
		
		return cookiesEnabled;
	},
			
	remove: function (name, path, domain)
	{
		if (this.getValue(name) != null)
		{
			if (!path) path = this._defaultPath;
	
			document.cookie = name + "=" + 
				((path) ? "; path=" + path : "") + 
				((domain) ? "; domain=" + domain : "") + 
				"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	},

	getValue: function(name)
	{
		name += "=";
		var ca = document.cookie.split(';');
		for(var i=0; i < ca.length; i++)
		{
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1, c.length);
			if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
		}
		return null;
	},
		
	setValue: function(name, value, expires, path, domain, secure)
	{
		if (expires == null)
		{
			expires = new Date();
			expires.setTime(expires.getTime() + (365 * 24 * 60 * 60 * 1000)); // a year
		}
	
		if (!path) path = this._defaultPath;

		document.cookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	}
};
//******************************************************************************
// extend Protoype Form object for form select elements
//******************************************************************************
Form.Select =
{
	addOption: function(element, value, text)
	{
		var newOption = document.createElement('option');
		newOption.text = text;
		newOption.value = value;

		try
		{
			element.add(newOption , null); // standards compliant; doesn't work in IE
		}
		catch(ex)
		{
			element.add(newOption ); // IE only
		}
		
		return newOption ;
	},
	
	clear: function(element)
	{
		while (element.options.length > 0) element.remove(0);
		element.options.length = 0;	
		element.options[0] = null;
	},
	
	getSelectedText: function(element)
	{
		return element.options[element.selectedIndex].text;
	},
	
	getSelectedValue: function(element)
	{
		return element.options[element.selectedIndex].value;
	},
	
	indexOfText: function(element, text)
	{
		var index = -1;
		text = text.toUpperCase();
	
		for (var optionCount = 0; optionCount < element.options.length; optionCount++)
		{
			if (element.options[optionCount].text.toUpperCase() == text)
			{
				index = optionCount;
				break;
			}
		}

		return index;
	},
	
	indexOfValue: function(element, value)
	{
		var index = -1;
		value = value.toUpperCase();

		for (var optionCount = 0; optionCount < element.options.length; optionCount++)
		{
			if (element.options[optionCount].value.toUpperCase() == value)
			{
				index = optionCount;
				break;
			}
		}
	
		return index;
	},
	
	selectedValue: function(element, newValue, defaultValue)
	{
		if (newValue != null) return this.setSelectedValue(element, newValue, defaultValue);
		else return this.getSelectedValue(element);
	},	
	
	setSelectedValue: function(element, value, defaultValue)
	{
		var defaultIndex = -1;
		value = value.toUpperCase();
		element.selectedIndex = -1;

		if (!defaultValue) defaultValue = "/*-NOMATCH";
		else defaultValue = defaultValue.toUpperCase();
	
		for (var optionCount = 0; optionCount < element.options.length; optionCount++)
		{
			if (element.options[optionCount].value.toUpperCase() == defaultValue) defaultIndex = optionCount;
		
			if (element.options[optionCount].value.toUpperCase() == value)
			{
				element.selectedIndex = optionCount;
				break;
			}
		}

		if (element.selectedIndex == -1 && defaultValue != "/*-NOMATCH")
		{
			element.selectedIndex = defaultIndex;
			return defaultValue;
		}
		else return value;
	},
	
	selectedText: function(element, newText, defaultValue)
	{
		if (newText != null) return this.setSelectedText(element, newValue, defaultValue);
		else return this.getSelectedText(element);
	},
	
	setSelectedText: function(element, text, defaultValue)
	{
		var defaultIndex;
		text = text.toUpperCase();
		element.selectedIndex = -1;

		if (!defaultValue) defaultValue = "/*-NOMATCH";
		else defaultValue = defaultValue.toUpperCase();
	
		for (var optionCount = 0; optionCount < this.options.length; optionCount++)
		{
			if (element.options[optionCount].text.toUpperCase() == defaultValue) defaultIndex = optionCount;
		
			if (element.options[optionCount].text.toUpperCase() == text)
			{
				element.selectedIndex = optionCount;
				break;
			}
		}
	
		if (element.selectedIndex == -1 && defaultValue!= "/*-NOMATCH")
		{
			element.selectedIndex = defaultIndex;
			return defaultValue;
		}
		else return text;
	}
};
//******************************************************************************
// extend Protoype Form object for form radio buttons
//******************************************************************************
Form.Radio =
{
	clear: function(nodeList)
	{
		if (nodeList.length) for (var nodeCount = 0; nodeCount < this.length; nodeCount++) nodeList[nodeCount].checked = false;
		else nodeList.checked = false;
	},
	
	getSelectedIndex: function(nodeList)
	{
		if (nodeList.length)
		{
			for (var nodeCount = 0; nodeCount < nodeList.length; nodeCount++)
			{
				if (nodeList[nodeCount].checked) return nodeCount;
			}
		}
		else if (nodeList.checked) return 0; // one radio button in list ... if its selected then the index is 0
		
		return -1;
	},
	
	getSelectedValue: function(nodeList)
	{
		if (nodeList.length)
		{
			for (var nodeCount = 0; nodeCount < nodeList.length; nodeCount++)
			{
				if (nodeList[nodeCount].checked) return nodeList[nodeCount].value;
			}
		}
		else if (nodeList.checked) return nodeList.value; // one radio button in list ... if its selected then the index is 0
		
		return "";
	},
	
	indexOfValue: function (nodeList, value)
	{
		var nodeIndex = -1;
		value = value.toUpperCase();
	
		if (nodeList.length)
		{
			for (var nodeCount = 0; nodeCount < nodeList.length; nodeCount++)
			{
				if (nodeList[nodeCount].value.toUpperCase() == value) return nodeCount;
			}
		}
		else
		{
			if (nodeList.value.toUpperCase() == value) nodeIndex = 0;
		}
		
		return nodeIndex;
	},
	
	setSelectedIndex: function(nodeList, index)
	{
		if (nodeList.length)
		{
			this.clear(nodeList);			
			if (index > -1) nodeList[index].checked = true; // there more than one radio button in the list
		}
		else
		{
			if (index == 0) nodeList.checked = true; // one radio button in list ... select that if the index is 0
			else index = -1;
		}
		// return the new index
		return index;
	},
	
	setSelectedValue: function(nodeList, value)
	{
		if (nodeList.length)
		{
			var nodeIndex = this.indexOfValue(nodeList, value);
			if (nodeIndex > -1) nodeList[nodeIndex].checked = true; // there more than one radio button in the list
		}
		else
		{
			if (nodeList.checked) nodeList.value = value; // one radio button in list ... select that if the index is 0
		}
		// return the new value
		return value;
	},
	
	selectedIndex: function(nodeList, newIndex)
	{
		var nodeIndex = -1;
		// is there a new index value
		if (newIndex != null) nodeIndex = this.setSelectedIndex(nodeList, newIndex);
		else nodeIndex = this.getSelectedIndex(nodeList);
		// return index
		return nodeIndex;
	},
	
	selectedValue: function(nodeList, newValue)
	{
		if (newValue != null) return this.setSelectedValue(nodeList, newValue);
		else return this.getSelectedValue(nodeList);
	},
	
	selectedValueIndex: function(nodeList, newValue, defaultValue)
	{
		var nodeIndex = -1;
	
		if (nodeList.length)
		{
			if (defaultValue == null) defaultValue = "";
			
			nodeIndex = this.indexOfValue(nodeList, newValue);			
							
			if (nodeIndex == -1) nodeIndex = this.indexOfValue(nodeList, defaultValue);				
			
			this.selectedIndex(nodeList, nodeIndex);
		}
		else
		{
			if (nodeList.value == newValue || nodeList.value == defaultValue)
			{
				nodeIndex = 0;
				nodeList.checked = true;
			}
		}

		return nodeIndex;
	}
};
//******************************************************************************
// extend Protoype Array object to check if a value exists in an Array
//******************************************************************************
Object.extend(Array.prototype,
{
  inArray: function(value)
  {
        for (var i = 0, item; item = this[i]; i++) if (item === value) return true;
        return false;
  }
});
//******************************************************************************
// extend Protoype Event object to check for keypresses
//******************************************************************************
Object.extend(Event,
{
	_specialKeys:
	{
		'backspace':    8,
		'tab':          9,
		'return':       13, 'enter':      13,
		'shift':        16,
		'ctrl':         17, 'control':    17, 'cmd': 17, // 'cmd' is detected by opera on macs instead of ctrl
		'alt':          18, 'alterative': 18,
		'pause':        19, 'break':      19,
		'capsLock':     20, 'caps':       20,
		'esc':          27, 'escape':     27,
		'pageUp':       33,
		'pageDown':     34,
		'end':          35,
		'home':         36,
		'left':         37, 'leftArrow':  37,
		'up':           38, 'upArrow':    38,
		'right':        39, 'rightArrow': 39,
		'down':         40, 'downArrow':  40,
		'insert':       45,
		'delete':       46, 'del':        46,
		'windowsLeft':  91, 'Windows':    91,
		'windowsRight': 92,
		'select':       93,
		'f1':           112,
		'f2':           113,
		'f3':           114,
		'f4':           115,
		'f5':           116,
		'f6':           117,
		'f7':           118,
		'f8':           119,
		'f9':           120,
		'f10':          121,
		'f11':          122,
		'f12':          123,
		'numLock':      144, 'num':       144,
		'scrollLock':   145, 'scroll':    145
	  },
  
	_code: function(event)
	{
		var keyResult;

		if (Object.isNumber(event.keyCode)) keyResult = event.keyCode;   //DOM
		else if (Object.isNumber(event.which)) keyResult = event.which;     //NS 4 compatible
		else if (Object.isNumber(event.charCode)) keyResult = event.charCode;  //also NS 6+, Mozilla 0.9+
		else keyResult = null; //total failure
	
	    return keyResult;
	},
  
	_withModifiers: function(event, keys)
	{
		if (!keys) return false;
		
		var check = [], modifiers = ['shift', 'ctrl', 'alt'];
		
		if (Object.isString(keys)) keys = $w(keys);
		
		keys.each(function(key, index)
		{
			if (modifiers.inArray(key)) check[check.length] = event[key + 'Key'];
	    });
	    
		return check.all();
	},
	
	isKey: function(event, key, modifiers)
	{
		var KeyCode = this._code(event)
		
		if ( ( (KeyCode == (/\D/.match(key) ? this._specialKeys[key] : key)) || 
		     (/\d/.match(key) && String.fromCharCode(KeyCode).toLowerCase() == key) ) && 
		     (Object.isUndefined(modifiers) ? true : this._withModifiers(event, modifiers)) ) return true;

		return false;
	}
});


var dasystems =
{
	dllURL: "",
	themeURL: "",
	dllAction: "",

	initialize: function()
	{
		this.dllURL = _parameters["dllurl"];
		this.themeURL = _parameters["themeurl"];	
		this.dllAction = _parameters["dllaction"];
		this.loadScripts();
	},

	loadScripts: function()
	{
		this.require(this.themeURL + "/scripts/dasystems.common.js");
		this.require(this.themeURL + "/scripts/dasystems.controls.js");
		
		if (this.dllAction != null) if (this.dllAction.length > 0) this.require(this.themeURL + "/scripts/" + this.dllAction + ".js");
	},
	
	require: function(scriptSource)
	{
		document.write('<script type="text/javascript" src="' + scriptSource + '"><\/script>');
	}
};

dasystems.initialize();