﻿var Phizz = {
	Version: "1.0",
	toString: function() {
		return "Phizz Javascript Library Version ".concat(this.Version);
	}
};

Phizz.Dom = {
	ByID: function(id) {
		return document.getElementById(id);
	},
	
	ByName: function(name) {
		return document.getElementsByName(name);
	},
	
	ByTagName: function(tagName) {
		return document.getElementsByTagName(tagName);
	},
	
	GetAbsolutePosition: function(obj) {
		var x = 0;
		var y = 0;
		if (obj.offsetParent) {
			x = obj.offsetLeft;
			y = obj.offsetTop;
			while (obj = obj.offsetParent) {
				x += obj.offsetLeft;
				y += obj.offsetTop;
			}
		}
		return {x:x, y:y};
	},
	
	GetScrollPosition: function() {
		var x = window.pageXOffset ? window.pageXOffset : 0;
		if (document.documentElement && document.documentElement.scrollLeft > x) x = document.documentElement.scrollLeft;
		if (document.body && document.body.scrollLeft > x) x = document.body.scrollLeft;
		var y = window.pageYOffset ? window.pageYOffset : 0;
		if (document.documentElement && document.documentElement.scrollTop > y) y = document.documentElement.scrollTop;
		if (document.body && document.body.scrollTop > y) y = document.body.scrollTop;
		return {x: x, y: y};
	},
	
	GetScrollSize: function() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		return {x: xScroll, y: yScroll};
	},
	
	GetWindowSize: function() {
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		
		return {width:windowWidth, height:windowHeight};
	},
	
	GetDocumentSize: function() {
		var scrollSize = Phizz.Dom.GetScrollSize();
		var windowSize = Phizz.Dom.GetWindowSize();
		
		// for small pages with total height less then height of the viewport
		if (scrollSize.y < windowSize.height){
			pageHeight = windowSize.height;
		} else { 
			pageHeight = scrollSize.y;
		}

		// for small pages with total width less then width of the viewport
		if (scrollSize.x < windowSize.width){	
			pageWidth = windowSize.width;
		} else {
			pageWidth = scrollSize.x;
		}

		return {width:pageWidth, height:pageHeight};
	},
	
	AlignToWindowCenter: function(obj) {
		if (obj.style.visibility == "visible") {
			var scrollPosition = Phizz.Dom.GetScrollPosition();
			var windowSize = Phizz.Dom.GetWindowSize();
			var left = scrollPosition.x + (windowSize.width * 0.5) - (obj.offsetWidth * 0.5);
			var top = scrollPosition.y + (windowSize.height * 0.5) - (obj.offsetHeight * 0.5);
			obj.style.left = left + "px";
			obj.style.top = top + "px";
		}
	},
	
	GetEventTarget: function(e) {
		var obj;
		if (!e) var e = window.event;
		if (e.target) {
			obj = e.target;
		} else if (e.srcElement) {
			obj = e.srcElement;
		}
		if (obj.nodeType == 3) obj = obj.parentNode;
		return obj;
	},
	
	CancelEventPropagation: function(e) {
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	},
	
	RegisterWindowLoadEvent: function(objFunction) {
		var existingHandler = window.onload; 
		if (typeof window.onload != 'function') { 
			window.onload = objFunction; 
		} else { 
			window.onload = function() { 
				if (existingHandler) existingHandler();
				objFunction(); 
			} 
		} 
	},
	
	RegisterWindowResizeEvent: function(objFunction) {
		var existingHandler = window.onresize; 
		if (typeof window.onresize != 'function') { 
			window.onresize = objFunction; 
		} else { 
			window.onresize = function() { 
				if (existingHandler) existingHandler();
				objFunction(); 
			} 
		}
	},
	
	RegisterWindowBlurEvent: function(objFunction) {
		var existingHandler = window.onblur; 
		if (typeof window.onblur != 'function') { 
			window.onblur = objFunction; 
		} else { 
			window.onblur = function() { 
				if (existingHandler) existingHandler();
				objFunction();
			} 
		}
	},
	
	RegisterWindowScrollEvent: function(objFunction) {
		var existingHandler = window.onscroll; 
		if (typeof window.onscroll != 'function') { 
			window.onscroll = objFunction; 
		} else { 
			window.onscroll = function() { 
				if (existingHandler) existingHandler();
				objFunction(); 
			} 
		} 
	},
	
	RegisterDocumentClickEvent: function(objFunction) {
		var existingHandler = document.onclick; 
		if (typeof document.onclick != 'function') { 
			document.onclick = objFunction; 
		} else { 
			document.onclick = function() { 
				if (existingHandler) existingHandler();
				objFunction(); 
			} 
		} 
	},
	
	RegisterDocumentOnMouseDownEvent: function(objFunction) {
		var existingHandler = document.onmousedown; 
		if (typeof document.onmousedown != 'function') { 
			document.onmousedown = objFunction; 
		} else { 
			document.onmousedown = function() { 
				if (existingHandler) existingHandler();
				objFunction(); 
			} 
		} 
	},
	
	RegisterDocumentOnMouseUpEvent: function(objFunction) {
		var existingHandler = document.onmouseup; 
		if (typeof document.onmouseup != 'function') { 
			document.onmouseup = objFunction; 
		} else { 
			document.onmouseup = function() { 
				if (existingHandler) existingHandler();
				objFunction(); 
			} 
		} 
	},
	
	RegisterDocumentOnMouseMouseEvent: function(objFunction) {
		var existingHandler = document.onmousemouse; 
		if (typeof document.onmousemouse != 'function') { 
			document.onmousemouse = objFunction; 
		} else { 
			document.onmousemouse = function() { 
				if (existingHandler) existingHandler();
				objFunction(); 
			} 
		} 
	},

	GetMouseCoordinates: function(e) {
		var x = 0;
		var y = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			x = e.pageX;
			y = e.pageY;
		} else if (e.clientX || e.clientY) 	{
			x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		return {x:x, y:y}
	},

	GetMouseCoordinatesInElement: function(obj, e) {
		var p = Phizz.Dom.GetMouseCoordinates(e);
		var p2 = Phizz.Dom.GetAbsolutePosition(obj);
		p.x -= p2.x;
		p.y -= p2.y;
		return p;
	},
	
	CheckboxList: {
		GetCheckedObject: function(name) {
			var objs = Phizz.Dom.ByName(name);
			if (objs == null || objs.length == 0) return null;
			for (var i = 0; i < objs.length; i++) {
				if (objs[i].checked) return objs[i];
			}
		},
		GetCheckedValue: function(name) {
			var objs = Phizz.Dom.ByName(name);
			if (objs == null || objs.length == 0) return null;
			var value = "";
			for (var i = 0; i < objs.length; i++) {
				if (objs[i].checked) value += (value.length > 0 ? "," : "") + objs[i].value;
			}
			return value;
		},
		SetCheckedValue: function(name, value) {
			var objs = Phizz.Dom.ByName(name);
			for (var i = 0; i < objs.length; i++) {
				if (objs[i].value == value) objs[i].checked = true;
			}
		},
		SetCheckStatus: function(name, isChecked) {
			var objs = Phizz.Dom.ByName(name);
			for (var i = 0; i < objs.length; i++) objs[i].checked = isChecked;
		}
	}
};



Phizz.Browser = {
	IsIE: (navigator.appVersion.indexOf("MSIE") > -1),
	IsIE6: (navigator.appName == "Microsoft Internet Explorer" && (navigator.appVersion.indexOf("MSIE 6") > -1 || navigator.appVersion.indexOf("MSIE 5.5") > -1)),
	IsSafari: (navigator.appVersion.indexOf("Safari") > -1)
};


Phizz.Css = {
	SetOpacity: function(obj, percentage) {
		obj.style.opacity = percentage * 0.01;
		obj.style.MozOpacity = percentage * 0.01;
		obj.style.KhtmlOpacity = percentage * 0.01;
		obj.style.filter = "alpha(opacity=".concat(percentage, ")");
	}
};


Phizz.Navigation = {
	Go: function(url) {
		window.location.href = url;
	},
	
	GoAndMaintainQueryString: function(url) {
		var currentUrl = window.location.href;
		if (currentUrl.indexOf("#") > -1) currentUrl = currentUrl.substr(0, currentUrl.indexOf("#"));
		if (currentUrl.indexOf("?") > -1) {
			Phizz.Navigation.Go(url.concat("?", currentUrl.split("?")[1]));
		} else {
			Phizz.Navigation.Go(url);
		}
	},
	
	Back: function() {
		window.history.back();
	},

	GetPageTitle: function() {
		var objTitle = document.getElementsByTagName("title")[0];
		if (window.sidebar) {
			return objTitle.textContent;
		} else {
			return objTitle.innerText;
		}
	},

	AddBookmark: function() {
		var title = this.GetPageTitle();
		var url = window.location.href;
		if (window.sidebar) { 
			window.sidebar.addPanel(title, url, ""); 
		} else if (window.external) {
			window.external.AddFavorite(url, title);
		}
	},

	OpenWindow: function(url, preferredWidth, preferredHeight, windowName, forceScroll) {
		var usedWidth = Math.min(preferredWidth, screen.width-80);
		var usedHeight = Math.min(preferredHeight, screen.height-120);
		if (windowName == null) windowName = "w" + Math.round(Math.random() * 10000);
		window.open(url, windowName, "width=" + usedWidth + ", height=" + usedHeight + ", top=" + (screen.height/2 - usedHeight/2 - 20) + ", left=" + (screen.width/2 - usedWidth/2) + ", location=no, menubar=no, resizable=yes, status=no, toolbar=no, scrollbars=" + (forceScroll || (usedWidth < preferredWidth || usedHeight < preferredHeight) ? "yes" : "no"));
	},

	OpenCustomWindow: function(url, preferredWidth, preferredHeight, windowName, showLocationBar, showMenuBar, isResizable, showStatusBar, showToolbar, showScrollBars) {
		var usedWidth = Math.min(preferredWidth, screen.width-80);
		var usedHeight = Math.min(preferredHeight, screen.height-120);
		if (windowName == null) windowName = "w" + Math.round(Math.random() * 10000);
		window.open(url, windowName, "width=" + usedWidth + ", height=" + usedHeight + ", top=" + (screen.height/2 - usedHeight/2 - 20) + ", left=" + (screen.width/2 - usedWidth/2) + ", location=" + (showLocationBar ? "yes" : "no") + ", menubar=" + (showMenuBar ? "yes" : "no") + ", resizable=" + (isResizable ? "yes" : "no") + ", status=" + (showStatusBar ? "yes" : "no") + ", toolbar=" + (showToolbar ? "yes" : "no") + ", scrollbars=" + (showScrollBars ? "yes" : "no"));
	},

	CreateCookie: function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},

	ReadCookie: function(name) {
		var nameEQ = 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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},

	EraseCookie: function(name) {
		createCookie(name,"",-1);
	}
};



Phizz.Navigation.QueryString = {
	_values: null,
	
	Parse: function() {
		var url = window.location.href;
		this._values = new Array();
		if (url.indexOf("?") > -1) {
			var qs = url.substr(url.indexOf("?") + 1);
			var pairs = qs.split("&");
			qs = "";
			for (var i = 0; i < pairs.length; i++) {
				var keyAndValue = pairs[i].split("=");
				this._values[keyAndValue[0]] = keyAndValue[1];
			}
		}
	},
	
	Get: function(key) {
		if (this._values == null) this.Parse();
		return this._values[key];
	},
	
	Set: function(key, value) {
		var url = window.location.href;
		var qs = "";
		if (url.indexOf("?") > -1) {
			qs = url.substr(url.indexOf("?") + 1);
			var pairs = qs.split("&");
			qs = "";
			for (var i = 0; i < pairs.length; i++) {
				if (pairs[i].substr(0, key.length + 1) != (key + "=")) qs += (qs.length == 0 ? "?" : "&") + pairs[i];
			}
			url = url.substr(0, url.indexOf("?")) + qs;
		}
		window.location.href = url + (qs.length == 0 ? "?" : "&") + encodeURIComponent(key) + "=" + encodeURIComponent(value);
	},
	
	SetMultiple: function(keyArray, valueArray) {
		if (keyArray.length != valueArray.length) alert ("ERROR : Phizz.QueryString.SetMultiple() : keyArray and valueArray must be the same length.");
		var url = window.location.href;
		var qs = "";
		if (url.indexOf("?") > -1) {
			qs = url.substr(url.indexOf("?") + 1);
			var pairs = qs.split("&");
			qs = "";
			for (var i = 0; i < pairs.length; i++) {
				var keyInUse = false;
				for (var j = 0; j < keyArray.length; j++) {
					if (pairs[i].substr(0, keyArray[j].length + 1) == (keyArray[j] + "=")) {
						keyInUse = true; break;
					}
				}
				if (!keyInUse) qs += (qs.length == 0 ? "?" : "&") + pairs[i];
			}
			url = url.substr(0, url.indexOf("?")) + qs;
		}
		var newPairs = "";
		for (var i = 0; i < keyArray.length; i++) newPairs += (newPairs.length == 0 ? "" : "&") + encodeURIComponent(keyArray[i]) + "=" + encodeURIComponent(valueArray[i]);
		window.location.href = url + (qs.length == 0 ? "?" : "&") + newPairs;
	},
	
	Delete: function(key) {
		var url = window.location.href;
		var qs = "";
		if (url.indexOf("?") > -1) {
			qs = url.substr(url.indexOf("?") + 1);
			var pairs = qs.split("&");
			qs = "";
			for (var i = 0; i < pairs.length; i++) {
				if (pairs[i].substr(0, key.length + 1) != (key + "=")) qs += (qs.length == 0 ? "?" : "&") + pairs[i];
			}
			url = url.substr(0, url.indexOf("?")) + qs;
		}
		window.location.href = url;
	},
	
	DeleteMultiple: function(keyArray) {
		var url = window.location.href;
		var qs = "";
		if (url.indexOf("?") > -1) {
			qs = url.substr(url.indexOf("?") + 1);
			var pairs = qs.split("&");
			qs = "";
			for (var i = 0; i < pairs.length; i++) {
				var keyInUse = false;
				for (var j = 0; j < keyArray.length; j++) {
					if (pairs[i].substr(0, keyArray[j].length + 1) == (keyArray[j] + "=")) {
						keyInUse = true; break;
					}
				}
				if (!keyInUse) qs += (qs.length == 0 ? "?" : "&") + pairs[i];
			}
			url = url.substr(0, url.indexOf("?")) + qs;
		}
		window.location.href = url;
	}
};


Phizz.Tools = {
	ParseKeyValuePairs: function(s) {
		var a = new Array();
		var pairs = s.split("&");
		for (var i = 0; i < pairs.length; i++) {
			var keyAndValue = pairs[i].split("=");
			a[keyAndValue[0]] = keyAndValue[1];
		}
		return a;
	},
	
	_PreloadedImages: new Array(),
	PreloadImage: function(src, width, height) {
		this._PreloadedImages.push(new Image(width, height));
		this._PreloadedImages[this._PreloadedImages.length - 1].src = src;
	},
	
	EnableIE6ImageTransparency: function(objImage, blankImageUrl) {
		if (objImage != null) {
			var src = objImage.src;
			objImage.src = blankImageUrl;
			objImage.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src='" + src + "')";
		}
	},
	
	EnableIE6BackgroundTransparency: function(obj, imageUrl) {
		if (obj != null) {
			obj.style.backgroundImage = "none";
			obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src='" + imageUrl + "')";
		}
	},
	
	ShowIE6Foreground: function() {
		var objsObject = document.getElementsByTagName("OBJECT");
		for (var i = 0; i < objsObject.length; i++) objsObject[i].style.visibility = "visible";
		var objsSelect = document.getElementsByTagName("SELECT");
		for (var i = 0; i < objsSelect.length; i++) objsSelect[i].style.visibility = "visible";
	},
	
	HideIE6Foreground: function() {
		var objsObject = document.getElementsByTagName("OBJECT");
		for (var i = 0; i < objsObject.length; i++) objsObject[i].style.visibility = "hidden";
		var objsSelect = document.getElementsByTagName("SELECT");
		for (var i = 0; i < objsSelect.length; i++) objsSelect[i].style.visibility = "hidden";
	},

	TrimString: function(s) {
		return s.replace(/\xA0+/g, "").replace(/(^\s+)|(\s+$)/g, "");
	}
};


Phizz.Validation = {
	DisplayFunction: function(s) {alert(s)},
	objFocus: null,
	
	ReturnFocus: function() {
		if (this.objFocus == null) return;
		this.objFocus.focus();
		this.objFocus = null;
	},

	Value: function(id) {
		var obj = Phizz.Dom.ByID(id);
		if (Phizz.Tools.TrimString(obj.value).length == 0) {
			this.DisplayFunction("Please provide a value for the " + obj.title + " field.");
			(this.objFocus = obj).focus();
			return false;
		} else {
			return true;
		}
	},

	Regex: function(id, regExp, errorMessage, allowEmpty) {
		var obj = Phizz.Dom.ByID(id);
		if (allowEmpty && obj.value.length == 0) return true;
		if (!regExp.test(obj.value)) {
			this.DisplayFunction(errorMessage);
			(this.objFocus = obj).focus();
			return false;
		} else {
			return true;
		}
	},

	Equivalent: function(id1, id2) {
		var obj1 = Phizz.Dom.ByID(id1);
		var obj2 = Phizz.Dom.ByID(id2);
		if (Phizz.Tools.TrimString(obj1.value) != Phizz.Tools.TrimString(obj2.value)) {
			this.DisplayFunction("The " + obj1.title + " and " + obj2.title + " field values must be equivalent. Please check and try again.");
			obj1.focus();
			return false;
		} else {
			return true;
		}
	},

	Email: function(id, allowEmpty) {
		var obj = Phizz.Dom.ByID(id);
		if (allowEmpty && obj.value.length == 0) return true;
		if (!Phizz.Validation.IsEmail(obj.value)) {
			this.DisplayFunction("Please enter a valid email address in the " + obj.title + " field.");
			(this.objFocus = obj).focus();
			return false;
		} else {
			return true;
		}
	},

	IsEmail: function(s) {
		return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(s);
	},

	Integer: function(id, allowEmpty) {
		var obj = Phizz.Dom.ByID(id);
		if (allowEmpty && obj.value.length == 0) return true;
		if (!Phizz.Validation.IsInteger(obj.value)) {
			this.DisplayFunction("Please enter a valid whole number value in the " + obj.title + " field.");
			(this.objFocus = obj).focus();
			return false;
		} else {
			return true;
		}
	},

	IsInteger: function(s) {
		return /^-?\d+$/.test(s);
	},

	Decimal: function(id, allowEmpty) {
		var obj = Phizz.Dom.ByID(id);
		if (allowEmpty && obj.value.length == 0) return true;
		if (!Phizz.Validation.IsDecimal(obj.value)) {
			this.DisplayFunction("Please enter a valid numeric value in the " + obj.title + " field.");
			(this.objFocus = obj).focus();
			return false;
		} else {
			return true;
		}
	},

	IsDecimal: function(s) {
		return /^-?\d+(\.\d+)?$/.test(s);
	},

	DDMMMYYYYDate: function(id, allowEmpty) {
		var obj = Phizz.Dom.ByID(id);
		if (allowEmpty && obj.value.length == 0) return true;
		if (!Phizz.Validation.IsDDMMMYYYYDate(obj.value)) {
			this.DisplayFunction("Please enter a valid date value in the " + obj.title + " field, in the format [dd-Mmm-yyyy]. If unsure, please use the Date Picker.");
			(this.objFocus = obj).focus();
			return false;
		} else {
			return true;
		}
	},

	IsDDMMMYYYYDate: function(s) {
		var isValid = true;
		var dateParts = (s.indexOf("-") > -1 ? s.split("-") : new Array());
		if (dateParts.length != 3) isValid = false;
		if (isValid) {
			var dayPart = dateParts[0]; if (dayPart.length == 2 && dayPart.substr(0,1) == "0") dayPart = dayPart.substr(1,1);
			var monthPart = dateParts[1].toLowerCase(); monthPart = monthPart.substr(0,1).toUpperCase() + monthPart.substr(1);
			var yearPart = dateParts[2];
			if (!/^[0-9]{1,2}$/.test(dayPart) || !/^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)$/.test(monthPart) || !/^[1-9]{1}[0-9]{3}$/.test(yearPart)) isValid = false;
			if (isValid) {
				dayPart = parseInt(dayPart);
				yearPart = parseInt(yearPart);
				if (dayPart < 1 || dayPart > 31 || (dayPart > 30 && /^(Apr|Jun|Sep|Nov)$/.test(monthPart)) || (dayPart > 29 && monthPart == "Feb") || (dayPart == 29 && monthPart == "Feb" && !IsLeapYear(yearPart))) isValid = false;
			}
		}
		return isValid;
	},

	IsLeapYear: function(year) {
		if (year % 4 == 0) {
			if (year % 100 == 0) {
				return (year % 400 == 0);
			} else {	
				return true;
			}
		} else {
			return false;
		}
	},
	
	Dropdown: function(id, allowEmpty) {
		var obj = Phizz.Dom.ByID(id);
		if (obj.selectedIndex == 0 || (!allowEmpty && obj.options[obj.selectedIndex].value.length == 0)) {
			this.DisplayFunction("A value must be selected for the " + obj.title + " field.");
			(this.objFocus = obj).focus();
			return false;
		} else {
			return true;
		}
	},
	
	CheckboxList: function(name, customMessage) {
		var objs = Phizz.Dom.ByName(name);
		if (objs == null || objs.length == 0) return true;
		var checked = false;
		for (var i = 0; i < objs.length; i++) {
			if (objs[i].checked) {checked = true; break;}
		}
		if (!checked) {
			if (customMessage != null) {
				this.DisplayFunction(customMessage);
			} else {
				this.DisplayFunction("A value must be selected for the " + objs[0].title + " field.");
			}
			objs[0].focus();
			return false;
		} else {
			return true;
		}
	},

	HexColor: function(id, allowEmpty) {
		var obj = Phizz.Dom.ByID(id);
		if (allowEmpty && obj.value.length == 0) return true;
		if (!Phizz.Validation.IsHexColor(Phizz.Tools.TrimString(obj.value))) {
			this.DisplayFunction("Please enter a valid 3 or 6 digit hexadecimal colour value in the " + obj.title + " field. If you are unsure, use the colour chooser provided." + (allowEmpty ? "\r\rIf you do not want to give a value for this field, please leave it blank." : ""));
			(this.objFocus = obj).focus();
			return false;
		} else {
			return true;
		}
	},
	
	IsHexColor: function(s) {
		return /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(s);
	}
};

Phizz.Blackout = {
	N: 0,
	objBlackout: null,
	
	Create: function(hexColor) {
		this.objBlackout = document.createElement("div");
		this.objBlackout.style.cssText = "position:absolute;z-index:5;visibility:hidden;width:100%;height:1px;top:0px;left:0px;background-color:#" + (hexColor != null && /^[0-9A-Fa-f]{6}$/.test(hexColor) ? hexColor : "303132") + ";opacity:0.87;-moz-opacity:0.87;filter:alpha(opacity=87);";
		this.objBlackout.appendChild(document.createTextNode(" "));
		document.body.appendChild(this.objBlackout);
		Phizz.Dom.RegisterWindowResizeEvent(function() {Phizz.Blackout.Render();});
	},
	
	Show: function(hexColor) {
		if (this.objBlackout == null) this.Create(hexColor);
		if (this.N == 0) {
			if (Phizz.Browser.IsIE6) Phizz.Tools.HideIE6Foreground();
			this.objBlackout.style.visibility = "visible";
			this.Render();
		}
		this.N++;
	},
	
	Render: function() {
		if (this.objBlackout != null && this.objBlackout.style.visibility == "visible") {
			this.objBlackout.style.height = "1px";
			this.objBlackout.style.height = Phizz.Dom.GetDocumentSize().height + "px";
		}
	},

	Hide: function() {
		if (this.N == 1) {
			this.objBlackout.style.visibility = "hidden";
			if (Phizz.Browser.IsIE6) Phizz.Tools.ShowIE6Foreground();
		}
		this.N--;
	}
};