// COOKIES
function setCookie(name, value, expirationDayCount, path, domain, secure ) {
	var expirationDate = new Date();
	expirationDate.setDate(expirationDate.getDate() + expirationDayCount);
	
	document.cookie = name + "=" +escape( value ) +
	( ( expirationDayCount ) ? ";expires=" + expirationDate.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
function getCookie(cookieName) {
	if (document.cookie.length>0) {
		var startIndex = document.cookie.indexOf(cookieName + "=");
		if (startIndex!=-1) { 
	    	startIndex = startIndex + cookieName.length + 1; 
	    	var endIndex = document.cookie.indexOf(";", startIndex);
	    	if (endIndex==-1) {
	    		endIndex = document.cookie.length;
	    	}	
	    	return unescape(document.cookie.substring(startIndex, endIndex));
		} 
	}
	return "";
}

// SECURITY
function isElementVisible(elementId) {
	var element = document.getElementById(elementId);
	return (element.style.display == "inline");
}
function showOnlyForCreator(elementId, createdByMemberUid, sessionMemberUid, display) {
	if (createdByMemberUid == sessionMemberUid) {
		if (display == undefined) {
			setElementDisplay(elementId, "inline");
		} else {
			setElementDisplay(elementId, display);
		}
	}
}
function showOnlyForAdministrator(elementId, subscriptionRoleId, display) {
	if (subscriptionRoleId >= 5) {
		if (display == undefined) {
			setElementDisplay(elementId, "inline");
		} else {
			setElementDisplay(elementId, display);
		}
	}
}
function showOnlyForModerator(elementId, subscriptionRoleId, display) {
	if (subscriptionRoleId >= 4) {
		if (display == undefined) {
			setElementDisplay(elementId, "inline");
		} else {
			setElementDisplay(elementId, display);
		}
	}
}
function showOnlyIfNew(elementId, createdByMemberUid, creationDate, sessionMemberUid, subscriptionLastVisitDate) {
	if (creationDate > subscriptionLastVisitDate && sessionMemberUid != createdByMemberUid) {
		setElementDisplay(elementId, "inline");
	}
}

// NAVIGATION
function openPage(url, target) {
	var pageReference = window.open(url, target);
	if (!pageReference) { 
		alert('Please deactivate you anti-popup system in order to open this page in a new window');
		document.location = url;
	} else {
		pageReference.focus();
	}
}
function openPopUp(url, height, width) {
	var options = 'resizable,status=no,top=0,left=0,height=' + height + ',width=' + width;
	var popUpReference = window.open(url, "popUp", options);
	if (!popUpReference) { 
		alert('Please deactivate you anti-popup system in order to open this page in a new window');
		document.location = url;
	} else {
		popUpReference.focus();
	}
}
function goToPage(url) {
	location.href = url;
}	
function reloadPage() { // called from Flex applications
	window.location.reload();
}

// WINDOWS
function centerWindow(name) { // called from Flex applications
	var window = ColdFusion.Window.getWindowObject(name);
	window.center();
}
function hideWindow(name) { // called from Flex applications
	ColdFusion.Window.hide(name);
}
function resizeWindow(name, height, width) { // called from Flex applications
	var window = ColdFusion.Window.getWindowObject(name);
	window.setContentSize(width, height);
	window.center();
}
function showWindow(name, optionalUrl) { // called from Flex applications
	if (optionalUrl != undefined && optionalUrl != "") {
		ColdFusion.navigate(optionalUrl, name);
	}
	ColdFusion.Window.show(name);
}


// SHARE
function shareOnFacebook(title, url) {
	var popUpReference = window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title),'facebookSharePopUp','toolbar=0,status=0,width=626,height=436'); 
	if (popUpReference) {
		popUpReference.focus();
	}
	return false;
}

function shareOnMyspace(title, url) {
	var popUpReference = window.open('http://www.myspace.com/index.cfm?fuseaction=postto&t=' + encodeURIComponent(title) + '&c=&u=' + encodeURIComponent(url) + '&l=1','myspaceShareWindow'); 
	if (popUpReference) {
		popUpReference.focus();
	}
	return false;
}
				
// UTIL
function setElementDisplay(elementId, value) {
	var element = document.getElementById(elementId);
	element.style.display = value;
}
function getQueryVariable(variable) { 
	var query = window.location.search.substring(1); 
	var vars = query.split("&"); 
	for (var i=0;i<vars.length;i++) { 
		var pair = vars[i].split("="); 
		if (pair[0] == variable) { 
			return pair[1]; 
		} 
	} 
}