﻿
//
// Put JavaScript somewhere below the form.
//

// The function that does the work.
function FillForm() {

//
// To customize your form, specify form's name 
//   between the quotes on the next line.
var FormName = "ReportRequestForm";

// Find the location of the ? in the URL.
var questionlocation = location.href.indexOf('?');

// If no ?, return out of the function.
if(questionlocation < 0) { return; }

// Assign the text following the ? to variable q
// The text might look something like (just an example):
//   name=will%20%22B%22&email=will%40example.com&fun=yes
var q = location.href.substr(questionlocation + 1);

// Split q on & characters and assign to array variable list.
// (In other words, chop the string at each & character 
//    and store the pieces in an array variable named list.)
// The array elements, using the above example, are:
//      name=will%20%22B%22
//      email=will%40example.com
//      money=enough
var list = q.split('&');

// For each element of array list, execute the {} block.
for(var i = 0; i < list.length; i++) {

   // Split the list array element on = character and 
   //   assign the pieces to array variable kv.
   // kv[0] will then contain the field name and kv[1] 
   //   will contain the field value.
   var kv = list[i].split('=');

   // If the form does not have a field name kv[0], 
   //   go to the top of the {} loop and continue there.
	if(! eval('document.'+FormName+'.'+kv[0])) { continue; }

   // Convert %##'s to the actual characters.
   //    will%20%22B%22 becomes: will "B"
   //    will%40example.com becomes: will@example.com
   kv[1] = unescape(kv[1]);

   // If value kv[1] contains a " character, execute the 
   //   {} loop.
   // (The " character needs to be escaped because the 
   //   value in the eval() function below uses " for field 
   //   value delimiters.)
   if(kv[1].indexOf('"') > -1) {

      // Assign a regular expression to variable re. The 
      //   expression looks for all " characters.
      var re = /"/g;

      // In kv[1], replace each " character with: \"
      // (The \ itself needs to be escaped here.)
      kv[1] = kv[1].replace(re,'\\"');
      }

   // Create an evaluation expression for the eval() 
   //   function that assigns the value kv[1] to the 
   //   form field kv[0].
   eval('document.'+FormName+'.'+kv[0]+'.value="'+kv[1]+'"');
   }

// end of function
}

// Execute the above function to pre-fill in the form fields.
FillForm();


