﻿//------------------------------------------------------
// ajax class
//------------------------------------------------------

function clsAjax(objPage, blnAsync, funResponse) {
	var self = this;
	this.ajax = null;
	this.page = objPage;
	this.async = blnAsync;
	this.onready = null;
	this.onerror = null;
	this.response = "";
	this.error = "";
	this.waitresponse = false;
	this.onresponse = (typeof(funResponse) == "function") ? funResponse : null;

	//constructor
	this.constructor = function() {
		if (navigator.userAgent.indexOf("Opera") != -1)
			;
		else if (navigator.userAgent.indexOf("MSIE") != -1) {
			var strName = "Msxml2.XMLHTTP";
			if (navigator.appVersion.indexOf("MSIE 5.5") >= 0) {
				strName = "Microsoft.XMLHTTP";
			}
			try {
				this.ajax = new ActiveXObject(strName);
				if (this.async) {
					if (this.onresponse == null) {
						this.onresponse = function() {
							self.ready();
						};
					}
					this.ajax.onreadystatechange = this.onresponse;
				}
			}
			catch (e) {
				this.ajax = null;
			}
		}
		else if (navigator.userAgent.indexOf("Mozilla") != -1) {
			this.ajax = new XMLHttpRequest();
			if (this.async) {
				if (this.onresponse == null) {
					this.onresponse = function() {
						self.ready();
					};
				}
				this.ajax.onload = this.onresponse;
				this.ajax.onerror = this.onresponse;
			}
		}
	};   //constructor

	this.destructor = function() {
		if (this.ajax != null) {
			try { this.ajax.onload = null; } catch (e) { }
			try { this.ajax.onerror = null; } catch (e) { }
			try { this.ajax.onreadystatechange = null; } catch (e) { }
		}
		this.ajax = null;
	};  //destructor

	this.ready = function() {
		if (this.ajax.readyState == 4) {
			this.waitresponse = false;
			if (this.ajax.status == 200) {
				this.response = this.ajax.responseText;
				if (typeof (this.onready) == "string")
					eval(this.onready + "(this);");
				else if (typeof (this.onready) == "function")
					this.onready(self);
			}
			else {
				this.error = this.ajax.statusText;
				if (typeof (this.onerror) == "string")
					eval(this.onerror + "(this);");
				else if (typeof (this.onerror) == "function")
					this.onerror(self);
			}
		}
	};     //ready

	this.override = function(strMime) {
		if (this.ajax.overrideMimeType)
			this.ajax.overrideMimeType(strMime)
	};  //override
	
	//get
	this.get = function(strUrl) {
		this.response = "";
		this.error = "";
		if (this.ajax != null) {
			this.waitresponse = this.async;
			try {
				this.ajax.open("GET", strUrl, this.async);
				this.ajax.send("");
				if (!this.async)
					this.response = this.ajax.responseText;
			}
			catch (e) {
				this.error = (e.description) ? e.description : e.message;
				if (this.onerror != null)
					eval(this.onerror + "(this);");
			}
		}
		return this.response;
	};   //get
	
	//post
	this.post = function(strUrl, strData) {
		this.response = "";
		this.error = "";
		if (this.ajax != null) {
			this.waitresponse = this.async;
			this.ajax.open("POST", strUrl, false);
			this.ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			this.ajax.send(strData);
			if (!this.async)
				this.response = this.ajax.responseText;
		}
		return this.response;
	};  //post

	//initialise
	this.constructor();
	
} //clsAJax

//------------------------------------------------------
// clsXmlDoc class
//------------------------------------------------------

function clsXmlDoc(strXml) {
	this.xml = null;
	this.loaded = false;
	this.parser = null;
	this.doc = null;

	this.constructor = function(strXml) {
		if (window.ActiveXObject) {
			this.doc = new ActiveXObject("Microsoft.XMLDOM");
			if (this.doc != null) {
				this.doc.async = "false";
				if (typeof (strXml) != "undefined")
					this.doc.loadXML(strXml);
				this.loaded = true;
			}
		}
		else {
			this.xml = document.implementation.createDocument("", "", null);
			if (this.xml != null) {
				this.xml.async = "false";
				this.parser = new DOMParser();
				if (this.parser != null) {
					if (typeof (strXml) != "undefined")
						this.doc = this.parser.parseFromString(strXml, "text/xml");
					this.loaded = true;
				}
			}
		}
	};  //constructor

	this.destructor = function() {
		this.doc = null;
		this.xml = null;
		this.parser = null;
	};  //destructor


	this.getElementsByTagName = function(strTag) {
		return (this.doc != null) ? this.doc.getElementsByTagName(strTag) : null;
	};  //getElementsByTagName

	this.getAttributeCount = function(objNode) {
		return parseInt(objNode.attributes.length);
	};  //getAttribute

	this.getAttribute = function(objNode, intIndex) {
		return objNode.attributes[intIndex];
	};  //getAttribute

	this.getAttributeValue = function(objAttribute) {
		return objAttribute.value;
	};  //getAttributeValue

	this.getAttributeName = function(objAttribute) {
		return (this.parser == null) ? objAttribute.baseName : objAttribute.name;
	};  //getAttributeName

	this.getInnerText = function(objNode) {
		var strText = "";
		if (objNode == null)
			strText = "";
		else if (this.parser == null)
			strText = objNode.text;
		else if (objNode.childNodes.length == 0)
			strText = "";
		else {
			strText = objNode.childNodes[0].nodeValue;
			for (var intIndex = 0; intIndex < objNode.childNodes.length; intIndex++) {
				if (objNode.childNodes[intIndex].nodeType == 4) {
					strText = objNode.childNodes[intIndex].nodeValue;
					break;
				}
			} //for
		}
		return strText;
	}; //getInnerText

	this.constructor(strXml);
} //clsXmlDoc

//------------------------------------------------------
// roman class
//------------------------------------------------------

function clsRoman() {
	this.digits = new Array(9);
	this.offset = 0;
	this.pages = 0;
	this.mappings = null;

	//class constructor
	this.constructor = function() {
		this.digits[0] = ["M", "C", "X", "I"];
		this.digits[1] = ["MM", "CC", "XX", "II"];
		this.digits[2] = ["MMM", "CCC", "XXX", "III"];
		this.digits[3] = [null, "CD", "XL", "IV"];
		this.digits[4] = [null, "D", "L", "V"];
		this.digits[5] = [null, "DC", "LX", "VI"];
		this.digits[6] = [null, "DCC", "LXX", "VII"];
		this.digits[7] = [null, "DCCC", "LXXX", "VIII"];
		this.digits[8] = [null, "CM", "XC", "IX"];
		//incase function used outside republicast
		try {
			this.offset = Config.pageoffset;
			this.pages = Config.pages;
		}
		catch (e) {
		}
	};  //constructor

	//convert to roman numerial
	this.toRoman = function(intValue) {
		var strResult = "";
		if (intValue < 10000) {
			for (var intIndex = 0; intIndex < 4; intIndex++) {
				var intPower = Math.floor(Math.pow(10, 3 - intIndex));
				var intDigit = Math.floor(intValue / intPower);
				intValue -= intDigit * intPower;
				if (intDigit > 0)
					strResult += this.digits[intDigit - 1][intIndex];
			}
		}
		return strResult;
	};  //toRoman

	//convert from roman numerial
	this.fromRoman = function(strValue) {
		var strRoman = strValue.toUpperCase();
		var strDigit = "";
		var intResult = 0;
		var intPos = 0;
		for (var intPower = 0; intPower < 4; intPower++) {
			for (var intDigit = 8; intDigit >= 0; intDigit--) {
				strDigit = this.digits[intDigit][intPower];
				if ((strDigit != null) && (strRoman.indexOf(strDigit, intPos) == intPos)) {
					intResult = intResult + Math.floor(Math.pow(10, 3 - intPower)) * (intDigit + 1);
					intPos = intPos + strDigit.length;
					break;
				}
			} //for
			if (intPos == strRoman.length)
				break;
		} //for
		if (intPos != strRoman.length)
			intResult = 0;
		return intResult;
	};  //fromRoman

	//test if numeric
	this.isNumeric = function(strValue) {
		return ((!isNaN(parseInt(strValue))) && (parseInt(strValue) == strValue))
	};  //isNumeric

	//convert logical to physical number
	this.getphysical = function(strPage) {
		var intPage = 0;
		if (this.mappings != null) {
			for (var intIndex = 0; intIndex < this.mappings.length; intIndex++) {
				if (this.mappings[intIndex] == strPage) {
					intPage = intIndex + 1;
					break;
				}
			} //for
		}
		if (intPage == 0) {
			if (this.isNumeric(strPage)) {
				intPage = parseInt(strPage) + this.offset;
				if ((intPage > this.pages) || (intPage <= this.offset))
					intPage = 0;
			}
			else {
				intPage = this.fromRoman(strPage);
				if (intPage > this.offset)
					intPage = 0;
			}
		}
		return intPage;
	}; //getPhysical

	//covert physical to logical number
	this.getlogical = function(intPage) {
		if ((this.mappings != null) && (intPage > 0) && (intPage <= this.mappings.length))
			strPage = this.mappings[intPage - 1];
		else {
			var strPage = intPage - this.offset;
			if ((intPage > this.pages) || (intPage <= 0))
				strPage = "";
			else if (strPage <= 0) {
				var objRoman = new clsRoman();
				objRoman.offset = this.offset;
				objRoman.pages = this.pages;
				strPage = objRoman.toRoman(intPage).toLowerCase();
			}
		}
		return strPage;
	};   //getlogical

	this.constructor();
} //clsRoman

//------------------------------------------------------
// rectangle class
//------------------------------------------------------

function clsRect(objRect) {
	this.top = 0;
	this.left = 0;
	this.bottom = 0;
	this.right = 0;

	this.constructor = function(objRect) {
		if ((typeof (objRect) == "object") && (objRect != null)) {
			this.top = objRect.top;
			this.left = objRect.left;
			this.bottom = objRect.bottom;
			this.right = objRect.right;
		}
	};  //constructor

	this.inRect = function(intX, intY) {
		return ((intX >= this.left) && (intX < this.right) && (intY >= this.top) && (intY < this.bottom));
	};  //inRect
	
	this.constructor(objRect);
} //clsRect

//------------------------------------------------------
// browser detect object
//------------------------------------------------------

var BrowserDetect = {
	init: function() {
		this.browser = this.searchString(this.dataBrowser) || "Unknown";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "Unknown";
		this.major = parseInt(this.version);
		this.OS = this.searchString(this.dataOS) || "Unknown";
	},
	searchString: function(data) {
		for (var i = 0; i < data.length; i++) {
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function(dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			versionSearch: "Chrome/",
			identity: "Chrome"
		},
		{ string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "IE",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS: [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};   //BrowserDetect;
BrowserDetect.init();

