function setDataSelection_Limit(optObject, optText, patternStr, limit){
	if (optObject.checked){
		var theSelection = getCookie("selection");
		var templist = theSelection.split(",");
		var count = 0;
		
		//If the limit check pattern string contains a wild card
		if (patternStr.indexOf("*") != -1){
			var patternList = patternStr.split("*");
			for(var i=0; i<templist.length; i++){
				var isGroup = true;
				for(var j=0; j<patternList.length; j++){
					if(templist[i].indexOf(patternList[j]) == -1) {
						isGroup = false;
						break;
					}
				}
				if (isGroup) count += 1;
			}					
		}else{
			for(var i=0; i<templist.length; i++){
				if(templist[i].indexOf(patternStr) == 0) count += 1;
			}
		}
		
		if (count < limit)
			setDataCookie(optObject.value, optText);
		else{
			var alertStr = "Up-to " + limit + " data layers from this data category are allowed for each\n" + 
						"map (see below for details). You have already reached the limit.\n" +
						"This data layer will not be added to your selection."
			alert(alertStr);
			optObject.checked = false;
		}		
	}else
		deleteDataCookie(optObject.value);	
}

function setDataSelection(optObject,optText) {

	if (optObject.checked)
		setDataCookie(optObject.value, optText);
	else
		deleteDataCookie(optObject.value);
}

function setDataCookie(aValue,aText){
	if (aValue == "") 
		return;
	var theCookie = extractCookie("selectionName",aValue);
	var theSelection = getCookie("selection");

	//Initialize theSelection string.
	if (theSelection == null) {
		theSelection = "";
	}

	if ( (theCookie == null) || (theSelection.indexOf(aValue + ",") < 0)) {
		theSelection = theSelection + aValue + ",";
		updateCookie("selectionName",aValue,aText);
	}
	setCookie("selection", theSelection);
	return true;
}

function deleteDataCookie(aValue){
	//Update the 'selection' cookie.
	var theSelection = getCookie("selection");
	if (theSelection == null) 
		var tmpSplit = new Array()
	else
		var tmpSplit = theSelection.split(",")
	theSelection = "";
	for(var i=0; i<tmpSplit.length;i++){
		if ((tmpSplit[i] != aValue) && (tmpSplit[i] != ""))
			theSelection += tmpSplit[i] + ",";
	}

	setCookie("selection", theSelection);
	dropFromCookie("selectionName",aValue);
}

function deleteData(id){
	deleteDataCookie(id);
	window.document.location.reload();
}

function strReplace(theStr, findTxt, replaceTxt) {
	var pos = 0;
	var len = findTxt.length;
	pos = theStr.indexOf(findTxt);
	while (pos != -1) {
		preStr = theStr.substring(0, pos);
		postStr = theStr.substring(pos+len,theStr.length);
		theStr = preStr + replaceTxt + postStr;
		pos = theStr.indexOf(findTxt);
	}
	return theStr
}

function getSelectedData() {
	var theSelection = getCookie("selection");

	if (theSelection != null) {
		var datasets = document.Datalist.dataset;
		if (datasets == null) return;
		var n_data = datasets.length;

		var tmpSplit = theSelection.split(",")
		for(var i=0; i<tmpSplit.length;i++){
			if(n_data == null){
				if(datasets.value == tmpSplit[i]) datasets.checked = true;
			}else{
	   			for (var j=0; j<n_data;j++) {
					if (datasets[j].value == tmpSplit[i]) datasets[j].checked = true;
				}
			}
		}
   	}  
}

function getListSelection(listPrefix){
	if (document.Datalist.maplist == null) 
		return;

	var theSelection = getCookie("selection");
	var n = listPrefix.length;

	if (theSelection != null) {
   		var valArr = theSelection.split(",");

  		for(var i=0; i<valArr.length;i++) {
			if(valArr[i].substr(0,n) == listPrefix){
				var txt = extractCookie("selectionName",valArr[i]);
				document.Datalist.maplist.options[document.Datalist.maplist.length] = new Option(txt, valArr[i]);
			}
		}
   	}  
}

function openData(){
	var theList = document.forms[0].specialCat;
	if(theList.selectedIndex < 2){
		theList.selectedIndex = 0;
		return;
	}else{
		var theURL = theList.options[theList.selectedIndex].value;
		window.document.location = theURL;
	}
}

function getNotes(noteURL) {
   	var xpos = screen.width - 750;
	if (noteURL.indexOf('http:') == -1) 
		noteURL = '/layernotes/'+noteURL+'.html'
	infoWin = window.open(noteURL,'infowin','width=740,height=600,toolbar=1,scrollbars=1,resizable=1,left='+xpos)
	infoWin.focus();
	window.status = "Layer information page";
}

