/* =================================================================
   Builder class for constructing HTML to push through asyncronous
   IFrame POST requests (instead of using string concatenation in
   the main ASP code, this class can be used to create the string
   by specifying information such as the form name, ASP page
   (action) and the various data fields.

   This makes reading the ASP code a lot easier.
================================================================= */

function CFormData() {
 this.FormName = '';
 this.FormID = '';
 this.FormMethod = '';
 this.FormAction = '';
 this.FormTarget = '';

 this.FieldNames = new Array();
 this.FieldValues = new Array();

 // ====================================================================
 // SHORT-CUT POST FUNCTION;
 // USE IN PLACE OF FormDetails() AND GetFormHTML()
 // SPECIFY A FRAME TO TARGET WHICH WILL POST THE SPECIFIED PAGE TO
 // ITSELF
 // ====================================================================
 this.PostToFrame = function(strFrameName, strPage) {
  // Push data to iframe
  // NB: Firefox and IE support access to frame location through
  //     contentWindow.document, but this won't work with Opera.
  //     Use document.frames[frameId].document instead (this will
  //     also work with IE but not firefox)
  // NB: To work with Opera (and not get a 'protected variable'
  //     error, the iframe must already have a source assigned
  //     from the same domain as this page)
  var objFrame = document.getElementById(strFrameName);
  var objDocument;
  if (objFrame.contentWindow)
   objDocument = objFrame.contentWindow.document;
  else
   objDocument = document.frames[strFrameName];
  this.FormDetails('', '', 'POST', strPage, '_self');
  objDocument.body.innerHTML = this.GetFormHTML();
  objDocument.forms[0].submit();
 }

 // ====================================================================
 // EQUIVALENT OF PostToFrame() USING GET REQUEST
 // ====================================================================
 this.GetToFrame = function(strFrameName, strPage) {
  // Push data to iframe
  // NB: Firefox and IE support access to frame location through
  //     contentWindow.document, but this won't work with Opera.
  //     Use document.frames[frameId].document instead (this will
  //     also work with IE but not firefox)
  // NB: To work with Opera (and not get a 'protected variable'
  //     error, the iframe must already have a source assigned
  //     from the same domain as this page)
  var objFrame = document.getElementById(strFrameName);
  var objDocument;
  if (objFrame.contentWindow)
   objDocument = objFrame.contentWindow.document;
  else
   objDocument = document.frames[strFrameName];

  // Create parameter string
  var arrParameters = new Array();
  for (var intLoop = 0; intLoop < this.FieldNames.length; intLoop++)


// John - I commented out the end of the below statement to break it, it didn't break and seems to work better... - Start

   arrParameters[intLoop]// = escapeCharacters(this.FieldNames[intLoop]) + '=' + escape(this.FieldValues[intLoop]);

// John - I commented out the end of the below statement to break it, it didn't break and seems to work better... - End


  var strParameters = '';
  if (arrParameters.length > 0)
   strParameters = '?' + arrParameters.join('&');

  // Change page location
  objDocument.location = strPage + strParameters;
 }

 // ====================================================================
 // PREPARE FORM DETAILS
 // ====================================================================
 this.FormDetails = function(name, id, method, action, target) {
  this.FormName = name;
  this.FormID = id;
  this.FormMethod = method;
  this.FormAction = action;
  this.FormTarget = target;
 }

 // ====================================================================
 // PASS DATA FOR ONE FIELD
 // ====================================================================
 this.AddField = function(name, value) {
  this.FieldNames[this.FieldNames.length] = name;
  this.FieldValues[this.FieldValues.length] = value;
 }

 // ====================================================================
 // RETRIEVE GENERATED HTML AFTER USE OF FormDetails() AND AddField()
 // ====================================================================
 this.GetFormHTML = function() {
  var intLoop;
  var strData = '';

  strData = '<form'; 
  if (this.FormName != '')
   strData = strData + ' name="' + this.FormName + '"';
  if (this.FormID != '')
   strData = strData + ' id="' + this.FormID + '"';
  if (this.FormMethod != '')
   strData = strData + ' method="' + this.FormMethod + '"';
  if (this.FormAction != '')
   strData = strData + ' action="' + this.FormAction + '"';
  if (this.FormTarget != '')
   strData = strData + ' target="' + this.FormTarget + '"';
  strData = strData + '>';

  for (var intLoop = 0; intLoop < this.FieldNames.length; intLoop++)
   strData = strData + '<input type="hidden" name="' + escapeCharacters(this.FieldNames[intLoop]) + '" value="' + escapeCharacters(this.FieldValues[intLoop]) + '">';

  strData = strData + '</form>'

  return(strData);
 }

 // ====================================================================
 // ESCAPE SPECIAL CHARACTERS
 // ====================================================================
 function escapeCharacters(strIn) {
  strIn = String(strIn);
  strIn = strIn.replace(/\\/ig,"\\\\");
  strIn = strIn.replace(/\r/ig,"\\r");
  strIn = strIn.replace(/\n/ig,"\\n");
  strIn = strIn.replace(/\"/ig,'\\"');
  strIn = strIn.replace(/\'/ig,"\\'");
  strIn = strIn.replace(/\t/ig,"\\t");
  return strIn;
 }

}
