
// 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;*/

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Load JS-specific CSS
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

var js_css_element = document.getElementById("js_css");

if (js_css_element !== null) {
	js_css_element.href = "includes/css/js.css";
}

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



////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	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");
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	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 = "cmd";
		e.value = "delete_file";
		
		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 = "cmd";
		e.value = "delete_dir";
		
		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();
	}
}

function download(path) {
	f = document.createElement("form");
	f.action = "download.php";
	f.method = "post";
	
	e = document.createElement("input");
	e.type = "hidden";
	e.name = "path";
	e.value = path;
	
	f.appendChild(e);
	
	document.body.appendChild(f);
	
	f.submit();
	
	document.body.removeChild(f);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	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;
}*/

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

