home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nintendo GameCube Preview CD-ROM
/
NINTENDOGAMECUBE.iso
/
site
/
js
/
form.js
< prev
next >
Wrap
Text File
|
2002-01-08
|
7KB
|
154 lines
// Form Functions v1.0
// http://www.dithered.com/javascript/form/index.html
// code by Chris Nott (chris@dithered.com)
// Convert a list of strings into a 'get' query string
function makeSearchString() {
var args = makeSearchString.arguments;
var searchString = "?";
var pair;
for (var i = 0; i < args.length; i++) {
pair = escape(args[i++]) + "=";
pair += escape(args[i]);
searchString += pair + "&";
}
return searchString.substring(0, searchString.length - 1);
}
// Create a 'get' query string with the data from a given form
function gatherFormData(form) {
var formData = '';
var element;
// For each form element, extract the name and value
for (var i = 0; i < form.elements.length; i++) {
element = form.elements[i];
if (element.type == "text" || element.type == "password" || element.type == "textarea") formData += "'" + element.name + "', '" + element.value + "', ";
else if (element.type.indexOf("select") != -1) {
for (var j = 0; j < element.options.length; j++) {
if (element.options[j].selected == true) formData += "'" + element.name + "', '" + element.options[element.selectedIndex].value + "', ";
}
}
else if (element.type == "checkbox" && element.checked) formData += "'" + element.name + "', '" + element.value + "', ";
else if (element.type == "radio" && element.checked == true) formData += "'" + element.name + "', '" + element.value + "', ";
}
// Feed strings to makeSearchString() to do 'get' query string conversion
return (eval("makeSearchString(" + formData.substring(0, formData.length - 2) + ")"));
}
// Transfer form data from a visible form to a hidden one
function transferToHiddenForm(sourceForm, destinationForm) {
var sourceElement, destinationElement, sourceOption;
// for each visible form element, transfer the associated value to the hidden form element with the same name
for (var i = 0; i < sourceForm.elements.length; i++) {
sourceElement = sourceForm.elements[i];
// make sure there is an associated hidden element for the visible one
if (sourceElement.name == '') continue;
destinationElement = eval('destinationForm.' + sourceElement.name);
if (destinationElement == null) continue;
if (sourceElement.type == "text" || sourceElement.type == "password" || sourceElement.type == "textarea") destinationElement.value = sourceElement.value;
else if (sourceElement.type == "select-one") destinationElement.value = sourceElement.options[sourceElement.selectedIndex].value;
// for multiple selects, create a |-delimited string of values
else if (sourceElement.type == "select-multiple") {
destinationElement.value = '|';
for (var j = 0; j < sourceElement.options.length; j++) {
sourceOption = sourceElement.options[j];
if (sourceOption.selected) destinationElement.value += sourceOption.value + '|';
}
}
else if (sourceElement.type == "checkbox" && sourceElement.checked == true) destinationElement.value = sourceElement.value;
else if (sourceElement.type == "radio" && sourceElement.checked == true) destinationElement.value = sourceElement.value;
}
}
// Transfer form data from a hidden form to a visible one
function transferFromHiddenForm(sourceForm, destinationForm) {
var sourceElement, destinationElement, sourceOption;
// for each visible form element, transfer the associated value from the hidden form element with the same name
for (var i = 0; i < destinationForm.elements.length; i++) {
destinationElement = destinationForm.elements[i];
// make sure there is an associated hidden element for the visible one
if (destinationElement.name == '') continue;
sourceElement = eval('sourceForm.' + destinationElement.name);
if (sourceElement == null) continue;
if (destinationElement.type == "text" || destinationElement.type == "password" || destinationElement.type == "textarea") destinationElement.value = sourceElement.value;
else if (destinationElement.type == "select-one") {
for (var j = 0; j < destinationElement.options.length; j++) {
if (sourceElement.value == destinationElement.options[j].value) {
destinationElement.selectedIndex = j;
break;
}
}
}
// look for multiple select values to be delimited by '|' in the hidden form element
else if (destinationElement.type == "select-multiple") {
for (var j = 0; j < destinationElement.options.length; j++) {
if (sourceElement.value.indexOf('|' + destinationElement.options[j].value + '|') != -1) destinationElement.options[j].selected = true;
}
}
else if (destinationElement.type == "checkbox") destinationElement.checked = true;
else if (destinationElement.type == "radio" && destinationElement.value == sourceElement.value) destinationElement.checked = true;
}
}
// Clear a form so that default initial values are erased
function clearForm(form) {
var element;
for (var i = 0; i < form.elements.length; i++) {
element = form.elements[i];
if (element.type == "text" || element.type == "password" || element.type == "textarea") element.value = '';
else if (element.type.indexOf("select") != -1) element.selectedIndex = -1;
else if (element.type == "checkbox" && element.checked) element.checked = false;
else if (element.type == "radio" && element.checked == true) element.checked = false;
}
}
// Build an associative array with all name and value pairs in a 'get' query string
function getSearchAsArray() {
var searchQuery = new Array;
var pair;
var temp;
var search = location.search;
// replace all '+'s with ' 's because unescape() doesn't do it
search = search.replace(/\+/g, ' ');
// for each pair, separate, unescape and place into the associate array
var split = 1;
while (split > 0) {
split = search.lastIndexOf('&');
if (split == -1) split = 0;
pair = search.substring(split + 1, search.length);
// multiple select values should be placed in an array
if (searchQuery[unescape(pair.substring(0, pair.indexOf('=')))] != null) {
temp = searchQuery[unescape(pair.substring(0, pair.indexOf('=')))];
searchQuery[unescape(pair.substring(0, pair.indexOf('=')))] = new Array(temp, unescape(pair.substring(pair.indexOf('=') + 1)));
}
// all other form elements have a one-to-one name and value relationship
else searchQuery[unescape(pair.substring(0, pair.indexOf('=')))] = unescape(pair.substring(pair.indexOf('=') + 1));
search = search.substring(0, split);
}
return searchQuery;
}
var query = getSearchAsArray();