// common scripts

// Core scripts v.3.5 - 09.07.02

/*
These scripts are designed to be root relative - they require the site to operating as a domain or port number!

*/


// Window open code

// Better window code

function openWin( winURL, winName, winWidth, winHeight, winX, winY, scrollBars, statusBar, resizable ) {
	winName = window.open( winURL, winName, 'width='  + winWidth + ',height='  + winHeight + ',top=' + winX + ',left=' + winY + ',screenX=' + winX + ',screenY=' + winY + ',location=no,scrollbars=' + scrollBars + ',directories=no,status=' + statusBar + ',menubar=no,personalbar=no,resizable=' + resizable );
	winName.scrollTo( 0, 0 );
}

// Better window code lite

function openWinLite( winURL, winName ) {
	winName = window.open( winURL, winName, 'width=750,height=600,top=0,left=0,screenX=100,screenY=100,location=no,scrollbars=yes,directories=no,status=no,menubar=no,personalbar=no,resizable=no' );
	winName.scrollTo( 0, 0 );
}

function popUpSebnetProduct(winURL, winName)
{
	winName = window.open( winURL, winName, 'width=750,height=600,top=0,left=0,screenX=100,screenY=100,location=no,scrollbars=yes,directories=no,status=no,menubar=no,personalbar=no,resizable=yes' );
	winName.scrollTo( 0, 0 );
}

function printerFriendlyWin(winURL) {
	winName = window.open( winURL, 'printerWin', 'width=570,location=yes,scrollbars=yes,directories=yes,status=yes,menubar=yes,personalbar=no,resizable=yes');
}

// Generally, it's safe to amalgamate all Dreamweaver code down here

function createObject( n, d ) {
	// Object creation code
	var p, i, x;
	if( !d ) {
		d = document;
	}
	if( ( p = n.indexOf( "?" ) ) > 0 && parent.frames.length ) {
		d = parent.frames[n.substring( p + 1 )].document;
		n = n.substring( 0, p );
	}
	if( !( x = d[n] ) && d.all ){
		x = d.all[n];
		for( i = 0; !x && i < d.forms.length; i++ ) {
			x = d.forms[i][n];
		}
		for( i = 0; !x && d.layers && i < d.layers.length; i++ ) {
			x = createObject( n, d.layers[i].document );
		}
	}
	return x;
}

function preloadImages() {
	var d = document;
	if( d.images ){
		if( !d.preloadArr ) {
			d.preloadArr = new Array();
			var i, j = d.preloadArr.length, a = preloadImages.arguments;
			for( i = 0; i < a.length; i++ ) {
				if ( a[i].indexOf( "#" ) != 0 ){
					d.preloadArr[j] = new Image; d.preloadArr[j++].src = a[i];
				}
			}
		}
	}
}

function swapImage() {
	var i, j = 0, x, a = swapImage.arguments;
	document.imageRestoreArr = new Array;
	for( i = 0; i < ( a.length - 2 ); i += 3 )
		if( ( x = createObject( a[i] ) ) != null ) {
			document.imageRestoreArr[j++] = x;
			if( !x.oSrc )
				x.oSrc = x.src;
				x.src = a[i + 2];
				}
}

function swapImageRestore() {
	var i, x, a = document.imageRestoreArr;
	for( i = 0; a && i < a.length && ( x = a[i] ) && x.oSrc; i++ ) {
		x.src = x.oSrc;
	}
}

// image path starts always from root directory
function openWinImg(imgPath)
{
	winURL = '/showImage.aspx?imgUrl=' + imgPath
	winName = window.open( winURL, 'imgWin', 'width=200,height=500,location=no,scrollbars=yes,directories=no,status=no,menubar=no,personalbar=no,resizable=yes');
}

/* onLoad="preloadImages('images/navigation/home_over.gif')" */
/* onMouseOver="swapImage('aboutus','','image_over.file',1)" onfocus="swapImage('aboutus','','image_over.file',1)" onMouseOut="swapImageRestore()" onblur="swapImageRestore()" */
/* Element Show/Hide by ID using CSS */
/* Rather useful little function, generally */
function toggleElementDisplay( elementID, elementAttribute, attributeValue1, attributeValue2 ) {
	if( document.getElementById ) {
		theElementStyle = document.getElementById( elementID ).style;
	} else {
		theElementStyle = document.all[ elementID ].style;
	}

	if( theElementStyle[elementAttribute] == attributeValue2 || !theElementStyle[elementAttribute] ) {
		theElementStyle[elementAttribute] = attributeValue1;
	} else {
		theElementStyle[elementAttribute] = attributeValue2;
	}
}

function popUpTechInfo(techID)
{
	winURL = '/energymap/app_emap_tech_info.asp?techID=' + techID
	winName = window.open( winURL, 'imgWin', 'width=800,height=600,location=no,scrollbars=yes,directories=no,status=no,menubar=no,personalbar=no,resizable=yes');
}

function popUpActionInfo(actionID)
{
	winURL = '/energymap/app_emap_action_desc.asp?actionID=' + actionID
	winName = window.open( winURL, 'imgWin', 'width=800,height=600,location=no,scrollbars=yes,directories=no,status=no,menubar=no,personalbar=no,resizable=yes');
}

function popUpEnergySupplyInfo()
{
	winURL = '/energymap/app_emap_energy_supply_info.asp'
	winName = window.open( winURL, 'imgWin', 'width=800,height=600,location=no,scrollbars=yes,directories=no,status=no,menubar=no,personalbar=no,resizable=yes');
}

function popUpProgressInfo(progressID)
{
	winURL = '/energymap/app_emap_progress_info.asp?progressID=' + progressID
	winName = window.open( winURL, 'imgWin', 'width=800,height=600,location=no,scrollbars=yes,directories=no,status=no,menubar=no,personalbar=no,resizable=yes');
}

function popUpSectorInfo()
{
	winURL = '/energymap/app_emap_sector_info.asp'
	winName = window.open( winURL, 'imgWin', 'width=800,height=600,location=no,scrollbars=yes,directories=no,status=no,menubar=no,personalbar=no,resizable=yes');
}

function changeImage(imgObject, event)
{
	if (event.type == "mouseover")
	{
		imgObject.src = "/images_upload/banners/" + imgObject.id + "_On.jpg";
	}

	else if (event.type == "mouseout")
	{
		imgObject.src = "/images_upload/banners/" + imgObject.id + "_Off.jpg";
	}
}

function showHideDiv(objId, linkId)
{
    var sourceObj = document.getElementById(objId);
    
    if (sourceObj.style.display == "block")
    {
        sourceObj.style.display = "none";
    }
    
    else if (sourceObj.style.display == "none")
    {
        sourceObj.style.display = "block";
    }
    
    if ((linkId != "") && (linkId != null))
    {
        var linkObj = document.getElementById(linkId);
        if (linkObj.innerHTML == "expand [+]")
        {
            linkObj.innerHTML = "shrink [-]";
        }
        
        else if (linkObj.innerHTML == "shrink [-]")
        {
            linkObj.innerHTML = "expand [+]";
        }
    }
    
    if (sourceObj.className == "ItemContent")
    {
        if (IsContainerEmpty("ctl00_ContentPlaceHolderMain_ItemsListContainer", "div") == true)
        {
            var divObject = document.getElementById('ctl00_ContentPlaceHolderMain_ItemsListContainer');
            var defaultMessageObject = "<span id='DefaultNoCats_Message'>There are no categories displayed, please add a category using the menu to the left</span>";
            divObject.innerHtml = defaultMessageObject;
        }
        
        else
        {
            var divObject = document.getElementById('ctl00_ContentPlaceHolderMain_ItemsListContainer');
            var defaultMessageObject = document.getElementById("DefaultNoCats_Message");
            
            if ((divObject != null) && (defaultMessageObject != null))
            {
                divObject.removeChild(defaultMessageObject);
            }
        }
    }
}

function showHideDiv_Img(divId, imgId)
{
    var divObj = document.getElementById(divId);
    var imgObj = document.getElementById(imgId);
    
    if (divObj.style.display == "block")
    {
        divObj.style.display = "none";
        
        if (imgObj != null)
        {
            imgObj.src = "/images/EnergyMap/EnergyWizard/Buttons/Button_ExpandSign_Green.gif";
        }
    }
    
    else if (divObj.style.display == "none")
    {
        divObj.style.display = "block";
        
        if (imgObj != null)
        {
            imgObj.src = "/images/EnergyMap/EnergyWizard/Buttons/Button_ShrinkSign_Green.gif";
        }
    }
}

function TreeView_ExpandAll(parentElementId)
{
    if ((parentElementId != "") && (parentElementId != null))
    {
        var parentObject = document.getElementById(parentElementId);
        var divObjects = parentObject.getElementsByTagName('div');
        
        for (var i=0; i<divObjects.length; i++)
        {
            if ((divObjects[i].className == "TreeView_Sector_Container") || (divObjects[i].className == "TreeView_CategoryList_Container") || (divObjects[i].className == "TreeView_TechnologyList_Container") || (divObjects[i].className == "TreeView_ActionList_Container"))
            {
                if (divObjects[i].className != "TreeView_Sector_Container")
                {
                    divObjects[i].style.display = "block";
                }
                
                var imgObjects = divObjects[i].getElementsByTagName('img');
                
                for (var x=0; x<imgObjects.length; x++)
                {
                    if (imgObjects[x].id.match("TreeView_ExpandOrShrink_Img_") != null)
                    {
                        imgObjects[x].src = "/images/EnergyMap/EnergyWizard/Buttons/Button_ShrinkSign_Green.gif";
                    }
                } 
            }
        }
    }
}

function TreeView_ShrinkAll(parentElementId)
{
    if ((parentElementId != "") && (parentElementId != null))
    {
        var parentObject = document.getElementById(parentElementId);
        var divObjects = parentObject.getElementsByTagName('div');
        
        for (var i=0; i<divObjects.length; i++)
        {
            if ((divObjects[i].className == "TreeView_Sector_Container") || (divObjects[i].className == "TreeView_CategoryList_Container") || (divObjects[i].className == "TreeView_TechnologyList_Container") || (divObjects[i].className == "TreeView_ActionList_Container"))
            {
                if (divObjects[i].className != "TreeView_Sector_Container")
                {
                    divObjects[i].style.display = "none";
                }
                
                var imgObjects = divObjects[i].getElementsByTagName('img');
                
                for (var x=0; x<imgObjects.length; x++)
                {
                    if (imgObjects[x].id.match("TreeView_ExpandOrShrink_Img_") != null)
                    {
                        imgObjects[x].src = "/images/EnergyMap/EnergyWizard/Buttons/Button_ExpandSign_Green.gif";
                    }
                }
            }
        }
    }
}

function IsContainerEmpty(containerId, typeToCheck)
{
    var containerObj = document.getElementById(containerId);
    var containerInnerObjs = document.getElementsByTag(typeToCheck);
    
    var isEmpty = true;
    
    if (containerObj != null)
    {
        if (containerInnerObjs != null)
        {
            for (var i=0; i<containerInnerObjs.length; i++)
            {
                if ((containerInnerObjs[i].style.display == "block") || ((containerInnerObjs[i].style.display == "inline-block")) || ((containerInnerObjs[i].style.display == "inline")))
                {
                    isEmpty = false;
                }
            }
        }
    }
    
    return isEmpty;
}

/* Javascript for Schools Pledge Programme */
function CheckParticipated(event, source)
{
    var schoolsListRow = document.getElementById('ctl00_ctl00_ContentPlaceHolder_Main_ContentPlaceHolder_Main_PledgeForm1_DataCollection_DataRow2');
    
    if (source.id.match("DataCollection_Row1_Input1") != null)
    {
        if (schoolsListRow != null)
        {
            schoolsListRow.style.display = "";
        }
    }
    
    else if (source.id.match("DataCollection_Row1_Input2") != null)
    {
        if (schoolsListRow != null)
        {
            schoolsListRow.style.display = "none";
        }
    }
}

function confirmDelete(ev, text)
{
    var theEvent = ev || window.event;
    var confirmAns = confirm(text || 'Are you sure you want to delete this item?');
    
    if (!confirmAns)
    {
        if (theEvent.cancelBubble != null)
         theEvent.cancelBubble = true;
        if (theEvent.stopPropagation)
         theEvent.stopPropagation();
        if (theEvent.preventDefault)
         theEvent.preventDefault();
        if (window.event)
         theEvent.returnValue = false;
        if (theEvent.cancel != null)
         theEvent.cancel = true;
     }

    return confirmAns;
}