
//
// This script pre-populates a form with data passed into the page.
//  To use, first call the getQsFromLocation method, then
//  call populateFields, passing it the form object you want to 
//  populate.
//
// @author Jon Steffey
// @version 1.0  13 Nov 2001
//

//
// These arrays will store the names and values, respectively,
//  of the data passed in on the url.
//
// Pass the data in like this:
//  <a href="thispage.html?fieldName1=val&fieldName2=val&etc=etc">
//
// See the comments in the populateFields method below for 
//  exactly what/how the form gets populated.
//
var keys = new Array();
var vals = new Array();
var params = new Object();

//
// Parses through the query string creating the keys and vals arrays.
//
function getQsFromLocation() {
	var qs = unescape(location.search);
	qs = qs.substring(1); // removes '?'
	var pairsArray = qs.split("&");
	for (var i=0; i<pairsArray.length; i++) {
		var onePair = pairsArray[i].split("=");
		keys[i] = onePair[0]; vals[i] = unescape(onePair[1]);
		params[onePair[0]] = unescape(onePair[1]);
	}
}

//
// Currently works only with textfields, checkboxes, radio buttons,
//  hidden fields, and popup-menus (<select>); later, if there's need,
//  I can add textareas and scrolling lists (<select multiple>),
//  though personally I think <select multiple> is bad UI for web,
//  and pre-populating a textarea, while useful, would not be needed often.
//
function populateFields() {
	// Put first argument passed to this function in 'form' variable.

	var form = arguments[0];


	// Put all the form elements in an array.
	var elemsArray = form.elements;


	// Loop through the array of form elements.
	for (var i=0; i<elemsArray.length; i++) {
		// For textfields and hidden fields, if the name of a form field
		//  matches one of our keys, write the corresponding value
		//  into that form field.
		if (elemsArray[i].type == "text" || elemsArray[i].type == "hidden") {
			for (var j=0; j<keys.length; j++) {
				if (elemsArray[i].name == keys[j]) {
					elemsArray[i].value = vals[j];
					break;
				}
			}
		// For checkboxes, if the name of the checkbox matches one of 
		//  our keys, mark that checkbox as checked (unless it was passed
		//  in on the query string with a value of 'false', 
		//  e.g. ?checkboxName=false).
		} else if (elemsArray[i].type == "checkbox") {
			for (var j=0; j<keys.length; j++) {
				if (elemsArray[i].name == keys[j]) {
					if (vals[j]!="false") {
						elemsArray[i].checked = true;
						break;
					}
				}
			}
		// For radio button lists, first see if the name of the 
		//  group of buttons matches a key; if so, see if the 
		//  value of a particular button matches that key's
		//  corresponding value (and thus mark that particular
		//  radio button as checked).
		} else if (elemsArray[i].type == "radio") {
			for (var j=0; j<keys.length; j++) {
				if (elemsArray[i].name == keys[j]) {
					if (vals[j]==elemsArray[i].value) {
						elemsArray[i].checked = true;
						break;
					}
				}
			}
		// For popup-menus or single-select scrolling lists, 
		//  first see if the name of the menu matches one of our
		//  keys; if so, loop through the options for that list,
		//  seeing if the value of one of the options matches
		//  that key's corresponding entry in vals (and thus
		//  make that option selected).
		} else if (elemsArray[i].type == "select-one") {
			for (var j=0; j<keys.length; j++) {
				if (elemsArray[i].name == keys[j]) {
					var menuOptions = elemsArray[i].options;
					for (var k=0; k<menuOptions.length; k++) {
						if (vals[j]==menuOptions[k].value) {
							elemsArray[i].selectedIndex = k;
							break;
						}
					}
				}
			}
		}
	}
}

