/*
  How to use: See http://javascript.about.com/library/blforms.htm
  Version 2.1 (9/10/08)
  Modified by Philip in Oct 2008
  - support "s" flag in st argument (style)
*/

// JavaScript Form Generator
// copyright Stephen Chapman 21st June 2008
// http://javascript.about.com
// You are granted permission to use this script
// provided that the code including these comments
// is used exactly as supplied

// a: action
// m: method get or post
// e: enc type; can be left as null string
// txt: string label of the whole form
function Form(a, m, e, txt) {
  this.F = document.createElement("form"); 
  this.F.action = a;
  this.F.method = m;
  this.F.id = txt.replace(/\s/gi,""); // ppsh
  this.F.parent = this; // ppsh
  if (e != '') this.F.enctype = e; 
  this.fld = document.createElement("fieldset"); 
  var t = document.createTextNode(txt); 
  var l = document.createElement("legend"); 
  l.appendChild(t); 
  this.fld.appendChild(l);
} 

// l : string label
// n : symbolic name (and id) of element
// v : value/data contained in the element
// tx: text, radio, checkbox, submit, button, password
// st: (c)hecked, (d)isabled, (r)eadonly, st(y)le may be null string
// p : panel ID
Form.prototype.addInput = function(tx, l, n, v, st, p) {
  switch(tx) {
    case 'password':
    case 'text': 
    case 'radio': 
    case 'checkbox': 
    case 'submit': 
    case 'button': 
      var d = document.createElement("div"); 
      var t = document.createTextNode(l+' '); 
      var lab2 = document.createElement("label"); 
      lab2.appendChild(t); /*@cc_on @if (@_jscript) var inp = document.createElement("<input name='"+n+"'>"); @else */ 
      var inp = document.createElement("input"); 
      inp.name = n; /* @end @*/ 
      inp.type = tx;
      /* ppsh - IE bug fix */     
      if (st.match(/c/i) && (tx=='radio' || tx=='checkbox')) {
      	if (document.all && !window.opera) {
      	  var ele = "<input type=radio";
      	  if (tx=='checkbox') ele = "<input type=checkbox"; 
      	  inp=document.createElement(ele + " name=" + n + " checked>");
      	}
      } 
      /* ppsh - start */
      var lab = lab2;
      if (st.match(/y/i)) {
        var lab = document.createElement("div");
        lab.id = l.replace(/\s/gi,"");
        lab.appendChild (lab2);
      }
      /* ppsh - end */      
      if (tx == 'radio') {
        if (this.n != +this.n) this.n = 0; 
        this.n++; 
        n+= this.n;
      } 
      inp.id = n; 
      lab2.htmlFor = n; 
      inp.value = v; 
      if (st.match(/c/i)) inp.checked = true; 
      if (st.match(/d/i)) inp.disabled = true; 
      if (st.match(/r/i)) inp.readOnly = true; 
      if (tx == 'radio' || tx == 'checkbox') {
        d.appendChild(inp); 
        d.appendChild(lab); 
      }
      else {
        d.appendChild(lab); 
        d.appendChild(inp);
      }
      if (p != null) p.appendChild (d); 
        else this.fld.appendChild(d); 
      break; 
    default:
  }
};


// l : string label
// n : symbolic name (and id) of element
// v : value/data contained in the element
// st: (c)hecked, (d)isabled, (r)eadonly, st(y)le may be null string
Form.prototype.addTextarea = function(l, n, v, st) {
  var d = document.createElement("div"); 
  var t = document.createTextNode(l+' '); 
  var lab2 = document.createElement("label"); 
  lab2.appendChild(t); /*@cc_on @if (@_jscript) var inp = document.createElement("<textarea name='"+n+"'>"); @else */ 
  var inp = document.createElement("textarea"); 
  inp.name = n; /* @end @*/ 
  inp.id = n; 
  var c = document.createTextNode(v); 
  inp.appendChild(c); 
  /* ppsh - start */
  var lab = lab2;
  if (st.match(/y/i)) {
    var lab = document.createElement("div");
    lab.id = l.replace(/\s/gi,"");
    lab.appendChild (lab2);
  }
  /* ppsh - end */      
  if (st.match(/d/i)) inp.disabled = true; 
  if (st.match(/r/i)) inp.readOnly = true; 
  d.appendChild(lab); 
  d.appendChild(inp); 
  this.fld.appendChild(d);
};

 
// n : symbolic name (and id) of element
// v : value/data contained in the element
Form.prototype.addHidden = function(n, v) {
  /*@cc_on @if (@_jscript) var inp = document.createElement("<input name='"+n+"'>"); @else */ 
  var inp = document.createElement("input"); 
  inp.name = n; /* @end @*/ 
  inp.type = 'hidden'; 
  inp.id = n; 
  inp.value = v; 
  this.fld.appendChild(inp);
}; 


// st: (d)isabled, (m)ultiple, st(y)le may be null string
Form.prototype.addSelect = function(l,n,sz,opt,st) {
  var d = document.createElement("div"); 
  var t = document.createTextNode(l+' '); 
  var lab2 = document.createElement("label"); 
  lab2.appendChild(t); /*@cc_on @if (@_jscript) var inp = document.createElement("<select name='"+n+"'>"); @else */ 
  var inp = document.createElement("select"); 
  inp.name = n; /* @end @*/ 
  inp.id = n; 
  lab2.htmlFor = n; 
  for (i = 0; i < opt.length; i++) {
    inp.options[i] = new Option(opt[i][0],opt[i][1]); 
    if (opt[i][2].match(/d/i)) inp.options[i].disabled = true; 
    if (opt[i][2].match(/s/i)) inp.options[i].selected = true;
  } 
  /* ppsh - start */
  var lab = lab2;
  if (st.match(/y/i)) {
    var lab = document.createElement("div");
    lab.id = l.replace(/\s/gi,"");
    lab.appendChild (lab2);
  }
  /* ppsh - end */      
  if (st.match(/d/i)) inp.disabled = true; 
  if (st.match(/m/i)) {
    inp.multiple = true; 
    if (sz == 1) sz = '';
  } 
  if (sz != '') inp.size=sz; 
  d.appendChild(lab); 
  d.appendChild(inp); 
  this.fld.appendChild(d);
}; 


// l : string text
// n : symbolic name (and id) of element
// p : panel ID
Form.prototype.addText = function(l, n, p) {
  var d = document.createElement("div"); 
  var t = document.createTextNode(l+' '); 
  d.appendChild(t);
  d.id = n;
  if (p != null) p.appendChild (d); 
    else this.fld.appendChild(d);
};


// p: panel name (panel used to group other elements)
Form.prototype.addPanel = function(p) {
  var d = document.createElement("div"); 
  d.name = p; d.id = p;
  this.fld.appendChild(d);
  return d;
};


// n: common name of radio button group
Form.prototype.getRadioValue = function(n) {
  var set = document.getElementsByName (n);
  for (var i=0; i < set.length; i++)
    if (set[i].checked) return set[i].value;
  return "";
}

// n: name of checkbox
// returns value if box is checked, "" otherwise
Form.prototype.getCheckboxValue = function (n) {
  var cb = document.getElementById (n);
  if (cb.checked) return cb.value;
  return "";
}


// n: name of checkbox group
// k: number of checkboxes in the group
// returns semicolon-separated string of selected checkbox values
Form.prototype.getCheckboxGroup = function (n,k) {
  var s = "";
  for (var i=1; i <= k; i++) {
    var v = this.getCheckboxValue (n + i);
    if (v != "") s += ";" + v;
  }
  return s.substr(1);
}


// i: id of <div> container hosting the form
Form.prototype.addForm = function(i) {
  this.F.appendChild(this.fld); 
  document.getElementById(i).appendChild(this.F);
};

//alert ("MyForm.js has been loaded");