﻿// JScript File
//<!--

var names = "Steve Buscemi Catherine Keener Dermot Mulroney Danielle Zerneck James LeGros Rica Martens Peter Dinklage Kevin Corrigan Hilary Gilford Robert Wightman Tom Jarmusch Michael Griffiths Matthew Grace Ryan Bowker Francesca DiMauro";
var blurb = "phpBB is a free, open source Internet community application, with outstanding discussion forums and membership management. Written in the PHP scripting language, and making use of the popular MySQL database, phpBB is a standard among web hosting companies throughout the world, and is one of the most widely-used bulletin board packages in the world. phpBB short-circuits the need for you to be a web development master in order to create and manage massive online communities";
var password = "secret";

//main fuinction to write data to all text fields
function fillerup(){
  var all_inputs    = document.getElementsByTagName('input');
  var all_selects   = document.getElementsByTagName('select');
  var all_textareas = document.getElementsByTagName('textarea');
  var inputname= '';

  // selects
  for (var i = 0, max = all_selects.length; i < max; i++) {
      var sel = all_selects[i]; // current element
      if (sel.selectedIndex != -1
          && sel.options[sel.selectedIndex].value) {
          continue; // has a default value, skip it
      }
      var howmany = 1; // how many options we'll select
      if (sel.type == 'select-multiple') { // multiple selects
          // random number of options will be selected
          var howmany = 1 + this.getRand(sel.options.length - 1);
      }
      for (var j = 0; j < howmany; j++) {
          var index = this.getRand(sel.options.length - 1);
          sel.options[index].selected = 'selected';
          // @todo - Check if the selected index actually
          //         has a value otherwise try again
      }
  }

  // textareas
  for (var i = 0, max = all_textareas.length; i < max; i++) {
      var ta = all_textareas[i];
      if (!ta.value) {
          ta.value = this.getRandomString(10)
                     + '\n\n'
                     + this.getRandomString(10);
      }
  }

  // inputs
  for (var i = 0, max = all_inputs.length; i < max; i++) {
      var inp = all_inputs[i];
      var type = inp.getAttribute('type');
      if (!type) {
          type = 'text'; // default is 'text''
      }
      if (type == 'checkbox') {
          // check'em all
          // who knows which ones are required
          inp.setAttribute('checked', 'checked');
          /* ... ooor random check-off
          if (!inp.getAttribute('checked')) {
              if (Math.round(Math.random())) { // 0 or 1
                  inp.setAttribute('checked', 'checked');
              }
          }
          */
      }

      if (type == 'radio') {

          var to_update = true;
          // we assume this radio needs to be checked
          // but in any event we'll check first

          var name = inp.name;
          var input_array = inp.form.elements[inp.name];
          for (var j = 0; j < input_array.length; j++) {
              if (input_array[j].checked) {
                  // match! already has a value
                  to_update = false;
                  continue;
              }
          }

          if (to_update) {
              // ok, ok, checking the radio
              // only ... randomly
              var index = this.getRand(input_array.length - 1);
              input_array[index].setAttribute('checked', 'checked');
          }
      }

      if (type == 'password') {
          if (!inp.value) {
              inp.value = this.getPassword();
          }
      }
      if (type == 'text') {
          if (!inp.value) {
              // try to be smart about some stuff
              // like email and name
              inputname = inp.name.toLowerCase();
              if (inputname.indexOf('name') != -1) { // name
                  inp.value = this.getRandomName() + ' ' + this.getRandomName();
              } else if (inputname.indexOf('email') != -1) { // email address
                  inp.value = replaceItem(this.getRandomString(1) + '@example.org', ',', '');
              } else if ((inputname.indexOf('url') != -1) || (inputname.indexOf('website') != -1)) { // url
                  inp.value = 'http://' + this.getRandomString(1) + '.com/';
              } else if (inputname.indexOf('phone') != -1) { // phone number
                  inp.value = '01234567';
              } else if (inputname.indexOf('postcode') != -1) { // phone number
                  inp.value = '1111';
              } else {
                  inp.value = this.getRandomString(1);
              }
          }
      }
  }
}
function getRandomString(how_many_words) {
  if (!how_many_words) {
      how_many_words = 5;
  }
  if (!this.words) {
      this.words = this.blurb.split(' ');
  }
  var retval = '';
  for (var i = 0; i < how_many_words; i++) {
      retval += this.words[this.getRand(this.words.length) - 1];
      retval += (i < how_many_words - 1) ? ' ' : '';
  }
  return retval;
}
function getRandomName() {
  if (!this.split_names) {
      this.split_names = this.names.split(' ');
  }
  return this.split_names[this.getRand(this.split_names.length) - 1];
}
function getPassword() {
  // always use the same password
  // in case there is a password confirmation
  if (!this.password) {
      this.password = 'secret';
  }
  return this.password;
}
function getRand(count) {
  return Math.round(count * Math.random());
}
function replaceItem(str, outval, inval){
	rExp = new RegExp(outval, "gi");
	results = str.replace(rExp, inval);
	return(results);
}
//-->