function toggleLayer(whichLayer)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}


function toggleVis(objID,show) {
		if (show) {
			document.getElementById(objID).style.display='block';
		}	else {
			document.getElementById(objID).style.display='none';
		}
}

function toggleFavourites(event) {

	// handles both IE and Mozilla
	var checkbox = document.all ? event.srcElement : event.currentTarget;
	
	var objectId = checkbox.value;

	var favourites;
	
	var cookie = getCookie('ec-favourites')
	if (cookie == undefined) {
		
		favourites = "";
	
	} else {
		
	
		favourites = cookie;
	}
	
	var objectId = checkbox.value;
	
	if (checkbox.checked == true) {
		
		favourites = add(favourites, objectId);
		
	} else {
	
		favourites = remove(favourites, objectId);
		
	}
	
	setCookie('ec-favourites', favourites);
	
	updateView();
	
}

function remove(favourites, objectId) {
	if (favourites.indexOf(objectId + ',') > -1) {
		// object exists, remove the object 
		favourites = favourites.replace (objectId + ',', '');
	}
	
	return favourites;
}

function add(favourites, objectId) {
	if (favourites.indexOf(objectId + ',') == -1) {
		// object doesnt exists, add the object 
		favourites = favourites + objectId + ',';
	}
	
	return favourites;
}

function updateView() {

	var favourites = getCookie('ec-favourites')
	var selectedFavourites = document.getElementById('selected_favourites');
	var matches = new Array();
		var count;
	
	if (favourites == null || favourites.indexOf(',') == -1 ){
		count = 0;		
	} else {
	
		matches = favourites.split(',');
	
		if (matches == undefined) {
			count = 0;
		} else {
			count = matches.length - 1;
		}
	}

	selectedFavourites.innerHTML = count;
}

function selectObjects() {
	
	var term = getCookie('ec-term');	
	
	var termElement = document.getElementById('queryText');
	if (termElement == null) {
		// nothing to select
		return;
	}
		
	var actualTerm = termElement.value;
	
	var favourites;
	
	if (term == null) {
		// no previous search
		setCookie('ec-term', actualTerm);	
		favourites = "";
	} else {
		// previous search, only carry across selected objects if term matches
		if (term == actualTerm) {
			favourites = getCookie('ec-favourites');
		} else {
			favourites = "";
			setCookie('ec-favourites', "");
			setCookie('ec-term', actualTerm);
		}
	}
	
	if (favourites != null && favourites != "") {
	
		var objectIds = favourites.split(',');
		var objects = document.getElementsByName('fav');
		var i = 0;
		for(var i = 0; i < objects.length; i++) {
			
			current = objects[i];
			
			if (favourites.indexOf(current.value+ ',') > -1) {
				current.checked = true;
			} else {
				current.checked = false;
			}
		}
		
		updateView();
	}
}

function checkAllResults(isChecked) {
	var favourites = getCookie('ec-favourites')
    var form = document.getElementById("resultlist_form");
    if (form != null) {
        for (var i=0; i < form.elements.length; i++) {
            var e = form.elements[i];
            if (e.type == 'checkbox') {
            	if (isChecked && e.checked == false) {
				e.click();
               } else if (!isChecked && e.checked == true) {
               	e.click();
               }
            }
        }
    }
}    

function submitFavourites(favouritesPath) {
    
    var favourites = getCookie('ec-favourites');
    
    var objectIds = favourites.split(',');
    
    favouritesPath =  favouritesPath + '?';
    
    var i;
    for (i = 0; i < objectIds.length; i++) {
        var objectId = objectIds[i];
        if (objectId != null && objectId != "") {    
    		favouritesPath = favouritesPath +  'fav=' + objectId + '&';
    	}
    }
    
    // get the querystring - its needed for the back navigation from the SA
	var hasQueryString = document.URL.indexOf('?');
	var additionalQueryString = "";
	if (hasQueryString != -1) {
		// Create variable from ? in the url to the end of the string
		additionalQueryString = document.URL.substring(hasQueryString+1, document.URL.length);
		
		// any previously selected favourites will appear here, they need to be removed
		var regex = /fav=[A-Z]\d+&/g;
		
		var match = regex.exec(additionalQueryString);
		while (match != null) {
			additionalQueryString = additionalQueryString.replace(match, '');
			regex.lastIndex = 0;
			match = regex.exec(additionalQueryString);
		}
		
		additionalQueryString = additionalQueryString.replace('created=true','');
		
		
		favouritesPath = favouritesPath + additionalQueryString;
	}

    window.location = favouritesPath;
}