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;
	var nWidth = 955; //(window.document.body.clientWidth) * .75;
	var nHeight;
	
	if (window.screen.height < 768)
	{
		nHeight = 608; //(window.document.body.clientHeight) * .75;
	}
	else
	{
		nHeight = 624;
	}
	if (window.screen.height <= 768)
	{
		nTop  = document.body.scrollTop;
		//nLeft = window.document.body.clientWidth - 955;
		nLeft = (window.document.body.clientWidth / 2) - (nWidth / 2);
	}
	else
	{
		nTop = document.body.scrollTop + (window.document.body.clientHeight / 10);
		nLeft = window.document.body.clientWidth / 8;
	}

	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.scrollTop + (oWnd.BrowserWindow.document.body.clientHeight / 10);
				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 != '') {
		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 img = wnd.BrowserWindow.document.getElementById(imageID);
	img.src = img.src + '?' + time.valueOf();
	wnd.Close();

}




function openWindow(name, url, nWidth, nHeight)
{
	var scrollTop = $(window).scrollTop();

	if (jQuery.browser.msie && jQuery.browser.version < 8) {
		document.documentElement.style.overflow = "hidden";
	}
	document.body.style.overflow = "hidden";

	var nLeft = (window.document.body.clientWidth / 2) - (nWidth / 2);
	var nTop = ($(window).height() - nHeight) / 2;  //window.document.body.scrollTop;
	if (nTop < 0) {
		nTop = 0;
	}
	
	nTop += 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;
}

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Philip Myers :: http://virtualipod.tripod.com/bookmark.html */
function bookmark(url,title){
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
	window.external.AddFavorite(url,title);
	} else if (navigator.appName == "Netscape") {
		window.sidebar.addPanel(title,url,"");
	} else {
		alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
	}
}

function replyTo(id, commentId, textVal)
{
	document.getElementById(id).value = commentId;
	document.getElementById("formHeader").innerHTML = textVal;
	document.getElementById("formHeader").className = textVal.toLowerCase().replace(/ /g, '_');
}




/***************** House Styles ******************************/



function colorFix() {
    try {
        if (document.getElementById('midContentBg').value == 'G') {
            document.getElementById("tcMidContent").style.backgroundColor = "#F4F0F1";
            document.getElementById("footNav").style.backgroundImage = "url(images/cosmo/bg/foot_nav_bgG.gif)";
        }
    }
    catch (e) {
    }
}

/*
Image/Story Flipper
Edited: 21/05/2008
*/

// Get the flipper links
function getLinks() {
    if (document.getElementById('image_flipper_links')) {
        return document.getElementById('image_flipper_links').getElementsByTagName('a');
    }
    return false;
}

// Get the images
function getImages() {
    if (document.getElementById('image_flipper_images')) {
        return document.getElementById('image_flipper_images').getElementsByTagName('li');
    }
    return false;
}

//  Get the captions
/*function getCaptions() {
if(document.getElementById('image_flipper')) {
var listItem = document.getElementById('image_flipper').getElementsByTagName('li');
var l = 0;
var captions = new Array();
		
for(var k = 0; k < listItem.length; k++) {
if((k % 2) != 0) {
//if(listItem[k].getAttribute('class') == 'caption') {
captions[l] = listItem[k];
l++;
}
}
return captions;
}
return false;
}*/

// Get the stories
function getStories() {
    if (document.getElementById('stories')) {
        return document.getElementById('stories').getElementsByTagName('li');
    }
    return false;
}

// Set position: absolute; left: -9999px; (default?) on all images, stories, and captions and position: static; (class = selected?) to the images, stories, and captions associated with the link,
function swapStories(selectedStory) {
    var image = getImages();
    //var caption = getCaptions();
    var story = getStories();

    for (var j = 0; j < image.length; j++) {
        image[j].style.position = 'absolute';
        //caption[j].style.position = 'absolute';
        story[j].style.position = 'absolute';
    }

    image[selectedStory].style.position = 'static';
    //caption[selectedStory].style.position = 'static';
    story[selectedStory].style.position = 'static';
}

// Add onClick events to the links
function imageFlipper() {
    var link;
    if (link = getLinks()) {
        swapStories(0);
        for (var i = 0; i < link.length; i++) {
            //link[i].setAttribute('onclick', 'swapStories(' + i + '); return false;');
            link[i].setAttribute('href', i);
            link[i].onclick = function() {
                swapStories(this.getAttribute('href'));
                return false;
            }
        }
    }
}

/*
Shopping Flipper
Edited: 15/05/2008
*/

// Get the links
function getShoppingLinks() {
    if (document.getElementById('products')) {
        return document.getElementById('products').getElementsByTagName('a');
    }
    return false;
}

// Get the descriptions
function getShoppingDescriptions() {
    if (document.getElementById('shoppingDescription')) {
        return document.getElementById('shoppingDescription').getElementsByTagName('li');
    }
    return false;
}

// Hide all descriptions and display the one you want
function swapDescriptions(selectedDescription) {
    var shoppingDescription;
    if (shoppingDescription = getShoppingDescriptions()) {
        for (var i = 0; i < shoppingDescription.length; i++) {
            shoppingDescription[i].style.display = 'none';
        }
        shoppingDescription[selectedDescription].style.display = 'block';
    }
}

// Add onMouseOver events and initialise the script
function shoppingFlipper() {
    var link
    if (link = getShoppingLinks()) {
        swapDescriptions(0);
        for (var i = 0; i < link.length; i++) {
            link[i].setAttribute('href', i);
            link[i].onmouseover = function() {
                swapDescriptions(this.getAttribute('href'));
            }
            link[i].onclick = function() {
                return false;
            }
        }
    }
}

/*
Expandy Contracty Thingy
Edited: 15/05/2008
*/

var expandLink;
var closeLink;
var outerPopout;
var popout;
var currentMarginLeft = 480;

function expandPopout() {
    var finalMarginLeft = 0;
    outerPopout.style.zIndex = 9999;

    if (currentMarginLeft > finalMarginLeft) {
        currentMarginLeft -= 16;
        popout.style.marginLeft = currentMarginLeft + 'px';
        setTimeout('expandPopout()', 1);
    }
    else {
        currentMarginLeft = finalMarginLeft;
        popout.style.marginLeft = currentMarginLeft + 'px';
    }
}

function closePopout() {
    var originalMarginLeft = 480;

    if (currentMarginLeft < originalMarginLeft) {
        currentMarginLeft += 32;
        popout.style.marginLeft = currentMarginLeft + 'px';
        setTimeout('closePopout()', 1);
    }
    else {
        currentMarginLeft = originalMarginLeft;
        popout.style.marginLeft = currentMarginLeft + 'px';
        outerPopout.style.zIndex = -1;
    }
}
/*
function init() {
// Get initial HTML blocks
expandLink = document.getElementById('btn_expandBox');
closeLink = document.getElementById('btn_closeBox');
outerPopout = document.getElementById('outerPopout');
popout = document.getElementById('popout');
	
// Set onClick events
if(expandLink && closeLink && outerPopout && popout) {
//if(currentMarginLeft == 480) {
//	expandPopout();
//}
//
//setTimeout("if(currentMarginLeft == 0) closePopout();", 5000);
		
expandLink.onclick = function() {
if(currentMarginLeft == 480) {
expandPopout();
}
return false;
}
		
closeLink.onclick = function() {
if(currentMarginLeft == 0) {
closePopout();
}
return false;
}
}
}*/

function init() {
    // Get initial HTML blocks	
    expandLink = document.getElementById('btn_expandBox');
    closeLink = document.getElementById('btn_closeBox');
    outerPopout = document.getElementById('outerPopout');
    popout = document.getElementById('popout');
    homeObj = document.getElementById('homeObj');

    // Set onClick events
    if (expandLink && closeLink && outerPopout && popout) {
        expandLink.onclick = function() {
            if (currentMarginLeft == 480) {
                expandPopout();
            }
            return false;
        }

        closeLink.onclick = function() {
            if (currentMarginLeft == 0) {
                closePopout();
            }
            return false;
        }
    }

    //open newsletter by default when browser starts
    var newsletterCookie = readCookie('newsletterCookie')
    if (newsletterCookie == null && homeObj) {
        createCookie('newsletterCookie', 'true', null);
        addLoadEvent(
            new function() {
                if (currentMarginLeft == 480) {
                    expandPopout();
                }
                return false;
            }
	    );
        addLoadEvent(
	        new function() {
	            setTimeout('closePopout()', 5000);
	        }
	    );
    }

}

/** cookie **/

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

/*
Load Events
*/

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

addLoadEvent(init);
addLoadEvent(imageFlipper);
addLoadEvent(shoppingFlipper);


/*** CONVERSION ***/

//length conversion function
var inches = new Array
(1, 0.0833, 0.0278, 0.00001578, 2.540, 0.0254, 0.00002539);
var feet = new Array
(12, 1, 0.3333, 0.0001894, 30.48, 0.3048, 0.00003048);
var yards = new Array
(36, 3, 1, 0.0005682, 91.44, 0.9144, 0.0009144);
var miles = new Array
(63360, 5280, 1760, 1, 160934, 1609.3, 1.609);
var centimeters = new Array
(0.3937, 0.0328, 0.0109, 0.000006214, 1, 0.01, 0.001);
var meters = new Array
(39.37, 3.281, 1.0936, 0.0006214, 100, 1, 0.001);
var kilometers = new Array
(39370, 3281, 1094, 0.6214, 100000, 1000, 1);

var factors = new Array(inches, feet, yards, miles,
  centimeters, meters, kilometers);

function Convert(me) {
    iFrom = me.fromUnits.selectedIndex;
    iTo = me.toUnits.selectedIndex;

    factor = factors[iFrom][iTo];

    if (isNaN(me.fromLength.value))
        document.getElementById("lengthResult").innerHTML = "Error";
    else
        document.getElementById("lengthResult").innerHTML = "Equals: " + factor * me.fromLength.value + " " + me.toUnits.options[me.toUnits.selectedIndex].text;
}


// weight conversion function
var grams = new Array
(1, 0.001, 1000, 0.035273, 0.002204);
var kilograms = new Array
(1000, 1, 1000000, 35.2739, 2.20462);
var milligrams = new Array
(0.001, 0.0000001, 1, 0.0000353, 0.0000022);
var ounces = new Array
(28.3495, 0.028349, 28349.523, 1, 0.062499);
var pounds = new Array
(453.592, 0.453592, 453592.37, 16, 1);

var weightFactors = new Array(grams, kilograms, milligrams, ounces,
  pounds);

function WeightConvert(me) {
    iFrom = me.fromWeight.selectedIndex;
    iTo = me.toWeight.selectedIndex;

    weightFactor = weightFactors[iFrom][iTo];

    if (isNaN(me.fromWeightTxt.value))
        document.getElementById("weightResult").innerHTML = "Error";
    else
        document.getElementById("weightResult").innerHTML = "Equals: " + weightFactor * me.fromWeightTxt.value + " " + me.toWeight.options[me.toWeight.selectedIndex].text;
}

//Area conversion function
var squreKilometer = new Array
(1, 1000000, 100, 10763910, 0.3861022);
var squareMeter = new Array
(0.000001, 1, 0.0001, 10.76391, 0.000000039);
var hectare = new Array
(0.01, 10000, 1, 107639.1, 0.003861);
var squareFoot = new Array
(0.00000009, 0.0929, 0.000009, 1, 0.000000036);
var squareMile = new Array
(2.589988, 2589988, 258.9988, 27878400, 1);

var areaFactors = new Array(squreKilometer, squareMeter, hectare, squareFoot, squareMile);

function AreaConvert(me) {
    iFrom = me.fromArea.selectedIndex;
    iTo = me.toArea.selectedIndex;

    areaFactor = areaFactors[iFrom][iTo];

    if (isNaN(me.fromAreaTxt.value))
    //    document.getElementById("toLength").innerHTML = "Error";
        document.getElementById("areaResult").innerHTML = "Error";
    else
        document.getElementById("areaResult").innerHTML = "Equals: " + areaFactor * me.fromAreaTxt.value + " " + me.toArea.options[me.toArea.selectedIndex].text;
}

//temperature conversion function 

function TemperatureConvert(me) {
    var FromVal, ToVal, FromName, ToName, v1;

    v1 = me.what.value;
    v1 = stripBad(v1);
    v1 = parseFloat(v1);
    if (isNaN(v1)) v1 = 0;
    me.what.value = v1;

    FromVal = me.from[me.from.selectedIndex].value;
    ToVal = me.to[me.to.selectedIndex].value;
    FromName = me.from.options[me.from.selectedIndex].text;
    ToName = me.to.options[me.to.selectedIndex].text;

    var ConvertedTemp = get_fact(v1, FromVal, ToVal);
    if (ConvertedTemp == "Below Absolute Zero") {
        document.getElementById("answer").innerHTML = "Your input cannot be below absolute zero.";
    } else {
        document.getElementById("answer").innerHTML = "Equals: " + ConvertedTemp + " " + ToName;
    }
}

function get_fact(ff, from_val, to_val) {
    // first convert to kelvin
    if (from_val == 0) {
        ff = ff + 273.15;
    } else if (from_val == 1) {
        ff = ((ff - 32) / 1.8) + 273.15;
    } else if (from_val == 2) {
        ff = ff / 1.8;
    } else if (from_val == 3) {
        ff = (ff * 1.25) + 273.15;
    }

    if (ff < 0) {
        // Below absolute zero
        return "Below Absolute Zero";
    }

    // now convert kelvin to unit
    if (to_val == 0) {
        ff = ff - 273.15;
    } else if (to_val == 1) {
        ff = (1.8 * (ff - 273.15)) + 32;
    } else if (to_val == 2) {
        ff = ff * 1.8;
    } else if (to_val == 3) {
        ff = (ff - 273.15) / 1.25;
    }

    // round it off
    if (Number.prototype.toFixed) {
        ff = ff.toFixed(7);
        ff = parseFloat(ff);
    }
    else {
        var leftSide = Math.floor(ff);
        var rightSide = ff - leftSide;
        ff = leftSide + Math.round(rightSide * 10000000) / 10000000;
    }

    return ff;
}

//Volume conversion function
var teaSpoon = new Array
(1, 0.333333, 0.020833, 5, 0.005, 0.008799, 0.175976, 0.00109985);
var tableSpoon = new Array
(3, 1, 0.0625, 15, 0.015, 0.0263964, 0.527927, 0.003300);
var cup = new Array
(48, 16, 1, 240, 0.24, 0.422342, 8.446832, 0.052793);
var mililitre = new Array
(0.2, 0.066667, 0.004167, 1, 0.001, 0.001760, 0.035195, 0.000220);
var litre = new Array
(200, 66.666667, 4.166667, 1000, 1, 1.759757, 35.195133, 0.219970);
var pint = new Array
(113.652077, 37.884026, 2.367752, 568.260384, 0.568260, 1, 20, 0.125);
var liquidOunce = new Array
(5.682604, 1.894201, 0.118388, 28.413019, 0.028413, 0.05, 1, 0.00625);
var gallon = new Array
(909.216614, 303.072205, 18.942013, 4546.083072, 4.546083, 8, 160, 1);

var volumeFactors = new Array(teaSpoon, tableSpoon, cup, mililitre, litre, pint, liquidOunce, gallon);

function VolumeConvert(me) {
    iFrom = me.fromVolume.selectedIndex;
    iTo = me.toVolume.selectedIndex;

    volumeFactor = volumeFactors[iFrom][iTo];

    if (isNaN(me.fromVolumeTxt.value))
        document.getElementById("volumeResult").innerHTML = "Error";
    else
        document.getElementById("volumeResult").innerHTML = "Equals: " + volumeFactor * me.fromVolumeTxt.value + " " + me.toVolume.options[me.toVolume.selectedIndex].text;
}


function stripBad(string) {
    for (var i = 0, output = '', valid = "eE-0123456789."; i < string.length; i++)
        if (valid.indexOf(string.charAt(i)) != -1)
        output += string.charAt(i)
    return output;
}

function microsoftKeyPressTemperature() {
    if (window.event.keyCode == 13)
        TemperatureConvert();
}
function microsoftKeyPressLength() {
    if (window.event.keyCode == 13)
        Convert();
}
function microsoftKeyPressWeight() {
    if (window.event.keyCode == 13)
        WeightConvert();
}
function microsoftKeyPressArea() {
    if (window.event.keyCode == 13)
        AreaConvert();
}
function microsoftKeyPressVolume() {
    if (window.event.keyCode == 13)
        VolumeConvert();
}

/**** END CONVERSION ****/