/*global document */
/*global window */

/*
@source: http://vnaum.com/stuff/corpogen/corpogen.js

@licstart  The following is the entire license notice for the
JavaScript code in this page.

Copyright (C) 2009 Vladislav Naumov

The JavaScript code in this page is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.  The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.

@licend  The above is the entire license notice
for the JavaScript code in this page.
*/

// pick single word
function pickword(type, banned, nocombos)
{
  var noun, doer, adj, edition, spref, spost, salt;
  noun = (
                     "Action Application Assistant Bit Business Byte Call Care " +
                     "Center Class Console Control Core Data Database " +
                     "Desk End Environment Genius Guard Help Image " +
                     "Knowledge Lab Layer Leaf Locker Master Matrix " +
                     "Media Module Net Office Page Part Path Point " +
                     "Portal Profile Quantum Query Root Safe Share Shell " +
                     "Software Solution Source Standard Station Force " +
                     "Structure System Target Track Trust Web " +
                     "Works Display Trace Service Host Code Name Value " +
                     "Repair Elite Platform Interface Team Studio Engine Sense"
    ).split(' ');

  doer = (
                     "Accelerator Action Agent Application Assistant " +
                     "Control Controller Creator Designer Environment " +
                     "Express Guard Helper Kit Module Installer " +
                     "Setup Software Solution Suite Tools Host Viewer " +
                     "Utility Manager Dispatcher Monitor Keeper " +
                     "Tracker Services Host Component Package Explorer " +
                     "Optimizer Platform Interface Pack Engine"
      ).split(' ');

  adj = (
                     "Active Advanced Best Bright Classic Clear Clean Compact " +
                     "Corporate Crystal Creative Database Easy Enterprise " +
                     "Express Extended Fast Final Front Genuine Golden High " +
                     "Instant Interactive Live Magic Management Master Modern " +
                     "Nice Portal Power Premium Quick Real Reliable Rich Root " +
                     "Safe Scalable Secure Simple Small Tiny Smart Stable " +
                     "Standard Strong Team Top Turbo Ultimate Ultra Visual " +
                     "Xtreme Managed Professional Optimal Elite Intelligent " +
                     "Digital Rapid Studio"
      ).split(' ');

  edition = (
                     "Business Classic Compact Core Corporate Enterprise Express " +
                     "Extended Final Free Mercury Micro Mobile Open Platinum " +
                     "Plus Premium Pro Silver Simple Standard Team Turbo " +
                     "Ultimate Ultra Visual Web Xtreme Value Professional " +
                     "Elite"
      ).split(' ');
  
  spref = (
                     "Auto Back Best  Bright Clean Clear Core Crypt Desk Double " +
                     "Easy Express Ez Fast Flexi Free Front Giga Gold Help High " +
                     "Info Inno IT Key Lab Live Macro Magic Master Mega Micro " +
                     "Middle Multi Open Power Pro Quad Quick Real Root Share " +
                     "Start Sure Top Zeta View Opti Team e- Extra Omni Intelli"
      ).split(' ');

  spost = (
                   "Auto Back Bright Core Crypt Easy Flex Free Front Guard Info " +
                   "Lab Macro Manage Micro Middle Plus Point Root Secure Shop " +
                   "Start Tech Top Tru Ware Works Assist View"
      ).split(' ');

  salt = "&trade; &reg; &copy;".split(' ');
  
  var word;
  word = null;
  while (word === null || banned[word.substr(0, 4).toLowerCase()])
  {
    switch(type)
    {
      case 'noun':
        word = noun[Math.floor(Math.random() * noun.length)];
        break;
      case 'doer':
        word = doer[Math.floor(Math.random() * doer.length)];
        break;
      case 'adj':
        word = adj[Math.floor(Math.random() * adj.length)];
        break;
      case 'edition':
        word = edition[Math.floor(Math.random() * edition.length)];
        break;
      case 'spref':
        word = spref[Math.floor(Math.random() * spref.length)];
        break;
      case 'spost':
        word = spost[Math.floor(Math.random() * spost.length)];
        break;
      default:
        word = type;
    }
  }

  banned[word.substr(0, 4).toLowerCase()] = 1;

  // That's it if we're not generating noun.
  // or if nocombos requested
  if (nocombos || type != 'noun')
  {
    return word;
  }
  
  var l = word.length;
  var lmin = 'IT'.length;
  var lmax = 'Professional'.length;
  if (Math.random() > (l - lmin) / (lmax - lmin) )
  {
    if (Math.random() < 0.5)
    {
      word = word + pickword('spost', banned, 1);
    }
    else
    {
      word = pickword('spref', banned, 1) + word;
    }
    // okay, it's a combo.
    // salt it with 0.5 probability:
    if (Math.random() < 0.5)
    {
      word = word + salt[Math.floor(Math.random() * salt.length)];
    }
  }
  return word;
}

// generate full name
function makecorpname()
{
  var templates = [
    "noun doer for noun", 
    "noun doer for adj noun", 
    "adj noun doer edition", 
    "edition noun doer",
    "adj noun edition"
  ];

  var rnd;
  rnd = Math.floor(Math.random() * templates.length);
  var tpl = templates[rnd].slice();

  var fin = [];
  var banned = [];

  var t = tpl.split(' ');
  var i;
  for(i = 0; i < t.length; i++)
  {
    // function pickword(type, banned, nocombos)
    fin.push( pickword(t[i], banned, 0) );
  }

  return fin.join(" ");
}

// onclick handler
function corpogen(divid)
{
  var num = document.getElementById('number').value;
  // alert("QQZ" + num);
  var div = document.getElementById(divid);
  var names = '';
  var i;
  for (i = 0; i < num; i++)
  {
    if (names)
    {
      names = names + "<br>";
    }
    names = names + makecorpname();
  }
  div.innerHTML = names;
}

// onkeypress handler
function submitenter(myfield,e)
{
  var keycode;
  if (window.event)
  {
    keycode = window.event.keyCode;
  }
  else if (e)
  {
    keycode = e.which;
  }
  else
  {
    return true;
  }

  if (keycode == 13)
  {
    corpogen('names');
    return false;
  }
  else
  {
    return true;
  }
}

