//=========================================================================================================
//High Peak CVS: Global JScript Functions and Variables
//=========================================================================================================
//V1.1
//Andrew Reid
//NOTE: NO ASP WILL BE COMPILED IN THIS INCLUDE FILE; JSCRIPT ONLY

//-----------------------------------------------------------------------------------------------------
//Global variables
//-----------------------------------------------------------------------------------------------------

var popupWindow
var window_properties = ""
var window_width = 0, window_height = 0
var window_x = 0, window_y = 0
var	screen_width = screen.availWidth, screen_height = screen.availHeight

//-----------------------------------------------------------------------------------------------------
//Global functions
//-----------------------------------------------------------------------------------------------------

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	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.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			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"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			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.init();

function DefineWindowProperties(width,height,scrollbars,resize) {
	//Centers window in screen according to width and height of window
	window_width = width
	window_height = height
	window_x = (screen_width-window_width)/2
	window_y = (screen_height-window_height)/2
	window_properties = "width="+window_width+",height="+window_height+",left="+window_x+",top="+window_y+",scrollbars="+scrollbars+",resizable="+resize+",toolbar=no,menubar=no"
}
function DefineWindowPropertiesToolBar(width,height,scrollbars,resize) {
	//Centers window in screen according to width and height of window
	window_width = width
	window_height = height
	window_x = (screen_width-window_width)/2
	window_y = (screen_height-window_height)/2
	window_properties = "width="+window_width+",height="+window_height+",left="+window_x+",top="+window_y+",scrollbars="+scrollbars+",resizable="+resize+",toolbar=no,menubar=yes"
}
function OpenWindow(url,windowID) {
	if (window_properties != "") {
		popupWindow = window.open(url,windowID,window_properties)
		popupWindow.opener = self
		popupWindow.focus()
	} else {
		alert("JScript logic error: cannot open popup window without first setting window properties")
	}
}
function CloseWindow() {
	popupWindow = null
	window_properties = ""
	window.close()
}
function CacheBuster() {
	randomString = ""
	for(i=0;i<8;i++) randomString+=String.fromCharCode(65+Math.floor(Math.random()*26))
	return randomString
}
function ValidateNumberField(numberField) {
	//Validates if field value contains only numbers, decimal points - NOTE: Call on event: onkeyup
	numberField = eval(numberField)	//Make sure type of variable is form object
	fieldValue = numberField.value
	passed = fieldValue.search(new RegExp("[^0-9.-]"))	//Search for non-numeric characters apart from minus (-) and decimal point (.)
	if (passed != -1) {
		//Failed validation (not number)
		OpenDialog("Input error","You cannot enter non numeric characters in a number field","Back","","","")
		//Replace all occurances of characters not belonging to the valid character set with nothing
		numberField.value = fieldValue.replace(new RegExp("[^0-9.-]","g"),"")
	}
}
function FormatCurrency(amount) {
	//Returns number in .xx format, e.g 1.9 becomes 1.90 but 1.99 stays 1.99
	return (amount == Math.floor(amount))?amount+".00":((amount*10 == Math.floor(amount*10))?amount+"0":amount)
}