// JavaScript Document

////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Global variables
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

var a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;

/*var blackoutDiv = null;
var popupContentHolder = null;
var popupContent = null;
var blackoutFadeInterval = null;*/

/// DON'T BOTHER WITH THIS CRAP BELOW, JUST USE SOMEBODY ELSE'S LIBRARY ?

var browser = new Object();

browser.supports = new Object();

browser.supports.geolocation = !!navigator.geolocation;

browser.supports.html5 = new Object();

browser.supports.html5.canvas = !!document.createElement("canvas").getContext;
browser.supports.html5.canvastext = browser.supports.html5.canvas ? typeof(document.createElement("canvas").getContext("2d").fillText) == "function" : false;
browser.supports.html5.video = !!document.createElement("video").canPlayType;
browser.supports.html5.inputplaceholder = typeof(document.createElement('input').placeholder) != "undefined";

browser.supports.html5.videoformats = new Object();

browser.supports.html5.videoformats.webm = browser.supports.html5.video ? document.createElement("video").canPlayType("video/webm; codecs='vp8, vorbis'") : false;
browser.supports.html5.videoformats.ogg = browser.supports.html5.video ? document.createElement("video").canPlayType("video/ogg; codecs='theora, vorbis'") : false;
browser.supports.html5.videoformats.h264 = browser.supports.html5.video ? document.createElement("video").canPlayType("video/mp4; codecs='avc1.42E01E, mp4a.40.2'") : false;

browser.supports.html5.inputtypes = new Object();

obj = document.createElement("input");
obj.setAttribute("type", "search");				browser.supports.html5.inputtypes.search = obj.type !== "text";
obj.setAttribute("type", "number");				browser.supports.html5.inputtypes.number = obj.type !== "text";
obj.setAttribute("type", "range");				browser.supports.html5.inputtypes.range = obj.type !== "text";
obj.setAttribute("type", "color");				browser.supports.html5.inputtypes.color = obj.type !== "text";
obj.setAttribute("type", "tel");				browser.supports.html5.inputtypes.tel = obj.type !== "text";
obj.setAttribute("type", "url");				browser.supports.html5.inputtypes.url = obj.type !== "text";
obj.setAttribute("type", "email");				browser.supports.html5.inputtypes.email = obj.type !== "text";
obj.setAttribute("type", "date");				browser.supports.html5.inputtypes.date = obj.type !== "text";
obj.setAttribute("type", "month");				browser.supports.html5.inputtypes.month = obj.type !== "text";
obj.setAttribute("type", "week");				browser.supports.html5.inputtypes.week = obj.type !== "text";
obj.setAttribute("type", "time");				browser.supports.html5.inputtypes.time = obj.type !== "text";
obj.setAttribute("type", "datetime");			browser.supports.html5.inputtypes.datetime = obj.type !== "text";
obj.setAttribute("type", "datetime-local");		browser.supports.html5.inputtypes.datetimelocal = obj.type !== "text";
obj = null;

////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	SUPERGLOBAL FUNCTIONS
//////////
//////////	The following functions can be used on any site.
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Window size
//////////
//////////	The following function returns an object that contains
//////////	the width and height of the window.
//////////
//////////	Last modified: 2010/08/20
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function getWindowSize() {
	var obj = new Object();
	
	if (window.innerWidth || window.innerHeight) {
		obj.width = window.innerWidth;
		obj.height = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		obj.width = document.documentElement.clientWidth;
		obj.height = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		obj.x = document.body.clientWidth;
		obj.y = document.body.clientHeight;
	} else if (document.body && (document.body.offsetWidth || document.body.offsetHeight)) {
		obj.width = document.body.offsetWidth;
		obj.height = document.body.offsetHeight;
	} else {
		obj.width = 0;
		obj.height = 0;
	}
	
	return obj;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Window scroll
//////////
//////////	The following function returns an object that contains
//////////	the x and y scrolling of the window.
//////////
//////////	Last modified: 2010/08/20
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function getWindowScroll() {
	var obj = new Object();
	
	if (typeof(window.scrollX) == "number" && typeof(window.scrollY) == "number") {
		obj.x = window.scrollX;
		obj.y = window.scrollY;
	} else if (typeof(window.pageXOffset) == "number" && typeof(window.pageYOffset) == "number") {
		obj.x = window.pageXOffset;
		obj.y = window.pageYOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		obj.x = document.body.scrollLeft;
		obj.y = document.body.scrollTop;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		obj.x = document.documentElement.scrollLeft;
		obj.y = document.documentElement.scrollTop;
	} else {
		obj.x = 0;
		obj.y = 0;
	}
	
	return obj;
}

function getWindowScrollLimits() {
	var obj = new Object();
	
	if (window.scrollMaxX || window.scrollMaxY) {
		obj.x = window.scrollMaxX;
		obj.y = window.scrollMaxY;
	} else if (document.documentElement && (document.documentElement.scrollWidth || document.documentElement.scrollHeight)) {
		obj.x = document.documentElement.scrollWidth;
		obj.y = document.documentElement.scrollHeight;
	} else if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
		obj.x = document.body.scrollWidth;
		obj.y = document.body.scrollHeight;
	} else {
		obj.x = 0;
		obj.y = 0;
	}
	
	return obj;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Get Document Bounds
//////////
//////////	The following function returns an object containing the offset x, y, width and height
//////////	values of an element on the page.
//////////
//////////	Last modified: 2011/05/20
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function getDocumentBounds(element, root) {
	if (typeof(element) == "string") {
		element = document.getElementById(element);
	} else if (typeof(element) != "object") {
		return false;
	}
	if (element === null) {
		return false;
	}
	
	var obj = new Object();
	
	// x and y coordinates
	
	obj.x = 0;
	obj.y = 0;
	
	var test_element = element;
	
	while (test_element.offsetParent !== null && (root == null || root != null && (test_element != root && test_element.className != root && test_element.id != root))) {
		obj.x += test_element.offsetLeft;
		obj.y += test_element.offsetTop;
		
		test_element = test_element.offsetParent;
	}
	
	// width and height values
	
	obj.w = 0;
	obj.h = 0;
	
	if (document.body.offsetWidth) {
		obj.w = element.offsetWidth;
	}
	if (document.body.offsetHeight) {
		obj.h = element.offsetHeight;
	}
	
	if (obj.w == 0) {
		if (element.style["width"]) {
			obj.w = element.style["width"];
		} else if (element.currentStyle) {
			obj.w = element.currentStyle["width"];
		} else if (window.getComputedStyle) {
			obj.w = document.defaultView.getComputedStyle(element, null).getPropertyValue("width");
		}
	}
	if (obj.h == 0) {
		if (element.style["height"]) {
			obj.h = element.style["height"];
		} else if (element.currentStyle) {
			obj.h = element.currentStyle["height"];
		} else if (window.getComputedStyle) {
			obj.h = document.defaultView.getComputedStyle(element, null).getPropertyValue("height");
		}
	}
	
	obj.w = parseFloat(obj.w);
	obj.h = parseFloat(obj.h);
	
	return obj;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Get Scrollbar Size
//////////
//////////	The following function determines the width/height of vertical/horizontal scrollbars
//////////	respectively.
//////////
//////////	Last modified: 2011/01/10
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function getScrollbarSize() {
	var outer = document.createElement("div");
	var inner = document.createElement("div");
	
	outer.style.position = "absolute";
	outer.style.left = 0 + "px";
	outer.style.top = 0 + "px";
	outer.style.width = 500 + "px";
	outer.style.height = 500 + "px";
	outer.style.overflow = "hidden";
	outer.style.visibility = "hidden";
	
	inner.style.height = 1000 + "px";
	
	outer.appendChild(inner);
	document.body.appendChild(outer);
	
	var w1, w2;
	
	w1 = getStyle(inner, "width");
	
	outer.style.overflow = "auto";
	
	w2 = getStyle(inner, "width");
	
	document.body.removeChild(outer);
	
	return w1 - w2;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Cookies
//////////
//////////	The following functions handle the creation, retrieval and removal of JS cookies
//////////
//////////	Last modified: 2010/07/07
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function setCookie(name, value, expires) {
	if (navigator.appName.indexOf("Microsoft") > -1) {
		document.cookie = name + "=" + value + "; expires=Wed, 1 Jan 2070 00:00:00 GMT; path=/";
	} else {
		document.cookie = name + "=" + value + "; expires=" + expires + "; path=/";
	}
}

function getCookie(name) {
	var value = "";
	
	var cookies = document.cookie.split("; ");
	
	for (var i = 0; i < cookies.length; i++) {
		var cookieData, cookieName, cookieValue;
		
		cookieData = cookies[i].split("=");
		cookieName = cookieData[0];
		cookieValue = cookieData[1];
		if (cookieName == name) {
			value = cookieValue;
		}
	}
	
	return value;
}

function deleteCookie(name) {
	document.cookie = name + "=; expires=Thu, 1 Jan 1970 00:00:00 GMT; path=/";
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Styles
//////////
//////////	The following function allows cross-platform compatible interpretation of
//////////	CSS styles.
//////////
//////////	Last modified: 2010/10/27
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function getStyle (element, style) {
	if (typeof(element) == "string") {
		element = document.getElementById(element);
	} else if (typeof(element) != "object") {
		return false;
	}
	if (element === null) {
		return false;
	}
	
	var value = null;
	
	if (style == "height") {
		if (document.body.offsetHeight) {
			value = element.offsetHeight;
		}
	} else if (style == "width") {
		if (document.body.offsetWidth) {
			value = element.offsetWidth;
		}
	}
	
	if (value == null) {
		if (element.style[style]) {
			value = element.style[style];
		} else if (element.currentStyle) {
			value = element.currentStyle[style];
		} else if (window.getComputedStyle) {
			value = document.defaultView.getComputedStyle(element, null).getPropertyValue(style);
		}
	}
	
	return value;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Alpha channel
//////////
//////////	The following functions allow cross-platform compatible opacity filtering of
//////////	HTML elements
//////////
//////////	Last modified: 2011/05/18
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function getAlpha(element) {
	var alpha = new Number(1.00);
	
	if (typeof(element) == "string") {
		element = document.getElementById(element);
	} else if (typeof(element) != "object") {
		return null;
	}
	if (element === null) {
		return null;
	}
	
	if (navigator.appName.indexOf("Microsoft") > -1) {
		if (element.style.filters == null) {
			if (element.style.filter) {
				if (element.style.filter.indexOf("alpha(opacity=") > -1) {
					alpha = Number(element.style.filter.slice(element.style.filter.indexOf("alpha(opacity=") + String("alpha(opacity=").length, element.style.filter.indexOf(")", String("alpha(opacity=").length))) / 100;
				}
			}
		} else {
			if (element.style.filters.alpha.opacity) {
				alpha = parseFloat(element.style.filters.alpha.opacity);
			}
		}
	} else {
		if (element.style.opacity) {
			alpha = parseFloat(element.style.opacity);
		}
	}
	
	if (isNaN(alpha)) {
		alpha = 1.00;
	} else {
		alpha = Math.round(alpha * 100) / 100
	}
	
	return alpha;
}

function setAlpha(element, alpha) {
	if (typeof(element) == "string") {
		element = document.getElementById(element);
	} else if (typeof(element) != "object") {
		return false;
	}
	if (element === null) {
		return false;
	}
	if (typeof(alpha) != "number") {
		alpha = Number(alpha);
		if (isNaN(alpha)) {
			return false;
		}
	}
	
	if (navigator.appName.indexOf("Microsoft") > -1) {
		if (element.style.filters == null) {
			element.style.filter = "alpha(opacity=" + Math.round(alpha * 100) + ")";
		} else {
			element.style.filters.alpha.opacity = alpha;
		}
	} else {
		element.style.opacity = alpha;
	}
	
	return true;
}

function clearAlpha(element) {
	if (typeof(element) == "string") {
		element = document.getElementById(element);
	} else if (typeof(element) != "object") {
		return false;
	}
	if (element === null) {
		return false;
	}
	
	if (navigator.appName.indexOf("Microsoft") > -1) {
		if (element.style.filters == null) {
			if (element.style.filter && element.style.removeAttribute) {
				element.style.removeAttribute("filter");
			}
		} else {
			if (element.style.filters.alpha && element.style.filters.removeAttribute) {
				element.style.filters.removeAttribute("alpha");
			}
		}
	} else {
		if (element.style.opacity && element.style.removeProperty) {
			element.style.removeProperty("opacity");
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	get_swf
//////////
//////////	Useful if you want to call Flash functions from JavaScript
//////////
//////////	Last modified: 2011/10/05
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function get_swf(swf_id) {
	try {
		o = swfobject.getObjectById(swf_id);
		if (o !== null) {
			return o;
		}
	} catch (e) {}
	if (document[swf_id]) {
		if (document[swf_id].length) {
			return document[swf_id][1];
		} else {
			return document[swf_id];
		}
	} else {
		if (window[swf_id]) {
			return window[swf_id];
		}
	}
	return document.getElementById(swf_id);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	JS POST functions
//////////
//////////	These allow you to quickly post common tasks to PHP
//////////
//////////	Last modified: 2011/05/23
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function deleteFile(filepath) {
	b = confirm("Are you sure you want to delete this file?");
	
	if (b) {
		f = document.createElement("form");
		f.method = "post";
		
		e = document.createElement("input");
		e.type = "hidden";
		e.name = "delete_file";
		e.value = 1;
		
		f.appendChild(e);
		
		e = document.createElement("input");
		e.type = "hidden";
		e.name = "filepath";
		e.value = filepath;
		
		f.appendChild(e);
		
		document.body.appendChild(f);
		
		f.submit();
	}
}

function deleteDir(dirpath) {
	b = confirm("Are you sure you want to delete this entire folder and all of its contents?");
	
	if (b) {
		f = document.createElement("form");
		f.method = "post";
		
		e = document.createElement("input");
		e.type = "hidden";
		e.name = "delete_dir";
		e.value = 1;
		
		f.appendChild(e);
		
		e = document.createElement("input");
		e.type = "hidden";
		e.name = "dirpath";
		e.value = dirpath;
		
		f.appendChild(e);
		
		document.body.appendChild(f);
		
		f.submit();
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	urlencode/urldecode
//////////
//////////	JS versions of the PHP functions
//////////
//////////	Last modified: 2011/07/12
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function urlencode(str) {
	str = escape(str);
	str = str.replace(/\@/g, "%40");
	str = str.replace(/\*/g, "%2A");
	str = str.replace(/\+/g, "%2B");
	str = str.replace(/\//g, "%2F");
	str = str.replace(/\%20/g, "+");
	return str;
}

function urldecode(str) {
	str = str.replace(/\+/g, " ");
	str = unescape(str)
	return str;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	AJAX
//////////
//////////	Functions for creating cross-platform AJAX objects
//////////
//////////	Last modified: 2011/10/06
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

/*--------------------------------------------------------------------------------------------------

onreadystatechange

readyState
0: request not initialized 
1: server connection established
2: request received 
3: processing request 
4: request finished and response is ready

status
200: "OK"
404: Page not found

--------------------------------------------------------------------------------------------------*/

// Define variables:

var ajax = null;
var ajax_active = false;

// Define functions:

function ajax_create_object() {
	if (window.XMLHttpRequest) {
		o = new XMLHttpRequest();
	} else {
		o = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return o;
}

function ajax_post(url, postdata, async) {
	ajax.open("POST", url, async);
	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	if (navigator.appName.indexOf("Microsoft") > -1) {
		ajax.setRequestHeader("Content-length", postdata.length);
		ajax.setRequestHeader("Connection", "close");
	}
	ajax.send(postdata);
}

// Execute:

ajax = ajax_create_object();

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Launch popup
//////////
//////////	The following code allows you to launch a popup that fades out
//////////	the rest of the background
//////////
//////////	Last modified: 2010/08/18
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

/*function launch_popup(type, src, width, height) {
	blackoutDiv = document.createElement("div");
	blackoutDiv.style.position = "fixed";
	blackoutDiv.style.zIndex = "998";
	blackoutDiv.style.top = "0px";
	blackoutDiv.style.left = "0px";
	blackoutDiv.style.width = "100%";
	blackoutDiv.style.height = "100%";
	blackoutDiv.style.backgroundColor = "#000000";
	
	popupContentHolder = document.createElement("div");
	popupContentHolder.style.position = "absolute";
	popupContentHolder.style.zIndex = "999";
	popupContentHolder.style.width = width + "px";
	popupContentHolder.style.height = height + "px";
	popupContentHolder.style.backgroundColor = "#FFFFFF";
	
	relocatePopup();
	
	setAlpha(blackoutDiv, 0.05);
	setAlpha(popupContentHolder, 0.05);
	
	if (type == "image") {
		popupContent = document.createElement("img");
		popupContent.src = src;
		popupContent.width = width;
		popupContent.height = height;
	} else if (type == "html") {
		popupContent = document.createElement("div");
		popupContent.innerHTML = src; // BETTER METHOD NEEDED. should remove the other div from the page and add it to the popup element instead of copying its contents.
	}
	
	popupContentHolder.appendChild(popupContent);
	document.body.appendChild(popupContentHolder);
	document.body.appendChild(blackoutDiv);
	
	blackoutFadeInterval = setInterval(fadeInPopup, 20);
	
	window.onresize = relocatePopup;
	window.onscroll = relocatePopup;
}

function relocatePopup() {
	if (popupContentHolder != null) {
		var windowSizeObj = getWindowSize();
		var windowScrollObj = getWindowScroll();
		var windowScrollLimitsObj = getWindowScrollLimits();
		
		var width, height, top, left;
		
		width = parseFloat(popupContentHolder.style.width);
		height = parseFloat(popupContentHolder.style.height);
		
		if (height > windowSizeObj.height) {
			top = windowScrollObj.y - (windowScrollObj.y / (windowScrollLimitsObj.y - windowSizeObj.height)) * (height - windowSizeObj.height);
		} else {
			top = windowSizeObj.height / 2;
			top -= parseFloat(popupContentHolder.style.height) / 2;
			top += windowScrollObj.y;
		}
		
		if (width > windowSizeObj.width) {
			left = windowScrollObj.x - (windowScrollObj.x / (windowScrollLimitsObj.x - windowSizeObj.width)) * (width - windowSizeObj.width);
		} else {
			left = windowSizeObj.width / 2;
			left -= parseFloat(popupContentHolder.style.width) / 2;
			left += windowScrollObj.x;
		}
		
		popupContentHolder.style.top = top + "px";
		popupContentHolder.style.left = left + "px";
	}
}

function fadeInPopup() {
	setAlpha(blackoutDiv, getAlpha(blackoutDiv) + 0.05);
	setAlpha(popupContentHolder, getAlpha(blackoutDiv) * 1.4285715);
	if (getAlpha(blackoutDiv) >= 0.70) {
		setAlpha(blackoutDiv, 0.70);
		clearAlpha(popupContentHolder);
		blackoutDiv.onclick = closePopup;
		popupContentHolder.onclick = closePopup;
		clearInterval(blackoutFadeInterval);
		blackoutFadeInterval = null;
	}
}

function closePopup() {
	blackoutDiv.onclick = null;
	popupContentHolder.onclick = null;
	blackoutFadeInterval = setInterval(fadeOutPopup, 20);
}

function fadeOutPopup() {
	setAlpha(blackoutDiv, getAlpha(blackoutDiv) - 0.05);
	setAlpha(popupContentHolder, getAlpha(blackoutDiv) * 1.4285715);
	if (getAlpha(blackoutDiv) <= 0.00) {
		clearAlpha(blackoutDiv);
		clearAlpha(popupContentHolder);
		clearPopup();
		clearInterval(blackoutFadeInterval);
		blackoutFadeInterval = null;
	}
}

function clearPopup() {
	document.body.removeChild(popupContentHolder);
	document.body.removeChild(blackoutDiv);
	popupContentHolder = null;
	blackoutDiv = null;
	window.onresize = null;
	window.onscroll = null;
}*/

////////////////////////////////////////////////////////////////////////////////////////////////////

