function getScreenResolution()
{
	document.getElementById("ScreenResolution").value = screen.width;
}

function GetRadWindow()
{
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}

// If this is run in a RADWindow then overwrite the self.close method.
var popupWindow;
try{
	popupWindow = GetRadWindow();
	if (popupWindow){}
}
catch(e){
popupWindow = null;
}
if (popupWindow != null)
{
	self.close = function () {
		closeEditWindow();
		}
}

function reloadParentWindowAndClose()
{
	var wnd = GetRadWindow();
	var a = popupWindow.GetWindowManager().GetWindows();
	// apparently 2 with one RadWindow open.
	if (a.length == 2)
	{
		wnd.BrowserWindow.location.reload();
	}
	else
	{
		// Seems to be that the order is <Browser Window> -> <First RadWindow> -> <Second RadWindow>
		// So we subtract 2 from the length, i.e. 3 - 2 = 1 = the second array item		
		    wnd.Argument.Reload();		    
		//}
	}
	wnd.Close();
}

function restoreContentEditWindow(contentId, reloadParent)
{
	var wnd = GetRadWindow();
	if (reloadParent)
	{
		wnd.Argument.Reload();
		wnd.Close();
	}
	else
	{
		wnd.Reload();
	}
}

function closeEditWindow()
{
	var wnd = GetRadWindow();
	wnd.Close();
}

function openEditWindow(name, url)
{
	var nTop;
	var nLeft;
	if (window.screen.width <= 1024)
	{
		nTop  = 0;
		nLeft = window.document.body.clientWidth - 955;
	}
	else
	{
		nTop = (window.document.body.clientHeight) / 8;
		nLeft = window.document.body.clientWidth / 8;
	}
	var nWidth = 955; //(window.document.body.clientWidth) * .75;
	var nHeight = 618; //(window.document.body.clientHeight) * .75;

	var url2 = '';
	if (url.indexOf('?') > 0)
	{
	url2 = url + '&res=' + window.screen.width
	}
	else
	{
	url2 = url + '?res=' + window.screen.width
	}
	var oManager = null;
	var wnd;
	try{
		oManager = GetRadWindowManager();
		wnd = oManager.Open(url2, name);
		document.getElementById("AdminMenu_MenuPanel").disabled = true;
		document.documentElement.style.overflow = "hidden";
		document.body.style.overflow = "hidden";
	}catch(e){}
	
	try{
		//if (wnd == 'undefined'){
			var oWnd = GetRadWindow();
			oManager = oWnd.GetWindowManager();
			if (window.screen.width <= 1024)
			{
				nLeft = oWnd.BrowserWindow.document.body.clientWidth - 955;
			}
			else
			{
				nTop = oWnd.BrowserWindow.document.body.clientHeight / 8;
				nLeft = oWnd.BrowserWindow.document.body.clientWidth / 8;
			}
			url2 = "CMSAdmin/" + url2;
			wnd = oManager.Open(url2, name);
			wnd.Argument = oWnd;
		//}
	}catch(e){}

	wnd.SetSize(nWidth, nHeight);
	wnd.MoveTo(nLeft, nTop);
	window.scroll(0,0);
	
}

function openSendToFriendWindow(name, url, nWidth, nHeight, params)
{
    var paramURL = location.href;
    
    paramURL = paramURL.replace(/&/g,"|");/** hack.. .NET trims out &amp; symbol **/
    params = params != '' ? params + '&' : '';
    openWindow(name, url + '?' + params + 'EmailLinks=' + paramURL, nWidth, nHeight);
}

function RadWindow_ClientClose(wnd)
{
	var oManager = GetRadWindowManager();
	// apparently 1 because the window is still closing
	if (oManager.GetWindows().length == 1)
	{
		if (document.getElementById("AdminMenu_MenuPanel") != null)
		{
			document.getElementById("AdminMenu_MenuPanel").disabled = false;
		}
		document.documentElement.style.overflow = "";
		document.body.style.overflow = "";
	}
	else
	{
		document.documentElement.style.overflow = "hidden";
		document.body.style.overflow = "hidden";
	}
}

function divDisplay(divID)
{
	if (document.getElementById(divID).style.display == 'block')	{
		document.getElementById(divID).style.display = "none";
	}
	else	{
		document.getElementById(divID).style.display = "block";
	}
}

function divDisplaySwitch(divID, YesNoID)
{
	if (document.getElementById(YesNoID).value == 'Yes' || document.getElementById(YesNoID).value == 'yes')	{
		document.getElementById(divID).style.display = "block";
	}
	else	{
		document.getElementById(divID).style.display = "none";
	}
}

function switchPage(currentDiv, requestedDiv)
{
	divDisplay(currentDiv);
	divDisplay(requestedDiv);
}

function clearSearch(me, defaultValue){
	if (me.value == defaultValue) {
		me.value = '';
	}
}

function restoreSearch(me, defaultValue){
	if (me.value == '') {
		me.value = defaultValue;
	}
}

function searchSubmit(me, action, additionalParams){
	if (me.value != 'Enter Keyword' && me.value != '') {
		window.location.href = action + '?q=' + escape(me.value) + additionalParams;
	}
}

function keypressSearchSubmit(me, e, action, additionalParams){
	try{
		if (e.keyCode == 13){
			searchSubmit(me, action, additionalParams);
			return false;
		}
	}catch(e){}
}

function openNormalWindow(name, url, options)
{
    window.open(url,name,options)
}
function refreshImage(imageID)
{
	var wnd = GetRadWindow();
	var today=new Date();
	var time=today.getSeconds();
	var img = wnd.Argument.GetContentFrame().contentWindow.document.getElementById(imageID);
	var tmp = new Date();
	tmp = "?"+tmp.getTime()
	img.src = img.src + tmp;	          
	wnd.Close();	

}




function openWindow(name, url, nWidth, nHeight)
{

	document.documentElement.style.overflow = "hidden";
	document.body.style.overflow = "hidden";
		
    var nLeft = (window.document.body.clientWidth / 2) - (nWidth / 2);
    var nTop = (window.document.body.clientHeight - nHeight) / 2;  //window.document.body.scrollTop;
    if (nTop < 0)
    {
		nTop = 0;
    }
    nTop += window.document.body.scrollTop;
    
    var url2 = '';
    
    if (url.indexOf('?') > 0)
    {
        url2 = url + '&res=' + window.screen.width
    }
    else
    {
        url2 = url + '?res=' + window.screen.width
    }

    var oManager = null;
    var wnd = null;
    
    try
    {
    	//alert ( "url: "+url2);
        oManager = GetRadWindowManager();
        wnd = oManager.Open(url2, name);
     }
     catch(e){}

    try
    {
    	//alert ( "url: "+url2);
       // var oWnd = GetRadWindow();
        oManager = GetRadWindow().GetWindowManager();   
        wnd = oManager.Open(url2, name);
    }
    catch(e){}
    
    wnd.SetSize(nWidth, nHeight);
    
    //wnd.MoveTo(nLeft, nTop); 

    
    if (!wnd) return;     
}

function initialiseRightNav() {
	if(document.getElementById('module_nav')) {
		var navItem = new Array();
		navItem = document.getElementById('module_nav').getElementsByTagName('li');
		var tabs = new Array('thirty_days_live_life', 'newsletter_sign_up'); // Valid names of tabs / DIVs we're going to use
		var anchor = new Array();
		
		// Initially display the first DIV only
		swapDivs(tabs, tabs[0]);
		
		
		// Confirm that the class of the LI matches one of the valid values defined above
		function checkValue(liClass) {
			for(var j = 0; j < tabs.length; j++) {
				if(liClass == tabs[j]) {
					return true;
				}
			}
			return false;
		}
		
		// Loop through each LI, validate its class. If valid assign an onClick to it.
		for(var i = 0; i < navItem.length; i++) {
			var validValue = false;
			validValue = checkValue(navItem[i].className);
			if(validValue == true) {
				anchor[i] = navItem[i].getElementsByTagName('a');
				anchor[i][0].onclick = function() {
					swapNavTabs(this.parentNode.parentNode, this.parentNode.className);
					swapDivs(tabs, this.parentNode.className);
					return false;
				}
			}
		}
	}
}

// Changes the class of the nav to change the appearance
// Arguments:
//	nav		The element you wish to assign the class to
//	activeTab	The class you wish to assign to nav
function swapNavTabs(nav, activeTab) {
	nav.className = activeTab;
}

// Makes selected DIV appear and the rest disappeared
// Arguments:
//	divs		IDs for the DIVs we're playing with
//	selected	ID for the DIV we want to show
function swapDivs(divs, selected) {
	var div = new Array();
	for(var i = 0; i < divs.length; i++) {
		div[i] = document.getElementById(divs[i]);
		div[i].style.display = 'none';
	}
	var visibleDiv = document.getElementById(selected);
	visibleDiv.style.display = 'block';
}

// Add Load Event Function
// borrowed from Simon Willison: http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

addLoadEvent(initialiseRightNav);