//-- Set up the word lists.

// Nouns
noun1 = [
	"involvement",
	"incentive",
	"participation",
	"issue",
	"process",
	"package",
	"dialogue",
	"initiative",
	"scheme",
	"approach",
	"project",
	"action",
	"collaboration",
	"objective",
	"impact",
	"methodology",
	"linkage",
	"partnership",
	"needs-assessment",
	"method",
	"infrastructure",
	"structure",
	"function",
	"funding",
	"solution",
	"mission",
	"staff",
	"training",
	"monitoring",
	"evaluation",
	"workshop",
	"proposal",
	"investment",
	"assessment",
	"strategy",
	"sustainability",
	"income-generation",
	"staffing",
	"leadership",
	"capacity",
	"planning",
	"trainer",
	"evaluator",
	"partner",
	"baseline",
	"indicator",
	"tool",
	"agribusiness",
	"development",
	"paradigm"
];

// Type I ("reversible") adjectives.
//  Example: The adjective "intensive" can be combined with the noun "system"
//	in two ways:
//		intensive system (a noun)
//	or
//		system-intensive (a Type II adjective)	
adj1 = [
	"motivated",
	"positive",
	"structured",
	"controlled",
	"integrated",
	"balanced",
	"functional",
	"programmed",
	"mobilized",
	"limited",
	"unlimited",
	"phased",
	"delegated",
	"maximized",
	"consistent",
	"intensive",
	"backed",
	"funded",
	"targeted",
	"sponsored",
	"monitored",
	"based",
	"evaluated",
	"implemented",
	"organized",
	"optimized",
	"trained",
	"staffed",
	"oriented",
	"sensitive"
];


// Type II ("irreversible") adjectives.
//  Example: The adjective "large" can be combined with the noun "system"
//	as:
//		large system (a noun)
//	but *not* 
//		system-large 	
adj2 = [
	"grassroots",
	"sectoral",
	"institutional",
	"urban",
	"organizational",
	"rural",
	"growth-oriented",
	"participatory",
	"cooperative",
	"on-going",
	"technical",
	"cross-cultural",
	"agrarian",
	"agricultural",
	"logistical",
	"gender-based",
	"sustainable",
	"practical",
	"feasible",
	"effective",
	"humanitarian",
	"bilateral",
	"multilateral"
];
// These adverbs are used to modify the adjectives above (types I and II)
adv1 = [
	"economically",
	"centrally",
	"rationally",
	"systematically",
	"formally",
	"fundamentally",
	"strategically",
	"dynamically",
	"democratically",
	"situationally",
	"moderately",
	"intensively",
	"comprehensively",
	"radically",
	"optimally",
	"locally",
	"globally",
	"nationally",
	"uniquely",
	"unequivocally",
	"programmatically",
	"theoretically"
];

institution1 = [
	"CBO",
	"NGO",
	"UNDP",
	"donor",
	"community",
	"WHO",
	"World Bank",
	"UN",
	"UNHCR",
	"UNICEF",
	"UNESCO",
	"government",
	"developing-nation",
	"North/South",
	"AID",
	"enterprise"
];

w7a = [
	" and",
	" or",
	" and/or",
	", without",
	", unlike",
	", as opposed to",
	", instead of"
];

q1 = [
	"The",
	"Some",
	"All",
	"Most",
	"It is important to note that",
	"Keep in mind that",
	"Others have observed that",
	"The prevailing view is that",
	"Conventional wisdom holds that",
	"It is obvious that",
	"Obviously,",
	"Common sense dictates that", 
	"Previous studies have shown that" ,
	"It is widely accepted that" ,
	"No one can deny that" ,
	"It would be sheer folly to deny that" ,
	"Often overlooked is the fact that" ,
	"It is noteworthy that" ,
	"It can hardly be stressed enough that" 
];

q1b = [
	"However,",
	"Moreover,",
	"Furthermore,",
	"Indeed,",
	"Of course,",
	"This implies that",
	"There is little doubt that",
	"Regardless,",
	"In fact,",
	"What's more,",
	"In other words,",
	"Surprisingly,",
	"Not surprisingly,",
	"This really should come as no surprise, since",
	"It should come as no surprise that",
	"Common sense also dictates that",
	"It goes without saying that",
];

q2b = [
	"Nevertheless,",
	"We cannot ignore the possibility that",
	"Thus,",
	"In short,",
	"To put it bluntly,",
	"Therefore,",
	"Notwithstanding specious claims to the contrary,",
	"Despite these facts, however,",
	"Unfortunately,",
	"Ultimately, however,",
	"In the final analysis, however,",
	"Despite these claims, however,",
	"These observations notwithstanding, however, it should be noted that",
	"To summarize:",
	"Additional funding is needed in order to establish definitively that",
	"It is thus vitally important to bear in mind that,",
	"In short,"
];

v1 = [
	"",
	"can ",
	"should ",
	"will ",
	"must ",
	"ought to ",
	"may ",
	"could "
];

v1a = [
	"",
	"occasionally",
	"always",
	"never",
	"sometimes",
	"indeed",
	"only",
	"inevitably"
];

// Transitive verbs
v2 = [
	"integrate with",
	"oversee",
	"implement",
	"monitor",
	"evaluate",
	"liaise with",
	"facilitate",
	"mobilize",
	"impact on",
	"comply with",
	"expand into",
	"undermine",
	"prioritize",
	"democratize",
	"delegate",
	"couple with",
	"establish linkage with",
	"decouple from",
	"sponsor",
	"interact with",
	"interface with",
	"recruit"
];
//-- Generate a random number between 0 and n-1.
// I.e., flips an n-sided coin.
function randomNum(n) {
	rNum=-1;
	while(rNum < 0) {
		rNum=Math.floor(Math.random()*(n));
	}

// This doesn't work on some browsers, for some reason:
//  rNum=NaN
//  while (isNaN(rNum)) {
//    rNum=Math.floor(Math.random()*(n))
//  }
  return rNum;
}

// returns 0 or 1
function FlipCoin() {
	return randomNum(2);
}

// Construct a noun phrase of up to 'nouncount' nouns
// each noun may be preceded by up to maxadj adjectives
function nounPhrase(nouncount, maxadj) {
	thePhrase = "";
	if (nouncount < 1)
		return thePhrase;
	switch(nouncount) {
	case 1:
		thePhrase += BuildANoun(maxadj);
		break;
	case 2:
		thePhrase += BuildANoun(maxadj) + " and " + BuildANoun(maxadj);
		break;
	default:
		for (i=0;i < nouncount-1;i++) {
			thePhrase += BuildANoun(maxadj) + ", "
		}
		thePhrase += "and " + BuildANoun(maxadj);
		break;
	}
	return thePhrase;
}

// Construct an adjectivial phrase of up to 'adjcount' adjectives
function adjPhrase(adjcount) {
	thePhrase = "";
	if (adjcount < 1)
		return thePhrase;
		
	switch(adjcount) {
	case 1:
		thePhrase = getAnAdj();
		break;
	case 2:
		thePhrase = getAnAdj() + " and " + getAnAdj();
		break;
	default:
		for (i=0;i < adjcount-1;i++) {
			thePhrase += getAnAdj() + ", ";
		}
		thePhrase += "and " + getAnAdj();
		break;
	}
	return thePhrase;
}

function getAnAdj() {
	theAdj="";
	if (FlipCoin())
		theAdj += adv1[randomNum(adv1.length)] + " ";
		
	if (FlipCoin()) 
		theAdj += adj2[randomNum(adj2.length)] ;
	else
		theAdj += PickANoun() + "-" + adj1[randomNum(adj1.length)];
	return theAdj;
}

// Produce a suitable prefix.
// If 'max' is large, the prefixes will rarely appear.
// If it's 1 they'll *always* appear. 5 seems about right.
function MicMac(max) {
	micmac = "";
	if (max < 1)
		max = 1;
	if (randomNum(max) == 0) {
		if (FlipCoin()) 
			micmac = "macro-";
		else
			micmac = "micro-";
	}
	return micmac;
}

function PickANoun() {
	theNoun = "";
	theNoun += MicMac(5);
	theNoun += noun1[randomNum(noun1.length)];
	return theNoun;
}

// returns a "noun" consisting of an adjective phrase (containing up to maxadj adjectives)
// plus a noun
function BuildANoun(maxadj) {
	theNoun = adjPhrase(1+randomNum(maxadj)) + " " + PickANoun();
	return theNoun;
}

//--Function to generate the random profundity.
function paragraph() {
	msg = q1[randomNum(q1.length)] + " " + pithyThought() + " ";
	
	n=randomNum(4) + 1; // number of additional sentences
	while (n--) {	
		paren = FlipCoin();
		if (paren) 
			msg += "(";
		msg += q1b[randomNum(q1b.length)] + " " + pithyThought();
		if (paren)
			msg += ")";
		msg += " ";
		
		msg += q2b[randomNum(q2b.length)] + " " + pithyThought() + " ";
	}
	return msg;
}

// Cheapo widget to pluralize nouns
function pluralize(aString) {
	result = "";
	len = aString.length;
	lastChar = aString.substring(len-1,len);
	if (lastChar == "h" || lastChar == "s" || lastChar == "x")
		result = aString + "es";
	else if (lastChar == "z") //(works for "quiz" but not "fizz")
		result = aString + "zes";
	else if (lastChar == "y") {
		result = aString.substring(0,len-1);
		result += "ies";
	}
	else
		result = aString + "s";
	return result;
}

function pithyThought() {
	msg = "";
	aPhrase = pluralize(nounPhrase(1,3)) ;
	msg += aPhrase + " ";
	if (randomNum(5) == 1) // flip a coin
		msg += "(" + acronymize(aPhrase) + "'s) ";
	msg += v1[randomNum(v1.length)];
	msg += v1a[randomNum(v1a.length)] + " ";
	msg += v2[randomNum(v2.length)] + " ";
	aPhrase = pluralize(nounPhrase(1,3)) ;
	msg += aPhrase ;
	if (randomNum(5) == 1) // flip a coin
		msg += " (" + acronymize(aPhrase) + "'s)";
	msg +=w7a[randomNum(w7a.length)] + " ";
	msg +=institution1[randomNum(institution1.length)]+ "-" + adj1[randomNum(adj1.length)]+" ";
	msg += pluralize(nounPhrase(1,3)) ;
	msg += ".";
	return msg;
}

// Create an acronym for a phrase
function acronymize(it) {
	result = "";
	len = it.length;
	if (len <= 0)
		return result;
	result = it.substring(0,1).toUpperCase();
	i = 1;
	doNext = false;
	while (i < len) {
		if (doNext==true)
			result += it.substring(i,i+1).toUpperCase();
		doNext = false
		if (it.substring(i,i+1) == " " || it.substring(i,i+1) == "-")
			doNext = true;
		i++	;
	}
	return result	;
}

// Capitalize every letter that follows a space
function titleCaps(it) {
	result = "";
	len = it.length;
	result = it.substring(0,1).toUpperCase();
	i = 1;
	capNext = false;
	while (i < len) {
		if (capNext == true)
			result += it.substring(i,i+1).toUpperCase();
		else
			result += it.substring(i,i+1);
		if (it.substring(i,i+1) == " ")
			capNext = true;
		else
			capNext=false;
		i++	;
	}
	return result;
}

function catchyTitle() {
	msg = "";
	msg += "<" + "center>";
	msg += "<" + "p><" + "i>Proceedings of the Society for the Advancement of Obfuscation in International Development<" +"br>";
	msg += "Vol " + (1 + randomNum(100)) + ", Number " + (1 + randomNum(52)) + ", p. " + (1 + randomNum(200)) + "<" +"\/i>";
	msg += "<" +"h1>";
	it = nounPhrase(1,2);
	msg += pluralize(titleCaps(it)) ;
	if (FlipCoin()) 
		msg += " (" + acronymize(it) + "'s)";
	msg += ":<" +"br>";
	msg += titleCaps(PickANoun()) + " or " + titleCaps(PickANoun()) + "?<" + "\/h1>";
	msg += "<" + "h2>A Case Study<\/h2>";
	msg += "<" + "FORM>";
	msg += "<" + "INPUT type = \"button\" value = \"Read a related article\" onClick = \"history.go(0)\")"; // (this will simply reload the page)
	msg += "<" + "\/FORM>";
	msg += "<" + "p><" + "br><" + "p>";
	msg += "<" + "\/center>";
	return msg;
}


function DoIt() {
	document.write(catchyTitle());
	pCount = randomNum(5)+4;
	while (pCount--)
		document.write("<" +"p>"+ paragraph() + "<" + "\/p>");
	document.close();	
}


DoIt();
