﻿/*///////////////////////////////////////////////////////////
XmlNodeTypes Enumeration
///////////////////////////////////////////////////////////*/
XmlNodeTypes = function()
{
}

var $XmlNodeTypes = XmlNodeTypes.prototype = 
{
	ELEMENT_NODE: 1,
	ATTRIBUTE_NODE: 2,
	TEXT_NODE: 3,
	CDATA_SECTION_NODE: 4,
	ENTITY_REFERENCE_NODE: 5,
	ENTITY_NODE: 6,
	PROCESSING_INSTRUCTION_NODE: 7,
	COMMENT_NODE: 8,
	DOCUMENT_NODE: 9,
	DOCUMENT_TYPE_NODE: 10,
	DOCUMENT_FRAGMENT_NODE: 11,
	NOTATION_NODE: 12
}

/*///////////////////////////////////////////////////////////
XmlWriter Class
///////////////////////////////////////////////////////////*/
var $XmlWriter = XmlWriter = function()
{
}

function XmlWriter$writeBeginElement(xmlNode)
{
	var elName = xmlNode.nodeName;
	var output = '';
	
	if(xmlNode.nodeType == $XmlNodeTypes.ELEMENT_NODE)
	{
		output += '<' + elName + ' ';
		if (xmlNode.hasAttributes())
		{
			for (var idx = 0; idx < xmlNode.attributes.length; idx++)
			{
				var attrib = xmlNode.attributes[idx];
				output += this.writeAttribute(attrib);
			}
		}
		output += '>';
	}
	
	for (var idx = 0; idx < xmlNode.childNodes.length; idx++)
	{
		var child = xmlNode.childNodes[idx];
		if ((child.nodeType == $XmlNodeTypes.ELEMENT_NODE) || (child.childNodes.length > 0))
		{
			output += this.writeBeginElement(child);
		}
		else if(child.nodeType == $XmlNodeTypes.TEXT_NODE)
		{
			output += this.writeElementValue(child);
		}
	}
	
	if ((xmlNode.nodeValue != null) && (xmlNode.nodeValue != 'undefined') && (xmlNode.nodeType == $XmlNodeTypes.TEXT_NODE))
	{
		output += this.writeElementValue(xmlNode);
	}
	
	if(xmlNode.nodeType == $XmlNodeTypes.ELEMENT_NODE)
	{
		output += this.writeEndElement(xmlNode);
	}
	
	return output;
}

function XmlWriter$writeEndElement(xmlNode)
{
	return '</' + xmlNode.nodeName + '>';
}

function XmlWriter$writeAttribute(xmlAttribute)
{
	return xmlAttribute.nodeName + '=\'' + xmlAttribute.nodeValue + '\' ';
}

function XmlWriter$writeElementValue(xmlNode)
{
	return xmlNode.nodeValue;
}

function XmlWriter$toXmlString(xmlDoc)
{
	var docRoot = xmlDoc.documentElement;
	var output = '';
	output += this.writeBeginElement(docRoot);
	return output;
}

XmlWriter.prototype = 
{
	writeBeginElement: XmlWriter$writeBeginElement,
	writeEndElement: XmlWriter$writeEndElement,
	writeAttribute: XmlWriter$writeAttribute,
	writeElementValue: XmlWriter$writeElementValue,
	toXmlString: XmlWriter$toXmlString
}

var $xmlWriter = xmlWriter = new $XmlWriter();

/*///////////////////////////////////////////////////////////
VStringBuilder Class
///////////////////////////////////////////////////////////*/
VStringBuilder = function()
{
	this.aStr = new Array();
	this.append = function(str) { this.aStr[this.aStr.length] = str; }
	this.toString = function() { return this.aStr.join(''); }
	this.init = function() { this.aStr = null; this.aStr = new Array(); }
}

/*///////////////////////////////////////////////////////////
VArrayList Class
///////////////////////////////////////////////////////////*/
VArrayList = function()
{
	this.array = new Array();
	this.add = function(obj) { this.array[this.array.length] = obj; }
	this.removeAt = function(index) { this.array.splice(index, 1); }
	this.count = function() { return this.array.length; }
	this.clear() = function() { this.array.splice(0, this.array.length); }
	this.contains(obj) = function() { return this.indexOf(obj) != -1; }
	this.indexOf(obj) = function() { for (var i = 0; i < this.array.length; i++) { if (this.array[i] == obj) return i; } return -1; }
	this.remove(obj) = function() { var i = this.indexOf(obj); if (i != -1) this.removeAt(i); }
}

/*///////////////////////////////////////////////////////////
VHashTable Class
///////////////////////////////////////////////////////////*/
var $HashTable = VHashTable = function Common$HashTable()
{
	this.hashtable = new Array();
	this.isHashTable = true;
}

function Common$HashTable$get_hashtable()
{
	if((this.hashtable == null) || (this.hashtable == 'undefined'))
	{
		this.hashtable = new Array();
	}
	return this.hashtable;
}

function Common$HashTable$set_hashtable(e)
{
	this.hashtable = e;
}

function Common$HashTable$get_isHashTable()
{
	return true;
}
		
function Common$HashTable$containsKey(key)
{
	var hashTable = this.get_hashtable();
	for (var i in hashTable)
	{
		if ((i == key) && (hashTable != null))
		{
			return true;
		}
	}
	return false;
}
	
function Common$HashTable$removeItem(key)
{
	var hashTable = this.get_hashtable();
	var value = hashTable[key];
	hashTable[key] = null;
	this.set_hashtable(hashTable);
	return value;
}
	
function Common$HashTable$addItem(key, value)
{
	var hashTable = this.get_hashtable();
	hashTable[key] = value;
	this.set_hashtable(hashTable);
}
	
function Common$HashTable$count()
{
	var size = 0;
	var hashTable = this.get_hashtable();
	for (var i in hashTable)
	{
		if (hashTable[i] != null)
		{
			size++;
		}
	}
	return size;
}

function Common$HashTable$keys()
{
	var keys = new Array();
	var hashTable = this.get_hashtable();
	for (var i in hashTable)
	{
		if (hashTable[i] != null)
		{
			keys.push(i);
		}
	}
	return keys;
}

function Common$HashTable$item(key)
{
	var hashTable = this.get_hashtable();
	return hashTable[key];
}

function Common$HashTable$setItem(key, value)
{
	var hashTable = this.get_hashtable();
	hashTable[key] = value;
	this.set_hashtable(hashTable);
}

VHashTable.prototype = 
{
	get_hashtable: Common$HashTable$get_hashtable,
	set_hashtable: Common$HashTable$set_hashtable,
	isHashTable: Common$HashTable$get_isHashTable,
	item: Common$HashTable$item,
	containsKey: Common$HashTable$containsKey,
	removeItem: Common$HashTable$removeItem,
	setItem: Common$HashTable$setItem,
	addItem: Common$HashTable$addItem,
	count: Common$HashTable$count,
	keys: Common$HashTable$keys
}

/*///////////////////////////////////////////////////////////
ListSerializer Class
///////////////////////////////////////////////////////////*/
ListSerializer = function()
{
	this.serialize = ListSerializer_Serialize;
	this.deserialize = ListSerializer_Deserialize;
}

function ListSerializer_Deserialize(xml)
{
	if ((xml == null) || (xml == '') || (xml == 'undefined'))// || (xml.innerText == 'undefined')
	{
		return new $HashTable();
	}
		
	var doc;
	if (window.attachEvent) // Internet Explorer
	{
		doc = new ActiveXObject("Microsoft.XMLDOM");
		doc.async = 'false';
		doc.loadXML(xml);
	}
	else if (window.addEventListener) // W3C DOM Compliant browsers, i.e. FireFox
	{
		var parser = new DOMParser();
		doc = parser.parseFromString(xml,'text/xml');
	}
	return ListSerializer_DeserializeList(doc.documentElement);
}

function ListSerializer_DeserializeList(element)
{
	var list = new $HashTable();
	if (element != null)
	{
		var items = element.getElementsByTagName("item");
		for (var i = 0; i < items.length; i++)
		{
			islist = (items[i].getAttribute("list") == "1");
			if (!islist)
			{
				var txt = '';
				if(items[i].hasChildNodes())
				{
					if(items[i].childNodes[0].nodeType == $XmlNodeTypes.TEXT_NODE)
					{
						txt = items[i].childNodes[0].nodeValue;
					}
				}
				list.addItem(items[i].getAttribute("key"), txt);
			}
			else
			{
				list.addItem(items[i].getAttribute("key"), ListSerializer_DeserializeList(items[i]));
			}
		}
	}
	return list;
}

function ListSerializer_Serialize(list)
{
	var doc;
	if (window.attachEvent) // Internet Explorer
	{
		doc = new ActiveXObject("Microsoft.XMLDOM");
	}
	else if (window.addEventListener) // W3C DOM Compliant browsers, i.e. FireFox
	{
		doc = document.implementation.createDocument('','',null);
	}
	
	var root = doc.createElement("item");
	root.setAttribute("list", "1");
	doc.appendChild(root);
	
	ListSerializer_SerializeList(doc, root, list);
	
	var xml = '';
	if(window.attachEvent)
	{
		xml = root.xml;
	}
	else if(window.addEventListener)
	{
		xml = $xmlWriter.toXmlString(doc);
	}
	return xml;
}

function ListSerializer_SerializeList(doc, parentElement, list)
{
	var keys = list.keys();
	for (var i = 0; i < keys.length; i++)
	{
		var itemElem = doc.createElement("item");
		
		var item = list.item(keys[i]);
		if (item.isHashTable)
		{
			itemElem.setAttribute("list", "1");
			ListSerializer_SerializeList(doc, itemElem, item);
		}
		else
		{
			itemElem.setAttribute("key", keys[i]);
			var itemText = doc.createTextNode(item.toString());
			itemElem.appendChild(itemText);
		}
		parentElement.appendChild(itemElem);
	}
}

/*///////////////////////////////////////////////////////////
Base64 Encoding
///////////////////////////////////////////////////////////*/
var VBase64Key = "ABCDEFGHIJKLMNOP" +
				"QRSTUVWXYZabcdef" +
				"ghijklmnopqrstuv" +
				"wxyz0123456789+/" +
				"=";

// Base64 encode an input string.
function VBase64Encode(input) 
{
	var output = "";
	var chr1, chr2, chr3 = "";
	var enc1, enc2, enc3, enc4 = "";
	var i = 0;

	do
	{
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2))
		{
			enc3 = enc4 = 64;
		}
		else if (isNaN(chr3))
		{
			enc4 = 64;
		}

		output = output + 
			VBase64Key.charAt(enc1) + 
			VBase64Key.charAt(enc2) + 
			VBase64Key.charAt(enc3) + 
			VBase64Key.charAt(enc4);
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";
	} while (i < input.length);

	return output;
}

// Decode the base64 encoded input string.
function VBase64Decode(input) 
{
	var output = "";
	var chr1, chr2, chr3 = "";
	var enc1, enc2, enc3, enc4 = "";
	var i = 0;

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	var base64test = /[^A-Za-z0-9\+\/\=]/g;
	if (base64test.exec(input))
	{
		alert(
			"There were invalid base64 characters in the input text.\n" +
			"Valid base64 characters are A-Z, a-z, 0-9, '+', '/', and '='\n" +
			"Expect errors in decoding.");
	}
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	do
	{
		enc1 = VBase64Key.indexOf(input.charAt(i++));
		enc2 = VBase64Key.indexOf(input.charAt(i++));
		enc3 = VBase64Key.indexOf(input.charAt(i++));
		enc4 = VBase64Key.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		output = output + String.fromCharCode(chr1);

		if (enc3 != 64)
		{
			output = output + String.fromCharCode(chr2);
		}
		if (enc4 != 64)
		{
			output = output + String.fromCharCode(chr3);
		}

		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";
	} while (i < input.length);

	return output;
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();