var menus = new Array('m_solutions','m_about','m_latestnews','m_journal','m_pressroom','m_globalwarming');
var menuTimer = '';

function show(menu) {
	window.clearTimeout(menuTimer);
	document.getElementById(menu).style.visibility = 'visible';
	menuParent = menu.replace('m_','');
	if (document.getElementById(menuParent).firstChild.className.indexOf('prm') < 0) {
		document.getElementById(menuParent).firstChild.className = 'active';
	}
	for (i=0;i<menus.length;i++) {
		if (menus[i] != menu) {
			document.getElementById(menus[i]).style.visibility = 'hidden';
		}
	}
}

function hide(menu) {
	menuParent = menu.replace('m_','');
	menuTimer = window.setTimeout("document.getElementById('"+menu+"').style.visibility='hidden'",250);
	if (document.getElementById(menuParent).firstChild.className.indexOf('prm') < 0) {
		document.getElementById(menuParent).firstChild.className = '';
	}
}

function hold(menu) {
	menuParent = menu.replace('m_','');
	if (document.getElementById(menuParent).firstChild.className.indexOf('prm') < 0) {
		document.getElementById(menuParent).firstChild.className = 'active';
	}
	window.clearTimeout(menuTimer);
}


Array.prototype.parentId = function(element, index) {
	for (i=0;i<this.length;i++) {
		if (this[i][index] == element) {
			return i;
			break;
		}
	}
	return false;
}

Array.prototype.contains = function(element, index) {
	for (i=0;i<this.length;i++) {
		if (this[i][index] == element) {
			return this[i][index];
			break;
		}
	}
	return false;
}

window.onload = function() { 
	if (document.getElementById('slideshow')) {
		initslideshow();
	}
}

////// SLIDESHOW //////

// USER-DEFINED VARIABLES


// This sets the dimensions of the slideshow - they must be the same as the dimensions you set for the slideshow div

var slidewidth = 528;
var slideheight = 183;

// This sets the speed of the fade-out - the higher the number, the slower it'll go

var fadespeed = 25;

// This defines for how many seconds an image displays before fading out.

var imagetime = 5;

// This defines the pre-loading text - use |loaded| to display the number of images loaded and |total| for the total number of images.

var preloadtext = 'Loading |loaded| of |total|';




// DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU'RE DOING!

// Browser sniffing
var moz = (document.getElementById && !document.all) ? 1:0;
var safari = (navigator.userAgent.indexOf('AppleWebKit') >= 0) ? 1:0;
var ie = (document.getElementById && document.all) ? 1:0;

// Returns alpha value of a given object
function getAlpha(obj) {
if (moz) {
	currAlpha = document.getElementById(obj).style.opacity * 100;
	}
else if (safari) {
	currAlpha = document.getElementById(obj).style.opacity * 100;
	}
else if (ie) {
//	ca = document.getElementById(obj).filter;
//	ca = ca.replace('alpha (Opacity=','');
//	currAlpha = parseInt(ca);
	currAlpha = document.getElementById(obj).filters.alpha.opacity;
	}
return currAlpha;
}

// Advances the slides
function switchImgs(index) {
slidecount = slides.length;
if (index == (slidecount -1)) {
	nextIndex = 0;
	}
else {
	nextIndex = index + 1;
	}
document.getElementById('slide').style.backgroundImage = 'url('+ slides[(index)] +')';
setTimeout("document.getElementById('slideshow').style.backgroundImage = 'url('+ slides[(nextIndex)] +')'",20);
setTimeout("fade('slide', nextIndex)",20);
setAlpha('slide',100);
}

// Sets the alpha of a given object
function setAlpha(obj, value) {
if (moz) {
	document.getElementById(obj).style.opacity = (value / 100);
	}
else if (safari) {
	document.getElementById(obj).style.opacity = (value / 100);
	}
else if (ie) {
	document.getElementById(obj).filters.alpha.opacity = value;
	}
}

imagetime = imagetime * 1000; // Converts user-set imagetime to miliseconds.
var fadeTimer = ''; // Assigned to the timeout for fading

// Fades slides 
function fade(obj, currIndex) {
theSlide = obj;
theIndex = currIndex
currAlpha = getAlpha('slide');
if (currAlpha > 0) {
	setAlpha('slide', (currAlpha - 5));
	fadeTimer = window.setTimeout("fade(theSlide, theIndex)",fadespeed);
	}
else {
	window.clearTimeout(fadeTimer);
	window.setTimeout("switchImgs(theIndex)",imagetime);
	}
}

function slideshow() {
	window.setTimeout("switchImgs(0)",imagetime);
	}


// Preloading block

var preload = new Array();
preload['loaded'] = new Array();
preload['loading'] = new Array();
var currCount = 0, slidetimer = '';

function initslideshow() {
	if (ie) {
		document.getElementById('slideshow').innerHTML = '<div id="slide" style="filter: alpha(Opacity: 100)"></div>';
//		alert(document.getElementById('slide').style.filter);
//		document.getElementById('slide').filter = 'alpha (Opacity=100)';
		document.getElementById('slide').filters.alpha.opacity = 100;
		}
	else {
		document.getElementById('slideshow').innerHTML = '<div id="slide"></div>';
		document.getElementById('slide').style.opacity = 1;
		}
	document.getElementById('slide').style.width = document.getElementById('slideshow').offsetWidth +'px';
	document.getElementById('slide').style.height = document.getElementById('slideshow').offsetHeight +'px';
	document.getElementById('slideshow').style.background = 'url('+ slides[0] +')';
	for (i = 0; i < slides.length; i++) { 
		preload['loading'][i] = new Image();
		preload['loading'][i].src = slides[i];
		}
	for (i=0;i<preload['loading'].length;i++) {
		preload['loaded'][i] = false;
		}
	checkLoad();
	}

var loaderdiv = '<div id="loader" style="display: inline; width: 150px; font-family: Tahoma, Arial, Sans-serif; font-size: 10px; color: #fff; padding: 1px">|loadingtext|</div>';

function checkLoad() {
pt = preloadtext.replace('|loaded|', currCount);
pt = pt.replace('|total|', slides.length);
ld = loaderdiv.replace('|loadingtext|', pt);
document.getElementById('slide').innerHTML = ld;
if (currCount == preload['loading'].length) {
	document.getElementById('loader').style.display = 'none';
	slideshow();
	return
	}
for (i=0;i<preload['loading'].length;i++) {
	if (preload['loaded'][i] == false && preload['loading'][i].complete) {
		preload['loaded'][i] = true;
		currCount++;
		}
	}
slidetimer = setTimeout("checkLoad()",10);
}


////////////////////

function linkCat(obj) {
	cat = obj.value;
	window.location.href = '#'+cat;
}

function newsArchive(obj) {
	if (obj.value != '#') {
		year = obj.value;
		window.location.href = '?s=news-archive&y='+year;
	}
}

function pubCat(obj) {
	cat = obj.value;
	if (cat == '#') {
		window.location.href = '?s=publications';
	}
	else {
		window.location.href = '?s=publications&aid='+cat;
	}
}

function expandNode(obj) {
	nodeArr = obj.href.split('&aid=');
	nodeId = nodeArr[(nodeArr.length -1)];
//	alert(nodeId);
	nodeObj = document.getElementById('a'+nodeId);
	if (nodeObj.style.display == 'none') {
		nodeObj.style.display = 'block';
		obj.parentNode.style.border = 'none';
		obj.parentNode.style.paddingBottom = 3+'px';
		obj.className = 'active';
	}
	else {
		nodeObj.style.display = 'none';
		obj.className = '';
		obj.parentNode.style.borderBottom = '1px dashed #AFBCC1';
		obj.parentNode.style.paddingBottom = 5+'px';
	}
	obj.blur();
	return false;
}

function blankField(field) {
	if (field.value == field.alt) {
		field.alt = field.value;
		field.value = '';
	}
}

function restoreField(field) {
	if (field.value == '') {
		field.value = field.alt;
	}
}


///////////////////

function validateForm(formObj) { // Hello, I'll be your form validating script today.
var formElements = formObj.elements;

// Now I'll set a bunch of boolean variables that will help us validate your form.
var canSubmit = true; // This will ultimately decide if the form gets submitted or not, and is dependant on the other vars to do so.
var emailValid = true; // This checks if the user's email address is valid (string@string.string).
var fieldsComplete = true; // This checks that all the required text fields are not blank.
var anyChecked = true; // This makes sure all required checkbox/radio box arrays are valid.
var captchaValid = true; // Captcha

// These arrays are used to validate the check/radio boxes.
var rcBoxes = new Array();
rcBoxes[0] = new Array();
rcBoxes[1] = new Array();
var rcArrays = new Array();
rcArrays[0] = new Array;
rcArrays[1] = new Array;

// Getting down to business now, we'll loop through all the elements of your form
for (i=0;i<formElements.length;i++) {
// We'll start with the text fields - making sure all the required ones are not blank.
	if (formElements[i].type == 'text' ||  formElements[i].type == 'textarea') {
		if (formElements[i].name.substring((formElements[i].name.length -4),(formElements[i].name.length)) == '_req' && (formElements[i].value == '' || formElements[i].value == 'undefined')) {
		formElements[i].className += ' highlight'; // Highlights a required field left blank.
		fieldsComplete = false;
			}
		if (formElements[i].name == 'confirm') {
			if (SHA1(formElements[i].value.toLowerCase()) != document.newsletterform.match.value) {
				formElements[i].className += ' highlight';
				captchaValid = false;
			}
		}
// Now we'll check if the user has provided you with a valid email address...
		if (formElements[i].name == 'email_req') {
			if (formElements[i].value.search('.+@.+\\.[A-Za-z]+') < 0) {
				formElements[i].className += ' highlight'; // Highlights the email field if blank or invalid.
				emailValid = false;
				}
			else { // Removes the highlight if email is valid.
				formElements[i].className = formElements[i].className.replace(' highlight','');
				}
			}
		else if (formElements[i].name.substring((formElements[i].name.length -4),(formElements[i].name.length)) == '_req' && formElements[i].value != '') {
			// Removes the highlight if fields are properly filled.
			formElements[i].className = formElements[i].className.replace(' highlight','');
			}
		}

// Finally, we go through the checkboxes and radio boxes, making sure each array of required check/radio boxes has at least one element checked.
	else if (formElements[i].type == 'radio' || formElements[i].type == 'checkbox') {
		if (formElements[i].name.substring((formElements[i].name.length -4),(formElements[i].name.length)) == '_req') {
			// Adds all checkboxes to an array which we'll be looping through later.
			rcBoxes[0].push(formElements[i].name);
			rcBoxes[1].push(formElements[i].checked);
			}
		}
	}

// After loading all the check/radio boxes into a separate array, we go through them again.
if (rcBoxes[0].length > 0) {
	for (i=0;i<rcBoxes[0].length;i++) {
		// The first thing we'll do is separate them into little arrays where they have a common name attribute.
		targetCheck = formObj[rcBoxes[0][i]];
		i += targetCheck.length;
		// Then we'll loop through that array.
		for (j=0;j<targetCheck.length;j++) {
			if (targetCheck[j].checked) {
				// If we find a checked box within a required array, add the element's name to rcArrays[0] and add true to rcArrays[1].
				rcArrays[1].push(true);
				rcArrays[0].push(targetCheck[j].name);
				// That's all we need, so we can break out of the loop.
				break;
				}
			else if (j == (targetCheck.length -1)) {
				// If nothing is checked, we only add the name of the element to rcArrays[0].
				rcArrays[0].push(targetCheck[j].name);
				}
			}
		}
	// So, with all the info we gathered up there, we'll check the length of rcArrays[0] against rcArrays[1].
	// If all the required check/radio arrays have at least one box checked, they're valid and the lengths should match.
	if (rcArrays[0].length != rcArrays[1].length) {
		// If the lengths of rcArrays[0] and rcArrays[1] don't match, it means something that should've been checked was left unchecked.
		anyChecked = false;
		}
	}

// Remember our boolean vars? This is where we check them and give the user an appropriate response.
if (!fieldsComplete || !anyChecked) {
	// This message will display if a required textfield was left blank or a checkbox was left un-checked.
	alert('Please fill out all required fields before submitting this form!');
	canSubmit = false; // Don't let the form submit.
	}
else if (!emailValid) {
	// This message will display if the user didn't enter a valid email address.
	alert('Please enter a valid email address in the highlighted field!');
	canSubmit = false; // Don't let the form submit.
	}
else if (!captchaValid) {
	alert('Please enter the exact word with no spaces.');
	canSubmit = false;
}
if (fieldsComplete && emailValid && anyChecked && captchaValid) {
	// If everything has been properly filled and a valid email address has been entered, the form can submit!
	canSubmit = true;
	}
return canSubmit;
}

function rss() {
	if (ie) {
		sw = document.body.clientWidth;
		sh = document.body.clientHeight;
	}
	else {
		sw = window.innerWidth;
		sh = window.innerHeight;
	}
	wx = Math.round((sw/2) - (270 / 2));
	wy = Math.round(((sh/2) -100) - (200 / 2));
	document.getElementById('rss_shade').style.display = 'block';
	document.getElementById('rss_menu').style.left = wx +'px';
	document.getElementById('rss_menu').style.top = wy +'px';
	document.getElementById('rss_menu').style.display = 'block';
	return false;
}

function hiderss() {
	document.getElementById('rss_shade').style.display = 'none';
	document.getElementById('rss_menu').style.display = 'none';
	document.getElementById('privacy').style.display = 'none';
	return false;
}

window.onresize = function() {
	if (ie) {
		sw = document.body.clientWidth;
		sh = document.body.clientHeight;
	}
	else {
		sw = window.innerWidth;
		sh = window.innerHeight;
	}
	wx = Math.round((sw/2) - (270 / 2));
	wy = Math.round(((sh/2) -100) - (200/2));
	wo = Math.round(((sh/2) -100) - (250/2));
	document.getElementById('rss_menu').style.left = wx +'px';
	document.getElementById('rss_menu').style.top = wy +'px';
	document.getElementById('privacy').style.left = wx +'px';
	document.getElementById('privacy').style.top = wo +'px';
}

function privacy() {
	if (ie) {
		sw = document.body.clientWidth;
		sh = document.body.clientHeight;
	}
	else {
		sw = window.innerWidth;
		sh = window.innerHeight;
	}
	wx = Math.round((sw/2) - (270 / 2));
	wy = Math.round(((sh/2) -100) - (250 / 2));
	document.getElementById('rss_shade').style.display = 'block';
	document.getElementById('privacy').style.left = wx +'px';
	document.getElementById('privacy').style.top = wy +'px';
	document.getElementById('privacy').style.display = 'block';
	return false;
}

function filterSearch() {
	if (document.getElementById('advancedsearch').style.display == 'block') {
		document.getElementById('advancedsearch').style.display = 'none'
		document.getElementById('advancedsearchlink').innerHTML = 'More<br />Options';
		document.filter.f.value = 'no';
		return false;
//		break;
	}
	else {
		document.getElementById('advancedsearch').style.display = 'block'
		document.getElementById('advancedsearchlink').innerHTML = 'Hide<br />Options';
		document.filter.f.value = 'yes';
		return false;
//		break
	}
}

function publicationCat(obj) {
	if (document.getElementById('cat'+obj).style.display == 'none') {
		document.getElementById('cat'+obj).style.display = 'block';
		document.getElementById('lnk'+obj).className = 'anchor cat active';
		return false;
	}
	
	else {
		document.getElementById('cat'+obj).style.display = 'none';
		document.getElementById('lnk'+obj).className = 'anchor cat';
		return false;
	}
}

/**
*
*  Secure Hash Algorithm (SHA1)
*  http://www.webtoolkit.info/
*
**/

function SHA1 (msg) {

    function rotate_left(n,s) {
        var t4 = ( n<<s ) | (n>>>(32-s));
        return t4;
    };

    function lsb_hex(val) {
        var str="";
        var i;
        var vh;
        var vl;

        for( i=0; i<=6; i+=2 ) {
            vh = (val>>>(i*4+4))&0x0f;
            vl = (val>>>(i*4))&0x0f;
            str += vh.toString(16) + vl.toString(16);
        }
        return str;
    };

    function cvt_hex(val) {
        var str="";
        var i;
        var v;

        for( i=7; i>=0; i-- ) {
            v = (val>>>(i*4))&0x0f;
            str += v.toString(16);
        }
        return str;
    };


    function Utf8Encode(string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    };

    var blockstart;
    var i, j;
    var W = new Array(80);
    var H0 = 0x67452301;
    var H1 = 0xEFCDAB89;
    var H2 = 0x98BADCFE;
    var H3 = 0x10325476;
    var H4 = 0xC3D2E1F0;
    var A, B, C, D, E;
    var temp;

    msg = Utf8Encode(msg);

    var msg_len = msg.length;

    var word_array = new Array();
    for( i=0; i<msg_len-3; i+=4 ) {
        j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 |
        msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
        word_array.push( j );
    }

    switch( msg_len % 4 ) {
        case 0:
            i = 0x080000000;
        break;
        case 1:
            i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000;
        break;

        case 2:
            i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000;
        break;

        case 3:
            i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8    | 0x80;
        break;
    }

    word_array.push( i );

    while( (word_array.length % 16) != 14 ) word_array.push( 0 );

    word_array.push( msg_len>>>29 );
    word_array.push( (msg_len<<3)&0x0ffffffff );


    for ( blockstart=0; blockstart<word_array.length; blockstart+=16 ) {

        for( i=0; i<16; i++ ) W[i] = word_array[blockstart+i];
        for( i=16; i<=79; i++ ) W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);

        A = H0;
        B = H1;
        C = H2;
        D = H3;
        E = H4;

        for( i= 0; i<=19; i++ ) {
            temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B,30);
            B = A;
            A = temp;
        }

        for( i=20; i<=39; i++ ) {
            temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B,30);
            B = A;
            A = temp;
        }

        for( i=40; i<=59; i++ ) {
            temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B,30);
            B = A;
            A = temp;
        }

        for( i=60; i<=79; i++ ) {
            temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B,30);
            B = A;
            A = temp;
        }

        H0 = (H0 + A) & 0x0ffffffff;
        H1 = (H1 + B) & 0x0ffffffff;
        H2 = (H2 + C) & 0x0ffffffff;
        H3 = (H3 + D) & 0x0ffffffff;
        H4 = (H4 + E) & 0x0ffffffff;

    }

    var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);

    return temp.toLowerCase();

}