
window.onload = function() 
{	
	Global.adjustContentArea();
	Global.handleExternalLinks();	
	Global.enableIE6BackgroundImageCache(true);
}

var Global =
{
    enableIE6BackgroundImageCache: function(enable) 
    {
	    if(document.execCommand)
        {
            try 
            {
                document.execCommand("BackgroundImageCache", false, enable);
            }
            catch(ex)
            {
            
            }
        }
	},
	
    handleExternalLinks: function()    
    { 
        if (!document.getElementsByTagName) return; 
        var anchors = document.getElementsByTagName('a'); 
        for (var i = 0; i < anchors.length; i++) 
        { 
            var anchor = anchors[i]; 
            if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external') 
                anchor.target = '_blank'; 
        } 
    },
    
    adjustContentArea: function() 
    {
		if(document.getElementById('ContentArea_floated')) 
		{
			if(document.getElementById('ContentArea_floated').offsetHeight > document.getElementById('RightContentArea').offsetHeight) 
			{	
				theDiff = document.getElementById('ContentArea_floated').offsetHeight - document.getElementById('RightContentArea').offsetHeight;
				theNewHeight = theDiff + document.getElementById('RightContentArea').offsetHeight;
				document.getElementById('RightContentArea').style.height = theNewHeight;
			}
		}
    },
    
    toggleBlock: function(element) 
    {
	    if(document.getElementById(element).style.display != 'block') 
	    {
		    document.getElementById(element).style.display = 'block';
	    }
	    else 
	    { 
	        document.getElementById(element).style.display = 'none'; 
	    }
    },
    
    doNothing: function()
    {
        return false;
    }
};

var StartPage =
{
    getRandomPresentationImageHtml: function(images)
    {
        // Preload the images
        var imageBuffer = new Array();
        for (i = 0; i < images.length; i++)
        {        
           imageBuffer[i] = new Image();
           imageBuffer[i].src = images[i].path;
        }
       
        // Select and return html for one of them
        var r = Math.round(Math.random()*(images.length-1));
        var selectedImage = images[r];
        return '<img src="' + selectedImage.path + '" alt="' + selectedImage.alt + '" />';
    }
};


var QuickSearch = 
{
    hasFocus: false,
    
    textBoxFocus: function(textBox)
    {
        QuickSearch.hasFocus = true;
        if(textBox.value == textBox.defaultValue)
            textBox.value = '';

    },
    
    textBoxBlur: function(textBox)
    {
        QuickSearch.hasFocus = false;
        if(textBox.value == '')
            textBox.value = textBox.defaultValue;
    }
};

var FormHelper =
{
    // In addition to setting Page.Form.DefaultButton we use this script
    // It checks if the quick search field has focus and sets the default button to the quck search button if so
    // Otherwise it uses the alternateButtonId (ie some other button on the page which should trigger a postback 
    // when the enter key is pressed)
    onKeyPressSubmit: function(event, quickSearchButtonId, alternateButtonId)
    {
        if(QuickSearch.hasFocus)
        {
            return Microsoft.WebForm_FireDefaultButton(event, quickSearchButtonId);
        }
        else if(alternateButtonId != '')
        {
           return Microsoft.WebForm_FireDefaultButton(event, alternateButtonId); 
        }
    }
};

var PublishingCalendar = 
{
    showPublication: function(type) 
    {
		if (type == 'PDF') {
			var dropdown = document.getElementById("dropdown");
			var url = dropdown.options[dropdown.selectedIndex].value;
		}
		else if (type == 'Excel') {
			var dropdown = document.getElementById("dropdownE");
			var url = dropdown.options[dropdown.selectedIndex].value;
		}
		if (url != 'x') {
			location.href = url;
		}
	}
};

function checkSearchForm(theFormName) {
	
	theURL = "";
	//alert(document.forms[theFormName].OnlyStatToggle.checked);	

	if(theFormName == "GlobalSearchForm" && document.forms[theFormName].SearchString.value == "bistånd") {  theURL = "2.7.htm"; }
	if(theFormName == "HittaSearchForm" && document.forms[theFormName].SearchString.value == "bistånd") {  theURL = "2.7.htm"; }
	if(theFormName == "RegionSearchForm" && document.forms[theFormName].SearchString.value == "huddinge") {  theURL = "2.4.1.htm"; }
	if(theFormName == "RegionSearchForm" && document.forms[theFormName].SearchString.value == "Huddinge") {  theURL = "2.4.1.htm"; }
	
	window.open(theURL,"_self");
	
}


function checkCalendarDrop(theValue) {

	if(theValue == "BE") {
	
		alert("Uppgiften löst. Listan filtreras för att endast visa kommande publiceringar inom området Befolkning.");
	
	}

	
}

function checkNewsArchiveSearch() {
	
	if(document.forms['NewsArchiveSearch'].statSubject.value == "EN" && document.forms['NewsArchiveSearch'].statPeriod.value == "dec2007") {
	
		alert("Uppgiften löst. Listan filtreras för att visa nyheter inom området energi, under december månad 2007.");
	}
	else if(document.forms['NewsArchiveSearch'].statSubject.value == "EN" && document.forms['NewsArchiveSearch'].statPeriod.value == "alla") {
	
		alert("Uppgiften delvis löst. Listan filtreras för att visa nyheter inom området energi, men alla månader visas.");
	}	
	
	
}


function statAreaSwitch(toShow, toHide) {

	document.getElementById(toShow).style.display = 'block';
	document.getElementById(toHide).style.display = 'none';

}

function expandMainContainer() {

	document.getElementById('MainContainer').style.width = '100%';
	document.body.style.background="#FFFFFF url(images/bgMainLeftOnly.jpg) no-repeat fixed top left";

	//document.getElementById('MainContentArea').style.width = '100%';


}

function contractMainContainer() {

	document.getElementById('MainContainer').style.width = '928px';
	document.body.style.background="#FFFFFF url(images/bgMain.jpg) no-repeat fixed top left";
	
	//document.getElementById('MainContentArea').style.width = '100%';

}

var Microsoft = 
{ 
    defaultFired: false,
    
    WebForm_FireDefaultButton : function(e, target) 
    {    
        var srcElement = (e.srcElement || e.target);
        var nonMSDOMBrowser = (window.navigator.appName.toLowerCase().indexOf('explorer') == -1);
        
        if (!Microsoft.defaultFired && e.keyCode == 13 && !(srcElement && (srcElement.tagName.toLowerCase() == "textarea"))) 
        {
            var defaultButton;
            if (nonMSDOMBrowser)
                defaultButton = document.getElementById(target);
            else
                defaultButton = document.all[target];

            if (typeof(defaultButton.click) != "undefined") 
            {
                defaultFired = true;
                defaultButton.click();
                e.cancelBubble = true;
                
                if (e.stopPropagation) 
                    e.stopPropagation();
                
                return false;
            }

            if (typeof(defaultButton.href) != "undefined") 
            {
                defaultFired = true;
                eval(defaultButton.href.substr(11));
                e.cancelBubble = true;
                
                if (e.stopPropagation) 
                    e.stopPropagation();
                    
                return false;
            }

        }
        return true;
    }
};
