//Redundant code, should not be used. Is only included here to prevent errors;
var bPageIsLoaded = false;
function SwapImage(){}
function RestoreImage(){}
function PreloadImages(){bPageIsLoaded=true;}

/************************************************************************
 *	@function	setCookie()
 *	@notes		Generic Set Cookie routine
 ***********************************************************************/
function setCookie(name, value, expires) {
    var cookie = name + "=" + escape(value) +"; path=/" + (expires ? "; expires=" + expires.toUTCString() : "");
	document.cookie = cookie;
}

/************************************************************************
 *	@function	getCookies()
 *	@notes		Returns the cookie jar as an object
 ***********************************************************************/
function getCookies() {
	var cookiejar = {};
    document.cookie.split("; ").each(function(cookie) {
		var crumbs = cookie.split("=");
		cookiejar[crumbs[0]] = unescape(crumbs[1]);
	});
	
	return cookiejar;
}

/************************************************************************
 *	@function	getCookie()
 *	@notes		Generic Get Cookie routine
 ***********************************************************************/
function getCookie(name) {
	var cookiejar = getCookies();
	return cookiejar[name] ? cookiejar[name] : null;
}

/************************************************************************
 *	@function	deleteCookie()
 *	@notes		Generic Delete Cookie routine
 ***********************************************************************/
function deleteCookie(name) {
	document.cookie = name + "=; path=/; expires=" + new Date().toUTCString();
}

/************************************************************************
 *	@function	saveReferrer()
 *	@notes		Saves the current page to a Cookie so that the next
 *				CGI request will treat it as HTTP Referrer.
 *				Some pretty poor emulation that is easily broken.
 ***********************************************************************/
function saveReferrer() {
	if (window.name == 'ActPopup')
		return false;
	
	var idx;
	var url = document.URL;
	if (window.location.hash && (idx = url.lastIndexOf(window.location.hash)))
		url = url.substr(0, idx);

	setCookie("ACTINIC_REFERRER", url);
}


/***********************************************************************
 *	@function	ShowPopUp
 *	@notes		creates pop up window
 * 	@input 		url		-	URL of the page to display
 *				width	-	Width of window
 *				height	-	Height of window
 ***********************************************************************/
function ShowPopUp(url, width, height) {  
	window.open(url, 'ActPopup', 'width=' + width + ',height=' + height + ',scrollbars, resizable');
}

/************************************************************************
 *	@function	DecodeMail()
 *	@notes		Just a rework of the original function by Zoltan Magyar
 *				Now uses Prototype library.
 *				Reduced from 17 lines to 7.
 *	@ZMnotes	decodes the obfuscated mail address in 'contactus' link
 ***********************************************************************/
function DecodeMail() {
	$$("a[name='contactus']").each(function(a) {
		a.href.replace(/\s*\[at\]\s*/, "@");
		while (a.href.match(/\s*\[dot\]\s*/))
			a.href.replace(/\s*\[dot\]\s*/, ".");
	});
};

/************************************************************************
 *	@function	HtmlInclude()
 *	@notes		Just a rework of the original function by Zoltan Magyar
 *				Now uses Prototype library.
 *				Reduced from 68 lines to 7.
 ***********************************************************************/
function HtmlInclude() { 
	$$("a[rel='fragment']").each(function(a) {
		new Ajax.Request(a.href, { method: 'get', onComplete: function(t) {
				a.replace(t.responseText);
			}
		});
	});
}

document.observe("dom:loaded", HtmlInclude);

/************************************************************************
 *	@function	get_cart()
 *	@input		nothing
 *	@output		{ total : string, count : int }
 ***********************************************************************/
function get_cart() {
	var cart = { total: "&pound;0.00", count: 0, value: 0 };
	var cookie = getCookie("CART_CONTENT");
	if (!cookie) return cart;
		
	cookie = cookie.split("\t");

	for (var i = 0; i < cookie.length; i+=2) {
		var key = cookie[i].match(/^CART_(.+)$/)[1].toLowerCase().strip();
		var value = cookie[i+1].strip();
		cart[key] = value;
	}
	
	cart.count = parseInt(cart.count);
	if (cart.total == "0") cart.total = "&pound;0.00";
	
	// extra: get the value of the cart as a float
	if (cart.total.match(/&#\d+;/g)) {
		var value = cart.total;
		value.match(/&#\d+;/g).each(function(ent){
			var char = parseInt(ent.match(/&#(\d+);/)[1]);
			value = value.replace(ent, String.fromCharCode(char));
		});
		cart.value = value.moneyValue();
	}
	
	return cart;
}

/************************************************************************
 *	@function	get_business()
 *	@input		nothing
 *	@output		object: ACTINIC_BUSINESS
 ***********************************************************************/
function get_business() {
	var business = {};
	var cookie = getCookie("ACTINIC_BUSINESS");
	if (!cookie || cookie == "-")
		return false;
		
	cookie.split("\n").each(function(pair){
		pair = pair.split("\t");
		business[pair[0].toLowerCase()] = pair[1];
	});

	return business;
}

/************************************************************************
 *	@function	toggle_attribute_lock()
 *	@input		event, element
 *	@purpose	locks attribute options for attributes which aren't selected
 ***********************************************************************/
function toggle_attribute_lock(event, element) {
	var ele = element ? element : Event.element(event);
	var table = ele.up("fieldset").down("table.attributes");
	table.select("input, select, textarea, button").each(function(i){
		  i.disabled = !ele.checked;
	});
}

/************************************************************************
 *	@function	line_up_elements()
 *	@input		CSS selector, columns
 *	@purpose	lines up the height of horizontal list items to look like a table.
 ***********************************************************************/
function line_up_elements(selector, columns) {
	$$(selector).inGroupsOf(columns).each(function(ele_group) {
		var max_height = ele_group.inject(0, function(acc, e) { return Math.max(acc, (e ? e.getHeight() : 0)); });
		ele_group.each(function(e) {
			if (!e) return;
			var padding = 0;
			if (parseInt(e.getStyle("border-top-width"))) padding += parseInt(e.getStyle("border-top-width"));
			if (parseInt(e.getStyle("border-bottom-width"))) padding += parseInt(e.getStyle("border-bottom-width"));
			if (parseInt(e.getStyle("padding-bottom"))) padding += parseInt(e.getStyle("padding-bottom"));
			if (parseInt(e.getStyle("padding-top"))) padding += parseInt(e.getStyle("padding-top"));
			e.style.height = (max_height - padding)+"px";
		});
	});
}

/************************************************************************
 *	@function	get_page_query()
 *	@purpose	returns an assoc array of the query string for the current page.
 ***********************************************************************/
function get_page_query() {
	var querystr = (window.location.search || window.location.href).match(/^\?(.*)$/);
	if (!querystr)
		return {};
		
	return querystr[1].split("&").inject({}, function(assoc, arg){
		var args = arg.split("=");
		assoc[unescape(args[0])] = unescape(args[1]) || "";
		return assoc;
	});
}