﻿// Controls UI for AdvancedQuerySearch.aspx

window.onload = function() {setupUnloader();criteriaChange(document.getElementById('criteria'));window.onload = null;};

function setupUnloader() {
	// Set the current unloader to a string.
	var currUnloader = window.onunload;
	// Create a new unloader, including this one.
	var newUnloader = function() {
		if (currUnloader) {currUnloader();}
		g_criteriaEntryLabel = null;
	}
	window.onunload = newUnloader;
}

// Stores the labels for the criteriaInput boxes.
var g_criteriaEntryLabel = [];
// Available fields: 0, 1, isNumeric, isDate, isRadio, stored1, stored2
g_criteriaEntryLabel['city'] = {0: 'Enter City:<br /><br />Type in a single city name or for multiple cities use a comma separated list.<br /><br />Example: Lakeland, Winter Haven', isNumeric: false, regexp: /^(?:([A-Z ]+)([ ]?,[ ]?)?)*$/i,regexpFailed: 'Please enter a city name.'};
g_criteriaEntryLabel['dor_cd'] = {0: 'Enter <a href="showLookupTable.aspx?table=dor" target="_blank">DOR Use Code</a>:<br /><br />Type in a single DOR Use Code, Partial code, or for multiple DOR Use Codes use a comma separated list.<br /><br />Example: 1%, 0001, 0002', isNumeric: false, regexp: /^(\d+(%|%,|%, |,|, )?)*$/i,regexpFailed: 'Please enter ditits, %, and commas only for DOR Use Code.'};
g_criteriaEntryLabel['acreage'] = {0: 'From:', 1: 'To:', isNumeric: false, regexp: /^([0-9\.\,\$]+)$/, regexpFailed: 'Please enter a size in acres.'};
g_criteriaEntryLabel['heat_ar'] = {0: 'From:', 1: 'To:<br /><br />Type Range in Square Feet.', isNumeric: true, regexp: /^([0-9\,\$]+)$/, regexpFailed: 'Please enter a size in square feet.'};
g_criteriaEntryLabel['act_ar'] = {0: 'From:', 1: 'To:<br /><br />Type Range in Square Feet.', isNumeric: true, regexp: /^([0-9\,\$]+)$/, regexpFailed: 'Please enter a size in square feet.'};
g_criteriaEntryLabel['nh_cd'] = {0: 'Enter <a href="showLookupTable.aspx?table=nhcd" target="_blank">NHBD No</a>:<br /><br />Type in a single NBHD No or for multiple NBHD No use a comma separated list.<br /><br />Example: 000387, 000388', isNumeric: false, regexp: /^(([\d]+)([\.]{1})([\d]{2})([ ]?,[ ]?)?)*$/, regexpFailed: 'Please enter the NBHD No, be sure to include the decimal point.'};
g_criteriaEntryLabel['name'] = {0: 'Enter Owner Name:', isNumeric: false};
g_criteriaEntryLabel['parcelid'] = {0: 'From:', 1: 'To:', isNumeric: false, isOptional2: true};
g_criteriaEntryLabel['accountid'] = {0: 'From:', 1: 'To:', isNumeric: false, isOptional2: true};
g_criteriaEntryLabel['or_bk'] = {0: 'Sale Book:', isNumeric: false};
g_criteriaEntryLabel['or_pg'] = {0: 'Sale Page:', isNumeric: false};
g_criteriaEntryLabel['dos'] = {0: 'From:', 1: 'To:', isNumeric: false, isDate: true, stored1: ''};
g_criteriaEntryLabel['price'] = {0: 'From:', 1: 'To:', isNumeric: true};
g_criteriaEntryLabel['qu'] = {0: 'Yes:', 1: 'No:', isNumeric: false, isRadio: true};
g_criteriaEntryLabel['str'] = {0: 'Enter Street Name:', isNumeric: false};
g_criteriaEntryLabel['str_num'] = {0: 'Enter Street Number:', isNumeric: true};
g_criteriaEntryLabel['sub'] = {0: 'Enter <a href="showLookupTable.aspx?table=sub" target="_blank">Subdivision Number</a>:', isNumeric: false};
g_criteriaEntryLabel['swimmingpool'] = {0: 'Enter <a href="showLookupTable.aspx?table=swimmingpool" target="_blank">Swimming Pool Code</a>:', isNumeric: false};
g_criteriaEntryLabel['tax_dist'] = {0: 'Enter <a href="showLookupTable.aspx?table=tax_dist" target="_blank">Tax District</a>:', isNumeric: false};
g_criteriaEntryLabel['eff'] = {0: 'From:', 1: 'To:', isNumeric: true};
g_criteriaEntryLabel['hx'] = {0: 'Yes:', 1: 'No:', isNumeric: false, isRadio: true};

function criteriaChange(selectObj) {
	window.status = selectObj.value;

	// If there are already criteria set then enable the buttons.
	if (document.getElementById('currentCriteria').options.length > 0) {
		// Enable the buttons.
		document.getElementById('btn_edit').disabled = false;
		document.getElementById('btn_remove').disabled = false;
		document.getElementById('btn_removeAll').disabled = false;
	}

	var criteriaEntry1Label = document.getElementById('criteriaEntry1Label');
	var criteriaEntry2Label = document.getElementById('criteriaEntry2Label');
	var criteriaEntry1 = document.getElementById('criteriaEntry1');
	var criteriaEntry2 = document.getElementById('criteriaEntry2');
	var criteriaEntryRadio1 = document.getElementById('criteriaEntryRadio1');
	var criteriaEntryRadio2 = document.getElementById('criteriaEntryRadio2');

	// Hide all the controls and reset the text, we will re-display it later if applicable.
	criteriaEntry1Label.style.display = 'none';
	criteriaEntry2Label.style.display = 'none';
	criteriaEntry1.style.display = 'none';
	criteriaEntry2.style.display = 'none';
	criteriaEntryRadio1.style.display = 'none';
	criteriaEntryRadio2.style.display = 'none';

	// Make sure that the selected criteria has data in the g_criteriaEntryLabel array.
	if (g_criteriaEntryLabel[selectObj.value]) {
		// If it has a first label and that label isn't blank then...
		if ((g_criteriaEntryLabel[selectObj.value][0]) && (g_criteriaEntryLabel[selectObj.value][0] != '')) {
			// Set the label text and show it.
			criteriaEntry1Label.innerHTML = g_criteriaEntryLabel[selectObj.value][0] + ' ';
			criteriaEntry1Label.style.display = 'inline';

			if (g_criteriaEntryLabel[selectObj.value].isRadio) {
				if ((g_criteriaEntryLabel[selectObj.value].stored1 == true) || (g_criteriaEntryLabel[selectObj.value].stored1 == 'true')) {
					criteriaEntryRadio1.checked = true;
					criteriaEntryRadio2.checked = false;
				} else {
					criteriaEntryRadio1.checked = false;
					criteriaEntryRadio2.checked = true;
				}
				criteriaEntryRadio1.style.display = 'inline';
			} else {
				// Reload the stored value.
				if (g_criteriaEntryLabel[selectObj.value].stored1) {
					criteriaEntry1.value = g_criteriaEntryLabel[selectObj.value].stored1;
				} else {
					criteriaEntry1.value = '';
				}
				// Show the input box.
				criteriaEntry1.style.display = 'inline';
			}
		}

		// If it has a second label and that label isn't blank then...
		if ((g_criteriaEntryLabel[selectObj.value][1]) && (g_criteriaEntryLabel[selectObj.value][1] != '')) {
			// Set the label text and show it.
			criteriaEntry2Label.innerHTML = g_criteriaEntryLabel[selectObj.value][1] + ' ';
			criteriaEntry2Label.style.display = 'inline';

			if (g_criteriaEntryLabel[selectObj.value].isRadio) {
				criteriaEntryRadio2.style.display = 'inline';
			} else {
				// Reload the stored value.
				if (g_criteriaEntryLabel[selectObj.value].stored2) {
					criteriaEntry2.value = g_criteriaEntryLabel[selectObj.value].stored2;
				} else {
					criteriaEntry2.value = '';
				}
				// Show the input box.
				criteriaEntry2.style.display = 'inline';
			}
		}
	}
}

function storeValue(inputObj) {
	// Get the ID so we know if this is value 1 or 2.
	var thisID = inputObj.id;
	var thisValue = inputObj.checked ? inputObj.checked : inputObj.value;
	var thisNumber = 0;
	if (thisID) {
		thisNumber = thisID.substring(thisID.length - 1);
	} else {
		return false;
	}
	
	// Get the current criteria.
	var currCriteria = '';
	if (document.getElementById('criteria')) {
		currCriteria = document.getElementById('criteria').value;
	} else {
		return false;
	}

	// Store the value.
	if (thisNumber == '1') {
		g_criteriaEntryLabel[currCriteria].stored1 = thisValue;
		
		if (g_criteriaEntryLabel[currCriteria].isRadio) {
			g_criteriaEntryLabel[currCriteria].stored2 = !thisValue;
		}
	} else if (thisNumber == '2') {
		g_criteriaEntryLabel[currCriteria].stored2 = thisValue;
		
		if (g_criteriaEntryLabel[currCriteria].isRadio) {
			g_criteriaEntryLabel[currCriteria].stored1 = !thisValue;
		}
	} else {
		return false;
	}
}

function addCriteria() {
	// Get the current criteria.
	var currCriteriaValue = '';
	var currCriteriaName = '';
	var currCriteriaIsNumeric = false;
	var currCriteriaIsDate = false;
	var currCriteriaIsRadio = false;
	var has1 = false;
	var has2 = false;
	var isOptional1 = false;
	var isOptional2 = false;
	if (document.getElementById('criteria')) {
		currCriteriaValue = document.getElementById('criteria').value;
		currCriteriaName = document.getElementById('criteria').options[document.getElementById('criteria').selectedIndex].text;

		if (!g_criteriaEntryLabel[currCriteriaValue]) {return false;}
		
		currCriteriaIsNumeric = g_criteriaEntryLabel[currCriteriaValue].isNumeric;
		currCriteriaIsDate = g_criteriaEntryLabel[currCriteriaValue].isDate;
		currCriteriaIsRadio = g_criteriaEntryLabel[currCriteriaValue].isRadio;
		
		if ((g_criteriaEntryLabel[currCriteriaValue][0]) && (g_criteriaEntryLabel[currCriteriaValue][0] != '')) {has1 = true;}
		if ((g_criteriaEntryLabel[currCriteriaValue][1]) && (g_criteriaEntryLabel[currCriteriaValue][1] != '')) {has2 = true;}
		if ((g_criteriaEntryLabel[currCriteriaValue].isOptional1) && (g_criteriaEntryLabel[currCriteriaValue].isOptional1 == true)) {isOptional1 = true;}
		if ((g_criteriaEntryLabel[currCriteriaValue].isOptional2) && (g_criteriaEntryLabel[currCriteriaValue].isOptional2 == true)) {isOptional2 = true;}
	} else {
		return false;
	}
	
	// Get the criteriaInputs
	var criteriaEntry1 = document.getElementById('criteriaEntry1');
	var criteriaEntry2 = document.getElementById('criteriaEntry2');
	var criteriaEntryRadio1 = document.getElementById('criteriaEntryRadio1');
	var criteriaEntryRadio2 = document.getElementById('criteriaEntryRadio2');
	
	var criteriaEntry1Value = '';
	var criteriaEntry2Value = '';

	// Add this criteria to the holding list.
	var criteriaHolding = document.getElementById('currentCriteria');
	if (!criteriaHolding) {return false;}

	// Process the values, make sure they are valid.
	// First check it against the custom handler.
		if ((g_criteriaEntryLabel[currCriteriaValue].regexp) && (g_criteriaEntryLabel[currCriteriaValue].regexp != '')) {
		var customRegExp = g_criteriaEntryLabel[currCriteriaValue].regexp;
		var customRegExpErrorMsg = g_criteriaEntryLabel[currCriteriaValue].regexpFailed;

		// If there is an input1 then validate it.
		if (has1) {
			var matching = criteriaEntry1.value.match(customRegExp);
			if (!matching) {
				alert(customRegExpErrorMsg);
				criteriaEntry1.focus();
				return false;
			}
			criteriaEntry1Value = criteriaEntry1.value;
		}
		// If there is an input2 then validate it as well.
		if (has2) {
			// Strip all commas and dollar signs.
			var matching = criteriaEntry2.value.match(customRegExp);
			if (!matching) {
				if ((criteriaEntry2.value == '') && (isOptional2)) {
					// Do nothing, the user is allowed to leave this value blank for this type.
				} else {
					alert(customRegExpErrorMsg);
					criteriaEntry2.focus();
					return false;
				}
			}
			criteriaEntry2Value = criteriaEntry2.value;
		}
	}

	// Now check it against the built-in handler.
	if (currCriteriaIsNumeric) {
		// Validate the input.
		// If there is an input1 then validate it.
		if (has1) {
			// Strip all commas and dollar signs.
			criteriaEntry1.value = criteriaEntry1.value.replace(/,/g,'').replace(/\$/g,'');
			var matching = criteriaEntry1.value.match(/^([\d]+)?([\.]?[\d]+)$/);
			if (!matching) {
				alert('Please enter a valid number.');
				criteriaEntry1.focus();
				return false;
			}
			criteriaEntry1Value = criteriaEntry1.value;
		}
		// If there is an input2 then validate it as well.
		if (has2) {
			// Strip all commas and dollar signs.
			criteriaEntry2.value = criteriaEntry2.value.replace(/,/g,'').replace(/\$/g,'');
			var matching = criteriaEntry2.value.match(/^([\d]+)?([\.]?[\d]+)$/);
			if (!matching) {
				if ((criteriaEntry2.value == '') && (isOptional2)) {
					// Do nothing, the user is allowed to leave this value blank for this type.
				} else {
					alert('Please enter a valid number.');
					criteriaEntry2.focus();
					return false;
				}
			}
			criteriaEntry2Value = criteriaEntry2.value;
		}
	} else if(currCriteriaIsDate) {
		// Validate the input.
		// If there is an input1 then validate it.
		if (has1) {
			// Convert dashes to slashes.
			criteriaEntry1.value = criteriaEntry1.value.replace(/-/g,'/');
			// It isn't pretty, but it will match 2 digit and 4 digit years.
			var matching = criteriaEntry1.value.match(/^([01]?[0-9]{1})\/{1}([0123]?[0-9]{1})\/{1}([12][0-9]{3}|[0-9]{2})$/);
			if (!matching) {
				alert('Please enter a date.');
				criteriaEntry1.focus();
				return false;
			}
			criteriaEntry1Value = criteriaEntry1.value;
		}
		// If there is an input2 then validate it as well.
		if (has2) {
			// Convert dashes to slashes.
			criteriaEntry2.value = criteriaEntry2.value.replace(/-/g,'/');
			// It isn't pretty, but it will match 2 digit and 4 digit years.
			var matching = criteriaEntry2.value.match(/^([01]?[0-9]{1})\/{1}([0123]?[0-9]{1})\/{1}([12][0-9]{3}|[0-9]{2})$/);
			if (!matching) {
				if ((criteriaEntry2.value == '') && (isOptional2)) {
					// Do nothing, the user is allowed to leave this field blank for this type.
				} else {
					alert('Please enter a date.');
					criteriaEntry2.focus();
					return false;
				}
			}
			criteriaEntry2Value = criteriaEntry2.value;
		}
	} else if (currCriteriaIsRadio) {
		criteriaEntry1Value = criteriaEntryRadio1.checked;
		criteriaEntry2Value = '';
	} else {
		// Alphanumeric, make sure that there is a valid.
		if (has1) {
			criteriaEntry1.value = criteriaEntry1.value.replace(/[|]/g,':');
			if (criteriaEntry1.value == '') {
				alert('Please enter a value.');
				criteriaEntry1.focus();
				return false;
			}
			criteriaEntry1Value = criteriaEntry1.value;
		}
		if (has2) {
			criteriaEntry2.value = criteriaEntry2.value.replace(/[|]/g,':');
			if (criteriaEntry2.value == '') {
				if ((criteriaEntry2.value == '') && (isOptional2)) {
					// Do nothing, the user is allowed to enter a blank value for this type.
				} else {
					alert('Please enter a value.');
					criteriaEntry2.focus();
					return false;
				}
			}
			criteriaEntry2Value = criteriaEntry2.value;
		}
	}
	
	var criteriaDisplay = '';
	if (criteriaEntry2Value != '') {
		criteriaDisplay = currCriteriaName + ':' + criteriaEntry1Value + '-' + criteriaEntry2Value;
	} else {
		criteriaDisplay = currCriteriaName + ':' + criteriaEntry1Value.toString().toUpperCase();
	}

	// If this value already exists then select it and remove it before adding it.
	for (var i=0;i<criteriaHolding.options.length;i++) {
		// Get everything before the first pipe, test it against the upcoming value.
		var matchTest = '^' + currCriteriaValue + '\\|';
		if (criteriaHolding.options[i].value.match(matchTest)) {
			// Select this one and then remove it.
			criteriaHolding.selectedIndex = i;
			removeCriteria();
			break;
		}
	}
	
	criteriaHolding.options[criteriaHolding.options.length] = new Option(criteriaDisplay, currCriteriaValue + '|' + criteriaEntry1Value + '|' + criteriaEntry2Value);

	// Clear the values then store them.
	criteriaEntry1.value = '';
	criteriaEntry2.value = '';
	criteriaEntryRadio1.checked = false;
	criteriaEntryRadio2.checked = false;
	if (currCriteriaIsRadio) {
		storeValue(criteriaEntryRadio1);
		storeValue(criteriaEntryRadio2);
	} else {
		storeValue(criteriaEntry1);
		storeValue(criteriaEntry2);
	}
	
	// Enable the buttons.
	document.getElementById('btn_edit').disabled = false;
	document.getElementById('btn_remove').disabled = false;
	document.getElementById('btn_removeAll').disabled = false;
}

function editCriteria() {
	var criteriaHolding = document.getElementById('currentCriteria');
	if (!criteriaHolding) {
		return false;
	}
	
	if (criteriaHolding.selectedIndex == -1) {
		alert('Please select a criteria to edit.');
		return false;
	}

	// Get the value, parse it out.  Then set the stored variables for this selected criteria and call the changeCriteria function to modify it.
	var thisValue = criteriaHolding.options[criteriaHolding.selectedIndex].value;
	// Split it up.
	var matches = thisValue.match(/^([\w]+)[|]{1}([^|]+)[|]{1}([^|]*)/i);
	if (!matches) {return false;}
	if (!g_criteriaEntryLabel[matches[1]]) {return false;}

	g_criteriaEntryLabel[matches[1]].stored1 = matches[2];
	g_criteriaEntryLabel[matches[1]].stored2 = matches[3];
	
	// Find this criteria in the "Choose Criteria" box and select it.
	var criteriaSelection = document.getElementById('criteria');
	if (!criteriaSelection) {return false;}
	for (var i=0;i<criteriaSelection.options.length;i++) {
		var matchTest = '^' + matches[1] + '$';
		if (criteriaSelection.options[i].value.match(matchTest)) {
			// Select this criteria.
			criteriaSelection.selectedIndex = i;
			break;
		}
	}

	criteriaChange(criteriaSelection);
}

function removeCriteria() {
	var criteriaHolding = document.getElementById('currentCriteria');
	if (!criteriaHolding) {return false;}

	if (criteriaHolding.selectedIndex > -1) {
		criteriaHolding.options[criteriaHolding.selectedIndex] = null;
	}

	// If this is the last criteria then disable the buttons.
	if (criteriaHolding.options.length == 0) {
		document.getElementById('btn_edit').disabled = true;
		document.getElementById('btn_remove').disabled = true;
		document.getElementById('btn_removeAll').disabled = true;
	} else {
		// Select the first entry now.
		criteriaHolding.selectedIndex = 0;
	}
}

function removeAllCriteria() {
	var criteriaHolding = document.getElementById('currentCriteria');
	if (!criteriaHolding) {return false;}
	
	criteriaHolding.options.length = 0;
	
	// Disable the buttons.
	document.getElementById('btn_edit').disabled = true;
	document.getElementById('btn_remove').disabled = true;
	document.getElementById('btn_removeAll').disabled = true;
}

function doSearch() {

	// Copy everything in the currCriteria box into a form field and submit the form.
    var destinationField = document.getElementById('currCriteriaHolding');
    var currCriteria = document.getElementById('currentCriteria');
    if (currCriteria.options.length == 0) 
    { 
    alert('Search cannot be performed, you have not added any criteria.');
    return false;
    }
	if (!destinationField) {return false;}
	
	destinationField.value = combineCriteria();

//	if (document.getElementById(g_recordsPerPageElementName)) {
var t = document.getElementById(g_recordsPerPageElementName);
		//document.getElementById('recordsPerPage').value = document.getElementById(g_recordsPerPageElementName).value;
		document.getElementById('recordsPerPage').value = t.value;
	//}

	var sortBy = document.getElementById(g_sortByElementName);
	var sortByDirection = document.getElementById(g_sortByDirectionElementName);
	if ((sortBy) && (sortByDirection)) {
		if ((sortByDirection.value == '') || (sortByDirection.value == 'ASC')) {
			document.getElementById('sortBy').value = sortBy.value;
		} else {
			document.getElementById('sortBy').value = sortBy.value + ' DESC';
		}
	}
	if (document.getElementsByName('currentIndex')[0] )
	{
   document.getElementsByName('currentIndex')[0].value = '0';
 
   }
   
	document.getElementById('aspnetForm').submit();
	return true;
	
}

function combineCriteria() {
	var criteriaHolding = document.getElementById('currentCriteria');
	var returnValue = '';
		
	for (var i=0;i<criteriaHolding.options.length;i++) {
		returnValue += criteriaHolding.options[i].value.replace(/~/g,'-').replace(/#/,'') + '#' + criteriaHolding.options[i].text.replace(/~/g,'-').replace(/#/,'') + '~';
	}

	return returnValue;
}

function onCriteriaLoad() {
	var savedCriteriaList = document.getElementById('savedCriteriaList');
	if (!savedCriteriaList) {return false;}
	if (savedCriteriaList.selectedIndex == -1) {return false;}

	thisName = savedCriteriaList[savedCriteriaList.selectedIndex].text;
	// Don't select the first entry.
	if (thisName == 'Select') {return false;}

	// Load the criteria from the cookie.
	thisValue = readCookie('savedCriteria-' + thisName);
	if (thisValue == null) {thisValue = '';}
	
	var criteriaHolding = document.getElementById('currentCriteria');
	// Remove all options
	while (criteriaHolding.options.length > 0) {
		criteriaHolding.options[criteriaHolding.options.length-1] = null;	
	}
	var criteriaArray = thisValue.split('~');
	for (var i=0;i<criteriaArray.length-1;i++) {
		if (criteriaArray[i] != '') {
			var subString = criteriaArray[i].split('#');
			// Add this option to the list.
			criteriaHolding.options[criteriaHolding.options.length] = new Option(subString[1], subString[0]);
		}
	}

	// Enable the buttons.
	document.getElementById('btn_edit').disabled = false;
	document.getElementById('btn_remove').disabled = false;
	document.getElementById('btn_removeAll').disabled = false;

	onCriteriaClose();
}

function onCriteriaDelete() {
	var savedCriteriaList = document.getElementById('savedCriteriaList');
	if (!savedCriteriaList) {return false;}
	if (savedCriteriaList.selectedIndex == -1) {return false;}

	thisName = savedCriteriaList[savedCriteriaList.selectedIndex].text;
	// Don't delete the first entry.
	if (thisName == 'Select') {return false;}

	// Get the name of the selected value.  1) Remove the cookie entry for it, 2) remove it from the namesList, 3) remove it from the select list.
	eraseCookie('savedCriteria-'+ thisName);

	// Rebuild the namesList, don't include the thisName value.
	// Get the names list cookie.
	var namesList = readCookie('savedCriteriaNames');
	
	// Stop if there are no names saved.
	if ((namesList != null) && (namesList != '')) {
		var newNamesList = '';
		var namesListArray = namesList.split('|');
		for (var i=0;i<namesListArray.length-2;i++) {
			if (namesListArray[i] != thisName) {
				newNamesList += namesListArray[i] + '|';
			}
		}
		eraseCookie('savedCriteriaNames');
		createCookie('savedCriteriaNames',newNamesList,360);
	}
	
	// Remove this entry from the select list.
	savedCriteriaList.options[savedCriteriaList.selectedIndex] = null;
}

function onCriteriaSave() {
	// Get the name.
	var saveCriteriaName = document.getElementById('saveCriteriaName');
	if (!saveCriteriaName) {return false;}
	var savedCriteriaList = document.getElementById('savedCriteriaList');
	if (!savedCriteriaList) {return false;}

	// Make sure it isn't a blank name.
	if (saveCriteriaName.value == '') {
		alert('Please specify a name.');
		return false;
	}

	var currCriteria = combineCriteria();

	// Save this to a cookie.
	createCookie('savedCriteria-'+ saveCriteriaName.value, currCriteria, 360);
	
	// Add the name to the namesList
	var namesList = readCookie('savedCriteriaNames');
	if (namesList == null) {namesList = '';}
	eraseCookie('savedCriteriaNames');
	namesList += saveCriteriaName.value + '|';
	
	createCookie('savedCriteriaNames',namesList, 360);
	
	// Add the name to the select list.
	savedCriteriaList.options[savedCriteriaList.options.length] = new Option(saveCriteriaName.value, currCriteria);

	onCriteriaClose();
}

function onCriteriaOpen() {
	populateSavedCriteriaList();

	var saveLoadDisplay = document.getElementById('saveLoadDisplay');
	if (saveLoadDisplay) {
		saveLoadDisplay.style.display = 'block';
	}
}

function onCriteriaClose() {
	var saveLoadDisplay = document.getElementById('saveLoadDisplay');
	if (saveLoadDisplay) {
		saveLoadDisplay.style.display = 'none';
	}
}

function populateSavedCriteriaList() {
	var savedCriteriaList = document.getElementById('savedCriteriaList');
	if (!savedCriteriaList) {return false;}
	
	// Get the names list cookie.
	var namesList = readCookie('savedCriteriaNames');
	
	// Stop if there are no names saved.
	if ((namesList == null) || (namesList == '')) {return false;}
	
	// Clear the options from the select list.
	while (savedCriteriaList.options.length > 0) {
		savedCriteriaList.options[savedCriteriaList.options.length-1] = null;	
	}
	
	// Add the names to the list, use the value for the name as the value of the option.
	var namesListArray = namesList.split('|');
	for (var i=0;i<namesListArray.length-1;i++) {
		if (namesListArray[i] != null) {
			var thisValue = readCookie('savedCriteria-' + namesListArray[i]);
			savedCriteriaList.options[savedCriteriaList.options.length] = new Option(namesListArray[i], thisValue);
		}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
